Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic macOS support #3

Merged
merged 5 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 100 additions & 44 deletions Examples/PreviewSnapshotsTestApp.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,32 @@ import XCTest

final class PreviewSnapshotsTestAppTests: XCTestCase {
func test_simpleViewSnapshots() {
SimpleView_Previews.snapshots.assertSnapshots(as: .testStrategy())
SimpleView_Previews.snapshots.assertSnapshots(as: .testStrategy(), named: platformName)
}

func test_previewStateViewSnapshots() {
PreviewStateView_Previews.snapshots.assertSnapshots(as: .testStrategy())
PreviewStateView_Previews.snapshots.assertSnapshots(as: .testStrategy(), named: platformName)
}

func test_observableObjectSnapshots() {
ObservableObjectView_Previews.snapshots.assertSnapshots(as: .testStrategy())
ObservableObjectView_Previews.snapshots.assertSnapshots(as: .testStrategy(), named: platformName)
}

func test_previewStateLightAndDark() {
PreviewStateView_Previews.snapshots
.assertSnapshots(as: [
"Light": .testStrategy(userInterfaceStyle: .light),
"Dark": .testStrategy(userInterfaceStyle: .dark),
])
.assertSnapshots(
as: [
"Light": .testStrategy(userInterfaceStyle: .light),
"Dark": .testStrategy(userInterfaceStyle: .dark),
],
named: platformName
)
}
}

#if os(iOS)
let platformName = "iOS"

extension Snapshotting where Value: SwiftUI.View, Format == UIImage {
/// Shared image test strategy
static func testStrategy(userInterfaceStyle: UIUserInterfaceStyle = .light) -> Self {
Expand All @@ -54,3 +60,48 @@ extension Snapshotting where Value: SwiftUI.View, Format == UIImage {
)
}
}
#elseif os(macOS)
let platformName = "macOS"

extension Snapshotting where Value: SwiftUI.View, Format == NSImage {
/// Shared image test strategy
static func testStrategy(userInterfaceStyle: UserInterfaceStyle = .light) -> Self {
let snapshotting = Snapshotting<NSView, NSImage>.image(size: .init(width: 400, height: 400)).pullback { (view: Value) in
let view = NSHostingView(rootView: view.environment(\.colorScheme, userInterfaceStyle.colorScheme))
view.wantsLayer = true
view.layer?.backgroundColor = NSColor.windowBackgroundColor.cgColor
return view
}

return Snapshotting(
pathExtension: snapshotting.pathExtension,
diffing: snapshotting.diffing,
asyncSnapshot: { view in
Async { callback in
userInterfaceStyle.appearance.performAsCurrentDrawingAppearance {
snapshotting.snapshot(view).run(callback)
}
}
}
)
}
}

