-
-
Save neilkimmett/9364757 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by azu on 2013/06/09. | |
// License MIT | |
#import <Foundation/Foundation.h> | |
#import "KWMatcher.h" | |
typedef NS_ENUM(NSUInteger, KWDateInequalityType){ | |
KWDateInequalityTypeEqualToDateIgnoringTime,// ≒ | |
KWDateInequalityTypeEqualToDateIgnoringMinutes | |
}; | |
/* How to Use | |
Put Your Spec File: | |
registerMatchers(@"AZ");// NSDate Custom Matcher | |
*/ | |
@interface AZNSDateKiwiMatcher : KWMatcher | |
- (void)beEqualToDateIgnoringTime:(NSDate *) otherDate; | |
- (void)beEqualToDateIgnoringMinutes:(NSDate *) otherDate; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by azu on 2013/06/09. | |
// | |
#import "AZNSDateKiwiMatcher.h" | |
#import "KWFormatter.h" | |
@interface AZNSDateKiwiMatcher () | |
@property(nonatomic) KWDateInequalityType dateInequalityType; | |
@property(nonatomic, strong) NSDate *otherDate; | |
@end | |
@implementation AZNSDateKiwiMatcher { | |
} | |
#pragma mark Getting Matcher Strings | |
// REQUIRED: Return an array of selector strings for the expectations this | |
// matcher is used for. | |
// | |
// For example, this matcher handles [[subject should] beTypeOfMammal:] and | |
// [[subject should] beTypeOfInsect:]. | |
+ (NSArray *)matcherStrings { | |
return @[@"beEqualToDateIgnoringTime:", @"beEqualToDateIgnoringMinutes:"]; | |
} | |
#pragma mark Matching | |
// REQUIRED: Evaluate the predicate here. | |
// self.subject is available automatically. | |
// self.otherDate is a member variable you would have declared yourself. | |
- (BOOL)evaluate { | |
NSDate *subjectDate = (NSDate *)self.subject; | |
if (![subjectDate isKindOfClass:[NSDate class]]) { | |
[NSException raise:@"KWMatcherException" format:@"subject is not a NSDate"]; | |
return NO; | |
} | |
switch (self.dateInequalityType) { | |
case KWDateInequalityTypeEqualToDateIgnoringTime: | |
return [self compareDateIgnoringTime]; | |
case KWDateInequalityTypeEqualToDateIgnoringMinutes: | |
return [self compareDateIgnoringMinutes]; | |
} | |
return NO; | |
} | |
- (BOOL)compareDateIgnoringTime { | |
NSCalendar *currentCalendar = [NSCalendar currentCalendar]; | |
enum NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; | |
NSDateComponents *components1 = [currentCalendar components:unitFlags fromDate:self.subject]; | |
NSDateComponents *components2 = [currentCalendar components:unitFlags fromDate:self.otherDate]; | |
return ((components1.year == components2.year) && | |
(components1.month == components2.month) && | |
(components1.day == components2.day)); | |
} | |
- (BOOL)compareDateIgnoringMinutes { | |
NSCalendar *currentCalendar = [NSCalendar currentCalendar]; | |
enum NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit; | |
NSDateComponents *components1 = [currentCalendar components:unitFlags fromDate:self.subject]; | |
NSDateComponents *components2 = [currentCalendar components:unitFlags fromDate:self.otherDate]; | |
return ((components1.year == components2.year) && | |
(components1.month == components2.month) && | |
(components1.day == components2.day) && | |
(components1.hour == components2.hour)); | |
} | |
#pragma mark Getting Failure Messages | |
- (NSString *)comparisonPhrase { | |
switch (self.dateInequalityType) { | |
case KWDateInequalityTypeEqualToDateIgnoringTime: | |
case KWDateInequalityTypeEqualToDateIgnoringMinutes: | |
return @"≒"; | |
} | |
assert(0 && "should never reach here"); | |
return nil; | |
} | |
- (NSString *)failureMessageForShould { | |
return [NSString stringWithFormat:@"expected subject to be %@ %@, got %@", | |
[self comparisonPhrase], | |
[KWFormatter formatObject:self.otherDate], | |
[KWFormatter formatObject:self.subject]]; | |
} | |
- (NSString *)description { | |
return [NSString stringWithFormat:@"be %@ %@", [self comparisonPhrase], [KWFormatter formatObject:self.otherDate]]; | |
} | |
#pragma mark Configuring Matchers | |
- (void)beEqualToDateIgnoringTime:(NSDate *) otherDate { | |
self.dateInequalityType = KWDateInequalityTypeEqualToDateIgnoringTime; | |
self.otherDate = otherDate; | |
} | |
- (void)beEqualToDateIgnoringMinutes:(NSDate *) otherDate { | |
self.dateInequalityType = KWDateInequalityTypeEqualToDateIgnoringMinutes; | |
self.otherDate = otherDate; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment