It is important that we manually read through the generated tests to
make sure that our function is behaving as intended, and to check if any
important tests are missing.
# Define a function with arguments
fn <- function(x, y, z = 10) {
if (x > 3) stop("'x' > 3")
if (y < 0) warning("'y'<0")
if (z == 10) message("'z' was 10!")
x + y + z
}
# Create tests for the function
# Note: We currently need to specify the list of arguments
# in the function call
gxs_function(fn = fn,
args_values = list(
"x" = list(2, 4, NA),
"y" = list(0,-1),
"z" = list(5, 10)
))
# Inserts the following tests:
## Testing 'fn' ####
## Initially generated by xpectr
# Testing different combinations of argument values
# Testing fn(x = 2, y = 0, z = 5)
xpectr::set_test_seed(42)
# Assigning output
output_19148 <- fn(x = 2, y = 0, z = 5)
# Testing class
expect_equal(
class(output_19148),
"numeric",
fixed = TRUE)
# Testing type
expect_type(
output_19148,
type = "double")
# Testing values
expect_equal(
output_19148,
7,
tolerance = 1e-4)
# Testing names
expect_equal(
names(output_19148),
NULL,
fixed = TRUE)
# Testing length
expect_equal(
length(output_19148),
1L)
# Testing sum of element lengths
expect_equal(
sum(xpectr::element_lengths(output_19148)),
1L)
# Testing fn(x = 4, y = 0, z = 5)
# Changed from baseline: x = 4
xpectr::set_test_seed(42)
# Testing side effects
expect_error(
xpectr::strip_msg(fn(x = 4, y = 0, z = 5)),
xpectr::strip("'x' > 3"),
fixed = TRUE)
# Testing fn(x = NA, y = 0, z = 5)
# Changed from baseline: x = NA
xpectr::set_test_seed(42)
# Testing side effects
expect_error(
xpectr::strip_msg(fn(x = NA, y = 0, z = 5)),
xpectr::strip("missing value where TRUE/FALSE needed"),
fixed = TRUE)
# Testing fn(x = NULL, y = 0, z = 5)
# Changed from baseline: x = NULL
xpectr::set_test_seed(42)
# Testing side effects
expect_error(
xpectr::strip_msg(fn(x = NULL, y = 0, z = 5)),
xpectr::strip("argument is of length zero"),
fixed = TRUE)
# Testing fn(x = 2, y = -1, z = 5)
# Changed from baseline: y = -1
xpectr::set_test_seed(42)
# Testing side effects
# Assigning side effects
side_effects_16417 <- xpectr::capture_side_effects(fn(x = 2, y = -1, z = 5), reset_seed = TRUE)
expect_equal(
xpectr::strip(side_effects_16417[['warnings']]),
xpectr::strip("'y'<0"),
fixed = TRUE)
expect_equal(
xpectr::strip(side_effects_16417[['messages']]),
xpectr::strip(character(0)),
fixed = TRUE)
# Assigning output
output_16417 <- xpectr::suppress_mw(fn(x = 2, y = -1, z = 5))
# Testing class
expect_equal(
class(output_16417),
"numeric",
fixed = TRUE)
# Testing type
expect_type(
output_16417,
type = "double")
# Testing values
expect_equal(
output_16417,
6,
tolerance = 1e-4)
# Testing names
expect_equal(
names(output_16417),
NULL,
fixed = TRUE)
# Testing length
expect_equal(
length(output_16417),
1L)
# Testing sum of element lengths
expect_equal(
sum(xpectr::element_lengths(output_16417)),
1L)
# Testing fn(x = 2, y = NULL, z = 5)
# Changed from baseline: y = NULL
xpectr::set_test_seed(42)
# Testing side effects
expect_error(
xpectr::strip_msg(fn(x = 2, y = NULL, z = 5)),
xpectr::strip("argument is of length zero"),
fixed = TRUE)
# Testing fn(x = 2, y = 0, z = 10)
# Changed from baseline: z = 10
xpectr::set_test_seed(42)
# Testing side effects
# Assigning side effects
side_effects_17365 <- xpectr::capture_side_effects(fn(x = 2, y = 0, z = 10), reset_seed = TRUE)
expect_equal(
xpectr::strip(side_effects_17365[['warnings']]),
xpectr::strip(character(0)),
fixed = TRUE)
expect_equal(
xpectr::strip(side_effects_17365[['messages']]),
xpectr::strip("'z' was 10!\n"),
fixed = TRUE)
# Assigning output
output_17365 <- xpectr::suppress_mw(fn(x = 2, y = 0, z = 10))
# Testing class
expect_equal(
class(output_17365),
"numeric",
fixed = TRUE)
# Testing type
expect_type(
output_17365,
type = "double")
# Testing values
expect_equal(
output_17365,
12,
tolerance = 1e-4)
# Testing names
expect_equal(
names(output_17365),
NULL,
fixed = TRUE)
# Testing length
expect_equal(
length(output_17365),
1L)
# Testing sum of element lengths
expect_equal(
sum(xpectr::element_lengths(output_17365)),
1L)
# Testing fn(x = 2, y = 0, z = NULL)
# Changed from baseline: z = NULL
xpectr::set_test_seed(42)
# Testing side effects
expect_error(
xpectr::strip_msg(fn(x = 2, y = 0, z = NULL)),
xpectr::strip("argument is of length zero"),
fixed = TRUE)
## Finished testing 'fn' ####