nevans-iphone_testify 0.1.14

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,366 @@
1
+ //
2
+ // GTMSenTestCase.m
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
+
19
+ #import "GTMSenTestCase.h"
20
+ #import <unistd.h>
21
+
22
+ #if !GTM_IPHONE_SDK
23
+ #import "GTMGarbageCollection.h"
24
+ #endif // !GTM_IPHONE_SDK
25
+
26
+ #if GTM_IPHONE_SDK
27
+ #import <stdarg.h>
28
+
29
+ @interface NSException (GTMSenTestPrivateAdditions)
30
+ + (NSException *)failureInFile:(NSString *)filename
31
+ atLine:(int)lineNumber
32
+ reason:(NSString *)reason;
33
+ @end
34
+
35
+ @implementation NSException (GTMSenTestPrivateAdditions)
36
+ + (NSException *)failureInFile:(NSString *)filename
37
+ atLine:(int)lineNumber
38
+ reason:(NSString *)reason {
39
+ NSDictionary *userInfo =
40
+ [NSDictionary dictionaryWithObjectsAndKeys:
41
+ [NSNumber numberWithInteger:lineNumber], SenTestLineNumberKey,
42
+ filename, SenTestFilenameKey,
43
+ nil];
44
+
45
+ return [self exceptionWithName:SenTestFailureException
46
+ reason:reason
47
+ userInfo:userInfo];
48
+ }
49
+ @end
50
+
51
+ @implementation NSException (GTMSenTestAdditions)
52
+
53
+ + (NSException *)failureInFile:(NSString *)filename
54
+ atLine:(int)lineNumber
55
+ withDescription:(NSString *)formatString, ... {
56
+
57
+ NSString *testDescription = @"";
58
+ if (formatString) {
59
+ va_list vl;
60
+ va_start(vl, formatString);
61
+ testDescription =
62
+ [[[NSString alloc] initWithFormat:formatString arguments:vl] autorelease];
63
+ va_end(vl);
64
+ }
65
+
66
+ NSString *reason = testDescription;
67
+
68
+ return [self failureInFile:filename atLine:lineNumber reason:reason];
69
+ }
70
+
71
+ + (NSException *)failureInCondition:(NSString *)condition
72
+ isTrue:(BOOL)isTrue
73
+ inFile:(NSString *)filename
74
+ atLine:(int)lineNumber
75
+ withDescription:(NSString *)formatString, ... {
76
+
77
+ NSString *testDescription = @"";
78
+ if (formatString) {
79
+ va_list vl;
80
+ va_start(vl, formatString);
81
+ testDescription =
82
+ [[[NSString alloc] initWithFormat:formatString arguments:vl] autorelease];
83
+ va_end(vl);
84
+ }
85
+
86
+ NSString *reason = [NSString stringWithFormat:@"'%@' should be %s. %@",
87
+ condition, isTrue ? "TRUE" : "FALSE", testDescription];
88
+
89
+ return [self failureInFile:filename atLine:lineNumber reason:reason];
90
+ }
91
+
92
+ + (NSException *)failureInEqualityBetweenObject:(id)left
93
+ andObject:(id)right
94
+ inFile:(NSString *)filename
95
+ atLine:(int)lineNumber
96
+ withDescription:(NSString *)formatString, ... {
97
+
98
+ NSString *testDescription = @"";
99
+ if (formatString) {
100
+ va_list vl;
101
+ va_start(vl, formatString);
102
+ testDescription =
103
+ [[[NSString alloc] initWithFormat:formatString arguments:vl] autorelease];
104
+ va_end(vl);
105
+ }
106
+
107
+ NSString *reason =
108
+ [NSString stringWithFormat:@"'%@' should be equal to '%@'. %@",
109
+ [left description], [right description], testDescription];
110
+
111
+ return [self failureInFile:filename atLine:lineNumber reason:reason];
112
+ }
113
+
114
+ + (NSException *)failureInEqualityBetweenValue:(NSValue *)left
115
+ andValue:(NSValue *)right
116
+ withAccuracy:(NSValue *)accuracy
117
+ inFile:(NSString *)filename
118
+ atLine:(int)lineNumber
119
+ withDescription:(NSString *)formatString, ... {
120
+
121
+ NSString *testDescription = @"";
122
+ if (formatString) {
123
+ va_list vl;
124
+ va_start(vl, formatString);
125
+ testDescription =
126
+ [[[NSString alloc] initWithFormat:formatString arguments:vl] autorelease];
127
+ va_end(vl);
128
+ }
129
+
130
+ NSString *reason;
131
+ if (accuracy) {
132
+ reason =
133
+ [NSString stringWithFormat:@"'%@' should be equal to '%@'. %@",
134
+ left, right, testDescription];
135
+ } else {
136
+ reason =
137
+ [NSString stringWithFormat:@"'%@' should be equal to '%@' +/-'%@'. %@",
138
+ left, right, accuracy, testDescription];
139
+ }
140
+
141
+ return [self failureInFile:filename atLine:lineNumber reason:reason];
142
+ }
143
+
144
+ + (NSException *)failureInRaise:(NSString *)expression
145
+ inFile:(NSString *)filename
146
+ atLine:(int)lineNumber
147
+ withDescription:(NSString *)formatString, ... {
148
+
149
+ NSString *testDescription = @"";
150
+ if (formatString) {
151
+ va_list vl;
152
+ va_start(vl, formatString);
153
+ testDescription =
154
+ [[[NSString alloc] initWithFormat:formatString arguments:vl] autorelease];
155
+ va_end(vl);
156
+ }
157
+
158
+ NSString *reason = [NSString stringWithFormat:@"'%@' should raise. %@",
159
+ expression, testDescription];
160
+
161
+ return [self failureInFile:filename atLine:lineNumber reason:reason];
162
+ }
163
+
164
+ + (NSException *)failureInRaise:(NSString *)expression
165
+ exception:(NSException *)exception
166
+ inFile:(NSString *)filename
167
+ atLine:(int)lineNumber
168
+ withDescription:(NSString *)formatString, ... {
169
+
170
+ NSString *testDescription = @"";
171
+ if (formatString) {
172
+ va_list vl;
173
+ va_start(vl, formatString);
174
+ testDescription =
175
+ [[[NSString alloc] initWithFormat:formatString arguments:vl] autorelease];
176
+ va_end(vl);
177
+ }
178
+
179
+ NSString *reason;
180
+ if ([[exception name] isEqualToString:SenTestFailureException]) {
181
+ // it's our exception, assume it has the right description on it.
182
+ reason = [exception reason];
183
+ } else {
184
+ // not one of our exception, use the exceptions reason and our description
185
+ reason = [NSString stringWithFormat:@"'%@' raised '%@'. %@",
186
+ expression, [exception reason], testDescription];
187
+ }
188
+
189
+ return [self failureInFile:filename atLine:lineNumber reason:reason];
190
+ }
191
+
192
+ @end
193
+
194
+ NSString *STComposeString(NSString *formatString, ...) {
195
+ NSString *reason = @"";
196
+ if (formatString) {
197
+ va_list vl;
198
+ va_start(vl, formatString);
199
+ reason =
200
+ [[[NSString alloc] initWithFormat:formatString arguments:vl] autorelease];
201
+ va_end(vl);
202
+ }
203
+ return reason;
204
+ }
205
+
206
+ NSString *const SenTestFailureException = @"SenTestFailureException";
207
+ NSString *const SenTestFilenameKey = @"SenTestFilenameKey";
208
+ NSString *const SenTestLineNumberKey = @"SenTestLineNumberKey";
209
+
210
+ @interface SenTestCase (SenTestCasePrivate)
211
+ // our method of logging errors
212
+ + (void)printException:(NSException *)exception fromTestName:(NSString *)name;
213
+ @end
214
+
215
+ @implementation SenTestCase
216
+ - (void)failWithException:(NSException*)exception {
217
+ [exception raise];
218
+ }
219
+
220
+ - (void)setUp {
221
+ }
222
+
223
+ - (void)performTest:(SEL)sel {
224
+ currentSelector_ = sel;
225
+ @try {
226
+ [self invokeTest];
227
+ } @catch (NSException *exception) {
228
+ [[self class] printException:exception
229
+ fromTestName:NSStringFromSelector(sel)];
230
+ [exception raise];
231
+ }
232
+ }
233
+
234
+ + (void)printException:(NSException *)exception fromTestName:(NSString *)name {
235
+ NSDictionary *userInfo = [exception userInfo];
236
+ NSString *filename = [userInfo objectForKey:SenTestFilenameKey];
237
+ NSNumber *lineNumber = [userInfo objectForKey:SenTestLineNumberKey];
238
+ NSString *className = NSStringFromClass([self class]);
239
+ if ([filename length] == 0) {
240
+ filename = @"Unknown.m";
241
+ }
242
+ fprintf(stderr, "%s:%ld: error: -[%s %s] : %s\n",
243
+ [filename UTF8String],
244
+ (long)[lineNumber integerValue],
245
+ [className UTF8String],
246
+ [name UTF8String],
247
+ [[exception reason] UTF8String]);
248
+ fflush(stderr);
249
+ }
250
+
251
+ - (void)invokeTest {
252
+ NSException *e = nil;
253
+ @try {
254
+ // Wrap things in autorelease pools because they may
255
+ // have an STMacro in their dealloc which may get called
256
+ // when the pool is cleaned up
257
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
258
+ // We don't log exceptions here, instead we let the person that called
259
+ // this log the exception. This ensures they are only logged once but the
260
+ // outer layers get the exceptions to report counts, etc.
261
+ @try {
262
+ [self setUp];
263
+ @try {
264
+ [self performSelector:currentSelector_];
265
+ } @catch (NSException *exception) {
266
+ e = [exception retain];
267
+ }
268
+ [self tearDown];
269
+ } @catch (NSException *exception) {
270
+ e = [exception retain];
271
+ }
272
+ [pool release];
273
+ } @catch (NSException *exception) {
274
+ e = [exception retain];
275
+ }
276
+ if (e) {
277
+ [e autorelease];
278
+ [e raise];
279
+ }
280
+ }
281
+
282
+ - (void)tearDown {
283
+ }
284
+
285
+ - (NSString *)description {
286
+ // This matches the description OCUnit would return to you
287
+ return [NSString stringWithFormat:@"-[%@ %@]", [self class],
288
+ NSStringFromSelector(currentSelector_)];
289
+ }
290
+ @end
291
+
292
+ #endif // GTM_IPHONE_SDK
293
+
294
+ @implementation GTMTestCase : SenTestCase
295
+ - (void)invokeTest {
296
+ Class devLogClass = NSClassFromString(@"GTMUnitTestDevLog");
297
+ if (devLogClass) {
298
+ [devLogClass performSelector:@selector(enableTracking)];
299
+ [devLogClass performSelector:@selector(verifyNoMoreLogsExpected)];
300
+
301
+ }
302
+ [super invokeTest];
303
+ if (devLogClass) {
304
+ [devLogClass performSelector:@selector(verifyNoMoreLogsExpected)];
305
+ [devLogClass performSelector:@selector(disableTracking)];
306
+ }
307
+ }
308
+ @end
309
+
310
+ // Leak detection
311
+ #if !GTM_IPHONE_DEVICE
312
+ // Don't want to get leaks on the iPhone Device as the device doesn't
313
+ // have 'leaks'. The simulator does though.
314
+
315
+ // COV_NF_START
316
+ // We don't have leak checking on by default, so this won't be hit.
317
+ static void _GTMRunLeaks(void) {
318
+ // This is an atexit handler. It runs leaks for us to check if we are
319
+ // leaking anything in our tests.
320
+ const char* cExclusionsEnv = getenv("GTM_LEAKS_SYMBOLS_TO_IGNORE");
321
+ NSMutableString *exclusions = [NSMutableString string];
322
+ if (cExclusionsEnv) {
323
+ NSString *exclusionsEnv = [NSString stringWithUTF8String:cExclusionsEnv];
324
+ NSArray *exclusionsArray = [exclusionsEnv componentsSeparatedByString:@","];
325
+ NSString *exclusion;
326
+ NSCharacterSet *wcSet = [NSCharacterSet whitespaceCharacterSet];
327
+ GTM_FOREACH_OBJECT(exclusion, exclusionsArray) {
328
+ exclusion = [exclusion stringByTrimmingCharactersInSet:wcSet];
329
+ [exclusions appendFormat:@"-exclude \"%@\" ", exclusion];
330
+ }
331
+ }
332
+ NSString *string
333
+ = [NSString stringWithFormat:@"/usr/bin/leaks %@%d"
334
+ @"| /usr/bin/sed -e 's/Leak: /Leaks:0: warning: Leak /'",
335
+ exclusions, getpid()];
336
+ int ret = system([string UTF8String]);
337
+ if (ret) {
338
+ fprintf(stderr, "%s:%d: Error: Unable to run leaks. 'system' returned: %d",
339
+ __FILE__, __LINE__, ret);
340
+ fflush(stderr);
341
+ }
342
+ }
343
+ // COV_NF_END
344
+
345
+ static __attribute__((constructor)) void _GTMInstallLeaks(void) {
346
+ BOOL checkLeaks = YES;
347
+ #if !GTM_IPHONE_SDK
348
+ checkLeaks = GTMIsGarbageCollectionEnabled() ? NO : YES;
349
+ #endif // !GTM_IPHONE_SDK
350
+ if (checkLeaks) {
351
+ checkLeaks = getenv("GTM_ENABLE_LEAKS") ? YES : NO;
352
+ if (checkLeaks) {
353
+ // COV_NF_START
354
+ // We don't have leak checking on by default, so this won't be hit.
355
+ fprintf(stderr, "Leak Checking Enabled\n");
356
+ fflush(stderr);
357
+ int ret = atexit(&_GTMRunLeaks);
358
+ _GTMDevAssert(ret == 0,
359
+ @"Unable to install _GTMRunLeaks as an atexit handler (%d)",
360
+ errno);
361
+ // COV_NF_END
362
+ }
363
+ }
364
+ }
365
+
366
+ #endif // !GTM_IPHONE_DEVICE
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2007 Google Inc.
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.