Skip to content

Commit

Permalink
replace throws with failures
Browse files Browse the repository at this point in the history
fix: #7
  • Loading branch information
paulz committed May 22, 2022
1 parent 4e272c9 commit b31d59b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
6 changes: 3 additions & 3 deletions Example/ApplicationTests/SnapshotTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import SwiftUI

class SnapshotTests: XCTestCase {
func testViews() throws {
try verifySnapshot(FavoriteView_Previews.self)
try verifySnapshot(ContentView())
try verifySnapshot(Text("SwiftUI").foregroundColor(.red), "example")
verifySnapshot(FavoriteView_Previews.self)
verifySnapshot(ContentView())
verifySnapshot(Text("SwiftUI").foregroundColor(.red), "example")
}
}
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ Add Swift Package to a project Test target

See [Example iOS app](https://github.com/paulz/SwiftUI-snapshot-testing/tree/main/Example) project with [SnapshotTests.swift](https://github.com/paulz/SwiftUI-snapshot-testing/blob/main/Example/ApplicationTests/SnapshotTests.swift)

func testViews() throws {
try verifySnapshot(FavoriteView_Previews.self)
try verifySnapshot(ContentView())
try verifySnapshot(Text("SwiftUI").foregroundColor(.red), "example")
func testViews() {
verifySnapshot(FavoriteView_Previews.self)
verifySnapshot(ContentView())
verifySnapshot(Text("SwiftUI").foregroundColor(.red), "example")
}
34 changes: 21 additions & 13 deletions Sources/verifySnapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,40 @@ import SwiftUI
import UniformTypeIdentifiers

public func verifySnapshot<P>(_ preview: P.Type = P.self, _ name: String? = nil, colorAccuracy: Float = 0.02,
file: StaticString = #filePath, line: UInt = #line) throws where P: PreviewProvider {
file: StaticString = #filePath, line: UInt = #line) where P: PreviewProvider {
var name = name ?? "\(P.self)"
let commonPreviewSuffix = "_Previews"
if name.hasSuffix(commonPreviewSuffix) {
name.removeLast(commonPreviewSuffix.count)
}
try verifySnapshot(preview.previews, name, colorAccuracy: colorAccuracy, file: file, line: line)
verifySnapshot(preview.previews, name, colorAccuracy: colorAccuracy, file: file, line: line)
}

public func verifySnapshot<V: View>(_ view: V, _ name: String? = nil, colorAccuracy: Float = 0.02,
file: StaticString = #filePath, line: UInt = #line) throws {
let image = try inWindowView(view) {
file: StaticString = #filePath, line: UInt = #line) {
guard let image = try? inWindowView(view, block: {
$0.renderLayerAsBitmap()
}) else {
XCTFail("failed to get snapshot of view")
return
}
let isRunningOnCI = ProcessInfo.processInfo.environment.keys.contains("CI")
let shouldOverwriteExpected = !isRunningOnCI
let pngData = try XCTUnwrap(image.pngData())
guard let pngData = image.pngData() else {
XCTFail("failed to get image data")
return
}
let fileName = (name ?? "\(V.self)") + ".png"
let url = folderUrl(String(describing: file)).appendingPathComponent(fileName)
if let expectedData = try? Data(contentsOf: url) {
let expectedImage = try XCTUnwrap(UIImage(data: expectedData))
try XCTContext.runActivity(named: "compare images") {
if let expectedData = try? Data(contentsOf: url), let expectedImage = UIImage(data: expectedData) {
XCTContext.runActivity(named: "compare images") {
let actualImage = XCTAttachment(data: pngData, uniformTypeIdentifier: UTType.png.identifier)
actualImage.name = "actual image"
$0.add(actualImage)
let diff = compare(image, expectedImage)
if diff.maxColorDifference() > colorAccuracy {
if shouldOverwriteExpected {
try pngData.write(to: url)
XCTAssertNoThrow({try pngData.write(to: url)}, "failed to overwrite snapshot")
}
XCTFail(
"""
Expand All @@ -44,16 +49,19 @@ public func verifySnapshot<V: View>(_ view: V, _ name: String? = nil, colorAccur
)
}
let ciImage = diff.difference
let diffImage = CIContext().createCGImage(ciImage, from: ciImage.extent)
let diffAttachment = XCTAttachment(image: UIImage(cgImage: try XCTUnwrap(diffImage)))
guard let diffImage = CIContext().createCGImage(ciImage, from: ciImage.extent) else {
XCTFail("failed to get image of difference")
return
}
let diffAttachment = XCTAttachment(image: UIImage(cgImage: diffImage))
diffAttachment.name = "difference"
$0.add(diffAttachment)
}
} else {
if shouldOverwriteExpected {
try XCTContext.runActivity(named: "recording missing snapshot") {
XCTContext.runActivity(named: "recording missing snapshot") {
$0.add(.init(data: pngData, uniformTypeIdentifier: UTType.png.identifier))
try pngData.write(to: url)
XCTAssertNoThrow({try pngData.write(to: url)}, "failed to record snapshot")
}
XCTFail("was missing snapshot: \(fileName), now recorded", file: file, line: line)
} else {
Expand Down
4 changes: 2 additions & 2 deletions Tests/SampleViewTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import SwiftUI
class SampleViewTest: XCTestCase {
let expectedSize = CGSize(width: 30, height: 20)

func testSwiftUIRendersInWindow() throws {
try verifySnapshot(SampleView())
func testSwiftUIRendersInWindow() {
verifySnapshot(SampleView())
}

func testImageSizeIsScaledFromExpected() throws {
Expand Down

0 comments on commit b31d59b

Please sign in to comment.