Skip to content

Commit

Permalink
Implement @key: value matching in parse() function of tester module
Browse files Browse the repository at this point in the history
  • Loading branch information
ladc committed Feb 27, 2016
1 parent 0d975fa commit 8859058
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions tester.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ local function tags_from_path(fn)
return tags
end

-- Patterns for parsing out test data.
local identifier_pat = "([_%a][_%w]*)"
local delim_pat = "^%-%-%-"
local name_pat = delim_pat.."%s*(.*)"
local desc_pat = "^%-%-(.*)"
local tag_pat = "%+"..identifier_pat
local kv_pat = "@"..identifier_pat.."%s*:%s*([^@]*)"

--- Parser for the tests.
-- @param fn string containing a filename (fullpath)
-- @return array containing test specs (name, code, description, tags)
Expand All @@ -39,10 +47,10 @@ local function parse_test(fn)
lookup[current_test.name] = #tests
end
for line in io.lines(fn) do
if line:match("^%-%-%-") then
if line:match(delim_pat) then
-- Next test is reached
store_test()
local testname = line:match("^%-%-%-%s*(.*)$")
local testname = line:match(name_pat)
if lookup[testname] then
error("Test "..testname.." is defined twice in the same file. Please give the tests unique names.")
end
Expand All @@ -54,12 +62,15 @@ local function parse_test(fn)
tags = cp(prelude.tags),
code = cp(prelude.code),
}
elseif line:match("^%-%-") and #current_test.code==#prelude.code then
-- Continue building current test description (and possibly tags)
current_test.description[#current_test.description+1] = line:match("^%-%-%s*(.*)$")
for tag in line:gmatch("%+([A-Za-z_][A-Za-z0-9_]*)") do
elseif line:match(desc_pat) and #current_test.code==#prelude.code then
-- Continue building current test description, and possibly extract tags and key-value pairs
current_test.description[#current_test.description+1] = line:match(desc_pat)
for tag in line:gmatch(tag_pat) do
current_test.tags[tag] = true
end
for key,value in line:gmatch(kv_pat) do
current_test[key] = value:gsub("%s*$","")
end
else
current_test.code[#current_test.code+1] = line
end
Expand Down

0 comments on commit 8859058

Please sign in to comment.