-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathThrowingHelpers.swift
69 lines (64 loc) · 1.51 KB
/
ThrowingHelpers.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
import Foundation
import XCTest
@discardableResult
public func assertDoesNotThrow<T>(
message: (Error) -> String = { "Unexpected error thrown: \($0)" },
file: StaticString = #filePath,
line: UInt = #line,
work: () throws -> T
) -> T {
do {
return try work()
} catch {
failTest(message(error), file: file, line: line)
}
}
@discardableResult
public func assertDoesNotThrow<T>(
message: (Error) -> String = { "Unexpected error thrown: \($0)" },
file: StaticString = #filePath,
line: UInt = #line,
work: () async throws -> T
) async -> T {
do {
return try await work()
} catch {
failTest(message(error), file: file, line: line)
}
}
public func assertThrows<T>(
file: StaticString = #filePath,
line: UInt = #line,
work: () throws -> (T)
) {
do {
_ = try work()
failTest("Expected to throw an error, but no error has been thrown", file: file, line: line)
} catch {
return
}
}
public func assertThrows<T>(
file: StaticString = #filePath,
line: UInt = #line,
work: () async throws -> (T)
) async {
do {
_ = try await work()
failTest("Expected to throw an error, but no error has been thrown", file: file, line: line)
} catch {
return
}
}
@discardableResult
public func skipThrownErrors<T>(
file: StaticString = #filePath,
line: UInt = #line,
work: () throws -> (T)
) -> T? {
do {
return try work()
} catch {
return nil
}
}