Skip to content

Commit fa26a55

Browse files
authored
Scale call check (#5443)
* Fix #5436 * Add tests
1 parent ad33741 commit fa26a55

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

R/scale-continuous.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ scale_x_continuous <- function(name = waiver(), breaks = waiver(),
8585
guide = waiver(), position = "bottom",
8686
sec.axis = waiver()) {
8787
call <- caller_call()
88-
if (is.null(call) || !startsWith(as.character(call[[1]]), "scale_")) {
88+
if (is.null(call) || !any(startsWith(as.character(call[[1]]), "scale_"))) {
8989
call <- current_call()
9090
}
9191
sc <- continuous_scale(
@@ -111,7 +111,7 @@ scale_y_continuous <- function(name = waiver(), breaks = waiver(),
111111
guide = waiver(), position = "left",
112112
sec.axis = waiver()) {
113113
call <- caller_call()
114-
if (is.null(call) || !startsWith(as.character(call[[1]]), "scale_")) {
114+
if (is.null(call) || !any(startsWith(as.character(call[[1]]), "scale_"))) {
115115
call <- current_call()
116116
}
117117
sc <- continuous_scale(

tests/testthat/test-scales.R

+30
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,36 @@ test_that("scale functions accurately report their calls", {
648648
expect_equal(calls, construct)
649649
})
650650

651+
test_that("scale call is found accurately", {
652+
653+
call_template <- quote(scale_x_continuous(trans = "log10"))
654+
655+
sc <- do.call("scale_x_continuous", list(trans = "log10"))
656+
expect_equal(sc$call, call_template)
657+
658+
sc <- inject(scale_x_continuous(!!!list(trans = "log10")))
659+
expect_equal(sc$call, call_template)
660+
661+
sc <- exec("scale_x_continuous", trans = "log10")
662+
expect_equal(sc$call, call_template)
663+
664+
foo <- function() scale_x_continuous(trans = "log10")
665+
expect_equal(foo()$call, call_template)
666+
667+
env <- new_environment()
668+
env$bar <- function() scale_x_continuous(trans = "log10")
669+
expect_equal(env$bar()$call, call_template)
670+
671+
# Now should recognise the outer function
672+
scale_x_new <- function() {
673+
scale_x_continuous(trans = "log10")
674+
}
675+
expect_equal(
676+
scale_x_new()$call,
677+
quote(scale_x_new())
678+
)
679+
})
680+
651681
test_that("training incorrectly appropriately communicates the offenders", {
652682

653683
sc <- scale_colour_viridis_d()

0 commit comments

Comments
 (0)