-
Notifications
You must be signed in to change notification settings - Fork 102
/
HaveValidSnapshot.swift
159 lines (125 loc) · 5.86 KB
/
HaveValidSnapshot.swift
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
//
// HaveValidSnapshot.swift
// Bootstrap
//
// Created by Ash Furrow on 2014-08-07.
// Copyright (c) 2014 Artsy. All rights reserved.
//
import Foundation
import UIKit
import Nimble
import QuartzCore
@objc protocol Snapshotable {
var snapshotObject: UIView? { get }
}
extension UIViewController : Snapshotable {
var snapshotObject: UIView? {
self.beginAppearanceTransition(true, animated: false)
self.endAppearanceTransition()
return view
}
}
extension UIView : Snapshotable {
var snapshotObject: UIView? {
return self
}
}
@objc class FBSnapshotTest {
var referenceImagesDirectory: String?
class var sharedInstance : FBSnapshotTest {
struct Instance {
static let instance: FBSnapshotTest = FBSnapshotTest()
}
return Instance.instance
}
class func setReferenceImagesDirectory(directory: String?) {
sharedInstance.referenceImagesDirectory = directory
}
class func compareSnapshot(instance: Snapshotable, snapshot: String, testCase: AnyObject, record: Bool, referenceDirectory: String) -> Bool {
var snapshotController: FBSnapshotTestController = FBSnapshotTestController(testClass: testCase.dynamicType)
snapshotController.recordMode = record
snapshotController.referenceImagesDirectory = referenceDirectory
assert(snapshotController.referenceImagesDirectory != nil, "Missing value for referenceImagesDirectory - Call FBSnapshotTest.setReferenceImagesDirectory(FB_REFERENCE_IMAGE_DIR)")
return snapshotController.compareSnapshotOfView(instance.snapshotObject, selector: Selector(snapshot), identifier: nil, error: nil)
}
}
func _getDefaultReferenceDirectory(sourceFileName: String) -> String {
if let globalReference = FBSnapshotTest.sharedInstance.referenceImagesDirectory {
return globalReference
}
// Search the test file's path to find the first folder with the substring "tests"
// then append "/ReferenceImages" and use that
var result: NSString?
let pathComponents: NSArray = sourceFileName.pathComponents
for folder in pathComponents {
if (folder.lowercaseString as NSString).hasSuffix("tests") {
let currentIndex = pathComponents.indexOfObject(folder) + 1
let folderPathComponents: NSArray = pathComponents.subarrayWithRange(NSMakeRange(0, currentIndex))
let folderPath = folderPathComponents.componentsJoinedByString("/")
result = folderPath + "/ReferenceImages"
}
}
assert(result != nil, "Could not infer reference image folder – You should provide a reference dir using FBSnapshotTest.setReferenceImagesDirectory(FB_REFERENCE_IMAGE_DIR)")
return result!
}
func _sanitizedTestPath(sourceLocation: String) -> String {
let filename = sourceLocation.pathComponents.last!
let characterSet = NSCharacterSet(charactersInString: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_")
let components: NSArray = filename.componentsSeparatedByCharactersInSet(characterSet.invertedSet)
return components.componentsJoinedByString("_")
}
func _clearFailureMessage(failureMessage: FailureMessage) {
failureMessage.actualValue = ""
failureMessage.expected = ""
failureMessage.postfixMessage = ""
failureMessage.to = ""
}
func _performSnapshotTest(name: String, actualExpression: Expression<Snapshotable>, failureMessage: FailureMessage) -> Bool {
let instance = actualExpression.evaluate()
let testFileLocation = actualExpression.location.file
let referenceImageDirectory = _getDefaultReferenceDirectory(testFileLocation)
let result = FBSnapshotTest.compareSnapshot(instance, snapshot: name, testCase: instance, record: false, referenceDirectory: referenceImageDirectory)
if !result {
_clearFailureMessage(failureMessage)
failureMessage.actualValue = "expected a matching snapshot in \(name)"
}
return result
}
func _recordSnapshot(name: String, actualExpression: Expression<Snapshotable>, failureMessage: FailureMessage) -> Bool {
let instance = actualExpression.evaluate()
let testFileLocation = actualExpression.location.file
let referenceImageDirectory = _getDefaultReferenceDirectory(testFileLocation)
let name = _sanitizedTestPath(testFileLocation)
_clearFailureMessage(failureMessage)
if FBSnapshotTest.compareSnapshot(instance, snapshot: name, testCase: instance, record: true, referenceDirectory: referenceImageDirectory) {
failureMessage.actualValue = "snapshot \(name) successfully recorded, replace recordSnapshot with a check"
} else {
failureMessage.actualValue = "expected to record a snapshot in \(name)"
}
return false
}
func haveValidSnapshot() -> MatcherFunc<Snapshotable> {
return MatcherFunc { actualExpression, failureMessage in
let testFileLocation = actualExpression.location.file
let name = _sanitizedTestPath(testFileLocation)
return _performSnapshotTest(name, actualExpression, failureMessage)
}
}
func haveValidSnapshot(named name: String) -> MatcherFunc<Snapshotable> {
return MatcherFunc { actualExpression, failureMessage in
let testFileLocation = actualExpression.location.file
return _performSnapshotTest(name, actualExpression, failureMessage)
}
}
func recordSnapshot() -> MatcherFunc<Snapshotable> {
return MatcherFunc { actualExpression, failureMessage in
let testFileLocation = actualExpression.location.file
let name = _sanitizedTestPath(testFileLocation)
return _recordSnapshot(name, actualExpression, failureMessage)
}
}
func recordSnapshot(named name: String) -> MatcherFunc<Snapshotable> {
return MatcherFunc { actualExpression, failureMessage in
return _recordSnapshot(name, actualExpression, failureMessage)
}
}