@@ -29,9 +29,37 @@ public func map<T, U>(_ transform: @escaping (T) async throws -> U, _ matcher: s
2929/// `map` works by transforming the expression to a value that the given matcher uses.
3030///
3131/// For example, you might only care that a particular property on a method equals some other value.
32+ /// So, you could write `expect(myObject).to(map(\.someOptionalIntValue, equal(3))`.
33+ /// This is also useful in conjunction with ``satisfyAllOf`` to do a partial equality of an object.
34+ public func map< T, U> ( _ transform: @escaping ( T ) throws -> U ? , _ matcher: Matcher < U > ) -> Matcher < T > {
35+ Matcher { ( received: Expression < T > ) in
36+ try matcher. satisfies ( received. cast { value in
37+ guard let value else { return nil }
38+ return try transform ( value)
39+ } )
40+ }
41+ }
42+
43+ /// `map` works by transforming the expression to a value that the given matcher uses.
44+ ///
45+ /// For example, you might only care that a particular property on a method equals some other value.
46+ /// So, you could write `expect(myObject).to(map(\.someOptionalIntValue, equal(3))`.
47+ /// This is also useful in conjunction with ``satisfyAllOf`` to do a partial equality of an object.
48+ public func map< T, U> ( _ transform: @escaping ( T ) async throws -> U ? , _ matcher: some AsyncableMatcher < U > ) -> AsyncMatcher < T > {
49+ AsyncMatcher { ( received: AsyncExpression < T > ) in
50+ try await matcher. satisfies ( received. cast { value in
51+ guard let value else { return nil }
52+ return try await transform ( value)
53+ } )
54+ }
55+ }
56+
57+ /// `compactMap` works by transforming the expression to a value that the given matcher uses.
58+ ///
59+ /// For example, you might only care that a particular property on a method equals some other value.
3260/// So, you could write `expect(myObject).to(compactMap({ $0 as? Int }, equal(3))`.
3361/// This is also useful in conjunction with ``satisfyAllOf`` to match against a converted type.
34- public func map < T, U> ( _ transform: @escaping ( T ) throws -> U ? , _ matcher: Matcher < U > ) -> Matcher < T > {
62+ public func compactMap < T, U> ( _ transform: @escaping ( T ) throws -> U ? , _ matcher: Matcher < U > ) -> Matcher < T > {
3563 Matcher { ( received: Expression < T > ) in
3664 let message = ExpectationMessage . expectedTo ( " Map from \( T . self) to \( U . self) " )
3765
@@ -47,12 +75,12 @@ public func map<T, U>(_ transform: @escaping (T) throws -> U?, _ matcher: Matche
4775 }
4876}
4977
50- /// `map ` works by transforming the expression to a value that the given matcher uses.
78+ /// `compactMap ` works by transforming the expression to a value that the given matcher uses.
5179///
5280/// For example, you might only care that a particular property on a method equals some other value.
5381/// So, you could write `expect(myObject).to(compactMap({ $0 as? Int }, equal(3))`.
5482/// This is also useful in conjunction with ``satisfyAllOf`` to match against a converted type.
55- public func map < T, U> ( _ transform: @escaping ( T ) async throws -> U ? , _ matcher: some AsyncableMatcher < U > ) -> AsyncMatcher < T > {
83+ public func compactMap < T, U> ( _ transform: @escaping ( T ) async throws -> U ? , _ matcher: some AsyncableMatcher < U > ) -> AsyncMatcher < T > {
5684 AsyncMatcher { ( received: AsyncExpression < T > ) in
5785 let message = ExpectationMessage . expectedTo ( " Map from \( T . self) to \( U . self) " )
5886
0 commit comments