Skip to content

Commit

Permalink
Tests and remove redundant switch cases
Browse files Browse the repository at this point in the history
  • Loading branch information
aphalo committed Nov 6, 2023
1 parent 51f132f commit a75d09b
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 20 deletions.
33 changes: 15 additions & 18 deletions R/position-nudge-stack.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,25 @@
#'
#' ggplot(birch_dw.df,
#' aes(y = dry.weight * 1e-3, x = Density, fill = Part)) +
#' stat_summary(geom = "col", fun = mean, na.rm = TRUE,
#' stat_summary(geom = "col", fun = mean,
#' position = "stack", alpha = 0.7, width = 0.67) +
#' stat_summary(geom = "linerange", fun.data = mean_cl_normal, na.rm = TRUE,
#' stat_summary(geom = "linerange", fun.data = mean_cl_normal,
#' position = position_stack_minmax()) +
#' labs(y = "Seedling dry mass (g)") +
#' scale_fill_grey(start = 0.7, end = 0.3) +
#' facet_wrap(facets = vars(Container))
#'
#' ggplot(birch_dw.df,
#' aes(y = dry.weight * 1e-3, x = Density, fill = Part)) +
#' stat_summary(geom = "col", fun = mean, na.rm = TRUE,
#' position = "stack", alpha = 0.7, width = 0.67) +
#' stat_summary(geom = "linerange", fun.data = mean_cl_normal, na.rm = TRUE,
#' position = position_stack_minmax(x = -0.1)) +
#' stat_summary(geom = "col", fun = mean,
#' position = "stack", alpha = 0.7, width = 0.67) +
#' # error bars for each stack bar
#' stat_summary(geom = "linerange", fun.data = mean_cl_normal,
#' position = position_stack_minmax(x = -0.1)) +
#' # error bar for the total
#' stat_summary(data = birch.df, aes(y = (dwstem + dwroot) * 1e-3, fill = NULL),
#' geom = "linerange", linewidth = 0.75,
#' position = position_nudge(x = 0.1), fun.data = mean_cl_normal) +
#' labs(y = "Seedling dry mass (g)") +
#' scale_fill_grey(start = 0.7, end = 0.3) +
#' facet_wrap(facets = vars(Container))
Expand Down Expand Up @@ -197,16 +202,12 @@ position_fillnudge <-
none = function(x) {1},
split = sign,
split.y = function(x) {1},
split.x = sign,
center = sign,
function(x) {1}),
split.x = sign),
.fun_y = switch(direction,
none = function(x) {1},
split = sign,
split.x = function(x) {1},
split.y = sign,
center = sign,
function(x) {1}),
split.y = sign),
kept.origin = kept.origin,
vjust = vjust,
reverse = reverse
Expand Down Expand Up @@ -448,16 +449,12 @@ position_stack_minmax <-
none = function(x) {1},
split = sign,
split.y = function(x) {1},
split.x = sign,
center = sign,
function(x) {1}),
split.x = sign),
.fun_y = switch(direction,
none = function(x) {1},
split = sign,
split.x = function(x) {1},
split.y = sign,
center = sign,
function(x) {1}),
split.y = sign),
kept.origin = kept.origin,
vjust = vjust,
reverse = reverse
Expand Down
38 changes: 36 additions & 2 deletions tests/testthat/test-position_fillnudge.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,55 @@ context("position_fillnudge")

test_that("incorrect kept.origin used", {
expect_error(
position_fillnudge(kept.origin = "", vjust = 0.5, x = -0.3) #,
position_fillnudge(kept.origin = "") #,
# "`kept.origin` must be one of \"stacked\", \"original\", or \"none\", not \"\"."
# rlang arg_match() adds to the error message characters that break expect_error()
)
})

test_that("correct kept.origin used", {
position <- position_fillnudge(kept.origin = "none", vjust = 0.5, x = -0.3)
position <- position_fillnudge(kept.origin = "none")
expect_no_error(position)
})

test_that("correct kept.origin used", {
position <- position_fillnudge(kept.origin = "original")
expect_no_error(position)
})

test_that("incorrect direction used", {
expect_error(
position_stack_minmax(direction = "") #,
# "`kept.origin` must be one of \"stacked\", \"original\", or \"none\", not \"\"."
# rlang arg_match() adds to the error message characters that break expect_error()
)
})

test_that("correct direction used", {
position <- position_stack_minmax(direction = "none")
expect_no_error(position)
})

test_that("correct direction used", {
position <- position_stack_minmax(direction = "split")
expect_no_error(position)
})

test_that("correct direction used", {
position <- position_stack_minmax(direction = "split.x")
expect_no_error(position)
})

test_that("correct direction used", {
position <- position_stack_minmax(direction = "split.y")
expect_no_error(position)
})

test_that("correct reverse, vjust and x assigned", {
position <- position_fillnudge(kept.origin = "none", vjust = 0.5, x = -0.3)
expect_false(position$reverse)
expect_identical(position$vjust, 0.5)
expect_identical(position$y, 0)
expect_identical(position$x, -0.3)
expect_identical(position$fill, TRUE)
})
48 changes: 48 additions & 0 deletions tests/testthat/test-position_stack_minmax.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,57 @@
context("position_stack_minmax")

test_that("incorrect kept.origin used", {
expect_error(
position_stack_minmax(kept.origin = "") #,
# "`kept.origin` must be one of \"stacked\", \"original\", or \"none\", not \"\"."
# rlang arg_match() adds to the error message characters that break expect_error()
)
})

test_that("correct kept.origin used", {
position <- position_stack_minmax(kept.origin = "none")
expect_no_error(position)
})

test_that("correct kept.origin used", {
position <- position_stack_minmax(kept.origin = "original")
expect_no_error(position)
})

test_that("incorrect direction used", {
expect_error(
position_stack_minmax(direction = "") #,
# "`kept.origin` must be one of \"stacked\", \"original\", or \"none\", not \"\"."
# rlang arg_match() adds to the error message characters that break expect_error()
)
})

test_that("correct direction used", {
position <- position_stack_minmax(direction = "none")
expect_no_error(position)
})

test_that("correct direction used", {
position <- position_stack_minmax(direction = "split")
expect_no_error(position)
})

test_that("correct direction used", {
position <- position_stack_minmax(direction = "split.x")
expect_no_error(position)
})

test_that("correct direction used", {
position <- position_stack_minmax(direction = "split.y")
expect_no_error(position)
})

test_that("test vjust, reverse, x, y and kept.origin arguments", {
position <- position_stack_minmax()
expect_identical(position$vjust, 1)
expect_identical(position$reverse, FALSE)
expect_identical(position$x, 0)
expect_identical(position$y, 0)
expect_identical(position$kept.origin, "stacked")
expect_identical(position$var, "y")
expect_identical(position$fill, FALSE)
Expand Down

0 comments on commit a75d09b

Please sign in to comment.