forked from dblock/ios-snapshot-test-case-expecta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FBSnapshotTestCaseDemoSpecs.m
322 lines (250 loc) · 11.6 KB
/
FBSnapshotTestCaseDemoSpecs.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//
// FBSnapshotTestCaseDemoSpecs.m
// FBSnapshotTestCaseDemo
//
// Created by Daniel Doubrovkine on 1/14/14.
// Copyright (c) 2014 Artsy Inc. All rights reserved.
//
#define EXP_SHORTHAND
#include <Specta/Specta.h>
#include <Expecta/Expecta.h>
#include <Expecta_Snapshots/EXPMatchers+FBSnapshotTest.h>
#import <FBSnapshotTestCase/FBSnapshotTestCasePlatform.h>
#include "FBExampleView.h"
#import "FBViewController.h"
#define test_expect(a) [expect(a) test]
#define assertPass(expr) \
XCTAssertNoThrow((expr))
#import "EXPExpect+Test.h"
#import <malloc/malloc.h>
#import <objc/runtime.h>
SpecBegin(FBExampleView)
__block CGRect frame = CGRectMake(0, 0, 64, 64);
__block NSFileManager *fileManager = [[NSFileManager alloc] init];
__block NSString *imagesDirectory = [NSString stringWithFormat:@"%s/%@", FB_REFERENCE_IMAGE_DIR, @"FBExampleViewSpec"];
describe(@"snapshots", ^{
dispatch_block_t deleteSnapshots = ^{
NSArray *files = [fileManager contentsOfDirectoryAtPath:imagesDirectory error:nil];
for (NSString *file in files) {
NSString *fullPath = [imagesDirectory stringByAppendingPathComponent:file];
[fileManager removeItemAtPath:fullPath error:nil];
}
};
beforeEach(deleteSnapshots);
afterAll(deleteSnapshots);
describe(@"with a view", ^{
__block FBExampleView *view;
beforeEach(^{
view = [[FBRedView alloc] initWithFrame:frame];
});
describe(@"recording", ^{
it(@"named", ^{
expect(view).toNot.recordSnapshotNamed(@"view 1"); // Using "toNot" because recording always causes the test to fail.
NSString *imageName = [[fileManager contentsOfDirectoryAtPath:imagesDirectory error:nil] firstObject];
expect(imageName).to.contain(@"view 1");
});
it(@"unnamed", ^{
expect(view).toNot.recordSnapshot();
NSString *imageName = [[fileManager contentsOfDirectoryAtPath:imagesDirectory error:nil] firstObject];
expect(imageName).to.contain(@"snapshots_with_a_view_recording_unnamed");
});
});
describe(@"matching", ^{
describe(@"named", ^{
it(@"matches view", ^{
expect(view).toNot.recordSnapshotNamed(@"view 2");
expect(view).to.haveValidSnapshotNamed(@"view 2");
});
it(@"doesn't match if file doesn't exist", ^{
expect(view).toNot.haveValidSnapshotNamed(@"nonexistent image");
});
it(@"doesn't match if files differ", ^{
expect(view).toNot.recordSnapshotNamed(@"view 3");
UIView *newView = [[FBBlueView alloc] initWithFrame:frame];
expect(newView).toNot.haveValidSnapshotNamed(@"view 3");
});
});
describe(@"unnamed", ^{
it(@"matches view", ^{
expect(view).toNot.recordSnapshot();
expect(view).to.haveValidSnapshot();
});
it(@"doesn't match if file doesn't exist", ^{
expect(view).toNot.haveValidSnapshot();
});
it(@"doesn't match if files differ", ^{
expect(view).toNot.recordSnapshot();
UIView *newView = [[FBBlueView alloc] initWithFrame:frame];
expect(newView).toNot.haveValidSnapshot();
});
});
});
});
describe(@"with a view controller", ^{
__block FBViewController *controller;
before(^{
controller = [[FBRedViewController alloc] init];
controller.view.frame = frame;
});
describe(@"recording", ^{
it(@"named", ^{
expect(controller).toNot.recordSnapshotNamed(@"view controller 1");
expect(controller.viewWillAppearCalled).to.beTruthy();
expect(controller.viewDidAppearCalled).to.beTruthy();
NSString *imageName = [[fileManager contentsOfDirectoryAtPath:imagesDirectory error:nil] firstObject];
expect(imageName).to.contain(@"view controller 1");
});
it(@"unnamed", ^{
expect(controller).toNot.to.recordSnapshot();
expect(controller.viewWillAppearCalled).to.beTruthy();
expect(controller.viewDidAppearCalled).to.beTruthy();
NSString *imageName = [[fileManager contentsOfDirectoryAtPath:imagesDirectory error:nil] firstObject];
expect(imageName).to.contain(@"snapshots_with_a_view_controller_recording_unnamed");
});
});
describe(@"matching", ^{
describe(@"named", ^{
it(@"matches view controller", ^{
expect(controller).toNot.recordSnapshotNamed(@"view controller 2");
FBViewController *newVC = [[FBRedViewController alloc] init];
newVC.view.frame = frame;
expect(newVC).to.haveValidSnapshotNamed(@"view controller 2");
expect(newVC.viewWillAppearCalled).to.beTruthy();
expect(newVC.viewDidAppearCalled).to.beTruthy();
});
it(@"doesn't match if file doesn't exist", ^{
expect(controller).toNot.haveValidSnapshotNamed(@"nonexistent image");
expect(controller.viewWillAppearCalled).to.beTruthy();
expect(controller.viewDidAppearCalled).to.beTruthy();
});
it(@"doesn't match if files differ", ^{
expect(controller).toNot.recordSnapshotNamed(@"view controller 3");
FBViewController *newVC = [[FBBlueViewController alloc] init];
newVC.view.frame = frame;
expect(newVC).toNot.haveValidSnapshotNamed(@"view controller 3");
expect(newVC.viewWillAppearCalled).to.beTruthy();
expect(newVC.viewDidAppearCalled).to.beTruthy();
});
});
describe(@"unnamed", ^{
it(@"matches view controller", ^{
expect(controller).toNot.recordSnapshot();
FBViewController *newVC = [[FBRedViewController alloc] init];
newVC.view.frame = frame;
expect(newVC).to.haveValidSnapshot();
expect(newVC.viewWillAppearCalled).to.beTruthy();
expect(newVC.viewDidAppearCalled).to.beTruthy();
});
it(@"doesn't match if file doesn't exist", ^{
expect(controller).toNot.haveValidSnapshot();
expect(controller.viewWillAppearCalled).to.beTruthy();
expect(controller.viewDidAppearCalled).to.beTruthy();
});
it(@"doesn't match if files differ", ^{
expect(controller).toNot.recordSnapshot();
FBViewController *newVC = [[FBBlueViewController alloc] init];
newVC.view.frame = frame;
expect(newVC).toNot.haveValidSnapshot();
expect(newVC.viewWillAppearCalled).to.beTruthy();
expect(newVC.viewDidAppearCalled).to.beTruthy();
});
});
});
it(@"Raises when a nil is passed in", ^{
expect(^{
[expect(nil) test].to.haveValidSnapshot() ;
}).to.raise(@"Nil was passed into haveValidSnapshot. snapshots_with_a_view_controller_Raises_when_a_nil_is_passed_in");
});
});
describe(@"device agnostic", ^{
__block FBViewController *controller;
before(^{
controller = [[FBRedViewController alloc] init];
controller.view.frame = frame;
[Expecta setDeviceAgnostic:TRUE];
});
after(^{
[Expecta setDeviceAgnostic:FALSE];
});
describe(@"recording", ^{
it(@"named", ^{
expect(controller).toNot.recordSnapshotNamed(@"view controller 1");
expect(controller.viewWillAppearCalled).to.beTruthy();
expect(controller.viewDidAppearCalled).to.beTruthy();
NSString *imageName = [[fileManager contentsOfDirectoryAtPath:imagesDirectory error:nil] firstObject];
expect(imageName).to.contain(@"view controller 1");
});
it(@"unnamed", ^{
expect(controller).toNot.recordSnapshot();
expect(controller.viewWillAppearCalled).to.beTruthy();
expect(controller.viewDidAppearCalled).to.beTruthy();
NSString *imageName = [[fileManager contentsOfDirectoryAtPath:imagesDirectory error:nil] firstObject];
NSString *expectedImageName = FBFileNameIncludeNormalizedFileNameFromOption(@"snapshots_device_agnostic_recording_unnamed", 0);
expect(imageName).to.contain(expectedImageName);
});
});
describe(@"unnamed", ^{
describe(@"named", ^{
it(@"matches view controller", ^{
expect(controller).toNot.recordSnapshotNamed(@"view controller 2");
FBViewController *newVC = [[FBRedViewController alloc] init];
newVC.view.frame = frame;
expect(newVC).to.haveValidSnapshotNamed(@"view controller 2");
expect(newVC.viewWillAppearCalled).to.beTruthy();
expect(newVC.viewDidAppearCalled).to.beTruthy();
});
});
it(@"matches view controller", ^{
expect(controller).toNot.recordSnapshot();
FBViewController *newVC = [[FBRedViewController alloc] init];
newVC.view.frame = frame;
expect(newVC).to.haveValidSnapshot();
expect(newVC.viewWillAppearCalled).to.beTruthy();
expect(newVC.viewDidAppearCalled).to.beTruthy();
});
});
});
describe(@"with tolerance", ^{
__block FBExampleView *view;
before(^{
view = [[FBRedView alloc] initWithFrame:frame];
expect(view).toNot.recordSnapshot();
CGAffineTransform scale = CGAffineTransformMakeScale(0.5, 0.5);
CGRect rect = CGRectApplyAffineTransform(frame, scale);
FBBlueView *blueView = [[FBBlueView alloc] initWithFrame:rect];
[view addSubview:blueView];
});
it(@"matches if tolerance is enough", ^{
expect(view).to.haveValidSnapshotWithTolerance(0.25);
});
it(@"does not match if tolerance is not enough", ^{
expect(view).toNot.haveValidSnapshotWithTolerance(0.24);
});
});
it(@"matches view with 256 bytes aligned pointer", ^{
Class viewClass = [UIView class];
size_t viewInstanceSize = class_getInstanceSize(viewClass);
void *ptr = calloc(255 + viewInstanceSize, 1);
void *alignedPtr = nil;
for (int i = 0; i < 256; ++i) {
signed char ptrVal = (signed char)(ptr + i);
if (ptrVal == 0) {
alignedPtr = ptr + i;
}
}
UIView *view;
@autoreleasepool {
if (alignedPtr) {
id result = (__bridge_transfer id)alignedPtr;
object_setClass(result, viewClass);
view = [result initWithFrame:frame];
CFRetain((__bridge CFTypeRef)view); // prevent ARC from deallocation
}
expect(view).toNot.recordSnapshot();
expect(view).to.haveValidSnapshot();
// [view dealloc]; // should be called manually
}
free(ptr);
});
});
SpecEnd