-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathDescriptions.swift
70 lines (57 loc) · 2.16 KB
/
Descriptions.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
import Foundation
func describe<T>(_ value: T) -> String {
if let stringArray = value as? [String] {
return joinDescriptions(stringArray.map {describe($0)})
}
if let string = value as? String {
return "\"\(string)\""
}
return String(describing: value)
}
func describeAddress<T: AnyObject>(_ object: T) -> String {
return NSString(format: "%p", unsafeBitCast(object, to: Int.self)) as String
}
func describeError(_ error: Error) -> String {
return "ERROR: \(error)"
}
func describeExpectedError() -> String {
return "EXPECTED ERROR"
}
func describeExpectedError(_ description: String) -> String {
return "EXPECTED ERROR: \(description)"
}
func describeErrorMismatch<T>(_ error: T, _ description: String, _ mismatchDescription: String?) -> String {
return "GOT ERROR: " + describeActualValue(error, mismatchDescription) + ", EXPECTED ERROR: \(description)"
}
func describeUnexpectedError() -> String {
return "UNEXPECTED ERROR"
}
public func describeMismatch<T>(_ value: T, _ description: String, _ mismatchDescription: String?) -> String {
return "GOT: " + describeActualValue(value, mismatchDescription) + ", EXPECTED: \(description)"
}
func describeActualValue<T>(_ value: T, _ mismatchDescription: String?) -> String {
return describe(value) + (mismatchDescription.map {" (\($0))"} ?? "")
}
func joinDescriptions(_ descriptions: [String]) -> String {
return joinStrings(descriptions)
}
func joinDescriptions(_ descriptions: [String?]) -> String? {
let notNil = filterNotNil(descriptions)
return notNil.isEmpty ? nil : joinStrings(notNil)
}
func joinMatcherDescriptions<S: Sequence, T>(_ matchers: S, prefix: String = "all of") -> String where S.Iterator.Element == Matcher<T> {
var generator = matchers.makeIterator()
if let first = generator.next(), generator.next() == nil {
return first.description
} else {
return prefix + " " + joinDescriptions(matchers.map({$0.description}))
}
}
private func joinStrings(_ strings: [String]) -> String {
switch strings.count {
case 1:
return strings[0]
default:
return "[" + strings.joined(separator: ", ") + "]"
}
}