Skip to content

Commit

Permalink
Simplify snapshot function
Browse files Browse the repository at this point in the history
  • Loading branch information
dogo committed Dec 12, 2023
1 parent 201e7dc commit 9a7ace6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ Pods/
Carthage/Checkouts
Carthage/Build

## Snapshot Failures
FailureDiffs

## Tuist
Tuist/Dependencies/graph.json
Tuist/Dependencies/Carthage
Expand Down
19 changes: 19 additions & 0 deletions SWDestinyTradesTests/Snapshot/SnapshotComparable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@ import iOSSnapshotTestCaseCore
import UIKit
import XCTest

@objc
protocol Snapshotable {
var snapshotObject: UIView? { get }
}

extension UIViewController: Snapshotable {
var snapshotObject: UIView? {
beginAppearanceTransition(true, animated: false)
endAppearanceTransition()
return view
}
}

extension UIView: Snapshotable {
var snapshotObject: UIView? {
return self
}
}

public extension FBSnapshotTestCase {

// swiftlint:disable:next function_parameter_count
Expand Down
35 changes: 5 additions & 30 deletions SWDestinyTradesTests/Snapshot/SnapshotableTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,18 @@ enum SnapshotTestMode {

class SnapshotableTestCase: FBSnapshotTestCase {

func snapshot(_ viewOrLayer: AnyObject,
func snapshot(_ instance: Snapshotable,
named: String? = nil,
testMode: SnapshotTestMode = .test,
perPixelTolerance: CGFloat = 0,
overallTolerance: CGFloat = 0,
file: StaticString = #file) -> Bool {
if viewOrLayer.isKind(of: UIView.self), let view = viewOrLayer as? UIView {
return verifySnapshot(view,
snapshotName: named,
testMode: testMode,
perPixelTolerance: perPixelTolerance,
overallTolerance: overallTolerance,
file: file)
} else if viewOrLayer.isKind(of: UIViewController.self), let viewController = viewOrLayer as? UIViewController {
viewController.beginAppearanceTransition(true, animated: false)
viewController.endAppearanceTransition()
return verifySnapshot(viewController.view,
snapshotName: named,
testMode: testMode,
perPixelTolerance: perPixelTolerance,
overallTolerance: overallTolerance,
file: file)
} else {
assertionFailure("Only UIView and CALayer classes can be snapshotted")
return false
guard let snapshotObject = instance.snapshotObject else {
fatalError("Failed unwrapping Snapshot Object")
}
}

// swiftlint:disable:next function_parameter_count
private func verifySnapshot(_ viewOrLayer: AnyObject,
snapshotName: String?,
testMode: SnapshotTestMode,
perPixelTolerance: CGFloat,
overallTolerance: CGFloat,
file: StaticString) -> Bool {
let sanitizedName = sanitizedTestName(snapshotName)
return FBSnapshotTestCase.validateSnapshot(viewOrLayer,
let sanitizedName = sanitizedTestName(named)
return FBSnapshotTestCase.validateSnapshot(snapshotObject,
snapshot: sanitizedName,
record: testMode == .record,
referenceDirectory: getDefaultReferenceDirectory(file),
Expand Down

0 comments on commit 9a7ace6

Please sign in to comment.