Skip to content

Commit 96bf2e9

Browse files
committed
Improved IdentitySnapshot to eliminate the requirement of using BytesRepresentables constantly
1 parent 6becaac commit 96bf2e9

16 files changed

+59
-89
lines changed

Sources/SnapshotTestingCustomDump/CustomDump.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extension SyncSnapshot where Output == StringBytes {
2525
/// ```
2626
public static var customDump: SyncSnapshot<Input, Output> {
2727
IdentitySyncSnapshot<StringBytes>.lines.pullback {
28-
StringBytes(rawValue: String(customDumping: $0))
28+
String(customDumping: $0)
2929
}
3030
}
3131
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ extension AsyncSnapshot where Input: WKWebView, Output == ImageBytes {
5959
.image
6060
.pullback { webView in
6161
Async {
62-
let image = try await webView.takeSnapshot(with: nil)
63-
return ImageBytes(rawValue: image)
62+
try await webView.takeSnapshot(with: nil)
6463
}
6564
}
6665
}
@@ -80,8 +79,7 @@ extension AsyncSnapshot where Input: WKWebView, Output == ImageBytes {
8079
pathExtension: "png",
8180
attachmentGenerator: .image,
8281
executor: Async { webView in
83-
let image = try await webView.takeSnapshot(with: nil)
84-
return ImageBytes(rawValue: image)
82+
try await webView.takeSnapshot(with: nil)
8583
}
8684
)
8785
}

Sources/XCSnapshotTesting/Methods/Snapshot+Any.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ extension SyncSnapshot where Output == StringBytes {
1515
/// ```
1616
public static var description: SyncSnapshot<Input, Output> {
1717
IdentitySyncSnapshot.lines.pullback {
18-
StringBytes(rawValue: String(describing: $0))
18+
String(describing: $0)
1919
}
2020
}
2121
}
@@ -41,14 +41,12 @@ extension SyncSnapshot where Output == StringBytes {
4141
]
4242

4343
let snapshot = IdentitySyncSnapshot.lines.pullback { (data: Input) in
44-
try StringBytes(
45-
rawValue: String(
46-
decoding: JSONSerialization.data(
47-
withJSONObject: data,
48-
options: options
49-
),
50-
as: UTF8.self
51-
)
44+
try String(
45+
decoding: JSONSerialization.data(
46+
withJSONObject: data,
47+
options: options
48+
),
49+
as: UTF8.self
5250
)
5351
}
5452

Sources/XCSnapshotTesting/Methods/Snapshot+CaseIterable.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,9 @@ extension SyncSnapshot where Input: CaseIterable & Sendable, Output == StringByt
5252
}
5353
.sequence()
5454
.map {
55-
StringBytes(
56-
rawValue:
57-
$0
58-
.map { "\"\($0)\",\"\($1)\"" }
59-
.joined(separator: "\n")
60-
)
55+
$0
56+
.map { "\"\($0)\",\"\($1)\"" }
57+
.joined(separator: "\n")
6158
}
6259
.callAsFunction(f) {
6360
continuation.resume(with: $0)

Sources/XCSnapshotTesting/Methods/Snapshot+Encodable.swift

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ extension SyncSnapshot where Input: Encodable & Sendable, Output == StringBytes
2727
/// - Parameter encoder: A JSON encoder.
2828
public static func json(_ encoder: JSONEncoder) -> SyncSnapshot<Input, Output> {
2929
let snapshot = IdentitySyncSnapshot.lines.pullback { (encodable: Input) in
30-
try .init(
31-
rawValue: String(
32-
decoding: encoder.encode(encodable),
33-
as: UTF8.self
34-
)
30+
try String(
31+
decoding: encoder.encode(encodable),
32+
as: UTF8.self
3533
)
3634
}
3735

@@ -78,11 +76,9 @@ extension SyncSnapshot where Input: Encodable & Sendable, Output == StringBytes
7876
/// - Parameter encoder: A property list encoder.
7977
public static func plist(_ encoder: Foundation.PropertyListEncoder) -> SyncSnapshot<Input, Output> {
8078
let snapshot = IdentitySyncSnapshot.lines.pullback { (encodable: Input) in
81-
try .init(
82-
rawValue: String(
83-
decoding: encoder.encode(encodable),
84-
as: UTF8.self
85-
)
79+
try String(
80+
decoding: encoder.encode(encodable),
81+
as: UTF8.self
8682
)
8783
}
8884

Sources/XCSnapshotTesting/Methods/Snapshot+URLRequest.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ extension SyncSnapshot where Input == URLRequest, Output == StringBytes {
6161
} ?? []
6262
}
6363

64-
return .init(rawValue: ([method] + headers + body).joined(separator: "\n"))
64+
return ([method] + headers + body).joined(separator: "\n")
6565
}
6666
}
6767

@@ -119,7 +119,7 @@ extension SyncSnapshot where Input == URLRequest, Output == StringBytes {
119119
// URL
120120
components.append("\"\(request.url!.sortingQueryItems()!.absoluteString)\"")
121121

122-
return .init(rawValue: components.joined(separator: " \\\n\t"))
122+
return components.joined(separator: " \\\n\t")
123123
}
124124
}
125125
}

