KWHaveValueMatcher.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // KWHaveValueMatcher.m
  3. // Kiwi
  4. //
  5. // Created by Luke Redpath on 24/01/2011.
  6. // Copyright 2011 Allen Ding. All rights reserved.
  7. //
  8. #import "KWHaveValueMatcher.h"
  9. #import "KWGenericMatchingAdditions.h"
  10. #import "KWGenericMatcher.h"
  11. #import "KWFormatter.h"
  12. @interface KWHaveValueMatcher()
  13. @property (nonatomic, strong) NSString *expectedKey;
  14. @property (nonatomic, strong) NSString *expectedKeyPath;
  15. @property (nonatomic, strong) id expectedValue;
  16. @end
  17. @implementation KWHaveValueMatcher
  18. #pragma mark - Getting Matcher Strings
  19. + (NSArray *)matcherStrings {
  20. return @[@"haveValue:forKey:",
  21. @"haveValueForKey:",
  22. @"haveValue:forKeyPath:",
  23. @"haveValueForKeyPath:"];
  24. }
  25. #pragma mark - Matching
  26. - (BOOL)evaluate {
  27. BOOL matched = NO;
  28. @try {
  29. id value = [self subjectValue];
  30. if (value) {
  31. matched = YES;
  32. if (self.expectedValue) {
  33. KWGenericMatcher *matcher = [KWGenericMatcher matcherWithSubject:value];
  34. [matcher match:self.expectedValue];
  35. matched = [matcher evaluate];
  36. }
  37. }
  38. }
  39. @catch (NSException * e) {} // catch KVO non-existent key errors
  40. return matched;
  41. }
  42. - (NSString *)failureMessageForShould {
  43. if (self.expectedValue == nil) {
  44. return [NSString stringWithFormat:@"expected subject to have a value for key %@",
  45. [KWFormatter formatObject:self.expectedKey]];
  46. }
  47. id subjectValue = [self subjectValue];
  48. if (subjectValue) {
  49. return [NSString stringWithFormat:@"expected subject to have value %@ for key %@, but it had value %@ instead",
  50. [KWFormatter formatObject:self.expectedValue],
  51. [KWFormatter formatObject:self.expectedKey],
  52. [KWFormatter formatObject:subjectValue]];
  53. } else {
  54. return [NSString stringWithFormat:@"expected subject to have value %@ for key %@, but the key was not present",
  55. [KWFormatter formatObject:self.expectedValue],
  56. [KWFormatter formatObject:self.expectedKey]];
  57. }
  58. }
  59. - (id)subjectValue {
  60. id value = nil;
  61. if (self.expectedKey) {
  62. value = [self.subject valueForKey:self.expectedKey];
  63. } else
  64. if (self.expectedKeyPath) {
  65. value = [self.subject valueForKeyPath:self.expectedKeyPath];
  66. }
  67. return value;
  68. }
  69. - (NSString *)description {
  70. NSString *keyDescription = nil;
  71. if (self.expectedKey) {
  72. keyDescription = [NSString stringWithFormat:@"key %@", [KWFormatter formatObject:self.expectedKey]];
  73. }
  74. else {
  75. keyDescription = [NSString stringWithFormat:@"keypath %@", [KWFormatter formatObject:self.expectedKeyPath]];
  76. }
  77. NSString *valueDescription = nil;
  78. if (self.expectedValue) {
  79. valueDescription = [NSString stringWithFormat:@"value %@", [KWFormatter formatObject:self.expectedValue]];
  80. }
  81. else {
  82. valueDescription = @"value";
  83. }
  84. return [NSString stringWithFormat:@"have %@ for %@", valueDescription, keyDescription];
  85. }
  86. #pragma mark - Configuring Matchers
  87. - (void)haveValue:(id)value forKey:(NSString *)key {
  88. self.expectedKey = key;
  89. self.expectedValue = value;
  90. }
  91. - (void)haveValue:(id)value forKeyPath:(NSString *)key {
  92. self.expectedKeyPath = key;
  93. self.expectedValue = value;
  94. }
  95. - (void)haveValueForKey:(NSString *)key {
  96. self.expectedKey = key;
  97. self.expectedValue = nil;
  98. }
  99. - (void)haveValueForKeyPath:(NSString *)keyPath {
  100. self.expectedKeyPath = keyPath;
  101. self.expectedValue = nil;
  102. }
  103. @end