Created
August 12, 2008 06:24
-
-
Save speedmax/5013 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
spec = { | |
specs = {}, passed = 0, failed = 0, current = nil | |
} | |
function spec.report(success, message) | |
spec.current.success = success | |
if success then | |
spec.passed = spec.passed + 1 | |
else | |
spec.failed = spec.failed + 1 | |
spec.current.message = message | |
spec.current.trace = debug.traceback() | |
end | |
end | |
-- | |
-- Collection of shoud matchers | |
matchers = { | |
should_be = function(self, expected) | |
local success = self.value == expected | |
local message | |
if not success then | |
message = "\texpecting : " .. tostring(expected) .. ", not ".. self.value | |
end | |
spec.report(success, message) | |
end; | |
should_be_true = function(self) | |
local success = self.value == true | |
local message | |
if not success then | |
message = "\texpecting : true " .. tostring(expected) .. ", not ".. self.value | |
end | |
spec.report(success, message) | |
end; | |
should_be_false = function(self) end; | |
should_be_nil = function(self) end; | |
should_not_be = function(self) end; | |
should_equal = function(self, expected) end; | |
should_match = function(self, pattern) end; | |
} | |
-- Expectation function | |
-- | |
function expect(target) | |
local instance = {} | |
instance.value = target | |
local matcher = function(self, method) | |
return function(...) | |
if matchers[method] then | |
matchers[method](self, ...) | |
end | |
end | |
end | |
setmetatable(instance, {__index = matcher}) | |
return instance | |
end | |
-- Descript a context | |
-- | |
function describe(context) | |
local tests = {} | |
spec.specs[context] = {} | |
return function(specs) | |
local instance = {} | |
local before = specs.before and specs.before or nil | |
local after = specs.after and specs.after or nil | |
-- prepare | |
if before then | |
specs.before = nil | |
before(instance) | |
end | |
if after then | |
specs.after = nil | |
end | |
-- run | |
for description, testcase in pairs(specs) do | |
spec.specs[context][description] = {} | |
spec.current = spec.specs[context][description] | |
testcase(instance) | |
end | |
-- post routine | |
if after then | |
after(instance) | |
end | |
end | |
end | |
-- Example | |
---------------------- | |
describe ("array.each") | |
{ | |
before = function(self) | |
self.list = "{1,2,3}" | |
end; | |
["should iterate over a collection"] = function(self) | |
local result = "abc" | |
expect(result).should_be 'abdc' | |
end; | |
["should be a function"] = function(self) | |
expect(self.list).should_be(self.list) | |
expect( type(table.foreach) ).should_be 'function' | |
end; | |
after = function() end | |
} | |
-- Very ugly reporting section | |
-- Ned some refactoring | |
table.foreach(spec.specs, function(context, cases) | |
print (context.."\n============================") | |
local index = 1 | |
table.foreach(cases, function(description, result) | |
if result.success then | |
print (index .. " - " .. description .. " : [ passed ]") | |
print ('----------------------------') | |
else | |
print (index .. " - " .. description .. " : [ failed ]") | |
print ('----------------------------') | |
print (result.message) | |
print (result.trace) | |
end | |
index = index + 1 | |
end) | |
end) | |
print ("Passed : " .. spec.passed ..", Failed : " .. spec.failed .. ". Rate : " .. | |
(spec.passed/ (spec.passed + spec.failed))*100 .."%") | |
print '============================' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment