Skip to content

Use constructive::construct() to give values that make the test fail #21

@etiennebacher

Description

@etiennebacher

Hi, great package, thank you!

When multiple arguments are involved, it can be annoying to manually write all the values that trigger the error:

library(quickcheck)
library(testthat)

foo <- function(a, b, c, d) {
  if (a > 1) stop("error")
}

test_that("foo() works", {
  for_all(
    a = character_(any_na = TRUE),
    b = character_(any_na = TRUE),
    c = character_(any_na = TRUE),
    d = character_(any_na = TRUE),
    property = function(a, b, c, d) {
      foo(a, b, c, d)
    }
  )
})
#> ── Failure: foo() works ────────────────────────────────────────────────────────
#> Falsifiable after 1 tests, and 8 shrinks
#> <simpleError in foo(a, b, c, d): error>
#> Counterexample:
#> $a
#> [1] "6#)APN"
#> 
#> $b
#> [1] NA
#> 
#> $c
#> [1] "RvCt/a4"
#> 
#> $d
#> [1] "pw{"
#> 
#> Backtrace:
#>     ▆
#>  1. └─quickcheck::for_all(...)
#>  2.   └─hedgehog::forall(...)
#> Error:
#> ! Test failed

It would be great if the print method could use constructive::construct() or something similar to make it easier to copy-paste the failing example:

l <- list(a = 1, b = 2, c = 3)

l
#> $a
#> [1] 1
#> 
#> $b
#> [1] 2
#> 
#> $c
#> [1] 3

constructive::construct(l)
#> list(a = 1, b = 2, c = 3)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions