Tue, 27 Oct 2015 19:36:17 +0000
scansion.generator: Initial basic lib to generate test scripts
scansion/generator.lua | file | annotate | diff | comparison | revisions |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scansion/generator.lua Tue Oct 27 19:36:17 2015 +0000 @@ -0,0 +1,54 @@ +local it = require "scansion.iterators"; + +local script_methods = {}; + +local excluded_fields = { name = true, defined_line = true, type = true }; +function script_methods:client(data) + self:_append("["..data.name.."]"); + for k, v in pairs(data) do + if not excluded_fields[k] then + self:_append("", k..": "..v); + end + end + self:_append(""); +end + +function script_methods:action(name, verb, data) + if self.in_header then + self.in_header = false; + self:_append("----------"); + self:_append(""); + end + + self:_append(name.." "..verb..(data and ":" or "")); + if data then + for s in tostring(data):gmatch("[^\n]+") do + self:_append("", s); + end + end + self:_append(""); +end + +function script_methods:_append(...) + local fields = { ... }; + for i = 1, select("#", ...) do + fields[i] = tostring(fields[i]); + end + table.insert(self.lines, table.concat(fields, "\t")); +end + +function script_methods:lines() + return it.values(self.lines); +end + +function script_methods:render() + return table.concat(self.lines, "\n"); +end + +local script_mt = { __index = script_methods }; + +return { + new_script = function () + return setmetatable({ in_header = true, n_clients = 0, clients = {} }, script_mt); + end; +}