1context("function-args")
2
3filter_args <- function(x) {
4  all_names <- names(x)
5  all_names <- setdiff(all_names, c("self", "data", "scales", "coordinates", "..."))
6  x[all_names]
7}
8
9test_that("geom_xxx and GeomXxx$draw arg defaults match", {
10  ggplot2_ns <- asNamespace("ggplot2")
11  objs <- ls(ggplot2_ns)
12  geom_fun_names <- objs[grepl("^(geom|annotation)_", objs)]
13  # These aren't actually geoms, or need special parameters and can't be tested this way.
14  geom_fun_names <- setdiff(
15    geom_fun_names,
16    c("geom_map", "geom_sf", "geom_smooth", "geom_column", "geom_area",
17      "geom_density", "annotation_custom", "annotation_map", "annotation_raster",
18      "annotation_id")
19  )
20
21  # For each geom_xxx function and the corresponding GeomXxx$draw and
22  # GeomXxx$draw_groups functions, make sure that if they have same args, that
23  # the args have the same default values.
24  lapply(geom_fun_names, function(geom_fun_name) {
25    geom_fun    <- ggplot2_ns[[geom_fun_name]]
26    geom <- geom_fun()$geom
27    if (!inherits(geom, "Geom")) # for geoms that return more than one thing
28      return()
29
30    fun_args <- formals(geom_fun)
31    draw_args <- c(
32      ggproto_formals(geom$draw_layer),
33      ggproto_formals(geom$draw_group)
34    )
35    draw_args <- filter_args(draw_args)
36
37    common_names <- intersect(names(fun_args), names(draw_args))
38
39    expect_identical(fun_args[common_names], draw_args[common_names],
40      info = paste0("Mismatch between arg defaults for ", geom_fun_name,
41        " and ", class(geom_fun()$geom)[1], "'s $draw and/or $draw_group functions.")
42    )
43  })
44})
45
46test_that("stat_xxx and StatXxx$compute_panel arg defaults match", {
47  ggplot2_ns <- asNamespace("ggplot2")
48  objs <- ls(ggplot2_ns)
49  stat_fun_names <- objs[grepl("^stat_", objs)]
50  # These aren't actually stats, or need special parameters and can't be tested this way.
51  stat_fun_names <- setdiff(
52    stat_fun_names,
53    c("stat_function", "stat_sf")
54  )
55
56  # For each stat_xxx function and the corresponding StatXxx$compute_panel and
57  # StatXxx$compute_group functions, make sure that if they have same args, that
58  # the args have the same default values.
59  lapply(stat_fun_names, function(stat_fun_name) {
60    stat_fun         <- ggplot2_ns[[stat_fun_name]]
61    calculate        <- stat_fun()$stat$compute_panel
62    calculate_groups <- stat_fun()$stat$compute_group
63
64    fun_args <- formals(stat_fun)
65    calc_args <- c(ggproto_formals(calculate), ggproto_formals(calculate_groups))
66    calc_args <- filter_args(calc_args)
67
68    common_names <- intersect(names(fun_args), names(calc_args))
69
70    expect_identical(fun_args[common_names], calc_args[common_names],
71      info = paste0("Mismatch between arg defaults for ", stat_fun_name,
72        " and ", class(stat_fun()$stat)[1], "'s $compute_panel and/or $compute_group functions.")
73    )
74  })
75})
76