Skip to content

Instantly share code, notes, and snippets.

@randrews
Created March 9, 2011 03:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save randrews/861635 to your computer and use it in GitHub Desktop.
Save randrews/861635 to your computer and use it in GitHub Desktop.
lt - a very light Lua test utility
-- print("Setup code goes here")
test("always passes",
function()
assert(true, "Yay!")
end)
test("always fails",
function()
assert(false, "Boo.")
end)
test("a new feature", "pending",
function()
assert(false, "I will get ignored")
end)
#!/usr/bin/lua
local count = 0
local errors = {} -- List of names and messages of all failing tests
local pending = 0
function test(name, fn)
count = count + 1
if fn == "pending" or fn == nil then -- A pending test
pending = pending + 1
io.write("*")
elseif type(fn) == "function" then -- A real test
local status, err = pcall(fn) -- Run the function
if status then
io.write(".")
else
table.insert(errors, { name = name, message = err })
io.write("F")
end
else -- Who knows?
io.write("?")
table.insert(errors, { name = name, message = "Expected a function" })
end
end
function lt_test_file(filename)
local status, err = pcall(dofile,filename)
if not status then -- Syntax error in the file, bail
print(string.format("\nError loading %s:\n\t%s\n", filename, err))
end
end
function lt_summary()
io.write(string.format("\n\n%d tests, %d failed, %d pending\n\n", count, #errors, pending))
for n, err in ipairs(errors) do
print(string.format("%s failed:\n\t%s\n", err.name, err.message))
end
end
-- Run all filenames passed as arguments
for i, file in ipairs(arg) do
lt_test_file(file)
end
lt_summary()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment