diff options
Diffstat (limited to 'src/tester')
-rw-r--r-- | src/tester/Runner.hx | 47 | ||||
-rw-r--r-- | src/tester/Test.hx | 30 |
2 files changed, 77 insertions, 0 deletions
diff --git a/src/tester/Runner.hx b/src/tester/Runner.hx new file mode 100644 index 0000000..77c273e --- /dev/null +++ b/src/tester/Runner.hx @@ -0,0 +1,47 @@ +package tester; + +/** + Class for running the defined tests. +**/ +class Runner { + var tests:Array<Test>; + + public function new() { + tests = new Array(); + } + + /** + Adds a new `test` to the test suite. + **/ + public function addTest(test:Test) { + tests.push(test); + } + + /** + Runs the tests of the test suite, prints the outcome of each of them + and the final results to standard output. + **/ + public function run() { + var passedTests = 0; + var failedTests = 0; + + for (index => test in tests) { + Sys.println('--- TEST ${index+1}/${tests.length}: ${test.description} ---'); + + var outcome = test.run(); + switch outcome { + case Pass: + passedTests++; + Sys.println('PASSED ${test.description}'); + case Fail(reason): + failedTests++; + Sys.println('FAILED ${test.description}: $reason'); + } + } + + Sys.println("--- RESULTS ---"); + Sys.println('Total: ${tests.length}'); + Sys.println('Passed: $passedTests'); + Sys.println('Failed: $failedTests'); + } +} diff --git a/src/tester/Test.hx b/src/tester/Test.hx new file mode 100644 index 0000000..b1e8d03 --- /dev/null +++ b/src/tester/Test.hx @@ -0,0 +1,30 @@ +package tester; + +enum Outcome { + Pass; + Fail(reason:String); +} + +/** + Class for creating a test case. + + Each test case *must* extend this class and override the `run` function. +**/ +class Test { + public var description(default, null):String; + + /** + Creates a new test case with a `description` that identifies and/or describes + what test is it. + **/ + public function new(description:String) { + this.description = description; + } + + /** + Runs the test and returns it's outcome. + **/ + public function run():Outcome { + return null; + } +} |