propcheck icon indicating copy to clipboard operation
propcheck copied to clipboard

Why do properties have to return boolean results?

Open TylerPachal opened this issue 3 years ago • 1 comments

Hello,

I am wondering why properties have to return a boolean result instead of a truthy result like regular ExUnit assertions. As far as I know, it means that I cannot write properties which assert using a pattern match like the following, because the assert statement with a single = does not return a boolean value:

property "returns a map" do
  forall i <- pos_integer() do
    assert %{val: ^i} = make_map(i)      # This doesn't work and raises an error {:error, :non_boolean_result}
  end
end

This is just a toy example, often I have more complicated pattern matches where it would not be as easy to swap the single = for a double ==.

Is it possible to change the assertion to check for a truthy value instead of a boolean? Or is that a constraint of PropEr?

TylerPachal avatar Dec 07 '20 16:12 TylerPachal

Sorry for the late delay, but if you want to assert a match, I would suppose to use match?:

property "returns a map" do
  forall i <- pos_integer() do
    match?(%{val: ^i}, make_map(i))
  end
end

But I assume that your problem is that assert requires a boolean expression according to the Elixir docs. In PropCheck you can use assertions which is helpful alone for the better error reporting.

alfert avatar Apr 25 '21 20:04 alfert