diff --git a/tests/testthat/test-error_if_field_not_in_df.R b/tests/testthat/test-error_if_field_not_in_df.R new file mode 100644 index 0000000..e950f99 --- /dev/null +++ b/tests/testthat/test-error_if_field_not_in_df.R @@ -0,0 +1,48 @@ +## Test error_if_field_not_in_df() ---- + +### Data for tests ---- + +test_error_if_field_not_in_df <- function(data, column) { + error_if_field_not_in_df(data, column) + invisible(NULL) +} + +good_data <- sf::st_read(system.file(file.path("extdata", + "marine_fish_richness.gpkg"), + package = "rphenofish"), quiet = TRUE) +bad_col <- "missing_column" +good_col <- "ECOREGION" + + +### Tests for errors ---- + +test_that("Test error_if_field_not_in_df() for error", { + + expect_error(error_if_field_not_in_df(good_data, bad_col), + "The column listed in 'bad_col' is absent from 'good_data'", + fixed = TRUE) + + expect_error(test_error_if_field_not_in_df(good_data, bad_col), + "The column listed in 'column' is absent from 'data'", + fixed = TRUE) + + expect_error(error_if_field_not_in_df(good_data, c(good_col, good_col)), + "Argument 'c(good_col, good_col)' must be a `character` of length 1", + fixed = TRUE) + + expect_error(test_error_if_field_not_in_df(good_data, c(good_col, good_col)), + "Argument 'column' must be a `character` of length 1", + fixed = TRUE) +}) + + +### Tests for success ---- + +test_that("Test error_if_field_not_in_df() for success", { + + expect_silent(error_if_field_not_in_df(good_data, good_col)) + expect_silent(test_error_if_field_not_in_df(good_data, good_col)) + + expect_invisible(x <- error_if_field_not_in_df(good_data, good_col)) + expect_invisible(x <- test_error_if_field_not_in_df(good_data, good_col)) +}) diff --git a/tests/testthat/test-error_if_missing.R b/tests/testthat/test-error_if_missing.R new file mode 100644 index 0000000..2c10dc2 --- /dev/null +++ b/tests/testthat/test-error_if_missing.R @@ -0,0 +1,34 @@ +## Test error_if_missing() ---- + +### Data for tests ---- + +test_error_if_missing <- function(data) { + error_if_missing(data) + invisible(NULL) +} + + +### Tests for errors ---- + +test_that("Test error_if_missing() for error", { + + expect_error(error_if_missing(), + "Argument '' is required", + fixed = TRUE) + + expect_error(test_error_if_missing(), + "Argument 'data' is required", + fixed = TRUE) +}) + + +### Tests for success ---- + +test_that("Test error_if_missing() for success", { + + expect_silent(error_if_missing(iris)) + expect_silent(test_error_if_missing(iris)) + + expect_invisible(x <- error_if_missing(iris)) + expect_invisible(x <- test_error_if_missing(iris)) +}) diff --git a/tests/testthat/test-error_if_not_character.R b/tests/testthat/test-error_if_not_character.R new file mode 100644 index 0000000..0bba22c --- /dev/null +++ b/tests/testthat/test-error_if_not_character.R @@ -0,0 +1,50 @@ +## Test error_if_not_character() ---- + +### Data for tests ---- + +test_error_if_not_character <- function(data) { + error_if_not_character(data) + invisible(NULL) +} + + +### Tests for errors ---- + +test_that("Test error_if_not_character() for error", { + + expect_error(error_if_not_character(1), + "Argument '1' must be a `character`", + fixed = TRUE) + + expect_error(error_if_not_character(iris), + "Argument 'iris' must be a `character`", + fixed = TRUE) + + expect_error(error_if_not_character(NULL), + "Argument 'NULL' must be a `character`", + fixed = TRUE) + + expect_error(test_error_if_not_character(1), + "Argument 'data' must be a `character`", + fixed = TRUE) + + expect_error(test_error_if_not_character(iris), + "Argument 'data' must be a `character`", + fixed = TRUE) + + expect_error(test_error_if_not_character(NULL), + "Argument 'data' must be a `character`", + fixed = TRUE) +}) + + +### Tests for success ---- + +test_that("Test error_if_not_character() for success", { + + expect_silent(error_if_not_character("this is a character")) + expect_silent(test_error_if_not_character("this is a character")) + + expect_invisible(x <- error_if_not_character("this is a character")) + expect_invisible(x <- test_error_if_not_character("this is a character")) +}) diff --git a/tests/testthat/test-error_if_not_length_of.R b/tests/testthat/test-error_if_not_length_of.R new file mode 100644 index 0000000..9cb65cb --- /dev/null +++ b/tests/testthat/test-error_if_not_length_of.R @@ -0,0 +1,81 @@ +## Test error_if_not_length_of() ---- + +### Data for tests ---- + +test_error_if_not_length_of <- function(data, n) { + error_if_not_length_of(data, n) + invisible(NULL) +} + + + +### Tests for errors ---- + +test_that("Test error_if_not_length_of() for error", { + + expect_error(error_if_not_length_of("length_1", n = "length_1"), + "Argument 'n' must be a `numeric`", + fixed = TRUE) + + expect_error(error_if_not_length_of("length_1", n = NULL), + "Argument 'n' must be a `numeric`", + fixed = TRUE) + + expect_error(error_if_not_length_of("length_1", n = NA), + "Argument 'n' must be a `numeric`", + fixed = TRUE) + + expect_error(error_if_not_length_of("length_1", n = 1:2), + "Argument 'n' must be a `numeric` of length 1", + fixed = TRUE) + + expect_error(error_if_not_length_of("length_1", n = 2), + "Argument '\"length_1\"' must be of length 2", + fixed = TRUE) + + expect_error(error_if_not_length_of(rep("length_1", 2), n = 1), + "Argument 'rep(\"length_1\", 2)' must be of length 1", + fixed = TRUE) + + expect_error(test_error_if_not_length_of("length_1", n = "length_1"), + "Argument 'n' must be a `numeric`", + fixed = TRUE) + + expect_error(test_error_if_not_length_of("length_1", n = NULL), + "Argument 'n' must be a `numeric`", + fixed = TRUE) + + expect_error(test_error_if_not_length_of("length_1", n = NA), + "Argument 'n' must be a `numeric`", + fixed = TRUE) + + expect_error(test_error_if_not_length_of("length_1", n = 1:2), + "Argument 'n' must be a `numeric` of length 1", + fixed = TRUE) + + expect_error(test_error_if_not_length_of("length_1", n = 2), + "Argument 'data' must be of length 2", + fixed = TRUE) + + expect_error(test_error_if_not_length_of(rep("length_1", 2), n = 1), + "Argument 'data' must be of length 1", + fixed = TRUE) +}) + + +### Tests for success ---- + +test_that("Test error_if_not_length_of() for success", { + + expect_silent(error_if_not_length_of("length_1", 1)) + expect_silent(test_error_if_not_length_of("length_1", 1)) + + expect_invisible(x <- error_if_not_length_of("length_1", 1)) + expect_invisible(x <- test_error_if_not_length_of("length_1", 1)) + + expect_silent(error_if_not_length_of(rep("length_1", 2), 2)) + expect_silent(test_error_if_not_length_of(rep("length_1", 2), 2)) + + expect_invisible(x <- error_if_not_length_of(rep("length_1", 2), 2)) + expect_invisible(x <- test_error_if_not_length_of(rep("length_1", 2), 2)) +}) diff --git a/tests/testthat/test-error_if_not_sf.R b/tests/testthat/test-error_if_not_sf.R new file mode 100644 index 0000000..194c8e6 --- /dev/null +++ b/tests/testthat/test-error_if_not_sf.R @@ -0,0 +1,47 @@ +## Test error_if_not_sf() ---- + +### Data for tests ---- + +test_error_if_not_sf <- function(data) { + error_if_not_sf(data) + invisible(NULL) +} + +bad_data <- iris +good_data <- sf::st_read(system.file(file.path("extdata", + "marine_fish_richness.gpkg"), + package = "rphenofish"), quiet = TRUE) + + +### Tests for errors ---- + +test_that("Test error_if_not_sf() for error", { + + expect_error(error_if_not_sf(bad_data), + "Argument 'bad_data' must be an `sf` object", + fixed = TRUE) + + expect_error(test_error_if_not_sf(bad_data), + "Argument 'data' must be an `sf` object", + fixed = TRUE) + + expect_error(error_if_not_sf(good_data[0, ]), + "Argument 'good_data[0, ]' must have at least one row", + fixed = TRUE) + + expect_error(test_error_if_not_sf(good_data[0, ]), + "Argument 'data' must have at least one row", + fixed = TRUE) +}) + + +### Tests for success ---- + +test_that("Test error_if_not_sf() for success", { + + expect_silent(error_if_not_sf(good_data)) + expect_silent(test_error_if_not_sf(good_data)) + + expect_invisible(x <- error_if_not_sf(good_data)) + expect_invisible(x <- test_error_if_not_sf(good_data)) +})