From 29524a85448445178cff72807b9a71e718b1a8ba 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 b75a32e58d..af233b39e0 100644 --- a/tests/testthat/test-scale-colour-continuous.R +++ b/tests/testthat/test-scale-colour-continuous.R @@ -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