OCObserverMockObject.m revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
1//---------------------------------------------------------------------------------------
2//  $Id$
3//  Copyright (c) 2009 by Mulle Kybernetik. See License file for details.
4//---------------------------------------------------------------------------------------
5
6#import "OCObserverMockObject.h"
7#import "OCMObserverRecorder.h"
8
9
10@implementation OCObserverMockObject
11
12#pragma mark  Initialisers, description, accessors, etc.
13
14- (id)init
15{
16	self = [super init];
17	recorders = [[NSMutableArray alloc] init];
18	return self;
19}
20
21- (void)dealloc
22{
23	[recorders release];
24	[super dealloc];
25}
26
27- (NSString *)description
28{
29	return @"OCMockObserver";
30}
31
32- (void)setExpectationOrderMatters:(BOOL)flag
33{
34    expectationOrderMatters = flag;
35}
36
37
38#pragma mark  Public API
39
40- (id)expect
41{
42	OCMObserverRecorder *recorder = [[[OCMObserverRecorder alloc] init] autorelease];
43	[recorders addObject:recorder];
44	return recorder;
45}
46
47- (void)verify
48{
49	if([recorders count] == 1)
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}
60
61
62
63#pragma mark  Receiving notifications
64
65- (void)handleNotification:(NSNotification *)aNotification
66{
67	NSUInteger i, limit;
68	
69	limit = expectationOrderMatters ? 1 : [recorders count];
70	for(i = 0; i < limit; i++)
71	{
72		if([[recorders objectAtIndex:i] matchesNotification:aNotification])
73		{
74			[recorders removeObjectAtIndex:i];
75			return;
76		}
77	}
78	[NSException raise:NSInternalInconsistencyException format:@"%@: unexpected notification observed: %@", [self description], 
79	  [aNotification description]];
80}
81
82
83@end
84