Skip to content

Commit 49fe4ad

Browse files
committed
Added TestSuite.as for running many testcases in a row.
1 parent 395c238 commit 49fe4ad

File tree

8 files changed

+153
-17
lines changed

8 files changed

+153
-17
lines changed

TODO.rdoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
== TODO
22

3-
.TestSuite
4-
- Combining testcases into testsuites
3+
.TestCase
4+
- Should TestCase still be able to do reporting through run or should we force tests to run through a suite to avoid duplication?
55

66
.Output
77
- Add a scrollbar to deal with long test reports

example/MtascMain.as

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import test.*
2+
import leanUnit.*
23

34
// Example usage:
45
// mtasc -cp .. -swf example.swf -header 550:400:10:080808 -main MtascMain.as
@@ -7,7 +8,9 @@ class MtascMain
78
{
89
static function main(root)
910
{
10-
var st = new StringTest()
11-
st.run()
11+
var testSuite = new TestSuite()
12+
testSuite.addCase( new StringTest() )
13+
testSuite.addCase( new ArrayTest() )
14+
testSuite.run()
1215
}
1316
}

example/example.fla

1 KB
Binary file not shown.

example/test/ArrayTest.as

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class test.ArrayTest extends leanUnit.TestCase
2+
{
3+
var array
4+
5+
function setup()
6+
{
7+
array = ['asdf', 'qwer', 'hjkl']
8+
}
9+
10+
function testLength()
11+
{
12+
assertEqual( 3, array.length )
13+
}
14+
15+
function testJoin()
16+
{
17+
assertEqual( 'asdf_qwer_hjkl', array.join('_') )
18+
assertEqual( 'asdf qwer hjkl', array.join(' ') )
19+
assertEqual( 'asdf,qwer,hjkl', array.join() )
20+
}
21+
}

leanUnit/Assertions.as

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import leanUnit.*
22

33
class leanUnit.Assertions
44
{
5-
var currentClass:String
5+
var className:String
66
var currentMethod:String
77

88
var assertionCount:Number
@@ -23,7 +23,7 @@ class leanUnit.Assertions
2323
else
2424
{
2525
Output.addFail()
26-
failures.push(new Failure(currentClass, currentMethod, message))
26+
failures.push(new Failure(className, currentMethod, message))
2727
}
2828
}
2929

leanUnit/Failure.as

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
class leanUnit.Failure
22
{
3+
var className:String
34
var method:String
45
var message:String
56

67
//-------------------------------------------------------------------
78
// CONSTRUCTOR
89
//-------------------------------------------------------------------
910

10-
function Failure(method, message)
11+
function Failure(className, method, message)
1112
{
13+
this.className = className
1214
this.method = method
1315
this.message = message
1416
}
@@ -19,7 +21,7 @@ class leanUnit.Failure
1921

2022
function toString()
2123
{
22-
return method+": "+message
24+
return className+"#"+method+" - "+message
2325
}
2426

2527
}

leanUnit/TestCase.as

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,49 @@ class leanUnit.TestCase extends Assertions
4343
var setup:Function
4444
var teardown:Function
4545

46+
//-------------------------------------------------------------------
47+
// CONSTRUCTOR
48+
//-------------------------------------------------------------------
49+
50+
function TestCase()
51+
{
52+
className = Reflection.getClassName(this)
53+
className = className.substr(className.lastIndexOf('.')+1) // remove namespace
54+
55+
reset()
56+
}
57+
4658
//-------------------------------------------------------------------
4759
// PUBLIC METHODS
4860
//-------------------------------------------------------------------
4961

50-
function run()
62+
function run( silent:Boolean )
5163
{
5264
reset()
5365

54-
var startTime = getTimer()
55-
Output.writeln("Running "+currentClass)
66+
if( !silent)
67+
{
68+
Output.writeln("Running "+className)
69+
}
5670

71+
var startTime = getTimer()
5772
for(var i=0; i<testMethods.length; i++)
5873
{
5974
runMethod(testMethods[i])
6075
}
6176
var endTime = getTimer() - startTime
6277

63-
Output.writeln()
64-
Output.writeln('Finished in '+(endTime/1000)+' seconds')
65-
66-
report()
78+
if( !silent)
79+
{
80+
Output.writeln()
81+
Output.writeln('Finished in '+(endTime/1000)+' seconds')
82+
report()
83+
}
84+
}
85+
86+
function toString()
87+
{
88+
return className
6789
}
6890

6991
//-------------------------------------------------------------------
@@ -74,8 +96,6 @@ class leanUnit.TestCase extends Assertions
7496
{
7597
failures = new Array()
7698
assertionCount = 0
77-
currentClass = Reflection.getClassName(this)
78-
currentClass = currentClass.substr(currentClass.lastIndexOf('.')+1) // remove namespace
7999
}
80100

81101
private function runMethod(methodName)

leanUnit/TestSuite.as

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
Should hold n TestCase's, run them and do a single report for all.
3+
*/
4+
5+
import leanUnit.*
6+
7+
class leanUnit.TestSuite
8+
{
9+
var testCases:Array
10+
11+
var testCount:Number = 0
12+
var assertionCount:Number = 0
13+
var failures:Array = new Array()
14+
15+
//-------------------------------------------------------------------
16+
// CONSTRUCTOR
17+
//-------------------------------------------------------------------
18+
19+
function TestSuite()
20+
{
21+
testCases = new Array()
22+
}
23+
24+
25+
//-------------------------------------------------------------------
26+
// PUBLIC FUNCTIONS
27+
//-------------------------------------------------------------------
28+
29+
function addCase( testCase:TestCase )
30+
{
31+
testCases.push( testCase )
32+
}
33+
34+
function run()
35+
{
36+
reset()
37+
38+
Output.writeln( "Running "+testCases.join(', ') )
39+
40+
var startTime = getTimer()
41+
for( var i=0; i<testCases.length; i++ )
42+
{
43+
var testCase:TestCase = testCases[i]
44+
testCase.run( true )
45+
46+
failures = merge( failures, testCase.failures )
47+
testCount += testCase.testMethods.length
48+
assertionCount += testCase.assertionCount
49+
}
50+
var endTime = getTimer() - startTime
51+
52+
Output.writeln()
53+
Output.writeln('Finished in '+(endTime/1000)+' seconds')
54+
55+
report()
56+
}
57+
58+
//-------------------------------------------------------------------
59+
// PRIVATE FUNCTIONS
60+
//-------------------------------------------------------------------
61+
62+
private function reset()
63+
{
64+
testCount = 0
65+
assertionCount = 0
66+
failures = new Array()
67+
}
68+
69+
private function merge( array:Array, array2:Array )
70+
{
71+
for( var i=0; i<array2.length; i++ )
72+
{
73+
array.push(array2[i])
74+
}
75+
return array
76+
}
77+
78+
private function report()
79+
{
80+
for( var i=0; i<failures.length; i++ )
81+
{
82+
Output.writeln()
83+
Output.writeln( (i+1)+")")
84+
Output.writeln( failures[i], "fail" )
85+
}
86+
Output.writeln()
87+
Output.writeln( testCount+" tests, "+assertionCount+" assertions, "+failures.length+" failures", failures.length > 0 ? 'fail' : 'success' )
88+
}
89+
90+
}

0 commit comments

Comments
 (0)