From 323a8a3a59c6232930f4ed0ad02e4d364f6c437f Mon Sep 17 00:00:00 2001 From: raysinensis Date: Thu, 30 Sep 2021 11:57:40 -0600 Subject: [PATCH 1/5] basic tests for trace, mirroring point --- tests/testthat/test-geom-path-trace.R | 91 +++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 tests/testthat/test-geom-path-trace.R diff --git a/tests/testthat/test-geom-path-trace.R b/tests/testthat/test-geom-path-trace.R new file mode 100644 index 0000000..4a800fc --- /dev/null +++ b/tests/testthat/test-geom-path-trace.R @@ -0,0 +1,91 @@ +test_that("unexpected args throw warning", { + expect_warning(ggplot() + geom_path_trace(blah = "blerg")) +}) + +test_that("specify aes params", { + p <- ggplot(stocks, aes(day, value, color = name)) + + p2 <- p + geom_path_trace(linetype = 5) + expect_identical(p2$layers[[1]]$aes_params$linetype, 5) + + p2 <- p + geom_path_trace(color = "red") + expect_identical(p2$layers[[1]]$aes_params$colour, "red") + + p2 <- p + geom_path_trace(colour = "red") + expect_identical(p2$layers[[1]]$aes_params$colour, "red") + + p2 <- p + geom_path_trace(fill = "red") + expect_identical(p2$layers[[1]]$aes_params$fill, "red") + + p2 <- p + geom_path_trace(alpha = 0.5) + expect_identical(p2$layers[[1]]$aes_params$alpha, 0.5) +}) + +test_that("aesthetics to variable", { + p <- ggplot(stocks, aes(day, value, color = name)) + + p2 <- p + geom_path_trace() + expect_identical(as.character(p2$mapping$colour)[[2]], "name") +}) + +test_that("aesthetics from geom", { + p <- ggplot(stocks, aes(day, value, color = name)) + expect_identical(as.character(p$mapping$colour)[2], "name") + + p <- geom_path_trace(aes(fill = name)) + expect_identical(as.character(p[[1]]$mapping$fill)[2], "name") + + p <- geom_path_trace(aes(linetype = name)) + expect_identical(as.character(p[[1]]$mapping$linetype)[2], "name") + + p <- geom_path_trace(aes(alpha = name)) + expect_identical(as.character(p[[1]]$mapping$alpha)[2], "name") + + p <- geom_path_trace(aes(stroke = name)) + expect_identical(as.character(p[[1]]$mapping$stroke)[2], "name") +}) + +test_that("trace_position predicate return list", { + p <- geom_path_trace(trace_position = day < 500 | day > 1500) + + expect_type(p, "list") + expect_true(length(p) == 2) + expect_identical(p[[1]]$geom_params$bkgd_colour, NA) + expect_true(length(p[[2]]$aes_params) == 0) +}) + +test_that("trace_position predicate data", { + p <- ggplot(stocks, aes(day, value, color = name)) + + geom_path_trace(trace_position = day < 500 | day > 1500) + + expect_identical(p$layers[[2]]$data(stocks) %>% dplyr::filter(KEEP_THIS_ROW_PLEASE) %>% dplyr::select(-KEEP_THIS_ROW_PLEASE) %>% tibble::as_tibble(), subset(stocks, day < 500 | day > 1500)) +}) + +test_that("trace_position pass data as data.frame", { + dat <- subset(stocks, day < 500 | day > 1500) + p <- geom_path_trace(data = dat, trace_position = subset(stocks, day < 500 | day > 1500)) + + expect_identical(p[[2]]$data(dat) %>% dplyr::select(day, name, value) %>% tibble::as_tibble(), subset(stocks, day < 500 | day > 1500)) +}) + +test_that("trace_position pass data as function", { + dat <- function(x) subset(x, day < 500 | day > 1500) + p <- geom_path_trace(data = dat, trace_position = day < 500 | day > 1500) + + expect_identical(p[[2]]$data(stocks) %>% dplyr::select(-KEEP_THIS_ROW_PLEASE) %>% tibble::as_tibble(), subset(stocks, day < 500 | day > 1500)) +}) + +test_that("background_params color", { + p <- ggplot(stocks, aes(day, value, color = name)) + + geom_path_trace( + trace_position = day < 500 | day > 1500, + background_params = list(color = NA, fill = "grey75") + ) + + expect_true(p$layers[[1]]$geom_params$bkgd_fill == "grey75") + + p <- ggplot(stocks, aes(day, value, color = name)) + + geom_path_trace(trace_position = day < 500 | day > 1500) + + expect_true(is.na(p$layers[[1]]$geom_params$bkgd_colour)) +}) From 07b282d1f8ca26c339d3eb6006cfe9cfb58af183 Mon Sep 17 00:00:00 2001 From: raysinensis Date: Fri, 1 Oct 2021 12:22:53 -0600 Subject: [PATCH 2/5] basic tests for line and step --- tests/testthat/test-geom-line-trace.R | 91 +++++++++++++++++++++++++++ tests/testthat/test-geom-step-trace.R | 91 +++++++++++++++++++++++++++ 2 files changed, 182 insertions(+) create mode 100644 tests/testthat/test-geom-line-trace.R create mode 100644 tests/testthat/test-geom-step-trace.R diff --git a/tests/testthat/test-geom-line-trace.R b/tests/testthat/test-geom-line-trace.R new file mode 100644 index 0000000..8e66972 --- /dev/null +++ b/tests/testthat/test-geom-line-trace.R @@ -0,0 +1,91 @@ +test_that("unexpected args throw warning", { + expect_warning(ggplot() + geom_line_trace(blah = "blerg")) +}) + +test_that("specify aes params", { + p <- ggplot(stocks, aes(day, value, color = name)) + + p2 <- p + geom_line_trace(linetype = 5) + expect_identical(p2$layers[[1]]$aes_params$linetype, 5) + + p2 <- p + geom_line_trace(color = "red") + expect_identical(p2$layers[[1]]$aes_params$colour, "red") + + p2 <- p + geom_line_trace(colour = "red") + expect_identical(p2$layers[[1]]$aes_params$colour, "red") + + p2 <- p + geom_line_trace(fill = "red") + expect_identical(p2$layers[[1]]$aes_params$fill, "red") + + p2 <- p + geom_line_trace(alpha = 0.5) + expect_identical(p2$layers[[1]]$aes_params$alpha, 0.5) +}) + +test_that("aesthetics to variable", { + p <- ggplot(stocks, aes(day, value, color = name)) + + p2 <- p + geom_line_trace() + expect_identical(as.character(p2$mapping$colour)[[2]], "name") +}) + +test_that("aesthetics from geom", { + p <- ggplot(stocks, aes(day, value, color = name)) + expect_identical(as.character(p$mapping$colour)[2], "name") + + p <- geom_line_trace(aes(fill = name)) + expect_identical(as.character(p[[1]]$mapping$fill)[2], "name") + + p <- geom_line_trace(aes(linetype = name)) + expect_identical(as.character(p[[1]]$mapping$linetype)[2], "name") + + p <- geom_line_trace(aes(alpha = name)) + expect_identical(as.character(p[[1]]$mapping$alpha)[2], "name") + + p <- geom_line_trace(aes(stroke = name)) + expect_identical(as.character(p[[1]]$mapping$stroke)[2], "name") +}) + +test_that("trace_position predicate return list", { + p <- geom_line_trace(trace_position = day < 500 | day > 1500) + + expect_type(p, "list") + expect_true(length(p) == 2) + expect_identical(p[[1]]$geom_params$bkgd_colour, NA) + expect_true(length(p[[2]]$aes_params) == 0) +}) + +test_that("trace_position predicate data", { + p <- ggplot(stocks, aes(day, value, color = name)) + + geom_line_trace(trace_position = day < 500 | day > 1500) + + expect_identical(p$layers[[2]]$data(stocks) %>% dplyr::filter(KEEP_THIS_ROW_PLEASE) %>% dplyr::select(-KEEP_THIS_ROW_PLEASE) %>% tibble::as_tibble(), subset(stocks, day < 500 | day > 1500)) +}) + +test_that("trace_position pass data as data.frame", { + dat <- subset(stocks, day < 500 | day > 1500) + p <- geom_line_trace(data = dat, trace_position = subset(stocks, day < 500 | day > 1500)) + + expect_identical(p[[2]]$data(dat) %>% dplyr::select(day, name, value) %>% tibble::as_tibble(), subset(stocks, day < 500 | day > 1500)) +}) + +test_that("trace_position pass data as function", { + dat <- function(x) subset(x, day < 500 | day > 1500) + p <- geom_line_trace(data = dat, trace_position = day < 500 | day > 1500) + + expect_identical(p[[2]]$data(stocks) %>% dplyr::select(-KEEP_THIS_ROW_PLEASE) %>% tibble::as_tibble(), subset(stocks, day < 500 | day > 1500)) +}) + +test_that("background_params color", { + p <- ggplot(stocks, aes(day, value, color = name)) + + geom_line_trace( + trace_position = day < 500 | day > 1500, + background_params = list(color = NA, fill = "grey75") + ) + + expect_true(p$layers[[1]]$geom_params$bkgd_fill == "grey75") + + p <- ggplot(stocks, aes(day, value, color = name)) + + geom_line_trace(trace_position = day < 500 | day > 1500) + + expect_true(is.na(p$layers[[1]]$geom_params$bkgd_colour)) +}) diff --git a/tests/testthat/test-geom-step-trace.R b/tests/testthat/test-geom-step-trace.R new file mode 100644 index 0000000..4065759 --- /dev/null +++ b/tests/testthat/test-geom-step-trace.R @@ -0,0 +1,91 @@ +test_that("unexpected args throw warning", { + expect_warning(ggplot() + geom_step_trace(blah = "blerg")) +}) + +test_that("specify aes params", { + p <- ggplot(stocks, aes(day, value, color = name)) + + p2 <- p + geom_step_trace(linetype = 5) + expect_identical(p2$layers[[1]]$aes_params$linetype, 5) + + p2 <- p + geom_step_trace(color = "red") + expect_identical(p2$layers[[1]]$aes_params$colour, "red") + + p2 <- p + geom_step_trace(colour = "red") + expect_identical(p2$layers[[1]]$aes_params$colour, "red") + + p2 <- p + geom_step_trace(fill = "red") + expect_identical(p2$layers[[1]]$aes_params$fill, "red") + + p2 <- p + geom_step_trace(alpha = 0.5) + expect_identical(p2$layers[[1]]$aes_params$alpha, 0.5) +}) + +test_that("aesthetics to variable", { + p <- ggplot(stocks, aes(day, value, color = name)) + + p2 <- p + geom_step_trace() + expect_identical(as.character(p2$mapping$colour)[[2]], "name") +}) + +test_that("aesthetics from geom", { + p <- ggplot(stocks, aes(day, value, color = name)) + expect_identical(as.character(p$mapping$colour)[2], "name") + + p <- geom_step_trace(aes(fill = name)) + expect_identical(as.character(p[[1]]$mapping$fill)[2], "name") + + p <- geom_step_trace(aes(linetype = name)) + expect_identical(as.character(p[[1]]$mapping$linetype)[2], "name") + + p <- geom_step_trace(aes(alpha = name)) + expect_identical(as.character(p[[1]]$mapping$alpha)[2], "name") + + p <- geom_step_trace(aes(stroke = name)) + expect_identical(as.character(p[[1]]$mapping$stroke)[2], "name") +}) + +test_that("trace_position predicate return list", { + p <- geom_step_trace(trace_position = day < 500 | day > 1500) + + expect_type(p, "list") + expect_true(length(p) == 2) + expect_identical(p[[1]]$geom_params$bkgd_colour, NA) + expect_true(length(p[[2]]$aes_params) == 0) +}) + +test_that("trace_position predicate data", { + p <- ggplot(stocks, aes(day, value, color = name)) + + geom_step_trace(trace_position = day < 500 | day > 1500) + + expect_identical(p$layers[[2]]$data(stocks) %>% dplyr::filter(KEEP_THIS_ROW_PLEASE) %>% dplyr::select(-KEEP_THIS_ROW_PLEASE) %>% tibble::as_tibble(), subset(stocks, day < 500 | day > 1500)) +}) + +test_that("trace_position pass data as data.frame", { + dat <- subset(stocks, day < 500 | day > 1500) + p <- geom_step_trace(data = dat, trace_position = subset(stocks, day < 500 | day > 1500)) + + expect_identical(p[[2]]$data(dat) %>% dplyr::select(day, name, value) %>% tibble::as_tibble(), subset(stocks, day < 500 | day > 1500)) +}) + +test_that("trace_position pass data as function", { + dat <- function(x) subset(x, day < 500 | day > 1500) + p <- geom_step_trace(data = dat, trace_position = day < 500 | day > 1500) + + expect_identical(p[[2]]$data(stocks) %>% dplyr::select(-KEEP_THIS_ROW_PLEASE) %>% tibble::as_tibble(), subset(stocks, day < 500 | day > 1500)) +}) + +test_that("background_params color", { + p <- ggplot(stocks, aes(day, value, color = name)) + + geom_step_trace( + trace_position = day < 500 | day > 1500, + background_params = list(color = NA, fill = "grey75") + ) + + expect_true(p$layers[[1]]$geom_params$bkgd_fill == "grey75") + + p <- ggplot(stocks, aes(day, value, color = name)) + + geom_step_trace(trace_position = day < 500 | day > 1500) + + expect_true(is.na(p$layers[[1]]$geom_params$bkgd_colour)) +}) From 3919ac567d34ddfca3aee3370b8404837129b405 Mon Sep 17 00:00:00 2001 From: "rmsheridan@cassini" Date: Fri, 1 Oct 2021 12:40:24 -0600 Subject: [PATCH 3/5] update DESCRIPTION --- DESCRIPTION | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DESCRIPTION b/DESCRIPTION index a59041a..2ca2a75 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -27,6 +27,8 @@ Suggests: rmarkdown, testthat (>= 3.0.0), tidyr + dplyr + tibble VignetteBuilder: knitr Config/testthat/edition: 3 From d464cb1de165f0938baf830ca3837b308ecf66d4 Mon Sep 17 00:00:00 2001 From: "rmsheridan@cassini" Date: Fri, 1 Oct 2021 12:45:25 -0600 Subject: [PATCH 4/5] updated DESCRIPTION --- DESCRIPTION | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 2ca2a75..5dbd9bc 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -26,9 +26,8 @@ Suggests: knitr, rmarkdown, testthat (>= 3.0.0), - tidyr + tibble, dplyr - tibble VignetteBuilder: knitr Config/testthat/edition: 3 From 1231ec7c6b20e3eda10435d70ef4cd45d64a0a5a Mon Sep 17 00:00:00 2001 From: "rmsheridan@cassini" Date: Fri, 1 Oct 2021 12:48:54 -0600 Subject: [PATCH 5/5] updated DESCRIPTION --- DESCRIPTION | 1 + 1 file changed, 1 insertion(+) diff --git a/DESCRIPTION b/DESCRIPTION index 5dbd9bc..71b2495 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -26,6 +26,7 @@ Suggests: knitr, rmarkdown, testthat (>= 3.0.0), + tidyr, tibble, dplyr VignetteBuilder: