From 875e8b54c907c60682567a6f900cbf99bedf55c5 Mon Sep 17 00:00:00 2001 From: rc_05 Date: Tue, 14 May 2024 01:07:07 +0200 Subject: First commit. --- CHANGELOG | 3 +++ LICENSE | 21 +++++++++++++++++++++ README | 17 +++++++++++++++++ build.hxml | 5 +++++ examples/SimpleTests.hx | 40 ++++++++++++++++++++++++++++++++++++++++ generate-docs.sh | 17 +++++++++++++++++ haxelib.json | 12 ++++++++++++ src/tester/Runner.hx | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/tester/Test.hx | 30 ++++++++++++++++++++++++++++++ 9 files changed, 192 insertions(+) create mode 100644 CHANGELOG create mode 100644 LICENSE create mode 100644 README create mode 100644 build.hxml create mode 100644 examples/SimpleTests.hx create mode 100755 generate-docs.sh create mode 100644 haxelib.json create mode 100644 src/tester/Runner.hx create mode 100644 src/tester/Test.hx diff --git a/CHANGELOG b/CHANGELOG new file mode 100644 index 0000000..bbaae05 --- /dev/null +++ b/CHANGELOG @@ -0,0 +1,3 @@ +1.0.0 (Initial release) +----- +First release, everything works fine. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..870ac11 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright © 2024 rc_05 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README b/README new file mode 100644 index 0000000..5faf922 --- /dev/null +++ b/README @@ -0,0 +1,17 @@ +--- Haxe Tester --- + +A simple Haxe library for creating test cases. + +* Installation + +$ haxelib git haxe-tester https://git.rc-05.com/haxe-tester + +* Documentation + +The documentation is available at https://docs.rc-05.com/haxe-tester/git for the +unstable git repository. + +* License + +This project is licensed with the MIT License which can be viewed by +reading the LICENSE file supplied with this codebase. diff --git a/build.hxml b/build.hxml new file mode 100644 index 0000000..257fc92 --- /dev/null +++ b/build.hxml @@ -0,0 +1,5 @@ +--class-path examples +--main SimpleTests +--interp +--define analyzer-optimize +--library haxe-tester diff --git a/examples/SimpleTests.hx b/examples/SimpleTests.hx new file mode 100644 index 0000000..de065e6 --- /dev/null +++ b/examples/SimpleTests.hx @@ -0,0 +1,40 @@ +import tester.Runner; +import tester.Test.Test; +import tester.Test.Outcome; + +class Foo extends Test { + override public function run():Outcome { + if (3 < 2) { + return Pass; + } else { + return Fail("Expected 1 < 2"); + } + } +} + +class Bar extends Test { + override public function run():Outcome { + if (false) { + return Pass; + } else { + return Fail("Test is supposed to always pass."); + } + } +} + +class SimpleTests { + static function main() { + var runner = new Runner(); + var tests = [ + new Foo("Foo"), + new Bar("Bar") + ]; + + // Prepare the tests to be run. + for (test in tests) { + runner.addTest(test); + } + // Run the tests. + runner.run(); + } +} diff --git a/generate-docs.sh b/generate-docs.sh new file mode 100755 index 0000000..93459d7 --- /dev/null +++ b/generate-docs.sh @@ -0,0 +1,17 @@ +#!/bin/sh + +SOURCE_PATH="https://git.rc-05.com/haxe-tester/tree/" +VERSION=${VERSION:-git} + +haxe --class-path src \ + --no-output \ + --interp \ + --xml types.xml \ + tester + +haxelib run dox \ + -i types.xml \ + -o docs \ + --toplevel-package tester \ + -D version $VERSION \ + -D source-path $SOURCE_PATH diff --git a/haxelib.json b/haxelib.json new file mode 100644 index 0000000..859e4b8 --- /dev/null +++ b/haxelib.json @@ -0,0 +1,12 @@ +{ + "name": "haxe-tester", + "url" : "https://git.rc-05.com/haxe-tester", + "license": "MIT", + "tags": ["cross", "test", "suite", "testsuite"], + "description": "Simple Haxe library for creating test cases.", + "version": "1.0.0", + "classPath": "src/", + "releasenote": "Initial release", + "contributors": ["rc_05"], + "dependencies": {} +} 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; + + 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; + } +} -- cgit v1.2.3