diff options
Diffstat (limited to 'test/test_data.lua')
| -rw-r--r-- | test/test_data.lua | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/test/test_data.lua b/test/test_data.lua new file mode 100644 index 0000000..08c20f9 --- /dev/null +++ b/test/test_data.lua @@ -0,0 +1,39 @@ +-- Parser checks for lib/data.lua. +-- Run from the repo root: lua test/test_data.lua +-- Parsers are what break silently on a kernel or hardware change, so they are +-- what gets a test. Everything else in this project is verified by screenshot. + +package.path = './?.lua;' .. package.path +local data = require 'lib.data' + +local function read(path) + local f = assert(io.open(path, 'r')) + local s = f:read('*a') + f:close() + return s +end + +-- cpu_times: total and idle jiffies from the aggregate "cpu " line. +local total, idle = data.cpu_times(read('test/fixtures/proc_stat')) +-- 209094+2199+135469+11604898+24408+0+946 = 11977014 +assert(total == 11977014, 'cpu total, got ' .. tostring(total)) +-- idle field is the 4th value, 11604898; iowait (24408) counts as idle too +assert(idle == 11629306, 'cpu idle, got ' .. tostring(idle)) + +-- mem_info: values in kB, as the file gives them. +local mem = data.mem_info(read('test/fixtures/proc_meminfo')) +assert(mem.total == 31943076, 'mem total, got ' .. tostring(mem.total)) +assert(mem.available == 25627088, 'mem available, got ' .. tostring(mem.available)) +-- used is total minus available, which is what a user means by "used" +assert(mem.used == 6315988, 'mem used, got ' .. tostring(mem.used)) + +-- millidegrees: hwmon temp*_input is millidegrees C, rounded to whole degrees. +assert(data.millidegrees(read('test/fixtures/temp1_input')) == 53, + 'temp, got ' .. tostring(data.millidegrees(read('test/fixtures/temp1_input')))) +-- A missing or unreadable sensor must yield nil, not an error and not 0: +-- 0 degrees is a plausible reading and would be indistinguishable from failure. +assert(data.millidegrees(nil) == nil, 'nil input must give nil') +assert(data.millidegrees('') == nil, 'empty input must give nil') +assert(data.millidegrees('garbage') == nil, 'unparseable input must give nil') + +print('test_data: all assertions passed') |
