Skip to content

Commit 7398575

Browse files
committed
WIP: Updating documentation
1 parent ab70025 commit 7398575

22 files changed

+253
-257
lines changed

Sources/InlineSnapshotTesting/Documentation.docc/InlineSnapshotTesting.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Powerfully convenient snapshot testing.
88
alongside the test files. This makes for compact test cases with single line assertions...
99

1010
```swift
11-
assertSnapshot(of: value, as: .json)
11+
assert(of: value, as: .json)
1212
```
1313

1414
...but can make verification more cumbersome: one must find the corresponding file in order to
@@ -68,7 +68,10 @@ Re-run "testMySnapshot" to test against the newly-recorded snapshot.
6868

6969
### Essentials
7070

71-
- ``assertInline(of:as:message:record:timeout:closureDescriptor:matches:file:function:line:column:)``
71+
- ``assertInline(of:as:message:record:timeout:serialization:closureDescriptor:matches:fileID:file:function:line:column:)-(_,SyncSnapshot<Input, Output>,_,_,_,_,_,_,_,_,_,_,_)``
72+
- ``assertInline(of:as:message:record:timeout:serialization:closureDescriptor:matches:fileID:file:function:line:column:)-(_,AsyncSnapshot<Input, Output>,_,_,_,_,_,_,_,_,_,_,_)``
73+
- ``assertInline(of:as:message:record:timeout:name:serialization:closureDescriptor:matches:fileID:file:function:line:column:)-(_,SyncSnapshot<Input, Output>,_,_,_,_,_,_,_,_,_,_,_,_)``
74+
- ``assertInline(of:as:message:record:timeout:name:serialization:closureDescriptor:matches:fileID:file:function:line:column:)-(_,AsyncSnapshot<Input, Output>,_,_,_,_,_,_,_,_,_,_,_,_)``
7275

7376
### Writing a custom helper
7477

Sources/SnapshotTesting/Documentation.docc/Articles/CustomStrategies.md

Lines changed: 0 additions & 97 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# ``SnapshotTesting``
2+
3+
<!--@START_MENU_TOKEN@-->Summary<!--@END_MENU_TOKEN@-->
4+
5+
## Overview
6+
7+
<!--@START_MENU_TOKEN@-->Text<!--@END_MENU_TOKEN@-->
8+
9+
## Topics
10+
11+
### <!--@START_MENU_TOKEN@-->Group<!--@END_MENU_TOKEN@-->
12+
13+
- <!--@START_MENU_TOKEN@-->``Symbol``<!--@END_MENU_TOKEN@-->

Sources/SnapshotTesting/Documentation.docc/Extensions/AssertSnapshot.md

Lines changed: 0 additions & 19 deletions
This file was deleted.

Sources/SnapshotTesting/Documentation.docc/Extensions/Deprecations/SnapshotTestingDeprecations.md

Lines changed: 0 additions & 17 deletions
This file was deleted.

Sources/SnapshotTesting/Documentation.docc/Extensions/Deprecations/diffTool-property-deprecation.md

Lines changed: 0 additions & 6 deletions
This file was deleted.

Sources/SnapshotTesting/Documentation.docc/Extensions/Deprecations/isRecording-property-deprecation.md

Lines changed: 0 additions & 6 deletions
This file was deleted.

Sources/SnapshotTesting/Documentation.docc/Extensions/Snapshotting.md

Lines changed: 0 additions & 63 deletions
This file was deleted.

Sources/SnapshotTesting/Documentation.docc/Extensions/WithSnapshotTesting.md

Lines changed: 0 additions & 7 deletions
This file was deleted.

