Skip to content

Commit 4c89293

Browse files
committed
add unit tests
1 parent 3a030f3 commit 4c89293

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

tests/testthat/test-scale-discrete.R

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,48 @@ 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+
opts <- options()
94+
on.exit(options(opts), add = TRUE)
95+
96+
df <- data_frame(
97+
x = 1:4, y = 1:4,
98+
two = c("a", "b", "a", "b"),
99+
four = c("a", "b", "c", "d")
100+
)
101+
102+
withr::with_options(
103+
list(ggplot2.discrete.fill = c("#FFFFFF", "#000000")), {
104+
# nlevels == ncodes
105+
two <- ggplot(df, aes(x, y, colour = two, fill = two)) + geom_point()
106+
expect_equal(layer_data(two)$colour, rep(c("#FFFFFF", "#000000"), 2))
107+
expect_equal(layer_data(two)$fill, rep(c("#FFFFFF", "#000000"), 2))
108+
109+
# nlevels > ncodes (so should fallback to scale_fill_hue())
110+
four_default <- ggplot(df, aes(x, y, colour = four, fill = four)) +
111+
geom_point()
112+
four_hue <- four + scale_fill_hue()
113+
expect_equal(layer_data(four_default)$colour, layer_data(four_hue)$colour)
114+
})
115+
116+
withr::with_options(
117+
list(
118+
ggplot2.discrete.fill = list(
119+
c("#FFFFFF", "#000000"),
120+
c("#FF0000", "#00FF00", "#0000FF", "#FF00FF")
121+
)
122+
), {
123+
# nlevels == 2
124+
two <- ggplot(df, aes(x, y, colour = two, fill = two)) + geom_point()
125+
expect_equal(layer_data(two)$colour, rep(c("#FFFFFF", "#000000"), 2))
126+
expect_equal(layer_data(two)$fill, rep(c("#FFFFFF", "#000000"), 2))
127+
128+
# nlevels == 4
129+
four <- ggplot(df, aes(x, y, colour = four, fill = four)) + geom_point()
130+
expect_equal(layer_data(four)$colour, c("#FF0000", "#00FF00", "#0000FF", "#FF00FF"))
131+
expect_equal(layer_data(four)$fill, c("#FF0000", "#00FF00", "#0000FF", "#FF00FF"))
132+
})
133+
134+
})

0 commit comments

Comments
 (0)