diff --git a/tests/testthat/test-scale-colour-continuous.R b/tests/testthat/test-scale-colour-continuous.R index cdeddfafef..0df43ba67e 100644 --- a/tests/testthat/test-scale-colour-continuous.R +++ b/tests/testthat/test-scale-colour-continuous.R @@ -19,6 +19,43 @@ test_that("type argument is checked for proper input", { ) }) +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