From 77104ea09e3eab9ccf6b4e673022b644aca69a79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Garc=C3=ADa=20Cota?= Date: Sat, 23 Apr 2011 20:07:57 +0200 Subject: [PATCH] simple arrays --- inspect.lua | 26 +++++++++++++++++++++++++- spec/inspect_spec.lua | 4 ++-- 2 files changed, 27 insertions(+), 3 deletions(-) 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)