Skip to content

Commit 9b2c74a

Browse files
authored
Updated docs for pullback. (#155)
* Updated docs for pullback. * update * Update Sources/SnapshotTesting/Snapshotting.swift Co-Authored-By: mbrandonw <[email protected]> * Update Sources/SnapshotTesting/Snapshotting.swift Co-Authored-By: mbrandonw <[email protected]> * Update Sources/SnapshotTesting/Snapshotting.swift Co-Authored-By: mbrandonw <[email protected]> * Update Sources/SnapshotTesting/Snapshotting.swift Co-Authored-By: mbrandonw <[email protected]> * Update Sources/SnapshotTesting/Snapshotting.swift Co-Authored-By: mbrandonw <[email protected]> * update doc style
1 parent f73aa80 commit 9b2c74a

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

Sources/SnapshotTesting/Snapshotting.swift

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,31 @@ public struct Snapshotting<Value, Format> {
4646
}
4747
}
4848

49+
/// Transforms a strategy on `Value`s into a strategy on `NewValue`s through a function `(NewValue) -> Value`.
50+
///
51+
/// This is the most important operation for transforming existing strategies into new strategies. It allows you to transform a `Snapshotting<Value, Format>` into a `Snapshotting<NewValue, Format>` by pulling it back along a function `(NewValue) -> Value`. Notice that the function must go in in the direction `(NewValue) -> Value` even though we are transforming in the other direction `(Snapshotting<Value, Format>) -> Snapshotting<NewValue, Format>`.
52+
///
53+
/// A simple example of this is to `pullback` the snapshot strategy on `UIView`s to work on `UIViewController`s:
54+
///
55+
/// let strategy = Snapshotting<UIView, UIImage>.image.pullback { (vc: UIViewController) in
56+
/// return vc.view
57+
/// }
58+
///
59+
/// Here we took the strategy that snapshots `UIView`s as `UIImage`s and pulled it back to work on `UIViewController`s by using the function `(UIViewController) -> UIView` that simply plucks the view out of the controller.
60+
///
61+
/// Nearly every snapshot strategy provided in this library is a pullback of some base strategy, which shows just how important this operation is.
62+
///
63+
/// - Parameters:
64+
/// - transform: A transform function from `NewValue` into `Value`.
65+
/// - otherValue: A value to be transformed.
66+
public func pullback<NewValue>(_ transform: @escaping (_ otherValue: NewValue) -> Value) -> Snapshotting<NewValue, Format> {
67+
return self.asyncPullback { newValue in Async(value: transform(newValue)) }
68+
}
69+
4970
/// Transforms a strategy on `Value`s into a strategy on `NewValue`s through a function `(NewValue) -> Async<Value>`.
5071
///
72+
/// See the documention of `pullback` for a full description of how pullbacks works. This operation differs from `pullback` in that it allows you to use a transformation `(NewValue) -> Async<Value>`, which is necessary when your transformation needs to perform some asynchronous work.
73+
///
5174
/// - Parameters:
5275
/// - transform: A transform function from `NewValue` into `Async<Value>`.
5376
/// - otherValue: A value to be transformed.
@@ -67,15 +90,6 @@ public struct Snapshotting<Value, Format> {
6790
}
6891
}
6992
}
70-
71-
/// Transforms a strategy on `Value`s into a strategy on `NewValue`s through a function `(NewValue) -> Value`.
72-
///
73-
/// - Parameters:
74-
/// - transform: A transform function from `NewValue` into `Value`.
75-
/// - otherValue: A value to be transformed.
76-
public func pullback<NewValue>(_ transform: @escaping (_ otherValue: NewValue) -> Value) -> Snapshotting<NewValue, Format> {
77-
return self.asyncPullback { newValue in Async(value: transform(newValue)) }
78-
}
7993
}
8094

8195
/// A snapshot strategy where the type being snapshot is also a diffable type.

0 commit comments

Comments
 (0)