Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Filtering list by minimum length fails when used in a property #139
Firstly, I'm new to both property-based testing and this library, so apologies if this is user error.
I'm trying to write a property for something that's only applicable to lists with at least two elements, but I can't seem to generate values for the test. Here's a reduced example:
defmodule MyAppTest do use ExUnit.Case use PropCheck property "for a list with at least two elements, the tail is never empty", [:verbose] do forall [_ | tail] <- such_that(l <- non_empty(list(term())), when: length(l) > 1) do Enum.any?(tail) end end end
When I run it, I get a "can't generate" error:
However, when I paste the same code into iex, it seems to work:
This is with propcheck 1.2.0, proper 1.3.0, elixir 1.9.4 and erlang 22.1.0, running on macOS 10.15.1.
For such cases, I usually build a generator that creates the first two elements and appends a possibly empty list of elements. This has the advantage that it always succeeds, so the constraint will never fail. Example:
def at_least_two_integers do let [first <- integer(), second <- integer(), many <- list(integer())], do: [first, second | many] end
This can then be used in
property "all integers" do forall ints <- at_least_two_integers() do Enum.all?(ints, &is_integer/1) end end
@alfert it kinda already is. Might be clearer, though.