Skip to content

Commit 29524a8

Browse files
committed
TEST: Palette capability: "accepts_native_output"
1 parent 82c0cfd commit 29524a8

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

tests/testthat/test-scale-colour-continuous.R

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,43 @@ test_that("scale_params mapping_method supports binned", {
2525
expect_equal(length(unique(only_two)), 2L)
2626
})
2727

28+
test_that("palette with accepts_native_output returns native colours", {
29+
sc <- scale_fill_continuous()
30+
sc$palette <- structure(
31+
function(x, color_fmt = "character") {
32+
if (color_fmt == "character") {
33+
rep("red", length(x))
34+
} else {
35+
rep(-16776961L, length(x))
36+
}
37+
},
38+
accepts_native_output = TRUE
39+
)
40+
x <- 0.5
41+
nat <- sc$map(x, limits = c(0, 1), scale_params = list(color_fmt = "native"))
42+
expect_equal(nat, -16776961L)
43+
chr <- sc$map(x, limits = c(0, 1), scale_params = list(color_fmt = "character"))
44+
expect_equal(chr, "red")
45+
chr2 <- sc$map(x, limits = c(0, 1))
46+
expect_equal(chr, chr2)
47+
})
48+
49+
test_that("palette without accepts_native_output returns native colours as well", {
50+
sc <- scale_fill_continuous()
51+
sc$palette <- function(x) {
52+
rep("red", length(x))
53+
}
54+
x <- 0.5
55+
nat <- sc$map(x, limits = c(0, 1), scale_params = list(color_fmt = "native"))
56+
expect_equal(nat, -16776961L)
57+
chr <- sc$map(x, limits = c(0, 1), scale_params = list(color_fmt = "character"))
58+
expect_equal(chr, "red")
59+
chr2 <- sc$map(x, limits = c(0, 1))
60+
expect_equal(chr, chr2)
61+
})
62+
63+
64+
2865
test_that("palette with may_return_NA=FALSE works as expected", {
2966
sc <- scale_fill_continuous()
3067
# A palette that may return NAs, will have NAs replaced by the scale's na.value

0 commit comments

Comments
 (0)