From 4e3eb102ed4ef60b4b3b40b2f4e8edd965aa2fdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Garc=C3=ADa=20Cota?= Date: Sun, 24 Apr 2011 00:16:36 +0200 Subject: [PATCH] tables working quite well, I am happy :) --- inspect.lua | 19 +++++++++---------- spec/inspect_spec.lua | 15 +++++++++++---- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/inspect.lua b/inspect.lua index 1b7c152..f00d4af 100644 --- a/inspect.lua +++ b/inspect.lua @@ -85,24 +85,23 @@ function Inspector:tabify(level) end function Inspector:addTable(t, level) + local length, needsComma = #t, false self:puts('{') - local length = #t - local needsComma = false for i=1, length do - if i > 1 then - self:puts(', ') - needsComma = true - end + if needsComma then self:puts(', ') end + needsComma = true self:addValue(t[i], level + 1) end - local dictKeys, k, v = getDictionaryKeys(t) + local dictKeys = getDictionaryKeys(t) - for i=1, #dictKeys do + for _,k in ipairs(dictKeys) do if needsComma then self:puts(',') end needsComma = true - k = dictKeys[i] - self:tabify(level+1):addKey(k, level + 1):puts(' = '):addValue(t[k], level + 1) + self:tabify(level+1) + self:addKey(k, level + 1) + self:puts(' = ') + self:addValue(t[k], level + 1) end if #dictKeys > 0 then self:tabify(level) end diff --git a/spec/inspect_spec.lua b/spec/inspect_spec.lua index 7eec048..e51e40a 100644 --- a/spec/inspect_spec.lua +++ b/spec/inspect_spec.lua @@ -46,7 +46,6 @@ context( 'inspect', function() context('tables', function() - test('Should work with simple array-like tables', function() assert_equal(inspect({1,2,3}), "{1, 2, 3}" ) end) @@ -60,14 +59,22 @@ context( 'inspect', function() end) test('Should work with nested dictionary tables', function() - nestedDict = [[{ + assert_equal(inspect( {d=3, b={c=2}, a=1} ), [[{ a = 1, b = { c = 2 }, d = 3 -}]] - assert_equal(inspect( {a=1, b={c=2}, d=3} ), nestedDict) +}]]) + end) + + test('Should work with hybrid tables', function() + assert_equal(inspect({ 'a', {b = 1}, 2, c = 3, ['ahoy you'] = 4 }), [[{"a", { + b = 1 + }, 2, + ["ahoy you"] = 4, + c = 3 +}]]) end) end)