KWMatcher.m 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // Licensed under the terms in License.txt
  3. //
  4. // Copyright 2010 Allen Ding. All rights reserved.
  5. //
  6. #import "KWMatcher.h"
  7. #import "KWFormatter.h"
  8. #import "KWFutureObject.h"
  9. @implementation KWMatcher
  10. #pragma mark - Initializing
  11. - (id)initWithSubject:(id)anObject {
  12. self = [super init];
  13. if (self) {
  14. _subject = anObject;
  15. }
  16. return self;
  17. }
  18. + (id)matcherWithSubject:(id)anObject {
  19. return [[self alloc] initWithSubject:anObject];
  20. }
  21. #pragma mark - Properties
  22. - (id)subject
  23. {
  24. if ([_subject isKindOfClass:[KWFutureObject class]]) {
  25. return [(KWFutureObject *)_subject object];
  26. }
  27. return _subject;
  28. }
  29. #pragma mark - Getting Matcher Strings
  30. + (NSArray *)matcherStrings {
  31. return nil;
  32. }
  33. #pragma mark - Getting Matcher Compatability
  34. + (BOOL)canMatchSubject:(id)anObject {
  35. return YES;
  36. }
  37. #pragma mark - Matching
  38. - (BOOL)evaluate {
  39. [NSException raise:NSInternalInconsistencyException format:@"%@ must override -evaluate",
  40. [KWFormatter formatObject:[self class]]];
  41. return NO;
  42. }
  43. #pragma mark - Getting Failure Messages
  44. - (NSString *)failureMessageForShould {
  45. return @"subject did not meet expectation";
  46. }
  47. - (NSString *)failureMessageForShouldNot {
  48. NSString *failureMessageForShould = [self failureMessageForShould];
  49. NSRange markerRange = [failureMessageForShould rangeOfString:@" to "];
  50. if (markerRange.location == NSNotFound)
  51. return @"subject did not meet expectation";
  52. NSRange replacementRange = NSMakeRange(0, markerRange.location + markerRange.length);
  53. NSString *message = [failureMessageForShould stringByReplacingOccurrencesOfString:@" to "
  54. withString:@" not to "
  55. options:0
  56. range:replacementRange];
  57. return message;
  58. }
  59. @end