enum UserInterfaceStyle {
case light, dark

var colorScheme: ColorScheme {
switch self {
case .light: return .light
case .dark: return .dark
}
}

var appearance: NSAppearance {
switch self {
case .light: return NSAppearance(named: .aqua)!
case .dark: return NSAppearance(named: .darkAqua)!
}
}
}
#endif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ let package = Package(
platforms: [
.iOS(.v13),
.tvOS(.v13),
.macOS(.v10_15),
],
products: [
.library(
Expand Down
110 changes: 93 additions & 17 deletions Sources/PreviewSnapshotsTesting/PreviewSnapshots+assertSnapshots.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension PreviewSnapshots {
/// snapshots recorded on disk.
///
/// - Parameters:
/// - snapshotting: Snapshotting instance for `AnyView` into a `UIImage`.
/// - snapshotting: Strategy for serializing, deserializing, and comparing `AnyView`.
/// - name: An optional description of the snapshot to include with the configuration name.
/// - recording: Whether or not to record a new reference.
/// - file: The file in which failure occurred. Defaults to the file name of the test case in
Expand All @@ -29,8 +29,8 @@ extension PreviewSnapshots {
/// of the test case in which this function was called.
/// - line: The line number on which failure occurred. Defaults to the line number on which
/// this function was called.
public func assertSnapshots(
as snapshotting: Snapshotting<AnyView, UIImage> = .image,
public func assertSnapshots<Format>(
as snapshotting: Snapshotting<AnyView, Format>,
named name: String? = nil,
record recording: Bool = false,
file: StaticString = #file,
Expand Down Expand Up @@ -62,8 +62,8 @@ extension PreviewSnapshots {
/// of the test case in which this function was called.
/// - line: The line number on which failure occurred. Defaults to the line number on which
/// this function was called.
public func assertSnapshots(
as strategies: [String: Snapshotting<AnyView, UIImage>],
public func assertSnapshots<Format>(
as strategies: [String: Snapshotting<AnyView, Format>],
named name: String? = nil,
record recording: Bool = false,
file: StaticString = #file,
Expand Down Expand Up @@ -96,8 +96,8 @@ extension PreviewSnapshots {
/// of the test case in which this function was called.
/// - line: The line number on which failure occurred. Defaults to the line number on which
/// this function was called.
public func assertSnapshots(
as strategies: [Snapshotting<AnyView, UIImage>],
public func assertSnapshots<Format>(
as strategies: [Snapshotting<AnyView, Format>],
named name: String? = nil,
record recording: Bool = false,
file: StaticString = #file,
Expand Down Expand Up @@ -131,14 +131,14 @@ extension PreviewSnapshots {
///
/// ```swift
/// func test_snapshots() {
/// ContentView_Previews.snapshots.assertSnapshots {
/// ContentView_Previews.snapshots.assertSnapshots(as: .image) {
/// $0.border(.red)
/// }
/// }
/// ```
///
/// - Parameters:
/// - snapshotting: Snapshotting instance that converts an `AnyView` into a `UIImage`.
/// - snapshotting: Strategy for serializing, deserializing, and comparing `AnyView`.
/// - name: An optional description of the snapshot to include with the configuration name.
/// - recording: Whether or not to record a new reference.
/// - file: The file in which failure occurred. Defaults to the file name of the test case in
Expand All @@ -148,8 +148,8 @@ extension PreviewSnapshots {
/// - line: The line number on which failure occurred. Defaults to the line number on which
/// this function was called.
/// - modify: A closure to update the preview content before snapshotting.
public func assertSnapshots<Modified: View>(
as snapshotting: Snapshotting<Modified, UIImage> = .image,
public func assertSnapshots<Modified: View, Format>(
as snapshotting: Snapshotting<Modified, Format>,
named name: String? = nil,
record recording: Bool = false,
file: StaticString = #file,
Expand Down Expand Up @@ -178,7 +178,10 @@ extension PreviewSnapshots {
///
/// ```swift
/// func test_snapshots() {
/// ContentView_Previews.snapshots.assertSnapshots {
/// ContentView_Previews.snapshots.assertSnapshots(as: [
/// "Light": .image(traits: UITraitCollection(userInterfaceStyle: .light)),
/// "Dark": .image(traits: UITraitCollection(userInterfaceStyle: .dark)),
/// ] {
/// $0.border(.red)
/// }
/// }
Expand All @@ -196,8 +199,8 @@ extension PreviewSnapshots {
/// - line: The line number on which failure occurred. Defaults to the line number on which
/// this function was called.
/// - modify: A closure to update the preview content before snapshotting.
public func assertSnapshots<Modified: View>(
as strategies: [String: Snapshotting<AnyView, UIImage>],
public func assertSnapshots<Modified: View, Format>(
as strategies: [String: Snapshotting<AnyView, Format>],
named name: String? = nil,
record recording: Bool = false,
file: StaticString = #file,
Expand Down Expand Up @@ -228,7 +231,10 @@ extension PreviewSnapshots {
///
/// ```swift
/// func test_snapshots() {
/// ContentView_Previews.snapshots.assertSnapshots {
/// ContentView_Previews.snapshots.assertSnapshots(as: [
/// .image(layout: .fixed(width: 200, height: 200),
/// .image(layout: .fixed(width: 400, height: 400),
/// ]) {
/// $0.border(.red)
/// }
/// }
Expand All @@ -245,8 +251,8 @@ extension PreviewSnapshots {
/// - line: The line number on which failure occurred. Defaults to the line number on which
/// this function was called.
/// - modify: A closure to update the preview content before snapshotting.
public func assertSnapshots<Modified: View>(
as strategies: [Snapshotting<Modified, UIImage>],
public func assertSnapshots<Modified: View, Format>(
as strategies: [Snapshotting<Modified, Format>],
named name: String? = nil,
record recording: Bool = false,
file: StaticString = #file,
Expand All @@ -268,6 +274,76 @@ extension PreviewSnapshots {
}
}

#if os(iOS) || os(tvOS)
// MARK: - UIImage defaults

extension PreviewSnapshots {
/// Assert that all of the snapshots defined in a `PreviewSnapshots` collection match their
/// snapshots recorded on disk using the `Snapshotting<AnyView, UIImage>.image` strategy.
///
/// - Parameters:
/// - name: An optional description of the snapshot to include with the configuration name.
/// - recording: Whether or not to record a new reference.
/// - file: The file in which failure occurred. Defaults to the file name of the test case in
/// which this function was called.
/// - testName: The name of the test in which failure occurred. Defaults to the function name
/// of the test case in which this function was called.
/// - line: The line number on which failure occurred. Defaults to the line number on which
/// this function was called.
public func assertSnapshots(
named name: String? = nil,
record recording: Bool = false,
file: StaticString = #file,
testName: String = #function,
line: UInt = #line
) {
assertSnapshots(as: .image, named: name, record: recording, file: file, testName: testName, line: line)
}
}

// MARK: - PreviewSnapshots.assertSnapshots + modify

extension PreviewSnapshots {
/// Assert that all of the snapshots defined in a `PreviewSnapshots` collection match their
/// snapshots recorded on disk using the `Snapshotting<AnyView, UIImage>.image` strategy after
/// applying some modification.
///
/// `modify` can be used to update the configured view in a way that's useful for snapshotting,
/// but doesn't make sense for previews.
///
/// For example, adding a border to better visualize the edges of the view in snapshots:
///
/// ```swift
/// func test_snapshots() {
/// ContentView_Previews.snapshots.assertSnapshots {
/// $0.border(.red)
/// }
/// }
/// ```
///
/// - Parameters:
/// - name: An optional description of the snapshot to include with the configuration name.
/// - recording: Whether or not to record a new reference.
/// - file: The file in which failure occurred. Defaults to the file name of the test case in
/// which this function was called.
/// - testName: The name of the test in which failure occurred. Defaults to the function name
/// of the test case in which this function was called.
/// - line: The line number on which failure occurred. Defaults to the line number on which
/// this function was called.
/// - modify: A closure to update the preview content before snapshotting.
public func assertSnapshots<Modified: View>(
named name: String? = nil,
record recording: Bool = false,
file: StaticString = #file,
testName: String = #function,
line: UInt = #line,
modify: (AnyView) -> Modified
) {
assertSnapshots(as: .image, named: name, record: recording, file: file, testName: testName, line: line, modify: modify)
}
}
#endif

// MARK: Configuration name helper

private extension PreviewSnapshots.Configuration {
Expand Down
53 changes: 22 additions & 31 deletions Tests/PreviewSnapshotsTests/PreviewSnapshotsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,7 @@ final class PreviewSnapshotsTests: XCTestCase {
}
)

snapshots.assertSnapshots(as: .testStrategy)
}

/// PreviewSnapshots assertion using `named` parameter
func test_namedAssertion() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was just added in the last PR, but this test felt redundant now that every test is using the named argument

struct ContentView: View {
let message: String

var body: some View {
Text(message)
.font(.largeTitle)
.foregroundColor(.blue)
.padding(8)
}
}

let snapshots = PreviewSnapshots<String>(
configurations: [
.init(name: "Short Message", state: "Hello!"),
.init(name: "Long Message", state: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
],
configure: { message in
ContentView(message: message)
}
)

snapshots.assertSnapshots(as: .testStrategy, named: "Named Assertion")
snapshots.assertSnapshots(as: .testStrategy, named: frameworkName)
}

/// PreviewSnapshots with a tuple as the state
Expand Down Expand Up @@ -105,7 +79,7 @@ final class PreviewSnapshotsTests: XCTestCase {
}
)

snapshots.assertSnapshots(as: .testStrategy)
snapshots.assertSnapshots(as: .testStrategy, named: frameworkName)
}

/// PreviewSnapshots with an ObservableObject as the state
Expand Down Expand Up @@ -144,7 +118,7 @@ final class PreviewSnapshotsTests: XCTestCase {
}
)

snapshots.assertSnapshots(as: .testStrategy)
snapshots.assertSnapshots(as: .testStrategy, named: frameworkName)
}

/// PreviewSnapshots using a NamedPreviewState as the state
Expand Down Expand Up @@ -193,7 +167,7 @@ final class PreviewSnapshotsTests: XCTestCase {
}
)

snapshots.assertSnapshots(as: .testStrategy)
snapshots.assertSnapshots(as: .testStrategy, named: frameworkName)
}

/// PreviewSnapshots using assertSnapshots's modify function
Expand All @@ -218,11 +192,13 @@ final class PreviewSnapshotsTests: XCTestCase {
}
)

snapshots.assertSnapshots(as: .testStrategy) { view in
snapshots.assertSnapshots(as: .testStrategy, named: frameworkName) { view in
view.border(Color.blue)
}
}
}
#if os(iOS) || os(tvOS)
let frameworkName = "UIKit"

extension Snapshotting where Value: SwiftUI.View, Format == UIImage {
/// Shared image test strategy
Expand All @@ -233,3 +209,18 @@ extension Snapshotting where Value: SwiftUI.View, Format == UIImage {
)
}
}
#elseif os(macOS)
let frameworkName = "AppKit"

extension Snapshotting where Value: SwiftUI.View, Format == NSImage {
/// Shared image test strategy
static var testStrategy: Self {
Snapshotting<NSView, NSImage>.image(size: .init(width: 400, height: 400)).pullback { view in
let view = NSHostingView(rootView: view)
view.wantsLayer = true
view.layer?.backgroundColor = NSColor.windowBackgroundColor.cgColor
return view
}
}
}
#endif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Diff not rendered.
Diff not rendered.