From 76138d86a78684636972eef36550ed2e7ea723fd Mon Sep 17 00:00:00 2001 From: kikito Date: Sun, 15 Sep 2013 13:02:45 +0200 Subject: [PATCH] added filter option --- inspect.lua | 36 ++++++++++++++++++--------- spec/inspect_spec.lua | 57 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 11 deletions(-) diff --git a/inspect.lua b/inspect.lua index 2abe631..a9c2afb 100644 --- a/inspect.lua +++ b/inspect.lua @@ -136,10 +136,20 @@ local function countTableAppearances(t, tableAppearances) return tableAppearances end +local function parse_filter(filter) + if type(filter) == 'function' then return filter end + -- not a function, so it must be a table or table-like + filter = type(filter) == 'table' and filter or {filter} + local dictionary = {} + for _,v in pairs(filter) do dictionary[v] = true end + return function(x) return dictionary[x] end +end + ------------------------------------------------------------------- function inspect.inspect(rootObject, options) - options = options or {} - depth = options.depth or math.huge + options = options or {} + local depth = options.depth or math.huge + local filter = parse_filter(options.filter or {}) local tableAppearances = countTableAppearances(rootObject) @@ -252,16 +262,20 @@ function inspect.inspect(rootObject, options) -- putvalue is forward-declared before putTable & putKey putValue = function(v) - local tv = type(v) - - if tv == 'string' then - puts(smartQuote(unescape(v))) - elseif tv == 'number' or tv == 'boolean' or tv == 'nil' then - puts(tostring(v)) - elseif tv == 'table' then - putTable(v) + if filter(v) then + puts('') else - puts('<',tv,' ',getId(v),'>') + local tv = type(v) + + if tv == 'string' then + puts(smartQuote(unescape(v))) + elseif tv == 'number' or tv == 'boolean' or tv == 'nil' then + puts(tostring(v)) + elseif tv == 'table' then + putTable(v) + else + puts('<',tv,' ',getId(v),'>') + end end end diff --git a/spec/inspect_spec.lua b/spec/inspect_spec.lua index 980a5ca..8c55052 100644 --- a/spec/inspect_spec.lua +++ b/spec/inspect_spec.lua @@ -166,6 +166,63 @@ describe( 'inspect', function() end) end) + describe('The filter option', function() + + it('filters hash values', function() + local a = {'this is a'} + local b = {x = 1, a = a} + + assert.equals(inspect(b, {filter = {a}}), [[{ + a = , + x = 1 +}]]) + end) + + it('filtereds hash keys', function() + local a = {'this is a'} + local b = {x = 1, [a] = 'a is used as a key here'} + + assert.equals(inspect(b, {filter = {a}}), [[{ + x = 1, + [] = "a is used as a key here" +}]]) + end) + + it('filtereds array values', function() + assert.equals(inspect({10,20,30}, {filter = {20}}), "{ 10, , 30 }") + end) + + it('filtereds metatables', function() + local a = {'this is a'} + local b = setmetatable({x = 1}, a) + assert.equals(inspect(b, {filter = {a}}), [[{ + x = 1, + = +}]]) + + end) + + it('does not increase the table ids', function() + local a = {'this is a'} + local b = {} + local c = {a, b, b} + assert.equals(inspect(c, {filter = {a}}), "{ , <1>{}, }") + end) + + it('can be a non table (gets interpreted as a table with one element)', function() + assert.equals(inspect({'foo', 'bar', 'baz'}, {filter = "bar"}), '{ "foo", , "baz" }') + end) + + it('can be a function which returns true for the elements that needs to be filtered', function() + local msg = inspect({1,2,3,4,5}, { filter = function(x) + return type(x) == 'number' and x % 2 == 0 + end }) + + assert.equals(msg, '{ 1, , 3, , 5 }') + end) + + end) + describe('metatables', function() it('includes the metatable as an extra hash attribute', function()