diff options
author | Asbjørn Sloth Tønnesen <ast@2e8.dk> | 2019-07-16 19:31:51 +0000 |
---|---|---|
committer | Asbjørn Sloth Tønnesen <ast@2e8.dk> | 2019-07-16 19:31:51 +0000 |
commit | a25b3f729116c5fc9d33233e6636f59770908a1a (patch) | |
tree | f44d78763b09b7b557f852a8e50b055eb88fe49d | |
parent | 1f23598daf191162a472f14f5e39157595ab9b48 (diff) | |
download | lua-inet-a25b3f729116c5fc9d33233e6636f59770908a1a.tar.gz lua-inet-a25b3f729116c5fc9d33233e6636f59770908a1a.tar.xz lua-inet-a25b3f729116c5fc9d33233e6636f59770908a1a.zip |
tests: test the examples in the readme
Signed-off-by: Asbjørn Sloth Tønnesen <ast@2e8.dk>
-rw-r--r-- | test/all.lua | 1 | ||||
-rw-r--r-- | test/readme.lua | 116 |
2 files changed, 117 insertions, 0 deletions
diff --git a/test/all.lua b/test/all.lua index 4ee056a..c3a1637 100644 --- a/test/all.lua +++ b/test/all.lua @@ -1,4 +1,5 @@ local all = require('test').new() all:depend('inet') all:depend('inet_set') +all:depend('readme') return all diff --git a/test/readme.lua b/test/readme.lua new file mode 100644 index 0000000..17f8312 --- /dev/null +++ b/test/readme.lua @@ -0,0 +1,116 @@ +local lpeg = require 'lpeg' +local test = require 'test' +local io = require 'io' + +local format = string.format +local pack = table.pack +local concat = table.concat + +local readme_parser +do + local P = lpeg.P + local Ct = lpeg.Ct + local C = lpeg.C + local Cc = lpeg.Cc + + local sp = P(' ') + local eq = P('=') + local nl = P('\n') + local div = P(' -- returns ') + local non_nl = P(1)-nl + + local example = Ct(Cc('example') * C(((P(1)-(div+nl))^1)) * div * C(non_nl^1)) + local assign_left = P('local ')^-1 * C(((P(1)-((sp^0 * eq)+nl))^1)) + local assign_mid = sp^1 * eq * sp^1 + local assign_right = C(non_nl^1) + local assignment = Ct(Cc('assignment') * assign_left * assign_mid * assign_right) + local indented_line = sp^2 * (example + assignment) + local anyline = non_nl^0 + local line = indented_line + anyline + + readme_parser = Ct((line * nl)^0 * line^-1 * -1) +end + +local env = { + tostring = tostring, + require = require, +} + +local function run(name, code) + local f = assert(load(code, name, 't', env)) + local ret = pack(pcall(f)) + if ret[1] then + return ret + else + print() + print('code:', code) + print('error:', ret[2]) + print() + return { true, nil, n=3 } + end +end + +local function run_example(name, code) + return run(name, format('return %s', code)) +end + +local function pack2str(t) + local new = {} + local n = t.n + for i=2,n do + local v = t[i] + local vt = type(v) + if vt == 'nil' then + new[i] = 'nil' + else + new[i] = format('%s "%s"', vt, v) + end + end + return concat(new, ', ', 2, n) +end + +local function compare_packs(a, b) + local n = a.n + if n ~= b.n then return false end + for i=1,n do + local va = a[i] + local vb = b[i] + local vat = type(va) + local vbt = type(vb) + if vat ~= vbt then return false end + if va ~= vb then return false end + end + return true +end + +local function hdl_assignment(line) + local code = format('%s = %s', line[2], line[3]) + run('assignment', code) +end + +local function hdl_example(line) + local t1 = line[2] + local t2 = line[3] + local r1 = run_example('left side', t1) + local r2 = run_example('right side', t2) + local errmsg = format('"%s" returns %s, not %s', t1, pack2str(r1), pack2str(r2)) + assert(compare_packs(r1, r2), errmsg) +end + +local handlers = { + assignment = hdl_assignment, + example = hdl_example, +} + +local function readme_test() + local data = assert(io.open('README.rst', 'r')):read('*a') + local lines = assert(readme_parser:match(data)) + for i=1,#lines do + local line = lines[i] + local kind = line[1] + local handler = handlers[kind] + handler(line) + end +end + +return test.new(readme_test) |