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

    00001 @import <Foundation/CPObject.j>
    00002
    00003 @implementation OJAssert : CPObject
    00004
    00010 + (void)assertTrue:(BOOL)condition
    00011 {
    00012     [self assertTrue:condition message:"expected YES but got NO"];
    00013 }
    00014
    00021 + (void)assertTrue:(BOOL)condition message:(CPString)message
    00022 {
    00023     if (!condition)
    00024         [self fail:message];
    00025 }
    00026
    00032 + (void)assertFalse:(BOOL)condition
    00033 {
    00034     [self assertFalse:condition message:"expected NO but got YES"];
    00035 }
    00036
    00043 + (void)assertFalse:(BOOL)condition message:(CPString)message
    00044 {
    00045     [self assertTrue:(!condition) message:message];
    00046 }
    00047
    00054 + (void)assert:(id)expected equals:(id)actual
    00055 {
    00056     [self assert:expected equals:actual message:nil];
    00057 }
    00058
    00067 + (void)assert:(id)expected equals:(id)actual message:(CPString)message
    00068 {
    00069     if (expected !== actual && ![expected isEqual:actual])
    00070         [self failNotEqual:expected actual:actual message:message];
    00071 }
    00072
    00079 + (void)assert:(id)expected notEqual:(id)actual
    00080 {
    00081     [self assert:expected notEqual:actual message:nil];
    00082 }
    00083
    00092 + (void)assert:(id)expected notEqual:(id)actual message:(CPString)message
    00093 {
    00094     if (expected === actual || [expected isEqual:actual])
    00095         [self failEqual:expected actual:actual message:message];
    00096 }
    00097
    00104 + (void)assert:(id)expected same:(id)actual
    00105 {
    00106     [self assert:expected same:actual message:nil];
    00107 }
    00108
    00117 + (void)assert:(id)expected same:(id)actual message:(CPString)message
    00118 {
    00119     if (expected !== actual)
    00120         [self failSame:expected actual:actual message:message];
    00121 }
    00122
    00129 + (void)assert:(id)expected notSame:(id)actual
    00130 {
    00131     [self assert:expected notSame:actual message:nil];
    00132 }
    00133
    00142 + (void)assert:(id)expected notSame:(id)actual message:(CPString)message
    00143 {
    00144     if (expected === actual)
    00145         [self failNotSame:expected actual:actual message:message];
    00146 }
    00147
    00153 + (void)assertNull:(id)object
    00154 {
    00155     [self assertNull:object message:"expected null but got " + stringValueOf(object)];
    00156 }
    00157
    00163 + (void)assertNull:(id)object message:(CPString)message
    00164 {
    00165     [self assertTrue:(object === null) message:message];
    00166 }
    00167
    00173 + (void)assertNotNull:(id)object
    00174 {
    00175     [self assertNotNull:object message:"expected an object but got null"];
    00176 }
    00177
    00184 + (void)assertNotNull:(id)object message:(CPString)message
    00185 {
    00186     [self assertTrue:(object !== null) message:message];
    00187 }
    00188
    00198 + (void)assertNoThrow:(Function)zeroArgClosure
    00199 {
    00200     var exception = nil;
    00201     try { zeroArgClosure(); }
    00202     catch (e) { exception = e; }
    00203     [self assertNull:exception message:"Caught unexpected exception " + exception];
    00204 }
    00205
    00215 + (void)assertThrows:(Function)zeroArgClosure
    00216 {
    00217     var exception = nil;
    00218     try { zeroArgClosure(); }
    00219     catch (e) { exception = e; }
    00220     [self assertNotNull:exception message:"Should have caught an exception, but got nothing"];
    00221 }
    00222
    00231 + (void)assert:(CPString)aRegex matches:(CPString)aString
    00232 {
    00233     [self assertTrue:aString.match(RegExp(aRegex))
    00234         message:"string '" + aString + "' should be matched by regex /" + aRegex + "/"];
    00235 }
    00236
    00240 + (void)fail
    00241 {
    00242     [self fail:nil];
    00243 }
    00244
    00250 + (void)fail:(CPString)message
    00251 {
    00252     [CPException raise:AssertionFailedError reason:(message || "Unknown")];
    00253 }
    00254
    00255 + (void)failSame:(id)expected actual:(id)actual message:(CPString)message
    00256 {
    00257     [self fail:((message ? message+" " : "")+"expected not same")];
    00258 }
    00259
    00260 + (void)failNotSame:(id)expected actual:(id)actual message:(CPString)message
    00261 {
    00262     [self fail:((message ? message+" " : "")+"expected same:<"+stringValueOf(expected)+
    00263         "> was not:<"+stringValueOf(actual)+">")];
    00264 }
    00265
    00266 + (void)failEqual:(id)expected actual:(id)actual message:(CPString)message
    00267 {
    00268     [self fail:((message ? message+" " : "")+"expected inequality. Expected:<"+stringValueOf(expected)+
    00269         "> Got:<"+stringValueOf(actual)+">")];
    00270 }
    00271
    00272 + (void)failNotEqual:(id)expected actual:(id)actual message:(CPString)message
    00273 {
    00274     [self fail:((message ? message+" " : "")+"expected:<"+stringValueOf(expected)+
    00275         "> but was:<"+stringValueOf(actual)+">")];
    00276 }
    00277
    00278 @end
    00279
    00280 function stringValueOf(obj) {
    00281     if(obj && obj.isa)
    00282         var result = [obj description];
    00283     else
    00284         var result = obj;
    00285
    00286     return result;
    00287 }

All Classes Files Functions Variables