main
 1//
 2//  KWChangeMatcher.m
 3//  Kiwi
 4//
 5//  Copyright (c) 2013 Eloy Durรกn <eloy.de.enige@gmail.com>.
 6//  All rights reserved.
 7//
 8
 9#import "KWChangeMatcher.h"
10#import "KWBlock.h"
11
12@interface KWChangeMatcher ()
13@property (nonatomic, copy) KWChangeMatcherCountBlock countBlock;
14@property (nonatomic, assign) BOOL anyChange;
15@property (nonatomic, assign) NSInteger expectedDifference, expectedTotal, actualTotal;
16@end
17
18@implementation KWChangeMatcher
19
20@synthesize countBlock = _countBlock;
21@synthesize anyChange = _anyChange;
22@synthesize expectedDifference = _expectedDifference;
23@synthesize expectedTotal = _expectedTotal;
24@synthesize actualTotal = _actualTotal;
25
26- (void)dealloc {
27    Block_release(_countBlock);
28    [super dealloc];
29}
30
31+ (NSArray *)matcherStrings {
32    return @[@"change:by:", @"change:"];
33}
34
35- (NSString *)failureMessageForShould {
36    if (self.anyChange) {
37        return @"expected subject to change the count";
38    } else {
39        return [NSString stringWithFormat:@"expected subject to change the count to %d, got %d", (int)self.expectedTotal, (int)self.actualTotal];
40    }
41}
42
43- (NSString *)failureMessageForShouldNot {
44    if (self.anyChange) {
45        return @"expected subject to not change the count";
46    } else {
47        return [NSString stringWithFormat:@"expected subject not to change the count to %d", (int)self.actualTotal];
48    }
49}
50
51- (NSString *)description {
52    if (self.anyChange) {
53        return @"change count";
54    } else {
55        return [NSString stringWithFormat:@"change count by %d", (int)self.expectedDifference];
56    }
57}
58
59- (BOOL)evaluate {
60    NSInteger before = self.countBlock();
61    // Perform actual work, which is expected to change the result of countBlock.
62    [self.subject call];
63    self.actualTotal = self.countBlock();
64
65    if (self.anyChange) {
66        return before != self.actualTotal;
67    } else {
68        self.expectedTotal = before + self.expectedDifference;
69        return self.expectedTotal == self.actualTotal;
70    }
71}
72
73- (void)change:(KWChangeMatcherCountBlock)countBlock by:(NSInteger)expectedDifference {
74    self.anyChange = NO;
75    self.expectedDifference = expectedDifference;
76    self.countBlock = countBlock;
77}
78
79- (void)change:(KWChangeMatcherCountBlock)countBlock {
80    self.anyChange = YES;
81    self.countBlock = countBlock;
82}
83
84@end