Frameworks/OJUnit/OJTestRunnerText.j
Go to the documentation of this file.

    00001 @import <Foundation/Foundation.j>
    00002 @import "OJTestCase.j"
    00003 @import "OJTestSuite.j"
    00004 @import "OJTestResult.j"
    00005 @import "OJTestListenerText.j"
    00006 @import <OJCov/OJCoverageListener.j>
    00007 @import <OJCov/OJCoverageReporter.j>
    00008
    00009 var OS = require("os");
    00010 var stream = require("narwhal/term").stream;
    00011
    00012 @implementation OJTestRunnerText : CPObject
    00013 {
    00014     OJTestListener _listener;
    00015 }
    00016
    00017 - (id)init
    00018 {
    00019     if (self = [super init])
    00020     {
    00021         _listener = [[OJTestListenerText alloc] init];
    00022     }
    00023     return self;
    00024 }
    00025
    00026 - (OJTest)getTest:(CPString)suiteClassName
    00027 {
    00028     var testClass = objj_lookUpClass(suiteClassName);
    00029
    00030     if (testClass)
    00031     {
    00032         var suite = [[OJTestSuite alloc] initWithClass:testClass];
    00033         return suite;
    00034     }
    00035
    00036     CPLog.warn("unable to get tests");
    00037     return nil;
    00038 }
    00039
    00040 - (void)startWithArguments:(CPArray)args
    00041 {
    00042     if (args.length === 0)
    00043     {
    00044         [self report];
    00045         return;
    00046     }
    00047
    00048     var testCaseFile = [self nextTest:args];
    00049
    00050     if(!testCaseFile || testCaseFile == "")
    00051     {
    00052         [self report];
    00053         return;
    00054     }
    00055
    00056     var matches = testCaseFile.match(/([^\/]+)\.j$/);
    00057
    00058     if (matches)
    00059     {
    00060
    00061         system.stderr.write(matches[1]).flush();
    00062         var testCaseClass = matches[1];
    00063
    00064         [self beforeRequire];
    00065         require(testCaseFile);
    00066
    00067         var suite = [self getTest:testCaseClass];
    00068         [self run:suite];
    00069         [self afterRun];
    00070         system.stderr.write("\n").flush();
    00071     }
    00072     else
    00073         system.stderr.write("Skipping " + testCaseFile + ": not an Objective-J source file.\n").flush();
    00074
    00075     // run the next test when this is done
    00076     [self startWithArguments:args];
    00077 }
    00078
    00079 - (void)beforeRequire
    00080 {
    00081     // do nothing. This is for subclassing purposes.
    00082 }
    00083
    00084 - (void)afterRun
    00085 {
    00086     // do nothing. This is for subclassing purposes.
    00087 }
    00088
    00089 - (CPString)nextTest:(CPArray)arguments
    00090 {
    00091     return require("file").absolute(arguments.shift());
    00092 }
    00093
    00094 - (OJTestResult)run:(OJTest)suite wait:(BOOL)wait
    00095 {
    00096     var result = [[OJTestResult alloc] init];
    00097
    00098     [result addListener:_listener];
    00099
    00100     [suite run:result];
    00101
    00102     return result;
    00103 }
    00104
    00105 - (OJTestResult)run:(OJTest)suite
    00106 {
    00107     return [self run:suite wait:NO];
    00108 }
    00109
    00110 + (void)runTest:(OJTest)suite
    00111 {
    00112     var runner = [[OJTestRunnerText alloc] init];
    00113     [runner run:suite];
    00114 }
    00115
    00116 + (void)runClass:(Class)aClass
    00117 {
    00118     [self runTest:[[OJTestSuite alloc] initWithClass:aClass]];
    00119 }
    00120
    00121 - (void)report
    00122 {
    00123     var totalErrors = [[_listener errors] count] + [[_listener failures] count];
    00124
    00125     if (totalErrors === 0) {
    00126         stream.print("\0green(All tests passed in the test suite.\0)");
    00127         OS.exit(0);
    00128     } else {
    00129         stream.print("Test suite failed with \0red(" + [[_listener errors] count] +
    00130             " errors\0) and \0red(" + [[_listener failures] count] + " failures\0).");
    00131         OS.exit(1);
    00132     }
    00133 }
    00134
    00135 @end

All Classes Files Functions Variables