Skip to content

Commit fa89c5d

Browse files
committed
add unit tests
1 parent 3a030f3 commit fa89c5d

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

tests/testthat/test-scale-discrete.R

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,45 @@ test_that("discrete non-position scales can accept functional limits", {
8787
scale$train(c("a", "b", "c"))
8888
expect_identical(scale$get_limits(), c("c", "b", "a"))
8989
})
90+
91+
92+
test_that("discrete scale defaults can be set globally", {
93+
df <- data_frame(
94+
x = 1:4, y = 1:4,
95+
two = c("a", "b", "a", "b"),
96+
four = c("a", "b", "c", "d")
97+
)
98+
99+
withr::with_options(
100+
list(ggplot2.discrete.fill = c("#FFFFFF", "#000000")), {
101+
# nlevels == ncodes
102+
two <- ggplot(df, aes(x, y, colour = two, fill = two)) + geom_point()
103+
expect_equal(layer_data(two)$colour, rep(c("#FFFFFF", "#000000"), 2))
104+
expect_equal(layer_data(two)$fill, rep(c("#FFFFFF", "#000000"), 2))
105+
106+
# nlevels > ncodes (so should fallback to scale_fill_hue())
107+
four_default <- ggplot(df, aes(x, y, colour = four, fill = four)) +
108+
geom_point()
109+
four_hue <- four_default + scale_fill_hue()
110+
expect_equal(layer_data(four_default)$colour, layer_data(four_hue)$colour)
111+
})
112+
113+
withr::with_options(
114+
list(
115+
ggplot2.discrete.fill = list(
116+
c("#FFFFFF", "#000000"),
117+
c("#FF0000", "#00FF00", "#0000FF", "#FF00FF")
118+
)
119+
), {
120+
# nlevels == 2
121+
two <- ggplot(df, aes(x, y, colour = two, fill = two)) + geom_point()
122+
expect_equal(layer_data(two)$colour, rep(c("#FFFFFF", "#000000"), 2))
123+
expect_equal(layer_data(two)$fill, rep(c("#FFFFFF", "#000000"), 2))
124+
125+
# nlevels == 4
126+
four <- ggplot(df, aes(x, y, colour = four, fill = four)) + geom_point()
127+
expect_equal(layer_data(four)$colour, c("#FF0000", "#00FF00", "#0000FF", "#FF00FF"))
128+
expect_equal(layer_data(four)$fill, c("#FF0000", "#00FF00", "#0000FF", "#FF00FF"))
129+
})
130+
131+
})

0 commit comments

Comments
 (0)