From fdfd6d908edb5dff2ca9064e23f7bfca220f4b6f Mon Sep 17 00:00:00 2001 From: Sergio Oller Date: Sun, 6 Nov 2022 12:02:36 +0100 Subject: [PATCH] TEST: Palette capability: "accepts_native_output" --- tests/testthat/test-scale-colour-continuous.R | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) 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