2013-12-05 08:20:10 +00:00
|
|
|
pcall(require, "strict")
|
|
|
|
local csv = require"csv"
|
|
|
|
|
|
|
|
local errors = 0
|
|
|
|
|
2014-05-18 17:52:16 +00:00
|
|
|
local function testhandle(handle, correct_result)
|
2013-12-05 08:20:10 +00:00
|
|
|
local result = {}
|
2014-05-18 17:52:16 +00:00
|
|
|
for r in handle:lines() do
|
2013-12-05 08:37:25 +00:00
|
|
|
if not r[1] then
|
|
|
|
local r2 = {}
|
|
|
|
for k, v in pairs(r) do r2[#r2+1] = k..":"..tostring(v) end
|
|
|
|
table.sort(r2)
|
|
|
|
r = r2
|
|
|
|
end
|
2013-12-05 08:20:10 +00:00
|
|
|
result[#result+1] = table.concat(r, ",")
|
|
|
|
end
|
2014-05-18 17:52:16 +00:00
|
|
|
|
|
|
|
handle:close()
|
|
|
|
|
2014-05-26 04:44:57 +00:00
|
|
|
result = table.concat(result, "!\n").."!"
|
2013-12-05 08:20:10 +00:00
|
|
|
if result ~= correct_result then
|
|
|
|
io.stderr:write(
|
2013-12-05 08:37:25 +00:00
|
|
|
("Error reading '%s':\nExpected output:\n%s\n\nActual output:\n%s\n\n"):
|
2014-05-18 17:52:16 +00:00
|
|
|
format(handle:name(), correct_result, result))
|
2013-12-05 08:20:10 +00:00
|
|
|
errors = errors + 1
|
2014-05-26 04:44:57 +00:00
|
|
|
return false
|
2013-12-05 08:20:10 +00:00
|
|
|
end
|
2014-05-18 17:52:16 +00:00
|
|
|
return true
|
2013-12-05 08:20:10 +00:00
|
|
|
end
|
|
|
|
|
2014-05-18 17:52:16 +00:00
|
|
|
local function test(filename, correct_result, parameters)
|
|
|
|
local f = csv.open(filename, parameters)
|
|
|
|
local fileok = testhandle(f, correct_result)
|
|
|
|
|
|
|
|
if fileok then
|
|
|
|
f = io.open(filename, "r")
|
|
|
|
local data = f:read("*a")
|
|
|
|
f:close()
|
|
|
|
|
|
|
|
f = csv.openstring(data, parameters)
|
|
|
|
testhandle(f, correct_result)
|
|
|
|
end
|
|
|
|
end
|
2013-12-05 08:37:25 +00:00
|
|
|
|
2013-12-05 08:20:10 +00:00
|
|
|
test("../test-data/embedded-newlines.csv", [[
|
|
|
|
embedded
|
|
|
|
newline,embedded
|
|
|
|
newline,embedded
|
2014-05-26 04:44:57 +00:00
|
|
|
newline!
|
2013-12-05 08:20:10 +00:00
|
|
|
embedded
|
|
|
|
newline,embedded
|
|
|
|
newline,embedded
|
2014-05-26 05:06:28 +00:00
|
|
|
newline!]], { buffer_size=4})
|
2013-12-05 08:20:10 +00:00
|
|
|
|
2013-12-05 08:37:25 +00:00
|
|
|
test("../test-data/embedded-quotes.csv", [[
|
2014-05-26 04:44:57 +00:00
|
|
|
embedded "quotes",embedded "quotes",embedded "quotes"!
|
|
|
|
embedded "quotes",embedded "quotes",embedded "quotes"!]])
|
2013-12-05 08:37:25 +00:00
|
|
|
|
|
|
|
test("../test-data/header.csv", [[
|
2014-05-26 04:44:57 +00:00
|
|
|
alpha:ONE,bravo:two,charlie:3!
|
2014-05-26 05:06:28 +00:00
|
|
|
alpha:four,bravo:five,charlie:6!]], {header=true, buffer_size=5})
|
2013-12-05 08:37:25 +00:00
|
|
|
|
|
|
|
test("../test-data/header.csv", [[
|
2014-05-26 04:44:57 +00:00
|
|
|
apple:one,charlie:30!
|
|
|
|
apple:four,charlie:60!]],
|
2014-05-26 05:06:28 +00:00
|
|
|
{ buffer_size = 7,
|
|
|
|
columns = {
|
2013-12-05 08:37:25 +00:00
|
|
|
apple = { name = "ALPHA", transform = string.lower },
|
|
|
|
charlie = { transform = function(x) return tonumber(x) * 10 end }}})
|
|
|
|
|
2013-12-05 23:56:02 +00:00
|
|
|
test("../test-data/blank-line.csv", [[
|
2014-05-26 05:06:28 +00:00
|
|
|
this,file,ends,with,a,blank,line!]], { buffer_size=11 })
|
2013-12-05 23:56:02 +00:00
|
|
|
|
2013-12-05 08:37:25 +00:00
|
|
|
|
2013-12-05 08:20:10 +00:00
|
|
|
if errors == 0 then
|
|
|
|
io.stdout:write("Passed\n")
|
|
|
|
elseif errors == 1 then
|
|
|
|
io.stdout:write("1 error\n")
|
|
|
|
else
|
|
|
|
io.stdout:write(("%d errors\n"):format(errors))
|
|
|
|
end
|
|
|
|
|
2014-05-18 17:52:16 +00:00
|
|
|
os.exit(errors)
|