Skip to content

Commit

Permalink
Add index() and extract() to tester module.
Browse files Browse the repository at this point in the history
index(paths) recursively indexes the tests in paths.
extract(test,[fn]) extracts the test to filename fn.
  • Loading branch information
ladc committed Feb 21, 2016
1 parent 46e1fea commit a4ec002
Showing 1 changed file with 60 additions and 3 deletions.
63 changes: 60 additions & 3 deletions tester.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
local lfs = require("lfs")
local tconcat = table.concat

--- Shallow table copy
local function cp(t)
local t1 = {}
Expand Down Expand Up @@ -33,8 +36,6 @@ local function parse_test(fn)
local current_test = prelude
local function store_test()
if current_test and current_test~=prelude then
current_test.code = table.concat(current_test.code,"\n")
current_test.description = table.concat(current_test.description,"\n")
tests[#tests+1] = current_test
lookup_tests[current_test.name] = #tests
end
Expand Down Expand Up @@ -70,7 +71,63 @@ local function parse_test(fn)
return tests, lookup_tests, prelude
end

-- Recursively parse the path and store the tests in test_index
-- @param path string containing a path to a file or directory
-- @param test_index array containing the tables with {tests,lookup,prelude}
-- @param test_index_lookup lookup table containing keys = paths of the test
-- files and values = respective indices in test_index
local function recursive_parse(path,test_index,test_index_lookup)
print(path)
local att = lfs.attributes(path)
if att.mode=="directory" then
for path2 in lfs.dir(path) do
if path2~=".." and path2~="." then
recursive_parse(path.."/"..path2,test_index,test_index_lookup)
end
end
elseif att.mode=="file" then
local tests, tests_lookup, prelude = parse_test(path)
if tests then
test_index[#test_index+1] = {tests=tests, lookup=tests_lookup, prelude=prelude}
test_index_lookup[path] = #test_index
end
end
end

--- Walk the directory tree and index tests.
-- @param paths array of paths that should be indexed.
-- @return array of tables containing { tests, lookup, prelude } as returned by
-- parse
-- @return lookup table containing keys = paths of the test files and values =
-- respective indices in test_index
local function index_tests(paths)
local test_index = {}
local test_index_lookup = {}
for _,path in ipairs(paths) do
recursive_parse(path,test_index,test_index_lookup)
end
return test_index, test_index_lookup
end

--- Extract a test into a directory
--@param test the test table returned by parse, containing name, description, tags, code
--@param fn string containing filename where the test should be extracted (default:os.tmpname())
--@return filename to where the test was extracted
local function extract_test(test,fn)
fn = fn or os.tmpname()
local f = io.open(fn,"w")
if not f then
error("Could not write to file "..fn)
end
f:write("--- ",test.name,"\n")
f:write("--",tconcat(test.description,"\n--"),"\n")
f:write(tconcat(test.code,"\n"))
f:close()
return fn
end

return {
parse = parse_test
parse = parse_test,
index = index_tests,
extract = extract_test,
}

0 comments on commit a4ec002

Please sign in to comment.