mirror of
https://github.com/kikito/inspect.lua.git
synced 2024-12-15 14:34:21 +00:00
(break) remove process option
This commit is contained in:
parent
9c8a68da30
commit
78c67816ef
48
inspect.lua
48
inspect.lua
@ -14,9 +14,6 @@ local inspect = {Options = {}, }
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
inspect._VERSION = 'inspect.lua 3.1.0'
|
inspect._VERSION = 'inspect.lua 3.1.0'
|
||||||
inspect._URL = 'http://github.com/kikito/inspect.lua'
|
inspect._URL = 'http://github.com/kikito/inspect.lua'
|
||||||
inspect._DESCRIPTION = 'human-readable representations of tables'
|
inspect._DESCRIPTION = 'human-readable representations of tables'
|
||||||
@ -153,46 +150,6 @@ local function countCycles(x, cycles)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function makePath(path, a, b)
|
|
||||||
local newPath = {}
|
|
||||||
local len = #path
|
|
||||||
for i = 1, len do newPath[i] = path[i] end
|
|
||||||
|
|
||||||
newPath[len + 1] = a
|
|
||||||
newPath[len + 2] = b
|
|
||||||
|
|
||||||
return newPath
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local function processRecursive(process,
|
|
||||||
item,
|
|
||||||
path,
|
|
||||||
visited)
|
|
||||||
if item == nil then return nil end
|
|
||||||
if visited[item] then return visited[item] end
|
|
||||||
|
|
||||||
local processed = process(item, path)
|
|
||||||
if type(processed) == "table" then
|
|
||||||
local processedCopy = {}
|
|
||||||
visited[item] = processedCopy
|
|
||||||
local processedKey
|
|
||||||
|
|
||||||
for k, v in rawpairs(processed) do
|
|
||||||
processedKey = processRecursive(process, k, makePath(path, k, inspect.KEY), visited)
|
|
||||||
if processedKey ~= nil then
|
|
||||||
processedCopy[processedKey] = processRecursive(process, v, makePath(path, processedKey), visited)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local mt = processRecursive(process, getmetatable(processed), makePath(path, inspect.METATABLE), visited)
|
|
||||||
if type(mt) ~= 'table' then mt = nil end
|
|
||||||
setmetatable(processedCopy, mt)
|
|
||||||
processed = processedCopy
|
|
||||||
end
|
|
||||||
return processed
|
|
||||||
end
|
|
||||||
|
|
||||||
local function puts(buf, str)
|
local function puts(buf, str)
|
||||||
buf.n = buf.n + 1
|
buf.n = buf.n + 1
|
||||||
buf[buf.n] = str
|
buf[buf.n] = str
|
||||||
@ -304,11 +261,6 @@ function inspect.inspect(root, options)
|
|||||||
local depth = options.depth or (math.huge)
|
local depth = options.depth or (math.huge)
|
||||||
local newline = options.newline or '\n'
|
local newline = options.newline or '\n'
|
||||||
local indent = options.indent or ' '
|
local indent = options.indent or ' '
|
||||||
local process = options.process
|
|
||||||
|
|
||||||
if process then
|
|
||||||
root = processRecursive(process, root, {}, {})
|
|
||||||
end
|
|
||||||
|
|
||||||
local cycles = {}
|
local cycles = {}
|
||||||
countCycles(root, cycles)
|
countCycles(root, cycles)
|
||||||
|
48
inspect.tl
48
inspect.tl
@ -7,13 +7,10 @@ local record inspect
|
|||||||
KEY: table
|
KEY: table
|
||||||
METATABLE: table
|
METATABLE: table
|
||||||
|
|
||||||
type ProcessFunction = function(any, {any}): any
|
|
||||||
|
|
||||||
record Options
|
record Options
|
||||||
depth: integer
|
depth: integer
|
||||||
newline: string
|
newline: string
|
||||||
indent: string
|
indent: string
|
||||||
process: ProcessFunction
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -153,46 +150,6 @@ local function countCycles(x: any, cycles: {any:integer}): nil
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function makePath(path: {any}, a: any, b: any): {any}
|
|
||||||
local newPath: {any} = {}
|
|
||||||
local len: integer = #path
|
|
||||||
for i=1, len do newPath[i] = path[i] end
|
|
||||||
|
|
||||||
newPath[len + 1] = a
|
|
||||||
newPath[len + 2] = b
|
|
||||||
|
|
||||||
return newPath
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local function processRecursive(process: inspect.ProcessFunction,
|
|
||||||
item: any,
|
|
||||||
path: {any},
|
|
||||||
visited: {any:any}): any
|
|
||||||
if item == nil then return nil end
|
|
||||||
if visited[item] then return visited[item] end
|
|
||||||
|
|
||||||
local processed: any = process(item, path)
|
|
||||||
if processed is table then
|
|
||||||
local processedCopy = {}
|
|
||||||
visited[item] = processedCopy
|
|
||||||
local processedKey: any
|
|
||||||
|
|
||||||
for k,v in rawpairs(processed) do
|
|
||||||
processedKey = processRecursive(process, k, makePath(path, k, inspect.KEY), visited)
|
|
||||||
if processedKey ~= nil then
|
|
||||||
processedCopy[processedKey] = processRecursive(process, v, makePath(path, processedKey), visited)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local mt = processRecursive(process, getmetatable(processed), makePath(path, inspect.METATABLE), visited)
|
|
||||||
if type(mt) ~= 'table' then mt = nil end -- ignore not nil/table __metatable field
|
|
||||||
setmetatable(processedCopy, mt as metatable<{any:any}>)
|
|
||||||
processed = processedCopy
|
|
||||||
end
|
|
||||||
return processed
|
|
||||||
end
|
|
||||||
|
|
||||||
local function puts(buf: table, str:string): nil
|
local function puts(buf: table, str:string): nil
|
||||||
buf.n = buf.n as integer + 1
|
buf.n = buf.n as integer + 1
|
||||||
buf[buf.n as integer] = str
|
buf[buf.n as integer] = str
|
||||||
@ -304,11 +261,6 @@ function inspect.inspect(root: any, options: inspect.Options): string
|
|||||||
local depth: integer = options.depth or (math.huge as integer)
|
local depth: integer = options.depth or (math.huge as integer)
|
||||||
local newline: string = options.newline or '\n'
|
local newline: string = options.newline or '\n'
|
||||||
local indent: string = options.indent or ' '
|
local indent: string = options.indent or ' '
|
||||||
local process: inspect.ProcessFunction = options.process
|
|
||||||
|
|
||||||
if process then
|
|
||||||
root = processRecursive(process, root, {}, {})
|
|
||||||
end
|
|
||||||
|
|
||||||
local cycles = {}
|
local cycles = {}
|
||||||
countCycles(root, cycles)
|
countCycles(root, cycles)
|
||||||
|
@ -262,96 +262,6 @@ describe( 'inspect', function()
|
|||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('the process option', function()
|
|
||||||
|
|
||||||
it('removes one element', function()
|
|
||||||
local names = {'Andrew', 'Peter', 'Ann' }
|
|
||||||
local removeAnn = function(item) if item ~= 'Ann' then return item end end
|
|
||||||
assert.equals('{ "Andrew", "Peter" }', inspect(names, {process = removeAnn}))
|
|
||||||
end)
|
|
||||||
|
|
||||||
it('uses the path', function()
|
|
||||||
local names = {'Andrew', 'Peter', 'Ann' }
|
|
||||||
local removeThird = function(item, path) if path[1] ~= 3 then return item end end
|
|
||||||
assert.equals('{ "Andrew", "Peter" }', inspect(names, {process = removeThird}))
|
|
||||||
end)
|
|
||||||
|
|
||||||
it('replaces items', function()
|
|
||||||
local names = {'Andrew', 'Peter', 'Ann' }
|
|
||||||
local filterAnn = function(item) return item == 'Ann' and '<filtered>' or item end
|
|
||||||
assert.equals('{ "Andrew", "Peter", "<filtered>" }', inspect(names, {process = filterAnn}))
|
|
||||||
end)
|
|
||||||
|
|
||||||
it('nullifies metatables', function()
|
|
||||||
local mt = {'world'}
|
|
||||||
local t = setmetatable({'hello'}, mt)
|
|
||||||
local removeMt = function(item) if item ~= mt then return item end end
|
|
||||||
assert.equals('{ "hello" }', inspect(t, {process = removeMt}))
|
|
||||||
end)
|
|
||||||
|
|
||||||
it('nullifies metatables using their paths', function()
|
|
||||||
local mt = {'world'}
|
|
||||||
local t = setmetatable({'hello'}, mt)
|
|
||||||
local removeMt = function(item, path) if path[#path] ~= inspect.METATABLE then return item end end
|
|
||||||
assert.equals('{ "hello" }', inspect(t, {process = removeMt}))
|
|
||||||
end)
|
|
||||||
|
|
||||||
it('nullifies the root object', function()
|
|
||||||
local names = {'Andrew', 'Peter', 'Ann' }
|
|
||||||
local removeNames = function(item) if item ~= names then return item end end
|
|
||||||
assert.equals('nil', inspect(names, {process = removeNames}))
|
|
||||||
end)
|
|
||||||
|
|
||||||
it('changes keys', function()
|
|
||||||
local dict = {a = 1}
|
|
||||||
local changeKey = function(item) return item == 'a' and 'x' or item end
|
|
||||||
assert.equals('{\n x = 1\n}', inspect(dict, {process = changeKey}))
|
|
||||||
end)
|
|
||||||
|
|
||||||
it('nullifies keys', function()
|
|
||||||
local dict = {a = 1, b = 2}
|
|
||||||
local removeA = function(item) return item ~= 'a' and item or nil end
|
|
||||||
assert.equals('{\n b = 2\n}', inspect(dict, {process = removeA}))
|
|
||||||
end)
|
|
||||||
|
|
||||||
it('prints inspect.KEY & inspect.METATABLE', function()
|
|
||||||
local t = {inspect.KEY, inspect.METATABLE}
|
|
||||||
assert.equals("{ inspect.KEY, inspect.METATABLE }", inspect(t))
|
|
||||||
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 = {}
|
|
||||||
local addItem = function(item, path)
|
|
||||||
items[#items + 1] = {item = item, path = path}
|
|
||||||
return item
|
|
||||||
end
|
|
||||||
|
|
||||||
inspect(t, {process = addItem})
|
|
||||||
|
|
||||||
assert.same({
|
|
||||||
{item = t, path = {}},
|
|
||||||
{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', inspect.KEY}},
|
|
||||||
{item = 2, path = {{a=1}, 'b'}},
|
|
||||||
{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'}}
|
|
||||||
}, items)
|
|
||||||
|
|
||||||
end)
|
|
||||||
|
|
||||||
it('handles recursive tables correctly', function()
|
|
||||||
local tbl = { 1,2,3}
|
|
||||||
tbl.loop = tbl
|
|
||||||
inspect(tbl, { process=function(x) return x end})
|
|
||||||
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