Last active
July 11, 2022 01:35
-
-
Save krlmlr/2002077dea9120d9e6006aaea2bd7973 to your computer and use it in GitHub Desktop.
Generate all combinations with named and unnamed arguments for a call
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
generate <- function(named, unnamed, used = exprs()) { | |
if (length(named) == 0 && length(unnamed) == 0) { | |
return(deparse(call2("check_api", !!!used), width.cutoff = 500)) | |
} | |
out <- character() | |
for (i in seq_along(named)) { | |
out <- c(out, generate(named[-i], unnamed, c(used, named[i]))) | |
} | |
if (length(unnamed) > 0) { | |
out <- c(out, generate(named, unnamed[-1], c(used, unnamed[[1]]))) | |
} | |
out | |
} | |
args <- exprs( | |
dm = dm, | |
table = a, | |
b > 1, | |
) | |
generate_named <- function(args, named = list(), unnamed = list()) { | |
if (length(args) == 0) { | |
out <- generate(named, unnamed) | |
writeLines(c( | |
"expect_same(", | |
paste0(" ", out, collapse = ",\n"), | |
")" | |
)) | |
return(invisible()) | |
} | |
generate_named(args[-1], named, c(unnamed, args[[1]])) | |
if (names2(args)[[1]] != "") { | |
generate_named(args[-1], c(named, args[1]), unnamed) | |
} | |
invisible() | |
} | |
generate_named(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment