-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNSObject+Shoulda.m
188 lines (155 loc) · 5.09 KB
/
NSObject+Shoulda.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//
// NSObject+Shoulda.m
//
// Created by Saul Mora on 3/11/10.
// Copyright 2010 Magical Panda Software, LLC All rights reserved.
//
#import "NSObject+Shoulda.h"
@implementation NSObject (Shoulda)
+ (void) object:(id)actual shouldBe:(id)expected withDescription:(NSString *)reason
{
GHAssertEquals(actual, expected, reason);
}
+ (void) object:(id)actual shouldNotBe:(id)expected withDescription:(NSString *)reason
{
GHAssertNotEquals(actual, expected, reason);
}
- (void) shouldNotBeNilWithDescription:(NSString *)reason
{
[self runTestCase:(TestBlock)^{
return self != nil;
}
withDescription:(ExpressionBlock)^{
return [NSString stringWithFormat:@"((%@) should != nil)", [self description]];
} andReason:reason];
}
- (void) shouldBeNilWithDescription:(NSString *)reason
{
[self runTestCase:(TestBlock)^ {
return self == nil;
}
withDescription:(ExpressionBlock)^{
return [NSString stringWithFormat:@"(%@) should == nil", [self description]];
} andReason:reason];
}
- (void) shouldBeTrueWithDescription:(NSString *)reason;
{
[self runTestCase:(TestBlock)^ {
return [[NSNumber numberWithBool:YES] isEqualToNumber:(NSNumber *)self];
}
withDescription:(ExpressionBlock)^{
return [NSString stringWithFormat:@"%@ should == YES", [self description]];
} andReason:reason];
}
- (void) shouldBeFalseWithDescription:(NSString *)reason;
{
[self runTestCase:(TestBlock)^ {
return [[NSNumber numberWithBool:NO] isEqualToNumber:(NSNumber *)self];
}
withDescription:(ExpressionBlock)^{
return [NSString stringWithFormat:@"%@ should == NO", [self description]];
} andReason:reason];
}
- (void) shouldBeEqualTo:(id)expected withDescription:(NSString *)reason;
{
[self runTestCase:(TestBlock)^ {
return [expected isEqual:self];
} withDescription:(ExpressionBlock)^{
return [NSString stringWithFormat:@"%@ should == %@", [self description], [expected description]];
} andReason:reason];
}
- (void) shouldNotBeEqualTo:(id)expected withDescription:(NSString *)reason;
{
[[NSException exceptionWithName:@"Not Yet implemented" reason:@"Fix Me!" userInfo:nil] raise];
}
- (void) shouldThrowException:(NSException *)expectedException withDescription:(NSString *)reason;
{
[[NSException exceptionWithName:@"Not Yet implemented" reason:@"Fix Me!" userInfo:nil] raise];
}
- (void) shouldThrowExceptionNamed:(NSString *)exceptionName withDescription:(NSString *)reason;
{
[[NSException exceptionWithName:@"Not Yet implemented" reason:@"Fix Me!" userInfo:nil] raise];
}
- (void) shouldRespondToSelector:(SEL)expectedSelector withDescription:(NSString *)reason
{
[self runTestCase:(TestBlock)^{
return [self respondsToSelector:expectedSelector];
} withDescription:(ExpressionBlock)^{
return [NSString stringWithFormat:@"'%@' should respond to selector '%@'", self, NSStringFromSelector(expectedSelector)];
} andReason:reason];
}
- (void) shouldNotRespondToSelector:(SEL)selector withDescription:(NSString *)reason
{
[self runTestCase:(TestBlock)^{
return ![self respondsToSelector:selector];
} withDescription:(ExpressionBlock)^{
return [NSString stringWithFormat:@"'%@' should NOT respond to selector '%@'", self, NSStringFromSelector(selector)];
} andReason:reason];
}
+ (void) shouldImplementProtocol:(Protocol *)expectedProtocol withDescription:(NSString *)reason
{
[self runTestCase:(TestBlock)^{
return [self conformsToProtocol:expectedProtocol];
} withDescription:(ExpressionBlock)^{
return [NSString stringWithFormat:@"'%@' should implement protocol '%@'", NSStringFromClass(self), NSStringFromProtocol(expectedProtocol)];
} andReason:reason];
}
@end
@implementation NSObject (SimpleShoulda)
+ (void) shouldBeNil:(id)actual
{
[NSObject object:actual shouldBe:nil];
}
+ (void) shouldNotBeNil:(id)actual
{
[NSObject object:actual shouldNotBe:nil];
}
+ (void) object:(id)actual shouldBe:(id)expected
{
[NSObject object:actual shouldBe:expected withDescription:@""];
}
+ (void) object:(id)actual shouldNotBe:(id)expected;
{
[NSObject object:actual shouldNotBe:expected withDescription:@""];
}
- (void) shouldBeNil
{
[self shouldBeNilWithDescription:@""];
}
- (void) shouldNotBeNil
{
[self shouldNotBeNilWithDescription:@""];
}
- (void) shouldBeTrue
{
[self shouldBeTrueWithDescription:@""];
}
- (void) shouldBeFalse
{
[self shouldBeFalseWithDescription:@""];
}
- (void) shouldBeEqualTo:(id)expected
{
[self shouldBeEqualTo:expected withDescription:@""];
}
- (void) shouldThrowException:(NSException *)expectedException
{
[self shouldThrowException:expectedException withDescription:@""];
}
- (void) shouldThrowExceptionNamed:(NSString *)exceptionName
{
[self shouldThrowExceptionNamed:exceptionName withDescription:@""];
}
- (void) shouldRespondToSelector:(SEL)expectedSelector
{
[self shouldRespondToSelector:expectedSelector withDescription:@""];
}
- (void) shouldNotRespondToSelector:(SEL)selector
{
[self shouldNotRespondToSelector:selector withDescription:@""];
}
+ (void) shouldImplementProtocol:(Protocol *)expectedProtocol
{
[self shouldImplementProtocol:expectedProtocol withDescription:@""];
}
@end