PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/thirdparty/breakpad/common/mac/testing/GTMSenTestCase.h

http://github.com/tomahawk-player/tomahawk
C++ Header | 1004 lines | 645 code | 66 blank | 293 comment | 79 complexity | c5894571506dac69c8279ec4c84d7a1f MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-3.0, GPL-2.0
  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. NSString *STComposeString(NSString *, ...);
  57. #endif
  58. // Generates a failure when a1 != noErr
  59. // Args:
  60. // a1: should be either an OSErr or an OSStatus
  61. // description: A format string as in the printf() function. Can be nil or
  62. // an empty string but must be present.
  63. // ...: A variable number of arguments to the format string. Can be absent.
  64. #define STAssertNoErr(a1, description, ...) \
  65. do { \
  66. @try {\
  67. OSStatus a1value = (a1); \
  68. if (a1value != noErr) { \
  69. NSString *_expression = [NSString stringWithFormat:@"Expected noErr, got %ld for (%s)", a1value, #a1]; \
  70. if (description) { \
  71. _expression = [NSString stringWithFormat:@"%@: %@", _expression, STComposeString(description, ##__VA_ARGS__)]; \
  72. } \
  73. [self failWithException:[NSException failureInFile:[NSString stringWithUTF8String:__FILE__] \
  74. atLine:__LINE__ \
  75. withDescription:_expression]]; \
  76. } \
  77. }\
  78. @catch (id anException) {\
  79. [self failWithException:[NSException failureInRaise:[NSString stringWithFormat:@"(%s) == noErr fails", #a1] \
  80. exception:anException \
  81. inFile:[NSString stringWithUTF8String:__FILE__] \
  82. atLine:__LINE__ \
  83. withDescription:STComposeString(description, ##__VA_ARGS__)]]; \
  84. }\
  85. } while(0)
  86. // Generates a failure when a1 != a2
  87. // Args:
  88. // a1: received value. Should be either an OSErr or an OSStatus
  89. // a2: expected value. Should be either an OSErr or an OSStatus
  90. // description: A format string as in the printf() function. Can be nil or
  91. // an empty string but must be present.
  92. // ...: A variable number of arguments to the format string. Can be absent.
  93. #define STAssertErr(a1, a2, description, ...) \
  94. do { \
  95. @try {\
  96. OSStatus a1value = (a1); \
  97. OSStatus a2value = (a2); \
  98. if (a1value != a2value) { \
  99. NSString *_expression = [NSString stringWithFormat:@"Expected %s(%ld) but got %ld for (%s)", #a2, a2value, a1value, #a1]; \
  100. if (description) { \
  101. _expression = [NSString stringWithFormat:@"%@: %@", _expression, STComposeString(description, ##__VA_ARGS__)]; \
  102. } \
  103. [self failWithException:[NSException failureInFile:[NSString stringWithUTF8String:__FILE__] \
  104. atLine:__LINE__ \
  105. withDescription:_expression]]; \
  106. } \
  107. }\
  108. @catch (id anException) {\
  109. [self failWithException:[NSException failureInRaise:[NSString stringWithFormat:@"(%s) == (%s) fails", #a1, #a2] \
  110. exception:anException \
  111. inFile:[NSString stringWithUTF8String:__FILE__] \
  112. atLine:__LINE__ \
  113. withDescription:STComposeString(description, ##__VA_ARGS__)]]; \
  114. }\
  115. } while(0)
  116. // Generates a failure when a1 is NULL
  117. // Args:
  118. // a1: should be a pointer (use STAssertNotNil for an object)
  119. // description: A format string as in the printf() function. Can be nil or
  120. // an empty string but must be present.
  121. // ...: A variable number of arguments to the format string. Can be absent.
  122. #define STAssertNotNULL(a1, description, ...) \
  123. do { \
  124. @try {\
  125. const void* a1value = (a1); \
  126. if (a1value == NULL) { \
  127. NSString *_expression = [NSString stringWithFormat:@"(%s) != NULL", #a1]; \
  128. if (description) { \
  129. _expression = [NSString stringWithFormat:@"%@: %@", _expression, STComposeString(description, ##__VA_ARGS__)]; \
  130. } \
  131. [self failWithException:[NSException failureInFile:[NSString stringWithUTF8String:__FILE__] \
  132. atLine:__LINE__ \
  133. withDescription:_expression]]; \
  134. } \
  135. }\
  136. @catch (id anException) {\
  137. [self failWithException:[NSException failureInRaise:[NSString stringWithFormat:@"(%s) != NULL fails", #a1] \
  138. exception:anException \
  139. inFile:[NSString stringWithUTF8String:__FILE__] \
  140. atLine:__LINE__ \
  141. withDescription:STComposeString(description, ##__VA_ARGS__)]]; \
  142. }\
  143. } while(0)
  144. // Generates a failure when a1 is not NULL
  145. // Args:
  146. // a1: should be a pointer (use STAssertNil for an object)
  147. // description: A format string as in the printf() function. Can be nil or
  148. // an empty string but must be present.
  149. // ...: A variable number of arguments to the format string. Can be absent.
  150. #define STAssertNULL(a1, description, ...) \
  151. do { \
  152. @try {\
  153. const void* a1value = (a1); \
  154. if (a1value != NULL) { \
  155. NSString *_expression = [NSString stringWithFormat:@"(%s) == NULL", #a1]; \
  156. if (description) { \
  157. _expression = [NSString stringWithFormat:@"%@: %@", _expression, STComposeString(description, ##__VA_ARGS__)]; \
  158. } \
  159. [self failWithException:[NSException failureInFile:[NSString stringWithUTF8String:__FILE__] \
  160. atLine:__LINE__ \
  161. withDescription:_expression]]; \
  162. } \
  163. }\
  164. @catch (id anException) {\
  165. [self failWithException:[NSException failureInRaise:[NSString stringWithFormat:@"(%s) == NULL fails", #a1] \
  166. exception:anException \
  167. inFile:[NSString stringWithUTF8String:__FILE__] \
  168. atLine:__LINE__ \
  169. withDescription:STComposeString(description, ##__VA_ARGS__)]]; \
  170. }\
  171. } while(0)
  172. // Generates a failure when a1 is equal to a2. This test is for C scalars,
  173. // structs and unions.
  174. // Args:
  175. // a1: argument 1
  176. // a2: argument 2
  177. // description: A format string as in the printf() function. Can be nil or
  178. // an empty string but must be present.
  179. // ...: A variable number of arguments to the format string. Can be absent.
  180. #define STAssertNotEquals(a1, a2, description, ...) \
  181. do { \
  182. @try {\
  183. if (@encode(__typeof__(a1)) != @encode(__typeof__(a2))) { \
  184. [self failWithException:[NSException failureInFile:[NSString stringWithUTF8String:__FILE__] \
  185. atLine:__LINE__ \
  186. withDescription:[[[NSString stringWithFormat:@"Type mismatch (%@/%@) -- ",@encode(__typeof__(a1)),@encode(__typeof__(a2))] stringByAppendingString:STComposeString(description, ##__VA_ARGS__)]]]; \
  187. } else { \
  188. __typeof__(a1) a1value = (a1); \
  189. __typeof__(a2) a2value = (a2); \
  190. NSValue *a1encoded = [NSValue value:&a1value withObjCType:@encode(__typeof__(a1))]; \
  191. NSValue *a2encoded = [NSValue value:&a2value withObjCType:@encode(__typeof__(a2))]; \
  192. if ([a1encoded isEqualToValue:a2encoded]) { \
  193. NSString *_expression = [NSString stringWithFormat:@"(%s) != (%s)", #a1, #a2]; \
  194. if (description) { \
  195. _expression = [NSString stringWithFormat:@"%@: %@", _expression, STComposeString(description, ##__VA_ARGS__)]; \
  196. } \
  197. [self failWithException:[NSException failureInFile:[NSString stringWithUTF8String:__FILE__] \
  198. atLine:__LINE__ \
  199. withDescription:_expression]]; \
  200. } \
  201. } \
  202. } \
  203. @catch (id anException) {\
  204. [self failWithException:[NSException failureInRaise:[NSString stringWithFormat:@"(%s) != (%s)", #a1, #a2] \
  205. exception:anException \
  206. inFile:[NSString stringWithUTF8String:__FILE__] \
  207. atLine:__LINE__ \
  208. withDescription:STComposeString(description, ##__VA_ARGS__)]]; \
  209. }\
  210. } while(0)
  211. // Generates a failure when a1 is equal to a2. This test is for objects.
  212. // Args:
  213. // a1: argument 1. object.
  214. // a2: argument 2. object.
  215. // description: A format string as in the printf() function. Can be nil or
  216. // an empty string but must be present.
  217. // ...: A variable number of arguments to the format string. Can be absent.
  218. #define STAssertNotEqualObjects(a1, a2, desc, ...) \
  219. do { \
  220. @try {\
  221. id a1value = (a1); \
  222. id a2value = (a2); \
  223. if ( (@encode(__typeof__(a1value)) == @encode(id)) && \
  224. (@encode(__typeof__(a2value)) == @encode(id)) && \
  225. ![(id)a1value isEqual:(id)a2value] ) continue; \
  226. NSString *_expression = [NSString stringWithFormat:@"%s('%@') != %s('%@')", #a1, [a1 description], #a2, [a2 description]]; \
  227. if (desc) { \
  228. _expression = [NSString stringWithFormat:@"%@: %@", _expression, STComposeString(desc, ##__VA_ARGS__)]; \
  229. } \
  230. [self failWithException:[NSException failureInFile:[NSString stringWithUTF8String:__FILE__] \
  231. atLine:__LINE__ \
  232. withDescription:_expression]]; \
  233. }\
  234. @catch (id anException) {\
  235. [self failWithException:[NSException failureInRaise:[NSString stringWithFormat: @"(%s) != (%s)", #a1, #a2] \
  236. exception:anException \
  237. inFile:[NSString stringWithUTF8String:__FILE__] \
  238. atLine:__LINE__ \
  239. withDescription:STComposeString(desc, ##__VA_ARGS__)]]; \
  240. }\
  241. } while(0)
  242. // Generates a failure when a1 is not 'op' to a2. This test is for C scalars.
  243. // Args:
  244. // a1: argument 1
  245. // a2: argument 2
  246. // op: operation
  247. // description: A format string as in the printf() function. Can be nil or
  248. // an empty string but must be present.
  249. // ...: A variable number of arguments to the format string. Can be absent.
  250. #define STAssertOperation(a1, a2, op, description, ...) \
  251. do { \
  252. @try {\
  253. if (@encode(__typeof__(a1)) != @encode(__typeof__(a2))) { \
  254. [self failWithException:[NSException failureInFile:[NSString stringWithUTF8String:__FILE__] \
  255. atLine:__LINE__ \
  256. withDescription:[[[NSString stringWithFormat:@"Type mismatch (%@/%@) -- ",@encode(__typeof__(a1)),@encode(__typeof__(a2))] stringByAppendingString:STComposeString(description, ##__VA_ARGS__)]]]; \
  257. } else { \
  258. __typeof__(a1) a1value = (a1); \
  259. __typeof__(a2) a2value = (a2); \
  260. if (!(a1value op a2value)) { \
  261. double a1DoubleValue = a1value; \
  262. double a2DoubleValue = a2value; \
  263. NSString *_expression = [NSString stringWithFormat:@"%s (%lg) %s %s (%lg)", #a1, a1DoubleValue, #op, #a2, a2DoubleValue]; \
  264. if (description) { \
  265. _expression = [NSString stringWithFormat:@"%@: %@", _expression, STComposeString(description, ##__VA_ARGS__)]; \
  266. } \
  267. [self failWithException:[NSException failureInFile:[NSString stringWithUTF8String:__FILE__] \
  268. atLine:__LINE__ \
  269. withDescription:_expression]]; \
  270. } \
  271. } \
  272. } \
  273. @catch (id anException) {\
  274. [self failWithException:[NSException \
  275. failureInRaise:[NSString stringWithFormat:@"(%s) %s (%s)", #a1, #op, #a2] \
  276. exception:anException \
  277. inFile:[NSString stringWithUTF8String:__FILE__] \
  278. atLine:__LINE__ \
  279. withDescription:STComposeString(description, ##__VA_ARGS__)]]; \
  280. }\
  281. } while(0)
  282. // Generates a failure when a1 is not > a2. This test is for C scalars.
  283. // Args:
  284. // a1: argument 1
  285. // a2: argument 2
  286. // op: operation
  287. // description: A format string as in the printf() function. Can be nil or
  288. // an empty string but must be present.
  289. // ...: A variable number of arguments to the format string. Can be absent.
  290. #define STAssertGreaterThan(a1, a2, description, ...) \
  291. STAssertOperation(a1, a2, >, description, ##__VA_ARGS__)
  292. // Generates a failure when a1 is not >= a2. This test is for C scalars.
  293. // Args:
  294. // a1: argument 1
  295. // a2: argument 2
  296. // op: operation
  297. // description: A format string as in the printf() function. Can be nil or
  298. // an empty string but must be present.
  299. // ...: A variable number of arguments to the format string. Can be absent.
  300. #define STAssertGreaterThanOrEqual(a1, a2, description, ...) \
  301. STAssertOperation(a1, a2, >=, description, ##__VA_ARGS__)
  302. // Generates a failure when a1 is not < a2. This test is for C scalars.
  303. // Args:
  304. // a1: argument 1
  305. // a2: argument 2
  306. // op: operation
  307. // description: A format string as in the printf() function. Can be nil or
  308. // an empty string but must be present.
  309. // ...: A variable number of arguments to the format string. Can be absent.
  310. #define STAssertLessThan(a1, a2, description, ...) \
  311. STAssertOperation(a1, a2, <, description, ##__VA_ARGS__)
  312. // Generates a failure when a1 is not <= a2. This test is for C scalars.
  313. // Args:
  314. // a1: argument 1
  315. // a2: argument 2
  316. // op: operation
  317. // description: A format string as in the printf() function. Can be nil or
  318. // an empty string but must be present.
  319. // ...: A variable number of arguments to the format string. Can be absent.
  320. #define STAssertLessThanOrEqual(a1, a2, description, ...) \
  321. STAssertOperation(a1, a2, <=, description, ##__VA_ARGS__)
  322. // Generates a failure when string a1 is not equal to string a2. This call
  323. // differs from STAssertEqualObjects in that strings that are different in
  324. // composition (precomposed vs decomposed) will compare equal if their final
  325. // representation is equal.
  326. // ex O + umlaut decomposed is the same as O + umlaut composed.
  327. // Args:
  328. // a1: string 1
  329. // a2: string 2
  330. // description: A format string as in the printf() function. Can be nil or
  331. // an empty string but must be present.
  332. // ...: A variable number of arguments to the format string. Can be absent.
  333. #define STAssertEqualStrings(a1, a2, description, ...) \
  334. do { \
  335. @try {\
  336. id a1value = (a1); \
  337. id a2value = (a2); \
  338. if (a1value == a2value) continue; \
  339. if ([a1value isKindOfClass:[NSString class]] && \
  340. [a2value isKindOfClass:[NSString class]] && \
  341. [a1value compare:a2value options:0] == NSOrderedSame) continue; \
  342. [self failWithException:[NSException failureInEqualityBetweenObject: a1value \
  343. andObject: a2value \
  344. inFile: [NSString stringWithUTF8String:__FILE__] \
  345. atLine: __LINE__ \
  346. withDescription: STComposeString(description, ##__VA_ARGS__)]]; \
  347. }\
  348. @catch (id anException) {\
  349. [self failWithException:[NSException failureInRaise:[NSString stringWithFormat: @"(%s) == (%s)", #a1, #a2] \
  350. exception:anException \
  351. inFile:[NSString stringWithUTF8String:__FILE__] \
  352. atLine:__LINE__ \
  353. withDescription:STComposeString(description, ##__VA_ARGS__)]]; \
  354. }\
  355. } while(0)
  356. // Generates a failure when string a1 is equal to string a2. This call
  357. // differs from STAssertEqualObjects in that strings that are different in
  358. // composition (precomposed vs decomposed) will compare equal if their final
  359. // representation is equal.
  360. // ex O + umlaut decomposed is the same as O + umlaut composed.
  361. // Args:
  362. // a1: string 1
  363. // a2: string 2
  364. // description: A format string as in the printf() function. Can be nil or
  365. // an empty string but must be present.
  366. // ...: A variable number of arguments to the format string. Can be absent.
  367. #define STAssertNotEqualStrings(a1, a2, description, ...) \
  368. do { \
  369. @try {\
  370. id a1value = (a1); \
  371. id a2value = (a2); \
  372. if ([a1value isKindOfClass:[NSString class]] && \
  373. [a2value isKindOfClass:[NSString class]] && \
  374. [a1value compare:a2value options:0] != NSOrderedSame) continue; \
  375. [self failWithException:[NSException failureInEqualityBetweenObject: a1value \
  376. andObject: a2value \
  377. inFile: [NSString stringWithUTF8String:__FILE__] \
  378. atLine: __LINE__ \
  379. withDescription: STComposeString(description, ##__VA_ARGS__)]]; \
  380. }\
  381. @catch (id anException) {\
  382. [self failWithException:[NSException failureInRaise:[NSString stringWithFormat: @"(%s) != (%s)", #a1, #a2] \
  383. exception:anException \
  384. inFile:[NSString stringWithUTF8String:__FILE__] \
  385. atLine:__LINE__ \
  386. withDescription:STComposeString(description, ##__VA_ARGS__)]]; \
  387. }\
  388. } while(0)
  389. // Generates a failure when c-string a1 is not equal to c-string a2.
  390. // Args:
  391. // a1: string 1
  392. // a2: string 2
  393. // description: A format string as in the printf() function. Can be nil or
  394. // an empty string but must be present.
  395. // ...: A variable number of arguments to the format string. Can be absent.
  396. #define STAssertEqualCStrings(a1, a2, description, ...) \
  397. do { \
  398. @try {\
  399. const char* a1value = (a1); \
  400. const char* a2value = (a2); \
  401. if (a1value == a2value) continue; \
  402. if (strcmp(a1value, a2value) == 0) continue; \
  403. [self failWithException:[NSException failureInEqualityBetweenObject: [NSString stringWithUTF8String:a1value] \
  404. andObject: [NSString stringWithUTF8String:a2value] \
  405. inFile: [NSString stringWithUTF8String:__FILE__] \
  406. atLine: __LINE__ \
  407. withDescription: STComposeString(description, ##__VA_ARGS__)]]; \
  408. }\
  409. @catch (id anException) {\
  410. [self failWithException:[NSException failureInRaise:[NSString stringWithFormat: @"(%s) == (%s)", #a1, #a2] \
  411. exception:anException \
  412. inFile:[NSString stringWithUTF8String:__FILE__] \
  413. atLine:__LINE__ \
  414. withDescription:STComposeString(description, ##__VA_ARGS__)]]; \
  415. }\
  416. } while(0)
  417. // Generates a failure when c-string a1 is equal to c-string a2.
  418. // Args:
  419. // a1: string 1
  420. // a2: string 2
  421. // description: A format string as in the printf() function. Can be nil or
  422. // an empty string but must be present.
  423. // ...: A variable number of arguments to the format string. Can be absent.
  424. #define STAssertNotEqualCStrings(a1, a2, description, ...) \
  425. do { \
  426. @try {\
  427. const char* a1value = (a1); \
  428. const char* a2value = (a2); \
  429. if (strcmp(a1value, a2value) != 0) continue; \
  430. [self failWithException:[NSException failureInEqualityBetweenObject: [NSString stringWithUTF8String:a1value] \
  431. andObject: [NSString stringWithUTF8String:a2value] \
  432. inFile: [NSString stringWithUTF8String:__FILE__] \
  433. atLine: __LINE__ \
  434. withDescription: STComposeString(description, ##__VA_ARGS__)]]; \
  435. }\
  436. @catch (id anException) {\
  437. [self failWithException:[NSException failureInRaise:[NSString stringWithFormat: @"(%s) != (%s)", #a1, #a2] \
  438. exception:anException \
  439. inFile:[NSString stringWithUTF8String:__FILE__] \
  440. atLine:__LINE__ \
  441. withDescription:STComposeString(description, ##__VA_ARGS__)]]; \
  442. }\
  443. } while(0)
  444. #if GTM_IPHONE_SDK
  445. // SENTE_BEGIN
  446. /*" Generates a failure when !{ [a1 isEqualTo:a2] } is false
  447. (or one is nil and the other is not).
  448. _{a1 The object on the left.}
  449. _{a2 The object on the right.}
  450. _{description A format string as in the printf() function. Can be nil or
  451. an empty string but must be present.}
  452. _{... A variable number of arguments to the format string. Can be absent.}
  453. "*/
  454. #define STAssertEqualObjects(a1, a2, description, ...) \
  455. do { \
  456. @try {\
  457. id a1value = (a1); \
  458. id a2value = (a2); \
  459. if (a1value == a2value) continue; \
  460. if ( (@encode(__typeof__(a1value)) == @encode(id)) && \
  461. (@encode(__typeof__(a2value)) == @encode(id)) && \
  462. [(id)a1value isEqual: (id)a2value] ) continue; \
  463. [self failWithException:[NSException failureInEqualityBetweenObject: a1value \
  464. andObject: a2value \
  465. inFile: [NSString stringWithUTF8String:__FILE__] \
  466. atLine: __LINE__ \
  467. withDescription: STComposeString(description, ##__VA_ARGS__)]]; \
  468. }\
  469. @catch (id anException) {\
  470. [self failWithException:[NSException failureInRaise:[NSString stringWithFormat: @"(%s) == (%s)", #a1, #a2] \
  471. exception:anException \
  472. inFile:[NSString stringWithUTF8String:__FILE__] \
  473. atLine:__LINE__ \
  474. withDescription:STComposeString(description, ##__VA_ARGS__)]]; \
  475. }\
  476. } while(0)
  477. /*" Generates a failure when a1 is not equal to a2. This test is for
  478. C scalars, structs and unions.
  479. _{a1 The argument on the left.}
  480. _{a2 The argument on the right.}
  481. _{description A format string as in the printf() function. Can be nil or
  482. an empty string but must be present.}
  483. _{... A variable number of arguments to the format string. Can be absent.}
  484. "*/
  485. #define STAssertEquals(a1, a2, description, ...) \
  486. do { \
  487. @try {\
  488. if (@encode(__typeof__(a1)) != @encode(__typeof__(a2))) { \
  489. [self failWithException:[NSException failureInFile:[NSString stringWithUTF8String:__FILE__] \
  490. atLine:__LINE__ \
  491. withDescription:[[NSString stringWithFormat:@"Type mismatch (%@/%@) -- ",@encode(__typeof__(a1)),@encode(__typeof__(a2))] stringByAppendingString:STComposeString(description, ##__VA_ARGS__)]]]; \
  492. } else { \
  493. __typeof__(a1) a1value = (a1); \
  494. __typeof__(a2) a2value = (a2); \
  495. NSValue *a1encoded = [NSValue value:&a1value withObjCType: @encode(__typeof__(a1))]; \
  496. NSValue *a2encoded = [NSValue value:&a2value withObjCType: @encode(__typeof__(a2))]; \
  497. if (![a1encoded isEqualToValue:a2encoded]) { \
  498. [self failWithException:[NSException failureInEqualityBetweenValue: a1encoded \
  499. andValue: a2encoded \
  500. withAccuracy: nil \
  501. inFile: [NSString stringWithUTF8String:__FILE__] \
  502. atLine: __LINE__ \
  503. withDescription: STComposeString(description, ##__VA_ARGS__)]]; \
  504. } \
  505. } \
  506. } \
  507. @catch (id anException) {\
  508. [self failWithException:[NSException failureInRaise:[NSString stringWithFormat: @"(%s) == (%s)", #a1, #a2] \
  509. exception:anException \
  510. inFile:[NSString stringWithUTF8String:__FILE__] \
  511. atLine:__LINE__ \
  512. withDescription:STComposeString(description, ##__VA_ARGS__)]]; \
  513. }\
  514. } while(0)
  515. #define STAbsoluteDifference(left,right) (MAX(left,right)-MIN(left,right))
  516. /*" Generates a failure when a1 is not equal to a2 within + or - accuracy is false.
  517. This test is for scalars such as floats and doubles where small differences
  518. could make these items not exactly equal, but also works for all scalars.
  519. _{a1 The scalar on the left.}
  520. _{a2 The scalar on the right.}
  521. _{accuracy The maximum difference between a1 and a2 for these values to be
  522. considered equal.}
  523. _{description A format string as in the printf() function. Can be nil or
  524. an empty string but must be present.}
  525. _{... A variable number of arguments to the format string. Can be absent.}
  526. "*/
  527. #define STAssertEqualsWithAccuracy(a1, a2, accuracy, description, ...) \
  528. do { \
  529. @try {\
  530. if (@encode(__typeof__(a1)) != @encode(__typeof__(a2))) { \
  531. [self failWithException:[NSException failureInFile:[NSString stringWithUTF8String:__FILE__] \
  532. atLine:__LINE__ \
  533. withDescription:[[[NSString stringWithFormat:@"Type mismatch (%@/%@) -- ",@encode(__typeof__(a1)),@encode(__typeof__(a2))] stringByAppendingString:STComposeString(description, ##__VA_ARGS__)]]]; \
  534. } else { \
  535. __typeof__(a1) a1value = (a1); \
  536. __typeof__(a2) a2value = (a2); \
  537. __typeof__(accuracy) accuracyvalue = (accuracy); \
  538. if (STAbsoluteDifference(a1value, a2value) > accuracyvalue) { \
  539. NSValue *a1encoded = [NSValue value:&a1value withObjCType:@encode(__typeof__(a1))]; \
  540. NSValue *a2encoded = [NSValue value:&a2value withObjCType:@encode(__typeof__(a2))]; \
  541. NSValue *accuracyencoded = [NSValue value:&accuracyvalue withObjCType:@encode(__typeof__(accuracy))]; \
  542. [self failWithException:[NSException failureInEqualityBetweenValue: a1encoded \
  543. andValue: a2encoded \
  544. withAccuracy: accuracyencoded \
  545. inFile: [NSString stringWithUTF8String:__FILE__] \
  546. atLine: __LINE__ \
  547. withDescription: STComposeString(description, ##__VA_ARGS__)]]; \
  548. } \
  549. } \
  550. } \
  551. @catch (id anException) {\
  552. [self failWithException:[NSException failureInRaise:[NSString stringWithFormat: @"(%s) == (%s)", #a1, #a2] \
  553. exception:anException \
  554. inFile:[NSString stringWithUTF8String:__FILE__] \
  555. atLine:__LINE__ \
  556. withDescription:STComposeString(description, ##__VA_ARGS__)]]; \
  557. }\
  558. } while(0)
  559. /*" Generates a failure unconditionally.
  560. _{description A format string as in the printf() function. Can be nil or
  561. an empty string but must be present.}
  562. _{... A variable number of arguments to the format string. Can be absent.}
  563. "*/
  564. #define STFail(description, ...) \
  565. [self failWithException:[NSException failureInFile: [NSString stringWithUTF8String:__FILE__] \
  566. atLine: __LINE__ \
  567. withDescription: STComposeString(description, ##__VA_ARGS__)]]
  568. /*" Generates a failure when a1 is not nil.
  569. _{a1 An object.}
  570. _{description A format string as in the printf() function. Can be nil or
  571. an empty string but must be present.}
  572. _{... A variable number of arguments to the format string. Can be absent.}
  573. "*/
  574. #define STAssertNil(a1, description, ...) \
  575. do { \
  576. @try {\
  577. id a1value = (a1); \
  578. if (a1value != nil) { \
  579. NSString *_a1 = [NSString stringWithUTF8String: #a1]; \
  580. NSString *_expression = [NSString stringWithFormat:@"((%@) == nil)", _a1]; \
  581. [self failWithException:[NSException failureInCondition: _expression \
  582. isTrue: NO \
  583. inFile: [NSString stringWithUTF8String:__FILE__] \
  584. atLine: __LINE__ \
  585. withDescription: STComposeString(description, ##__VA_ARGS__)]]; \
  586. } \
  587. }\
  588. @catch (id anException) {\
  589. [self failWithException:[NSException failureInRaise:[NSString stringWithFormat: @"(%s) == nil fails", #a1] \
  590. exception:anException \
  591. inFile:[NSString stringWithUTF8String:__FILE__] \
  592. atLine:__LINE__ \
  593. withDescription:STComposeString(description, ##__VA_ARGS__)]]; \
  594. }\
  595. } while(0)
  596. /*" Generates a failure when a1 is nil.
  597. _{a1 An object.}
  598. _{description A format string as in the printf() function. Can be nil or
  599. an empty string but must be present.}
  600. _{... A variable number of arguments to the format string. Can be absent.}
  601. "*/
  602. #define STAssertNotNil(a1, description, ...) \
  603. do { \
  604. @try {\
  605. id a1value = (a1); \
  606. if (a1value == nil) { \
  607. NSString *_a1 = [NSString stringWithUTF8String: #a1]; \
  608. NSString *_expression = [NSString stringWithFormat:@"((%@) != nil)", _a1]; \
  609. [self failWithException:[NSException failureInCondition: _expression \
  610. isTrue: NO \
  611. inFile: [NSString stringWithUTF8String:__FILE__] \
  612. atLine: __LINE__ \
  613. withDescription: STComposeString(description, ##__VA_ARGS__)]]; \
  614. } \
  615. }\
  616. @catch (id anException) {\
  617. [self failWithException:[NSException failureInRaise:[NSString stringWithFormat: @"(%s) != nil fails", #a1] \
  618. exception:anException \
  619. inFile:[NSString stringWithUTF8String:__FILE__] \
  620. atLine:__LINE__ \
  621. withDescription:STComposeString(description, ##__VA_ARGS__)]]; \
  622. }\
  623. } while(0)
  624. /*" Generates a failure when expression evaluates to false.
  625. _{expr The expression that is tested.}
  626. _{description A format string as in the printf() function. Can be nil or
  627. an empty string but must be present.}
  628. _{... A variable number of arguments to the format string. Can be absent.}
  629. "*/
  630. #define STAssertTrue(expr, description, ...) \
  631. do { \
  632. BOOL _evaluatedExpression = (expr);\
  633. if (!_evaluatedExpression) {\
  634. NSString *_expression = [NSString stringWithUTF8String: #expr];\
  635. [self failWithException:[NSException failureInCondition: _expression \
  636. isTrue: NO \
  637. inFile: [NSString stringWithUTF8String:__FILE__] \
  638. atLine: __LINE__ \
  639. withDescription: STComposeString(description, ##__VA_ARGS__)]]; \
  640. } \
  641. } while (0)
  642. /*" Generates a failure when expression evaluates to false and in addition will
  643. generate error messages if an exception is encountered.
  644. _{expr The expression that is tested.}
  645. _{description A format string as in the printf() function. Can be nil or
  646. an empty string but must be present.}
  647. _{... A variable number of arguments to the format string. Can be absent.}
  648. "*/
  649. #define STAssertTrueNoThrow(expr, description, ...) \
  650. do { \
  651. @try {\
  652. BOOL _evaluatedExpression = (expr);\
  653. if (!_evaluatedExpression) {\
  654. NSString *_expression = [NSString stringWithUTF8String: #expr];\
  655. [self failWithException:[NSException failureInCondition: _expression \
  656. isTrue: NO \
  657. inFile: [NSString stringWithUTF8String:__FILE__] \
  658. atLine: __LINE__ \
  659. withDescription: STComposeString(description, ##__VA_ARGS__)]]; \
  660. } \
  661. } \
  662. @catch (id anException) {\
  663. [self failWithException:[NSException failureInRaise:[NSString stringWithFormat: @"(%s) ", #expr] \
  664. exception:anException \
  665. inFile:[NSString stringWithUTF8String:__FILE__] \
  666. atLine:__LINE__ \
  667. withDescription:STComposeString(description, ##__VA_ARGS__)]]; \
  668. }\
  669. } while (0)
  670. /*" Generates a failure when the expression evaluates to true.
  671. _{expr The expression that is tested.}
  672. _{description A format string as in the printf() function. Can be nil or
  673. an empty string but must be present.}
  674. _{... A variable number of arguments to the format string. Can be absent.}
  675. "*/
  676. #define STAssertFalse(expr, description, ...) \
  677. do { \
  678. BOOL _evaluatedExpression = (expr);\
  679. if (_evaluatedExpression) {\
  680. NSString *_expression = [NSString stringWithUTF8String: #expr];\
  681. [self failWithException:[NSException failureInCondition: _expression \
  682. isTrue: YES \
  683. inFile: [NSString stringWithUTF8String:__FILE__] \
  684. atLine: __LINE__ \
  685. withDescription: STComposeString(description, ##__VA_ARGS__)]]; \
  686. } \
  687. } while (0)
  688. /*" Generates a failure when the expression evaluates to true and in addition
  689. will generate error messages if an exception is encountered.
  690. _{expr The expression that is tested.}
  691. _{description A format string as in the printf() function. Can be nil or
  692. an empty string but must be present.}
  693. _{... A variable number of arguments to the format string. Can be absent.}
  694. "*/
  695. #define STAssertFalseNoThrow(expr, description, ...) \
  696. do { \
  697. @try {\
  698. BOOL _evaluatedExpression = (expr);\
  699. if (_evaluatedExpression) {\
  700. NSString *_expression = [NSString stringWithUTF8String: #expr];\
  701. [self failWithException:[NSException failureInCondition: _expression \
  702. isTrue: YES \
  703. inFile: [NSString stringWithUTF8String:__FILE__] \
  704. atLine: __LINE__ \
  705. withDescription: STComposeString(description, ##__VA_ARGS__)]]; \
  706. } \
  707. } \
  708. @catch (id anException) {\
  709. [self failWithException:[NSException failureInRaise:[NSString stringWithFormat: @"!(%s) ", #expr] \
  710. exception:anException \
  711. inFile:[NSString stringWithUTF8String:__FILE__] \
  712. atLine:__LINE__ \
  713. withDescription:STComposeString(description, ##__VA_ARGS__)]]; \
  714. }\
  715. } while (0)
  716. /*" Generates a failure when expression does not throw an exception.
  717. _{expression The expression that is evaluated.}
  718. _{description A format string as in the printf() function. Can be nil or
  719. an empty string but must be present.}
  720. _{... A variable number of arguments to the format string. Can be absent.
  721. "*/
  722. #define STAssertThrows(expr, description, ...) \
  723. do { \
  724. @try { \
  725. (expr);\
  726. } \
  727. @catch (id anException) { \
  728. continue; \
  729. }\
  730. [self failWithException:[NSException failureInRaise: [NSString stringWithUTF8String:#expr] \
  731. exception: nil \
  732. inFile: [NSString stringWithUTF8String:__FILE__] \
  733. atLine: __LINE__ \
  734. withDescription: STComposeString(description, ##__VA_ARGS__)]]; \
  735. } while (0)
  736. /*" Generates a failure when expression does not throw an exception of a
  737. specific class.
  738. _{expression The expression that is evaluated.}
  739. _{specificException The specified class of the exception.}
  740. _{description A format string as in the printf() function. Can be nil or
  741. an empty string but must be present.}
  742. _{... A variable number of arguments to the format string. Can be absent.}
  743. "*/
  744. #define STAssertThrowsSpecific(expr, specificException, description, ...) \
  745. do { \
  746. @try { \
  747. (expr);\
  748. } \
  749. @catch (specificException *anException) { \
  750. continue; \
  751. }\
  752. @catch (id anException) {\
  753. NSString *_descrip = STComposeString(@"(Expected exception: %@) %@", NSStringFromClass([specificException class]), description);\
  754. [self failWithException:[NSException failureInRaise: [NSString stringWithUTF8String:#expr] \
  755. exception: anException \
  756. inFile: [NSString stringWithUTF8String:__FILE__] \
  757. atLine: __LINE__ \
  758. withDescription: STComposeString(_descrip, ##__VA_ARGS__)]]; \
  759. continue; \
  760. }\
  761. NSString *_descrip = STComposeString(@"(Expected exception: %@) %@", NSStringFromClass([specificException class]), description);\
  762. [self failWithException:[NSException failureInRaise: [NSString stringWithUTF8String:#expr] \
  763. exception: nil \
  764. inFile: [NSString stringWithUTF8String:__FILE__] \
  765. atLine: __LINE__ \
  766. withDescription: STComposeString(_descrip, ##__VA_ARGS__)]]; \
  767. } while (0)
  768. /*" Generates a failure when expression does not throw an exception of a
  769. specific class with a specific name. Useful for those frameworks like
  770. AppKit or Foundation that throw generic NSException w/specific names
  771. (NSInvalidArgumentException, etc).
  772. _{expression The expression that is evaluated.}
  773. _{specificException The specified class of the exception.}
  774. _{aName The name of the specified exception.}
  775. _{description A format string as in the printf() function. Can be nil or
  776. an empty string but must be present.}
  777. _{... A variable number of arguments to the format string. Can be absent.}
  778. "*/
  779. #define STAssertThrowsSpecificNamed(expr, specificException, aName, description, ...) \
  780. do { \
  781. @try { \
  782. (expr);\
  783. } \
  784. @catch (specificException *anException) { \
  785. if ([aName isEqualToString: [anException name]]) continue; \
  786. NSString *_descrip = STComposeString(@"(Expected exception: %@ (name: %@)) %@", NSStringFromClass([specificException class]), aName, description);\
  787. [self failWithException: \
  788. [NSException failureInRaise: [NSString stringWithUTF8String:#expr] \
  789. exception: anException \
  790. inFile: [NSString stringWithUTF8String:__FILE__] \
  791. atLine: __LINE__ \
  792. withDescription: STComposeString(_descrip, ##__VA_ARGS__)]]; \
  793. continue; \
  794. }\
  795. @catch (id anException) {\
  796. NSString *_descrip = STComposeString(@"(Expected exception: %@) %@", NSStringFromClass([specificException class]), description);\
  797. [self failWithException: \
  798. [NSException failureInRaise: [NSString stringWithUTF8String:#expr] \
  799. exception: anException \
  800. inFile: [NSString stringWithUTF8String:__FILE__] \
  801. atLine: __LINE__ \
  802. withDescription: STComposeString(_descrip, ##__VA_ARGS__)]]; \
  803. continue; \
  804. }\
  805. NSString *_descrip = STComposeString(@"(Expected exception: %@) %@", NSStringFromClass([specificException class]), description);\
  806. [self failWithException: \
  807. [NSException failureInRaise: [NSString stringWithUTF8String:#expr] \
  808. exception: nil \
  809. inFile: [NSString stringWithUTF8String:__FILE__] \
  810. atLine: __LINE__ \
  811. withDescription: STComposeString(_descrip, ##__VA_ARGS__)]]; \
  812. } while (0)
  813. /*" Generates a failure when expression does throw an exception.
  814. _{expression The expression that is evaluated.}
  815. _{description A format string as in the printf() function. Can be nil or
  816. an empty string but must be present.}
  817. _{... A variable number of arguments to the format string. Can be absent.}
  818. "*/
  819. #define STAssertNoThrow(expr, description, ...) \
  820. do { \
  821. @try { \
  822. (expr);\
  823. } \
  824. @catch (id anException) { \
  825. [self failWithException:[NSException failureInRaise: [NSString stringWithUTF8String:#expr] \
  826. exception: anException \
  827. inFile: [NSString stringWithUTF8String:__FILE__] \
  828. atLine: __LINE__ \
  829. withDescription: STComposeString(description, ##__VA_ARGS__)]]; \
  830. }\
  831. } while (0)
  832. /*" Generates a failure when expression does throw an exception of the specitied
  833. class. Any other exception is okay (i.e. does not generate a failure).
  834. _{expression The expression that is evaluated.}
  835. _{specificException The specified class of the exception.}
  836. _{description A format string as in the printf() function. Can be nil or
  837. an empty string but must be present.}
  838. _{... A variable number of arguments to the format string. Can be absent.}
  839. "*/
  840. #define STAssertNoThrowSpecific(expr, specificException, description, ...) \
  841. do { \
  842. @try { \
  843. (expr);\
  844. } \
  845. @catch (specificException *anException) { \
  846. [self failWithException:[NSException failureInRaise: [NSString stringWithUTF8String:#expr] \
  847. exception: anException \
  848. inFile: [NSString stringWithUTF8String:__FILE__] \
  849. atLine: __LINE__ \
  850. withDescription: STComposeString(description, ##__VA_ARGS__)]]; \
  851. }\
  852. @catch (id anythingElse) {\
  853. ; \
  854. }\
  855. } while (0)
  856. /*" Generates a failure when expression does throw an exception of a
  857. specific class with a specific name. Useful for those frameworks like
  858. AppKit or Foundation that throw generic NSException w/specific names
  859. (NSInvalidArgumentException, etc).
  860. _{expression The expression that is evaluated.}
  861. _{specificException The specified class of the exception.}
  862. _{aName The name of the specified exception.}
  863. _{description A format string as in the printf() function. Can be nil or
  864. an empty string but must be present.}
  865. _{... A variable number of arguments to the format string. Can be absent.}
  866. "*/
  867. #define STAssertNoThrowSpecificNamed(expr, specificException, aName, description, ...) \
  868. do { \
  869. @try { \
  870. (expr);\
  871. } \
  872. @catch (specificException *anException) { \
  873. if ([aName isEqualToString: [anException name]]) { \
  874. NSString *_descrip = STComposeString(@"(Expected exception: %@ (name: %@)) %@", NSStringFromClass([specificException class]), aName, description);\
  875. [self failWithException: \
  876. [NSException failureInRaise: [NSString stringWithUTF8String:#expr] \
  877. exception: anException \
  878. inFile: [NSString stringWithUTF8String:__FILE__] \
  879. atLine: __LINE__ \
  880. withDescription: STComposeString(_descrip, ##__VA_ARGS__)]]; \
  881. } \
  882. continue; \
  883. }\
  884. @catch (id anythingElse) {\
  885. ; \
  886. }\
  887. } while (0)
  888. @interface NSException (GTMSenTestAdditions)
  889. + (NSException *)failureInFile:(NSString *)filename
  890. atLine:(int)lineNumber
  891. withDescription:(NSString *)formatString, ...;
  892. + (NSException *)failureInCondition:(NSString *)condition
  893. isTrue:(BOOL)isTrue
  894. inFile:(NSString *)filename
  895. atLine:(int)lineNumber
  896. withDescription:(NSString *)formatString, ...;
  897. + (NSException *)failureInEqualityBetweenObject:(id)left
  898. andObject:(id)right
  899. inFile:(NSString *)filename
  900. atLine:(int)lineNumber
  901. withDescription:(NSString *)formatString, ...;
  902. + (NSException *)failureInEqualityBetweenValue:(NSValue *)left
  903. andValue:(NSValue *)right
  904. withAccuracy:(NSValue *)accuracy
  905. inFile:(NSString *)filename
  906. atLine:(int) ineNumber
  907. withDescription:(NSString *)formatString, ...;
  908. + (NSException *)failureInRaise:(NSString *)expression
  909. inFile:(NSString *)filename
  910. atLine:(int)lineNumber
  911. withDescription:(NSString *)formatString, ...;
  912. + (NSException *)failureInRaise:(NSString *)expression
  913. exception:(NSException *)exception
  914. inFile:(NSString *)filename
  915. atLine:(int)lineNumber
  916. withDescription:(NSString *)formatString, ...;
  917. @end
  918. // SENTE_END
  919. @interface SenTestCase : NSObject {
  920. SEL currentSelector_;
  921. }
  922. - (void)setUp;
  923. - (void)invokeTest;
  924. - (void)tearDown;
  925. - (void)performTest:(SEL)sel;
  926. - (void)failWithException:(NSException*)exception;
  927. @end
  928. GTM_EXTERN NSString *const SenTestFailureException;
  929. GTM_EXTERN NSString *const SenTestFilenameKey;
  930. GTM_EXTERN NSString *const SenTestLineNumberKey;
  931. #endif // GTM_IPHONE_SDK
  932. // All unittest cases in GTM should inherit from GTMTestCase. It makes sure
  933. // to set up our logging system correctly to verify logging calls.
  934. // See GTMUnitTestDevLog.h for details
  935. @interface GTMTestCase : SenTestCase
  936. @end