SHARE
TWEET

Untitled

a guest Jun 16th, 2019 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ```chapel
  2. module UnitTest {
  3.   use TestResult;
  4.   /*
  5.     :class:`TestError` is a base class.
  6.   */
  7.   class TestError: Error {
  8.     var details: string;
  9.  
  10.     proc init(details: string = "") {
  11.       this.details = details;  
  12.     }
  13.    
  14.     // Message function overridden here
  15.     override proc message() {
  16.       return this.details;
  17.     }
  18.   }
  19.  
  20.   /*Assertion Error class. Raised when assert Function Failed*/
  21.   class AssertionError: TestError {
  22.     proc init(details: string = "") {
  23.       super.init(details);
  24.     }
  25.   }
  26.  
  27.   /* TestSkipped Error Class. Raised when a test is skipped.*/
  28.   class TestSkipped: TestError {
  29.     proc init(details: string = "") {
  30.       super.init(details);
  31.     }
  32.   }
  33.  
  34.   /* TestDependencyNotMet Error Class. Raised when a all dependency
  35.     of a test are not met.
  36.   */
  37.   class TestDependencyNotMet: TestError {
  38.     proc init(details: string = "") {
  39.       super.init(details);
  40.     }
  41.   }
  42.  
  43.   // This is a dummy test so that we get capture the function signature
  44.   private
  45.   proc testSignature(test: Test) throws { }
  46.   var tempFcf = testSignature;
  47.   type argType = tempFcf.type;  //Type of First Class Test Functions
  48.  
  49.   // This is jusst for printing in singular or plural form.
  50.   private
  51.   proc printTest(count) {
  52.     if count > 1 {
  53.       return "tests";
  54.     }
  55.     return "test";
  56.   }
  57.  
  58.   /*A test result class that can print formatted text results to a stream.*/
  59.   class TextTestResult: TestResult {
  60.     var separator1 = "="* 70,
  61.         separator2 = "-"* 70;
  62.    
  63.     proc startTest(test, indx) throws {
  64.       super.startTest();
  65.       stdout.write(test: string,"(",indx: string,"): ");
  66.     }
  67.  
  68.     override proc addError(test, errMsg) throws {
  69.       super.addError(test, errMsg);
  70.       stdout.writeln("ERROR");
  71.     }
  72.  
  73.     override proc addFailure(test, errMsg) throws {
  74.       super.addFailure(test, errMsg);
  75.       stdout.writeln("FAIL");
  76.     }
  77.  
  78.     override proc addSuccess(test) throws {
  79.       super.addSuccess(test);
  80.       stdout.writeln("OK");
  81.     }
  82.  
  83.     override proc addSkip(test, reason) throws {
  84.       super.addSkip(test, reason);
  85.       stdout.writeln("SKIPPED '",reason,"'");
  86.     }
  87.  
  88.     proc printErrors() throws {
  89.       stdout.writeln();
  90.       this.printErrorList("ERROR", this.errors);
  91.       this.printErrorList("FAIL", this.failures);
  92.     }
  93.  
  94.     proc printErrorList(flavour, errors) throws {
  95.       for (test, err) in errors {
  96.         stdout.writeln(this.separator1);
  97.         stdout.writeln(flavour," ",test);
  98.         stdout.writeln(this.separator2);
  99.         stdout.writeln(err);
  100.       }
  101.     }
  102.  
  103.    
  104.     /* Function to print the result*/
  105.     proc PrintResult() throws {
  106.       var skipped = this.skippedTests();
  107.       var run = this.testsRun - skipped;
  108.       stdout.writeln("Run "+ run +" "+printTest(run));
  109.       stdout.writeln();
  110.       var infos: [1..0](string);
  111.       if !this.wasSuccessful() {
  112.         stdout.write("FAILED");
  113.         var failed = this.failedTests(),
  114.             errored = this.erroredTests();
  115.         if failed then
  116.           infos.push_back("failures = "+failed);
  117.         if errored then
  118.           infos.push_back("errors = "+errored);
  119.       }
  120.       else
  121.         stdout.write("OK");
  122.       if skipped then
  123.         infos.push_back("skipped = "+skipped);
  124.       if infos.size {
  125.         stdout.write(" ");
  126.         for info in infos do stdout.write(info," ");
  127.       }
  128.       stdout.write("\n");
  129.       // for value in this.failures do stdout.writeln(value);
  130.       // for value in this.skipped do stdout.writeln(value);
  131.     }
  132.   }
  133.   /*Runs the tests*/
  134.   proc runTest(tests: argType ...?n) throws {
  135.  
  136.     // Assuming 1 global test suite for now
  137.     // Per-module or per-class is possible too
  138.     var testSuite = new TestSuite();
  139.     testSuite.addTests(tests);
  140.     var testResult = new TextTestResult();
  141.     var indx = 0;
  142.     stdout.writeln("Found "+testSuite.testCount+" "+printTest(testSuite.testCount));
  143.     for test in testSuite {
  144.       try {
  145.         // Create a test object per test
  146.         var testObject = new Test();
  147.         //test is a FCF:
  148.         testResult.startTest(test: string, indx);
  149.         test(testObject);
  150.         testResult.addSuccess(test: string);
  151.       }
  152.       // A variety of catch statements will handle errors thrown
  153.       catch e: AssertionError {
  154.         testResult.addFailure(test:string, e:string);
  155.         // print info of the assertion error
  156.       }
  157.       catch e: TestSkipped {
  158.         testResult.addSkip(test:string, e:string);
  159.         // Print info on test skipped
  160.       }
  161.       catch e: TestDependencyNotMet {
  162.         // Pop test out of array and append to end
  163.       }
  164.       catch e {
  165.         testResult.addError(test:string, e:string);
  166.       }
  167.       indx += 1;
  168.     }
  169.     testResult.printErrors();
  170.     stdout.writeln(testResult.separator2);
  171.     testResult.PrintResult();
  172.   }
  173.  
  174.   class TestSuite {
  175.     var testCount = 0;
  176.     var _tests: [1..0] argType;
  177.    
  178.     proc addTest(test) lifetime this < test {
  179.       // var tempTest = new Test();
  180.       // param test_name = test: string;
  181.       // if !canResolve(test_name,tempTest) then
  182.       //   compilerError(test + " is not callable");
  183.       this._tests.push_back(test);
  184.       this.testCount += 1;
  185.     }
  186.  
  187.     proc addTests(tests) lifetime this < tests {
  188.       /*if isString(tests) then
  189.         compilerError("tests must be an iterable, not a string");*/
  190.       for test in tests do
  191.         this.addTest(test);
  192.     }
  193.    
  194.     iter these() {
  195.       for i in this._tests do
  196.         yield i;
  197.     }
  198.   }
  199.  
  200. }
  201. ```
  202.  
  203.  
  204. **Example**
  205. ```chapel
  206. use UnitTest;
  207.  
  208. proc test1(test: Test) throws {
  209.   test.assertTrue(false);
  210. }
  211.  
  212. proc test2(test: Test) throws {
  213.   test.assertTrue(false);
  214. }
  215.  
  216. proc test3(test: Test) throws {
  217.   test.skip("Skipping Test 3");
  218. }
  219. UnitTest.runTest(test1,test2,test3);
  220. ```
  221.  
  222. **Current Output**
  223. ```bash
  224. Run 3 test
  225. FAILED
  226. failures= 2
  227. skipped= 1
  228. ```
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top