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

    00001 @import <Foundation/Foundation.j>
    00002
    00003 @implementation OJTestSuite : CPObject
    00004 {
    00005     CPArray         _tests;
    00006     CPString        _name;
    00007     CPArray         _testClassesRan;
    00008 }
    00009
    00010 - (id)init
    00011 {
    00012     if (self = [super init])
    00013     {
    00014         _tests = [];
    00015         _testClassesRan = [];
    00016     }
    00017     return self;
    00018 }
    00019
    00020 - (id)initWithName:(CPString)aName
    00021 {
    00022     if (self = [self init])
    00023     {
    00024         [self setName:aName];
    00025     }
    00026     return self;
    00027 }
    00028
    00029 - (id)initWithClass:(Class)aClass
    00030 {
    00031     if (self = [self init])
    00032     {
    00033         var superClass = aClass,
    00034             names = [];
    00035         while (superClass)
    00036         {
    00037             var methods = class_copyMethodList(superClass);
    00038             for (var i = 0; i < methods.length; i++)
    00039             {
    00040                 [self addTestMethod:methods[i].name names:names class:aClass]
    00041             }
    00042
    00043             var autotestObject;
    00044             if([aClass respondsToSelector:@selector(autotest)])
    00045                 autotestObject = objj_msgSend(aClass, "autotest");
    00046
    00047             if(autotestObject && ![_testClassesRan containsObject:aClass])
    00048             {
    00049                 [self createAccessorAutotests:class_copyIvarList([autotestObject class]) inClass:aClass forObject:autotestObject];
    00050                 [_testClassesRan addObject:aClass];
    00051             }
    00052
    00053             superClass = superClass.super_class;
    00054         }
    00055
    00056         if ([_tests count] == 0)
    00057             CPLog.warn("No tests");
    00058     }
    00059
    00060     return self;
    00061 }
    00062
    00063 - (void)createAccessorAutotests:(CPArray)ivars inClass:(Class)aClass forObject:(id)autotestObject
    00064 {
    00065     for (var i = 0; i < ivars.length; i++)
    00066     {
    00067         if(ivars[i].accessors)
    00068         {
    00069             var getAccessorName = ivars[i].accessors.get;
    00070             var ivarName = ivars[i].name;
    00071             var newMethodName = "testThat__"+ivars[i].accessors.get+"__WorksIn"+[autotestObject class];
    00072             class_addMethod(aClass, newMethodName, function(){
    00073                 var target = [aClass autotest];
    00074                 var expected = @"";
    00075                 target[ivarName] = expected;
    00076                 var actual = objj_msgSend(target, getAccessorName);
    00077                 [OJAssert assert:expected equals:actual];
    00078             });
    00079             [self addTestMethod:newMethodName names:[] class:aClass];
    00080
    00081             if(ivars[i].accessors.set)
    00082             {
    00083                 var setAccessorName = ivars[i].accessors.set;
    00084                 var newMethodName = "testThat__"+ivars[i].accessors.set+"__WorksIn"+[autotestObject class];
    00085                 class_addMethod(aClass, newMethodName, function(){
    00086                     var target = [aClass autotest];
    00087                     var expected = @"";
    00088                     objj_msgSend(target, setAccessorName, expected);
    00089                     var actual = objj_msgSend(target, getAccessorName);
    00090                     [OJAssert assert:expected equals:actual];
    00091                 });
    00092                 [self addTestMethod:newMethodName names:[] class:aClass];
    00093             }
    00094         }
    00095     }
    00096 }
    00097
    00098 - (id)initWithClass:(Class)aClass name:(CPString)aName
    00099 {
    00100     [self initWithClass:aClass];
    00101     [self setName:aName];
    00102 }
    00103
    00104 - (void)addTest:(OJTest)aTest
    00105 {
    00106     [_tests addObject:aTest];
    00107 }
    00108
    00109 - (void)addTestSuite:(Class)aTestClass
    00110 {
    00111     [self addTest:[[OJTestSuite alloc] initWithClass:aTestClass]];
    00112 }
    00113
    00114 - (void)addTestMethod:(SEL)selector names:(CPArray)names class:(Class)aClass
    00115 {
    00116     if ([names containsObject:selector] || ![self isTestMethod:selector])
    00117         return;
    00118
    00119     [names addObject:selector];
    00120     [self addTest:[self createTestWithSelector:selector class:aClass]];
    00121 }
    00122
    00123 - (OJTest)createTestWithSelector:(SEL)selector class:(Class)aClass
    00124 {
    00125     var initSelector = [self getTestConstructor:aClass];
    00126
    00127     var test;
    00128     if (selector.indexOf(":") < 0)
    00129     {
    00130         test = [[aClass alloc] performSelector:initSelector];
    00131         if ([test isKindOfClass:[OJTestCase class]])
    00132             [test setSelector:selector];
    00133     }
    00134     else
    00135     {
    00136         test = [aClass performSelector:initSelector withObject:selector];
    00137     }
    00138
    00139     return test;
    00140 }
    00141
    00142 - (void)isTestMethod:(SEL)selector
    00143 {
    00144     return selector.substring(0,4) == "test" && selector.indexOf(":") == -1;
    00145 }
    00146
    00147 - (SEL)getTestConstructor:(Class)aClass
    00148 {
    00149     return "init";
    00150 }
    00151
    00152 - (void)run:(OJTestResult)result
    00153 {
    00154     for (var i = 0; i < _tests.length; i++)
    00155     {
    00156         if ([result shouldStop])
    00157             break;
    00158
    00159         [_tests[i] run:result];
    00160     }
    00161 }
    00162
    00163 - (CPString)name
    00164 {
    00165     return _name;
    00166 }
    00167
    00168 - (void)setName:(CPString)aName
    00169 {
    00170     _name = aName;
    00171 }
    00172
    00173 - (int)countTestCases
    00174 {
    00175     var count = 0;
    00176     for (var i = 0; i < _tests.length; i++)
    00177         [_tests[i] countTestCases];
    00178     return count;
    00179 }
    00180
    00181 @end

All Classes Files Functions Variables