Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for utilities #280

Merged
merged 10 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ Imports:
stringr,
labeling
Suggests:
clipr,
knitr,
rmarkdown,
testthat (>= 3.0.0),
vdiffr (>= 1.0.0)
vdiffr (>= 1.0.0),
withr
VignetteBuilder:
knitr
Config/testthat/edition: 3
Expand Down
1 change: 0 additions & 1 deletion R/error-checks.R
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

12 changes: 6 additions & 6 deletions R/utilities-file.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#' Source all .R files located in a specific folder
#'
#' @param folderPath Path to the folder where .R files are located
#' @param recursive If TRUE, the contents of the sub-folders are also sourced,
#' @param recursive If `TRUE`, the contents of the sub-folders are also sourced,
#' otherwise only the files located directly in the directory are considered.
#' Default is FALSE.
#' Default is `FALSE`.
#' @export
sourceAll <- function(folderPath, recursive = FALSE) {
filesPaths <- list.files(folderPath, recursive = recursive)
Expand All @@ -18,15 +18,15 @@ sourceAll <- function(folderPath, recursive = FALSE) {
invisible(lapply(file.path(folderPath, filesPaths), sourceFile))
}

#' pathFromClipboard
#' Convert Windows filepaths for R
#'
#' Converts the windows-like path (using `\`) from the clipboard to the form
#' Converts the Windows-like path (using `\`) from the clipboard to the form
#' readable by R (using` /`).
#'
#' @param path Path that will be converted. If "clipboard" (default), path is
#' @param path Path that will be converted. If `"clipboard"` (default), path is
#' queried from clipboard.
#'
#' @return String representation of a file path with `/` as separator
#' @return String representation of a file path with `/` as separator.
#' @export
pathFromClipboard <- function(path = "clipboard") {
y <- if (path == "clipboard") {
Expand Down
10 changes: 5 additions & 5 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ removeFromList <- function(entry, listArg) {
return(listArg)
}

#' Compare values including NA
#' Compare values including `NA`
#'
#' @param v1 Value or a list of values to compare. May include NA.
#' @param v2 Value or a list of values to compare. May include NA.
#' @param v1 Value or a list of values to compare. May include `NA`.
#' @param v2 Value or a list of values to compare. May include `NA`.
#' @details From http://www.cookbook-r.com/Manipulating_data/Comparing_vectors_or_factors_with_NA/
#'
#' @return TRUE wherever elements are the same, including NA's,
# and FALSE everywhere else.
#' @return `TRUE` wherever elements are the same, including `NA`'s,
# and `FALSE` everywhere else.
#' @export
compareWithNA <- function(v1, v2) {
same <- (v1 == v2) | (is.na(v1) & is.na(v2))
Expand Down
5 changes: 4 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ build_script:
- travis-tool.sh r_binary_install curl
- Rscript -e "install.packages('https://github.com/Open-Systems-Pharmacology/rClr/releases/download/v0.9.2/rClr_0.9.2.zip', repos = NULL, type = 'binary')"
- Rscript -e "install.packages('https://ci.appveyor.com/api/projects/open-systems-pharmacology-ci/ospsuite-r/artifacts/ospsuite.zip', repos = NULL, type = 'binary')"
- Rscript -e "install.packages(c('covr', 'FME', 'readr', 'tidyr', 'ggplot2', 'patchwork', 'vdiffr'), repos = 'http://cran.us.r-project.org')"
# hard dependencies (Imports)
- Rscript -e "install.packages(c('FME', 'readr', 'tidyr', 'ggplot2', 'patchwork'), repos = 'http://cran.us.r-project.org')"
# soft dependencies (Suggests)
- Rscript -e "install.packages(c('covr', 'clipr', 'vdiffr', 'withr'), repos = 'http://cran.us.r-project.org')"
- Rscript -e "install.packages('https://ci.appveyor.com/api/projects/open-systems-pharmacology-ci/ospsuite-rutils/artifacts/ospsuite.utils.zip', repos = NULL, type = 'binary')"
- Rscript -e "install.packages('https://ci.appveyor.com/api/projects/open-systems-pharmacology-ci/tlf-library/artifacts/tlf.zip', repos = NULL, type = 'binary')"
- Rscript -e "install.packages('https://ci.appveyor.com/api/projects/open-systems-pharmacology-ci/ospsuite-parameteridentification/artifacts/ospsuite.parameteridentification.zip', repos = NULL, type = 'binary')"
Expand Down
10 changes: 5 additions & 5 deletions man/compareWithNA.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions man/pathFromClipboard.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/sourceAll.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions tests/testthat/_snaps/scenario-configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# `ScenarioConfiguration` defaults are as expected

Code
mySC <- ScenarioConfiguration$new(projectConfiguration = ProjectConfiguration$
new())
mySC
Output
ProjectConfiguration:
Model folder:
Parameters folder:
Parameters file name:
Individual parameters file name:
Individual physiology file name:
Population parameters file name:
Scenario definitions file name:
Scenario applications definitions file name:
Experimental data folder:
Experimental data file:
Data importer configuration:
Output folder:
Output device:
ScenarioConfiguration:
Model file name:
Scenario name:
Parameters sheets:
Individual Id:
Application protocol:
Simulation time:
Points per minute: 1
Simulate steady-state: FALSE
Steady-state time: 1000
Set test parameters: FALSE

8 changes: 8 additions & 0 deletions tests/testthat/test-scenario-configuration.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## context("ScenarioConfiguration")

test_that("`ScenarioConfiguration` defaults are as expected", {
expect_snapshot({
mySC <- ScenarioConfiguration$new(projectConfiguration = ProjectConfiguration$new())
mySC
})
})
38 changes: 38 additions & 0 deletions tests/testthat/test-utilities-file.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
## context("sourceAll")

test_that("`sourceAll()` sources all files in the directory", {
skip_if_not_installed("withr")

withr::with_tempdir(
code = {
f1 <- file.create("f1.R")
f2 <- file.create("f2.R")
writeLines("var1 <- 1 + 1", "f1.R")
writeLines("var2 <- paste0('a', 'b')", "f2.R")

sourceAll(".")

expect_true(exists("var1"))
expect_true(exists("var2"))
}
)
})

## context("pathFromClipboard")

test_that("`pathFromClipboard()` converts paths as expected", {
skip_if_not_installed("clipr")

# This will work only in interactive mode, i.e. with
# `devtools::test_active_file()` or `devtools::test()`, but not during R CMD
# Check on CRAN or AppVeyor where the system clipboard is not available
if (clipr::clipr_available()) {
path <- "C:\\Users\\Documents"
clipr::write_clip(path, allow_non_interactive = TRUE)

expect_equal(
pathFromClipboard(),
"C:/Users/Documents"
)
}
})
11 changes: 11 additions & 0 deletions tests/testthat/test-utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,14 @@ test_that("It finds a 0 with relative threshold", {
test_that("Escape a string for possible regular expression match", {
expect_equal(escapeForRegex("C:/Downloads"), "\\QC:/Downloads\\E")
})

## context("compareWithNA")

test_that("`compareWithNA()` works as expected", {
res <- compareWithNA(
c(NA, "a", "b", NA),
c(NA, "c", "b", "a")
)

expect_equal(res, c(TRUE, FALSE, TRUE, FALSE))
})