/
BeInstanceOfSpec.mm
189 lines (154 loc) · 7.77 KB
/
BeInstanceOfSpec.mm
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
189
#if TARGET_OS_IPHONE
#import <Cedar/CDRSpecHelper.h>
#else
#import <Cedar/CDRSpecHelper.h>
#endif
extern "C" {
#import "ExpectFailureWithMessage.h"
}
@interface FooBase : NSObject; @end
@implementation FooBase; @end
@interface FooDerived : FooBase; @end
@implementation FooDerived; @end
@interface BarNotMemberOfOwnClass : NSObject @end
@implementation BarNotMemberOfOwnClass
- (BOOL)isMemberOfClass:(Class)aClass {
return NO;
}
- (BOOL)isKindOfClass:(Class)aClass {
return NO;
}
@end
using namespace Cedar::Matchers;
SPEC_BEGIN(BeInstanceOfSpec)
describe(@"be_instance_of matcher", ^{
describe(@"without decoration", ^{
Class expectedClass = [FooBase class];
describe(@"when the actual value is an instance of the expected class", ^{
id actualValue = [[[FooBase alloc] init] autorelease];
describe(@"positive match", ^{
it(@"should should pass", ^{
expect(actualValue).to(be_instance_of(expectedClass));
});
});
describe(@"negative match", ^{
it(@"should fail with a sensible failure message", ^{
expectFailureWithMessage([NSString stringWithFormat:@"Expected <%@ (%@)> to not be an instance of class <%@>", actualValue, [actualValue class], expectedClass], ^{
expect(actualValue).to_not(be_instance_of(expectedClass));
});
});
});
});
describe(@"when the actual value is an instance of a subclass of the expected class", ^{
id actualValue = [[[FooDerived alloc] init] autorelease];
describe(@"positive match", ^{
it(@"should fail with a sensible failure message", ^{
expectFailureWithMessage([NSString stringWithFormat:@"Expected <%@ (%@)> to be an instance of class <%@>", actualValue, [actualValue class], expectedClass], ^{
expect(actualValue).to(be_instance_of(expectedClass));
});
});
});
describe(@"negative match", ^{
it(@"should should pass", ^{
expect(actualValue).to_not(be_instance_of(expectedClass));
});
});
});
describe(@"when the actual value is not an instance of the expected class, or any of its subclasses", ^{
id actualValue = [NSString string];
describe(@"positive match", ^{
it(@"should fail with a sensible failure message", ^{
expectFailureWithMessage([NSString stringWithFormat:@"Expected <%@ (%@)> to be an instance of class <%@>", actualValue, [actualValue class], expectedClass], ^{
expect(actualValue).to(be_instance_of(expectedClass));
});
});
});
describe(@"negative match", ^{
it(@"should should pass", ^{
expect(actualValue).to_not(be_instance_of(expectedClass));
});
});
});
describe(@"when the actual value is not an instance of the expected class but they have the same class name", ^{
Class expectedClass = [BarNotMemberOfOwnClass class];
id actualValue = [[[BarNotMemberOfOwnClass alloc] init] autorelease];
describe(@"positive match", ^{
it(@"should fail with a failure message suggesting solution to the problem", ^{
expectFailureWithMessage([NSString stringWithFormat:@"Expected <%@ (%@)> to be an instance of class <%@>. Did you accidentally add the class to your specs target also?", actualValue, [actualValue class], expectedClass], ^{
expect(actualValue).to(be_instance_of(expectedClass));
});
});
});
describe(@"negative match", ^{
it(@"should should pass", ^{
expect(actualValue).to_not(be_instance_of(expectedClass));
});
});
});
});
describe(@"with the or_any_subclass decoration", ^{
Class expectedClass = [FooBase class];
describe(@"when the actual value is an instance of the expected class", ^{
id actualValue = [[[FooBase alloc] init] autorelease];
describe(@"positive match", ^{
it(@"should should pass", ^{
expect(actualValue).to(be_instance_of(expectedClass).or_any_subclass());
});
});
describe(@"negative match", ^{
it(@"should fail with a sensible failure message", ^{
expectFailureWithMessage([NSString stringWithFormat:@"Expected <%@ (%@)> to not be an instance of class <%@>, or any of its subclasses", actualValue, [actualValue class], expectedClass], ^{
expect(actualValue).to_not(be_instance_of(expectedClass).or_any_subclass());
});
});
});
});
describe(@"when the actual value is an instance of a subclass of the expected class", ^{
id actualValue = [[[FooDerived alloc] init] autorelease];
describe(@"positive match", ^{
it(@"should should pass", ^{
expect(actualValue).to(be_instance_of(expectedClass).or_any_subclass());
});
});
describe(@"negative match", ^{
it(@"should fail with a sensible failure message", ^{
expectFailureWithMessage([NSString stringWithFormat:@"Expected <%@ (%@)> to not be an instance of class <%@>, or any of its subclasses", actualValue, [actualValue class], expectedClass], ^{
expect(actualValue).to_not(be_instance_of(expectedClass).or_any_subclass());
});
});
});
});
describe(@"when the actual value is not an instance of the expected class, or any of its subclasses", ^{
id actualValue = [NSString string];
describe(@"positive match", ^{
it(@"should fail with a sensible failure message", ^{
expectFailureWithMessage([NSString stringWithFormat:@"Expected <%@ (%@)> to be an instance of class <%@>, or any of its subclasses", actualValue, [actualValue class], expectedClass], ^{
expect(actualValue).to(be_instance_of(expectedClass).or_any_subclass());
});
});
});
describe(@"negative match", ^{
it(@"should should pass", ^{
expect(actualValue).to_not(be_instance_of(expectedClass).or_any_subclass());
});
});
});
describe(@"when the actual value is not an instance of the expected class but they have the same class name", ^{
Class expectedClass = [BarNotMemberOfOwnClass class];
id actualValue = [[[BarNotMemberOfOwnClass alloc] init] autorelease];
describe(@"positive match", ^{
it(@"should fail with a failure message suggesting solution to the problem", ^{
expectFailureWithMessage([NSString stringWithFormat:@"Expected <%@ (%@)> to be an instance of class <%@>, or any of its subclasses. Did you accidentally add the class to your specs target also?", actualValue, [actualValue class], expectedClass], ^{
expect(actualValue).to(be_instance_of(expectedClass).or_any_subclass());
});
});
});
describe(@"negative match", ^{
it(@"should should pass", ^{
expect(actualValue).to_not(be_instance_of(expectedClass).or_any_subclass());
});
});
});
});
});
SPEC_END