-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGenericColorTests.swift
162 lines (138 loc) · 5.16 KB
/
GenericColorTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import XCTest
@testable import GenericColor
enum OSVersion {
case iOS, macOS, unknown
}
#if os(macOS)
import AppKit
let osVersion = OSVersion.macOS
#elseif os(iOS)
import UIKit
let osVersion = OSVersion.iOS
#else
let osVersion = OSVersion.unknown
#endif
final class GenericColorsTests: XCTestCase {
// test default init
var defaultColor: Color<RGB> = .init(
red: .byte(250),
green: .byte(104),
blue: .byte(120),
alpha: .byte(255)
)
var testableCollection: [(desc: String, color: Color<RGB>)] = []
override func setUp() {
var color = defaultColor
testableCollection.append(("{ r: 250, g: 104, b: 120, a: 1 }", color))
color = Color(rgb: 0xfa6878)! // color from 6-digit hex rgb
testableCollection.append(("Color(rgb: 0xfa6878)", color))
color = Color(rgba: 0xfa6878ff)! // color from 8-digit hex rgb
testableCollection.append(("Color(rgba: 0xfa6878ff)", color))
color = .hex(rgb: 0xfa6878)! // color from 6-digit hex rgb
testableCollection.append(("Color.hex(rgb: 0xfa6878)", color))
color = .hex(rgba: 0xfa6878ff)! // color from 8-digit hex rgb
testableCollection.append(("Color.hex(rgba: 0xfa6878ff)", color))
// case-insensitive string color initializers
let base = ["fa6878", "fa6878ff", "#fa6878", "#fa6878ff"] // use hash prefix if you want
let colorStrings = base + base.map{ $0.uppercased() }
testableCollection += colorStrings.map { ("Color(hex:\($0))", Color(hex: $0)!) }
testableCollection += colorStrings.map { ("Color.hex(\($0))", .hex($0)!) }
}
func testInitializers() {
// let literal: Color<RGB> = #colorLiteral(red: 0.9803921568627451, green: 0.40784313725490196, blue: 0.47058823529411764, alpha: 1)
// XCTAssertEqual(literal, defaultColor)
// test setup initializers
testableCollection.forEach { XCTAssertEqual(defaultColor, $0.color, $0.desc) }
// test other initializers
// color from 3-digit hex rgb
XCTAssertEqual(Color(hex: "abc"), Color(hex: "abcf"))
XCTAssertEqual(Color(hex: "abc"), Color(hex: "#abcf"))
XCTAssertEqual(Color(hex: "abc"), Color(hex: "aabbcc"))
XCTAssertEqual(Color(hex: "abc"), Color(hex: "#aabbcc"))
XCTAssertEqual(Color(hex: "abc"), Color(hex: "aabbccff"))
XCTAssertEqual(Color(hex: "abc"), Color(rgba: 0xaabbccff))
XCTAssertEqual(Color(hex: "#abc"), Color(hex: "ABC"))
XCTAssertEqual(Color(hex: "#abc"), Color(rgba: 0xAABBCCFF))
// 2 will be rounded to `.max` value
XCTAssertEqual(Color<RGB>(white: 2), Color(red: .max, green: .max, blue: .max))
}
func testRgbToHsb() {
let color = Color<HSB>(
hue: 0.9817351598173516,
saturation: 0.584,
brightness: 0.9803921568627453952,
alpha: 1
)
XCTAssertEqual(defaultColor.hsb, color.container)
XCTAssertEqual(defaultColor.map(\.hsb), color)
XCTAssertEqual(defaultColor.convert(), color)
}
func testHsbToRgb() {
let color = Color<HSB>(
hue: 0.9817351598173516,
saturation: 0.584,
brightness: 0.9803921568627453952,
alpha: 1.0
)
XCTAssertEqual(color.rgb, defaultColor.container)
XCTAssertEqual(color.map(\.rgb), defaultColor)
XCTAssertEqual(color.convert(), defaultColor)
}
func testRgbToCmyk() {
let color = Color<CMYK>(
cyan: .zero,
magenta: 0.584,
yellow: 0.52,
key: 0.01960784313725460992,
alpha: .max
)
XCTAssertEqual(defaultColor.cmyk, color.container)
XCTAssertEqual(defaultColor.map(\.cmyk), color)
XCTAssertEqual(defaultColor.convert(), color)
}
func testCmykToRgb() {
let color = Color<CMYK>(
cyan: .zero,
magenta: 0.584,
yellow: 0.52,
key: 0.01960784313725460992,
alpha: .max
)
XCTAssertEqual(color.rgb, defaultColor.container)
XCTAssertEqual(color.map(\.rgb), defaultColor)
XCTAssertEqual(color.convert(), defaultColor)
}
func testCoding() {
guard let data = try? JSONEncoder().encode(defaultColor)
else { return XCTFail("Could not encode color.") }
guard let color = try? JSONDecoder().decode(Color<RGB>.self, from: data)
else { return XCTFail("Could not decode color.") }
XCTAssertEqual(color, defaultColor)
}
func testHexStringRepresentation() {
let color1 = Color<RGB>.hex(rgb: 0x00aabb)!
let color2 = Color<RGB>.hex(rgba:0x000)!
let color3 = Color<RGB>.hex(rgb: 0xffffff)!
let color4 = Color<RGB>.hex(rgba: 0xffffffaa)!
XCTAssertEqual(color1.hex(uppercase: true, hashTagPrefix: true), "#00AABBFF")
XCTAssertEqual(color2.hex(uppercase: false, hashTagPrefix: false), "00000000")
XCTAssertEqual(color3.hex(uppercase: false, hashTagPrefix: true), "#ffffffff")
XCTAssertEqual(color4.hex(), "ffffffaa")
}
/// Should not couse initializer ambiguity
func testMacOSColorInit() throws {
try XCTSkipUnless(osVersion == .macOS, "MacOS only test")
#if os(macOS)
let genericColor = Color<RGB>()
_ = NSColor(genericColor)
#endif
}
/// Should not couse initializer ambiguity
func testIOSColorInit() throws {
try XCTSkipUnless(osVersion == .iOS, "iOS only test")
#if os(iOS)
let genericColor = Color<RGB>()
_ = UIColor(genericColor)
#endif
}
}