Skip to content

Commit

Permalink
Merge pull request #6 from ladc/master
Browse files Browse the repository at this point in the history
Test runner and test examples in declarative style 
As this simply puts thing in a side directory to test out, I think it's good to get it into the repo so we can start playing with it.
  • Loading branch information
Wiladams committed Mar 4, 2016
2 parents a273241 + 581a474 commit 634b212
Show file tree
Hide file tree
Showing 7 changed files with 424 additions and 30 deletions.
86 changes: 86 additions & 0 deletions experimental/test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
-- test.lua
--
-- Recursively run tests in the given paths, filtered by the given tags.
-- Failed tests are extracted to the directory "failed_tests", and error
-- details are appended.

local t = require("tester")
local tconcat, iowrite = table.concat, io.write
local tagpat_inc = "^%+([_%a][_%w]*)$"
local tagpat_exc = "^%-([_%a][_%w]*)$"
local paths, tags_inc, tags_exc = {}, {}, {}
local verbose = false
local arg = arg or {...}
local runcmd
local function print_help()
io.write[[
Usage: luajit test.lua path1 path2 +tagtofilter +orthistag -tagtoexclude -andthistag
Options:
path Recursively include tests in path. Default: test
+tag_inc Include only tests with tag tag_inc. Multiple include tags are ORed.
-tag_exc Exclude tests with tag tag_exc.
--help Print this help message.
--verbose Explicitly list all tests.
--runcmd='cmd'
If runcmd is defined, Command to run the tests with, externally, e.g.
--runcmd="luajit -joff"
]]
end

