KWBlockRaiseMatcher.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // Licensed under the terms in License.txt
  3. //
  4. // Copyright 2010 Allen Ding. All rights reserved.
  5. //
  6. #import "KWBlockRaiseMatcher.h"
  7. #import "KWBlock.h"
  8. @interface KWBlockRaiseMatcher()
  9. @property (nonatomic, readwrite, strong) NSException *exception;
  10. @property (nonatomic, readwrite, strong) NSException *actualException;
  11. @end
  12. @implementation KWBlockRaiseMatcher
  13. #pragma mark - Getting Matcher Strings
  14. + (NSArray *)matcherStrings {
  15. return @[@"raise",
  16. @"raiseWithName:",
  17. @"raiseWithReason:",
  18. @"raiseWithName:reason:"];
  19. }
  20. #pragma mark - Matching
  21. - (BOOL)evaluate {
  22. if (![self.subject isKindOfClass:[KWBlock class]])
  23. [NSException raise:@"KWMatcherException" format:@"subject must be a KWBlock"];
  24. @try {
  25. [self.subject call];
  26. } @catch (NSException *anException) {
  27. self.actualException = anException;
  28. if ([self.exception name] != nil && ![[self.exception name] isEqualToString:[anException name]])
  29. return NO;
  30. if ([self.exception reason] != nil && ![[self.exception reason] isEqualToString:[anException reason]])
  31. return NO;
  32. return YES;
  33. }
  34. return NO;
  35. }
  36. #pragma mark - Getting Failure Messages
  37. + (NSString *)exceptionPhraseWithException:(NSException *)anException {
  38. if (anException == nil)
  39. return @"nothing";
  40. NSString *namePhrase = nil;
  41. if ([anException name] == nil)
  42. namePhrase = @"exception";
  43. else
  44. namePhrase = [anException name];
  45. if ([anException reason] == nil)
  46. return namePhrase;
  47. return [NSString stringWithFormat:@"%@ \"%@\"", namePhrase, [anException reason]];
  48. }
  49. - (NSString *)failureMessageForShould {
  50. return [NSString stringWithFormat:@"expected %@, but %@ raised",
  51. [[self class] exceptionPhraseWithException:self.exception],
  52. [[self class] exceptionPhraseWithException:self.actualException]];
  53. }
  54. - (NSString *)failureMessageForShouldNot {
  55. return [NSString stringWithFormat:@"expected %@ not to be raised",
  56. [[self class] exceptionPhraseWithException:self.actualException]];
  57. }
  58. #pragma mark - Configuring Matchers
  59. - (void)raise {
  60. [self raiseWithName:nil reason:nil];
  61. }
  62. - (void)raiseWithName:(NSString *)aName {
  63. [self raiseWithName:aName reason:nil];
  64. }
  65. - (void)raiseWithReason:(NSString *)aReason {
  66. [self raiseWithName:nil reason:aReason];
  67. }
  68. - (void)raiseWithName:(NSString *)aName reason:(NSString *)aReason {
  69. self.exception = [NSException exceptionWithName:aName reason:aReason userInfo:nil];
  70. }
  71. - (NSString *)description {
  72. return [NSString stringWithFormat:@"raise %@", [[self class] exceptionPhraseWithException:self.exception]];
  73. }
  74. @end