From 9054ee1051f99b52b31a184848992fd368bde887 Mon Sep 17 00:00:00 2001 From: kikito Date: Sat, 13 Sep 2014 12:12:41 +0200 Subject: [PATCH] , -> inspect.KEY, inspect.METATABLE --- README.md | 8 ++++---- inspect.lua | 11 ++++++++--- spec/inspect_spec.lua | 23 ++++++++++++++--------- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index e7e933f..d79101d 100644 --- a/README.md +++ b/README.md @@ -139,10 +139,10 @@ local processed_item = function(item, path) * `path` is an array-like table built with all the keys that have been used to reach `item`, from the root. * For values, it is just a regular list of keys. For example, to reach the 1 in `{a = {b = 1}}`, the `path` will be `{'a', 'b'}` - * For keys, a special value called `` is inserted. For example, to reach the `c` in `{a = {b = {c = 1}}}`, - the path will be `{'a', 'b', 'c', '' }` - * For metatables, a special value called `` is inserted. For `{a = {b = 1}}}`, the path - `{'a', 'b', ''}` means "the metatable of the table `{b = 1}`". + * For keys, the special value `inspect.KEY` is inserted. For example, to reach the `c` in `{a = {b = {c = 1}}}`, + the path will be `{'a', 'b', 'c', inspect.KEY }` + * For metatables, the special value `inspect.METATABLE` is inserted. For `{a = {b = 1}}}`, the path + `{'a', {b = 1}, inspect.METATABLE}` means "the metatable of the table `{b = 1}`". * `processed_item` is the value returned by `options.process`. If it is equal to `item`, then the inspected table will look unchanged. If it is different, then the table will look different; most notably, if it's `nil`, the item will dissapear on the inspected table. diff --git a/inspect.lua b/inspect.lua index 9bdaf99..d548be1 100644 --- a/inspect.lua +++ b/inspect.lua @@ -28,6 +28,9 @@ local inspect ={ ]] } +inspect.KEY = setmetatable({}, {__tostring = function() return 'inspect.KEY' end}) +inspect.METATABLE = setmetatable({}, {__tostring = function() return 'inspect.METATABLE' end}) + -- Apostrophizes the string if it has quotes, but not aphostrophes -- Otherwise, it returns a regular quoted string local function smartQuote(str) @@ -160,13 +163,13 @@ local function processRecursive(process, item, path) local processedKey for k,v in pairs(processed) do - processedKey = processRecursive(process, k, makePath(path, k, '')) + processedKey = processRecursive(process, k, makePath(path, k, inspect.KEY)) if processedKey ~= nil then processedCopy[processedKey] = processRecursive(process, v, makePath(path, processedKey)) end end - local mt = processRecursive(process, getmetatable(processed), makePath(path, '')) + local mt = processRecursive(process, getmetatable(processed), makePath(path, inspect.METATABLE)) setmetatable(processedCopy, mt) processed = processedCopy end @@ -227,7 +230,9 @@ function Inspector:putKey(k) end function Inspector:putTable(t) - if self:alreadyVisited(t) then + if t == inspect.KEY or t == inspect.METATABLE then + self:puts(tostring(t)) + elseif self:alreadyVisited(t) then self:puts('') elseif self.level >= self.depth then self:puts('{...}') diff --git a/spec/inspect_spec.lua b/spec/inspect_spec.lua index 2e3511b..958aa81 100644 --- a/spec/inspect_spec.lua +++ b/spec/inspect_spec.lua @@ -227,7 +227,7 @@ describe( 'inspect', function() it('nullifies metatables using their paths', function() local mt = {'world'} local t = setmetatable({'hello'}, mt) - local removeMt = function(item, path) if path[#path] ~= '' then return item end end + local removeMt = function(item, path) if path[#path] ~= inspect.METATABLE then return item end end assert.equals(inspect(t, {process = removeMt}), '{ "hello" }') end) @@ -249,7 +249,12 @@ describe( 'inspect', function() assert.equals(inspect(dict, {process = removeA}), '{\n b = 2\n}') end) - it('marks key paths with and metatables with ', function() + it('prints inspect.KEY & inspect.METATABLE', function() + local t = {inspect.KEY, inspect.METATABLE} + assert.equals(inspect(t), "{ inspect.KEY, inspect.METATABLE }") + end) + + it('marks key paths with inspect.KEY and metatables with inspect.METATABLE', function() local t = { [{a=1}] = setmetatable({b=2}, {c=3}) } local items = {} @@ -262,15 +267,15 @@ describe( 'inspect', function() assert.same(items, { {item = t, path = {}}, - {item = {a=1}, path = {{a=1}, ''}}, - {item = 'a', path = {{a=1}, '', 'a', ''}}, - {item = 1, path = {{a=1}, '', 'a'}}, + {item = {a=1}, path = {{a=1}, inspect.KEY}}, + {item = 'a', path = {{a=1}, inspect.KEY, 'a', inspect.KEY}}, + {item = 1, path = {{a=1}, inspect.KEY, 'a'}}, {item = setmetatable({b=2}, {c=3}), path = {{a=1}}}, - {item = 'b', path = {{a=1}, 'b', ''}}, + {item = 'b', path = {{a=1}, 'b', inspect.KEY}}, {item = 2, path = {{a=1}, 'b'}}, - {item = {c=3}, path = {{a=1}, ''}}, - {item = 'c', path = {{a=1}, '', 'c', ''}}, - {item = 3, path = {{a=1}, '', 'c'}} + {item = {c=3}, path = {{a=1}, inspect.METATABLE}}, + {item = 'c', path = {{a=1}, inspect.METATABLE, 'c', inspect.KEY}}, + {item = 3, path = {{a=1}, inspect.METATABLE, 'c'}} }) end)