KWNilMatcher.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // KWBeNilMatcher.m
  3. // iOSFalconCore
  4. //
  5. // Created by Luke Redpath on 14/01/2011.
  6. // Copyright 2011 LJR Software Limited. All rights reserved.
  7. //
  8. #import "KWNilMatcher.h"
  9. #import "KWExample.h"
  10. #import "KWExampleSuiteBuilder.h"
  11. #import "KWFormatter.h"
  12. #import "KWMatchVerifier.h"
  13. #import "KWVerifying.h"
  14. @interface KWNilMatcher ()
  15. @property (nonatomic, assign) BOOL expectsNil;
  16. @end
  17. @implementation KWNilMatcher
  18. #pragma mark - Getting Matcher Strings
  19. + (NSArray *)matcherStrings {
  20. return @[@"beNil", @"beNil:", @"beNonNil", @"beNonNil:"];
  21. }
  22. #pragma mark - Matching
  23. - (BOOL)isNilMatcher {
  24. return YES;
  25. }
  26. - (BOOL)evaluate {
  27. if (self.expectsNil) {
  28. return (self.subject == nil);
  29. } else {
  30. return (self.subject != nil);
  31. }
  32. }
  33. // These two methods gets invoked by be(Non)Nil macro in case the subject is nil
  34. // (and therefore cannot have a verifier attached).
  35. + (BOOL)verifyNilSubject {
  36. return [self verifySubjectExpectingNil:YES];
  37. }
  38. + (BOOL)verifyNonNilSubject {
  39. return [self verifySubjectExpectingNil:NO];
  40. }
  41. #pragma mark Getting Failure Messages
  42. - (NSString *)failureMessageForShould {
  43. if (self.expectsNil) {
  44. return [NSString stringWithFormat:@"expected subject to be nil, got %@",
  45. [KWFormatter formatObject:self.subject]];
  46. } else {
  47. return [NSString stringWithFormat:@"expected subject not to be nil"];
  48. }
  49. }
  50. - (NSString *)failureMessageForShouldNot {
  51. if (self.expectsNil) {
  52. return [NSString stringWithFormat:@"expected subject not to be nil"];
  53. } else {
  54. return [NSString stringWithFormat:@"expected subject to be nil, got %@",
  55. [KWFormatter formatObject:self.subject]];
  56. }
  57. }
  58. - (NSString *)description {
  59. return [NSString stringWithFormat:@"be %@nil", self.expectsNil ? @"" : @"non "];
  60. }
  61. - (void)beNil {
  62. self.expectsNil = YES;
  63. }
  64. - (void)beNil:(BOOL)workaroundArgument {
  65. self.expectsNil = YES;
  66. }
  67. - (void)beNonNil {
  68. self.expectsNil = NO;
  69. }
  70. - (void)beNonNil:(BOOL)workaroundArgument {
  71. self.expectsNil = NO;
  72. }
  73. #pragma mark - Internal Methods
  74. + (BOOL)verifySubjectExpectingNil:(BOOL)expectNil {
  75. KWExample *currentExample = [[KWExampleSuiteBuilder sharedExampleSuiteBuilder] currentExample];
  76. id<KWVerifying> verifier = currentExample.unresolvedVerifier;
  77. if (verifier && ![verifier subject] && [verifier isKindOfClass:[KWMatchVerifier class]]) {
  78. KWMatchVerifier *matchVerifier = (KWMatchVerifier *)verifier;
  79. if (expectNil) {
  80. [matchVerifier performSelector:@selector(beNil)];
  81. } else {
  82. [matchVerifier performSelector:@selector(beNonNil)];
  83. }
  84. currentExample.unresolvedVerifier = nil;
  85. return NO;
  86. }
  87. return YES;
  88. }
  89. @end