From 15a87414676fa45a339493d3405234ec286473a2 Mon Sep 17 00:00:00 2001 From: Marc Schultz Date: Sat, 15 Jun 2024 00:28:43 +0200 Subject: [PATCH] Test image scaling --- .../SnapshotTestingTests.swift | 12 +++++++ Tests/SnapshotTestingTests/TestHelpers.swift | 31 ++++++++++++++++++ .../testSnapshotUnscaledByDefault.1.png | Bin 0 -> 2335 bytes 3 files changed, 43 insertions(+) create mode 100644 Tests/SnapshotTestingTests/__Snapshots__/SnapshotTestingTests/testSnapshotUnscaledByDefault.1.png diff --git a/Tests/SnapshotTestingTests/SnapshotTestingTests.swift b/Tests/SnapshotTestingTests/SnapshotTestingTests.swift index 4a5df06c0..4a42d7b9f 100644 --- a/Tests/SnapshotTestingTests/SnapshotTestingTests.swift +++ b/Tests/SnapshotTestingTests/SnapshotTestingTests.swift @@ -1133,6 +1133,18 @@ final class SnapshotTestingTests: XCTestCase { let firstLine = message?.split(whereSeparator: \.isNewline).first XCTAssertEqual(firstLine, "[\(platform)] Snapshot size (123.0, 123.0) is unequal to expected size (100.0, 100.0)") } + + func testSnapshotUnscaledByDefault() throws { + let size = CGSize(width: 100, height: 100) + let view = TestView(size: size) + view.backgroundColor = .cyan + assertSnapshot(of: view, as: .image) + let url = URL(fileURLWithPath: String(#file), isDirectory: false) + .deletingLastPathComponent() + .appendingPathComponent("__Snapshots__/SnapshotTestingTests/testSnapshotUnscaledByDefault.1.png") + let image = try XCTUnwrap(XImage(contentsOf: url)) + XCTAssertEqual(image.size, size) + } #endif #if os(iOS) diff --git a/Tests/SnapshotTestingTests/TestHelpers.swift b/Tests/SnapshotTestingTests/TestHelpers.swift index f708c40eb..3ee99ff41 100644 --- a/Tests/SnapshotTestingTests/TestHelpers.swift +++ b/Tests/SnapshotTestingTests/TestHelpers.swift @@ -110,3 +110,34 @@ let osVersion = ProcessInfo.processInfo.operatingSystemVersion } } #endif + +#if os(iOS) || os(macOS) || os(tvOS) + extension XImage { + convenience init?(contentsOf url: URL) { + #if os(iOS) || os(tvOS) + self.init(contentsOfFile: url.path) + #elseif os(macOS) + self.init(byReferencing: url) + #endif + } + } +#endif + +class TestView: XView { + #if os(macOS) + var backgroundColor: NSColor? { + get { + guard let cgColor = layer?.backgroundColor else { return nil } + return NSColor(cgColor: cgColor) + } + set { + wantsLayer = true + layer?.backgroundColor = newValue?.cgColor + } + } + #endif + + convenience init(size: CGSize) { + self.init(frame: .init(origin: .zero, size: size)) + } +} diff --git a/Tests/SnapshotTestingTests/__Snapshots__/SnapshotTestingTests/testSnapshotUnscaledByDefault.1.png b/Tests/SnapshotTestingTests/__Snapshots__/SnapshotTestingTests/testSnapshotUnscaledByDefault.1.png new file mode 100644 index 0000000000000000000000000000000000000000..af261e5fb665bcebdab1506bfbe20eda42412567 GIT binary patch literal 2335 zcmeAS@N?(olHy`uVBq!ia0vp^CqS5k4M?tyST~P>frT&A**PHF+1)umC%-7TATc@B zJvA@2C^I?8-HCz0qPI6RHbW*<#BTP^&DtvGny)NdHi_?_>69%wK^I$Yz4({9Iromv z>@{a7ir|a#mmh6D{_MyRtCEAY z7kFzg=xX;()taitDE;`*{#$R~D;&Fb{oD5M|I=Tu3vg%bxZ8STjm0UYN2v_|M2~KY zZD4WyeS-6P(htrf);V(&9tj=Z$`CFwkN?A_^D`$Jb;P^sXsqh_cjR^l`*Y=QQ7r7m zA8z;An66l>sdU=)q}l3Oe#)2kHvg{Q=6W~#$n9qT{>=upBKMhhe4DOSH0@!#!2HlB zcS<_e>t${}D)2vbXC~)IWro<@ds>UQKH42g4z1oJ^P*(sD)Wm6w3lC*nJ#d@xuW!- z&8cJWT>fk|jGx0*S{F2j=NZ2o!-Gd!x*Mv#E6Z*0&D82R-5Z`^cKlPvj~&m%(wJ)B z>858|UB6(tG_A`~=Uw;XPl92~?T&=6oHyO+Z@lhykz=a1ZEPUd|=;B4go;r5Ou9 zr)B?@KU`RK=zT`0@*bYL%8b>e7dX=jl&=_i>U&OV{vo3@`H6v~;$nqK>L-I!dQvW0 zdGVPZpQcot@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