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)
|
2014-05-26 05:20:17 +00:00
|
|
|
parameters = parameters or {}
|
|
|
|
for i = 1, 16 do
|
|
|
|
parameters.buffer_size = i
|
|
|
|
local f = csv.open(filename, parameters)
|
|
|
|
local fileok = testhandle(f, correct_result)
|
2014-05-18 17:52:16 +00:00
|
|
|
|
2014-05-26 05:20:17 +00:00
|
|
|
if fileok then
|
|
|
|
f = io.open(filename, "r")
|
|
|
|
local data = f:read("*a")
|
|
|
|
f:close()
|
2014-05-18 17:52:16 +00:00
|
|
|
|
2014-05-26 05:20:17 +00:00
|
|
|
f = csv.openstring(data, parameters)
|
|
|
|
testhandle(f, correct_result)
|
|
|
|
end
|
2014-05-18 17:52:16 +00:00
|
|
|
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:20:17 +00:00
|
|
|
newline!]])
|
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:20:17 +00:00
|
|
|
alpha:four,bravo:five,charlie:6!]], {header=true})
|
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:20:17 +00:00
|
|
|
{ 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:20:17 +00:00
|
|
|
this,file,ends,with,a,blank,line!]])
|
2013-12-05 23:56:02 +00:00
|
|
|
|
2014-07-20 23:15:00 +00:00
|
|
|
test("../test-data/BOM.csv", [[
|
|
|
|
apple:one,charlie:30!
|
|
|
|
apple:four,charlie:60!]],
|
|
|
|
{ columns = {
|
|
|
|
apple = { name = "ALPHA", transform = string.lower },
|
|
|
|
charlie = { transform = function(x) return tonumber(x) * 10 end }}})
|
|
|
|
|
2014-07-21 00:58:11 +00:00
|
|
|
test("../test-data/bars.txt", [[
|
|
|
|
there's a comma in this field, but no newline,embedded
|
|
|
|
newline,embedded
|
|
|
|
newline!
|
|
|
|
embedded
|
|
|
|
newline,embedded
|
|
|
|
newline,embedded
|
|
|
|
newline!]])
|
2014-07-20 23:15:00 +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)
|