toolkit/crashreporter/google-breakpad/src/common/mac/testing/GTMSenTestCase.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 //
michael@0 2 // GTMSenTestCase.h
michael@0 3 //
michael@0 4 // Copyright 2007-2008 Google Inc.
michael@0 5 //
michael@0 6 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
michael@0 7 // use this file except in compliance with the License. You may obtain a copy
michael@0 8 // of the License at
michael@0 9 //
michael@0 10 // http://www.apache.org/licenses/LICENSE-2.0
michael@0 11 //
michael@0 12 // Unless required by applicable law or agreed to in writing, software
michael@0 13 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
michael@0 14 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
michael@0 15 // License for the specific language governing permissions and limitations under
michael@0 16 // the License.
michael@0 17 //
michael@0 18
michael@0 19 // Portions of this file fall under the following license, marked with
michael@0 20 // SENTE_BEGIN - SENTE_END
michael@0 21 //
michael@0 22 // Copyright (c) 1997-2005, Sen:te (Sente SA). All rights reserved.
michael@0 23 //
michael@0 24 // Use of this source code is governed by the following license:
michael@0 25 //
michael@0 26 // Redistribution and use in source and binary forms, with or without modification,
michael@0 27 // are permitted provided that the following conditions are met:
michael@0 28 //
michael@0 29 // (1) Redistributions of source code must retain the above copyright notice,
michael@0 30 // this list of conditions and the following disclaimer.
michael@0 31 //
michael@0 32 // (2) Redistributions in binary form must reproduce the above copyright notice,
michael@0 33 // this list of conditions and the following disclaimer in the documentation
michael@0 34 // and/or other materials provided with the distribution.
michael@0 35 //
michael@0 36 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
michael@0 37 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
michael@0 38 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
michael@0 39 // IN NO EVENT SHALL Sente SA OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 40 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
michael@0 41 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
michael@0 42 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
michael@0 43 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
michael@0 44 // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 45 //
michael@0 46 // Note: this license is equivalent to the FreeBSD license.
michael@0 47 //
michael@0 48 // This notice may not be removed from this file.
michael@0 49
michael@0 50 // Some extra test case macros that would have been convenient for SenTestingKit
michael@0 51 // to provide. I didn't stick GTM in front of the Macro names, so that they would
michael@0 52 // be easy to remember.
michael@0 53
michael@0 54 #import "GTMDefines.h"
michael@0 55
michael@0 56 #if (!GTM_IPHONE_SDK) || (GTM_IPHONE_USE_SENTEST)
michael@0 57 #import <SenTestingKit/SenTestingKit.h>
michael@0 58 #else
michael@0 59 #import <Foundation/Foundation.h>
michael@0 60 #ifdef __cplusplus
michael@0 61 extern "C" {
michael@0 62 #endif
michael@0 63
michael@0 64 #if defined __clang__
michael@0 65 // gcc and gcc-llvm do not allow you to use STAssert(blah, nil) with nil
michael@0 66 // as a description if you have the NS_FORMAT_FUNCTION on.
michael@0 67 // clang however will not compile without warnings if you don't have it.
michael@0 68 NSString *STComposeString(NSString *, ...) NS_FORMAT_FUNCTION(1, 2);
michael@0 69 #else
michael@0 70 NSString *STComposeString(NSString *, ...);
michael@0 71 #endif // __clang__
michael@0 72
michael@0 73 #ifdef __cplusplus
michael@0 74 }
michael@0 75 #endif
michael@0 76
michael@0 77 #endif // !GTM_IPHONE_SDK || GTM_IPHONE_USE_SENTEST
michael@0 78
michael@0 79 // Generates a failure when a1 != noErr
michael@0 80 // Args:
michael@0 81 // a1: should be either an OSErr or an OSStatus
michael@0 82 // description: A format string as in the printf() function. Can be nil or
michael@0 83 // an empty string but must be present.
michael@0 84 // ...: A variable number of arguments to the format string. Can be absent.
michael@0 85 #define STAssertNoErr(a1, description, ...) \
michael@0 86 do { \
michael@0 87 @try { \
michael@0 88 OSStatus a1value = (a1); \
michael@0 89 if (a1value != noErr) { \
michael@0 90 NSString *_expression = [NSString stringWithFormat:@"Expected noErr, got %ld for (%s)", (long)a1value, #a1]; \
michael@0 91 [self failWithException:([NSException failureInCondition:_expression \
michael@0 92 isTrue:NO \
michael@0 93 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 94 atLine:__LINE__ \
michael@0 95 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)])]; \
michael@0 96 } \
michael@0 97 } \
michael@0 98 @catch (id anException) { \
michael@0 99 [self failWithException:[NSException failureInRaise:[NSString stringWithFormat:@"(%s) == noErr fails", #a1] \
michael@0 100 exception:anException \
michael@0 101 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 102 atLine:__LINE__ \
michael@0 103 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 104 } \
michael@0 105 } while(0)
michael@0 106
michael@0 107 // Generates a failure when a1 != a2
michael@0 108 // Args:
michael@0 109 // a1: received value. Should be either an OSErr or an OSStatus
michael@0 110 // a2: expected value. Should be either an OSErr or an OSStatus
michael@0 111 // description: A format string as in the printf() function. Can be nil or
michael@0 112 // an empty string but must be present.
michael@0 113 // ...: A variable number of arguments to the format string. Can be absent.
michael@0 114 #define STAssertErr(a1, a2, description, ...) \
michael@0 115 do { \
michael@0 116 @try { \
michael@0 117 OSStatus a1value = (a1); \
michael@0 118 OSStatus a2value = (a2); \
michael@0 119 if (a1value != a2value) { \
michael@0 120 NSString *_expression = [NSString stringWithFormat:@"Expected %s(%ld) but got %ld for (%s)", #a2, (long)a2value, (long)a1value, #a1]; \
michael@0 121 [self failWithException:([NSException failureInCondition:_expression \
michael@0 122 isTrue:NO \
michael@0 123 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 124 atLine:__LINE__ \
michael@0 125 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)])]; \
michael@0 126 } \
michael@0 127 } \
michael@0 128 @catch (id anException) { \
michael@0 129 [self failWithException:[NSException failureInRaise:[NSString stringWithFormat:@"(%s) == (%s) fails", #a1, #a2] \
michael@0 130 exception:anException \
michael@0 131 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 132 atLine:__LINE__ \
michael@0 133 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 134 } \
michael@0 135 } while(0)
michael@0 136
michael@0 137
michael@0 138 // Generates a failure when a1 is NULL
michael@0 139 // Args:
michael@0 140 // a1: should be a pointer (use STAssertNotNil for an object)
michael@0 141 // description: A format string as in the printf() function. Can be nil or
michael@0 142 // an empty string but must be present.
michael@0 143 // ...: A variable number of arguments to the format string. Can be absent.
michael@0 144 #define STAssertNotNULL(a1, description, ...) \
michael@0 145 do { \
michael@0 146 @try { \
michael@0 147 __typeof__(a1) a1value = (a1); \
michael@0 148 if (a1value == (__typeof__(a1))NULL) { \
michael@0 149 NSString *_expression = [NSString stringWithFormat:@"((%s) != NULL)", #a1]; \
michael@0 150 [self failWithException:([NSException failureInCondition:_expression \
michael@0 151 isTrue:NO \
michael@0 152 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 153 atLine:__LINE__ \
michael@0 154 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)])]; \
michael@0 155 } \
michael@0 156 } \
michael@0 157 @catch (id anException) { \
michael@0 158 [self failWithException:[NSException failureInRaise:[NSString stringWithFormat:@"(%s) != NULL fails", #a1] \
michael@0 159 exception:anException \
michael@0 160 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 161 atLine:__LINE__ \
michael@0 162 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 163 } \
michael@0 164 } while(0)
michael@0 165
michael@0 166 // Generates a failure when a1 is not NULL
michael@0 167 // Args:
michael@0 168 // a1: should be a pointer (use STAssertNil for an object)
michael@0 169 // description: A format string as in the printf() function. Can be nil or
michael@0 170 // an empty string but must be present.
michael@0 171 // ...: A variable number of arguments to the format string. Can be absent.
michael@0 172 #define STAssertNULL(a1, description, ...) \
michael@0 173 do { \
michael@0 174 @try { \
michael@0 175 __typeof__(a1) a1value = (a1); \
michael@0 176 if (a1value != (__typeof__(a1))NULL) { \
michael@0 177 NSString *_expression = [NSString stringWithFormat:@"((%s) == NULL)", #a1]; \
michael@0 178 [self failWithException:([NSException failureInCondition:_expression \
michael@0 179 isTrue:NO \
michael@0 180 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 181 atLine:__LINE__ \
michael@0 182 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)])]; \
michael@0 183 } \
michael@0 184 } \
michael@0 185 @catch (id anException) { \
michael@0 186 [self failWithException:[NSException failureInRaise:[NSString stringWithFormat:@"(%s) == NULL fails", #a1] \
michael@0 187 exception:anException \
michael@0 188 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 189 atLine:__LINE__ \
michael@0 190 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 191 } \
michael@0 192 } while(0)
michael@0 193
michael@0 194 // Generates a failure when a1 is equal to a2. This test is for C scalars,
michael@0 195 // structs and unions.
michael@0 196 // Args:
michael@0 197 // a1: argument 1
michael@0 198 // a2: argument 2
michael@0 199 // description: A format string as in the printf() function. Can be nil or
michael@0 200 // an empty string but must be present.
michael@0 201 // ...: A variable number of arguments to the format string. Can be absent.
michael@0 202 #define STAssertNotEquals(a1, a2, description, ...) \
michael@0 203 do { \
michael@0 204 @try { \
michael@0 205 if (strcmp(@encode(__typeof__(a1)), @encode(__typeof__(a2)))) { \
michael@0 206 [self failWithException:[NSException failureInFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 207 atLine:__LINE__ \
michael@0 208 withDescription:@"Type mismatch -- %@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 209 } else { \
michael@0 210 __typeof__(a1) a1value = (a1); \
michael@0 211 __typeof__(a2) a2value = (a2); \
michael@0 212 NSValue *a1encoded = [NSValue value:&a1value withObjCType:@encode(__typeof__(a1))]; \
michael@0 213 NSValue *a2encoded = [NSValue value:&a2value withObjCType:@encode(__typeof__(a2))]; \
michael@0 214 if ([a1encoded isEqualToValue:a2encoded]) { \
michael@0 215 NSString *_expression = [NSString stringWithFormat:@"((%s) != (%s))", #a1, #a2]; \
michael@0 216 [self failWithException:([NSException failureInCondition:_expression \
michael@0 217 isTrue:NO \
michael@0 218 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 219 atLine:__LINE__ \
michael@0 220 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)])]; \
michael@0 221 }\
michael@0 222 } \
michael@0 223 } \
michael@0 224 @catch (id anException) { \
michael@0 225 [self failWithException:[NSException failureInRaise:[NSString stringWithFormat:@"(%s) != (%s)", #a1, #a2] \
michael@0 226 exception:anException \
michael@0 227 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 228 atLine:__LINE__ \
michael@0 229 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 230 } \
michael@0 231 } while(0)
michael@0 232
michael@0 233 // Generates a failure when a1 is equal to a2. This test is for objects.
michael@0 234 // Args:
michael@0 235 // a1: argument 1. object.
michael@0 236 // a2: argument 2. object.
michael@0 237 // description: A format string as in the printf() function. Can be nil or
michael@0 238 // an empty string but must be present.
michael@0 239 // ...: A variable number of arguments to the format string. Can be absent.
michael@0 240 #define STAssertNotEqualObjects(a1, a2, description, ...) \
michael@0 241 do { \
michael@0 242 @try {\
michael@0 243 id a1value = (a1); \
michael@0 244 id a2value = (a2); \
michael@0 245 if ( (strcmp(@encode(__typeof__(a1value)), @encode(id)) == 0) && \
michael@0 246 (strcmp(@encode(__typeof__(a2value)), @encode(id)) == 0) && \
michael@0 247 (![(id)a1value isEqual:(id)a2value]) ) continue; \
michael@0 248 [self failWithException:([NSException failureInEqualityBetweenObject:a1value \
michael@0 249 andObject:a2value \
michael@0 250 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 251 atLine:__LINE__ \
michael@0 252 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)])]; \
michael@0 253 }\
michael@0 254 @catch (id anException) {\
michael@0 255 [self failWithException:([NSException failureInRaise:[NSString stringWithFormat:@"(%s) != (%s)", #a1, #a2] \
michael@0 256 exception:anException \
michael@0 257 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 258 atLine:__LINE__ \
michael@0 259 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)])]; \
michael@0 260 }\
michael@0 261 } while(0)
michael@0 262
michael@0 263 // Generates a failure when a1 is not 'op' to a2. This test is for C scalars.
michael@0 264 // Args:
michael@0 265 // a1: argument 1
michael@0 266 // a2: argument 2
michael@0 267 // op: operation
michael@0 268 // description: A format string as in the printf() function. Can be nil or
michael@0 269 // an empty string but must be present.
michael@0 270 // ...: A variable number of arguments to the format string. Can be absent.
michael@0 271 #define STAssertOperation(a1, a2, op, description, ...) \
michael@0 272 do { \
michael@0 273 @try { \
michael@0 274 if (strcmp(@encode(__typeof__(a1)), @encode(__typeof__(a2)))) { \
michael@0 275 [self failWithException:[NSException failureInFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 276 atLine:__LINE__ \
michael@0 277 withDescription:@"Type mismatch -- %@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 278 } else { \
michael@0 279 __typeof__(a1) a1value = (a1); \
michael@0 280 __typeof__(a2) a2value = (a2); \
michael@0 281 if (!(a1value op a2value)) { \
michael@0 282 double a1DoubleValue = a1value; \
michael@0 283 double a2DoubleValue = a2value; \
michael@0 284 NSString *_expression = [NSString stringWithFormat:@"(%s (%lg) %s %s (%lg))", #a1, a1DoubleValue, #op, #a2, a2DoubleValue]; \
michael@0 285 [self failWithException:([NSException failureInCondition:_expression \
michael@0 286 isTrue:NO \
michael@0 287 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 288 atLine:__LINE__ \
michael@0 289 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)])]; \
michael@0 290 } \
michael@0 291 } \
michael@0 292 } \
michael@0 293 @catch (id anException) { \
michael@0 294 [self failWithException:[NSException \
michael@0 295 failureInRaise:[NSString stringWithFormat:@"(%s) %s (%s)", #a1, #op, #a2] \
michael@0 296 exception:anException \
michael@0 297 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 298 atLine:__LINE__ \
michael@0 299 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 300 } \
michael@0 301 } while(0)
michael@0 302
michael@0 303 // Generates a failure when a1 is not > a2. This test is for C scalars.
michael@0 304 // Args:
michael@0 305 // a1: argument 1
michael@0 306 // a2: argument 2
michael@0 307 // op: operation
michael@0 308 // description: A format string as in the printf() function. Can be nil or
michael@0 309 // an empty string but must be present.
michael@0 310 // ...: A variable number of arguments to the format string. Can be absent.
michael@0 311 #define STAssertGreaterThan(a1, a2, description, ...) \
michael@0 312 STAssertOperation(a1, a2, >, description, ##__VA_ARGS__)
michael@0 313
michael@0 314 // Generates a failure when a1 is not >= a2. This test is for C scalars.
michael@0 315 // Args:
michael@0 316 // a1: argument 1
michael@0 317 // a2: argument 2
michael@0 318 // op: operation
michael@0 319 // description: A format string as in the printf() function. Can be nil or
michael@0 320 // an empty string but must be present.
michael@0 321 // ...: A variable number of arguments to the format string. Can be absent.
michael@0 322 #define STAssertGreaterThanOrEqual(a1, a2, description, ...) \
michael@0 323 STAssertOperation(a1, a2, >=, description, ##__VA_ARGS__)
michael@0 324
michael@0 325 // Generates a failure when a1 is not < a2. This test is for C scalars.
michael@0 326 // Args:
michael@0 327 // a1: argument 1
michael@0 328 // a2: argument 2
michael@0 329 // op: operation
michael@0 330 // description: A format string as in the printf() function. Can be nil or
michael@0 331 // an empty string but must be present.
michael@0 332 // ...: A variable number of arguments to the format string. Can be absent.
michael@0 333 #define STAssertLessThan(a1, a2, description, ...) \
michael@0 334 STAssertOperation(a1, a2, <, description, ##__VA_ARGS__)
michael@0 335
michael@0 336 // Generates a failure when a1 is not <= a2. This test is for C scalars.
michael@0 337 // Args:
michael@0 338 // a1: argument 1
michael@0 339 // a2: argument 2
michael@0 340 // op: operation
michael@0 341 // description: A format string as in the printf() function. Can be nil or
michael@0 342 // an empty string but must be present.
michael@0 343 // ...: A variable number of arguments to the format string. Can be absent.
michael@0 344 #define STAssertLessThanOrEqual(a1, a2, description, ...) \
michael@0 345 STAssertOperation(a1, a2, <=, description, ##__VA_ARGS__)
michael@0 346
michael@0 347 // Generates a failure when string a1 is not equal to string a2. This call
michael@0 348 // differs from STAssertEqualObjects in that strings that are different in
michael@0 349 // composition (precomposed vs decomposed) will compare equal if their final
michael@0 350 // representation is equal.
michael@0 351 // ex O + umlaut decomposed is the same as O + umlaut composed.
michael@0 352 // Args:
michael@0 353 // a1: string 1
michael@0 354 // a2: string 2
michael@0 355 // description: A format string as in the printf() function. Can be nil or
michael@0 356 // an empty string but must be present.
michael@0 357 // ...: A variable number of arguments to the format string. Can be absent.
michael@0 358 #define STAssertEqualStrings(a1, a2, description, ...) \
michael@0 359 do { \
michael@0 360 @try { \
michael@0 361 id a1value = (a1); \
michael@0 362 id a2value = (a2); \
michael@0 363 if (a1value == a2value) continue; \
michael@0 364 if ([a1value isKindOfClass:[NSString class]] && \
michael@0 365 [a2value isKindOfClass:[NSString class]] && \
michael@0 366 [a1value compare:a2value options:0] == NSOrderedSame) continue; \
michael@0 367 [self failWithException:[NSException failureInEqualityBetweenObject:a1value \
michael@0 368 andObject:a2value \
michael@0 369 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 370 atLine:__LINE__ \
michael@0 371 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 372 } \
michael@0 373 @catch (id anException) { \
michael@0 374 [self failWithException:[NSException failureInRaise:[NSString stringWithFormat:@"(%s) == (%s)", #a1, #a2] \
michael@0 375 exception:anException \
michael@0 376 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 377 atLine:__LINE__ \
michael@0 378 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 379 } \
michael@0 380 } while(0)
michael@0 381
michael@0 382 // Generates a failure when string a1 is equal to string a2. This call
michael@0 383 // differs from STAssertEqualObjects in that strings that are different in
michael@0 384 // composition (precomposed vs decomposed) will compare equal if their final
michael@0 385 // representation is equal.
michael@0 386 // ex O + umlaut decomposed is the same as O + umlaut composed.
michael@0 387 // Args:
michael@0 388 // a1: string 1
michael@0 389 // a2: string 2
michael@0 390 // description: A format string as in the printf() function. Can be nil or
michael@0 391 // an empty string but must be present.
michael@0 392 // ...: A variable number of arguments to the format string. Can be absent.
michael@0 393 #define STAssertNotEqualStrings(a1, a2, description, ...) \
michael@0 394 do { \
michael@0 395 @try { \
michael@0 396 id a1value = (a1); \
michael@0 397 id a2value = (a2); \
michael@0 398 if ([a1value isKindOfClass:[NSString class]] && \
michael@0 399 [a2value isKindOfClass:[NSString class]] && \
michael@0 400 [a1value compare:a2value options:0] != NSOrderedSame) continue; \
michael@0 401 [self failWithException:[NSException failureInEqualityBetweenObject:a1value \
michael@0 402 andObject:a2value \
michael@0 403 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 404 atLine:__LINE__ \
michael@0 405 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 406 } \
michael@0 407 @catch (id anException) { \
michael@0 408 [self failWithException:[NSException failureInRaise:[NSString stringWithFormat:@"(%s) != (%s)", #a1, #a2] \
michael@0 409 exception:anException \
michael@0 410 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 411 atLine:__LINE__ \
michael@0 412 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 413 } \
michael@0 414 } while(0)
michael@0 415
michael@0 416 // Generates a failure when c-string a1 is not equal to c-string a2.
michael@0 417 // Args:
michael@0 418 // a1: string 1
michael@0 419 // a2: string 2
michael@0 420 // description: A format string as in the printf() function. Can be nil or
michael@0 421 // an empty string but must be present.
michael@0 422 // ...: A variable number of arguments to the format string. Can be absent.
michael@0 423 #define STAssertEqualCStrings(a1, a2, description, ...) \
michael@0 424 do { \
michael@0 425 @try { \
michael@0 426 const char* a1value = (a1); \
michael@0 427 const char* a2value = (a2); \
michael@0 428 if (a1value == a2value) continue; \
michael@0 429 if (strcmp(a1value, a2value) == 0) continue; \
michael@0 430 [self failWithException:[NSException failureInEqualityBetweenObject:[NSString stringWithUTF8String:a1value] \
michael@0 431 andObject:[NSString stringWithUTF8String:a2value] \
michael@0 432 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 433 atLine:__LINE__ \
michael@0 434 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 435 } \
michael@0 436 @catch (id anException) { \
michael@0 437 [self failWithException:[NSException failureInRaise:[NSString stringWithFormat:@"(%s) == (%s)", #a1, #a2] \
michael@0 438 exception:anException \
michael@0 439 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 440 atLine:__LINE__ \
michael@0 441 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 442 } \
michael@0 443 } while(0)
michael@0 444
michael@0 445 // Generates a failure when c-string a1 is equal to c-string a2.
michael@0 446 // Args:
michael@0 447 // a1: string 1
michael@0 448 // a2: string 2
michael@0 449 // description: A format string as in the printf() function. Can be nil or
michael@0 450 // an empty string but must be present.
michael@0 451 // ...: A variable number of arguments to the format string. Can be absent.
michael@0 452 #define STAssertNotEqualCStrings(a1, a2, description, ...) \
michael@0 453 do { \
michael@0 454 @try { \
michael@0 455 const char* a1value = (a1); \
michael@0 456 const char* a2value = (a2); \
michael@0 457 if (strcmp(a1value, a2value) != 0) continue; \
michael@0 458 [self failWithException:[NSException failureInEqualityBetweenObject:[NSString stringWithUTF8String:a1value] \
michael@0 459 andObject:[NSString stringWithUTF8String:a2value] \
michael@0 460 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 461 atLine:__LINE__ \
michael@0 462 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 463 } \
michael@0 464 @catch (id anException) { \
michael@0 465 [self failWithException:[NSException failureInRaise:[NSString stringWithFormat:@"(%s) != (%s)", #a1, #a2] \
michael@0 466 exception:anException \
michael@0 467 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 468 atLine:__LINE__ \
michael@0 469 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 470 } \
michael@0 471 } while(0)
michael@0 472
michael@0 473 /*" Generates a failure when a1 is not equal to a2 within + or - accuracy is false.
michael@0 474 This test is for GLKit types (GLKVector, GLKMatrix) where small differences
michael@0 475 could make these items not exactly equal. Do not use this version directly.
michael@0 476 Use the explicit STAssertEqualGLKVectors and STAssertEqualGLKMatrices defined
michael@0 477 below.
michael@0 478 _{a1 The GLKType on the left.}
michael@0 479 _{a2 The GLKType on the right.}
michael@0 480 _{accuracy The maximum difference between a1 and a2 for these values to be
michael@0 481 considered equal.}
michael@0 482 _{description A format string as in the printf() function. Can be nil or
michael@0 483 an empty string but must be present.}
michael@0 484 _{... A variable number of arguments to the format string. Can be absent.}
michael@0 485 "*/
michael@0 486
michael@0 487 #define STInternalAssertEqualGLKVectorsOrMatrices(a1, a2, accuracy, description, ...) \
michael@0 488 do { \
michael@0 489 @try { \
michael@0 490 if (strcmp(@encode(__typeof__(a1)), @encode(__typeof__(a2)))) { \
michael@0 491 [self failWithException:[NSException failureInFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 492 atLine:__LINE__ \
michael@0 493 withDescription:@"Type mismatch -- %@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 494 } else { \
michael@0 495 __typeof__(a1) a1GLKValue = (a1); \
michael@0 496 __typeof__(a2) a2GLKValue = (a2); \
michael@0 497 __typeof__(accuracy) accuracyvalue = (accuracy); \
michael@0 498 float *a1FloatValue = ((float*)&a1GLKValue); \
michael@0 499 float *a2FloatValue = ((float*)&a2GLKValue); \
michael@0 500 for (size_t i = 0; i < sizeof(__typeof__(a1)) / sizeof(float); ++i) { \
michael@0 501 float a1value = a1FloatValue[i]; \
michael@0 502 float a2value = a2FloatValue[i]; \
michael@0 503 if (STAbsoluteDifference(a1value, a2value) > accuracyvalue) { \
michael@0 504 NSMutableArray *strings = [NSMutableArray arrayWithCapacity:sizeof(a1) / sizeof(float)]; \
michael@0 505 NSString *string; \
michael@0 506 for (size_t j = 0; j < sizeof(__typeof__(a1)) / sizeof(float); ++j) { \
michael@0 507 string = [NSString stringWithFormat:@"(%0.3f == %0.3f)", a1FloatValue[j], a2FloatValue[j]]; \
michael@0 508 [strings addObject:string]; \
michael@0 509 } \
michael@0 510 string = [strings componentsJoinedByString:@", "]; \
michael@0 511 NSString *desc = STComposeString(description, ##__VA_ARGS__); \
michael@0 512 desc = [NSString stringWithFormat:@"%@ With Accuracy %0.3f: %@", string, (float)accuracyvalue, desc]; \
michael@0 513 [self failWithException:[NSException failureInFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 514 atLine:__LINE__ \
michael@0 515 withDescription:@"%@", desc]]; \
michael@0 516 break; \
michael@0 517 } \
michael@0 518 } \
michael@0 519 } \
michael@0 520 } \
michael@0 521 @catch (id anException) { \
michael@0 522 [self failWithException:[NSException failureInRaise:[NSString stringWithFormat:@"(%s) == (%s)", #a1, #a2] \
michael@0 523 exception:anException \
michael@0 524 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 525 atLine:__LINE__ \
michael@0 526 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 527 } \
michael@0 528 } while(0)
michael@0 529
michael@0 530 #define STAssertEqualGLKVectors(a1, a2, accuracy, description, ...) \
michael@0 531 STInternalAssertEqualGLKVectorsOrMatrices(a1, a2, accuracy, description, ##__VA_ARGS__)
michael@0 532
michael@0 533 #define STAssertEqualGLKMatrices(a1, a2, accuracy, description, ...) \
michael@0 534 STInternalAssertEqualGLKVectorsOrMatrices(a1, a2, accuracy, description, ##__VA_ARGS__)
michael@0 535
michael@0 536 #define STAssertEqualGLKQuaternions(a1, a2, accuracy, description, ...) \
michael@0 537 STInternalAssertEqualGLKVectorsOrMatrices(a1, a2, accuracy, description, ##__VA_ARGS__)
michael@0 538
michael@0 539 #if GTM_IPHONE_SDK && !GTM_IPHONE_USE_SENTEST
michael@0 540 // When not using the Xcode provided version, define everything ourselves.
michael@0 541
michael@0 542 // SENTE_BEGIN
michael@0 543 /*" Generates a failure when !{ [a1 isEqualTo:a2] } is false
michael@0 544 (or one is nil and the other is not).
michael@0 545 _{a1 The object on the left.}
michael@0 546 _{a2 The object on the right.}
michael@0 547 _{description A format string as in the printf() function. Can be nil or
michael@0 548 an empty string but must be present.}
michael@0 549 _{... A variable number of arguments to the format string. Can be absent.}
michael@0 550 "*/
michael@0 551 #define STAssertEqualObjects(a1, a2, description, ...) \
michael@0 552 do { \
michael@0 553 @try { \
michael@0 554 id a1value = (a1); \
michael@0 555 id a2value = (a2); \
michael@0 556 if (a1value == a2value) continue; \
michael@0 557 if ((strcmp(@encode(__typeof__(a1value)), @encode(id)) == 0) && \
michael@0 558 (strcmp(@encode(__typeof__(a2value)), @encode(id)) == 0) && \
michael@0 559 [(id)a1value isEqual:(id)a2value]) continue; \
michael@0 560 [self failWithException:[NSException failureInEqualityBetweenObject:a1value \
michael@0 561 andObject:a2value \
michael@0 562 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 563 atLine:__LINE__ \
michael@0 564 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 565 } \
michael@0 566 @catch (id anException) { \
michael@0 567 [self failWithException:[NSException failureInRaise:[NSString stringWithFormat:@"(%s) == (%s)", #a1, #a2] \
michael@0 568 exception:anException \
michael@0 569 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 570 atLine:__LINE__ \
michael@0 571 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 572 } \
michael@0 573 } while(0)
michael@0 574
michael@0 575
michael@0 576 /*" Generates a failure when a1 is not equal to a2. This test is for
michael@0 577 C scalars, structs and unions.
michael@0 578 _{a1 The argument on the left.}
michael@0 579 _{a2 The argument on the right.}
michael@0 580 _{description A format string as in the printf() function. Can be nil or
michael@0 581 an empty string but must be present.}
michael@0 582 _{... A variable number of arguments to the format string. Can be absent.}
michael@0 583 "*/
michael@0 584 #define STAssertEquals(a1, a2, description, ...) \
michael@0 585 do { \
michael@0 586 @try { \
michael@0 587 if (strcmp(@encode(__typeof__(a1)), @encode(__typeof__(a2)))) { \
michael@0 588 [self failWithException:[NSException failureInFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 589 atLine:__LINE__ \
michael@0 590 withDescription:@"Type mismatch -- %@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 591 } else { \
michael@0 592 __typeof__(a1) a1value = (a1); \
michael@0 593 __typeof__(a2) a2value = (a2); \
michael@0 594 NSValue *a1encoded = [NSValue value:&a1value withObjCType:@encode(__typeof__(a1))]; \
michael@0 595 NSValue *a2encoded = [NSValue value:&a2value withObjCType:@encode(__typeof__(a2))]; \
michael@0 596 if (![a1encoded isEqualToValue:a2encoded]) { \
michael@0 597 [self failWithException:[NSException failureInEqualityBetweenValue:a1encoded \
michael@0 598 andValue:a2encoded \
michael@0 599 withAccuracy:nil \
michael@0 600 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 601 atLine:__LINE__ \
michael@0 602 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 603 } \
michael@0 604 } \
michael@0 605 } \
michael@0 606 @catch (id anException) { \
michael@0 607 [self failWithException:[NSException failureInRaise:[NSString stringWithFormat:@"(%s) == (%s)", #a1, #a2] \
michael@0 608 exception:anException \
michael@0 609 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 610 atLine:__LINE__ \
michael@0 611 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 612 } \
michael@0 613 } while(0)
michael@0 614
michael@0 615 #define STAbsoluteDifference(left,right) (MAX(left,right)-MIN(left,right))
michael@0 616
michael@0 617
michael@0 618 /*" Generates a failure when a1 is not equal to a2 within + or - accuracy is false.
michael@0 619 This test is for scalars such as floats and doubles where small differences
michael@0 620 could make these items not exactly equal, but also works for all scalars.
michael@0 621 _{a1 The scalar on the left.}
michael@0 622 _{a2 The scalar on the right.}
michael@0 623 _{accuracy The maximum difference between a1 and a2 for these values to be
michael@0 624 considered equal.}
michael@0 625 _{description A format string as in the printf() function. Can be nil or
michael@0 626 an empty string but must be present.}
michael@0 627 _{... A variable number of arguments to the format string. Can be absent.}
michael@0 628 "*/
michael@0 629
michael@0 630 #define STAssertEqualsWithAccuracy(a1, a2, accuracy, description, ...) \
michael@0 631 do { \
michael@0 632 @try { \
michael@0 633 if (strcmp(@encode(__typeof__(a1)), @encode(__typeof__(a2)))) { \
michael@0 634 [self failWithException:[NSException failureInFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 635 atLine:__LINE__ \
michael@0 636 withDescription:@"Type mismatch -- %@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 637 } else { \
michael@0 638 __typeof__(a1) a1value = (a1); \
michael@0 639 __typeof__(a2) a2value = (a2); \
michael@0 640 __typeof__(accuracy) accuracyvalue = (accuracy); \
michael@0 641 if (STAbsoluteDifference(a1value, a2value) > accuracyvalue) { \
michael@0 642 NSValue *a1encoded = [NSValue value:&a1value withObjCType:@encode(__typeof__(a1))]; \
michael@0 643 NSValue *a2encoded = [NSValue value:&a2value withObjCType:@encode(__typeof__(a2))]; \
michael@0 644 NSValue *accuracyencoded = [NSValue value:&accuracyvalue withObjCType:@encode(__typeof__(accuracy))]; \
michael@0 645 [self failWithException:[NSException failureInEqualityBetweenValue:a1encoded \
michael@0 646 andValue:a2encoded \
michael@0 647 withAccuracy:accuracyencoded \
michael@0 648 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 649 atLine:__LINE__ \
michael@0 650 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 651 } \
michael@0 652 } \
michael@0 653 } \
michael@0 654 @catch (id anException) { \
michael@0 655 [self failWithException:[NSException failureInRaise:[NSString stringWithFormat:@"(%s) == (%s)", #a1, #a2] \
michael@0 656 exception:anException \
michael@0 657 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 658 atLine:__LINE__ \
michael@0 659 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 660 } \
michael@0 661 } while(0)
michael@0 662
michael@0 663
michael@0 664
michael@0 665 /*" Generates a failure unconditionally.
michael@0 666 _{description A format string as in the printf() function. Can be nil or
michael@0 667 an empty string but must be present.}
michael@0 668 _{... A variable number of arguments to the format string. Can be absent.}
michael@0 669 "*/
michael@0 670 #define STFail(description, ...) \
michael@0 671 [self failWithException:[NSException failureInFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 672 atLine:__LINE__ \
michael@0 673 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]
michael@0 674
michael@0 675
michael@0 676
michael@0 677 /*" Generates a failure when a1 is not nil.
michael@0 678 _{a1 An object.}
michael@0 679 _{description A format string as in the printf() function. Can be nil or
michael@0 680 an empty string but must be present.}
michael@0 681 _{... A variable number of arguments to the format string. Can be absent.}
michael@0 682 "*/
michael@0 683 #define STAssertNil(a1, description, ...) \
michael@0 684 do { \
michael@0 685 @try { \
michael@0 686 id a1value = (a1); \
michael@0 687 if (a1value != nil) { \
michael@0 688 NSString *_a1 = [NSString stringWithUTF8String:#a1]; \
michael@0 689 NSString *_expression = [NSString stringWithFormat:@"((%@) == nil)", _a1]; \
michael@0 690 [self failWithException:[NSException failureInCondition:_expression \
michael@0 691 isTrue:NO \
michael@0 692 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 693 atLine:__LINE__ \
michael@0 694 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 695 } \
michael@0 696 } \
michael@0 697 @catch (id anException) { \
michael@0 698 [self failWithException:[NSException failureInRaise:[NSString stringWithFormat:@"(%s) == nil fails", #a1] \
michael@0 699 exception:anException \
michael@0 700 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 701 atLine:__LINE__ \
michael@0 702 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 703 } \
michael@0 704 } while(0)
michael@0 705
michael@0 706
michael@0 707 /*" Generates a failure when a1 is nil.
michael@0 708 _{a1 An object.}
michael@0 709 _{description A format string as in the printf() function. Can be nil or
michael@0 710 an empty string but must be present.}
michael@0 711 _{... A variable number of arguments to the format string. Can be absent.}
michael@0 712 "*/
michael@0 713 #define STAssertNotNil(a1, description, ...) \
michael@0 714 do { \
michael@0 715 @try { \
michael@0 716 id a1value = (a1); \
michael@0 717 if (a1value == nil) { \
michael@0 718 NSString *_a1 = [NSString stringWithUTF8String:#a1]; \
michael@0 719 NSString *_expression = [NSString stringWithFormat:@"((%@) != nil)", _a1]; \
michael@0 720 [self failWithException:[NSException failureInCondition:_expression \
michael@0 721 isTrue:NO \
michael@0 722 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 723 atLine:__LINE__ \
michael@0 724 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 725 } \
michael@0 726 } \
michael@0 727 @catch (id anException) { \
michael@0 728 [self failWithException:[NSException failureInRaise:[NSString stringWithFormat:@"(%s) != nil fails", #a1] \
michael@0 729 exception:anException \
michael@0 730 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 731 atLine:__LINE__ \
michael@0 732 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 733 } \
michael@0 734 } while(0)
michael@0 735
michael@0 736
michael@0 737 /*" Generates a failure when expression evaluates to false.
michael@0 738 _{expr The expression that is tested.}
michael@0 739 _{description A format string as in the printf() function. Can be nil or
michael@0 740 an empty string but must be present.}
michael@0 741 _{... A variable number of arguments to the format string. Can be absent.}
michael@0 742 "*/
michael@0 743 #define STAssertTrue(expr, description, ...) \
michael@0 744 do { \
michael@0 745 BOOL _evaluatedExpression = (expr); \
michael@0 746 if (!_evaluatedExpression) { \
michael@0 747 NSString *_expression = [NSString stringWithUTF8String:#expr]; \
michael@0 748 [self failWithException:[NSException failureInCondition:_expression \
michael@0 749 isTrue:NO \
michael@0 750 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 751 atLine:__LINE__ \
michael@0 752 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 753 } \
michael@0 754 } while (0)
michael@0 755
michael@0 756
michael@0 757 /*" Generates a failure when expression evaluates to false and in addition will
michael@0 758 generate error messages if an exception is encountered.
michael@0 759 _{expr The expression that is tested.}
michael@0 760 _{description A format string as in the printf() function. Can be nil or
michael@0 761 an empty string but must be present.}
michael@0 762 _{... A variable number of arguments to the format string. Can be absent.}
michael@0 763 "*/
michael@0 764 #define STAssertTrueNoThrow(expr, description, ...) \
michael@0 765 do { \
michael@0 766 @try { \
michael@0 767 BOOL _evaluatedExpression = (expr); \
michael@0 768 if (!_evaluatedExpression) { \
michael@0 769 NSString *_expression = [NSString stringWithUTF8String:#expr]; \
michael@0 770 [self failWithException:[NSException failureInCondition:_expression \
michael@0 771 isTrue:NO \
michael@0 772 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 773 atLine:__LINE__ \
michael@0 774 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 775 } \
michael@0 776 } \
michael@0 777 @catch (id anException) { \
michael@0 778 [self failWithException:[NSException failureInRaise:[NSString stringWithFormat:@"(%s) ", #expr] \
michael@0 779 exception:anException \
michael@0 780 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 781 atLine:__LINE__ \
michael@0 782 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 783 } \
michael@0 784 } while (0)
michael@0 785
michael@0 786
michael@0 787 /*" Generates a failure when the expression evaluates to true.
michael@0 788 _{expr The expression that is tested.}
michael@0 789 _{description A format string as in the printf() function. Can be nil or
michael@0 790 an empty string but must be present.}
michael@0 791 _{... A variable number of arguments to the format string. Can be absent.}
michael@0 792 "*/
michael@0 793 #define STAssertFalse(expr, description, ...) \
michael@0 794 do { \
michael@0 795 BOOL _evaluatedExpression = (expr); \
michael@0 796 if (_evaluatedExpression) { \
michael@0 797 NSString *_expression = [NSString stringWithUTF8String:#expr]; \
michael@0 798 [self failWithException:[NSException failureInCondition:_expression \
michael@0 799 isTrue:YES \
michael@0 800 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 801 atLine:__LINE__ \
michael@0 802 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 803 } \
michael@0 804 } while (0)
michael@0 805
michael@0 806
michael@0 807 /*" Generates a failure when the expression evaluates to true and in addition
michael@0 808 will generate error messages if an exception is encountered.
michael@0 809 _{expr The expression that is tested.}
michael@0 810 _{description A format string as in the printf() function. Can be nil or
michael@0 811 an empty string but must be present.}
michael@0 812 _{... A variable number of arguments to the format string. Can be absent.}
michael@0 813 "*/
michael@0 814 #define STAssertFalseNoThrow(expr, description, ...) \
michael@0 815 do { \
michael@0 816 @try { \
michael@0 817 BOOL _evaluatedExpression = (expr); \
michael@0 818 if (_evaluatedExpression) { \
michael@0 819 NSString *_expression = [NSString stringWithUTF8String:#expr]; \
michael@0 820 [self failWithException:[NSException failureInCondition:_expression \
michael@0 821 isTrue:YES \
michael@0 822 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 823 atLine:__LINE__ \
michael@0 824 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 825 } \
michael@0 826 } \
michael@0 827 @catch (id anException) { \
michael@0 828 [self failWithException:[NSException failureInRaise:[NSString stringWithFormat:@"!(%s) ", #expr] \
michael@0 829 exception:anException \
michael@0 830 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 831 atLine:__LINE__ \
michael@0 832 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 833 } \
michael@0 834 } while (0)
michael@0 835
michael@0 836
michael@0 837 /*" Generates a failure when expression does not throw an exception.
michael@0 838 _{expression The expression that is evaluated.}
michael@0 839 _{description A format string as in the printf() function. Can be nil or
michael@0 840 an empty string but must be present.}
michael@0 841 _{... A variable number of arguments to the format string. Can be absent.
michael@0 842 "*/
michael@0 843 #define STAssertThrows(expr, description, ...) \
michael@0 844 do { \
michael@0 845 @try { \
michael@0 846 (expr); \
michael@0 847 } \
michael@0 848 @catch (id anException) { \
michael@0 849 continue; \
michael@0 850 } \
michael@0 851 [self failWithException:[NSException failureInRaise:[NSString stringWithUTF8String:#expr] \
michael@0 852 exception:nil \
michael@0 853 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 854 atLine:__LINE__ \
michael@0 855 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 856 } while (0)
michael@0 857
michael@0 858
michael@0 859 /*" Generates a failure when expression does not throw an exception of a
michael@0 860 specific class.
michael@0 861 _{expression The expression that is evaluated.}
michael@0 862 _{specificException The specified class of the exception.}
michael@0 863 _{description A format string as in the printf() function. Can be nil or
michael@0 864 an empty string but must be present.}
michael@0 865 _{... A variable number of arguments to the format string. Can be absent.}
michael@0 866 "*/
michael@0 867 #define STAssertThrowsSpecific(expr, specificException, description, ...) \
michael@0 868 do { \
michael@0 869 @try { \
michael@0 870 (expr); \
michael@0 871 } \
michael@0 872 @catch (specificException *anException) { \
michael@0 873 continue; \
michael@0 874 } \
michael@0 875 @catch (id anException) { \
michael@0 876 NSString *_descrip = STComposeString(@"(Expected exception: %@) %@", NSStringFromClass([specificException class]), description); \
michael@0 877 [self failWithException:[NSException failureInRaise:[NSString stringWithUTF8String:#expr] \
michael@0 878 exception:anException \
michael@0 879 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 880 atLine:__LINE__ \
michael@0 881 withDescription:@"%@", STComposeString(_descrip, ##__VA_ARGS__)]]; \
michael@0 882 continue; \
michael@0 883 } \
michael@0 884 NSString *_descrip = STComposeString(@"(Expected exception: %@) %@", NSStringFromClass([specificException class]), description); \
michael@0 885 [self failWithException:[NSException failureInRaise:[NSString stringWithUTF8String:#expr] \
michael@0 886 exception:nil \
michael@0 887 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 888 atLine:__LINE__ \
michael@0 889 withDescription:@"%@", STComposeString(_descrip, ##__VA_ARGS__)]]; \
michael@0 890 } while (0)
michael@0 891
michael@0 892
michael@0 893 /*" Generates a failure when expression does not throw an exception of a
michael@0 894 specific class with a specific name. Useful for those frameworks like
michael@0 895 AppKit or Foundation that throw generic NSException w/specific names
michael@0 896 (NSInvalidArgumentException, etc).
michael@0 897 _{expression The expression that is evaluated.}
michael@0 898 _{specificException The specified class of the exception.}
michael@0 899 _{aName The name of the specified exception.}
michael@0 900 _{description A format string as in the printf() function. Can be nil or
michael@0 901 an empty string but must be present.}
michael@0 902 _{... A variable number of arguments to the format string. Can be absent.}
michael@0 903
michael@0 904 "*/
michael@0 905 #define STAssertThrowsSpecificNamed(expr, specificException, aName, description, ...) \
michael@0 906 do { \
michael@0 907 @try { \
michael@0 908 (expr); \
michael@0 909 } \
michael@0 910 @catch (specificException *anException) { \
michael@0 911 if ([aName isEqualToString:[anException name]]) continue; \
michael@0 912 NSString *_descrip = STComposeString(@"(Expected exception: %@ (name: %@)) %@", NSStringFromClass([specificException class]), aName, description); \
michael@0 913 [self failWithException: \
michael@0 914 [NSException failureInRaise:[NSString stringWithUTF8String:#expr] \
michael@0 915 exception:anException \
michael@0 916 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 917 atLine:__LINE__ \
michael@0 918 withDescription:@"%@", STComposeString(_descrip, ##__VA_ARGS__)]]; \
michael@0 919 continue; \
michael@0 920 } \
michael@0 921 @catch (id anException) { \
michael@0 922 NSString *_descrip = STComposeString(@"(Expected exception: %@) %@", NSStringFromClass([specificException class]), description); \
michael@0 923 [self failWithException: \
michael@0 924 [NSException failureInRaise:[NSString stringWithUTF8String:#expr] \
michael@0 925 exception:anException \
michael@0 926 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 927 atLine:__LINE__ \
michael@0 928 withDescription:@"%@", STComposeString(_descrip, ##__VA_ARGS__)]]; \
michael@0 929 continue; \
michael@0 930 } \
michael@0 931 NSString *_descrip = STComposeString(@"(Expected exception: %@) %@", NSStringFromClass([specificException class]), description); \
michael@0 932 [self failWithException: \
michael@0 933 [NSException failureInRaise:[NSString stringWithUTF8String:#expr] \
michael@0 934 exception:nil \
michael@0 935 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 936 atLine:__LINE__ \
michael@0 937 withDescription:@"%@", STComposeString(_descrip, ##__VA_ARGS__)]]; \
michael@0 938 } while (0)
michael@0 939
michael@0 940
michael@0 941 /*" Generates a failure when expression does throw an exception.
michael@0 942 _{expression The expression that is evaluated.}
michael@0 943 _{description A format string as in the printf() function. Can be nil or
michael@0 944 an empty string but must be present.}
michael@0 945 _{... A variable number of arguments to the format string. Can be absent.}
michael@0 946 "*/
michael@0 947 #define STAssertNoThrow(expr, description, ...) \
michael@0 948 do { \
michael@0 949 @try { \
michael@0 950 (expr); \
michael@0 951 } \
michael@0 952 @catch (id anException) { \
michael@0 953 [self failWithException:[NSException failureInRaise:[NSString stringWithUTF8String:#expr] \
michael@0 954 exception:anException \
michael@0 955 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 956 atLine:__LINE__ \
michael@0 957 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 958 } \
michael@0 959 } while (0)
michael@0 960
michael@0 961
michael@0 962 /*" Generates a failure when expression does throw an exception of the specitied
michael@0 963 class. Any other exception is okay (i.e. does not generate a failure).
michael@0 964 _{expression The expression that is evaluated.}
michael@0 965 _{specificException The specified class of the exception.}
michael@0 966 _{description A format string as in the printf() function. Can be nil or
michael@0 967 an empty string but must be present.}
michael@0 968 _{... A variable number of arguments to the format string. Can be absent.}
michael@0 969 "*/
michael@0 970 #define STAssertNoThrowSpecific(expr, specificException, description, ...) \
michael@0 971 do { \
michael@0 972 @try { \
michael@0 973 (expr); \
michael@0 974 } \
michael@0 975 @catch (specificException *anException) { \
michael@0 976 [self failWithException:[NSException failureInRaise:[NSString stringWithUTF8String:#expr] \
michael@0 977 exception:anException \
michael@0 978 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 979 atLine:__LINE__ \
michael@0 980 withDescription:@"%@", STComposeString(description, ##__VA_ARGS__)]]; \
michael@0 981 } \
michael@0 982 @catch (id anythingElse) { \
michael@0 983 ; \
michael@0 984 } \
michael@0 985 } while (0)
michael@0 986
michael@0 987
michael@0 988 /*" Generates a failure when expression does throw an exception of a
michael@0 989 specific class with a specific name. Useful for those frameworks like
michael@0 990 AppKit or Foundation that throw generic NSException w/specific names
michael@0 991 (NSInvalidArgumentException, etc).
michael@0 992 _{expression The expression that is evaluated.}
michael@0 993 _{specificException The specified class of the exception.}
michael@0 994 _{aName The name of the specified exception.}
michael@0 995 _{description A format string as in the printf() function. Can be nil or
michael@0 996 an empty string but must be present.}
michael@0 997 _{... A variable number of arguments to the format string. Can be absent.}
michael@0 998
michael@0 999 "*/
michael@0 1000 #define STAssertNoThrowSpecificNamed(expr, specificException, aName, description, ...) \
michael@0 1001 do { \
michael@0 1002 @try { \
michael@0 1003 (expr); \
michael@0 1004 } \
michael@0 1005 @catch (specificException *anException) { \
michael@0 1006 if ([aName isEqualToString:[anException name]]) { \
michael@0 1007 NSString *_descrip = STComposeString(@"(Expected exception: %@ (name: %@)) %@", NSStringFromClass([specificException class]), aName, description); \
michael@0 1008 [self failWithException: \
michael@0 1009 [NSException failureInRaise:[NSString stringWithUTF8String:#expr] \
michael@0 1010 exception:anException \
michael@0 1011 inFile:[NSString stringWithUTF8String:__FILE__] \
michael@0 1012 atLine:__LINE__ \
michael@0 1013 withDescription:@"%@", STComposeString(_descrip, ##__VA_ARGS__)]]; \
michael@0 1014 } \
michael@0 1015 continue; \
michael@0 1016 } \
michael@0 1017 @catch (id anythingElse) { \
michael@0 1018 ; \
michael@0 1019 } \
michael@0 1020 } while (0)
michael@0 1021
michael@0 1022
michael@0 1023
michael@0 1024 @interface NSException (GTMSenTestAdditions)
michael@0 1025 + (NSException *)failureInFile:(NSString *)filename
michael@0 1026 atLine:(int)lineNumber
michael@0 1027 withDescription:(NSString *)formatString, ... NS_FORMAT_FUNCTION(3, 4);
michael@0 1028 + (NSException *)failureInCondition:(NSString *)condition
michael@0 1029 isTrue:(BOOL)isTrue
michael@0 1030 inFile:(NSString *)filename
michael@0 1031 atLine:(int)lineNumber
michael@0 1032 withDescription:(NSString *)formatString, ... NS_FORMAT_FUNCTION(5, 6);
michael@0 1033 + (NSException *)failureInEqualityBetweenObject:(id)left
michael@0 1034 andObject:(id)right
michael@0 1035 inFile:(NSString *)filename
michael@0 1036 atLine:(int)lineNumber
michael@0 1037 withDescription:(NSString *)formatString, ... NS_FORMAT_FUNCTION(5, 6);
michael@0 1038 + (NSException *)failureInEqualityBetweenValue:(NSValue *)left
michael@0 1039 andValue:(NSValue *)right
michael@0 1040 withAccuracy:(NSValue *)accuracy
michael@0 1041 inFile:(NSString *)filename
michael@0 1042 atLine:(int) ineNumber
michael@0 1043 withDescription:(NSString *)formatString, ... NS_FORMAT_FUNCTION(6, 7);
michael@0 1044 + (NSException *)failureInRaise:(NSString *)expression
michael@0 1045 inFile:(NSString *)filename
michael@0 1046 atLine:(int)lineNumber
michael@0 1047 withDescription:(NSString *)formatString, ... NS_FORMAT_FUNCTION(4, 5);
michael@0 1048 + (NSException *)failureInRaise:(NSString *)expression
michael@0 1049 exception:(NSException *)exception
michael@0 1050 inFile:(NSString *)filename
michael@0 1051 atLine:(int)lineNumber
michael@0 1052 withDescription:(NSString *)formatString, ... NS_FORMAT_FUNCTION(5, 6);
michael@0 1053 @end
michael@0 1054
michael@0 1055 // SENTE_END
michael@0 1056
michael@0 1057 @protocol SenTestCase
michael@0 1058 + (id)testCaseWithInvocation:(NSInvocation *)anInvocation;
michael@0 1059 - (id)initWithInvocation:(NSInvocation *)anInvocation;
michael@0 1060 - (void)setUp;
michael@0 1061 - (void)invokeTest;
michael@0 1062 - (void)tearDown;
michael@0 1063 - (void)performTest;
michael@0 1064 - (void)failWithException:(NSException*)exception;
michael@0 1065 - (NSInvocation *)invocation;
michael@0 1066 - (SEL)selector;
michael@0 1067 + (NSArray *)testInvocations;
michael@0 1068 @end
michael@0 1069
michael@0 1070 @interface SenTestCase : NSObject<SenTestCase> {
michael@0 1071 @private
michael@0 1072 NSInvocation *invocation_;
michael@0 1073 }
michael@0 1074 @end
michael@0 1075
michael@0 1076 GTM_EXTERN NSString *const SenTestFailureException;
michael@0 1077
michael@0 1078 GTM_EXTERN NSString *const SenTestFilenameKey;
michael@0 1079 GTM_EXTERN NSString *const SenTestLineNumberKey;
michael@0 1080
michael@0 1081 #endif // GTM_IPHONE_SDK && !GTM_IPHONE_USE_SENTEST
michael@0 1082
michael@0 1083 // All unittest cases in GTM should inherit from GTMTestCase. It makes sure
michael@0 1084 // to set up our logging system correctly to verify logging calls.
michael@0 1085 // See GTMUnitTestDevLog.h for details
michael@0 1086 @interface GTMTestCase : SenTestCase
michael@0 1087
michael@0 1088 // Returns YES if this is an abstract testCase class as opposed to a concrete
michael@0 1089 // testCase class that you want tests run against. SenTestCase is not designed
michael@0 1090 // out of the box to handle an abstract class hierarchy descending from it with
michael@0 1091 // some concrete subclasses. In some cases we want all the "concrete"
michael@0 1092 // subclasses of an abstract subclass of SenTestCase to run a test, but we don't
michael@0 1093 // want that test to be run against an instance of an abstract subclass itself.
michael@0 1094 // By returning "YES" here, the tests defined by this class won't be run against
michael@0 1095 // an instance of this class. As an example class hierarchy:
michael@0 1096 //
michael@0 1097 // FooExtensionTestCase
michael@0 1098 // GTMTestCase <- ExtensionAbstractTestCase <
michael@0 1099 // BarExtensionTestCase
michael@0 1100 //
michael@0 1101 // So FooExtensionTestCase and BarExtensionTestCase inherit from
michael@0 1102 // ExtensionAbstractTestCase (and probably FooExtension and BarExtension inherit
michael@0 1103 // from a class named Extension). We want the tests in ExtensionAbstractTestCase
michael@0 1104 // to be run as part of FooExtensionTestCase and BarExtensionTestCase, but we
michael@0 1105 // don't want them run against ExtensionAbstractTestCase. The default
michael@0 1106 // implementation checks to see if the name of the class contains the word
michael@0 1107 // "AbstractTest" (case sensitive).
michael@0 1108 + (BOOL)isAbstractTestCase;
michael@0 1109
michael@0 1110 @end

mercurial