Sources/XCSnapshotTesting/Methods/UI/Core/ViewOperationPayload.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,11 @@ extension Async where Output == ViewOperationPayload {
4646
}
4747

4848
func snapshot(
49-
_ executor: Sync<ImageBytes, ImageBytes>
49+
_ executor: Sync<SDKImage, ImageBytes>
5050
) -> Async<Input, ImageBytes> {
5151
map { @MainActor payload in
5252
let image = try await executor(
53-
ImageBytes(
54-
rawValue: payload.input.snapshot()
55-
)
53+
payload.input.snapshot()
5654
)
5755

5856
payload.window.removeRootViewController()
@@ -78,14 +76,12 @@ extension Async where Output == ViewOperationPayload {
7876
}
7977

8078
func descriptor(
81-
_ executor: Sync<StringBytes, StringBytes>,
79+
_ executor: Sync<String, StringBytes>,
8280
method: SnapshotUIController.DescriptorMethod
8381
) -> Async<Input, StringBytes> {
8482
map { @MainActor payload in
8583
let string = try await executor(
86-
StringBytes(
87-
rawValue: payload.input.descriptor(method)
88-
)
84+
payload.input.descriptor(method)
8985
)
9086

9187
payload.window.removeRootViewController()

Sources/XCSnapshotTesting/Methods/UI/Snapshot+CALayer.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extension SyncSnapshot where Input: CALayer, Output == ImageBytes {
4040
layer.layoutIfNeeded()
4141
layer.render(in: context)
4242
image.unlockFocus()
43-
return .init(rawValue: image)
43+
return image
4444
}
4545
}
4646
}
@@ -72,13 +72,11 @@ extension SyncSnapshot where Input: CALayer, Output == ImageBytes {
7272
perceptualPrecision: perceptualPrecision
7373
).pullback { layer in
7474
let renderer = UIGraphicsImageRenderer(bounds: layer.bounds, format: .init(for: traits))
75-
return .init(
76-
rawValue: renderer.image { ctx in
77-
layer.setNeedsLayout()
78-
layer.layoutIfNeeded()
79-
layer.render(in: ctx.cgContext)
80-
}
81-
)
75+
return renderer.image { ctx in
76+
layer.setNeedsLayout()
77+
layer.layoutIfNeeded()
78+
layer.render(in: ctx.cgContext)
79+
}
8280
}
8381
}
8482
}

Sources/XCSnapshotTesting/Methods/UI/Snapshot+CGPath.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ extension SyncSnapshot where Input: CGPath, Output == ImageBytes {
5858
return true
5959
}
6060

61-
return .init(rawValue: image)
61+
return image
6262
}
6363
}
6464
}
@@ -96,13 +96,11 @@ extension SyncSnapshot where Input: CGPath, Output == ImageBytes {
9696
}
9797
format.scale = scale
9898
let renderer = UIGraphicsImageRenderer(bounds: bounds, format: format)
99-
return .init(
100-
rawValue: renderer.image { ctx in
101-
let cgContext = ctx.cgContext
102-
cgContext.addPath(path)
103-
cgContext.drawPath(using: drawingMode)
104-
}
105-
)
99+
return renderer.image { ctx in
100+
let cgContext = ctx.cgContext
101+
cgContext.addPath(path)
102+
cgContext.drawPath(using: drawingMode)
103+
}
106104
}
107105
}
108106
}
@@ -166,7 +164,7 @@ extension SyncSnapshot where Input: CGPath, Output == StringBytes {
166164
string += "\n"
167165
}
168166

169-
return .init(rawValue: string)
167+
return string
170168
}
171169
}
172170
}

Sources/XCSnapshotTesting/Methods/UI/Snapshot+NSBezierPath.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ extension SyncSnapshot where Input: NSBezierPath, Output == ImageBytes {
5151
return true
5252
}
5353

54-
return .init(rawValue: image)
54+
return image
5555
}
5656
}
5757
}
@@ -115,7 +115,7 @@ extension SyncSnapshot where Input: NSBezierPath, Output == StringBytes {
115115
string += "\n"
116116
}
117117

118-
return .init(rawValue: string)
118+
return string
119119
}
120120
}
121121
}

0 commit comments

Comments
 (0)