2023-10-18 17:56:02 +08:00
|
|
|
#if os(macOS)
|
2021-05-16 20:21:17 +08:00
|
|
|
import Foundation
|
|
|
|
import AppKit
|
2024-09-26 05:17:26 +08:00
|
|
|
import Testing
|
|
|
|
import Defaults
|
|
|
|
|
|
|
|
private let suite_ = createSuite()
|
2021-05-16 20:21:17 +08:00
|
|
|
|
2024-01-18 17:58:06 +08:00
|
|
|
private let fixtureColor = NSColor(red: Double(103) / Double(0xFF), green: Double(132) / Double(0xFF), blue: Double(255) / Double(0xFF), alpha: 1)
|
|
|
|
private let fixtureColor1 = NSColor(red: Double(255) / Double(0xFF), green: Double(241) / Double(0xFF), blue: Double(180) / Double(0xFF), alpha: 1)
|
|
|
|
private let fixtureColor2 = NSColor(red: Double(255) / Double(0xFF), green: Double(180) / Double(0xFF), blue: Double(194) / Double(0xFF), alpha: 1)
|
2021-05-16 20:21:17 +08:00
|
|
|
|
|
|
|
extension Defaults.Keys {
|
2024-09-26 05:17:26 +08:00
|
|
|
fileprivate static let color = Defaults.Key<NSColor>("NSColor", default: fixtureColor, suite: suite_)
|
|
|
|
fileprivate static let colorArray = Defaults.Key<[NSColor]>("NSColorArray", default: [fixtureColor], suite: suite_)
|
|
|
|
fileprivate static let colorDictionary = Defaults.Key<[String: NSColor]>("NSColorArray", default: ["0": fixtureColor], suite: suite_)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Suite(.serialized)
|
|
|
|
final class DefaultsNSColorTests {
|
|
|
|
init() {
|
|
|
|
Defaults.removeAll(suite: suite_)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
deinit {
|
|
|
|
Defaults.removeAll(suite: suite_)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testKey() {
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<NSColor>("independentNSColorKey", default: fixtureColor, suite: suite_)
|
|
|
|
#expect(Defaults[key].isEqual(fixtureColor))
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key] = fixtureColor1
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key].isEqual(fixtureColor1))
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2022-04-20 19:16:30 +08:00
|
|
|
func testPreservesColorSpace() {
|
|
|
|
let fixture = NSColor(displayP3Red: 1, green: 0.3, blue: 0.7, alpha: 1)
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<NSColor?>("independentNSColorPreservesColorSpaceKey", suite: suite_)
|
2022-04-20 19:16:30 +08:00
|
|
|
Defaults[key] = fixture
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key]?.colorSpace == fixture.colorSpace)
|
|
|
|
#expect(Defaults[key]?.cgColor.colorSpace == fixture.cgColor.colorSpace)
|
|
|
|
#expect(Defaults[key] == fixture)
|
|
|
|
#expect(Defaults[key]?.cgColor == fixture.cgColor)
|
2022-04-20 19:16:30 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testOptionalKey() {
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<NSColor?>("independentNSColorOptionalKey", suite: suite_)
|
|
|
|
#expect(Defaults[key] == nil)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key] = fixtureColor
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key]?.isEqual(fixtureColor) ?? false)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testArrayKey() {
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<[NSColor]>("independentNSColorArrayKey", default: [fixtureColor], suite: suite_)
|
|
|
|
#expect(Defaults[key][0].isEqual(fixtureColor))
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key].append(fixtureColor1)
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key][1].isEqual(fixtureColor1))
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testArrayOptionalKey() {
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<[NSColor]?>("independentNSColorOptionalKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
|
|
|
|
#expect(Defaults[key] == nil)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key] = [fixtureColor]
|
|
|
|
Defaults[key]?.append(fixtureColor1)
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key]?[0].isEqual(fixtureColor) ?? false)
|
|
|
|
#expect(Defaults[key]?[1].isEqual(fixtureColor1) ?? false)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testNestedArrayKey() {
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<[[NSColor]]>("independentNSColorNestedArrayKey", default: [[fixtureColor]], suite: suite_)
|
|
|
|
#expect(Defaults[key][0][0].isEqual(fixtureColor))
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key][0].append(fixtureColor1)
|
|
|
|
Defaults[key].append([fixtureColor2])
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key][0][1].isEqual(fixtureColor1))
|
|
|
|
#expect(Defaults[key][1][0].isEqual(fixtureColor2))
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testArrayDictionaryKey() {
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<[[String: NSColor]]>("independentNSColorArrayDictionaryKey", default: [["0": fixtureColor]], suite: suite_)
|
|
|
|
#expect(Defaults[key][0]["0"]?.isEqual(fixtureColor) ?? false)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key][0]["1"] = fixtureColor1
|
|
|
|
Defaults[key].append(["0": fixtureColor2])
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key][0]["1"]?.isEqual(fixtureColor1) ?? false)
|
|
|
|
#expect(Defaults[key][1]["0"]?.isEqual(fixtureColor2) ?? false)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testDictionaryKey() {
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<[String: NSColor]>("independentNSColorDictionaryKey", default: ["0": fixtureColor], suite: suite_)
|
|
|
|
#expect(Defaults[key]["0"]?.isEqual(fixtureColor) ?? false)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key]["1"] = fixtureColor1
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key]["1"]?.isEqual(fixtureColor1) ?? false)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testDictionaryOptionalKey() {
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<[String: NSColor]?>("independentNSColorDictionaryOptionalKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
|
|
|
|
#expect(Defaults[key] == nil)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key] = ["0": fixtureColor]
|
|
|
|
Defaults[key]?["1"] = fixtureColor1
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key]?["0"]?.isEqual(fixtureColor) ?? false)
|
|
|
|
#expect(Defaults[key]?["1"]?.isEqual(fixtureColor1) ?? false)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testDictionaryArrayKey() {
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<[String: [NSColor]]>("independentNSColorDictionaryArrayKey", default: ["0": [fixtureColor]], suite: suite_)
|
|
|
|
#expect(Defaults[key]["0"]?[0].isEqual(fixtureColor) ?? false)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key]["0"]?.append(fixtureColor1)
|
|
|
|
Defaults[key]["1"] = [fixtureColor2]
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key]["0"]?[1].isEqual(fixtureColor1) ?? false)
|
|
|
|
#expect(Defaults[key]["1"]?[0].isEqual(fixtureColor2) ?? false)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testType() {
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[.color].isEqual(fixtureColor))
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[.color] = fixtureColor1
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[.color].isEqual(fixtureColor1))
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testArrayType() {
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[.colorArray][0].isEqual(fixtureColor))
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[.colorArray][0] = fixtureColor1
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[.colorArray][0].isEqual(fixtureColor1))
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testDictionaryType() {
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[.colorDictionary]["0"]?.isEqual(fixtureColor) ?? false)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[.colorDictionary]["0"] = fixtureColor1
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[.colorDictionary]["0"]?.isEqual(fixtureColor1) ?? false)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
}
|
2022-04-20 19:16:30 +08:00
|
|
|
#endif
|