Skip to content

Commit fdfd6d9

Browse files
committed
TEST: Palette capability: "accepts_native_output"
1 parent 8a18be6 commit fdfd6d9

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
@@ -19,6 +19,43 @@ test_that("type argument is checked for proper input", {
1919
)
2020
})
2121

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

0 commit comments

Comments
 (0)