From 3535f3d088113cf24705014eec6a17f0fd73237f Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 20 Apr 2022 18:16:30 +0700 Subject: [PATCH] Add tests for #97 --- Tests/DefaultsTests/DefaultsColorTests.swift | 24 +++++++++++++++++++ .../DefaultsTests/DefaultsNSColorTests.swift | 12 ++++++++++ .../DefaultsTests/DefaultsUIColorTests.swift | 9 +++++++ 3 files changed, 45 insertions(+) create mode 100644 Tests/DefaultsTests/DefaultsColorTests.swift diff --git a/Tests/DefaultsTests/DefaultsColorTests.swift b/Tests/DefaultsTests/DefaultsColorTests.swift new file mode 100644 index 0000000..6852932 --- /dev/null +++ b/Tests/DefaultsTests/DefaultsColorTests.swift @@ -0,0 +1,24 @@ +import SwiftUI +import Defaults +import XCTest + +@available(iOS 15, tvOS 15, watchOS 8, *) +final class DefaultsColorTests: XCTestCase { + override func setUp() { + super.setUp() + Defaults.removeAll() + } + + override func tearDown() { + super.tearDown() + Defaults.removeAll() + } + + func testPreservesColorSpace() { + let fixture = Color(.displayP3, red: 1, green: 0.3, blue: 0.7, opacity: 1) + let key = Defaults.Key("independentColorPreservesColorSpaceKey") + Defaults[key] = fixture + XCTAssertEqual(Defaults[key]?.cgColor?.colorSpace, fixture.cgColor?.colorSpace) + XCTAssertEqual(Defaults[key]?.cgColor, fixture.cgColor) + } +} diff --git a/Tests/DefaultsTests/DefaultsNSColorTests.swift b/Tests/DefaultsTests/DefaultsNSColorTests.swift index 051d39f..49a8c05 100644 --- a/Tests/DefaultsTests/DefaultsNSColorTests.swift +++ b/Tests/DefaultsTests/DefaultsNSColorTests.swift @@ -1,3 +1,4 @@ +#if canImport(AppKit) import Foundation import Defaults import XCTest @@ -31,6 +32,16 @@ final class DefaultsNSColorTests: XCTestCase { XCTAssertTrue(Defaults[key].isEqual(fixtureColor1)) } + func testPreservesColorSpace() { + let fixture = NSColor(displayP3Red: 1, green: 0.3, blue: 0.7, alpha: 1) + let key = Defaults.Key("independentNSColorPreservesColorSpaceKey") + Defaults[key] = fixture + XCTAssertEqual(Defaults[key]?.colorSpace, fixture.colorSpace) + XCTAssertEqual(Defaults[key]?.cgColor.colorSpace, fixture.cgColor.colorSpace) + XCTAssertEqual(Defaults[key], fixture) + XCTAssertEqual(Defaults[key]?.cgColor, fixture.cgColor) + } + func testOptionalKey() { let key = Defaults.Key("independentNSColorOptionalKey") XCTAssertNil(Defaults[key]) @@ -304,3 +315,4 @@ final class DefaultsNSColorTests: XCTestCase { waitForExpectations(timeout: 10) } } +#endif diff --git a/Tests/DefaultsTests/DefaultsUIColorTests.swift b/Tests/DefaultsTests/DefaultsUIColorTests.swift index 0e615fd..d21a1a8 100644 --- a/Tests/DefaultsTests/DefaultsUIColorTests.swift +++ b/Tests/DefaultsTests/DefaultsUIColorTests.swift @@ -32,6 +32,15 @@ final class DefaultsNSColorTests: XCTestCase { XCTAssertTrue(Defaults[key].isEqual(fixtureColor1)) } + func testPreservesColorSpace() { + let fixture = UIColor(displayP3Red: 1, green: 0.3, blue: 0.7, alpha: 1) + let key = Defaults.Key("independentNSColorPreservesColorSpaceKey") + Defaults[key] = fixture + XCTAssertEqual(Defaults[key], fixture) + XCTAssertEqual(Defaults[key]?.cgColor.colorSpace, fixture.cgColor.colorSpace) + XCTAssertEqual(Defaults[key]?.cgColor, fixture.cgColor) + } + func testOptionalKey() { let key = Defaults.Key("independentNSColorOptionalKey") XCTAssertNil(Defaults[key])