Lines Matching +full:end +full:- +full:failure
1 --[[
10 ]]--
15 -- private exported functions (for testing)
20 --[[ Some people like assertEquals( actual, expected ) and some people prefer
22 ]]--
28 -- set this to false to debug luaunit
36 -- set EXPORT_ASSERT_TO_GLOBALS to have all asserts visible as global values
37 -- EXPORT_ASSERT_TO_GLOBALS = true
39 -- we need to keep a copy of the script args before it is overriden
42 M.FAILURE_PREFIX = 'LuaUnit test FAILURE: ' -- prefix string for failed tests
46 -h, --help: Print this help
47 --version: Print version information
48 -v, --verbose: Increase verbosity
49 -q, --quiet: Set verbosity to minimum
50 -e, --error: Stop on first error
51 -f, --failure: Stop on first failure or error
52 -o, --output OUTPUT: Set output type to OUTPUT
54 -n, --name NAME: For junit only, mandatory name of xml file
55 -p, --pattern PATTERN: Execute all test names matching the Lua PATTERN
62 ----------------------------------------------------------------
63 --
64 -- general utility functions
65 --
66 ----------------------------------------------------------------
76 number = function(a, b) return a < b end,
77 string = function(a, b) return a < b end,
78 other = function(a, b) return tostring(a) < tostring(b) end,
86 end
90 end
93 -- Returns a sequence consisting of t's keys, sorted.
98 end
102 end
106 -- Equivalent of the next() function of table iteration, but returns the
107 -- keys in sorted order (see __genSortedIndex and crossTypeSort).
108 -- The state is a temporary variable during iteration and contains the
109 -- sorted key table (state.sortedIdx). It also stores the last index (into
110 -- the keys) used by the iteration, to find the next one quickly.
113 --print("sortedNext: control = "..tostring(control) )
115 -- start of iteration
119 end
121 -- normally, we expect the control variable to match the last key used
123 -- strange, we have to find the next value by ourselves
124 -- the key table is sorted in crossTypeSort() order! -> use bisection
130 if key == control then break; end -- key found (and thus prev index)
132 -- key < control, continue search "right" (towards upper bound)
135 -- key > control, continue search "left" (towards lower bound)
136 upper = state.lastIdx - 1
137 end
139 if lower > upper then -- only true if the key wasn't found, ...
140 state.lastIdx = count -- ... so ensure no match for the code below
141 end
142 end
144 -- proceed by retrieving the next value (or nil) from the sorted keys
149 end
151 -- getting here means returning `nil`, which will end the iteration
152 end
155 -- Equivalent of the pairs() function on tables. Allows to iterate in
156 -- sorted order. As required by "generic for" loops, this will return the
157 -- iterator (function), an "invariant state", and the initial control value.
158 -- (see http://www.lua.org/pil/7.2.html)
160 end
164 -- Split text into a list consisting of the strings in text,
165 -- separated by strings matching delimiter (which may be a pattern).
166 -- example: strsplit(",%s*", "Anna, Bob, Charlie,Dolores")
167 if string.find("", delimiter, 1, true) then -- this would result in endless loops
169 end
173 if first then -- found?
174 table.insert(list, text:sub(pos, first - 1))
179 end
180 end
182 end
186 -- return true if s has a newline
188 end
192 -- Prefix all the lines of s with prefix
194 end
198 -- return true if s matches completely the pattern from index start to index end
199 -- return false in every other cases
200 -- if start is nil, matches from the beginning of the string
201 -- if final is nil, matches to the end of the string
207 end
211 -- Return s escaped for XML attributes
212 -- escapes table:
213 -- " "
214 -- ' '
215 -- < <
216 -- > >
217 -- & &
226 end
230 -- Return s escaped for CData section, escapes: "]]>"
232 end
236 --[[
237 -- Example of a traceback:
277 -- first line is "stack traceback": KEEP
278 -- next line may be luaunit line: REMOVE
279 -- next lines are call in the program under testOk: REMOVE
280 -- next lines are calls from luaunit to call the program under test: KEEP
282 -- Strategy:
283 -- keep first line
284 -- remove lines that are part of luaunit
285 -- kepp lines until we hit a luaunit line
289 -- return true if line of stack trace comes from inside luaunit
291 end
293 -- print( '<<'..stackTrace..'>>' )
296 -- print( prettystr(t) )
300 -- remove lines that are still part of luaunit
302 -- print('Removing : '..t[idx] )
304 end
306 -- keep lines until we hit luaunit again
308 -- print('Keeping : '..t[idx] )
310 end
312 -- remove remaining luaunit lines
314 -- print('Removing : '..t[idx] )
316 end
318 -- print( prettystr(t) )
321 end
328 if keeponeline then v = v:gsub("\n", "\\n") end
330 -- use clever delimiters according to content:
331 -- enclose with single quotes if string contains ", but no '
334 end
335 -- use double quotes otherwise, escape embedded "
339 --if v.__class__ then
340 -- return string.gsub( tostring(v), 'table', v.__class__ )
341 --end
343 end
346 end
349 --[[ Better string conversion, to display nice variable content:
354 ]]--
358 -- some table contain recursive references,
359 -- so we must recompute the value by including all table references
360 -- else the result looks like crap
363 end
365 end
369 --[[
384 -- line break(s) detected, add padding
386 end
388 end
392 -- like prettystr but do not enclose with "" if the string is just alphanumerical
393 -- this is better for displaying table keys who are often simple strings
396 end
398 end
414 -- for the sequential part of tables, we'll skip the "<key>=" output
419 end
420 if recursionTable[v] then -- recursion detected!
426 end
429 end
431 -- set dispOnMultLines if the maximum LINE_LENGTH would be exceeded
438 end
439 end
442 -- adjust with length of separator(s):
443 -- two items need 1 sep, three items two seps, ... plus len of '{}'
445 totalLength = totalLength + TABLE_TOSTRING_SEP_LEN * (count - 1)
446 end
448 end
450 -- now reformat the result table (currently holding element strings)
452 local indentString = string.rep(" ", indentLevel - 1)
458 end
460 table.insert(result, 1, "<"..tostring(tbl).."> ") -- prepend table ref
461 end
463 end
464 M.private._table_tostring = _table_tostring -- prettystr_sub() needs it
471 -- if we wanted recursive items content comparison, we could use
472 -- _is_table_items_equals(v, expected) but one level of just comparing
473 -- items is sufficient
476 end
480 end
481 end
482 end
483 end
484 end
486 end
493 end
494 end
498 end
499 end
505 end
507 end
513 end
518 -- If the keys are tables, things get a bit tricky here as we
519 -- can have _is_table_equals(k1, k2) and t[k1] ~= t[k2]. So we
520 -- collect actual's table keys, group them by length for
521 -- performance, and then for each table key in expected we look
522 -- it up in actualTableKeys.
523 if not actualTableKeys[#k] then actualTableKeys[#k] = {} end
528 end
529 end
530 end
535 if not candidates then return false end
540 -- Remove the candidate we matched against from the list
541 -- of candidates, so each key in actual can only match
542 -- one key in expected.
545 end
546 end
547 if not(found and _is_table_equals(actual[found], v)) then return false end
551 end
552 end
553 end
557 -- if there are any keys left in any actualTableKeys[i] then
558 -- that is a key in actual with no matching key in expected,
559 -- and so the tables aren't equal.
560 if next(keys) then return false end
561 end
562 end
569 end
571 end
574 local function failure(msg, level) function
575 -- raise an error indicating a test failure
576 -- for error() compatibility we adjust "level" here (by +1), to report the
577 -- calling context
579 end
582 -- failure with printf-style formatted message and given error level
583 failure(string.format(...), (level or 1) + 1)
584 end
588 -- printf-style error()
590 end
592 ----------------------------------------------------------------
593 --
594 -- assertions
595 --
596 ----------------------------------------------------------------
601 end
605 end
608 end
611 -- assert that calling f with the arguments will raise an error
612 -- example: assertError( f, 1, 2 ) => f(1,2) should generate an error
614 failure( "Expected an error when calling function but no error generated", 2 )
615 end
616 end
620 failure("expected: true, actual: " ..prettystr(value), 2)
621 end
622 end
626 failure("expected: false, actual: " ..prettystr(value), 2)
627 end
628 end
632 failure("expected: nil, actual: " ..prettystr(value), 2)
633 end
634 end
638 failure("expected non nil value, received nil", 2)
639 end
640 end
645 failure( errorMsgEquality(actual, expected), 2 )
646 end
648 failure( errorMsgEquality(actual, expected), 2 )
650 failure( errorMsgEquality(actual, expected), 2 )
651 end
652 end
654 -- Help Lua in corner cases like almostEquals(1.1, 1.0, 0.1), which by default
655 -- may not work. We need to give margin a small boost; EPSILON defines the
656 -- default value to use for this:
662 end
665 end
667 return math.abs(expected - actual) <= realmargin
668 end
671 -- check that two floats are close by margin
675 end
678 end
679 end
684 end
689 end
692 end
694 end
697 -- check that two floats are not close by margin
701 end
704 end
705 end
708 -- this relies on lua string.find function
709 -- a string always contains the empty string
714 end
715 end
718 -- this relies on lua string.find function
719 -- a string always contains the empty string
724 end
725 end
728 -- this relies on lua string.find function
729 -- a string always contains the empty string
734 end
735 end
738 -- this relies on lua string.find function
739 -- a string always contains the empty string
744 end
745 end
748 -- Verify a full match for the string
749 -- for a partial match, simply use assertStrContains with useRe set to true
754 end
755 end
758 -- assert that calling f with the arguments will raise an error
759 -- example: assertError( f, 1, 2 ) => f(1,2) should generate an error
762 … failure( 'No error generated when calling function but expected error: "'..expectedMsg..'"', 2 )
763 end
768 end
769 end
772 -- assert that calling f with the arguments will raise an error
773 -- example: assertError( f, 1, 2 ) => f(1,2) should generate an error
776 …failure( 'No error generated when calling function but expected error containing: '..prettystr(par…
777 end
782 end
783 end
786 -- assert that calling f with the arguments will raise an error
787 -- example: assertError( f, 1, 2 ) => f(1,2) should generate an error
790 …failure( 'No error generated when calling function but expected error matching: "'..expectedMsg..'…
791 end
796 end
797 end
799 --[[
804 M.assertIsXxx(value) -> ensure that type(value) conforms to "xxx"
810 local typeExpected = funcName:match("^assertIs([A-Z]%a*)$")
811 -- Lua type() always returns lowercase, also make sure the match() succeeded
819 end
820 end
821 end
823 --[[
824 Add non-type assertion functions to the module table M. Each of these functions
828 M.assertNotIsXxx(value) -> ensure that type(value) is not "xxx"
834 local typeUnexpected = funcName:match("^assertNotIs([A-Z]%a*)$")
835 -- Lua type() always returns lowercase, also make sure the match() succeeded
843 end
844 end
845 end
851 end
855 end
856 end
862 end
865 end
866 end
869 -- checks that the items of table expected
870 -- are contained in table actual. Warning, this function
871 -- is at least O(n^2)
876 end
877 end
879 ----------------------------------------------------------------
880 -- Compatibility layer
881 ----------------------------------------------------------------
883 -- for compatibility with LuaUnit v2.x
888 -- In LuaUnit version <= 2.1 , this function was necessary to include
889 -- a test function inside the global test suite. Nowadays, the functions
890 -- are simply run directly as part of the test discovery process.
891 -- so just do nothing !
893 --[[
899 end
902 end
906 end
909 -- { official function name , alias }
911 -- general assertions
933 -- type assertions: assertIsXXX -> assert_is_xxx
943 -- type assertions: assertIsXXX -> assertXxx
953 -- type assertions: assertIsXXX -> assert_xxx (luaunit v2 compat)
963 -- type assertions: assertNotIsXXX -> assert_not_is_xxx
973 -- type assertions: assertNotIsXXX -> assertNotXxx (luaunit v2 compat)
983 -- type assertions: assertNotIsXXX -> assert_not_xxx
993 -- all assertions with Coroutine duplicate Thread assertions
1004 -- Create all aliases in M
1012 end
1013 end
1015 ----------------------------------------------------------------
1016 --
1017 -- Outputters
1018 --
1019 ----------------------------------------------------------------
1021 ----------------------------------------------------------------
1022 -- class TapOutput
1023 ----------------------------------------------------------------
1026 local TapOutput = { __class__ = 'TapOutput' } -- class
1027 local TapOutput_MT = { __index = TapOutput } -- metatable
1029 -- For a good reference for TAP format, check: http://testanything.org/tap-specification.html
1033 end
1037 end
1041 end
1042 end
1043 function TapOutput:startTest(testName) end
1049 end
1052 end
1053 end
1059 end
1060 end
1062 function TapOutput:endClass() end
1067 end
1070 -- class TapOutput end
1072 ----------------------------------------------------------------
1073 -- class JUnitOutput
1074 ----------------------------------------------------------------
1076 -- See directory junitxml for more information about the junit format
1077 local JUnitOutput = { __class__ = 'JUnitOutput' } -- class
1078 local JUnitOutput_MT = { __index = JUnitOutput } -- metatable
1083 end
1086 -- open xml file early to deal with errors
1088 error('With Junit, an output filename must be supplied with --name!')
1089 end
1090 if string.sub(self.fname,-4) ~= '.xml' then
1092 end
1096 end
1100 end
1104 end
1105 end
1108 end
1111 print('# Failure: ' .. node.msg)
1112 -- print('# ' .. node.stackTrace)
1113 end
1117 -- print('# ' .. node.stackTrace)
1118 end
1121 end
1124 end
1129 -- XML file writing
1130 self.fd:write('<?xml version="1.0" encoding="UTF-8" ?>\n')
1138 -- XXX please include system name and version if possible
1146 end
1148 end
1150 -- Next two lines are needed to validate junit ANT xsd, but really not useful in general:
1151 self.fd:write(' <system-out/>\n')
1152 self.fd:write(' <system-err/>\n')
1158 end
1161 -- class TapOutput end
1163 ----------------------------------------------------------------
1164 -- class TextOutput
1165 ----------------------------------------------------------------
1167 --[[
1169 -- Python Non verbose:
1175 ERROR / FAILURE: TestName (testfile.testclass)
1176 ---------
1180 then --------------
1184 -- Python Verbose:
1189 then --------------
1193 -- Ruby:
1200 -- Ruby:
1207 1) Failure:
1215 -- Java Junit
1218 There was 1 failure:
1229 -- Maven
1232 -------------------------------------------------------
1234 -------------------------------------------------------
1237 0.03 sec <<< FAILURE!
1247 -- LuaUnit
1248 ---- non verbose
1250 ---- verbose
1252 ----
1254 * number) ERROR or FAILURE: TestName
1257 * number) ERROR or FAILURE: TestName
1260 then --------------
1267 local TextOutput = { __class__ = 'TextOutput' } -- class
1268 local TextOutput_MT = { __index = TextOutput } -- metatable
1273 end
1278 end
1279 end
1282 -- display nothing when starting a new class
1283 end
1288 end
1289 end
1292 -- nothing
1293 end
1296 -- nothing
1297 end
1305 end
1310 --[[
1311 -- find out when to do this:
1314 end
1317 -- write only the first character of status
1319 end
1320 end
1321 end
1324 -- nothing
1325 end
1327 function TextOutput:displayOneFailedTest( index, failure )
1328 print(index..") "..failure.testName )
1329 print( failure.msg )
1330 print( failure.stackTrace )
1332 end
1335 if self.result.notPassedCount == 0 then return end
1337 print("-------------")
1340 end
1341 end
1348 end
1354 end
1355 end
1357 -- class TextOutput end
1360 ----------------------------------------------------------------
1361 -- class NilOutput
1362 ----------------------------------------------------------------
1365 --print(42)
1367 end
1369 local NilOutput = { __class__ = 'NilOuptut' } -- class
1370 local NilOutput_MT = { __index = nopCallable } -- metatable
1374 end
1376 ----------------------------------------------------------------
1377 --
1378 -- class LuaUnit
1379 --
1380 ----------------------------------------------------------------
1391 end
1395 end
1397 -----------------[[ Utility methods ]]---------------------
1400 -- return "aObject" if it is a function, and nil otherwise
1401 if 'function' == type(aObject) then return aObject end
1402 end
1405 -- return true if aName contains a class + a method name in the form class:method
1407 end
1410 -- return a pair className, methodName for a name in the form class:method
1411 -- return nil if not a class + method name
1412 -- name is class + method
1415 if not hasMethod then return nil end
1417 className = string.sub(someName,1,hasMethod-1)
1419 end
1422 -- return true is the name matches the name of a test method
1423 -- default rule is that is starts with 'Test' or with 'test'
1425 end
1428 -- return true is the name matches the name of a test
1429 -- default rule is that is starts with 'Test' or with 'test'
1431 end
1434 -- return a list of all test names in the global namespace
1435 -- that match LuaUnit.isTestName
1441 end
1442 end
1445 end
1448 -- parse the command line
1449 -- Supported command line parameters:
1450 -- --verbose, -v: increase verbosity
1451 -- --quiet, -q: silence output
1452 -- --error, -e: treat errors as fatal (quit program)
1453 -- --output, -o, + name: select output type
1454 -- --pattern, -p, + pattern: run test matching pattern, may be repeated
1455 -- --name, -n, + fname: name of output file for junit, default to stdout
1456 -- [testnames, ...]: run selected test names
1457 --
1458 -- Returns a table with the following fields:
1459 -- verbosity: nil, M.VERBOSITY_DEFAULT, M.VERBOSITY_QUIET, M.VERBOSITY_VERBOSE
1460 -- output: nil, 'tap', 'junit', 'text', 'nil'
1461 -- testNames: nil or a list of test names to run
1462 -- pattern: nil or a list of patterns
1472 end
1475 if option == '--help' or option == '-h' then
1478 elseif option == '--version' then
1481 elseif option == '--verbose' or option == '-v' then
1484 elseif option == '--quiet' or option == '-q' then
1487 elseif option == '--error' or option == '-e' then
1490 elseif option == '--failure' or option == '-f' then
1493 elseif option == '--output' or option == '-o' then
1496 elseif option == '--name' or option == '-n' then
1499 elseif option == '--pattern' or option == '-p' then
1502 end
1504 end
1518 end
1520 end
1522 end
1530 if cmdArg:sub(1,1) == '-' then
1537 end
1538 end
1539 end
1540 end
1544 end
1548 end
1552 end
1555 end
1560 end
1565 end
1568 -- check if any of patternFilter is contained in expr. If so, return true.
1569 -- return false if None of the patterns are contained in expr
1570 -- if patternFilter is nil, return true (no filtering)
1573 end
1578 end
1579 end
1582 end
1584 ----------------------------------------------------------------
1585 -- class NodeStatus
1586 ----------------------------------------------------------------
1588 local NodeStatus = { __class__ = 'NodeStatus' } -- class
1589 local NodeStatus_MT = { __index = NodeStatus } -- metatable
1592 -- values of status
1602 end
1606 -- useless but we know it's the field we want to use
1609 end
1615 end
1621 end
1625 end
1628 -- print('hasFailure: '..prettystr(self))
1630 end
1634 end
1638 end
1648 {' <failure type="', xmlEscape(self.msg), '">\n',
1650 ']]></failure>\n'})
1651 end
1652 return ' <passed/>\n' -- (not XSD-compliant! normally shouldn't get here)
1653 end
1655 --------------[[ Output methods ]]-------------------------
1658 -- return status line string according to results
1664 end
1667 end
1670 end
1672 s = s..string.format(", %d non-selected", result.nonSelectedCount )
1673 end
1675 end
1689 self.result.startIsodate = os.date('%Y-%m-%dT%H:%M:%S')
1703 end
1708 end
1721 end
1724 -- "err" is expected to be a table / result from protectedCall()
1725 if err.status == NodeStatus.PASS then return end
1729 --[[ As a first approach, we will report only one error or one failure for one test.
1731 However, we can have the case where the test is in failure, and the teardown is in error.
1732 … In such case, it's a good idea to report both a failure and an error in the test suite. This is
1739 -- if the node is already in failure/error, just don't report the new error (see above)
1740 if node.status ~= NodeStatus.PASS then return end
1752 end
1753 end
1757 -- print( 'endTest() '..prettystr(node))
1758 -- print( 'endTest() '..prettystr(node:isNotPassed()))
1759 node.duration = os.clock() - node.startTime
1767 -- Runtime error - abort test execution as requested by
1768 -- "--error" option. This is done by setting a special
1769 -- flag that gets handled in runSuiteByInstances().
1772 end
1775 -- Failure - abort test execution as requested by
1776 -- "--failure" option. This is done by setting a special
1777 -- flag that gets handled in runSuiteByInstances().
1780 end
1781 end
1783 end
1787 end
1791 error('LuaUnit:endSuite() -- suite was already ended' )
1792 end
1793 self.result.duration = os.clock()-self.result.startTime
1796 -- Expose test counts for outputter's endSuite(). This could be managed
1797 -- internally instead, but unit tests (and existing use cases) might
1798 -- rely on these fields being present.
1804 end
1807 -- default to text
1808 -- tap produces results according to TAP format
1812 end
1816 end
1820 end
1824 end
1826 end
1828 --------------[[ Runner ]]-----------------
1831 -- if classInstance is nil, this is just a function call
1832 -- else, it's method of a class being called.
1835 -- transform error into a table, adding the traceback information
1841 end
1845 -- stupid Lua < 5.2 does not allow xpcall with arguments so let's use a workaround
1846 ok, err = xpcall( function () methodInstance(classInstance) end, err_handler )
1848 ok, err = xpcall( function () methodInstance() end, err_handler )
1849 end
1852 end
1854 -- determine if the error was a failed test:
1855 -- We do this by stripping the failure prefix from the error message,
1856 -- while keeping track of the gsub() count. A non-zero value -> failure
1861 end
1863 -- reformat / improve the stack trace
1864 if prettyFuncName then -- we do have the real method name
1866 end
1869 end
1871 return err -- return the error "object" (table)
1872 end
1876 -- When executing a test function, className and classInstance must be nil
1877 -- When executing a class method, all parameters must be set
1881 end
1889 end
1894 end
1897 end
1901 -- run setUp first (if any)
1909 end
1910 end
1912 -- run testMethod()
1915 end
1917 -- lastly, run tearDown (if any)
1925 end
1926 end
1929 end
1932 -- add all test methods of classInstance to result
1936 end
1937 end
1938 end
1941 …-- expand all classes (provided as {className, classInstance}) to a list of {className.methodName,…
1942 -- functions and methods remain untouched
1952 end
1958 end
1962 end
1963 end
1964 end
1967 end
1972 -- local name, instance = v[1], v[2]
1977 end
1978 end
1980 end
1983 -- Run an explicit list of tests. All test instances and names must be supplied.
1984 -- each test must be one of:
1985 -- * { function name, function instance }
1986 -- * { class name, class instance }
1987 -- * { class.method name, class instance }
2009 end
2011 end
2012 end
2013 if self.result.aborted then break end -- "--error" or "--failure" option triggered
2014 end
2018 end
2023 print("LuaUnit ABORTED (as requested by --error or --failure option)")
2024 os.exit(-2)
2025 end
2026 end
2029 -- Run an explicit list of test names
2042 end
2046 end
2051 end
2054 -- for functions and classes
2057 end
2061 end
2065 end
2068 end
2071 end
2074 -- Run some specific test classes.
2075 -- If no arguments are passed, run the class names specified on the
2076 -- command line. If no class name is specified on the command line
2077 -- run all classes whose name starts with 'Test'
2078 --
2079 -- If arguments are passed, they must be strings of the class names
2080 -- that you want to run or generic command line arguments (-o, -p, -v, ...)
2084 end
2090 -- run was called with the syntax M.LuaUnit:runSuite()
2091 -- we support both M.LuaUnit.run() and M.LuaUnit:run()
2092 -- strip out the first argument
2094 end
2098 end
2102 print(val) -- error message
2105 os.exit(-1)
2106 end
2110 -- We expect these option fields to be either `nil` or contain
2111 -- valid values, so it's safe to always copy them directly.
2119 print('With junit output, a filename must be supplied with -n or --name')
2120 os.exit(-1)
2121 end
2126 print(val) -- error message
2129 os.exit(-1)
2130 end
2131 end
2136 end
2137 -- class LuaUnit
2139 -- For compatbility with LuaUnit v2
2145 end