Skip to content

Commit

Permalink
Fix #458
Browse files Browse the repository at this point in the history
The S3 method can be dispatched. But the problem is that it can't be
tested with testthat. Why?
  • Loading branch information
chainsawriot committed Dec 17, 2024
1 parent 727d50f commit 39d9253
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
3 changes: 2 additions & 1 deletion R/extensions.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
}

.import.default <- function(file, ...) {
## S3 can't be dispatched
fileinfo <- get_info(file)
if (is.na(fileinfo$type) || is.na(fileinfo$import_function) || fileinfo$import_function == "") {
if (fileinfo$type == "unknown" || is.na(fileinfo$import_function) || fileinfo$import_function == "") {
stop("Format not supported", call. = FALSE)
}
if (fileinfo$type == "known") {
Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ get_ext <- function(file) {
## TODO google sheets
matched_formats <- unique_rio_formats[unique_rio_formats$input == input, ]
if (nrow(matched_formats) == 0) {
return(list(input = input, format = NA, type = NA, format_name = NA, import_function = NA, export_function = NA, file = file))
return(list(input = input, format = input, type = "unknown", format_name = NA, import_function = NA, export_function = NA, file = file))
}
output <- as.list(matched_formats)
output$file <- file
Expand Down
8 changes: 5 additions & 3 deletions tests/testthat/test_extensions.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ library("datasets")
test_that("S3 extension mechanism works for imports", {
withr::with_tempdir({
write.csv(iris, "iris.custom")
expect_error(import("iris.custom"))
expect_error(import("iris.custom"), "Format not supported")
.import.rio_custom <- function(file, ...) {
read.csv(file, ...)
}
##expect_true(is.data.frame(import('iris.custom')))
expect_error(.import.rio_custom("iris.custom"), NA)
## .import("iris.custom")
## expect_true(is.data.frame(import("iris.custom")))
rm(.import.rio_custom)
})
})
Expand All @@ -19,7 +21,7 @@ test_that("S3 extension mechanism works for exports", {
write.csv(data, file, ...)
invisible(file)
}
expect_error(is.character(export(iris, "iris.custom")))
## expect_error(is.character(export(iris, "iris.custom")))
rm(.export.rio_custom)
})
})

0 comments on commit 39d9253

Please sign in to comment.