Skip to content

Commit e0b0719

Browse files
committed
use snapshot test for partially matching arguments
1 parent 63ca94e commit e0b0719

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# GeomXxx$parameters() does not contain partial matches
2+
3+
Code
4+
problems
5+
Output
6+
[1] "GeomBoxplot : `notch` with `notchwidth`"
7+
[2] "GeomContour : `arrow` with `arrow.fill`"
8+
[3] "GeomCurve : `arrow` with `arrow.fill`"
9+
[4] "GeomDensity2d: `arrow` with `arrow.fill`"
10+
[5] "GeomFunction : `arrow` with `arrow.fill`"
11+
[6] "GeomLine : `arrow` with `arrow.fill`"
12+
[7] "GeomPath : `arrow` with `arrow.fill`"
13+
[8] "GeomQuantile : `arrow` with `arrow.fill`"
14+
[9] "GeomSegment : `arrow` with `arrow.fill`"
15+
[10] "GeomSf : `arrow` with `arrow.fill`"
16+
[11] "GeomSpoke : `arrow` with `arrow.fill`"
17+
[12] "GeomStep : `arrow` with `arrow.fill`"
18+
19+
# StatXxx$parameters() does not contain partial matches
20+
21+
Code
22+
problems
23+
Output
24+
[1] "StatDensity : `n` with `na.rm`"
25+
[2] "StatDensity2d : `na.rm` with `n`"
26+
[3] "StatDensity2dFilled: `na.rm` with `n`"
27+
[4] "StatQuantile : `method` with `method.args`"
28+
[5] "StatSmooth : `method` with `method.args`, `n` with `na.rm`"
29+
[6] "StatSummary2d : `fun` with `fun.args`"
30+
[7] "StatSummaryHex : `fun` with `fun.args`"
31+

tests/testthat/test-function-args.R

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@ filter_args <- function(x) {
44
x[all_names]
55
}
66

7+
find_partial_match_pairs <- function(args) {
8+
if (length(args) < 2) {
9+
return(NULL)
10+
}
11+
combinations <- combn(args, 2L)
12+
contains <- startsWith(combinations[1, ], combinations[2, ]) |
13+
startsWith(combinations[2, ], combinations[1, ])
14+
15+
if (!any(contains)) {
16+
return(NULL)
17+
}
18+
19+
problem <- combinations[, contains, drop = FALSE]
20+
paste0("`", problem[1, ], "` with `", problem[2, ], "`")
21+
}
22+
23+
724
test_that("geom_xxx and GeomXxx$draw arg defaults match", {
825
ggplot2_ns <- asNamespace("ggplot2")
926
objs <- ls(ggplot2_ns)
@@ -73,3 +90,53 @@ test_that("stat_xxx and StatXxx$compute_panel arg defaults match", {
7390
)
7491
})
7592
})
93+
94+
# If the following tests fail, you may have introduced a potential partial match
95+
# in argument names. The code should be double checked that is doesn't
96+
# accidentally use `list$arg` when `list$arg_name` also exists. If that doesn't
97+
# occur, the snapshot can be updated.
98+
99+
test_that("GeomXxx$parameters() does not contain partial matches", {
100+
ggplot2_ns <- asNamespace("ggplot2")
101+
objs <- ls(ggplot2_ns)
102+
geom_class_names <- grep("^Geom", objs, value = TRUE)
103+
geom_class_names <- setdiff(geom_class_names, c("Geom"))
104+
105+
problems <- list()
106+
107+
for (geom_class_name in geom_class_names) {
108+
geom_obj <- ggplot2_ns[[geom_class_name]]
109+
params <- geom_obj$parameters()
110+
issues <- find_partial_match_pairs(params)
111+
if (length(issues) == 0) {
112+
next
113+
}
114+
problems[[geom_class_name]] <- issues
115+
}
116+
117+
problems <- vapply(problems, paste0, character(1), collapse = ", ")
118+
problems <- paste0(format(names(problems)), ": ", problems)
119+
expect_snapshot(problems)
120+
})
121+
122+
test_that("StatXxx$parameters() does not contain partial matches", {
123+
ggplot2_ns <- asNamespace("ggplot2")
124+
objs <- ls(ggplot2_ns)
125+
stat_class_names <- grep("^Stat", objs, value = TRUE)
126+
stat_class_names <- setdiff(stat_class_names, c("Stat"))
127+
128+
problems <- list()
129+
130+
for (stat_class_name in stat_class_names) {
131+
stat_obj <- ggplot2_ns[[stat_class_name]]
132+
params <- stat_obj$parameters()
133+
issues <- find_partial_match_pairs(params)
134+
if (length(issues) == 0) {
135+
next
136+
}
137+
problems[[stat_class_name]] <- issues
138+
}
139+
problems <- vapply(problems, paste0, character(1), collapse = ", ")
140+
problems <- paste0(format(names(problems)), ": ", problems)
141+
expect_snapshot(problems)
142+
})

0 commit comments

Comments
 (0)