OCObserverMockObject.m 2.9 KB

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