Skip to content

Conversation

@jeremyowensboggs
Copy link

Marks the conflicting parts of the pattern in red.
For assert_receive, prints the pattern & the value for each of the last 10 messages, coloring the conflicting parts of the pattern.

I updated the value formatting module as well, but left it disabled for now.

defp compare_list(%{ast: [{:|, _, [head, tail]} | _rest]}, [rh_head | rh_tail], env) do
head_pattern = %{ast: head, type: :cons_l}
tail_pattern = %{ast: tail, type: :cons_r}
{h, env} = compare(head_pattern, rh_head, env)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The result using Myers difference is much better than comparing heads, especially with one wrong item at the beginning of the list. The problem is hard to use it if you have things like | operator inside of the list.

I would suggest to parse the list before and make it flat, saving the indexes were the pipes occur so you can rebuild the pipes into the result. You will have another problem here because your %PatternDiff{} ties the left and the right sides together, so it's hard to know which index you are actually in. I would change the struct to represent only one side and return two structs, one for the left (pattern) and other for the right (value), then it will be really easy to find the indexes and also it will be easy to print the differences.

If you do that, you can basically copy the Myers algorithm from the List module and change only the comparison to check that two elements are equal, the compact_reverse/2 to rebuild the list and the accumulator to have all the :ins and :eq on the right side and :del and :eq (again) on the left side.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will also notice that you won't need anymore the @no_value because you no longer have to the difference between the two sides, making the diffs easier to understand.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@josevalim I don't know if it's the case, but I think it would be amazing if the List.myers_difference could also accept a comparison function since it's now accepting a diff one.

|> Enum.intersperse(", ")

[
["%{" | items] ++ ["}"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you can use the Inspect.Algebra module and just use Inspect.Algebra.format/2 to print after. It will make the formatting way more flexible and print nicely the things that don't fit on one line. If I'm not wrong this was also suggested in the original issue.

@@ -0,0 +1,265 @@
defmodule ExUnit.Pattern.FormatValue do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This module is very similar to ExUnit.Pattern.PatternValue and can also be completely merged together if you represent the two diffs separately and with the same internal structure.

@no_value :ex_unit_no_meaningful_value

def create_context(%ContainerDiff{type: :list, items: items}, old_ctx) do
if Enum.all?(items, &keyword_tuple?/1),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can move this logic to the format because this only changes the presentation.

defmodule ExUnit.Pattern.DiffContext do
alias ExUnit.{ContainerDiff, PatternDiff}

defstruct [:keys, :print_when?, :vars, :pins]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The attribute print_when? can also be dropped if you have two separated diffs because you will only have when when they actually exists and should be printed

@josevalim
Copy link
Member

Hi @jeremyowensboggs! How are you?

Just a follow up here: what do you think are the next steps? Do you want us to work on @ggcampinho's feedback and tidy up the work? Or will you plan to incorporate the proposed changes?

Thanks!

@jeremyowensboggs
Copy link
Author

jeremyowensboggs commented Jan 25, 2019 via email

@josevalim
Copy link
Member

Thanks for the prompt reply @jeremyowensboggs!

@ggcampinho would you like to do a pass on it incorporating your suggestions before we go ahead?

@ggcampinho
Copy link
Contributor

ggcampinho commented Jan 26, 2019 via email

@ggcampinho
Copy link
Contributor

Sorry, I had some personal issues and I ended up delaying this. I already checked how to integrate my code to this PR and I'll be working on this next week

@josevalim josevalim closed this in 98c6bba Jul 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants