OLD | NEW |
1 //------------------------------------------------------------------------------
--------- | 1 /* |
2 // $Id$ | 2 * Copyright (c) 2009-2015 Erik Doernenburg and contributors |
3 // Copyright (c) 2009 by Mulle Kybernetik. See License file for details. | 3 * |
4 //------------------------------------------------------------------------------
--------- | 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 * not use these files except in compliance with the License. You may obtain |
| 6 * a copy of the License at |
| 7 * |
| 8 * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 * |
| 10 * Unless required by applicable law or agreed to in writing, software |
| 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 * License for the specific language governing permissions and limitations |
| 14 * under the License. |
| 15 */ |
5 | 16 |
6 #import "OCObserverMockObject.h" | 17 #import "OCObserverMockObject.h" |
7 #import "OCMObserverRecorder.h" | 18 #import "OCMObserverRecorder.h" |
| 19 #import "OCMLocation.h" |
| 20 #import "OCMFunctionsPrivate.h" |
8 | 21 |
9 | 22 |
10 @implementation OCObserverMockObject | 23 @implementation OCObserverMockObject |
11 | 24 |
12 #pragma mark Initialisers, description, accessors, etc. | 25 #pragma mark Initialisers, description, accessors, etc. |
13 | 26 |
14 - (id)init | 27 - (id)init |
15 { | 28 { |
16 » self = [super init]; | 29 if ((self = [super init])) |
17 » recorders = [[NSMutableArray alloc] init]; | 30 { |
| 31 recorders = [[NSMutableArray alloc] init]; |
| 32 centers = [[NSMutableArray alloc] init]; |
| 33 } |
| 34 » |
18 return self; | 35 return self; |
19 } | 36 } |
20 | 37 |
| 38 - (id)retain |
| 39 { |
| 40 return [super retain]; |
| 41 } |
| 42 |
21 - (void)dealloc | 43 - (void)dealloc |
22 { | 44 { |
| 45 for(NSNotificationCenter *c in centers) |
| 46 [c removeObserver:self]; |
| 47 [centers release]; |
23 [recorders release]; | 48 [recorders release]; |
24 [super dealloc]; | 49 [super dealloc]; |
25 } | 50 } |
26 | 51 |
27 - (NSString *)description | 52 - (NSString *)description |
28 { | 53 { |
29 return @"OCMockObserver"; | 54 return @"OCMockObserver"; |
30 } | 55 } |
31 | 56 |
32 - (void)setExpectationOrderMatters:(BOOL)flag | 57 - (void)setExpectationOrderMatters:(BOOL)flag |
33 { | 58 { |
34 expectationOrderMatters = flag; | 59 expectationOrderMatters = flag; |
35 } | 60 } |
36 | 61 |
| 62 - (void)autoRemoveFromCenter:(NSNotificationCenter *)aCenter |
| 63 { |
| 64 [centers addObject:aCenter]; |
| 65 } |
| 66 |
37 | 67 |
38 #pragma mark Public API | 68 #pragma mark Public API |
39 | 69 |
40 - (id)expect | 70 - (id)expect |
41 { | 71 { |
42 OCMObserverRecorder *recorder = [[[OCMObserverRecorder alloc] init] auto
release]; | 72 OCMObserverRecorder *recorder = [[[OCMObserverRecorder alloc] init] auto
release]; |
43 [recorders addObject:recorder]; | 73 [recorders addObject:recorder]; |
44 return recorder; | 74 return recorder; |
45 } | 75 } |
46 | 76 |
47 - (void)verify | 77 - (void)verify |
48 { | 78 { |
49 » if([recorders count] == 1) | 79 [self verifyAtLocation:nil]; |
50 » { | |
51 » » [NSException raise:NSInternalInconsistencyException format:@"%@:
expected notification was not observed: %@", | |
52 » » [self description], [[recorders lastObject] description]]; | |
53 » } | |
54 » if([recorders count] > 0) | |
55 » { | |
56 » » [NSException raise:NSInternalInconsistencyException format:@"%@
: %ld expected notifications were not observed.", | |
57 » » [self description], (unsigned long)[recorders count]]; | |
58 » } | |
59 } | 80 } |
60 | 81 |
| 82 - (void)verifyAtLocation:(OCMLocation *)location |
| 83 { |
| 84 if([recorders count] == 1) |
| 85 { |
| 86 NSString *description = [NSString stringWithFormat:@"%@: expected notifi
cation was not observed: %@", |
| 87 [self description], [[recorders lastObject] description]]; |
| 88 OCMReportFailure(location, description); |
| 89 } |
| 90 else if([recorders count] > 0) |
| 91 { |
| 92 NSString *description = [NSString stringWithFormat:@"%@ : %@ expected no
tifications were not observed.", |
| 93 [self description], @([recorders count])]; |
| 94 OCMReportFailure(location, description); |
| 95 } |
| 96 } |
| 97 |
| 98 |
| 99 #pragma mark Receiving recording requests via macro |
| 100 |
| 101 - (void)notificationWithName:(NSString *)name object:(id)sender |
| 102 { |
| 103 [[self expect] notificationWithName:name object:sender]; |
| 104 } |
61 | 105 |
62 | 106 |
63 #pragma mark Receiving notifications | 107 #pragma mark Receiving notifications |
64 | 108 |
65 - (void)handleNotification:(NSNotification *)aNotification | 109 - (void)handleNotification:(NSNotification *)aNotification |
66 { | 110 { |
67 NSUInteger i, limit; | 111 NSUInteger i, limit; |
68 | 112 |
69 limit = expectationOrderMatters ? 1 : [recorders count]; | 113 limit = expectationOrderMatters ? 1 : [recorders count]; |
70 for(i = 0; i < limit; i++) | 114 for(i = 0; i < limit; i++) |
71 { | 115 { |
72 if([[recorders objectAtIndex:i] matchesNotification:aNotificatio
n]) | 116 if([[recorders objectAtIndex:i] matchesNotification:aNotificatio
n]) |
73 { | 117 { |
74 [recorders removeObjectAtIndex:i]; | 118 [recorders removeObjectAtIndex:i]; |
75 return; | 119 return; |
76 } | 120 } |
77 } | 121 } |
78 [NSException raise:NSInternalInconsistencyException format:@"%@: unexpec
ted notification observed: %@", [self description], | 122 [NSException raise:NSInternalInconsistencyException format:@"%@: unexpec
ted notification observed: %@", [self description], |
79 [aNotification description]]; | 123 [aNotification description]]; |
80 } | 124 } |
81 | 125 |
82 | 126 |
83 @end | 127 @end |
OLD | NEW |