mirror of
https://github.com/kikito/inspect.lua.git
synced 2024-12-15 14:34:21 +00:00
remove filter option
This commit is contained in:
parent
e67a7471d4
commit
7195bfa423
14
inspect.lua
14
inspect.lua
@ -136,15 +136,6 @@ local function countTableAppearances(t, tableAppearances)
|
|||||||
return tableAppearances
|
return tableAppearances
|
||||||
end
|
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
|
|
||||||
|
|
||||||
local function makePath(path, key)
|
local function makePath(path, key)
|
||||||
local newPath, len = {}, #path
|
local newPath, len = {}, #path
|
||||||
for i=1, len do newPath[i] = path[i] end
|
for i=1, len do newPath[i] = path[i] end
|
||||||
@ -156,7 +147,6 @@ end
|
|||||||
function inspect.inspect(rootObject, options)
|
function inspect.inspect(rootObject, options)
|
||||||
options = options or {}
|
options = options or {}
|
||||||
local depth = options.depth or math.huge
|
local depth = options.depth or math.huge
|
||||||
local filter = parse_filter(options.filter or {})
|
|
||||||
|
|
||||||
local tableAppearances = countTableAppearances(rootObject)
|
local tableAppearances = countTableAppearances(rootObject)
|
||||||
|
|
||||||
@ -269,9 +259,6 @@ function inspect.inspect(rootObject, options)
|
|||||||
|
|
||||||
-- putvalue is forward-declared before putTable & putKey
|
-- putvalue is forward-declared before putTable & putKey
|
||||||
putValue = function(v, path)
|
putValue = function(v, path)
|
||||||
if filter(v, path) then
|
|
||||||
puts('<filtered>')
|
|
||||||
else
|
|
||||||
local tv = type(v)
|
local tv = type(v)
|
||||||
|
|
||||||
if tv == 'string' then
|
if tv == 'string' then
|
||||||
@ -284,7 +271,6 @@ function inspect.inspect(rootObject, options)
|
|||||||
puts('<',tv,' ',getId(v),'>')
|
puts('<',tv,' ',getId(v),'>')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
putValue(rootObject, {})
|
putValue(rootObject, {})
|
||||||
|
|
||||||
|
@ -180,79 +180,6 @@ describe( 'inspect', function()
|
|||||||
end)
|
end)
|
||||||
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('filters by path', function()
|
|
||||||
local people = { tony = { age = 21 }, martha = { age = 34} }
|
|
||||||
local hideMarthaAge = function(_,path)
|
|
||||||
return table.concat(path, '.') == 'martha.age'
|
|
||||||
end
|
|
||||||
|
|
||||||
assert.equals(inspect(people, {filter = hideMarthaAge}), [[{
|
|
||||||
martha = {
|
|
||||||
age = <filtered>
|
|
||||||
},
|
|
||||||
tony = {
|
|
||||||
age = 21
|
|
||||||
}
|
|
||||||
}]])
|
|
||||||
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()
|
describe('metatables', function()
|
||||||
|
|
||||||
it('includes the metatable as an extra hash attribute', function()
|
it('includes the metatable as an extra hash attribute', function()
|
||||||
|
Loading…
Reference in New Issue
Block a user