From e05b176d4d6bf34284cdc782dc9d52276649f4e2 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Tue, 16 Apr 2024 10:29:29 +0200 Subject: [PATCH] add tests --- tests/testthat/test-pal-.R | 46 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 tests/testthat/test-pal-.R diff --git a/tests/testthat/test-pal-.R b/tests/testthat/test-pal-.R new file mode 100644 index 00000000..5329ec2e --- /dev/null +++ b/tests/testthat/test-pal-.R @@ -0,0 +1,46 @@ +test_that("continuous palettes can be created, tested and coerced", { + + pal <- new_continuous_palette( + function(x) ((x - 0.5) * 4) ^2, + "numeric", na_safe = FALSE + ) + expect_equal(pal(seq(0, 1, by = 0.25)), c(4, 1, 0, 1, 4)) + + expect_true(is_pal(pal)) + expect_true(is_continuous_pal(pal)) + expect_false(is_discrete_pal(pal)) + expect_false(is_colour_pal(pal)) + expect_true(is_numeric_pal(pal)) + + expect_equal(palette_type(pal), "numeric") + expect_equal(palette_na_safe(pal), FALSE) + expect_equal(palette_nlevels(pal), NA_integer_) + + new <- as_discrete_pal(pal) + expect_true(is_discrete_pal(new)) + expect_equal(new(5), c(4, 1, 0, 1, 4)) + +}) + +test_that("discrete palettes can be created, tested and coerced", { + + pal <- new_discrete_palette( + function(n) c("red", "green", "blue")[seq_len(n)], + "colour", nlevels = 3 + ) + expect_equal(pal(2), c("red", "green")) + + expect_true(is_pal(pal)) + expect_true(is_discrete_pal(pal)) + expect_false(is_continuous_pal(pal)) + expect_true(is_colour_pal(pal)) + expect_false(is_numeric_pal(pal)) + + expect_equal(palette_type(pal), "colour") + expect_equal(palette_na_safe(pal), FALSE) + expect_equal(palette_nlevels(pal), 3) + + new <- as_continuous_pal(pal) + expect_true(is_continuous_pal(new)) + expect_equal(new(c(0, 0.5, 1)), c("#FF0000", "#00FF00", "#0000FF")) +})