-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathBasicMatchers.swift
66 lines (53 loc) · 1.95 KB
/
BasicMatchers.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
public func anything<T>() -> Matcher<T> {
return Matcher("anything") {value in true}
}
public func sameInstance<T: AnyObject>(_ expectedValue: T) -> Matcher<T> {
return Matcher("same instance as \(describeAddress(expectedValue))") { (value: T) -> MatchResult in
value === expectedValue ? .match : .mismatch(describeAddress(value))
}
}
// MARK: is
public func `is`<T>(_ matcher: Matcher<T>) -> Matcher<T> {
return Matcher("is " + matcher.description) { (value: T) -> MatchResult in
return matcher.matches(value)
}
}
public func isA<T: Any>(_ expectedType: T.Type) -> Matcher<Any> {
return `is`(instanceOf(expectedType))
}
public func `is`<T: Equatable>(_ expectedValue: T) -> Matcher<T> {
return `is`(equalTo(expectedValue))
}
// MARK: optionals
public func nilValue<T>() -> Matcher<Optional<T>> {
return Matcher("nil") {$0 == nil}
}
public func present<T>() -> Matcher<Optional<T>> {
return Matcher("present") {$0 != nil}
}
public func presentAnd<T>(_ matcher: Matcher<T>) -> Matcher<Optional<T>> {
return Matcher("present and \(matcher.description)") { (value: T?) -> MatchResult in
if let unwrappedValue = value {
return matcher.matches(unwrappedValue)
} else {
return .mismatch(nil)
}
}
}
// MARK: casting
public func instanceOf<T: Any>(_ expectedType: T.Type) -> Matcher<Any> {
// There seems to be no way to get the type name.
return Matcher("instance of \(expectedType)") {$0 is T}
}
public func instanceOf<T: Any>(_ expectedType: T.Type, and matcher: Matcher<T>) -> Matcher<Any> {
return instanceOfAnd(matcher)
}
public func instanceOfAnd<T: Any>(_ matcher: Matcher<T>) -> Matcher<Any> {
return Matcher("instance of \(T.self) and \(matcher.description)") { (value: Any) -> MatchResult in
if let value = value as? T {
return matcher.matches(value)
} else {
return .mismatch("mismatched type")
}
}
}