-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTestUtils.swift
93 lines (82 loc) · 3.79 KB
/
TestUtils.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
//
// TestUtils.swift
// BuildaUtils
//
// Created by Mateusz Zając on 23/07/15.
// Copyright © 2015 Honza Dvorsky. All rights reserved.
//
import Foundation
import XCTest
// MARK: Exception assertions
// Based on: https://forums.developer.apple.com/thread/5824
extension XCTestCase {
/**
Replacement method for XCTAssertThrowsError which isn't currently supported.
- parameter message: Message which should be displayed
- parameter file: File in which assertion happened
- parameter line: Line in which assertion happened
- parameter block: Block of code against which assertion should be matched
*/
func XCTempAssertThrowsError(_ message: String = "", file: StaticString = #file, line: UInt = #line, _ block: () throws -> ()) {
do {
try block()
let msg = (message == "") ? "Tested block did not throw error as expected." : message
XCTFail(msg, file: file, line: line)
} catch {}
}
/**
Replacement method for XCTAssertThrowsSpecificError which isn't currently supported.
- parameter kind: ErrorType which is expected to be thrown from block
- parameter message: Message which should be displayed
- parameter file: File in which assertion happened
- parameter line: Line in which assertion happened
- parameter block: Block of code against which assertion should be matched
*/
func XCTempAssertThrowsSpecificError(_ kind: Error, _ message: String = "", file: StaticString = #file, line: UInt = #line, _ block: () throws -> ()) {
do {
try block()
let msg = (message == "") ? "Tested block did not throw expected \(kind) error." : message
XCTFail(msg, file: file, line: line)
} catch let error as NSError {
let expected = kind as NSError
if ((error.domain != expected.domain) || (error.code != expected.code)) {
let msg = (message == "") ? "Tested block threw \(error), not expected \(kind) error." : message
XCTFail(msg, file: file, line: line)
}
}
}
/**
Replacement method for XCTAssertNoThrowsError which isn't currently supported.
- parameter message: Message which should be displayed
- parameter file: File in which assertion happened
- parameter line: Line in which assertion happened
- parameter block: Block of code against which assertion should be matched
*/
func XCTempAssertNoThrowError(_ message: String = "", file: StaticString = #file, line: UInt = #line, _ block: () throws -> ()) {
do {
try block()
} catch {
let msg = (message == "") ? "Tested block threw unexpected error." : message
XCTFail(msg, file: file, line: line)
}
}
/**
Replacement method for XCTAssertNoThrowsSpecificError which isn't currently supported.
- parameter kind: ErrorType which isn't expected to be thrown from block
- parameter message: Message which should be displayed
- parameter file: File in which assertion happened
- parameter line: Line in which assertion happened
- parameter block: Block of code against which assertion should be matched
*/
func XCTempAssertNoThrowSpecificError(_ kind: Error, _ message: String = "", file: StaticString = #file, line: UInt = #line, _ block: () throws -> ()) {
do {
try block()
} catch let error as NSError {
let unwanted = kind as NSError
if ((error.domain == unwanted.domain) && (error.code == unwanted.code)) {
let msg = (message == "") ? "Tested block threw unexpected \(kind) error." : message
XCTFail(msg, file: file, line: line)
}
}
}
}