Skip to content

Commit

Permalink
TEST: Palette capability: "accepts_native_output"
Browse files Browse the repository at this point in the history
  • Loading branch information
zeehio committed Nov 6, 2022
1 parent 82c0cfd commit 29524a8
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions tests/testthat/test-scale-colour-continuous.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,43 @@ test_that("scale_params mapping_method supports binned", {
expect_equal(length(unique(only_two)), 2L)
})

test_that("palette with accepts_native_output returns native colours", {
sc <- scale_fill_continuous()
sc$palette <- structure(
function(x, color_fmt = "character") {
if (color_fmt == "character") {
rep("red", length(x))
} else {
rep(-16776961L, length(x))
}
},
accepts_native_output = TRUE
)
x <- 0.5
nat <- sc$map(x, limits = c(0, 1), scale_params = list(color_fmt = "native"))
expect_equal(nat, -16776961L)
chr <- sc$map(x, limits = c(0, 1), scale_params = list(color_fmt = "character"))
expect_equal(chr, "red")
chr2 <- sc$map(x, limits = c(0, 1))
expect_equal(chr, chr2)
})

test_that("palette without accepts_native_output returns native colours as well", {
sc <- scale_fill_continuous()
sc$palette <- function(x) {
rep("red", length(x))
}
x <- 0.5
nat <- sc$map(x, limits = c(0, 1), scale_params = list(color_fmt = "native"))
expect_equal(nat, -16776961L)
chr <- sc$map(x, limits = c(0, 1), scale_params = list(color_fmt = "character"))
expect_equal(chr, "red")
chr2 <- sc$map(x, limits = c(0, 1))
expect_equal(chr, chr2)
})



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
Expand Down

0 comments on commit 29524a8

Please sign in to comment.