mirror of
https://github.com/kikito/inspect.lua.git
synced 2024-12-15 14:34:21 +00:00
organized tests into groups
This commit is contained in:
parent
87b3e1d82c
commit
ecf208a938
20
inspect.lua
20
inspect.lua
@ -16,6 +16,19 @@ local function smartQuote(str)
|
|||||||
return string.format("%q", str )
|
return string.format("%q", str )
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local unescapedChars = {
|
||||||
|
["\a"] = "\\a", ["\b"] = "\\b", ["\f"] = "\\f", ["\n"] = "\\n",
|
||||||
|
["\r"] = "\\r", ["\t"] = "\\t", ["\v"] = "\\v", ["\\"] = "\\\\"
|
||||||
|
}
|
||||||
|
|
||||||
|
local function unescapeChar(c)
|
||||||
|
return unescapedChars[c]
|
||||||
|
end
|
||||||
|
|
||||||
|
local function unescape(str)
|
||||||
|
return string.gsub( str, "(%c)", unescapeChar )
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
local Buffer = {}
|
local Buffer = {}
|
||||||
|
|
||||||
@ -38,7 +51,9 @@ function Buffer:addValue(v)
|
|||||||
local tv = type(v)
|
local tv = type(v)
|
||||||
|
|
||||||
if tv == 'string' then
|
if tv == 'string' then
|
||||||
self:add(smartQuote(string.gsub( v, "\n", "\\n" )))
|
self:add(smartQuote(unescape(v)))
|
||||||
|
elseif tv == 'number' or tv == 'boolean' then
|
||||||
|
self:add(tostring(v))
|
||||||
elseif tv == 'table' then
|
elseif tv == 'table' then
|
||||||
self:add('{')
|
self:add('{')
|
||||||
for i=1, #v do
|
for i=1, #v do
|
||||||
@ -47,8 +62,9 @@ function Buffer:addValue(v)
|
|||||||
end
|
end
|
||||||
self:add('}')
|
self:add('}')
|
||||||
else
|
else
|
||||||
self:add(tostring(v))
|
self:add('<',tv,'>')
|
||||||
end
|
end
|
||||||
|
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -2,19 +2,50 @@ local inspect = require 'inspect'
|
|||||||
|
|
||||||
context( 'inspect', function()
|
context( 'inspect', function()
|
||||||
|
|
||||||
test('Should work with numbers', function()
|
context('numbers', function()
|
||||||
|
test('Should work with integers', function()
|
||||||
assert_equal(inspect(1), "1")
|
assert_equal(inspect(1), "1")
|
||||||
assert_equal(inspect(1.5), "1.5")
|
|
||||||
assert_equal(inspect(-3.14), "-3.14")
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
test('Should work with strings', function()
|
test('Should work with decimals', function()
|
||||||
assert_equal(inspect("hello"), '"hello"')
|
assert_equal(inspect(1.5), "1.5")
|
||||||
assert_equal(inspect('I have "quotes"'), "'I have \"quotes\"'")
|
|
||||||
assert_equal(inspect("I have \"quotes\" and 'apostrophes'"), '"I have \\"quotes\\" and \'apostrophes\'"')
|
|
||||||
assert_equal(inspect('I have \n new \n lines'), '"I have \\\\n new \\\\n lines"')
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
test('Should work with negative numbers', function()
|
||||||
|
assert_equal(inspect(-3.14), "-3.14")
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
context('strings', function()
|
||||||
|
test('Should put quotes around regular strings', function()
|
||||||
|
assert_equal(inspect("hello"), '"hello"')
|
||||||
|
end)
|
||||||
|
|
||||||
|
test('Should put apostrophes around strings with quotes', function()
|
||||||
|
assert_equal(inspect('I have "quotes"'), "'I have \"quotes\"'")
|
||||||
|
end)
|
||||||
|
|
||||||
|
test('Should use regular quotes if the string has both quotes and apostrophes', function()
|
||||||
|
assert_equal(inspect("I have \"quotes\" and 'apostrophes'"), '"I have \\"quotes\\" and \'apostrophes\'"')
|
||||||
|
end)
|
||||||
|
|
||||||
|
test('Should escape escape control characters', function()
|
||||||
|
assert_equal(inspect('I have \n new \n lines'), '"I have \\\\n new \\\\n lines"')
|
||||||
|
assert_equal(inspect('I have \b a back space'), '"I have \\\\b a back space"')
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
test('Should work with functions', function()
|
||||||
|
assert_equal(inspect(print), '<function>')
|
||||||
|
end)
|
||||||
|
|
||||||
|
test('Should work with booleans', function()
|
||||||
|
assert_equal(inspect(true), 'true')
|
||||||
|
assert_equal(inspect(false), 'false')
|
||||||
|
end)
|
||||||
|
|
||||||
|
context('tables', function()
|
||||||
|
|
||||||
test('Should work with simple array-like tables', function()
|
test('Should work with simple array-like tables', function()
|
||||||
assert_equal(inspect({1,2,3}), "{1, 2, 3}" )
|
assert_equal(inspect({1,2,3}), "{1, 2, 3}" )
|
||||||
end)
|
end)
|
||||||
@ -23,4 +54,6 @@ context( 'inspect', function()
|
|||||||
assert_equal(inspect({1,2,3, {4,5}, 6}), "{1, 2, 3, {4, 5}, 6}" )
|
assert_equal(inspect({1,2,3, {4,5}, 6}), "{1, 2, 3, {4, 5}, 6}" )
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
end)
|
||||||
|
|
||||||
end)
|
end)
|
||||||
|
Loading…
Reference in New Issue
Block a user