newline & indent options

This commit is contained in:
kikito 2014-09-13 12:33:20 +02:00
parent 9054ee1051
commit 99e8c03959
3 changed files with 39 additions and 1 deletions

View File

@ -124,6 +124,24 @@ assert(inspect(t5, {depth = 2}) == [[{
`options.depth` defaults to infinite (`math.huge`).
### options.newline & options.indent
These are the strings used by `inspect` to respectively add a newline and indent each level of a table.
By default, `options.newline` is `"\n"` and `options.indent` is `" "` (two spaces).
``` lua
local t = {a={b=1}}
assert(inspect(t) == [[{
a = {
b = 1
}
}]])
assert(inspect(t, {newline='@', indent="++"}), "{@++a = {@++++b = 1@++}@}"
```
### options.process
`options.process` is a function which allow altering the passed object before transforming it into a string.

View File

@ -199,7 +199,7 @@ function Inspector:down(f)
end
function Inspector:tabify()
self:puts("\n", string.rep(" ", self.level))
self:puts(self.newline, string.rep(self.indent, self.level))
end
function Inspector:commaControl(needsComma)
@ -304,6 +304,8 @@ function inspect.inspect(root, options)
options = options or {}
local depth = options.depth or math.huge
local process = options.process
local newline = options.newline or '\n'
local indent = options.indent or ' '
if process then
root = processRecursive(process, root, {})
end
@ -314,6 +316,8 @@ function inspect.inspect(root, options)
level = 0,
ids = setmetatable({}, idsMetaTable),
maxIds = setmetatable({}, maxIdsMetaTable),
newline = newline,
indent = indent,
tableAppearances = countTableAppearances(root)
}, Inspector_mt)

View File

@ -197,6 +197,22 @@ describe( 'inspect', function()
end)
end)
describe('the newline option', function()
it('changes the substring used for newlines', function()
local t = {a={b=1}}
assert.equal(inspect(t, {newline='@'}), "{@ a = {@ b = 1@ }@}")
end)
end)
describe('the indent option', function()
it('changes the substring used for indenting', function()
local t = {a={b=1}}
assert.equal(inspect(t, {indent='>>>'}), "{\n>>>a = {\n>>>>>>b = 1\n>>>}\n}")
end)
end)
describe('the process option', function()
it('removes one element', function()