more core docs
[io.git] / libs / iovm / io / UnitTest.io
bloba5000be0ced4e92d6c4ed3363a4f20587dad5f03
1 UnitTest := Object clone do(
2 verbose := method(s,
3 nil
4 //writeln(s)
7 docCategory("Testing")
8 setUp := method(nil)
9 tearDown := method(nil)
10 init := method(self exceptions := List clone)
11 testSlotNames := method(
12 names := self slotNames select(name, name beginsWithSeq("test"))
13 names sortByKey(name, self getSlot(name) message lineNumber)
16 testCount := method(testSlotNames size)
18 run := method(
19 testSlotNames foreach(n,
20 self setUp
21 verbose(" " .. n)
22 e := try(stopStatus(self doString(n)))
23 if(e,
24 write(" ", n, " - failed")
25 exceptions append(e)
26 write("\n")
27 writeln(e showStack)
28 //System exit
30 //Scheduler currentCoroutine yield
31 self tearDown
32 //Collector collect
34 self ?cleanUp
37 fail := method(Exception raise("fail"))
39 assertEquals := method(a, b, m,
40 //writeln("assertEquals1 call message = ", call message type)
41 mm := call message
42 if(m == nil, m = mm)
43 d := m argAt(0) code .. " != " .. call argAt(1) code
44 if(a != b, Exception raise("[" .. d .. "] [" .. a .. " != " .. b .. "]"))
45 //writeln("assertEquals2")
48 assertNotEquals := method(a, b, if(a == b, Exception raise(a .. " == " .. b)))
50 assertSame := method(a, b, assertEquals(a uniqueId, b uniqueId, call message))
51 assertNotSame := method(a, b, assertNotEquals(a uniqueId, b uniqueId, call message))
52 assertNil := method(a, assertEquals(a, nil, call message))
53 assertNotNil := method(a, assertNotEquals(a, nil, call message))
54 assertTrue := method(a, assertEquals(a, true, call message))
55 assertFalse := method(a, assertEquals(a, false, call message))
57 assertRaisesException := method(
58 e := try(
59 stopStatus(call evalArgAt(0))
60 writeln("Should have raised Exception")
62 e ifNil(Exception raise("Should have raised Exception"))
65 knownBug := method(
66 //writeln(" [known bug: ", call argAt(0) code, "]")
69 assertEqualsWithinDelta := method(expected, actual, delta,
70 if(((expected - actual) abs > delta),
71 Exception raise("expected " .. expected .. " but was " .. actual .. " (allowed delta: " .. delta .. ")")
76 TestSuite := Object clone do(
77 verbose := method(s,
78 nil
79 //writeln(s)
82 docCategory("Testing")
84 name := method(
85 path asMutable pathComponent lastPathComponent
88 newSlot("path", ".")
90 run := method(
91 verbose("\n" .. name)
92 unitTestFiles := Directory with(launchPath) files select(f, f name endsWithSeq("Test.io"))
93 exceptions := List clone
94 testCount := 0
96 unitTestFiles foreach(f,
97 1 repeat(
98 verbose(" " .. f name fileName)
99 test := Lobby doString(f contents, f path)
100 //Collector collect
101 test run
103 testCount = testCount + test testCount
104 exceptions appendSeq(test exceptions)
107 verbose(" ---------------")
108 //Collector collect
109 //if(testCount == 0, write("(no tests)"); File standardOutput flush; return(0))
110 if(exceptions size > 0) then(
111 //writeln(" FAILED ", testCount, " tests, ", exceptions size, " failures\n")
112 return(exceptions size)
113 ) else(
114 //writeln(" PASSED ", testCount, " tests\n")
115 return(0)