for _,a in ipairs(arg) do
if not a:match("^[+-]") then
paths[#paths+1]=a
elseif a:match(tagpat_inc) then
tags_inc[#tags_inc+1]=a:match(tagpat_inc)
elseif a:match(tagpat_exc) then
tags_exc[#tags_exc+1]=a:match(tagpat_exc)
elseif a:match("%-%-verbose") then
verbose = true
elseif a:match("%-%-help") then
print_help()
return
elseif a:match("%-%-runcmd") then
runcmd = a:match("%-%-runcmd=(.*)")
end
end

if #paths==0 then paths[1]="test" end
iowrite("Running tests in \"",tconcat(paths,"\",\""),"\"")
if #tags_inc > 0 then
iowrite(" with tags ",tconcat(tags_inc,","))
end
if #tags_exc > 0 then
iowrite(" but without tags ",tconcat(tags_exc,","))
end
iowrite("\n")

local index = t.index(paths)
index = t.filter(index,tags_inc,tags_exc)
local pass, fail, failed_tests, errors = t.run(index,verbose,runcmd)

iowrite("Passed ",pass,"/",pass+fail," tests.\n")
if fail==0 then return end

local function report_fail(test,err)
local extfn = t.extract(test,"failed_tests")
iowrite("----------------------------------------------------------",
"\nname: ",test.name,
"\nerror: ",err or "(no error message)",
"\nsource file: ",test.fn,
"\nextracted to file: ",extfn,
"\n")
-- Append error message for completeness
local extf = io.open(extfn,"a")
if extf then
extf:write("\n--[===[\nTest failed at ",os.date(),
"\nruncmd: ",runcmd or "(internal pcall)",
"\nerror:\n",err or "(no error message)",
"\n]===]\n")
end
end

iowrite("Failed tests:\n")
for i,failed_test in ipairs(failed_tests) do
report_fail(failed_test,errors[i])
end

11 changes: 11 additions & 0 deletions test/misc/select.lua → experimental/test/libs/core/select.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
-- Tests for the basic function select().
-- +select +fast

--- select #
-- Test whether select("#", 3, 4) returns the correct number of arguments.
do
local x = 0
for i=1,100 do
Expand All @@ -7,6 +11,8 @@ do
assert(x == 200)
end

--- select modf
-- Test whether select("#", func()) also works with func returning multiple values
do
local x = 0
for i=1,100 do
Expand All @@ -15,6 +21,7 @@ do
assert(x == 200)
end

--- select 1
do
local x = 0
for i=1,100 do
Expand All @@ -23,6 +30,7 @@ do
assert(x == 5050)
end

--- select 2
do
local x, y = 0, 0
for i=1,100 do
Expand All @@ -33,6 +41,7 @@ do
assert(x == 5050 and y == 6050)
end

--- select vararg #
do
local function f(a, ...)
local x = 0
Expand All @@ -50,6 +59,7 @@ do
end
end

--- select vararg i
do
local function f(a, ...)
local x = 0
Expand All @@ -68,6 +78,7 @@ do
end
end

--- select vararg 4
do
local function f(a, ...)
local x = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
local tinsert = table.insert
local assert = assert

--- table.insert(t,i)
do
local t = {}
for i=1,100 do t[i] = i end
for i=1,100 do tinsert(t, i) end
assert(#t == 200 and t[100] == 100 and t[200] == 100)
end

--- table.insert(t,i,i)
do
local t = {}
for i=1,200 do t[i] = i end
Expand Down
41 changes: 11 additions & 30 deletions test/misc/table_misc.lua → experimental/test/libs/table/misc.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

-- ABC elim
-- ABC elim
-- +opt +abc
do
local s, t = {}, {}
for i=1,100 do t[i] = 1 end
Expand All @@ -9,7 +9,8 @@ do
for i=1,n do s[i][i] = i end
end

-- TSETM
--- TSETM
-- Initialize table with multiple return values
do
local function f(a,b,c)
return a,b,c
Expand Down Expand Up @@ -41,25 +42,17 @@ do
assert(t[255] == 1 and t[256] == 2 and t[257] == 3 and t[258] == 4 and t[259] == nil)
end

--- TSETM 2
-- Initialize table with function returning 2 constant return values
do
local function f() return 9, 10 end
local t
for i=1,100 do t = { 1, 2, 3, f() } end
assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 9 and t[5] == 10 and
t[6] == nil)
end

-- table.new
do
local tnew = require("table.new")
local x, y
for i=1,100 do
x = tnew(100, 30)
if i == 90 then y = x end
end
assert(x ~= y)
end

-- table.concat
--- table.concat
do
local t = {a=1,b=2,c=3,d=4,e=5}
t[1] = 4
Expand All @@ -82,7 +75,7 @@ do
assert(q[93] == "9x8x7")
end

-- table.concat must inhibit CSE and DSE
--- table.concat must inhibit CSE and DSE
do
local t = {1,2,3}
local y, z
Expand All @@ -95,6 +88,7 @@ do
assert(z == "1x100x3")
end

--- table.concat must inhibit CSE and DSE 2
do
local y
for i=1,100 do
Expand All @@ -106,6 +100,7 @@ do
assert(y == "1x4x3")
end

--- table.concat must inhibit CSE and DSE 3
do
local t = {[0]={}, {}, {}, {}}
for i=1,30 do
Expand All @@ -115,17 +110,3 @@ do
end
end

-- table.pack
if os.getenv("LUA52") then
local t

t = table.pack()
assert(t.n == 0 and t[0] == nil and t[1] == nil)

t = table.pack(99)
assert(t.n == 1 and t[0] == nil and t[1] == 99 and t[2] == nil)

t = table.pack(nil, nil, nil)
assert(t.n == 3 and t[0] == nil and t[1] == nil and t[2] == nil and t[3] == nil and t[4] == nil)
end

Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@
local tremove = table.remove
local assert = assert

--- table.remove(t) removes correct entries
do
local t = {}
for i=1,200 do t[i] = i end
for i=1,100 do tremove(t) end
assert(#t == 100 and t[100] == 100)
end

--- table.remove(t) returns the removed entry
do
local t = {}
for i=1,200 do t[i] = i end
for i=1,100 do assert(tremove(t) == 201-i) end
assert(#t == 100 and t[100] == 100)
end

--- table.remove(t, 1) removes and returns the first entry
do
local t = {}
for i=1,200 do t[i] = i end
Expand Down
37 changes: 37 additions & 0 deletions experimental/test/libs_ext/table.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--- table.new
do
local tnew = require("table.new")
local x, y
for i=1,100 do
x = tnew(100, 30)
if i == 90 then y = x end
end
assert(x ~= y)
end

--- table.pack()
-- +lua52
do
if os.getenv("LUA52") then
local t = table.pack()
assert(t.n == 0 and t[0] == nil and t[1] == nil)
end
end

--- table.pack(99)
-- +lua52
do
if os.getenv("LUA52") then
local t = table.pack(99)
assert(t.n == 1 and t[0] == nil and t[1] == 99 and t[2] == nil)
end
end

--- table.pack(nils)
-- +lua52
do
if os.getenv("LUA52") then
local t = table.pack(nil, nil, nil)
assert(t.n == 3 and t[0] == nil and t[1] == nil and t[2] == nil and t[3] == nil and t[4] == nil)
end
end
Loading

0 comments on commit 634b212

Please sign in to comment.