-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add unit test to check_translation_csv() - closes #61
- Loading branch information
Showing
7 changed files
with
109 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,50 @@ | ||
#' csv_check | ||
#' | ||
#' | ||
#' @description Check the CSV written by hand | ||
#' | ||
#' @param path path to file to check | ||
#' @param source_encoding Encoding of the source file | ||
#' @param new_path Where to save the modified file | ||
#' @return The return value, if any, from executing the utility. | ||
#' | ||
#' @importFrom dplyr mutate_all | ||
#' @export | ||
check_translation_csv <- function(path) { | ||
if (!file.exists(path)) { | ||
stop("File doesn't exists") | ||
} | ||
if (tools::file_ext(path) != "csv") { | ||
stop("File should be a csv file") | ||
} | ||
fls <- read.csv(path) | ||
if ( | ||
names(fls)[1] != "entry" | ||
) { | ||
stop("The first column should be named 'entry'") | ||
} | ||
if ( | ||
!"en" %in% names(fls) | ||
) { | ||
stop("Their is no 'en' column") | ||
} | ||
if ( | ||
!"fr" %in% names(fls) | ||
) { | ||
stop("Their is no 'fr' column") | ||
} | ||
cli::cli_alert_success("CSV seems correct") | ||
check_translation_csv <- function(path, source_encoding = "latin1", new_path = path) { | ||
if (!file.exists(path)) { | ||
stop("File doesn't exists") | ||
} | ||
if (tools::file_ext(path) != "csv") { | ||
stop("path should be a csv file") | ||
} | ||
if (tools::file_ext(new_path) != "csv") { | ||
stop("new_path should be a csv file") | ||
} | ||
fls <- read.csv(path, encoding = source_encoding) | ||
if ( | ||
names(fls)[1] != "entry" | ||
) { | ||
stop("The first column should be named 'entry'") | ||
} | ||
if ( | ||
!"en" %in% names(fls) | ||
) { | ||
stop("Their is no 'en' column") | ||
} | ||
if ( | ||
!"fr" %in% names(fls) | ||
) { | ||
stop("Their is no 'fr' column") | ||
} | ||
if (source_encoding != "UTF-8") { | ||
fls <- fls %>% mutate_all(enc2utf8) | ||
write.csv(fls, file = new_path, | ||
fileEncoding = "UTF-8", | ||
# col.names = TRUE, | ||
row.names = FALSE) | ||
cli::cli_alert_success("CSV was re-encoded to UTF-8") | ||
} else { | ||
file.copy(path, new_path) | ||
} | ||
cli::cli_alert_success("CSV seems correct") | ||
|
||
return(fls) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
entry,en,fr | ||
toto,t�t�,t�t� |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
entry,en,fr | ||
toto,tùtù,tàtà |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
trad_latin <- system.file("reprex", "trad_latin1.csv", package = "diades.atlas") | ||
|
||
new_output_test <- tempfile(fileext = ".csv") | ||
|
||
test_that("check_translation_csv works with encoding Latin1", { | ||
output <- check_translation_csv( | ||
path = trad_latin, | ||
source_encoding = "latin1", new_path = new_output_test) | ||
expect_equal(names(output), c("entry", "en", "fr")) | ||
expect_equal(as.character(output[1,]), c("toto", "t\u00f9t\u00f9", "t\u00e0t\u00e0")) | ||
expect_equal( | ||
readLines(new_output_test)[1], | ||
c("\"entry\",\"en\",\"fr\"") | ||
) | ||
|
||
# Read re-encoded one: new_output_test | ||
output <- check_translation_csv( | ||
path = new_output_test, | ||
source_encoding = "UTF-8", new_path = new_output_test) | ||
expect_equal(names(output), c("entry", "en", "fr")) | ||
expect_equal(as.character(output[1,]), c("toto", "t\u00f9t\u00f9", "t\u00e0t\u00e0")) | ||
expect_equal( | ||
readLines(new_output_test)[1], | ||
c("\"entry\",\"en\",\"fr\"") # Copied with write.csv | ||
) | ||
}) | ||
|
||
# with uf8 ---- | ||
trad_utf8 <- system.file("reprex", "trad_utf8.csv", package = "diades.atlas") | ||
new_output_test <- tempfile(fileext = ".csv") | ||
|
||
test_that("check_translation_csv works with encoding UTF8 directly", { | ||
output <- check_translation_csv( | ||
path = trad_utf8, | ||
source_encoding = "UTF-8", new_path = new_output_test) | ||
expect_equal(names(output), c("entry", "en", "fr")) | ||
expect_equal(as.character(output[1,]), c("toto", "t\u00f9t\u00f9", "t\u00e0t\u00e0")) | ||
expect_equal( | ||
readLines(new_output_test)[1], | ||
c("entry,en,fr") # Copied as is | ||
) | ||
}) |