pax_global_header00006660000000000000000000000064121471671310014515gustar00rootroot0000000000000052 comment=a7c9687daf90b02da7a4112f4cc3c731a3904859 lua-unit-1.5+gita58da93/000077500000000000000000000000001214716713100146765ustar00rootroot00000000000000lua-unit-1.5+gita58da93/.gitignore000066400000000000000000000005451214716713100166720ustar00rootroot00000000000000 # ignore temp files *.sw? *.bak *.pyc #ignore thumbnails created by windows Thumbs.db #Ignore files build by Visual Studio *.obj *.exe *.pdb *.user *.aps *.pch *.vspscc *_i.c *_p.c *.ncb *.suo *.tlb *.tlh *.bak *.cache *.ilk *.log [Bb]in [Dd]ebug*/ *.lib *.sbr obj/ [Rr]elease*/ _ReSharper*/ [Tt]est[Rr]esult* *.sublime-project *.lsln *.sublime-workspacelua-unit-1.5+gita58da93/LICENSE.txt000077500000000000000000000025421214716713100165270ustar00rootroot00000000000000This software is distributed under the BSD License. Copyright (c) 2005,2007,2012, Philippe Fremy All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. lua-unit-1.5+gita58da93/README.txt000077500000000000000000000034541214716713100164050ustar00rootroot00000000000000 luaunit.lua by Philippe Fremy Luaunit is a unit-testing framework for Lua, in the spirit of many others unit-testing framework. Luaunit let's you write test functions, test classes with test methods and setup/teardown functionality. Luaunit can output test failures using the TAP format, for easier integration into Continuous Integration platforms like Jenkins. Luaunit is derived from the initial work of Ryu Gwang. It is released under the BSD license. Luaunit should work on all platforms supported by lua. It was tested on Windows XP and Gentoo Linux. Luaunit is now maintained on github: https://github.com/bluebird75/luaunit See file example_with_luaunit.lua to understand how to use it. History: ======== version 1.5: 8. Nov 2012 ------------ - compatibility with Lua 5.1 and 5.2 - better object model internally - a lot more of internal tests - several internal bug fixes - make it easy to customize the test output - running test functions no longer requires a wrapper - several level of verbosity version 1.4: 26. Jul 2012 ------------ - switch from X11 to more popular BSD license - add TAP output format for integration into Jenkins - official repository now on github version 1.3: 30. Oct 2007 ------------ - port to lua 5.1 - iterate over the test classes, methods and functions in the alphabetical order - change the default order of expected, actual in assertEquals (adjustable with USE_EXPECTED_ACTUAL_IN_ASSERT_EQUALS). version 1.2: 13. Jun 2005 ------------ - first public release version 1.1: ------------ - move global variables to internal variables - assertion order is configurable between expected/actual or actual/expected - new assertion to check that a function call returns an error - display the calling stack when an error is spotted - two verbosity level, like in python unittest lua-unit-1.5+gita58da93/TODO.txt000066400000000000000000000010201214716713100161750ustar00rootroot00000000000000-[[ More tests ]] - strip luaunit stack more intelligently - table assertions - sequence asserts - control verbosity and output type with command line - more user documentation - display time to run all tests - make sure test suite ends when running tests with RunByTestClass or RunByTestMethod - add assertAlmostEquals for floats - add assertContains for strings - add assertIsNumber, assertIsXXX - allow to skip tests - add other types of output Done: x compatibilty tests with several version of lua x add assertNotEquals lua-unit-1.5+gita58da93/example_with_luaunit.lua000066400000000000000000000055221214716713100216340ustar00rootroot00000000000000 require('luaunit') TestToto = {} --class function TestToto:setUp() -- set up tests self.a = 1 self.s = 'hop' end function TestToto:test1_withFailure() print( "some stuff test 1" ) assertEquals( self.a , 1 ) -- will fail assertEquals( self.a , 2 ) assertEquals( self.a , 2 ) end function TestToto:test2_withFailure() print( "some stuff test 2" ) assertEquals( self.a , 1 ) assertEquals( self.s , 'hop' ) -- will fail assertEquals( self.s , 'bof' ) assertEquals( self.s , 'bof' ) end function TestToto:test3() print( "some stuff test 3" ) assertEquals( self.a , 1 ) assertEquals( self.s , 'hop' ) assertEquals( type(self.a), 'number' ) end function TestToto:test4() print( "some stuff test 4" ) assertNotEquals( self.a , 1 ) end function TestToto:test5() print( "some stuff test 5" ) assertTrue( self.a ) assertFalse( self.a ) end function TestToto:test6() print( "some stuff test 6" ) assertTrue( false ) end -- class TestToto TestTiti = {} --class function TestTiti:setUp() -- set up tests self.a = 1 self.s = 'hop' print( 'TestTiti:setUp' ) end function TestTiti:tearDown() -- some tearDown() code if necessary print( 'TestTiti:tearDown' ) end function TestTiti:test1_withFailure() print( "some stuff test 1" ) assertEquals( self.a , 1 ) -- will fail assertEquals( self.a , 2 ) assertEquals( self.a , 2 ) end function TestTiti:test2_withFailure() print( "some stuff test 2" ) assertEquals( self.a , 1 ) assertEquals( self.s , 'hop' ) -- will fail assertEquals( self.s , 'bof' ) assertEquals( self.s , 'bof' ) end function TestTiti:test3() print( "some stuff test 3" ) assertEquals( self.a , 1 ) assertEquals( self.s , 'hop' ) end -- class TestTiti -- simple test functions that were written previously can be integrated -- in luaunit too function test1_withFailure() assert( 1 == 1) -- will fail assert( 1 == 2) end function test2_withFailure() assert( 'a' == 'a') -- will fail assert( 'a' == 'b') end function test3() assert( 1 == 1) assert( 'a' == 'a') end -- LuaUnit:run( 'test2_withFailure' ) -- run only one test function -- LuaUnit:run( 'TestFunctions:test1_withFailure' ) -- LuaUnit:run( 'TestToto' ) -- run only on test class -- LuaUnit:run( 'TestTiti:test3') -- run only one test method of a test class -- LuaUnit:run() -- run everything ---- Control test output: lu = LuaUnit -- lu:setOutputType( "NIL" ) -- lu:setOutputType( "TAP" ) lu:setVerbosity( 1 ) os.exit( lu:run() ) lua-unit-1.5+gita58da93/luaunit.lua000077500000000000000000000564761214716713100171070ustar00rootroot00000000000000--[[ luaunit.lua Description: A unit testing framework Homepage: https://github.com/bluebird75/luaunit Initial author: Ryu, Gwang (http://www.gpgstudy.com/gpgiki/LuaUnit) Lot of improvements by Philippe Fremy License: BSD License, see LICENSE.txt ]]-- argv = arg --[[ Some people like assertEquals( actual, expected ) and some people prefer assertEquals( expected, actual ). ]]-- USE_EXPECTED_ACTUAL_IN_ASSERT_EQUALS = true DEFAULT_VERBOSITY = 1 function assertError(f, ...) -- assert that calling f with the arguments will raise an error -- example: assertError( f, 1, 2 ) => f(1,2) should generate an error local has_error, error_msg = not pcall( f, ... ) if has_error then return end error( "Expected an error but no error generated", 2 ) end function mytostring( v ) if type(v) == 'string' then return '"'..v..'"' end if type(v) == 'table' then if v.__class__ then return string.gsub( tostring(v), 'table', v.__class__ ) end return tostring(v) end return tostring(v) end function errormsg(actual, expected) local errorMsg if not USE_EXPECTED_ACTUAL_IN_ASSERT_EQUALS then expected, actual = actual, expected end if type(expected) == 'string' then errorMsg = "\nexpected: "..mytostring(expected).."\n".. "actual : "..mytostring(actual).."\n" else errorMsg = "expected: "..mytostring(expected)..", actual: "..mytostring(actual) end return errorMsg end function _table_contains(t, element) local _, value, v if t then for _, value in pairs(t) do if type(value) == type(element) then if type(element) == 'table' then if _is_table_items_equals(v, expected) then return true end else if value == element then return true end end end end end return false end function _is_table_items_equals(actual, expected, parent_key, items) if (type(actual) == 'table') and (type(expected) == 'table') then local k,v for k,v in pairs(actual) do if not _table_contains(expected, v) then return false end end return true elseif type(actual) ~= type(expected) then return false elseif actual == expected then return true end return false end function _is_table_equals(actual, expected) if (type(actual) == 'table') and (type(expected) == 'table') then local k,v for k,v in ipairs(actual) do if not _is_table_equals(v, expected[k]) then return false end end for k,v in pairs(actual) do if (type(k) ~= 'number') and (not _is_table_equals(v, expected[k])) then return false end end return true elseif type(actual) ~= type(expected) then return false elseif actual == expected then return true end return false end function assertEquals(actual, expected) if type(actual) == 'table' and type(expected) == 'table' then if not _is_table_equals(actual, expected) then error( errormsg(actual, expected), 2 ) end elseif type(actual) ~= type(expected) then error( errormsg(actual, expected), 2 ) elseif actual ~= expected then error( errormsg(actual, expected), 2 ) end end function assertTrue(value) if not value then error("expected: true, actual: " ..mytostring(value), 2) end end function assertFalse(value) if value then error("expected: false, actual: " ..mytostring(value), 2) end end function assertNotEquals(actual, expected) if type(actual) == 'table' and type(expected) == 'table' then if _is_table_equals(actual, expected) then error( errormsg(actual, expected), 2 ) end elseif type(actual) == type(expected) and actual == expected then error( errormsg(actual, expected), 2 ) end end function assertItemsEquals(actual, expected) if not _is_table_items_equals(actual, expected, true) then error( errormsg(actual, expected), 2 ) end end function assertIsNumber(value) if type(value) ~= 'number' then error("expected: a number value, actual:" .. type(value)) end end function assertIsString(value) if type(value) ~= "string" then error("expected: a string value, actual:" .. type(value)) end end function assertIsTable(value) if type(value) ~= 'table' then error("expected: a table value, actual:" .. type(value)) end end function assertIsBoolean(value) if type(value) ~= 'boolean' then error("expected: a boolean value, actual:" .. type(value)) end end function assertIsNil(value) if type(value) ~= "nil" then error("expected: a nil value, actual:" .. type(value)) end end function assertIsFunction(value) if type(value) ~= 'function' then error("expected: a function value, actual:" .. type(value)) end end function assertIs(actual, expected) if actual ~= expected then error( errormsg(actual, expected), 2 ) end end function assertNotIs(actual, expected) if actual == expected then error( errormsg(actual, expected), 2 ) end end assert_equals = assertEquals assert_not_equals = assertNotEquals assert_error = assertError assert_true = assertTrue assert_false = assertFalse assert_is_number = assertIsNumber assert_is_string = assertIsString assert_is_table = assertIsTable assert_is_boolean = assertIsBoolean assert_is_nil = assertIsNil assert_is_function = assertIsFunction assert_is = assertIs assert_not_is = assertNotIs function __genOrderedIndex( t ) local orderedIndex = {} for key,_ in pairs(t) do table.insert( orderedIndex, key ) end table.sort( orderedIndex ) return orderedIndex end function orderedNext(t, state) -- Equivalent of the next() function of table iteration, but returns the -- keys in the alphabetic order. We use a temporary ordered key table that -- is stored in the table being iterated. --print("orderedNext: state = "..tostring(state) ) local key if state == nil then -- the first time, generate the index t.__orderedIndex = nil t.__orderedIndex = __genOrderedIndex( t ) key = t.__orderedIndex[1] return key, t[key] end -- fetch the next value key = nil for i = 1,#t.__orderedIndex do if t.__orderedIndex[i] == state then key = t.__orderedIndex[i+1] end end if key then return key, t[key] end -- no more value to return, cleanup t.__orderedIndex = nil return end function orderedPairs(t) -- Equivalent of the pairs() function on tables. Allows to iterate -- in order return orderedNext, t, nil end function strsplit(delimiter, text) -- Split text into a list consisting of the strings in text, -- separated by strings matching delimiter (which may be a pattern). -- example: strsplit(",%s*", "Anna, Bob, Charlie,Dolores") local list = {} local pos = 1 if string.find("", delimiter, 1) then -- this would result in endless loops error("delimiter matches empty string!") end while 1 do local first, last = string.find(text, delimiter, pos) if first then -- found? table.insert(list, string.sub(text, pos, first-1)) pos = last+1 else table.insert(list, string.sub(text, pos)) break end end return list end function prefixString( prefix, s ) local t, s2 t = strsplit('\n', s) s2 = prefix..table.concat(t, '\n'..prefix) return s2 end ---------------------------------------------------------------- -- class TapOutput ---------------------------------------------------------------- TapOutput = { -- class __class__ = 'TapOutput', runner = nil, result = nil, } TapOutput_MT = { __index = TapOutput } function TapOutput:new() local t = {} t.verbosity = 0 setmetatable( t, TapOutput_MT ) return t end function TapOutput:startSuite() end function TapOutput:startClass(className) end function TapOutput:startTest(testName) end function TapOutput:addFailure( errorMsg, stackTrace ) print(string.format("not ok %d\t%s", self.result.testCount, self.result.currentTestName )) if self.verbosity > 0 then print( prefixString( ' ', errorMsg ) ) end if self.verbosity > 1 then print( prefixString( ' ', stackTrace ) ) end end function TapOutput:endTest(testHasFailure) if not self.result.currentTestHasFailure then print(string.format("ok %d\t%s", self.result.testCount, self.result.currentTestName )) end end function TapOutput:endClass() end function TapOutput:endSuite() print("1.."..self.result.testCount) return self.result.failureCount end -- class TapOutput end ---------------------------------------------------------------- -- class JUnitOutput ---------------------------------------------------------------- JUnitOutput = { -- class __class__ = 'JUnitOutput', runner = nil, result = nil, xmlFile = nil, } JUnitOutput_MT = { __index = JUnitOutput } function JUnitOutput:new() local t = {} t.verbosity = 0 setmetatable( t, JUnitOutput_MT ) return t end function JUnitOutput:startSuite() end function JUnitOutput:startClass(className) xmlFile = io.open(string.lower(className) .. ".xml", "w") xmlFile:write('\n') end function JUnitOutput:startTest(testName) if xmlFile then xmlFile:write('') end end function JUnitOutput:addFailure( errorMsg, stackTrace ) if xmlFile then xmlFile:write('' ..errorMsg .. '\n') xmlFile:write('\n') end end function JUnitOutput:endTest(testHasFailure) if xmlFile then xmlFile:write('\n') end end function JUnitOutput:endClass() end function JUnitOutput:endSuite() if xmlFile then xmlFile:write('\n') end if xmlFile then xmlFile:close() end return self.result.failureCount end -- class TapOutput end ---------------------------------------------------------------- -- class TextOutput ---------------------------------------------------------------- TextOutput = { __class__ = 'TextOutput' } TextOutput_MT = { -- class __index = TextOutput } function TextOutput:new() local t = {} t.runner = nil t.result = nil t.errorList ={} t.verbosity = 1 setmetatable( t, TextOutput_MT ) return t end function TextOutput:startSuite() end function TextOutput:startClass(className) if self.verbosity > 0 then print( '>>>>>>>>> '.. self.result.currentClassName ) end end function TextOutput:startTest(testName) if self.verbosity > 0 then print( ">>> ".. self.result.currentTestName ) end end function TextOutput:addFailure( errorMsg, stackTrace ) table.insert( self.errorList, { self.result.currentTestName, errorMsg, stackTrace } ) if self.verbosity == 0 then io.stdout:write("F") end if self.verbosity > 0 then print( errorMsg ) print( 'Failed' ) end end function TextOutput:endTest(testHasFailure) if not testHasFailure then if self.verbosity > 0 then --print ("Ok" ) else io.stdout:write(".") end end end function TextOutput:endClass() if self.verbosity > 0 then print() end end function TextOutput:displayOneFailedTest( failure ) testName, errorMsg, stackTrace = unpack( failure ) print(">>> "..testName.." failed") print( errorMsg ) if self.verbosity > 1 then print( stackTrace ) end end function TextOutput:displayFailedTests() if #self.errorList == 0 then return end print("Failed tests:") print("-------------") for i,v in ipairs(self.errorList) do self:displayOneFailedTest( v ) end print() end function TextOutput:endSuite() if self.verbosity == 0 then print() else print("=========================================================") end self:displayFailedTests() local successPercent, successCount successCount = self.result.testCount - self.result.failureCount if self.result.testCount == 0 then successPercent = 100 else successPercent = math.ceil( 100 * successCount / self.result.testCount ) end print( string.format("Success : %d%% - %d / %d", successPercent, successCount, self.result.testCount) ) end -- class TextOutput end ---------------------------------------------------------------- -- class NilOutput ---------------------------------------------------------------- function nopCallable() --print(42) return nopCallable end NilOutput = { __class__ = 'NilOuptut', } NilOutput_MT = { __index = nopCallable, } function NilOutput:new() local t = {} t.__class__ = 'NilOutput' setmetatable( t, NilOutput_MT ) return t end ---------------------------------------------------------------- -- class LuaUnit ---------------------------------------------------------------- LuaUnit = { outputType = TextOutput, verbosity = DEFAULT_VERBOSITY, __class__ = 'LuaUnit' } LuaUnit_MT = { __index = LuaUnit } function LuaUnit:new() local t = {} setmetatable( t, LuaUnit_MT ) return t end -----------------[[ Utility methods ]]--------------------- function LuaUnit.isFunction(aObject) return 'function' == type(aObject) end --------------[[ Output methods ]]------------------------- function LuaUnit:ensureSuiteStarted( ) if self.result and self.result.suiteStarted then return end self:startSuite() end function LuaUnit:startSuite() self.result = {} self.result.failureCount = 0 self.result.testCount = 0 self.result.currentTestName = "" self.result.currentClassName = "" self.result.currentTestHasFailure = false self.result.suiteStarted = true self.outputType = self.outputType or TextOutput self.output = self.outputType:new() self.output.runner = self self.output.result = self.result self.output.verbosity = self.verbosity self.output:startSuite() end function LuaUnit:startClass( className ) self.result.currentClassName = className self.output:startClass( className ) end function LuaUnit:startTest( testName ) self.result.currentTestName = testName self.result.testCount = self.result.testCount + 1 self.result.currentTestHasFailure = false self.output:startTest( testName ) end function LuaUnit:addFailure( errorMsg, stackTrace ) if not self.result.currentTestHasFailure then self.result.failureCount = self.result.failureCount + 1 self.result.currentTestHasFailure = true end self.output:addFailure( errorMsg, stackTrace ) end function LuaUnit:endTest() self.output:endTest( self.result.currentTestHasFailure ) self.result.currentTestName = "" self.result.currentTestHasFailure = false end function LuaUnit:endClass() self.output:endClass() end function LuaUnit:endSuite() self.result.suiteStarted = false self.output:endSuite() end function LuaUnit:setOutputType(outputType) -- default to text -- tap produces results according to TAP format if outputType:upper() == "NIL" then self.outputType = NilOutput return end if outputType:upper() == "TAP" then self.outputType = TapOutput return end if outputType:upper() == "JUNIT" then self.outputType = JUnitOutput return end if outputType:upper() == "TEXT" then self.outputType = TextOutput return end error( 'No such format: '..outputType) end function LuaUnit:setVerbosity( verbosity ) self.verbosity = verbosity end --------------[[ Runner ]]----------------- SPLITTER = '\n>----------<\n' function LuaUnit:protectedCall( classInstance , methodInstance) -- if classInstance is nil, this is just a function run local function err_handler(e) return debug.traceback(e..SPLITTER, 4) end local ok=true, errorMsg, stackTrace if classInstance then -- stupid Lua < 5.2 does not allow xpcall with arguments so let's live with that ok, errorMsg = xpcall( function () methodInstance(classInstance) end, err_handler ) else ok, errorMsg = xpcall( function () methodInstance() end, err_handler ) end if not ok then t = strsplit( SPLITTER, errorMsg ) stackTrace = string.sub(t[2],2) self:addFailure( t[1], stackTrace ) end return ok end function LuaUnit:_runTestMethod(className, methodName, classInstance, methodInstance) -- When executing a class method, all parameters are set -- When executing a test function, className and classInstance are nil if type(methodInstance) ~= 'function' then error( tostring(methodName)..'must be a function, not '..type(methodInstance)) end if className == nil then className = '' end if self.lastClassName ~= className then if self.lastClassName ~= nil then self:endClass() end self:startClass( className ) self.lastClassName = className end self:startTest(className..':'..methodName) -- run setUp first(if any) if classInstance and self.isFunction( classInstance.setUp ) then self:protectedCall( classInstance, classInstance.setUp) end -- run testMethod() if not self.result.currentTestHasFailure then self:protectedCall( classInstance, methodInstance) end -- lastly, run tearDown(if any) if classInstance and self.isFunction(classInstance.tearDown) then self:protectedCall( classInstance, classInstance.tearDown) end self:endTest() end function LuaUnit:runSomeTest( someName, someInstance ) -- name is mandatory -- if instance is not given, it's looked up in the global namespace -- name can be a test class, a test function, or a test class + test method -- instance can be a test class or a test function -- example: runSomeTest( 'TestToto' ) -- example: runSomeTest( 'TestToto', TestToto ) -- example: runSomeTest( 'TestToto:testTiti' ) -- example: runSomeTest( 'TestToto:testTiti', TestToto ) -- example: runSomeTest( 'testFunction' ) -- example: runSomeTest( 'testFunction' , testFunction ) self:ensureSuiteStarted() local hasMethod, methodName, methodInstance, className, classInstance if someName == nil or someName == '' then error( 'Name is required!') end hasMethod = string.find(someName, ':' ) -- name is class + method if hasMethod then methodName = string.sub(someName, hasMethod+1) className = string.sub(someName,1,hasMethod-1) classInstance = someInstance classInstance = classInstance or _G[className] if classInstance == nil then error( "No such class: "..className ) end if type(classInstance) ~= 'table' then error( 'Instance must be a table') end methodInstance = classInstance[methodName] if methodInstance == nil then error( "Could not find method in class "..tostring(className).." for method "..tostring(methodName) ) end self:_runTestMethod( className, methodName, classInstance, methodInstance ) return end if someInstance == nil then someInstance = _G[someName] if not someInstance then error( "No such variable: "..someName ) end end if (type(someInstance) ~= 'table' and type(someInstance) ~= 'function') then error( 'Instance must be function or table') end -- name is either a function or a class if type(someInstance) == 'table' then -- run all test methods of the class className = someName classInstance = someInstance for methodName, methodInstance in orderedPairs(classInstance) do if LuaUnit.isFunction(methodInstance) and string.sub(methodName, 1, 4) == "test" then self:_runTestMethod( className, methodName, classInstance, methodInstance ) end end return end if type(someInstance) == 'function' then self:_runTestMethod( nil, someName, nil, someInstance ) return end error( 'Should never be reached...') end function LuaUnit:run(...) -- Run some specific test classes. -- If no arguments are passed, run the class names specified on the -- command line. If no class name is specified on the command line -- run all classes whose name starts with 'Test' -- -- If arguments are passed, they must be strings of the class names -- that you want to run local runner = self:new() local outputType = os.getenv("outputType") if outputType then LuaUnit:setOutputType(outputType) end return runner:runSuite(...) end function LuaUnit:runSuite(...) self:startSuite() args={...}; if #args == 0 then args = argv end if #args == 0 then -- create the list if classes to run now ! If not, you can -- not iterate over _G while modifying it. args = {} for key, val in pairs(_G) do if string.sub(key,1,4):lower() == 'test' then table.insert( args, key ) end end table.sort( args ) end for i,testName in ipairs( args ) do self:runSomeTest( testName ) end if self.lastClassName ~= nil then self:endClass() end self:endSuite() return self.result.failureCount end -- class LuaUnit lua-unit-1.5+gita58da93/make_release.py000066400000000000000000000041011214716713100176610ustar00rootroot00000000000000import subprocess, sys, os, shutil, os.path VERSION='1.5' RELEASE_NAME='luaunit-%s' % VERSION RELEASE_DIR='release/' + RELEASE_NAME + '/' TARGET_ZIP=RELEASE_NAME + '.zip' TARGET_TGZ=RELEASE_NAME + '.tgz' REPO_PATH='d:/work/luaunit/luaunit-git/luaunit/' LUA50='d:/program/lua/lua50/lua50.exe' LUA51='d:/program/lua/lua51/lua51.exe' LUA52='d:/program/lua/lua52/lua52.exe' ALL_LUA = ( (LUA52, 'lua 5.2'), (LUA51, 'lua 5.1'), # (LUA50, 'lua 5.0'), ) os.environ["nodosfilewarning"] = "1" def report( s ): print '[[[[[[[[[[[[[ %s ]]]]]]]]]]]]]' % s def run_tests(): '''Run tests with all versions of lua''' for lua, luaversion in ALL_LUA: report( 'Running tests with %s' % luaversion ) retcode = subprocess.call( [lua, 'test_luaunit.lua'] ) if retcode != 0: report( 'Invalid retcode when running tests: %d' % retcode ) sys.exit( retcode ) report( 'All tests succeed!' ) def run_example(): for lua, luaversion in ALL_LUA: report( 'Running examples with %s' % luaversion ) retcode = subprocess.call( [lua, 'example_with_luaunit.lua'] ) if retcode != 6: report( 'Invalid retcode when running examples: %d' % retcode ) sys.exit( retcode ) report( 'All examples ran!' ) def packageit(): shutil.rmtree('release', True) try: os.mkdir('release') except OSError: pass subprocess.check_call(['c:/Program Files/Git/bin/git.exe', 'clone', '--no-hardlinks', REPO_PATH, RELEASE_DIR]) os.chdir( RELEASE_DIR ) shutil.rmtree('.git') os.unlink('.gitignore') run_tests() run_example() os.chdir('..') report('Start packaging') shutil.make_archive(RELEASE_NAME, 'zip', root_dir='.', base_dir=RELEASE_NAME ) shutil.make_archive(RELEASE_NAME, 'gztar', root_dir='.', base_dir=RELEASE_NAME ) report('Zip and tgz ready!') if __name__ == '__main__': # run_tests() # run_example() packageit() # git checkout # store version number # generate zip and tgz # run example with several versions of lua, check return value lua-unit-1.5+gita58da93/test_luaunit.lua000077500000000000000000000634001214716713100201270ustar00rootroot00000000000000--[[ Author: Philippe Fremy License: BSD License, see LICENSE.txt ]]-- -- This is a bit tricky since the test uses the features that it tests. require('luaunit') Mock = { __class__ = 'Mock', calls = {} } function Mock:new() local t = {} t.__class__ = 'Mock' t.calls = {} function t.callRecorder( callInfo ) -- Return a function that stores its arguments in callInfo function f( ... ) -- Not lua 5.0 compliant: args ={...} for i,v in pairs(args) do table.insert( callInfo, v ) end end return f end local t_MT = {} function t_MT.__index( t, key ) local callInfo = { key } table.insert( t.calls, callInfo ) return t.callRecorder( callInfo ) end setmetatable( t, t_MT ) return t end TestMock = {} function TestMock:testMock() m = Mock:new() m.titi( 42 ) m.toto( 33, "abc", { 21} ) assertEquals( m.calls[1][1], 'titi' ) assertEquals( m.calls[1][2], 42 ) assertEquals( #m.calls[1], 2 ) assertEquals( m.calls[2][1], 'toto' ) assertEquals( m.calls[2][2], 33 ) assertEquals( m.calls[2][3], 'abc' ) assertEquals( m.calls[2][4][1], 21 ) assertEquals( #m.calls[2], 4 ) assertEquals( #m.calls, 2 ) end function printSeq( seq ) if type(seq) ~= 'table' then print( mytostring(seq) ) return end for i,v in ipairs(seq) do print( '['..i..']: '..mytostring(v) ) end end TestLuaUnit = {} --class TestLuaUnit.__class__ = 'TestLuaUnit' function TestLuaUnit:tearDown() executedTests = {} end function TestLuaUnit:setUp() executedTests = {} end ------------------------------------------------------------------ ---------[[ Utility Tests ]]---------- ------------------------------------------------------------------ function TestLuaUnit:test_orderedNextReturnsOrderedKeyValues() t1 = {} t1['aaa'] = 'abc' t1['ccc'] = 'def' t1['bbb'] = 'cba' k, v = orderedNext( t1, nil ) assertEquals( k, 'aaa' ) assertEquals( v, 'abc' ) k, v = orderedNext( t1, k ) assertEquals( k, 'bbb' ) assertEquals( v, 'cba' ) k, v = orderedNext( t1, k ) assertEquals( k, 'ccc' ) assertEquals( v, 'def' ) k, v = orderedNext( t1, k ) assertEquals( k, nil ) assertEquals( v, nil ) end function TestLuaUnit:test_orderedNextWorksTwiceOnTable() t1 = {} t1['aaa'] = 'abc' t1['ccc'] = 'def' t1['bbb'] = 'cba' k, v = orderedNext( t1, nil ) k, v = orderedNext( t1, k ) k, v = orderedNext( t1, nil ) assertEquals( k, 'aaa' ) assertEquals( v, 'abc' ) end function TestLuaUnit:test_orderedNextWorksOnTwoTables() t1 = { aaa = 'abc', ccc = 'def' } t2 = { ['3'] = '33', ['1'] = '11' } k, v = orderedNext( t1, nil ) assertEquals( k, 'aaa' ) assertEquals( v, 'abc' ) k, v = orderedNext( t2, nil ) assertEquals( k, '1' ) assertEquals( v, '11' ) k, v = orderedNext( t1, 'aaa' ) assertEquals( k, 'ccc' ) assertEquals( v, 'def' ) k, v = orderedNext( t2, '1' ) assertEquals( k, '3' ) assertEquals( v, '33' ) end function TestLuaUnit:test_strSplitOneCharDelim() t = strsplit( '\n', '1\n22\n333\n' ) assertEquals( t[1], '1') assertEquals( t[2], '22') assertEquals( t[3], '333') assertEquals( t[4], '') assertEquals( #t, 4 ) end function TestLuaUnit:test_strSplit3CharDelim() t = strsplit( '2\n3', '1\n22\n332\n3' ) assertEquals( t[1], '1\n2') assertEquals( t[2], '3') assertEquals( t[3], '') assertEquals( #t, 3 ) end function TestLuaUnit:test_strSplitOnFailure() s1 = 'd:/work/luaunit/luaunit-git/luaunit/test_luaunit.lua:467: expected: 1, actual: 2\n' s2 = [[stack traceback: .\luaunit.lua:443: in function <.\luaunit.lua:442> [C]: in function 'error' .\luaunit.lua:56: in function 'assertEquals' d:/work/luaunit/luaunit-git/luaunit/test_luaunit.lua:467: in function [C]: in function 'xpcall' .\luaunit.lua:447: in function 'protectedCall' .\luaunit.lua:479: in function '_runTestMethod' .\luaunit.lua:527: in function 'runTestMethod' .\luaunit.lua:569: in function 'runTestClass' .\luaunit.lua:609: in function <.\luaunit.lua:588> (...tail calls...) d:/work/luaunit/luaunit-git/luaunit/test_luaunit.lua:528: in main chunk [C]: in ? ]] t = strsplit( SPLITTER, s1..SPLITTER..s2) assertEquals( t[1], s1) assertEquals( t[2], s2) assertEquals( #t, 2 ) end function TestLuaUnit:test_prefixString() assertEquals( prefixString( '12 ', 'ab\ncd\nde'), '12 ab\n12 cd\n12 de' ) end ------------------------------------------------------------------ ---------[[ Assertion Tests ]]---------- ------------------------------------------------------------------ function TestLuaUnit:test_assertError() local function f( v ) v = v + 1 end local function f_with_error(v) v = v + 2 error('coucou') end local x = 1 -- f_with_error generates an error has_error = not pcall( f_with_error, x ) assertEquals( has_error, true ) -- f does not generate an error has_error = not pcall( f, x ) assertEquals( has_error, false ) -- assertError is happy with f_with_error assertError( f_with_error, x ) -- assertError is unhappy with f has_error = not pcall( assertError, f, x ) assertError( has_error, true ) -- multiple arguments local function f_with_multi_arguments(a,b,c) if a == b and b == c then return end error("three arguments not equal") end assertError( f_with_multi_arguments, 1, 1, 3 ) assertError( f_with_multi_arguments, 1, 3, 1 ) assertError( f_with_multi_arguments, 3, 1, 1 ) has_error = not pcall( assertError, f_with_multi_arguments, 1, 1, 1 ) assertEquals( has_error, true ) end function TestLuaUnit:test_assertEquals() f = function() return true end g = function() return true end assertEquals( 1, 1 ) assertEquals( "abc", "abc" ) assertEquals( nil, nil ) assertEquals( true, true ) assertEquals( f, f) assertEquals( {1,2,3}, {1,2,3}) assertEquals( {one=1,two=2,three=3}, {one=1,two=2,three=3}) assertEquals( {one=1,two=2,three=3}, {two=2,three=3,one=1}) assertEquals( {one=1,two={1,2},three=3}, {two={1,2},three=3,one=1}) assertEquals( {one=1,two={1,{2,nil}},three=3}, {two={1,{2,nil}},three=3,one=1}) assertEquals( {nil}, {nil} ) assertError( assertEquals, 1, 2) assertError( assertEquals, 1, "abc" ) assertError( assertEquals, 0, nil ) assertError( assertEquals, false, nil ) assertError( assertEquals, true, 1 ) assertError( assertEquals, f, 1 ) assertError( assertEquals, f, g ) assertError( assertEquals, {1,2,3}, {2,1,3} ) assertError( assertEquals, {1,2,3}, nil ) assertError( assertEquals, {1,2,3}, 1 ) assertError( assertEquals, {1,2,3}, true ) assertError( assertEquals, {1,2,3}, {one=1,two=2,three=3} ) assertError( assertEquals, {1,2,3}, {one=1,two=2,three=3,four=4} ) assertError( assertEquals, {one=1,two=2,three=3}, {2,1,3} ) assertError( assertEquals, {one=1,two=2,three=3}, nil ) assertError( assertEquals, {one=1,two=2,three=3}, 1 ) assertError( assertEquals, {one=1,two=2,three=3}, true ) assertError( assertEquals, {one=1,two=2,three=3}, {1,2,3} ) assertError( assertEquals, {one=1,two={1,2},three=3}, {two={2,1},three=3,one=1}) end function TestLuaUnit:test_assertNotEquals() f = function() return true end g = function() return true end assertNotEquals( 1, 2 ) assertNotEquals( "abc", 2 ) assertNotEquals( "abc", "def" ) assertNotEquals( 1, 2) assertNotEquals( 1, "abc" ) assertNotEquals( 0, nil ) assertNotEquals( false, nil ) assertNotEquals( true, 1 ) assertNotEquals( f, 1 ) assertNotEquals( f, g ) assertNotEquals( {one=1,two=2,three=3}, true ) assertNotEquals( {one=1,two={1,2},three=3}, {two={2,1},three=3,one=1} ) assertError( assertNotEquals, 1, 1) assertError( assertNotEquals, "abc", "abc" ) assertError( assertNotEquals, nil, nil ) assertError( assertNotEquals, true, true ) assertError( assertNotEquals, f, f) assertError( assertNotEquals, {one=1,two={1,{2,nil}},three=3}, {two={1,{2,nil}},three=3,one=1}) end function TestLuaUnit:test_assertNotEqualsDifferentTypes2() assertNotEquals( 2, "abc" ) end function TestLuaUnit:test_assertTrue() assertTrue(true) assertError( assertTrue, false) assertTrue(0) assertTrue("abc") assertError( assertTrue, nil ) assertTrue( function() return true end ) assertTrue( {} ) end function TestLuaUnit:test_assertFalse() assertFalse(false) assertError( assertFalse, true) assertFalse( nil ) assertError( assertFalse, 0 ) assertError( assertFalse, "abc" ) assertError( assertFalse, function() return true end ) assertError( assertFalse, {} ) end function TestLuaUnit:test_assertItemsEquals() assertItemsEquals(nil, nil) assertError(assertItemsEquals, {1}, {}) assertError(assertItemsEquals, nil, {1,2,3}) assertError(assertItemsEquals, {1,2,3}, nil) assertItemsEquals({1,2,3}, {3,1,2}) assertItemsEquals({nil},{nil}) assertItemsEquals({1,{2,1},3}, {3,1,{1,2}}) assertItemsEquals({one=1,two=2,three=3}, {two=2,one=1,three=3}) assertItemsEquals({one=1,two={1,2},three=3}, {two={2,1},one=1,three=3}) assertItemsEquals({one=1,two={1,{3,2,one=1}},three=3}, {two={{3,one=1,2},1},one=1,three=3}) assertError(assertItemsEquals, {one=1,two=2,three=3}, {two=2,one=1,three=2}) assertError(assertItemsEquals, {one=1,two=2,three=3}, {two=2,one=1,four=4}) assertError(assertItemsEquals, {one=1,two=2,three=3}, {two=2,one=1,three}) assertError(assertItemsEquals, {one=1,two=2,three=3}, {two=2,one=1,nil}) assertError(assertItemsEquals, {one=1,two=2,three=3}, {two=2,one=1}) end function TestLuaUnit:test_assertIsNumber() assertIsNumber(1) assertIsNumber(1.4) assertError(assertIsNumber, "hi there!") assertError(assertIsNumber, nil) assertError(assertIsNumber, {}) assertError(assertIsNumber, {1,2,3}) assertError(assertIsNumber, {1}) assertError(assertIsTable, true) end function TestLuaUnit:test_assertIsString() assertError(assertIsString, 1) assertError(assertIsString, 1.4) assertIsString("hi there!") assertError(assertIsString, nil) assertError(assertIsString, {}) assertError(assertIsString, {1,2,3}) assertError(assertIsString, {1}) assertError(assertIsTable, true) end function TestLuaUnit:test_assertIsTable() assertError(assertIsTable, 1) assertError(assertIsTable, 1.4) assertError(assertIsTable, "hi there!") assertError(assertIsTable, nil) assertIsTable({}) assertIsTable({1,2,3}) assertIsTable({1}) assertError(assertIsTable, true) end function TestLuaUnit:test_assertIsBoolean() assertError(assertIsBoolean, 1) assertError(assertIsBoolean, 1.4) assertError(assertIsBoolean, "hi there!") assertError(assertIsBoolean, nil) assertError(assertIsBoolean, {}) assertError(assertIsBoolean, {1,2,3}) assertError(assertIsBoolean, {1}) assertIsBoolean(true) assertIsBoolean(false) end function TestLuaUnit:test_assertIsNil() assertError(assertIsNil, 1) assertError(assertIsNil, 1.4) assertError(assertIsNil, "hi there!") assertIsNil(nil) assertError(assertIsNil, {}) assertError(assertIsNil, {1,2,3}) assertError(assertIsNil, {1}) assertError(assertIsNil, false) end function TestLuaUnit:test_assertIsFunction() f = function() return true end assertError(assertIsFunction, 1) assertError(assertIsFunction, 1.4) assertError(assertIsFunction, "hi there!") assertError(assertIsFunction, nil) assertError(assertIsFunction, {}) assertError(assertIsFunction, {1,2,3}) assertError(assertIsFunction, {1}) assertError(assertIsFunction, false) assertIsFunction(f) end function TestLuaUnit:test_assertIs() local f = function() return true end local g = function() return true end local temp = {} assertIs(1,1) assertIs(f,f) assertIs(temp,temp) temp = {a=1,{1,2},day="today"} assertIs(temp,temp) assertError(assertIs, 1, 2) assertError(assertIs, 1.4, 1) assertError(assertIs, "hi there!", "hola") assertError(assertIs, nil, 1) assertError(assertIs, {}, {}) assertError(assertIs, {1,2,3}, f) assertError(assertIs, f, g) end function TestLuaUnit:test_assertNotIs() local f = function() return true end local g = function() return true end local temp = {} assertError( assertNotIs, 1,1 ) assertError( assertNotIs, f,f ) assertError( assertNotIs, temp,temp ) temp = {a=1,{1,2},day="today"} assertError( assertNotIs, temp,temp) assertNotIs(1, 2) assertNotIs(1.4, 1) assertNotIs("hi there!", "hola") assertNotIs(nil, 1) assertNotIs({}, {}) assertNotIs({1,2,3}, f) assertNotIs(f, g) end ------------------------------------------------------------------ ---------[[ Execution Tests ]]---------- ------------------------------------------------------------------ executedTests = {} MyTestToto1 = {} --class function MyTestToto1:test1() table.insert( executedTests, "MyTestToto1:test1" ) end function MyTestToto1:testb() table.insert( executedTests, "MyTestToto1:testb" ) end function MyTestToto1:test3() table.insert( executedTests, "MyTestToto1:test3" ) end function MyTestToto1:testa() table.insert( executedTests, "MyTestToto1:testa" ) end function MyTestToto1:test2() table.insert( executedTests, "MyTestToto1:test2" ) end MyTestWithFailures = {} function MyTestWithFailures:testWithFailure1() assertEquals(1, 2) end function MyTestWithFailures:testWithFailure2() assertError( function() end ) end function MyTestWithFailures:testOk() end MyTestOk = {} function MyTestOk:testOk1() end function MyTestOk:testOk2() end function TestLuaUnit:test_MethodsAreExecutedInRightOrder() local runner = LuaUnit:new() runner:setOutputType( "NIL" ) runner:runSuite( 'MyTestToto1' ) assertEquals( #executedTests, 5 ) assertEquals( executedTests[1], "MyTestToto1:test1" ) assertEquals( executedTests[2], "MyTestToto1:test2" ) assertEquals( executedTests[3], "MyTestToto1:test3" ) assertEquals( executedTests[4], "MyTestToto1:testa" ) assertEquals( executedTests[5], "MyTestToto1:testb" ) end function TestLuaUnit:testRunSomeTestByName( ) assertEquals( #executedTests, 0 ) local runner = LuaUnit:new() runner:setOutputType( "NIL" ) runner:runSomeTest( 'MyTestToto1' ) assertEquals( #executedTests, 5 ) end function TestLuaUnit:testRunSomeTestByGlobalInstance( ) assertEquals( #executedTests, 0 ) local runner = LuaUnit:new() runner:setOutputType( "NIL" ) runner:runSomeTest( 'Toto', MyTestToto1 ) assertEquals( #executedTests, 5 ) end function TestLuaUnit:testRunSomeTestByLocalInstance( ) MyLocalTestToto1 = {} --class function MyLocalTestToto1:test1() table.insert( executedTests, "MyLocalTestToto1:test1" ) end assertEquals( #executedTests, 0 ) local runner = LuaUnit:new() runner:setOutputType( "NIL" ) runner:runSomeTest( 'MyLocalTestToto1', MyLocalTestToto1 ) assertEquals( #executedTests, 1 ) assertEquals( executedTests[1], 'MyLocalTestToto1:test1') end function TestLuaUnit:testRunTestByTestFunction() local function mytest() table.insert( executedTests, "mytest" ) end local runner = LuaUnit:new() runner:setOutputType( "NIL" ) runner:runSomeTest( 'mytest', mytest ) assertEquals( #executedTests, 1 ) assertEquals( executedTests[1], 'mytest') end function TestLuaUnit:testRunReturnsNumberOfFailures() local runner = LuaUnit:new() runner:setOutputType( "NIL" ) ret = runner:runSuite( 'MyTestWithFailures' ) assertEquals(ret, 2) ret = runner:runSuite( 'MyTestToto1' ) assertEquals(ret, 0) end function TestLuaUnit:testTestCountAndFailCount() local runner = LuaUnit:new() runner:setOutputType( "NIL" ) runner:runSuite( 'MyTestWithFailures' ) assertEquals( runner.result.testCount, 3) assertEquals( runner.result.failureCount, 2) runner:runSuite( 'MyTestToto1' ) assertEquals( runner.result.testCount, 5) assertEquals( runner.result.failureCount, 0) end function TestLuaUnit:testRunTestMethod() local myExecutedTests = {} local MyTestWithSetupTeardown = {} function MyTestWithSetupTeardown:setUp() table.insert( myExecutedTests, 'setUp' ) end function MyTestWithSetupTeardown:test1() table.insert( myExecutedTests, 'test1' ) end function MyTestWithSetupTeardown:tearDown() table.insert( myExecutedTests, 'tearDown' ) end local runner = LuaUnit:new() runner:setOutputType( "NIL" ) runner:runSomeTest( 'MyTestWithSetupTeardown:test1', MyTestWithSetupTeardown ) assertEquals( runner.result.failureCount, 0 ) assertEquals( myExecutedTests[1], 'setUp' ) assertEquals( myExecutedTests[2], 'test1') assertEquals( myExecutedTests[3], 'tearDown') assertEquals( #myExecutedTests, 3) end function TestLuaUnit:testWithSetupTeardownErrors1() local myExecutedTests = {} local MyTestWithSetupError = {} function MyTestWithSetupError:setUp() table.insert( myExecutedTests, 'setUp' ); assertEquals( 'b', 'c') end function MyTestWithSetupError:test1() table.insert( myExecutedTests, 'test1' ) end function MyTestWithSetupError:tearDown() table.insert( myExecutedTests, 'tearDown' ) end local runner = LuaUnit:new() runner:setOutputType( "NIL" ) runner:runSomeTest( 'MyTestWithSetupError', MyTestWithSetupError ) assertEquals( runner.result.failureCount, 1 ) assertEquals( runner.result.testCount, 1 ) assertEquals( myExecutedTests[1], 'setUp' ) assertEquals( myExecutedTests[2], 'tearDown') assertEquals( #myExecutedTests, 2) end function TestLuaUnit:testWithSetupTeardownErrors2() local myExecutedTests = {} local MyTestWithSetupError = {} function MyTestWithSetupError:setUp() table.insert( myExecutedTests, 'setUp' ) end function MyTestWithSetupError:test1() table.insert( myExecutedTests, 'test1' ) end function MyTestWithSetupError:tearDown() table.insert( myExecutedTests, 'tearDown' ); assertEquals( 'b', 'c') end runner = LuaUnit:new() runner:setOutputType( "NIL" ) runner:runSomeTest( 'MyTestWithSetupError', MyTestWithSetupError ) assertEquals( runner.result.failureCount, 1 ) assertEquals( runner.result.testCount, 1 ) assertEquals( myExecutedTests[1], 'setUp' ) assertEquals( myExecutedTests[2], 'test1' ) assertEquals( myExecutedTests[3], 'tearDown') assertEquals( #myExecutedTests, 3) end function TestLuaUnit:testWithSetupTeardownErrors3() local myExecutedTests = {} local MyTestWithSetupError = {} function MyTestWithSetupError:setUp() table.insert( myExecutedTests, 'setUp' ); assertEquals( 'b', 'c') end function MyTestWithSetupError:test1() table.insert( myExecutedTests, 'test1' ) end function MyTestWithSetupError:tearDown() table.insert( myExecutedTests, 'tearDown' ); assertEquals( 'b', 'c') end runner = LuaUnit:new() runner:setOutputType( "NIL" ) runner:runSomeTest( 'MyTestWithSetupError', MyTestWithSetupError ) assertEquals( runner.result.failureCount, 1 ) assertEquals( runner.result.testCount, 1 ) assertEquals( myExecutedTests[1], 'setUp' ) assertEquals( myExecutedTests[2], 'tearDown') assertEquals( #myExecutedTests, 2) end function TestLuaUnit:testWithSetupTeardownErrors4() local myExecutedTests = {} local MyTestWithSetupError = {} function MyTestWithSetupError:setUp() table.insert( myExecutedTests, 'setUp' ); assertEquals( 'b', 'c') end function MyTestWithSetupError:test1() table.insert( myExecutedTests, 'test1' ); assertEquals( 'b', 'c') end function MyTestWithSetupError:tearDown() table.insert( myExecutedTests, 'tearDown' ); assertEquals( 'b', 'c') end runner = LuaUnit:new() runner:setOutputType( "NIL" ) runner:runSomeTest( 'MyTestWithSetupError', MyTestWithSetupError ) assertEquals( runner.result.failureCount, 1 ) assertEquals( runner.result.testCount, 1 ) assertEquals( myExecutedTests[1], 'setUp' ) assertEquals( myExecutedTests[2], 'tearDown') assertEquals( #myExecutedTests, 2) end function TestLuaUnit:testWithSetupTeardownErrors5() local myExecutedTests = {} local MyTestWithSetupError = {} function MyTestWithSetupError:setUp() table.insert( myExecutedTests, 'setUp' ) end function MyTestWithSetupError:test1() table.insert( myExecutedTests, 'test1' ); assertEquals( 'b', 'c') end function MyTestWithSetupError:tearDown() table.insert( myExecutedTests, 'tearDown' ); assertEquals( 'b', 'c') end runner = LuaUnit:new() runner:setOutputType( "NIL" ) runner:runSomeTest( 'MyTestWithSetupError', MyTestWithSetupError ) assertEquals( runner.result.failureCount, 1 ) assertEquals( runner.result.testCount, 1 ) assertEquals( myExecutedTests[1], 'setUp' ) assertEquals( myExecutedTests[2], 'test1' ) assertEquals( myExecutedTests[3], 'tearDown') assertEquals( #myExecutedTests, 3) end function TestLuaUnit:testOutputInterface() local runner = LuaUnit:new() runner.outputType = Mock runner:runSuite( 'MyTestWithFailures', 'MyTestOk' ) m = runner.output assertEquals( m.calls[1][1], 'startSuite' ) assertEquals(#m.calls[1], 2 ) assertEquals( m.calls[2][1], 'startClass' ) assertEquals( m.calls[2][3], 'MyTestWithFailures' ) assertEquals(#m.calls[2], 3 ) assertEquals( m.calls[3][1], 'startTest' ) assertEquals( m.calls[3][3], 'MyTestWithFailures:testOk' ) assertEquals(#m.calls[3], 3 ) assertEquals( m.calls[4][1], 'endTest' ) assertEquals( m.calls[4][3], false ) assertEquals(#m.calls[4], 3 ) assertEquals( m.calls[5][1], 'startTest' ) assertEquals( m.calls[5][3], 'MyTestWithFailures:testWithFailure1' ) assertEquals(#m.calls[5], 3 ) assertEquals( m.calls[6][1], 'addFailure' ) assertEquals(#m.calls[6], 4 ) assertEquals( m.calls[7][1], 'endTest' ) assertEquals( m.calls[7][3], true ) assertEquals(#m.calls[7], 3 ) assertEquals( m.calls[8][1], 'startTest' ) assertEquals( m.calls[8][3], 'MyTestWithFailures:testWithFailure2' ) assertEquals(#m.calls[8], 3 ) assertEquals( m.calls[9][1], 'addFailure' ) assertEquals(#m.calls[9], 4 ) assertEquals( m.calls[10][1], 'endTest' ) assertEquals( m.calls[10][3], true ) assertEquals(#m.calls[10], 3 ) assertEquals( m.calls[11][1], 'endClass' ) assertEquals(#m.calls[11], 2 ) assertEquals( m.calls[12][1], 'startClass' ) assertEquals( m.calls[12][3], 'MyTestOk' ) assertEquals(#m.calls[12], 3 ) assertEquals( m.calls[13][1], 'startTest' ) assertEquals( m.calls[13][3], 'MyTestOk:testOk1' ) assertEquals(#m.calls[13], 3 ) assertEquals( m.calls[14][1], 'endTest' ) assertEquals( m.calls[14][3], false ) assertEquals(#m.calls[14], 3 ) assertEquals( m.calls[15][1], 'startTest' ) assertEquals( m.calls[15][3], 'MyTestOk:testOk2' ) assertEquals(#m.calls[15], 3 ) assertEquals( m.calls[16][1], 'endTest' ) assertEquals( m.calls[16][3], false ) assertEquals(#m.calls[16], 3 ) assertEquals( m.calls[17][1], 'endClass' ) assertEquals(#m.calls[17], 2 ) assertEquals( m.calls[18][1], 'endSuite' ) assertEquals(#m.calls[18], 2 ) assertEquals( m.calls[19], nil ) end LuaUnit.verbosity = 2 os.exit( LuaUnit:run() )