Skip to content

Commit

Permalink
#125 Review update
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduardodudu committed Jul 25, 2023
1 parent 44827c9 commit a20f41b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 39 deletions.
46 changes: 29 additions & 17 deletions tests/testthat/test_automatic.R
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
context("automatic")

test_that("translate_with_google_cloud", {
it("Should check if API is set", {
withr::with_envvar(
new = c(
GL_AUTH = "None"
), {
# Arrange
txt <- "Hello, how are you?"
target_lang <- "fr"
# Act
result <- evaluate_promise(
translate_with_google_cloud(txt, target_lang)
)
# Assert
expect_equal(length(result$messages), 7)
})
})
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)

})
2 changes: 1 addition & 1 deletion tests/testthat/test_preproc.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ test_that("test save_to_csv", {
file.remove("tmp.R")
})

test_that("Test create_translation_addin", {
test_that("create_translation_addin has proper behavior for rstudio addin", {
temp <- tempfile()
# Mock the behavior of RStudio API calls
with_mock(
Expand Down
37 changes: 20 additions & 17 deletions tests/testthat/test_ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test_that("test i18n_state", {
expect_equal(class(i18n_state("a")), "shiny.tag")
})

test_that("Test usei18n", {
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")
Expand All @@ -18,44 +18,47 @@ test_that("Test usei18n", {
class(i18ntag),
c("shiny.tag.list", "list")
)
expect_equal(length(i18ntag[[1]]$children), 2)
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("Test update_lang", {
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
ui <- fluidPage(
shiny.i18n::usei18n(i18n),
selectInput(
"selected_language",
i18n$t("Change language"),
choices = i18n$get_languages(),
selected = i18n$get_key_translation()
)
)
server <- function(input, output, session) {

observeEvent(input$selected_language, {
# Here is where we update language in session
shiny.i18n::update_lang(input$selected_language)
})

reactive_language <- reactive(input$selected_language)
}
# Assert
testServer(server, {
session$setInputs(selected_language = "pl")
expect_equal(reactive_language(), "pl")
expect_equal(session$userData$shiny.i18n$lang(), "pl")
})
})
11 changes: 7 additions & 4 deletions tests/testthat/test_zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ test_that("test get_i18n_config", {
expect_error(get_i18n_config("cultural_bignumer_markx"))
})

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

0 comments on commit a20f41b

Please sign in to comment.