Skip to content

Commit d9d6dea

Browse files
committed
TEST: Palette capability "may_return_na"
1 parent 92aacca commit d9d6dea

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

tests/testthat/test-scale-colour-continuous.R

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,38 @@ test_that("type argument is checked for proper input", {
1818
scale_colour_continuous(type = "abc")
1919
)
2020
})
21-
2221
test_that("scale_params mapping_method supports binned", {
2322
sc <- scale_fill_continuous()
2423
x <- seq(0, 1, length.out = 10)
2524
only_two <- sc$map(x, limits = c(0, 1), scale_params = list(mapping_method = "binned", mapping_method_bins = 2))
2625
expect_equal(length(unique(only_two)), 2L)
2726
})
2827

28+
test_that("palette with may_return_NA=FALSE works as expected", {
29+
sc <- scale_fill_continuous()
30+
# A palette that may return NAs, will have NAs replaced by the scale's na.value
31+
# by the scale:
32+
sc$palette <- structure(
33+
function(x) {
34+
rep(NA_character_, length(x))
35+
},
36+
may_return_NA = TRUE
37+
)
38+
sc$na.value <- "red"
39+
nat <- sc$map(0.5, limits = c(0, 1))
40+
expect_equal(nat, "red")
41+
42+
# This palette is lying, because it returns NA even though it says it can't.
43+
# The scale will not replace the NA values, leading to further errors.
44+
# You should not do this in production, but it helps to test:
45+
sc <- scale_fill_continuous()
46+
sc$palette <- structure(
47+
function(x) {
48+
rep(NA_character_, length(x))
49+
},
50+
may_return_NA = FALSE
51+
)
52+
sc$na.value <- "red"
53+
nat <- sc$map(0.5, limits = c(0, 1))
54+
expect_equal(nat, NA_character_)
55+
})

0 commit comments

Comments
 (0)