From 71728b80f036ce968d73450592f18c5b64ae57c4 Mon Sep 17 00:00:00 2001 From: Dr Nathan Green Date: Tue, 13 Feb 2024 14:07:14 +0000 Subject: [PATCH] started some unit tests with copilot --- R/mimR.R | 1 + tests/testthat/test-parse_formula.R | 30 +++++++++++++ tests/testthat/test-xxx_stats.R | 68 +++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 tests/testthat/test-parse_formula.R create mode 100644 tests/testthat/test-xxx_stats.R diff --git a/R/mimR.R b/R/mimR.R index 0ffd61a..8e55b10 100644 --- a/R/mimR.R +++ b/R/mimR.R @@ -40,6 +40,7 @@ #' mimR_gcomp_stan <- mimR(AC_IPD, BC_ALD, strategy = strategy_gcomp_stan()) #' mimR <- function(AC.IPD, BC.ALD, strategy, CI = 0.95, ...) { + if (CI <= 0 || CI >= 1) stop("CI argument must be between 0 and 1.") ##TODO: as method instead? if (!inherits(strategy, "strategy")) diff --git a/tests/testthat/test-parse_formula.R b/tests/testthat/test-parse_formula.R new file mode 100644 index 0000000..598c6f2 --- /dev/null +++ b/tests/testthat/test-parse_formula.R @@ -0,0 +1,30 @@ + +# unit test functions in parse_formula.R + + +test_that("test get_effect_modifiers", { + expect_equal(get_effect_modifiers(y ~ x), character(0)) + expect_equal(get_effect_modifiers(y ~ x + z), "z") +}) + +test_that("test get_treatment_name", { + expect_equal(get_treatment_name(y ~ x), "x") + expect_equal(get_treatment_name(y ~ x + z), "x") + expec +}) + +test_that("get_mean_names", { + expect_equal(get_mean_names(y ~ x), "y") + expect_equal(get_mean_names(y ~ x + z), "y") +}) + +test_that("get_sd_names", { + expect_equal(get_sd_names(y ~ x), "y") + expect_equal(get_sd_names(y ~ x + z), "y") +}) + +test_that("get_covariate_names", { + expect_equal(get_covariate_names(y ~ x), character(0)) + expect_equal(get_covariate_names(y ~ x + z), "z") +}) + diff --git a/tests/testthat/test-xxx_stats.R b/tests/testthat/test-xxx_stats.R new file mode 100644 index 0000000..e2bc8f2 --- /dev/null +++ b/tests/testthat/test-xxx_stats.R @@ -0,0 +1,68 @@ +# unit tests for ALD_stats() + +test_that("ALD_stats() returns the correct values", { + # test 1 + x <- c(1, 2, 3, 4, 5) + y <- c(1, 2, 3, 4, 5) + expect_equal(ALD_stats(x, y), c(0, 0, 0, 0, 0)) + # test 2 + x <- c(1, 2, 3, 4, 5) + y <- c(5, 4, 3, 2, 1) + expect_equal(ALD_stats(x, y), c(4, 4, 4, 4, 4)) + # test 3 + x <- c(1, 2, 3, 4, 5) + y <- c(1, 1, 1, 1, 1) + expect_equal(ALD_stats(x, y), c(0, 0, 0, 0, 0)) + # test 4 + x <- c(1, 2, 3, 4, 5) + y <- c(1, 2, 3, 4, 5) + expect_equal(ALD_stats(x, y), c(0, 0, 0, 0, 0)) + # test 5 + x <- c(1, 2, 3, 4, 5) + y <- c(1, 2, 3, 4, 5) + expect_equal(ALD_stats(x, y), c(0, 0, 0, 0, 0)) + # test 6 + x <- c(1, 2, 3, 4, 5) + y <- c(1, 2, 3, 4, 5) + expect_equal(ALD_stats(x, y), c(0, 0, 0, 0, 0)) + # test 7 + x <- c(1, 2, 3, 4, 5) + y <- c(1, 2, 3, 4, 5) + expect_equal(ALD_stats(x, y), c(0, 0, 0, 0, 0)) +}) + + +# unit tests for IPD_stats() + +test_that("IPD_stats() returns the correct values", { + # test 1 + x <- c(1, 2, 3, 4, 5) + y <- c(1, 2, 3, 4, 5) + expect_equal(IPD_stats(x, y), c(0, 0, 0, 0, 0)) + # test 2 + x <- c(1, 2, 3, 4, 5) + y <- c(5, 4, 3, 2, 1) + expect_equal(IPD_stats(x, y), c(4, 4, 4, 4, 4)) + # test 3 + x <- c(1, 2, 3, 4, 5) + y <- c(1, 1, 1, 1, 1) + expect_equal(IPD_stats(x, y), c(0, 0, 0, 0, 0)) + # test 4 + x <- c(1, 2, 3, 4, 5) + y <- c(1, 2, 3, 4, 5) + expect_equal(IPD_stats(x, y), c(0, 0, 0, 0, 0)) + # test 5 + x <- c(1, 2, 3, 4, 5) + y <- c(1, 2, 3, 4, 5) + expect_equal(IPD_stats(x, y), c(0, 0, 0, 0, 0)) + # test 6 + x <- c(1, 2, 3, 4, 5) + y <- c(1, 2, 3, 4, 5) + expect_equal(IPD_stats(x, y), c(0, 0, 0, 0, 0)) + # test 7 + x <- c(1, 2, 3, 4, 5) + y <- c(1, 2, 3, 4, 5) + expect_equal(IPD_stats(x, y), c(0, 0, 0, 0, 0)) +}) + +