mirror of
https://github.com/kikito/inspect.lua.git
synced 2024-12-15 14:34:21 +00:00
added filter option
This commit is contained in:
parent
8a65da658a
commit
76138d86a7
16
inspect.lua
16
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
|
||||
local depth = options.depth or math.huge
|
||||
local filter = parse_filter(options.filter or {})
|
||||
|
||||
local tableAppearances = countTableAppearances(rootObject)
|
||||
|
||||
@ -252,6 +262,9 @@ function inspect.inspect(rootObject, options)
|
||||
|
||||
-- putvalue is forward-declared before putTable & putKey
|
||||
putValue = function(v)
|
||||
if filter(v) then
|
||||
puts('<filtered>')
|
||||
else
|
||||
local tv = type(v)
|
||||
|
||||
if tv == 'string' then
|
||||
@ -264,6 +277,7 @@ function inspect.inspect(rootObject, options)
|
||||
puts('<',tv,' ',getId(v),'>')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
putValue(rootObject)
|
||||
|
||||
|
@ -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 = <filtered>,
|
||||
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,
|
||||
[<filtered>] = "a is used as a key here"
|
||||
}]])
|
||||
end)
|
||||
|
||||
it('filtereds array values', function()
|
||||
assert.equals(inspect({10,20,30}, {filter = {20}}), "{ 10, <filtered>, 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,
|
||||
<metatable> = <filtered>
|
||||
}]])
|
||||
|
||||
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}}), "{ <filtered>, <1>{}, <table 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", <filtered>, "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, <filtered>, 3, <filtered>, 5 }')
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
describe('metatables', function()
|
||||
|
||||
it('includes the metatable as an extra hash attribute', function()
|
||||
|
Loading…
Reference in New Issue
Block a user