-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathColorTests.m
195 lines (133 loc) · 7.96 KB
/
ColorTests.m
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
//
// ColorTests.m
// KiwiHarnessTests
//
// Created by Tim on 07/09/2012.
// Copyright (c) 2012 Charismatic Megafauna Ltd. All rights reserved.
//
#import "Kiwi.h"
#import "UIColor+HexColors.h"
SPEC_BEGIN(ColorTests)
describe(@"The UIColor class", ^{
context(@"with a HexColor category", ^{
context(@"converting from hex to UIColor", ^{
__block float rComponent = 0.0f;
__block float gComponent = 0.0f;
__block float bComponent = 0.0f;
__block float aComponent = 0.0f;
it(@"should should respond to the colorWithHexString: method", ^{
[[UIColor should] respondToSelector:@selector(colorWithHexString:)];
});
it(@"should return nil when given a nil parameter", ^{
NSString *parameter = nil;
UIColor *testColor = [UIColor colorWithHexString:parameter];
[testColor shouldBeNil];
});
it(@"should return nil if given too few parameters", ^{
NSString *parameter = @"abcd";
UIColor *testColor = [UIColor colorWithHexString:parameter];
[testColor shouldBeNil];
});
it(@"should return nil if given too many parameters", ^{
NSString *parameter = @"abcdefgh";
UIColor *testColor = [UIColor colorWithHexString:parameter];
[testColor shouldBeNil];
});
it(@"should return nil if given non-hex numeric characters", ^{
NSString *parameter = @"abc&qr";
UIColor *testColor = [UIColor colorWithHexString:parameter];
[testColor shouldBeNil];
});
it(@"should return black when given the hex string for black (#000000)", ^{
NSString *parameter = @"000000";
UIColor *testColor = [UIColor colorWithHexString:parameter];
[testColor shouldNotBeNil];
[testColor getRed:&rComponent green:&gComponent blue:&bComponent alpha:&aComponent];
[[theValue(rComponent) should] equal:theValue(0.0f)];
[[theValue(gComponent) should] equal:theValue(0.0f)];
[[theValue(bComponent) should] equal:theValue(0.0f)];
[[theValue(aComponent) should] equal:theValue(1.0f)];
});
it(@"should return white when given the hex string for white (#ffffff)", ^{
NSString *parameter = @"ffffff";
UIColor *testColor = [UIColor colorWithHexString:parameter];
[testColor shouldNotBeNil];
[testColor getRed:&rComponent green:&gComponent blue:&bComponent alpha:&aComponent];
[[theValue(rComponent) should] equal:theValue(1.0f)];
[[theValue(gComponent) should] equal:theValue(1.0f)];
[[theValue(bComponent) should] equal:theValue(1.0f)];
[[theValue(aComponent) should] equal:theValue(1.0f)];
});
it(@"should return white when given an uppercase hex string for white (#FFffFf)", ^{
NSString *parameter = @"FFffFf";
UIColor *testColor = [UIColor colorWithHexString:parameter];
[testColor shouldNotBeNil];
[testColor getRed:&rComponent green:&gComponent blue:&bComponent alpha:&aComponent];
[[theValue(rComponent) should] equal:theValue(1.0f)];
[[theValue(gComponent) should] equal:theValue(1.0f)];
[[theValue(bComponent) should] equal:theValue(1.0f)];
[[theValue(aComponent) should] equal:theValue(1.0f)];
});
it(@"should return red when given the hex string for red (#ff0000)", ^{
NSString *parameter = @"ff0000";
UIColor *testColor = [UIColor colorWithHexString:parameter];
[testColor shouldNotBeNil];
[testColor getRed:&rComponent green:&gComponent blue:&bComponent alpha:&aComponent];
[[theValue(rComponent) should] equal:theValue(1.0f)];
[[theValue(gComponent) should] equal:theValue(0.0f)];
[[theValue(bComponent) should] equal:theValue(0.0f)];
[[theValue(aComponent) should] equal:theValue(1.0f)];
});
it(@"should return green when given the hex string for red (#007f00)", ^{
NSString *parameter = @"007f00";
UIColor *testColor = [UIColor colorWithHexString:parameter];
[testColor shouldNotBeNil];
[testColor getRed:&rComponent green:&gComponent blue:&bComponent alpha:&aComponent];
[[theValue(rComponent) should] equal:theValue(0.0f)];
[[theValue(gComponent) should] equal:theValue(0.5f)];
[[theValue(bComponent) should] equal:theValue(0.0f)];
[[theValue(aComponent) should] equal:theValue(1.0f)];
});
it(@"should return green when given the hex string for muddy green (#007f00)", ^{
NSString *parameter = @"007f00";
UIColor *testColor = [UIColor colorWithHexString:parameter];
[testColor shouldNotBeNil];
[testColor getRed:&rComponent green:&gComponent blue:&bComponent alpha:&aComponent];
[[theValue(rComponent) should] equal:theValue(0.0f)];
[[theValue(gComponent) should] equal:theValue(0.5f)];
[[theValue(bComponent) should] equal:theValue(0.0f)];
[[theValue(aComponent) should] equal:theValue(1.0f)];
});
});
context(@"converting from UIColor to hex", ^{
it(@"should respond to the 'hexValuesFromUIColor: method", ^{
[[UIColor should] respondToSelector:@selector(hexValuesFromUIColor:)];
});
it(@"should return nil if passed a nil color", ^{
[[UIColor hexValuesFromUIColor:nil] shouldBeNil];
});
it(@"should return '000000' if passed blackColor", ^{
[[[UIColor hexValuesFromUIColor:[UIColor blackColor]] should] equal:@"000000"];
});
it(@"should return 'ffffff' if passed whiteColor", ^{
[[[UIColor hexValuesFromUIColor:[UIColor whiteColor]] should] equal:@"ffffff"];
});
it(@"should return 'ff0000' if passed redColor", ^{
[[[UIColor hexValuesFromUIColor:[UIColor redColor]] should] equal:@"ff0000"];
});
it(@"should return 'ffff00' if passed 1.0 1.0 0.0 as RGB values", ^{
UIColor *testColor = [UIColor colorWithRed:1.0f green:1.0 blue:0 alpha:1.0];
[[[UIColor hexValuesFromUIColor:testColor] should] equal:@"ffff00"];
});
it(@"should return '00ff80' if passed 0.0 1.0 0.502 as RGB values", ^{
UIColor *testColor = [UIColor colorWithRed:0.000 green:1.000 blue:0.502 alpha:1.000];
[[[UIColor hexValuesFromUIColor:testColor] should] equal:@"00ff80"];
});
it(@"should return 'b5c5ff' if passed 0.711 0.773 1.000 as RGB values", ^{
UIColor *testColor = [UIColor colorWithRed:0.711 green:0.773 blue:1.000 alpha:1.000];
[[[UIColor hexValuesFromUIColor:testColor] should] equal:@"b5c5ff"];
});
});
});
});
SPEC_END