Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```chapel
- module UnitTest {
- use TestResult;
- /*
- :class:`TestError` is a base class.
- */
- class TestError: Error {
- var details: string;
- proc init(details: string = "") {
- this.details = details;
- }
- // Message function overridden here
- override proc message() {
- return this.details;
- }
- }
- /*Assertion Error class. Raised when assert Function Failed*/
- class AssertionError: TestError {
- proc init(details: string = "") {
- super.init(details);
- }
- }
- /* TestSkipped Error Class. Raised when a test is skipped.*/
- class TestSkipped: TestError {
- proc init(details: string = "") {
- super.init(details);
- }
- }
- /* TestDependencyNotMet Error Class. Raised when a all dependency
- of a test are not met.
- */
- class TestDependencyNotMet: TestError {
- proc init(details: string = "") {
- super.init(details);
- }
- }
- // This is a dummy test so that we get capture the function signature
- private
- proc testSignature(test: Test) throws { }
- var tempFcf = testSignature;
- type argType = tempFcf.type; //Type of First Class Test Functions
- // This is jusst for printing in singular or plural form.
- private
- proc printTest(count) {
- if count > 1 {
- return "tests";
- }
- return "test";
- }
- /*A test result class that can print formatted text results to a stream.*/
- class TextTestResult: TestResult {
- var separator1 = "="* 70,
- separator2 = "-"* 70;
- proc startTest(test, indx) throws {
- super.startTest();
- stdout.write(test: string,"(",indx: string,"): ");
- }
- override proc addError(test, errMsg) throws {
- super.addError(test, errMsg);
- stdout.writeln("ERROR");
- }
- override proc addFailure(test, errMsg) throws {
- super.addFailure(test, errMsg);
- stdout.writeln("FAIL");
- }
- override proc addSuccess(test) throws {
- super.addSuccess(test);
- stdout.writeln("OK");
- }
- override proc addSkip(test, reason) throws {
- super.addSkip(test, reason);
- stdout.writeln("SKIPPED '",reason,"'");
- }
- proc printErrors() throws {
- stdout.writeln();
- this.printErrorList("ERROR", this.errors);
- this.printErrorList("FAIL", this.failures);
- }
- proc printErrorList(flavour, errors) throws {
- for (test, err) in errors {
- stdout.writeln(this.separator1);
- stdout.writeln(flavour," ",test);
- stdout.writeln(this.separator2);
- stdout.writeln(err);
- }
- }
- /* Function to print the result*/
- proc PrintResult() throws {
- var skipped = this.skippedTests();
- var run = this.testsRun - skipped;
- stdout.writeln("Run "+ run +" "+printTest(run));
- stdout.writeln();
- var infos: [1..0](string);
- if !this.wasSuccessful() {
- stdout.write("FAILED");
- var failed = this.failedTests(),
- errored = this.erroredTests();
- if failed then
- infos.push_back("failures = "+failed);
- if errored then
- infos.push_back("errors = "+errored);
- }
- else
- stdout.write("OK");
- if skipped then
- infos.push_back("skipped = "+skipped);
- if infos.size {
- stdout.write(" ");
- for info in infos do stdout.write(info," ");
- }
- stdout.write("\n");
- // for value in this.failures do stdout.writeln(value);
- // for value in this.skipped do stdout.writeln(value);
- }
- }
- /*Runs the tests*/
- proc runTest(tests: argType ...?n) throws {
- // Assuming 1 global test suite for now
- // Per-module or per-class is possible too
- var testSuite = new TestSuite();
- testSuite.addTests(tests);
- var testResult = new TextTestResult();
- var indx = 0;
- stdout.writeln("Found "+testSuite.testCount+" "+printTest(testSuite.testCount));
- for test in testSuite {
- try {
- // Create a test object per test
- var testObject = new Test();
- //test is a FCF:
- testResult.startTest(test: string, indx);
- test(testObject);
- testResult.addSuccess(test: string);
- }
- // A variety of catch statements will handle errors thrown
- catch e: AssertionError {
- testResult.addFailure(test:string, e:string);
- // print info of the assertion error
- }
- catch e: TestSkipped {
- testResult.addSkip(test:string, e:string);
- // Print info on test skipped
- }
- catch e: TestDependencyNotMet {
- // Pop test out of array and append to end
- }
- catch e {
- testResult.addError(test:string, e:string);
- }
- indx += 1;
- }
- testResult.printErrors();
- stdout.writeln(testResult.separator2);
- testResult.PrintResult();
- }
- class TestSuite {
- var testCount = 0;
- var _tests: [1..0] argType;
- proc addTest(test) lifetime this < test {
- // var tempTest = new Test();
- // param test_name = test: string;
- // if !canResolve(test_name,tempTest) then
- // compilerError(test + " is not callable");
- this._tests.push_back(test);
- this.testCount += 1;
- }
- proc addTests(tests) lifetime this < tests {
- /*if isString(tests) then
- compilerError("tests must be an iterable, not a string");*/
- for test in tests do
- this.addTest(test);
- }
- iter these() {
- for i in this._tests do
- yield i;
- }
- }
- }
- ```
- **Example**
- ```chapel
- use UnitTest;
- proc test1(test: Test) throws {
- test.assertTrue(false);
- }
- proc test2(test: Test) throws {
- test.assertTrue(false);
- }
- proc test3(test: Test) throws {
- test.skip("Skipping Test 3");
- }
- UnitTest.runTest(test1,test2,test3);
- ```
- **Current Output**
- ```bash
- Run 3 test
- FAILED
- failures= 2
- skipped= 1
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement