This repository has been archived by the owner on Aug 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSDateTest.m
144 lines (110 loc) · 5.26 KB
/
NSDateTest.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// NSDateTest.m
// Marshmallows
//
// Created by Sami Samhuri on 11-09-04.
// Copyright 2011 Sami Samhuri. All rights reserved.
//
#import <GHUnitIOS/GHUnit.h>
#import "NSDate+marshmallows.h"
#define MINUTE 60.0
#define HOUR (60.0 * MINUTE)
#define DAY (24.0 * HOUR)
#define WEEK (7.0 * DAY)
#define MONTH (30.0 * DAY)
#define YEAR (365.25 * DAY)
@interface NSDateTest : GHTestCase { }
@end
@implementation NSDateTest
- (void) testMethodExists
{
NSDate *date = [NSDate date];
GHAssertTrue([date respondsToSelector: @selector(relativeToNow)], @"Extension method -[NSDate relativeToNow] not available");
}
- (void) testPastDates
{
NSDate *date;
// < 1 minute ago
date = [NSDate dateWithTimeIntervalSinceNow: -59.0];
GHAssertEqualStrings(@"right now", [date relativeToNow], @"Incorrect relative date.");
// < 2 minutes ago
date = [NSDate dateWithTimeIntervalSinceNow: -119.0];
GHAssertEqualStrings(@"a minute ago", [date relativeToNow], @"Incorrect relative date.");
// < 1 hour ago
date = [NSDate dateWithTimeIntervalSinceNow: -57 * MINUTE];
GHAssertEqualStrings(@"57 minutes ago", [date relativeToNow], @"Incorrect relative date.");
// < 2 hours ago
date = [NSDate dateWithTimeIntervalSinceNow: -117 * MINUTE];
GHAssertEqualStrings(@"an hour ago", [date relativeToNow], @"Incorrect relative date.");
// < 1 day ago
date = [NSDate dateWithTimeIntervalSinceNow: -23 * HOUR];
GHAssertEqualStrings(@"23 hours ago", [date relativeToNow], @"Incorrect relative date.");
// < 2 days ago
date = [NSDate dateWithTimeIntervalSinceNow: -47 * HOUR];
GHAssertEqualStrings(@"yesterday", [date relativeToNow], @"Incorrect relative date.");
// < 1 week ago
date = [NSDate dateWithTimeIntervalSinceNow: -6 * DAY];
GHAssertEqualStrings(@"6 days ago", [date relativeToNow], @"Incorrect relative date.");
// < 2 weeks ago
date = [NSDate dateWithTimeIntervalSinceNow: -13 * DAY];
GHAssertEqualStrings(@"last week", [date relativeToNow], @"Incorrect relative date.");
// < 4 weeks ago
date = [NSDate dateWithTimeIntervalSinceNow: -3 * WEEK];
GHAssertEqualStrings(@"3 weeks ago", [date relativeToNow], @"Incorrect relative date.");
// < 8 weeks ago
date = [NSDate dateWithTimeIntervalSinceNow: -7 * WEEK];
GHAssertEqualStrings(@"last month", [date relativeToNow], @"Incorrect relative date.");
// < 1 year ago
date = [NSDate dateWithTimeIntervalSinceNow: -11 * MONTH];
GHAssertEqualStrings(@"11 months ago", [date relativeToNow], @"Incorrect relative date.");
// last year
date = [NSDate dateWithTimeIntervalSinceNow: -729 * DAY];
GHAssertEqualStrings(@"last year", [date relativeToNow], @"Incorrect relative date.");
// 5 years ago
date = [NSDate dateWithTimeIntervalSinceNow: -5 * YEAR - 1];
GHAssertEqualStrings(@"5 years ago", [date relativeToNow], @"Incorrect relative date.");
}
- (void) testFutureDates
{
NSDate *date;
// within 1 minute
date = [NSDate dateWithTimeIntervalSinceNow: 59.0];
GHAssertEqualStrings(@"right now", [date relativeToNow], @"Incorrect relative date.");
// within 2 minutes
date = [NSDate dateWithTimeIntervalSinceNow: 119.0];
GHAssertEqualStrings(@"in a minute", [date relativeToNow], @"Incorrect relative date.");
// within 1 hour
date = [NSDate dateWithTimeIntervalSinceNow: 57 * MINUTE + 1];
GHAssertEqualStrings(@"in 57 minutes", [date relativeToNow], @"Incorrect relative date.");
// in an hour
date = [NSDate dateWithTimeIntervalSinceNow: 117 * MINUTE];
GHAssertEqualStrings(@"in an hour", [date relativeToNow], @"Incorrect relative date.");
// within 1 day
date = [NSDate dateWithTimeIntervalSinceNow: 23 * HOUR + 1];
GHAssertEqualStrings(@"in 23 hours", [date relativeToNow], @"Incorrect relative date.");
// tomorrow
date = [NSDate dateWithTimeIntervalSinceNow: 47 * HOUR];
GHAssertEqualStrings(@"tomorrow", [date relativeToNow], @"Incorrect relative date.");
// within 1 week
date = [NSDate dateWithTimeIntervalSinceNow: 6 * DAY + 1];
GHAssertEqualStrings(@"in 6 days", [date relativeToNow], @"Incorrect relative date.");
// next week
date = [NSDate dateWithTimeIntervalSinceNow: 13 * DAY];
GHAssertEqualStrings(@"next week", [date relativeToNow], @"Incorrect relative date.");
// within 4 weeks
date = [NSDate dateWithTimeIntervalSinceNow: 3 * WEEK + 1];
GHAssertEqualStrings(@"in 3 weeks", [date relativeToNow], @"Incorrect relative date.");
// next month
date = [NSDate dateWithTimeIntervalSinceNow: 7 * WEEK];
GHAssertEqualStrings(@"next month", [date relativeToNow], @"Incorrect relative date.");
// within 1 year
date = [NSDate dateWithTimeIntervalSinceNow: 11 * MONTH + 1];
GHAssertEqualStrings(@"in 11 months", [date relativeToNow], @"Incorrect relative date.");
// next year
date = [NSDate dateWithTimeIntervalSinceNow: 729 * DAY];
GHAssertEqualStrings(@"next year", [date relativeToNow], @"Incorrect relative date.");
// in year 5 years
date = [NSDate dateWithTimeIntervalSinceNow: 5 * YEAR + 1];
GHAssertEqualStrings(@"in 5 years", [date relativeToNow], @"Incorrect relative date.");
}
@end