Skip to content

Commit

Permalink
Merge pull request #126 from Appsilon/edu.unit-tests
Browse files Browse the repository at this point in the history
#125 Write unit tests for shiny.i18n package
  • Loading branch information
jakubnowicki committed Jul 26, 2023
2 parents 3b8ebec + 54ce827 commit 132348b
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 23 deletions.
9 changes: 6 additions & 3 deletions R/preproc.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ extract_key_expressions <- function(text, handle = "i18n") {
#' @keywords internal
save_to_json <- function(key_expressions, output_path = NULL) {
list_to_save <- list(
translation = lapply(key_expressions,
function(x) list(key = unbox(x))),
languages = "key")
translation = lapply(
key_expressions,
function(x) list(key = unbox(x))
),
languages = "key"
)

if (is.null(output_path)) output_path <- "translation.json"
write_json(list_to_save, output_path)
Expand Down
11 changes: 7 additions & 4 deletions R/translator.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,13 @@ Translator <- R6Class( #nolint
automatic_translate = function(keyword, api = "google") {
if (identical(private$translation_language, character(0)))
stop("Please provide a 'translation_language'. Check docs how.")
tr <- switch(api,
google = translate_with_google_cloud(keyword,
private$translation_language),
stop("This 'api' is not supported.")
tr <- switch(
api,
google = translate_with_google_cloud(
keyword,
private$translation_language
),
stop("This 'api' is not supported.")
)
tr
},
Expand Down
12 changes: 6 additions & 6 deletions R/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
#' @import shiny
#' @keywords internal
i18n_state <- function(init_language) {
shiny::tags$div(
id = "i18n-state",
`data-keylang` = init_language,
`data-lang` = init_language,
style = "visibility: hidden; margin: 0; padding: 0; overflow: hidden; max-height: 0;"
)
shiny::tags$div(
id = "i18n-state",
`data-keylang` = init_language,
`data-lang` = init_language,
style = "visibility: hidden; margin: 0; padding: 0; overflow: hidden; max-height: 0;"
)
}

#' Use i18n in UI
Expand Down
28 changes: 18 additions & 10 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@
#' @importFrom utils read.csv
#' @keywords internal
multmerge <- function(filenames, sep = ",") {
datalist <- lapply(filenames, function(x) {
read.csv(file = x,
sep = sep,
header = TRUE,
encoding = "UTF-8")
})
datalist <- lapply(
filenames,
function(x) {
read.csv(
file = x,
sep = sep,
header = TRUE,
encoding = "UTF-8"
)
}
)
if (!validate_names(datalist))
stop("Key translation is not the same in all files.")
Reduce(
Expand Down Expand Up @@ -99,12 +104,15 @@ read_and_merge_csvs <- function(dir_path, sep = ",") {
#' @import yaml
#' @keywords internal
load_local_config <- function(yaml_config_path) {
if (!is.null(yaml_config_path) &&
file.exists(yaml_config_path)) {
if (!is.null(yaml_config_path) && file.exists(yaml_config_path)) {
local_config <- yaml::yaml.load_file(yaml_config_path)
} else {
warning(paste0("You didn't specify config translation yaml file. ",
"Default settings are used."))
warning(
paste0(
"You didn't specify config translation yaml file. ",
"Default settings are used."
)
)
local_config <- list()
}
local_config
Expand Down
32 changes: 32 additions & 0 deletions tests/testthat/test_automatic.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
context("automatic")

test_that("translate_with_google_cloud Should error if API is not set", {

with_mock(
`googleLanguageR::gl_translate` = function(txt_to_translate, target) {
message("Mock: gl_translate called")
stop("forced error")
}, {
# Arrange
txt <- "Hello, how are you?"
target_lang <- "fr"

# Act
result <- evaluate_promise(
translate_with_google_cloud(txt, target_lang)
)

}
)

# Assert
expected_error_messages <- c(
"Mock: gl_translate called\n",
"!!!\n",
"forced error\n",
"Did you set you google cloud API credentials correctly?\n",
"Check how here: https://github.com/ropensci/googleLanguageR/\n"
)
expect_equal(result$message, expected_error_messages)

})
29 changes: 29 additions & 0 deletions tests/testthat/test_preproc.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,32 @@ test_that("test save_to_csv", {
file.remove(tmp_json)
file.remove("tmp.R")
})

test_that("create_translation_addin has proper behavior for rstudio addin", {
temp <- tempfile()
# Mock the behavior of RStudio API calls
with_mock(
`rstudioapi::showDialog` = function(...) {
message("Mock: showDialog called")
TRUE
},
`rstudioapi::getActiveDocumentContext` = function() {
message("Mock: getActiveDocumentContext called")
list(path = temp)
},
`rstudioapi::showQuestion` = function(...) {
message("Mock: showQuestion called")
TRUE
},
`create_translation_file` = function(path, format) {
# Mock the behavior of create_translation_file
expect_equal(path, temp)
expect_equal(format, "json")
},
{
# Call the function
create_translation_addin()
}
)

})
56 changes: 56 additions & 0 deletions tests/testthat/test_ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,59 @@ test_that("test i18n_state", {
# expect returning shiny tag
expect_equal(class(i18n_state("a")), "shiny.tag")
})

test_that("usei18n returns proper format of shiny.tags from JSON parameters", {
# Arrange
i18n <- Translator$new(translation_json_path = "data/translation.json")
i18n$set_translation_language("en")
# Act
i18ntag <- usei18n(i18n)
# Assert
expect_equal(
class(i18ntag),
c("shiny.tag.list", "list")
)
expect_equal(
as.character(i18ntag[[1]]$children[[1]]),
paste0(
"<script>var i18n_translations = ",
"[{\"pl\":\"Witaj Shiny!\",\"en\":\"Hello Shiny!\",\"_row\":\"Hello Shiny!\"},",
"{\"pl\":\"Liczba podziałek\",\"en\":\"Number of bins:\",\"_row\":\"Number of bins:\"},",
"{\"pl\":\"To jest opis obrazka.\",\"en\":\"This is description of the plot.\",\"_row\":",
"\"This is description of the plot.\"},",
"{\"pl\":\"Histogram x\",\"en\":\"Histogram of x\",\"_row\":\"Histogram of x\"},",
"{\"pl\":\"Częstotliwość\",\"en\":\"Frequency\",\"_row\":\"Frequency\"}]</script>"
)
)
expect_equal(
as.character(i18ntag[[1]]$children[[2]]),
paste0("<script src=\"shiny_i18n/shiny-i18n.js\"></script>")
)
expect_equal(
i18ntag[[2]]$attribs,
list(
id = "i18n-state",
style = "visibility: hidden; margin: 0; padding: 0; overflow: hidden; max-height: 0;"
)
)
})


test_that("update_lang changes the selected language in session$userData$shiny.i18n$lang", {
# Arrange
i18n <- Translator$new(translation_json_path = "data/translation.json")
i18n$set_translation_language("en")

# Act
server <- function(input, output, session) {
observeEvent(input$selected_language, {
# Here is where we update language in session
shiny.i18n::update_lang(input$selected_language)
})
}
# Assert
testServer(server, {
session$setInputs(selected_language = "pl")
expect_equal(session$userData$shiny.i18n$lang(), "pl")
})
})
16 changes: 16 additions & 0 deletions tests/testthat/test_zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,19 @@ test_that("test get_i18n_config", {
expect_type(get_i18n_config("cultural_bignumer_mark"), "character")
expect_error(get_i18n_config("cultural_bignumer_markx"))
})

test_that(".onLoad correctly assigns proper content from package config", {
# Assert
expect_equal(
class(.i18_config),
c("list")
)
expect_equal(
.i18_config,
list(
cultural_date_format = "%d/%m/%Y",
cultural_bignumer_mark = " ",
cultural_punctuation_mark = ","
)
)
})

0 comments on commit 132348b

Please sign in to comment.