simple arrays

This commit is contained in:
Enrique García Cota 2011-04-23 20:07:57 +02:00
parent b66508cb56
commit 77104ea09e
2 changed files with 27 additions and 3 deletions

View File

@ -7,8 +7,32 @@
-- public function -- 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) 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 end
return inspect return inspect

View File

@ -2,8 +2,8 @@ local inspect = require 'inspect'
context( 'inspect', function() context( 'inspect', function()
test('Should work with simple arrays', function() test('Should work with simple array-like tables', function()
assert_equal(inspect( {1,2,3} assert_equal(inspect({1,2,3}), "{1, 2, 3}" )
end) end)
end) end)