antonmi / espec

Elixir Behaviour Driven Development
Other
808 stars 62 forks source link

Pattern matching: Improve formatting for MatchErrors #232

Closed sascha-wolf closed 7 years ago

sascha-wolf commented 7 years ago

When doing pattern matching in ExUnit via the match operator (=), a failed match will be nicely formatted in the following fashion:

Without assert

ExUnit - MatchErrror without assert

Using assert

ExUnit - MatchError using assert


ESpec on the other hand does not handle MatchErrors in any special way, so a failed match will look like this:

ESpec - MatchError

IMO ESpec provides a great deal of information for failed tests, often being superior to ExUnit in that regard. As such I think this would be a useful extension and improve the testing experience.

sascha-wolf commented 7 years ago

I'm happy to submit a pull request but not sure where to start on this issue. Can somebody point me in the right direction?

antonmi commented 7 years ago

Hi @Zeeker ! I suppose you mean code like:

assert {:ok, 1} = {:ok, 2}

It is quite tricky in espec, because under the hood 'assert' is just 'BeTruthy' assertion. So the expression above is the same as:

expect({:ok, 1} = {:ok, 2}).to be_truthy

To handle the case you mentioned we need to implemet 'assert' (and 'refute') in different way.

So, to help you start, take a look:

And, of courcse, take a look at the 'assert' implementation in ExUnit https://github.com/elixir-lang/elixir/blob/v1.5.1/lib/ex_unit/lib/ex_unit/assertions.ex#L161

Thank you!

sascha-wolf commented 7 years ago

I would suggest a different approach. We could add another matcher which performs pattern matching. For that we can use Kernel.match?/2. For a failed match a diff should suffice. I suggest match_pattern.

In addition I would probably extend test error handling to handle MatchErrors more gracefully to be closer to ExUnits handling.

What do you think?

antonmi commented 7 years ago

Sounds good! Let's try it!