Skip to content

Instantly share code, notes, and snippets.

@JamesxX
Created July 30, 2016 17:52
Show Gist options
  • Save JamesxX/c8bf0b1a8324a1221307f40194ab5cb9 to your computer and use it in GitHub Desktop.
Save JamesxX/c8bf0b1a8324a1221307f40194ab5cb9 to your computer and use it in GitHub Desktop.
local PRE = 1
local TEST = 2
local GREY = Color(200,200,200)
local PASS = Color(150,200,000)
local FAIL = Color(200,000,000)
local threads_to_reboot = { }
hook.Add( "Think", "ThreadRebooter", function()
for k,v in pairs( threads_to_reboot ) do
coroutine.resume( v )
if ( coroutine.status( v ) == "running" ) then
threads_to_reboot[k] = nil
end
end
end)
local function await_prereq( thread, func )
local done = function( ) table.insert(threads_to_reboot, thread) end
func( done )
return coroutine.yield( )
end
function it ( name )
return function( table )
return {
id = TEST,
test = name,
func = table[1] or function() return false end
}
end
end
function firstly (table)
return {
id = PRE,
func = table[1] or function( done ) return done() end
}
end
function premise( name )
local x = coroutine.create( function( self, tests )
local total_tests = 0
local total_passed = 0
for k,v in ipairs( tests ) do
if ( v.id == PRE ) then
await_prereq( self, v.func )
end
end
MsgC( "\n" )
MsgC( "\n" )
MsgC( GREY, "\t" .. name .. "\n" )
MsgC( "\n" )
for k,v in ipairs( tests ) do
if ( v.id == TEST ) then
local passed = v.func();
total_tests = total_tests + 1
total_passed = total_passed + (passed and 1 or 0)
MsgC( passed and PASS or FAIL, "\t\tit " .. v.test .. "\n" )
end
end
MsgC( "\n" )
if ( total_tests == total_passed ) then
MsgC( PASS, "\tPassed all " .. total_tests .. " tests." .. "\n" )
else
MsgC( FAIL, "\tPassed only " .. total_passed .. " of " ..total_tests .. " tests." .. "\n" )
end
MsgC( "\n" )
MsgC( "\n" )
end )
return function( tests )
coroutine.resume( x, x, tests)
end
end
local x;
premise "My addon should function correctly" {
firstly { function( done )
x = true
done()
end },
it "should should return true" { function()
return x;
end },
it "should should return true" { function()
return x;
end },
it "should should return true" { function()
return not x;
end },
it "should should return true" { function()
return x;
end }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment