-
-
Notifications
You must be signed in to change notification settings - Fork 608
Expand file tree
/
Copy pathBeAnInstanceOf.swift
More file actions
52 lines (48 loc) · 2.04 KB
/
Copy pathBeAnInstanceOf.swift
File metadata and controls
52 lines (48 loc) · 2.04 KB
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
import Foundation
/// A Nimble matcher that succeeds when the actual value is an _exact_ instance of the given class.
public func beAnInstanceOf<T, U>(_ expectedType: T.Type) -> Matcher<U> {
return Matcher.define { actualExpression in
let instance = try actualExpression.evaluate()
guard let validInstance: Any = instance else {
return MatcherResult(
status: .doesNotMatch,
message: .expectedActualValueTo("be an instance of \(String(describing: expectedType))")
)
}
return MatcherResult(
status: MatcherStatus(bool: type(of: validInstance) == expectedType),
message: .expectedCustomValueTo(
"be an instance of \(String(describing: expectedType))",
actual: "<\(String(describing: type(of: validInstance))) instance>"
)
)
}
}
/// A Nimble matcher that succeeds when the actual value is an instance of the given class.
/// @see beAKindOf if you want to match against subclasses
public func beAnInstanceOf(_ expectedClass: AnyClass) -> Matcher<NSObject> {
return Matcher.define { actualExpression in
let instance = try actualExpression.evaluate()
#if canImport(Darwin)
let matches = instance != nil && instance!.isMember(of: expectedClass)
#else
let matches = instance != nil && type(of: instance!) == expectedClass
#endif
return MatcherResult(
status: MatcherStatus(bool: matches),
message: .expectedCustomValueTo(
"be an instance of \(String(describing: expectedClass))",
actual: instance.map { "<\(String(describing: type(of: $0))) instance>" } ?? "<nil>"
)
)
}
}
#if canImport(Darwin)
extension NMBMatcher {
@objc public class func beAnInstanceOfMatcher(_ expected: AnyClass) -> NMBMatcher {
return NMBMatcher { actualExpression in
return try beAnInstanceOf(expected).satisfies(actualExpression).toObjectiveC()
}
}
}
#endif