Sources/SnapshotTesting/Documentation.docc/SnapshotTesting.md

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# ``SnapshotTesting/Testing/Trait``
2+
3+
### Configuration
4+
5+
- ``Testing/Trait/record(_:)``
6+
- ``Testing/Trait/diffTool(_:)``
7+
- ``Testing/Trait/maxConcurrentTests(_:)``
8+
- ``Testing/Trait/platform(_:)``
9+
- ``Testing/Trait/finalizeSnapshots``
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Defining custom snapshot strategies
2+
3+
While XCSnapshotTesting comes with a wide variety of snapshot strategies, it can also be extended with
4+
custom, user-defined strategies using the ``XCSnapshotTesting/Snapshot`` and
5+
``XCSnapshotTesting/DiffAttachmentGenerator`` types.
6+
7+
## Snapshotting
8+
9+
The ``XCSnapshotTesting/Snapshot`` type represents the ability to transform a snapshottable value
10+
(like a view or data structure) into a diffable format (like an image or text).
11+
12+
### Transforming existing strategies
13+
14+
Existing strategies can be transformed to work with new types using the `pullback` method.
15+
16+
For example, given the following `image` strategy on `UIView`:
17+
18+
``` swift
19+
Snapshot<UIView, ImageBytes>.image
20+
```
21+
22+
We can define an `image` strategy on `UIViewController` using the `pullback` method:
23+
24+
``` swift
25+
extension AsyncSnapshot where Input: UIViewController, Output == ImageBytes {
26+
public static let image = AsyncSnapshot<UIView, ImageBytes>
27+
.image
28+
.pullback { viewController in viewController.view }
29+
}
30+
```
31+
32+
Pullback takes a transform function from the new strategy's value to the existing strategy's value,
33+
in this case `(UIViewController) -> UIView`.
34+
35+
### Creating brand new strategies
36+
37+
Most strategies can be built from existing ones, but if you've defined your own
38+
``XCSnapshotTesting/DiffAttachmentGenerator`` strategy, you may need to create a base ``XCSnapshotTesting/Snapshot``
39+
value alongside it.
40+
41+
### Asynchronous Strategies
42+
43+
Some types need to be snapshot in an asynchronous fashion. ``XCSnapshotTesting/Snapshot`` offers
44+
two APIs for building asynchronous strategies by utilizing a built-in ``Async`` type.
45+
46+
#### Async pullbacks
47+
48+
Alongside ``Snapshotting/pullback(_:)`` there is ``Snapshotting/asyncPullback(_:)``, which takes a
49+
transform function `(NewStrategyValue) -> Async<ExistingStrategyValue>`.
50+
51+
For example, WebKit's `WKWebView` offers a callback-based API for taking image snapshots, where the
52+
image is passed asynchronously to the callback block. While `pullback` would require the `UIImage`
53+
to be returned from the transform function, `pullback` and `Async` allow us to pass the `image`
54+
a value that can pass its callback along to the scope in which the image has been created.
55+
56+
``` swift
57+
extension AsyncSnapshot where Input: WKWebView, Output == ImageBytes {
58+
public static let image = AsyncSnapshot<UIImage, Output>
59+
.image
60+
.pullback { webView in
61+
Async {
62+
let image = try await webView.takeSnapshot(with: nil)
63+
return ImageBytes(rawValue: image)
64+
}
65+
}
66+
}
67+
```
68+
69+
#### Async initialization
70+
71+
`Snapshot` defines an alternate initializer to describe snapshot values in an asynchronous
72+
fashion.
73+
74+
For example, were we to define a strategy for `WKWebView` _without_
75+
``Snapshotting/asyncPullback(_:)``:
76+
77+
``` swift
78+
extension AsyncSnapshot where Input: WKWebView, Output == ImageBytes {
79+
public static let image = AsyncSnapshot(
80+
pathExtension: "png",
81+
attachmentGenerator: .image,
82+
executor: Async { webView in
83+
let image = try await webView.takeSnapshot(with: nil)
84+
return ImageBytes(rawValue: image)
85+
}
86+
)
87+
}
88+
```
89+
90+
## DiffAttachmentGenerator
91+
92+
The ``XCSnapshotTesting/DiffAttachmentGenerator`` type represents the ability to compare
93+
`Value`s and convert them to and from ``XCSnapshotTesting/DiffAttachment``.

0 commit comments

Comments
 (0)