/
run-tests.R
131 lines (117 loc) · 3.83 KB
/
run-tests.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#' Run Tests
#'
#' @description `run_tests()` runs the tests in the `tests/` directory.
#' This function only works if the simulation study is set up as an R project
#' and if it is run when this R project is active.
#'
#' @examples
#' \dontrun{
#' # run tests in the tests/ directory
#' run_tests()}
#'
#' @export
run_tests <- function() {
# construct path to testthat.R
sim_project_path <- usethis::proj_get()
testthat_path <- paste(sim_project_path, "tests/testthat.R", sep = "/")
# attempt to run tests
if (file.exists(testthat_path)) {
source(testthat_path)
} else {
stop(
"Cannot find ", testthat_path, ". Is this simulation your active project?"
)
}
invisible()
}
#' Test simChef Ingredients
#'
#' @description `test_sim_dir()` wraps around
#' [testthat::test_dir()] to run all dgp-, method-, evaluator-,
#' and visualizer-related tests.
#'
#' @details This function only works if the simulation study is set up as an R
#' project and if it is run when this R project is active. Additionally, tests
#' must be stored in sub-directories whose names match those suggesting in the
#' "Setting Up Your Simulation" vignette. These details are conveniently taken
#' care of when a simulation study is set up using [create_sim()].
#'
#' @examples
#' \dontrun{
#' # run dgp-, method-, evaluator-, and visualizer-related tests
#' test_sim_dir()}
#'
#' @export
test_sim_dir <- function() {
# set up paths
path_to_tests <- paste0(usethis::proj_get(), "/tests/testthat")
# make sure that the testthat directory exists
assertthat::assert_that(
dir.exists(path_to_tests),
msg = paste0(
"Cannot find ", path_to_tests,
". Is this simulation your active project?"
)
)
# make sure that subdirectories exist
assertthat::assert_that(
all(dir.exists(paste0(
path_to_tests,
c("/dgp-tests", "/method-tests", "/eval-tests", "/viz-tests")
))),
msg = paste0("No test subdirectories found; dgp-, method-, evaluator- and ",
"visualizer-related tests must be saved in",
"tests/testthat/dgp-tests, tests/testthat/metod-tests, ",
"tests/testthat/eval-tests and tests/testthat/viz-tests, ",
"respectively.")
)
# test dgp-related functions
dgp_test_path <- paste0(path_to_tests, "/dgp-tests")
if (length(list.files(dgp_test_path)) > 0) {
cat("\n")
usethis::ui_line("Running dgp-related tests:")
testthat::test_dir(dgp_test_path)
}
# test method-related functions
method_test_path <- paste0(path_to_tests, "/method-tests")
if (length(list.files(method_test_path)) > 0) {
cat("\n")
usethis::ui_line("Running method-related tests:")
testthat::test_dir(method_test_path)
}
# test evaluator-related functions
eval_test_path <- paste0(path_to_tests, "/eval-tests")
if (length(list.files(eval_test_path)) > 0) {
cat("\n")
usethis::ui_line("Running evaluator-related tests:")
testthat::test_dir(eval_test_path)
}
# test visualizer-related functions
viz_test_path <- paste0(path_to_tests, "/viz-tests")
if (length(list.files(viz_test_path)) > 0) {
cat("\n")
usethis::ui_line("Running visualizer-related tests:")
testthat::test_dir(viz_test_path)
}
invisible()
}
#' Load All Simulation Functions in R/
#'
#' @description `load_all()` is simulation study counterpart to
#' [devtools::load_all()] it loads all of
#' the functions in the `R/dgp`, `R/method`, `R/eval` and
#' `R/viz` directories of the current simulation study.
#'
#' @examples
#' \dontrun{
#' # load all functions in R/dgp, R/method, R/eval, and R/viz directories
#' load_all()}
#'
#' @export
load_all <- function() {
sim_functions_files <- list.files(
c("R/dgp", "R/method", "R/eval", "R/viz"),
pattern = "*.R$", full.names = TRUE, ignore.case = TRUE
)
sapply(sim_functions_files, source)
}