elliotchance / concise

✅ Concise is test framework for using plain English and minimal code, built on PHPUnit.
MIT License
45 stars 3 forks source link

array equals diff #350

Open ostrolucky opened 7 years ago

ostrolucky commented 7 years ago
$this->assert([
            ['attach', ['event3', [1, 'method5']]],
            ['attach', ['event1', [1, 'method1']]],
            ['attach', ['event2', [1, 'method2']]],
            ['attach', ['event3', [1, 'method4']]],
            ['attach', ['event3', [1, 'method3']]],
        ])->equals([
            ['attach', ['event3', [1, 'method5']]],
            ['attach', ['event1', [1, 'method1']]],
            ['attach', ['event3', [1, 'method2']]],
            ['attach', ['event3', [1, 'method4']]],
            ['attach', ['event3', [1, 'method3']]],
        ]);

produces

   [
     [
       "attach",
       [
         "event3",
         [
           1,
           "method5"
         ]
       ]
     ],
     [
       "attach",
       [
         "event1",
         [
           1,
           "method1"
         ]
       ]
     ],
     [
       "attach",
       [
         "event2",
         [
           1,
           "method2"
         ]
       ]
     ],
     [
       "attach",
       [
         "event3",
         [
           1,
           "method4"
         ]
       ]
     ],
     [
       "attach",
       [
         "event3",
         [
           1,
           "method3"
         ]
       ]
     ]
   ] equals [
     [
       "attach",
       [
         "event3",
         [
           1,
           "method5"
         ]
       ]
     ],
     [
       "attach",
       [
         "event1",
         [
           1,
           "method1"
         ]
       ]
     ],
     [
       "attach",
       [
         "event3",
         [
           1,
           "method2"
         ]
       ]
     ],
     [
       "attach",
       [
         "event3",
         [
           1,
           "method4"
         ]
       ]
     ],
     [
       "attach",
       [
         "event3",
         [
           1,
           "method3"
         ]
       ]
     ]
   ]

vs phpunit's

$this->assertEquals([
            ['attach', ['event3', [1, 'method5']]],
            ['attach', ['event1', [1, 'method1']]],
            ['attach', ['event2', [1, 'method2']]],
            ['attach', ['event3', [1, 'method4']]],
            ['attach', ['event3', [1, 'method3']]],
        ], [
            ['attach', ['event3', [1, 'method5']]],
            ['attach', ['event1', [1, 'method1']]],
            ['attach', ['event3', [1, 'method2']]],
            ['attach', ['event3', [1, 'method4']]],
            ['attach', ['event3', [1, 'method3']]],
        ]);

which produces

  --- Expected
   +++ Actual
   @@ @@
            1 => Array (
   -            0 => 'event2'
   +            0 => 'event3'
                1 => Array (...)
            )
        )
        3 => Array (...)
        4 => Array (...)
    )
elliotchance commented 7 years ago

Yes, you're right. When you are comparing complex structures the differences become harder to read.

It could very well be redesigned, here are some ideas:

  1. Use the background colour of the terminal to highlight lines that are different. This makes the differences stand out immediately but it useless when the colours are disabled (like on a CI).

  2. We could produce a patch like PHPUnit. This focuses on the differences but doesn't always give a context that can make the output confusing.

  3. We could put them side by side with a diff algorithm, like:

actual     expected
[          [
  5,         5,
  2          3,      // This line would be purple
             2       // This line would be green
]          ]

I'm keen to hear your ideas?

ostrolucky commented 7 years ago

Best would be a combination of all 3:

If I had to do it simple though, I would just show +/- to current output

But I think important is also limiting amount of data shown. Phpunit is in this too restrictive and shows too little, but yours shows too much and it would be a problem when comparison data is large

elliotchance commented 7 years ago

Thats true, perhaps a CLI argument --context-lines which defaults to a sensible number like 5. This would allow people to configure how many lines above and below the changes they would see?