-
Notifications
You must be signed in to change notification settings - Fork 1
/
ViewSnapshotTestCase.swift
83 lines (70 loc) · 2.89 KB
/
ViewSnapshotTestCase.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
//
// ViewSnapshotTestCase.swift
// ViewSnapshots
//
// Created by Daniel Eggert on 30/12/2014.
// Copyright (c) 2014 Daniel Eggert. All rights reserved.
//
import UIKit
import XCTest
import Foundation
//
//func SnapshotVerifyView(testCase: FBSnapshotTestCase, view : @autoclosure () -> UIView, identifier : String, file: String = __FILE__, line: UWord = __LINE__) {
// let referenceImagesDir = NSProcessInfo().environment["REFERENCE_IMAGES_DIR"] as String
//
// var error: NSError?
// let success = testCase.compareSnapshotOfView(view(), referenceImagesDirectory: referenceImagesDir, identifier: identifier, error: &error)
// XCTAssertTrue(success, "Snapshot compoarison failed: \(error)", file: file, line: line)
// XCTAssertFalse(testCase.recordMode, "Test ran in record mode. Reference image is now saved. Disable record mode to perform an actual snapshot comparison!", file: file, line: line)
//}
/// Akin to the beloved XCTAssert() functions, this one will check that the specified view matches the snapshot on disk.
///
/// The XCTestClass subclass must implement the ViewSnapshotRecording protocol.
///
/// Use like this
///
/// let view: UIView <- assume we have this
/// SnapshotVerifyView(self, view, "normal")
///
/// Set the viewSnapshotMode inside the setUp method to .Record to create files or to .Verify to compare the view(s) against recorded snapshots.
public func SnapshotVerifyView(testCase: ViewSnapshotTestCase, @autoclosure view : () -> UIView, identifier : String = "", file: String = __FILE__, line: UInt = __LINE__) {
do {
let snapshot = try ViewSnapshot(view: view(), identifier: identifier, testCase: testCase)
let location = SourceCodeLocation(file: file, line: line)
switch testCase.viewSnapshotMode {
case .Record:
snapshot.recordSnapshotForLocation(location)
case .Verify:
try snapshot.verifySnapshotForLocation(location)
}
} catch ViewSnapshotError.Error(let message) {
XCTFail(message, file: file, line: line)
} catch let error {
XCTFail("Error: \(error)", file: file, line: line)
}
}
public typealias ViewSnapshotTestCase = protocol<NamedTestCase, ViewSnapshotRecording>
public enum ViewSnapshotMode {
case Record
case Verify
}
// Need NSObjectProtocol here, otherwise the swift compiler crashes.
public protocol ViewSnapshotRecording : NSObjectProtocol {
var viewSnapshotMode: ViewSnapshotMode { get }
}
public protocol NamedTestCase : NSObjectProtocol {
var testMethodName: String { get }
var testCaseName: String { get }
}
extension XCTestCase : NamedTestCase {
public var testMethodName: String {
get {
return NSStringFromSelector(self.invocation!.selector)
}
}
public var testCaseName: String {
get {
return NSStringFromClass(self.dynamicType)
}
}
}