Skip to content

Commit

Permalink
Merge pull request #45 from Merck/test-style
Browse files Browse the repository at this point in the history
Move test file scope logic into `helper.R`
  • Loading branch information
nanxstats committed Feb 20, 2024
2 parents 5b1e3d0 + 61a84ae commit a65f3e1
Show file tree
Hide file tree
Showing 10 changed files with 341 additions and 359 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ Suggests:
testthat,
covr
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
108 changes: 108 additions & 0 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Locate example package path
find_package <- function(pkg) {
system.file(file.path("examples", pkg), package = "pkglite")
}

# List files under a folder in an example package
find_files <- function(pkg, dir, pattern) {
pkg_path <- find_package(pkg)
d <- file.path(paste(pkg_path, dir, sep = "/")) %>%
list.files(
pattern = pattern,
ignore.case = TRUE,
full.names = TRUE
) %>%
gsub(pattern = paste0(".*", pkg, "/{1}"), replacement = "") %>%
as.data.frame(stringsAsFactors = FALSE)
names(d)[1] <- "files"
d
}

# Check if `file_spec()` returns the right object
file_spec_func_valid <- function() {
path <- "R/"
pattern <- "\\.R$"
format <- "text"
recursive <- FALSE
ignore_case <- TRUE
all_files <- FALSE

fs_source <- file_spec(
path = path, pattern = pattern, format = format,
recursive = recursive, ignore_case = ignore_case, all_files = all_files
)

return(
fs_source$path == path &
fs_source$pattern == pattern &
fs_source$format == format &
fs_source$recursive == recursive &
fs_source$ignore_case == ignore_case &
fs_source$all_files == all_files &
class(fs_source) == "file_spec"
)
}

# Flatten the structure of a `file_spec` list
fs_unlist <- function(fs) {
fs_len <- length(fs)
ls <- list()
ls_cnt <- 0

if (fs_len) {
for (i in seq_len(fs_len)) {
x <- fs[[i]]
if (class(x) == "file_spec") {
ls_cnt <- ls_cnt + 1
ls[[ls_cnt]] <- x
} else if (class(x) == "list") {
ls_tmp <- fs_unlist(x)
for (j in seq_len(length(ls_tmp))) {
ls_cnt <- ls_cnt + 1
ls[[ls_cnt]] <- ls_tmp[[j]]
}
}
}
}
ls
}

# Check if a `file_spec` object has all of the parameters as specified
is_file_spec_type <- function(fs_source, path, pattern, format, recursive, ignore_case, all_files) {
return(fs_source$path == path &
fs_source$pattern == pattern &
fs_source$format == format &
fs_source$recursive == recursive &
fs_source$ignore_case == ignore_case &
fs_source$all_files == all_files &
class(fs_source) == "file_spec")
}

# Save string to a `.txt` file
save_txt <- function(code, path = tempfile(fileext = ".txt")) {
writeLines(code, con = path)
path
}

# Create artifacts for testing `pack()`
create_artifacts_pack <- function() {
pkg1 <- system.file("examples/pkg1", package = "pkglite")
pkg2 <- system.file("examples/pkg2", package = "pkglite")
fc1 <- pkg1 %>% collate(file_default())
fc2 <- pkg2 %>% collate(file_default())
txt <- tempfile(fileext = ".txt")

list("pkg1" = pkg1, "pkg2" = pkg2, "fc1" = fc1, "fc2" = fc2, "txt" = txt)
}

# Create artifacts for testing `unpack()`
create_artifacts_unpack <- function() {
pkg1 <- "pkg1" %>% find_package()
pkg2 <- "pkg2" %>% find_package()
fc1 <- pkg1 %>% collate(file_r())
fc2 <- pkg2 %>% collate(file_r())
txt <- tempfile(fileext = ".txt")
pack(fc1, fc2, output = txt, quiet = TRUE)

list("pkg1" = pkg1, "pkg2" = pkg2, "fc1" = fc1, "fc2" = fc2, "txt" = txt)
}
28 changes: 14 additions & 14 deletions tests/testthat/test-independent-test_file_spec.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
testthat::test_that("file_spec() creates the correct 'file_spec' object", {
test_that("file_spec() creates the correct 'file_spec' object", {
path <- "R/"
pattern <- "\\.R$"
format <- "text"
Expand All @@ -11,17 +11,17 @@ testthat::test_that("file_spec() creates the correct 'file_spec' object", {
recursive = recursive, ignore_case = ignore_case, all_files = all_files
)

testthat::expect_equal(fs_source$path, path)
testthat::expect_equal(fs_source$pattern, pattern)
testthat::expect_equal(fs_source$format, format)
testthat::expect_equal(fs_source$recursive, recursive)
testthat::expect_equal(fs_source$ignore_case, ignore_case)
testthat::expect_equal(fs_source$all_files, all_files)
testthat::expect_equal(class(fs_source), "file_spec")
testthat::expect_error(file_spec(), "Must provide a non-empty `path`.")
expect_equal(fs_source$path, path)
expect_equal(fs_source$pattern, pattern)
expect_equal(fs_source$format, format)
expect_equal(fs_source$recursive, recursive)
expect_equal(fs_source$ignore_case, ignore_case)
expect_equal(fs_source$all_files, all_files)
expect_equal(class(fs_source), "file_spec")
expect_error(file_spec(), "Must provide a non-empty `path`.")
})

testthat::test_that("is_file_spec() checks the presence of 'file_spec' as the class specifier", {
test_that("is_file_spec() checks the presence of 'file_spec' as the class specifier", {
path <- "R/"
pattern <- "\\.R$"
format <- "text"
Expand All @@ -37,12 +37,12 @@ testthat::test_that("is_file_spec() checks the presence of 'file_spec' as the cl
dummy <- 1
class(dummy) <- "file"

testthat::expect_equal(is_file_spec(fs_source), TRUE)
testthat::expect_equal(is_file_spec(dummy), FALSE)
expect_equal(is_file_spec(fs_source), TRUE)
expect_equal(is_file_spec(dummy), FALSE)
})


testthat::test_that("Check print.file_spec() is returning the right file_spec() object", {
test_that("Check print.file_spec() is returning the right file_spec() object", {
path <- "R/"
pattern <- "\\.R$"
format <- "text"
Expand All @@ -57,5 +57,5 @@ testthat::test_that("Check print.file_spec() is returning the right file_spec()

x <- print.file_spec(fs_source)

testthat::expect_equal(x, fs_source)
expect_equal(x, fs_source)
})
Loading

0 comments on commit a65f3e1

Please sign in to comment.