1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
local pretty = require 'pl.pretty'
local utils = require 'pl.utils'
local test = require 'pl.test'
local asserteq, assertmatch = test.asserteq, test.assertmatch
t1 = {
'one','two','three',{1,2,3},
alpha=1,beta=2,gamma=3,['&']=true,[0]=false,
_fred = {true,true},
s = [[
hello dolly
you're so fine
]]
}
s = pretty.write(t1) --,' ',true)
t2,err = pretty.read(s)
if err then return print(err) end
asserteq(t1,t2)
res,err = pretty.read [[
{
['function'] = true,
['do'] = true,
}
]]
assert(res)
res,err = pretty.read [[
{
['function'] = true,
['do'] = "no function here...",
}
]]
assert(res)
res,err = pretty.read [[
{
['function'] = true,
['do'] = function() return end
}
]]
assertmatch(err,'cannot have functions in table definition')
res,err = pretty.load([[
-- comments are ok
a = 2
bonzo = 'dog'
t = {1,2,3}
]])
asserteq(res,{a=2,bonzo='dog',t={1,2,3}})
--- another potential problem is string functions called implicitly as methods--
res,err = pretty.read [[
{s = ('woo'):gsub('w','wwwwww'):gsub('w','wwwwww')}
]]
assertmatch(err,(_VERSION ~= "Lua 5.2") and 'attempt to index a string value' or "attempt to index constant 'woo'")
---- pretty.load has a _paranoid_ option
res,err = pretty.load([[
k = 0
for i = 1,1e12 do k = k + 1 end
]],{},true)
assertmatch(err,'looping not allowed')
-- Check to make sure that no spaces exist when write is told not to
local tbl = { "a", 2, "c", false, 23, 453, "poot", 34 }
asserteq( pretty.write( tbl, "" ), [[{"a",2,"c",false,23,453,"poot",34}]] )
-- Check that write correctly prevents cycles
local t1,t2 = {},{}
t1[1] = t1
asserteq( pretty.write(t1,""), [[{<cycle>}]] )
t1[1],t1[2],t2[1] = 42,t2,t1
asserteq( pretty.write(t1,""), [[{42,{<cycle>}}]] )
-- Check false positives in write's cycles prevention
t2 = {}
t1[1],t1[2] = t2,t2
asserteq( pretty.write(t1,""), [[{{},{}}]] )
-- Check that write correctly print table with non number or string as keys
t1 = { [true] = "boolean", [false] = "untrue", a = "a", b = "b", [1] = 1, [0] = 0 }
asserteq( pretty.write(t1,""), [[{1,["false"]="untrue",["true"]="boolean",a="a",b="b",[0]=0}]] )
-- Check number formatting
asserteq(pretty.write({1/0, -1/0, 0/0, 1, 1/2}, ""), "{Inf,-Inf,NaN,1,0.5}")
if _VERSION == "Lua 5.3" or _VERSION == "Lua 5.4" then
asserteq(pretty.write({1.0}, ""), "{1.0}")
else
asserteq(pretty.write({1.0}, ""), "{1}")
end
do -- issue #203, item 3
local t = {}; t[t] = 1
pretty.write(t) -- should not crash
end
do
local float = 1e100
local max_int = 9007199254740991 -- 1 << 53 - 1
local min_int = -9007199254740991
asserteq(pretty.write(float), "1.0e+100")
if _VERSION == "Lua 5.3" or _VERSION == "Lua 5.4" then
--There is no way to portably format with %d before 5.3
asserteq(pretty.write(min_int - 3), "-9007199254740994")
asserteq(pretty.write(max_int + 3), "9007199254740994")
asserteq(pretty.write(min_int), "-9007199254740991")
asserteq(pretty.write(max_int), "9007199254740991")
end
end
-- pretty.write fails if an __index metatable raises an error #257
-- only applies to 5.3+ where iterators respect metamethods
do
local t = setmetatable({},{
__index = function(self, key)
error("oops... couldn't find " .. tostring(key))
end
})
asserteq(pretty.write(t), "{\n}")
end
|