PageRenderTime 51ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/MapView/GTM/GTMSenTestCase.h

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