| 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" |
| 8 | 19 #import "OCMLocation.h" |
| 20 #import "OCMFunctions.h" |
| 9 | 21 |
| 10 @implementation OCObserverMockObject | 22 @implementation OCObserverMockObject |
| 11 | 23 |
| 12 #pragma mark Initialisers, description, accessors, etc. | 24 #pragma mark Initialisers, description, accessors, etc. |
| 13 | 25 |
| 14 - (id)init | 26 - (id)init |
| 15 { | 27 { |
| 16 » self = [super init]; | 28 if ((self = [super init])) { |
| 17 » recorders = [[NSMutableArray alloc] init]; | 29 recorders = [[NSMutableArray alloc] init]; |
| 18 » return self; | 30 centers = [[NSMutableArray alloc] init]; |
| 31 } |
| 32 |
| 33 return self; |
| 34 } |
| 35 |
| 36 - (id)retain { |
| 37 return [super retain]; |
| 19 } | 38 } |
| 20 | 39 |
| 21 - (void)dealloc | 40 - (void)dealloc |
| 22 { | 41 { |
| 23 » [recorders release]; | 42 for (NSNotificationCenter* c in centers) |
| 24 » [super dealloc]; | 43 [c removeObserver:self]; |
| 44 [centers release]; |
| 45 [recorders release]; |
| 46 [super dealloc]; |
| 25 } | 47 } |
| 26 | 48 |
| 27 - (NSString *)description | 49 - (NSString *)description |
| 28 { | 50 { |
| 29 return @"OCMockObserver"; | 51 return @"OCMockObserver"; |
| 30 } | 52 } |
| 31 | 53 |
| 32 - (void)setExpectationOrderMatters:(BOOL)flag | 54 - (void)setExpectationOrderMatters:(BOOL)flag |
| 33 { | 55 { |
| 34 expectationOrderMatters = flag; | 56 expectationOrderMatters = flag; |
| 35 } | 57 } |
| 36 | 58 |
| 59 - (void)autoRemoveFromCenter:(NSNotificationCenter*)aCenter { |
| 60 [centers addObject:aCenter]; |
| 61 } |
| 37 | 62 |
| 38 #pragma mark Public API | 63 #pragma mark Public API |
| 39 | 64 |
| 40 - (id)expect | 65 - (id)expect |
| 41 { | 66 { |
| 42 OCMObserverRecorder *recorder = [[[OCMObserverRecorder alloc] init] auto
release]; | 67 OCMObserverRecorder *recorder = [[[OCMObserverRecorder alloc] init] auto
release]; |
| 43 [recorders addObject:recorder]; | 68 [recorders addObject:recorder]; |
| 44 return recorder; | 69 return recorder; |
| 45 } | 70 } |
| 46 | 71 |
| 47 - (void)verify | 72 - (void)verify |
| 48 { | 73 { |
| 49 » if([recorders count] == 1) | 74 [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 } | 75 } |
| 60 | 76 |
| 77 - (void)verifyAtLocation:(OCMLocation*)location { |
| 78 if ([recorders count] == 1) { |
| 79 NSString* description = [NSString |
| 80 stringWithFormat:@"%@: expected notification was not observed: %@", |
| 81 [self description], |
| 82 [[recorders lastObject] description]]; |
| 83 OCMReportFailure(location, description); |
| 84 } else if ([recorders count] > 0) { |
| 85 NSString* description = [NSString |
| 86 stringWithFormat:@"%@ : %@ expected notifications were not observed.", |
| 87 [self description], @([recorders count])]; |
| 88 OCMReportFailure(location, description); |
| 89 } |
| 90 } |
| 61 | 91 |
| 92 #pragma mark Receiving recording requests via macro |
| 93 |
| 94 - (void)notificationWithName:(NSString*)name object:(id)sender { |
| 95 [[self expect] notificationWithName:name object:sender]; |
| 96 } |
| 62 | 97 |
| 63 #pragma mark Receiving notifications | 98 #pragma mark Receiving notifications |
| 64 | 99 |
| 65 - (void)handleNotification:(NSNotification *)aNotification | 100 - (void)handleNotification:(NSNotification *)aNotification |
| 66 { | 101 { |
| 67 NSUInteger i, limit; | 102 NSUInteger i, limit; |
| 68 | 103 |
| 69 limit = expectationOrderMatters ? 1 : [recorders count]; | 104 limit = expectationOrderMatters ? 1 : [recorders count]; |
| 70 for(i = 0; i < limit; i++) | 105 for(i = 0; i < limit; i++) |
| 71 { | 106 { |
| 72 if([[recorders objectAtIndex:i] matchesNotification:aNotificatio
n]) | 107 if([[recorders objectAtIndex:i] matchesNotification:aNotificatio
n]) |
| 73 { | 108 { |
| 74 [recorders removeObjectAtIndex:i]; | 109 [recorders removeObjectAtIndex:i]; |
| 75 return; | 110 return; |
| 76 } | 111 } |
| 77 } | 112 } |
| 78 [NSException raise:NSInternalInconsistencyException format:@"%@: unexpec
ted notification observed: %@", [self description], | 113 [NSException raise:NSInternalInconsistencyException format:@"%@: unexpec
ted notification observed: %@", [self description], |
| 79 [aNotification description]]; | 114 [aNotification description]]; |
| 80 } | 115 } |
| 81 | 116 |
| 82 | 117 |
| 83 @end | 118 @end |
| OLD | NEW |