ftcsv/spec/parseLine_spec.lua
FourierTransformer 86686314e0
New Release 1.2.0 (#26)
## Features
 * Can now parse files line by line in a fixed-size reading mode
 * Now has an option to ignore quotes when parsing

## Improvements
 * Speed increases in vanilla Lua and LuaJIT (benchmarks updated!)
 * Refactored code for easier maintenance

## Bugfixes
 * Better handling of multiple escaped quotes in vanilla lua (thanks @fredrikj83 #25)
2020-04-04 13:47:24 -05:00

77 lines
2.6 KiB
Lua

local ftcsv = require('ftcsv')
local cjson = require('cjson')
local function loadFile(textFile)
local file = io.open(textFile, "r")
if not file then error("File not found at " .. textFile) end
local allLines = file:read("*all")
file:close()
return allLines
end
describe("parseLine features small, working buffer size", function()
it("should handle correctness", function()
local json = loadFile("spec/json/correctness.json")
json = cjson.decode(json)
local parse = {}
for i, line in ftcsv.parseLine("spec/csvs/correctness.csv", ",", {bufferSize=52}) do
assert.are.same(json[i], line)
parse[i] = line
end
assert.are.same(#json, #parse)
assert.are.same(json, parse)
end)
end)
describe("parseLine features small, nonworking buffer size", function()
it("should handle correctness", function()
local test = function()
local parse = {}
for i, line in ftcsv.parseLine("spec/csvs/correctness.csv", ",", {bufferSize=63}) do
parse[i] = line
end
return parse
end
assert.has_error(test, "ftcsv: bufferSize needs to be larger to parse this file")
end)
end)
describe("parseLine features smaller, nonworking buffer size", function()
it("should handle correctness", function()
local test = function()
local parse = {}
for i, line in ftcsv.parseLine("spec/csvs/correctness.csv", ",", {bufferSize=50}) do
parse[i] = line
end
return parse
end
assert.has_error(test, "ftcsv: bufferSize needs to be larger to parse this file")
end)
end)
describe("smaller bufferSize than header and incorrect number of fields", function()
it("should handle correctness", function()
local test = function()
local parse = {}
for i, line in ftcsv.parseLine("spec/csvs/correctness.csv", ",", {bufferSize=23}) do
parse[i] = line
end
return parse
end
assert.has_error(test, "ftcsv: bufferSize needs to be larger to parse this file")
end)
end)
describe("smaller bufferSize than header, but with correct field numbers", function()
it("should handle correctness", function()
local test = function()
local parse = {}
for i, line in ftcsv.parseLine("spec/csvs/correctness.csv", ",", {bufferSize=30}) do
parse[i] = line
end
return parse
end
assert.has_error(test, "ftcsv: bufferSize needs to be larger to parse this file")
end)
end)