Skip to content

Commit

Permalink
Test i18n_set_language_option() in mock_exercise()
Browse files Browse the repository at this point in the history
  • Loading branch information
rossellhayes committed Jul 20, 2021
1 parent fde70e8 commit 68ad547
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
8 changes: 7 additions & 1 deletion R/i18n.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,13 @@ i18n_set_language_option <- function(language = NULL) {
}

knitr::opts_knit$set(tutorial.language = language)
Sys.setenv("LANGUAGE" = language)

switch(
language,
pt = Sys.setenv("LANGUAGE" = "pt_BR"),
Sys.setenv("LANGUAGE" = language)
)


invisible(current)
}
Expand Down
36 changes: 31 additions & 5 deletions tests/testthat/test-i18n.R
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,41 @@ test_that("i18n_span() returns an i18n span", {
})

test_that("i18n_set_language_option() changes message language", {
withr::local_envvar(list("LANGUAGE" = Sys.getenv("LANGUAGE")))
withr::defer(i18n_set_language_option("en"))

i18n_set_language_option("fr")
expect_equal(knitr::opts_knit$get("tutorial.language"), "fr")
expect_equal(Sys.getenv("LANGUAGE"), "fr")
expect_error(mean$x, "objet de type 'closure' non indiçable")

i18n_set_language_option("pt_BR")
expect_equal(knitr::opts_knit$get("tutorial.language"), "pt_BR")
ex <- mock_exercise(user_code = "mean$x")
ex$tutorial$language <- "fr"
result <- evaluate_exercise(ex, new.env())
expect_equal(result$error_message, "objet de type 'closure' non indiçable")

ex <- mock_exercise(
user_code = "mean$x",
global_setup = "i18n_set_language_option('fr')"
)
result <- evaluate_exercise(ex, new.env(), evaluate_global_setup = TRUE)
expect_equal(result$error_message, "objet de type 'closure' non indiçable")
})

test_that("i18n_set_language_option() has a special case for Portuguese", {
withr::defer(i18n_set_language_option("en"))

i18n_set_language_option("pt")
expect_equal(knitr::opts_knit$get("tutorial.language"), "pt")
expect_equal(Sys.getenv("LANGUAGE"), "pt_BR")
expect_error(mean$x, "objeto de tipo 'closure' não possível dividir em subconjuntos")

ex <- mock_exercise(user_code = "mean$x")
ex$tutorial$language <- "pt"
result <- evaluate_exercise(ex, new.env())
expect_equal(result$error_message, "objeto de tipo 'closure' não possível dividir em subconjuntos")

ex <- mock_exercise(
user_code = "mean$x",
global_setup = "i18n_set_language_option('pt')"
)
result <- evaluate_exercise(ex, new.env(), evaluate_global_setup = TRUE)
expect_equal(result$error_message, "objeto de tipo 'closure' não possível dividir em subconjuntos")
})

0 comments on commit 68ad547

Please sign in to comment.