Skip to content

Commit

Permalink
added test and fixed failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
schochastics committed Sep 7, 2023
1 parent 75fba38 commit 2a38418
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
7 changes: 2 additions & 5 deletions R/export_list.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@
export_list <- function(x, file, archive = "", ...) {
.check_file(file, single_only = FALSE)
archive_format <- find_compress(archive)
if (archive_format$file == "") {
archive_format$file <- "."
}
if (inherits(x, "data.frame")) {
stop("'x' must be a list. Perhaps you want export()?")
}
Expand Down Expand Up @@ -81,7 +78,7 @@ export_list <- function(x, file, archive = "", ...) {
}
outfiles <- file
}
if (is.na(archive_format$compress)) {
if (is.na(archive_format$compress) & archive_format$file != "") {
outfiles <- file.path(archive_format$file, outfiles)
}
out <- list()
Expand All @@ -94,7 +91,7 @@ export_list <- function(x, file, archive = "", ...) {
if (!is.na(archive_format$compress)) {
compress_out(archive, outfiles)
unlink(outfiles)
return(invisible(archive_format$file))
return(invisible(archive))
}
return(invisible(outfiles))
}
14 changes: 9 additions & 5 deletions tests/testthat/test_export_list.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
context("Test export_list()")
library("datasets")

export(list(mtcars3 = mtcars[1:10,],
mtcars2 = mtcars[11:20,],
mtcars1 = mtcars[21:32,]), "mtcars.xlsx")
export(list(
mtcars3 = mtcars[1:10, ],
mtcars2 = mtcars[11:20, ],
mtcars1 = mtcars[21:32, ]
), "mtcars.xlsx")
mylist <- import_list("mtcars.xlsx")

test_that("export_list() works", {

expect_error(export_list(mtcars), label = "export_list() fails on exporting single data frame")
expect_error(export_list(mylist, file = NULL), label = "export_list() fails when file is NULL")

expect_true(identical(export_list(mylist, file = paste0("mtcars_", 3:1, ".csv")), paste0("mtcars_", 3:1, ".csv")))
expect_true(identical(export_list(mylist, file = "%s.csv"), paste0("mtcars", 3:1, ".csv")))

expect_true(identical(export_list(mylist, file = paste0("file_", 1:3, ".csv"), archive = "archive.zip"), "archive.zip"))

expect_true(all.equal(mylist[["mtcars1"]], import("mtcars1.csv")))
expect_true(all.equal(mylist[["mtcars2"]], import("mtcars2.csv")))
expect_true(all.equal(mylist[["mtcars3"]], import("mtcars3.csv")))
Expand All @@ -29,7 +32,6 @@ test_that("export_list() works", {
names(mylist) <- c("a", "a", "c")
expect_error(export_list(mylist, file = "mtcars_%s.csv"), label = "export_list() fails with duplicated data frame names")
expect_error(export_list(mylist, file = c("mtcars1.csv", "mtcars1.csv", "mtcars3.csv")), label = "export_list() fails with duplicated data frame names")

})

unlink("mtcars.xlsx")
Expand All @@ -42,3 +44,5 @@ unlink("mtcars_3.csv")

unlink("a.csv")
unlink("b.csv")

unlink("archive.zip")

0 comments on commit 2a38418

Please sign in to comment.