added tests for embedded quotes and reading files with headers

This commit is contained in:
Geoff Leyland 2013-12-05 21:37:25 +13:00
parent 1a8aa46661
commit 279ca0717d
4 changed files with 31 additions and 3 deletions

View File

@ -43,7 +43,7 @@ local function build_column_name_map(columns)
t = { transform = v.transform, default = v.default }
if v.name then
names = { (v.name:gsub("_+", " ")) }
else
elseif v.names then
names = v.names
for i, n in ipairs(names) do names[i] = n:gsub("_+", " ") end
end

View File

@ -5,19 +5,26 @@ local errors = 0
local function test(filename, correct_result, parameters)
local result = {}
local f = csv.open(filename)
local f = csv.open(filename, parameters)
for r in f:lines() do
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
result[#result+1] = table.concat(r, ",")
end
result = table.concat(result, "\n")
if result ~= correct_result then
io.stderr:write(
("Error reading %s. Expected output\n%s\nActual output\n%s\n"):
("Error reading '%s':\nExpected output:\n%s\n\nActual output:\n%s\n\n"):
format(filename, correct_result, result))
errors = errors + 1
end
end
test("../test-data/embedded-newlines.csv", [[
embedded
newline,embedded
@ -28,6 +35,22 @@ newline,embedded
newline,embedded
newline]])
test("../test-data/embedded-quotes.csv", [[
embedded "quotes",embedded "quotes",embedded "quotes"
embedded "quotes",embedded "quotes",embedded "quotes"]])
test("../test-data/header.csv", [[
alpha:ONE,bravo:two,charlie:3
alpha:four,bravo:five,charlie:6]], {header=true})
test("../test-data/header.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 }}})
if errors == 0 then
io.stdout:write("Passed\n")
elseif errors == 1 then

View File

@ -0,0 +1,2 @@
"embedded ""quotes""","embedded ""quotes""","embedded ""quotes"""
"embedded ""quotes""","embedded ""quotes""","embedded ""quotes"""
1 embedded "quotes" embedded "quotes" embedded "quotes"
2 embedded "quotes" embedded "quotes" embedded "quotes"

3
test-data/header.csv Normal file
View File

@ -0,0 +1,3 @@
alpha,bravo,charlie
ONE,two,3
four,five,6
1 alpha bravo charlie
2 ONE two 3
3 four five 6