made depth an options parameter

This commit is contained in:
kikito 2013-09-15 11:23:01 +02:00
parent e898803196
commit 8a65da658a
2 changed files with 19 additions and 19 deletions

View File

@ -137,8 +137,9 @@ local function countTableAppearances(t, tableAppearances)
end end
------------------------------------------------------------------- -------------------------------------------------------------------
function inspect.dump(rootObject, depth) function inspect.inspect(rootObject, options)
depth = depth or math.huge options = options or {}
depth = options.depth or math.huge
local tableAppearances = countTableAppearances(rootObject) local tableAppearances = countTableAppearances(rootObject)
@ -269,7 +270,7 @@ function inspect.dump(rootObject, depth)
return table.concat(buffer) return table.concat(buffer)
end end
setmetatable(inspect, { __call = function(_, ...) return inspect.dump(...) end }) setmetatable(inspect, { __call = function(_, ...) return inspect.inspect(...) end })
return inspect return inspect

View File

@ -105,7 +105,16 @@ describe( 'inspect', function()
}]]) }]])
end) end)
describe('depth', function() it('displays <table x> instead of repeating an already existing table', function()
local a = { 1, 2, 3 }
local b = { 'a', 'b', 'c', a }
a[4] = b
a[5] = a
a[6] = b
assert.equals(inspect(a), '<1>{ 1, 2, 3, <2>{ "a", "b", "c", <table 1> }, <table 1>, <table 2> }')
end)
describe('The depth parameter', function()
local level5 = { 1,2,3, a = { b = { c = { d = { e = 5 } } } } } local level5 = { 1,2,3, a = { b = { c = { d = { e = 5 } } } } }
local keys = { [level5] = true } local keys = { [level5] = true }
@ -123,16 +132,16 @@ describe( 'inspect', function()
}]]) }]])
end) end)
it('is modifiable by the user', function() it('is modifiable by the user', function()
assert.equals(inspect(level5, 2), [[{ 1, 2, 3, assert.equals(inspect(level5, {depth = 2}), [[{ 1, 2, 3,
a = { a = {
b = {...} b = {...}
} }
}]]) }]])
assert.equals(inspect(level5, 1), [[{ 1, 2, 3, assert.equals(inspect(level5, {depth = 1}), [[{ 1, 2, 3,
a = {...} a = {...}
}]]) }]])
assert.equals(inspect(level5, 0), "{...}") assert.equals(inspect(level5, {depth = 0}), "{...}")
assert.equals(inspect(level5, 4), [[{ 1, 2, 3, assert.equals(inspect(level5, {depth = 4}), [[{ 1, 2, 3,
a = { a = {
b = { b = {
c = { c = {
@ -145,7 +154,7 @@ describe( 'inspect', function()
end) end)
it('respects depth on keys', function() it('respects depth on keys', function()
assert.equals(inspect(keys, 4), [[{ assert.equals(inspect(keys, {depth = 4}), [[{
[{ 1, 2, 3, [{ 1, 2, 3,
a = { a = {
b = { b = {
@ -155,16 +164,6 @@ describe( 'inspect', function()
}] = true }] = true
}]]) }]])
end) end)
it('displays <table x> instead of repeating an already existing table', function()
local a = { 1, 2, 3 }
local b = { 'a', 'b', 'c', a }
a[4] = b
a[5] = a
a[6] = b
assert.equals(inspect(a), '<1>{ 1, 2, 3, <2>{ "a", "b", "c", <table 1> }, <table 1>, <table 2> }')
end)
end) end)
describe('metatables', function() describe('metatables', function()