lunarmodules / luassert

Assertion library for Lua
MIT License
202 stars 77 forks source link

Highlight table differences when assert.same fails #97

Closed o-lim closed 9 years ago

o-lim commented 9 years ago

This highlights differences in table comparisons when assert.same fails, prefixing * next to each key where a difference was detected causing the assertion to fail. Thus, leaving a trail a bread crumbs to follow when trying to spot differences between two tables.

-- test_spec.lua
describe('Test table compare', function()
  it('fails when two tables are not the same', function()
    local t1 = {1, {"a", "b", {"foo", "bar"} }, "c"}
    local t2 = {1, {"a", "b", {"bar", "bar"} }, "c"}
    assert.same(t1, t2)
  end)
end)
$ busted test_spec.lua
Failure → test_spec.lua @ 3
Test table compare fails when two tables are not the same
spec/test_spec.lua:6: Expected objects to be the same.
Passed in:
(table): {
  [1] = 1
 *[2] = {
    [1] = 'a'
    [2] = 'b'
   *[3] = {
     *[1] = 'bar'
      [2] = 'bar' } }
  [3] = 'c' }
Expected:
(table): {
  [1] = 1
 *[2] = {
    [1] = 'a'
    [2] = 'b'
   *[3] = {
     *[1] = 'foo'
      [2] = 'bar' } }
  [3] = 'c' }

This should resolve issue #82.