/
test-color.R
102 lines (84 loc) 路 3.06 KB
/
test-color.R
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
# test_that()
describe("prepare_colors()", {
it("returns NULL if NULL or missing", {
expect_null(prepare_colors())
expect_null(prepare_colors(NULL))
expect_null(prepare_colors(list()))
})
it("requires a named vector or list", {
expect_error(prepare_colors("#00FF00"))
})
it("requires valid CSS names", {
expect_error(prepare_colors(c("light blue" = "#88f")))
expect_warning(prepare_colors(c("light/blue" = "#88f")), "light/blue")
expect_warning(prepare_colors(c("-lightblue" = "#88f")), "-lightblue")
expect_warning(prepare_colors(c("_lightblue" = "#88f")), "_lightblue")
colors <- c("light_blue" = "#88f", "light-blue" = "#88f")
expect_silent(
expect_equal(
prepare_colors(colors),
list(
list(color_name = "light_blue", value = "#88f"),
list(color_name = "light-blue", value = "#88f")
)
)
)
})
it("returns list with color_name and value for each color", {
colors <- c('test' = "#4ed4ed")
prepared <- prepare_colors(colors)
expect_equal(names(prepared[[1]]), c("color_name", "value"))
expect_equal(prepared[[1]]$color_name, names(colors)[[1]])
expect_equal(prepared[[1]]$value, colors[[1]])
})
})
describe("full_length_hex()", {
it("makes 3-length hex to 6-length", {
expect_equal(full_length_hex("#123"), "#112233")
})
it("keeps length 6 hex", {
expect_equal(full_length_hex("#123456"), "#123456")
})
it("errors if not a hex color", {
expect_error(full_length_hex("123abc"))
expect_error(full_length_hex("#1234567"))
expect_error(full_length_hex("#00000Z"))
})
})
describe("check_color_is_hex", {
it("returns TRUE when color is valid hex", {
expect_true(check_color_is_hex("#123456"))
expect_true(check_color_is_hex("#abc123"))
expect_true(check_color_is_hex("#abc"))
expect_true(check_color_is_hex("#123"))
})
it("returns FALSE and warns if requested", {
expect_false(check_color_is_hex("123456", throw = NULL))
expect_false(check_color_is_hex("123", throw = NULL))
expect_false(check_color_is_hex("apple", throw = NULL))
expect_warning(check_color_is_hex("123456"))
expect_warning(check_color_is_hex("123", msg = "{color} is bad"), "123 is bad")
expect_warning(check_color_is_hex("apple"))
})
it("errors if throw = stop", {
expect_error(check_color_is_hex("123", throw = stop), "123 is not")
})
})
describe("lighten_color() and darken_color()", {
it("errors if strength not in [0, 1]", {
expect_error(lighten_color("#123", 9))
expect_error(darken_color("#123", 9))
})
it("returns original color if strength = 0", {
expect_equal(lighten_color("#808080", 0), "#808080")
expect_equal(darken_color("#808080", 0), "#808080")
})
it("returns entire blend color if strength = 0", {
expect_equal(lighten_color("#808080", 1), "#FFFFFF")
expect_equal(darken_color("#808080", 1), "#000000")
})
it("returns midpoint if strength = 0.5", {
expect_equal(lighten_color("#000000", 0.5), "#7F7F7F")
expect_equal(darken_color("#FFFFFF", 0.5), "#7F7F7F")
})
})