Skip to content

Commit

Permalink
fixed bug when assigning fields with ignoreQuotes and wrote some more…
Browse files Browse the repository at this point in the history
… unit tests
  • Loading branch information
FourierTransformer committed Apr 15, 2019
1 parent 7f45445 commit b6eedb5
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
8 changes: 5 additions & 3 deletions ftcsv.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local ftcsv = {
_LICENSE = [[
The MIT License (MIT)
Copyright (c) 2016-2018 Shakil Thakur
Copyright (c) 2016-2019 Shakil Thakur
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -88,7 +88,9 @@ else
function luaCompatibility.findClosingQuote(i, inputLength, inputString, quote, doubleQuoteEscape)
local j, difference
i, j = inputString:find('"+', i)
if j == nil then return end
if j == nil then
return nil
end
if i == nil then
return inputLength-1, doubleQuoteEscape
end
Expand Down Expand Up @@ -189,7 +191,7 @@ local function parseString(inputString, i, options)
if fieldsToKeep == nil or fieldsToKeep[headerField[fieldNum]] then

-- create new field
if sbyte(inputString, i-1) == quote then
if ignoreQuotes == false and sbyte(inputString, i-1) == quote then
field = ssub(inputString, fieldStart, i-2)
else
field = ssub(inputString, fieldStart, i-1)
Expand Down
29 changes: 29 additions & 0 deletions spec/dynamic_features_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -460,4 +460,33 @@ describe("csv features", function()
end
end

for bom, i in pairs(BOM) do
for newline, j in pairs(newlines) do
for _, endline in ipairs(endlines) do
local name = "should handle ignoring quotes (%s + %s + %s) EOF: %s"
it(name:format(bom, newline, quote, endline), function()
local expectedHeaders = {"a", "b", "c"}
local expected = {}
expected[1] = {}
expected[1].a = '"apple"'
expected[1].b = '"banana"'
expected[1].c = '"carrot"'

local defaultString = '%sa,b,c%s"apple","banana","carrot"%s'

if endline == "NONE" then
defaultString = defaultString:format(i, j, "")
else
defaultString = defaultString:format(i, j, j)
end

local options = {loadFromString=true, ignoreQuotes=true}
local actual, actualHeaders = ftcsv.parse(defaultString, ",", options)
assert.are.same(expected, actual)
assert.are.same(expectedHeaders, actualHeaders)
end)
end
end
end

end)
15 changes: 14 additions & 1 deletion spec/error_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,17 @@ it("should error out when you want to encode a table and specify a field that do
end

assert.has_error(test, "ftcsv: the field 'c' doesn't exist in the inputTable")
end)
end)

describe("parseLine features small, nonworking buffer size", function()
it("should error out when trying to load from string", function()
local test = function()
local parse = {}
for i, line in ftcsv.parseLine("a,b,c\n1,2,3", ",", 63, {loadFromString=true}) do
parse[i] = line
end
return parse
end
assert.has_error(test, "ftcsv: parseLine currently doesn't support loading from string")
end)
end)
10 changes: 10 additions & 0 deletions spec/feature_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,14 @@ describe("csv features", function()
assert.are.same(expected, actual)
end)

it("should handle headers attempting to escape", function()
local expected = {}
expected[1] = {}
expected[1]["]] print('hello')"] = "apple"
expected[1].b = "banana"
expected[1].c = "carrot"
local actual = ftcsv.parse("]] print('hello'),b,c\napple,banana,carrot", ",", {loadFromString=true})
assert.are.same(expected, actual)
end)

end)

0 comments on commit b6eedb5

Please sign in to comment.