Defaults/Tests/DefaultsTests/DefaultsNSColorTests.swift

144 lines
5.3 KiB
Swift
Raw Normal View History

#if os(macOS)
import Foundation
import AppKit
2024-09-26 05:17:26 +08:00
import Testing
import Defaults
private let suite_ = createSuite()
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)
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_)
}
2024-09-26 05:17:26 +08:00
@Suite(.serialized)
final class DefaultsNSColorTests {
init() {
Defaults.removeAll(suite: suite_)
}
2024-09-26 05:17:26 +08:00
deinit {
Defaults.removeAll(suite: suite_)
}
2024-09-26 05:17:26 +08:00
@Test
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))
Defaults[key] = fixtureColor1
2024-09-26 05:17:26 +08:00
#expect(Defaults[key].isEqual(fixtureColor1))
}
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
func testOptionalKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<NSColor?>("independentNSColorOptionalKey", suite: suite_)
#expect(Defaults[key] == nil)
Defaults[key] = fixtureColor
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]?.isEqual(fixtureColor) ?? false)
}
2024-09-26 05:17:26 +08:00
@Test
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))
Defaults[key].append(fixtureColor1)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key][1].isEqual(fixtureColor1))
}
2024-09-26 05:17:26 +08:00
@Test
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)
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)
}
2024-09-26 05:17:26 +08:00
@Test
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))
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))
}
2024-09-26 05:17:26 +08:00
@Test
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)
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)
}
2024-09-26 05:17:26 +08:00
@Test
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)
Defaults[key]["1"] = fixtureColor1
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]["1"]?.isEqual(fixtureColor1) ?? false)
}
2024-09-26 05:17:26 +08:00
@Test
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)
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)
}
2024-09-26 05:17:26 +08:00
@Test
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)
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)
}
2024-09-26 05:17:26 +08:00
@Test
func testType() {
2024-09-26 05:17:26 +08:00
#expect(Defaults[.color].isEqual(fixtureColor))
Defaults[.color] = fixtureColor1
2024-09-26 05:17:26 +08:00
#expect(Defaults[.color].isEqual(fixtureColor1))
}
2024-09-26 05:17:26 +08:00
@Test
func testArrayType() {
2024-09-26 05:17:26 +08:00
#expect(Defaults[.colorArray][0].isEqual(fixtureColor))
Defaults[.colorArray][0] = fixtureColor1
2024-09-26 05:17:26 +08:00
#expect(Defaults[.colorArray][0].isEqual(fixtureColor1))
}
2024-09-26 05:17:26 +08:00
@Test
func testDictionaryType() {
2024-09-26 05:17:26 +08:00
#expect(Defaults[.colorDictionary]["0"]?.isEqual(fixtureColor) ?? false)
Defaults[.colorDictionary]["0"] = fixtureColor1
2024-09-26 05:17:26 +08:00
#expect(Defaults[.colorDictionary]["0"]?.isEqual(fixtureColor1) ?? false)
}
}
2022-04-20 19:16:30 +08:00
#endif