diff --git a/inspect.lua b/inspect.lua index 04723d9..ccd6a1c 100644 --- a/inspect.lua +++ b/inspect.lua @@ -7,8 +7,32 @@ -- public function +local bufferMethods = { + add = function(self, ...) + local args = {...} + for i=1, #args do + table.insert(self.data, tostring(args[i])) + end + return self + end +} + +local function newBuffer() + return setmetatable( { data = {} }, { + __index = bufferMethods, + __tostring = function(self) return table.concat(self.data) end + } ) +end + local function inspect(t) - return "" + local buffer = newBuffer() + buffer:add('{') + for i=1, #t do + if i > 1 then buffer:add(', ') end + buffer:add(tostring(t[i])) + end + buffer:add('}') + return tostring(buffer) end return inspect diff --git a/spec/inspect_spec.lua b/spec/inspect_spec.lua index 7d0d26b..8a3a831 100644 --- a/spec/inspect_spec.lua +++ b/spec/inspect_spec.lua @@ -2,8 +2,8 @@ local inspect = require 'inspect' context( 'inspect', function() - test('Should work with simple arrays', function() - assert_equal(inspect( {1,2,3} + test('Should work with simple array-like tables', function() + assert_equal(inspect({1,2,3}), "{1, 2, 3}" ) end) end)