Skip to content

Commit 6593048

Browse files
committed
test by mocking
1 parent 060b7cd commit 6593048

File tree

5 files changed

+80
-3
lines changed

5 files changed

+80
-3
lines changed

R/guides-.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ validate_guide <- function(guide) {
920920
if (isTRUE(grepl("::", guide))) {
921921
# Append prefix as namespaces to search environments
922922
prefix <- sub("::.*", "", guide)
923-
search_env <- c(search_env, list(asNamespace(prefix)))
923+
search_env <- c(search_env, list(as_namespace(prefix)))
924924
# Remove prefix from guide name
925925
guide <- sub(".*::", "", guide)
926926
}

R/scale-type.R

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ find_scale <- function(aes, x, env = parent.frame()) {
1313
if (isTRUE(grepl("::", scale))) {
1414
# Append prefix as namepaces to search environments
1515
prefix <- sub("::.*", "", scale)
16-
search_env <- c(search_env, list(asNamespace(prefix)))
16+
search_env <- c(search_env, list(as_namespace(prefix)))
1717
# Remove prefix from scale name
1818
scale <- sub(".*::", "", scale)
1919
}
@@ -41,7 +41,7 @@ find_global <- function(name, env, mode = "any") {
4141
if (!is.list(env)) {
4242
env <- list(env)
4343
}
44-
env <- c(env, list(asNamespace("ggplot2")))
44+
env <- c(env, list(as_namespace("ggplot2")))
4545

4646
for (e in env) {
4747
if (exists(name, envir = e, mode = mode)) {
@@ -52,6 +52,13 @@ find_global <- function(name, env, mode = "any") {
5252
NULL
5353
}
5454

55+
# This exists for testing purposes (mocking) only
56+
as_namespace <- function(...) NULL
57+
on_load({
58+
as_namespace <- base::asNamespace
59+
})
60+
61+
5562
#' Determine default scale type
5663
#'
5764
#' You will need to define a method for this method if you want to extend

tests/testthat/_snaps/guides.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737

3838
`nrow` * `ncol` needs to be larger than the number of breaks (5).
3939

40+
# validate_guide finds guides with namespace prefixes
41+
42+
Unknown guide: bar
43+
4044
# get_guide_data retrieves keys appropriately
4145

4246
Code

tests/testthat/test-guides.R

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,34 @@ test_that("guide specifications are properly checked", {
5151
expect_snapshot_error(ggplotGrob(p))
5252
})
5353

54+
55+
test_that("validate_guide finds guides with namespace prefixes", {
56+
57+
# Mock foo::bar as namespace
58+
fake_namespace <- new_environment()
59+
env_bind(
60+
fake_namespace,
61+
guide_bar = function(...) guide_legend(title = "bar", ...)
62+
)
63+
64+
local_mocked_bindings(
65+
as_namespace = function(ns, ...) {
66+
if (identical(ns, "foo")) {
67+
return(fake_namespace)
68+
} else {
69+
base::asNamespace(ns, ...)
70+
}
71+
}
72+
)
73+
74+
# Without prefix, we don't know here to look for guide_bar
75+
expect_snapshot_error(validate_guide("bar"))
76+
# With prefix, we know the namespace where to look for guide_bar
77+
g <- validate_guide("foo::bar")
78+
expect_true(is_guide(g))
79+
expect_equal(g$params$title, "bar")
80+
})
81+
5482
test_that("guide_coloursteps and guide_bins return ordered breaks", {
5583
scale <- scale_colour_viridis_c(breaks = c(2, 3, 1))
5684
scale$train(c(0, 4))

tests/testthat/test-scale-type.R

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,41 @@ test_that("find_scale gives sensible calls to scales", {
2424
quote(scale_colour_discrete())
2525
)
2626
})
27+
28+
test_that("find_scale finds scales with namespace prefixes", {
29+
30+
# Mock foo::bar as namespace
31+
fake_namespace <- new_environment()
32+
env_bind(
33+
fake_namespace,
34+
scale_x_bar = function(...) scale_x_continuous(name = "barname")
35+
)
36+
37+
local_mocked_bindings(
38+
as_namespace = function(ns, ...) {
39+
if (identical(ns, "foo")) {
40+
return(fake_namespace)
41+
} else {
42+
base::asNamespace(ns, ...)
43+
}
44+
}
45+
)
46+
47+
# No loaded namespace has a scale_x_bar
48+
registerS3method(
49+
"scale_type", "bar",
50+
method = function(x) "bar"
51+
)
52+
53+
sc <- find_scale("x", structure(1, class = "bar"))
54+
expect_null(sc)
55+
56+
# With prefix, we know the namespace where to look for scale_x_bar
57+
registerS3method(
58+
"scale_type", "bar",
59+
method = function(x) "foo::bar"
60+
)
61+
62+
sc <- find_scale("x", structure(1, class = "bar"))
63+
expect_equal(sc$name, "barname")
64+
})

0 commit comments

Comments
 (0)