Skip to content

Commit

Permalink
TEST: Palette capability "may_return_na"
Browse files Browse the repository at this point in the history
  • Loading branch information
zeehio committed Nov 6, 2022
1 parent 99b7c60 commit d9e577d
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions tests/testthat/test-scale-colour-continuous.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,32 @@ test_that("type argument is checked for proper input", {
scale_colour_continuous(type = "abc")
)
})

test_that("palette with may_return_NA=FALSE works as expected", {
sc <- scale_fill_continuous()
# A palette that may return NAs, will have NAs replaced by the scale's na.value
# by the scale:
sc$palette <- structure(
function(x) {
rep(NA_character_, length(x))
},
may_return_NA = TRUE
)
sc$na.value <- "red"
nat <- sc$map(0.5, limits = c(0, 1))
expect_equal(nat, "red")

# This palette is lying, because it returns NA even though it says it can't.
# The scale will not replace the NA values, leading to further errors.
# You should not do this in production, but it helps to test:
sc <- scale_fill_continuous()
sc$palette <- structure(
function(x) {
rep(NA_character_, length(x))
},
may_return_NA = FALSE
)
sc$na.value <- "red"
nat <- sc$map(0.5, limits = c(0, 1))
expect_equal(nat, NA_character_)
})

0 comments on commit d9e577d

Please sign in to comment.