Property-Based Testing with PropEr, Erlang, and Elixir:the order of the elements in the comparison expression(page 53)

I’m wondering why you chose the following sequence in your expression:
Function under test =:= reference expression

prop_biggest() ->
    ?FORALL(List, (list(integer())),
            begin
                biggest(List) =:= lists:last(lists:sort(List))
            end).

EUnit macros using assertEqual(Expect, Expr), whehe EUnit uses the opposite order of items.

biggest_test() ->
 ?assert(5 =:= biggest([1, 2, 3, 4, 5])),
 ?assert(8 =:= biggest([3, 8, 7, -1])),
 ?assert(0 =:= biggest([0])), 
 ?assert(5 =:= biggest([-10, 5, -901])).

I understand that the order of the elements doesn’t matter. When you proposed your order, what were you guided by, what were your motives?

That’s an interesting question. I don’t think it’s a super conscious choice, but I tend to use the order of other assertion macros, where for example the format is ?assertMatch(Pattern, Expression) (this is the only one where it’s very sensitive, otherwise you just get the expected/actual order reversed in a report).

I think this, along with pattern matching generally having the assertion left-side, makes me compare that way. For example, many years ago, before people used EUnit macro in common test, the pattern for assertions would have just been Expected = lists:last(lists:sort(List)), Expected = biggest(List) and then letting the est crash if it’s wrong.

In this case, I appear to have swapped the order with what I generally use or would suggest to use, since the model should be driving the test!

1 Like