-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathPrettyColorsTests.swift
380 lines (311 loc) · 9.68 KB
/
PrettyColorsTests.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import PrettyColors
import Foundation
import XCTest
class PrettyColorsTests: XCTestCase {
override func setUp() {
super.setUp()
}
override func tearDown() {
super.tearDown()
}
func test_basics() {
let redText: String = Color.Wrap(foreground: .Red).wrap("A red piece of text.")
println(redText)
Color.Wrap(foreground: .Yellow, style: .Bold)
Color.Wrap(foreground: .Green, background: .Black, style: .Bold, .Underlined)
// 8-bit (256) color support
Color.Wrap(foreground: 114)
Color.Wrap(foreground: 114, style: .Bold)
}
func test_problem_SingleStyleParameter() {
/*
As of `swift-600.0.57.3`, the following statement errors:
«Extra argument 'style' in call»
*/
// Color.Wrap(style: .Bold)
/*
Removing the supposedly "extra" argument 'style' errors:
«'().Type' does not have a member named 'Bold'»
*/
// Color.Wrap(.Bold)
/*
The true problem appears to be the ambiguity between the
two functions of the form `init(foreground:background:style:)`.
*/
// Workarounds:
Color.Wrap(foreground: nil as UInt8?, style: .Bold)
Color.Wrap(foreground: nil as Color.Named.Color?, style: .Bold)
[StyleParameter.Bold] as Color.Wrap
Color.Wrap(styles: .Bold)
// Multiple
Color.Wrap(styles: .Bold, .Blink)
}
func test_problem_TypeInference() {
// As of `swift-600.0.57.3`, this doesn't get type-inferred properly.
/*
Color.Wrap(
parameters: [
Color.Named(foreground: .Green),
Color.EightBit(foreground: 114),
StyleParameter.Bold
]
)
*/
// Workarounds:
Color.Wrap(
parameters: [
Color.Named(foreground: .Green),
Color.EightBit(foreground: 114),
StyleParameter.Bold
] as [Color.Wrap.Element]
)
Color.Wrap(
parameters: [
Color.Named(foreground: .Green),
Color.EightBit(foreground: 114),
StyleParameter.Bold
] as [Parameter]
)
[
Color.Named(foreground: .Green),
Color.EightBit(foreground: 114),
StyleParameter.Bold
] as Color.Wrap
}
func testImmutableFilterOrMap() {
let redBold = Color.Wrap(foreground: .Red, style: .Bold)
let redItalic = Color.Wrap(foreground: .Red, style: .Italic)
XCTAssert(
redBold == redItalic
.filter { $0 != StyleParameter.Italic }
+ [ StyleParameter.Bold ]
)
XCTAssert(
redBold == Color.Wrap(
parameters: redItalic
.map { (parameter: Parameter) in
return parameter == StyleParameter.Italic
? StyleParameter.Bold
: parameter
}
)
)
}
func testEmptyWrap() {
XCTAssert(
Color.Wrap(parameters: []).code.enable == "",
"Wrap with no parameters wrapping an empty string should return an empty SelectGraphicRendition."
)
XCTAssert(
Color.Wrap(parameters: []).wrap("") == "",
"Wrap with no parameters wrapping an empty string should return an empty string."
)
}
func testMulti() {
var multi = [
Color.EightBit(foreground: 227),
Color.Named(foreground: .Green, brightness: .NonBright)
] as Color.Wrap
XCTAssert(
multi.code.enable ==
ECMA48.controlSequenceIntroducer + "38;5;227" + ";" + "32" + "m"
)
XCTAssert(
multi.code.disable ==
ECMA48.controlSequenceIntroducer + "0" + "m"
)
}
func testLetWorkflow() {
let redOnBlack = Color.Wrap(foreground: .Red, background: .Black)
let boldRedOnBlack: Color.Wrap = redOnBlack + [ StyleParameter.Bold ] as Color.Wrap
XCTAssert(
boldRedOnBlack == Color.Wrap(foreground: .Red, background: .Black, style: .Bold)
)
XCTAssert(
[
boldRedOnBlack,
Color.Wrap(foreground: .Red, background: .Black, style: .Bold)
].reduce(true) {
(previous, value) in
return previous && value.parameters.reduce(true) {
(previous, value) in
let enable = value.code.enable
return previous && (
value == Color.Named(foreground: .Red) as Parameter ||
value == Color.Named(background: .Black) as Parameter ||
value == StyleParameter.Bold
)
}
} == true
)
}
func testAppendStyleParameter() {
let red = Color.Wrap(foreground: .Red)
let _ = { (wrap: Color.Wrap) -> Void in
var formerlyRed = wrap
formerlyRed.append(StyleParameter.Bold)
XCTAssert(
formerlyRed == Color.Wrap(foreground: .Red, style: .Bold)
)
}(red)
let _ = { (wrap: Color.Wrap) -> Void in
var formerlyRed = wrap
formerlyRed.append(style: .Bold)
XCTAssert(
formerlyRed == Color.Wrap(foreground: .Red, style: .Bold)
)
}(red)
XCTAssert(
red + Color.Wrap(styles: .Bold) == Color.Wrap(foreground: .Red, style: .Bold)
)
// Multiple
let _ = { (wrap: Color.Wrap) -> Void in
var formerlyRed = wrap
formerlyRed.append(StyleParameter.Bold)
formerlyRed.append(StyleParameter.Italic)
XCTAssert(
formerlyRed == Color.Wrap(foreground: .Red, style: .Bold, .Italic)
)
}(red)
let _ = { (wrap: Color.Wrap) -> Void in
var formerlyRed = wrap
formerlyRed.append(style: .Bold, .Italic)
XCTAssert(
formerlyRed == Color.Wrap(foreground: .Red, style: .Bold, .Italic)
)
}(red)
XCTAssert(
red + Color.Wrap(styles: .Bold, .Italic) == Color.Wrap(foreground: .Red, style: .Bold, .Italic)
)
}
func testMutableAppend() {
var formerlyRed = Color.Wrap(foreground: .Red)
let redBlackBackground = Color.Wrap(foreground: .Red, background: .Black)
formerlyRed.append( Color.Named(background: .Black) )
XCTAssert(
formerlyRed == redBlackBackground
)
}
//------------------------------------------------------------------------------
// MARK: - Foreground/Background
//------------------------------------------------------------------------------
func testSetForeground() {
var formerlyRed = Color.Wrap(foreground: .Red)
formerlyRed.foreground = Color.EightBit(foreground: 227) // A nice yellow
XCTAssert(
formerlyRed == Color.Wrap(foreground: 227)
)
}
func testSetForegroundToNil() {
var formerlyRed = Color.Wrap(foreground: .Red)
formerlyRed.foreground = nil
XCTAssert(
formerlyRed == Color.Wrap(foreground: nil as UInt8?)
)
}
func testSetForegroundToParameter() {
var formerlyRed = Color.Wrap(foreground: .Red)
formerlyRed.foreground = StyleParameter.Bold
XCTAssert( formerlyRed == [StyleParameter.Bold] as Color.Wrap )
}
func testTransformForeground() {
var formerlyRed = Color.Wrap(foreground: .Red)
formerlyRed.foreground { (color: ColorType) -> ColorType in
return Color.EightBit(foreground: 227) // A nice yellow
}
XCTAssert( formerlyRed == Color.Wrap(foreground: 227) )
}
func testTransformForeground2() {
var formerlyRed = Color.Wrap(foreground: 124)
formerlyRed.foreground { (var color: ColorType) -> ColorType in
if let color = color as? Color.EightBit {
var soonYellow = color
soonYellow.color += (227-124)
return soonYellow
} else { return color }
}
XCTAssert( formerlyRed == Color.Wrap(foreground: 227) )
}
func testTransformForegroundWithVar() {
var formerlyRed = Color.Wrap(foreground: .Red)
formerlyRed.foreground { (var color: ColorType) -> ColorType in
if let namedColor = color as? Color.Named {
var soonYellow = namedColor
soonYellow.color = .Yellow
return soonYellow
} else { return color }
}
XCTAssert( formerlyRed == Color.Wrap(foreground: .Yellow) )
}
func testTransformForegroundToBright() {
var formerlyRed = Color.Wrap(foreground: .Red)
formerlyRed.foreground { (var color: ColorType) -> ColorType in
var clone = color as Color.Named
clone.brightness.toggle()
return clone
}
let brightRed = [
Color.Named(foreground: .Red, brightness: .Bright)
] as Color.Wrap
XCTAssert( formerlyRed == brightRed )
}
func testComputedVariableForegroundEquality() {
XCTAssert(
Color.Named(foreground: .Red) == Color.Wrap(foreground: .Red).foreground! as Color.Named
)
}
func testEightBitForegroundBackgroundDifference() {
let one = Color.EightBit(foreground: 114)
let two = Color.EightBit(background: 114)
let difference = one.code.enable.reduce(
two.code.enable.reduce(0 as UInt8) { return $0.0 + $0.1 }
) { return $0.0 - $0.1 }
XCTAssert( difference == 10 )
}
func testNamedForegroundBackgroundDifference() {
let one = Color.Named(foreground: .Green)
let two = Color.Named(background: .Green)
let difference = one.code.enable.reduce(
two.code.enable.reduce(0 as UInt8) { return $0.0 + $0.1 }
) { return $0.0 - $0.1 }
XCTAssert( difference == 10 )
}
func testNamedBrightnessDifference() {
let one = Color.Named(foreground: .Green)
let two = Color.Named(foreground: .Green, brightness: .Bright)
let difference = one.code.enable.reduce(
two.code.enable.reduce(0 as UInt8) { return $0.0 + $0.1 }
) { return $0.0 - $0.1 }
XCTAssert( difference == 60 )
}
//------------------------------------------------------------------------------
// MARK: - Zap
//------------------------------------------------------------------------------
func testZapAllStyleParameters() {
let red = Color.Named(foreground: .Red)
let niceColor = Color.EightBit(foreground: 114)
let iterables: Array<Array<Parameter>> = [
[red],
[niceColor],
]
for parameters in iterables {
let wrap = Color.Wrap(parameters: parameters)
for i in stride(from: 1 as UInt8, through: 55, by: 1) {
if let parameter = StyleParameter(rawValue: i) {
for wrapAndSuffix in [
(wrap, "normal"),
(wrap + [ StyleParameter.Bold ] as Color.Wrap, "bold"),
(wrap + [ StyleParameter.Italic ] as Color.Wrap, "italic"),
(wrap + [ StyleParameter.Underlined ] as Color.Wrap, "underlined")
] {
let wrap = (wrapAndSuffix.0 + [parameter] as Color.Wrap)
let suffix = wrapAndSuffix.1
let string = "• " + wrap.wrap("__|øat·•ªº^∆©|__") +
" " + NSString(format: "%02d", i) + " + " + suffix
println(string)
}
}
}
}
}
}