--- title: "fuzzr: Fuzz-Testing for R Functions" author: "Matthew Lincoln" date: "`r Sys.Date()`" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{fuzzr: Fuzz-Testing for R Functions} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- R's dynamic typing can be both blessing and curse. One drawback is that a function author must decide how to check which inputs should be accepted, and which should throw warnings or errors. fuzzr helps you to check how cleanly and informatively your function responds to a range of unexpected inputs. Say we build a function intended to a single string and a single integer, repeat the string that number of times, and paste it together using a given delimiter: ```{r} my_function <- function(x, n, delim = " - ") { paste(rep(x, n), collapse = delim) } my_function("fuzz", 7) ``` Simple enough. However, this function quickly breaks if we pass in somewhat unexpected values: ```{r, error = TRUE, purl = FALSE} my_function("fuzz", "bar") ``` Let's test this with a full battery of fuzz tests: ```{r, results = "asis"} library(fuzzr) # Note that, while we are specifically fuzz testing the 'n' argument, we still # need to provide an 'x' argument to pass along to my_function(). We do not have # to supply a delimiter, as my_function() declares a default value for this # argument. my_fuzz_results <- fuzz_function(my_function, "n", x = 1:3, tests = test_all()) # Produce a data frame summary of the results fuzz_df <- as.data.frame(my_fuzz_results) knitr::kable(fuzz_df) ``` Almost all the unexpected values for `n` throw the fairly generic warning `invalid 'times' argument`, which really comes from the `rep` function within `my_function`. Some types, like doubles, factors, and even dates (!) don't throw errors, but instead return a result. We can check the value of that result with `fuzz_value()`, and the call originating it with `fuzz_call()`, both of which search for the first test result that matches a regex of the test name. The argument should match the name of the argument tested with in `fuzz_function`: ```{r} fuzz_call(my_fuzz_results, n = "dbl_single") fuzz_value(my_fuzz_results, n = "dbl_single") fuzz_call(my_fuzz_results, n = "date_single") # Hm, dates can be coerced into very large integers. Let's see how long this # result is. nchar(fuzz_value(my_fuzz_results, n = "date_single")) # Oh dear. ``` Perhaps we might chose to enforce this with a tailored type check (using [assertthat](https://cran.r-project.org/package=assertthat)) that catches unexpected values and produces a more informative error message. ```{r, results = "asis"} my_function_2 <- function(x, n, delim = " - ") { assertthat::assert_that(assertthat::is.count(n)) paste(rep(x, n), collapse = delim) } # We will abbreviate this check by only testing against double and date vectors fuzz_df_2 <- as.data.frame(fuzz_function(my_function_2, "n", x = "fuzz", tests = c(test_dbl(), test_date()))) knitr::kable(fuzz_df_2) ``` ## Fuzzing multiple arguments `fuzz_function` works by mapping several test inputs over one argument of a function while keeping the other arguments static. `p_fuzz_function` lets you specify a battery of tests for each variable as a named list of named lists. Every test combination is then run. These tests can be specified using the provided functions like `test_char`, or with variable inputs you provide. Remember that each test condition must, itself, be named. ```{r, results = 'asis'} p_args <- list( x = list( simple_char = "test", numbers = 1:3 ), n = test_all(), delim = test_all()) pr <- p_fuzz_function(my_function_2, p_args) prdf <- as.data.frame(pr) knitr::kable(head(prdf)) ``` Specifying multiple arguments can quickly compound the number of total test combinations to run, so `p_fuzz_function` will prompt the user to confirm running more than 500,000 tests at once.