Skip to content

Commit d9e577d

Browse files
committed
TEST: Palette capability "may_return_na"
1 parent 99b7c60 commit d9e577d

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,32 @@ test_that("type argument is checked for proper input", {
1818
scale_colour_continuous(type = "abc")
1919
)
2020
})
21+
22+
test_that("palette with may_return_NA=FALSE works as expected", {
23+
sc <- scale_fill_continuous()
24+
# A palette that may return NAs, will have NAs replaced by the scale's na.value
25+
# by the scale:
26+
sc$palette <- structure(
27+
function(x) {
28+
rep(NA_character_, length(x))
29+
},
30+
may_return_NA = TRUE
31+
)
32+
sc$na.value <- "red"
33+
nat <- sc$map(0.5, limits = c(0, 1))
34+
expect_equal(nat, "red")
35+
36+
# This palette is lying, because it returns NA even though it says it can't.
37+
# The scale will not replace the NA values, leading to further errors.
38+
# You should not do this in production, but it helps to test:
39+
sc <- scale_fill_continuous()
40+
sc$palette <- structure(
41+
function(x) {
42+
rep(NA_character_, length(x))
43+
},
44+
may_return_NA = FALSE
45+
)
46+
sc$na.value <- "red"
47+
nat <- sc$map(0.5, limits = c(0, 1))
48+
expect_equal(nat, NA_character_)
49+
})

0 commit comments

Comments
 (0)