ftcsv/spec/parse_encode_spec.lua

90 lines
2.3 KiB
Lua
Raw Normal View History

2016-03-09 12:37:25 +00:00
local cjson = require("cjson")
local ftcsv = require('ftcsv')
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
local files = {
2017-12-01 04:46:10 +00:00
"bom-os9",
2016-03-09 12:37:25 +00:00
"comma_in_quotes",
"correctness",
"empty",
"empty_no_newline",
"empty_no_quotes",
"empty_crlf",
"escaped_quotes",
"escaped_quotes_in_header",
2016-03-09 12:37:25 +00:00
"json",
"json_no_newline",
"newlines",
"newlines_crlf",
2017-12-01 04:46:10 +00:00
"os9",
2016-03-09 12:37:25 +00:00
"quotes_and_newlines",
"quotes_non_escaped",
2016-03-09 12:37:25 +00:00
"simple",
"simple_crlf",
"utf8"
}
describe("csv decode", function()
for _, value in ipairs(files) do
it("should handle " .. value, function()
local json = loadFile("spec/json/" .. value .. ".json")
json = cjson.decode(json)
local parse = ftcsv.parse("spec/csvs/" .. value .. ".csv", ",")
2016-03-19 18:27:47 +00:00
assert.are.same(#json, #parse)
2016-03-09 12:37:25 +00:00
assert.are.same(json, parse)
end)
end
end)
describe("csv parseLine decode", function()
for _, value in ipairs(files) do
it("should handle " .. value, function()
local json = loadFile("spec/json/" .. value .. ".json")
json = cjson.decode(json)
local parse = {}
for i, v in ftcsv.parseLine("spec/csvs/" .. value .. ".csv", ",") do
parse[i] = v
assert.are.same(json[i], v)
end
assert.are.same(#json, #parse)
assert.are.same(json, parse)
end)
end
end)
describe("csv decode from string", function()
for _, value in ipairs(files) do
it("should handle " .. value, function()
local contents = loadFile("spec/csvs/" .. value .. ".csv")
local json = loadFile("spec/json/" .. value .. ".json")
json = cjson.decode(json)
local parse = ftcsv.parse(contents, ",", {loadFromString=true})
assert.are.same(json, parse)
end)
end
end)
2016-03-09 12:37:25 +00:00
describe("csv encode", function()
for _, value in ipairs(files) do
it("should handle " .. value, function()
local jsonFile = loadFile("spec/json/" .. value .. ".json")
local jsonDecode = cjson.decode(jsonFile)
-- local parse = staecsv:ftcsv(contents, ",")
local reEncoded = ftcsv.parse(ftcsv.encode(jsonDecode, ","), ",", {loadFromString=true})
2016-03-09 12:37:25 +00:00
-- local f = csv.openstring(contents, {separator=",", header=true})
-- local parse = {}
-- for fields in f:lines() do
-- parse[#parse+1] = fields
-- end
assert.are.same(jsonDecode, reEncoded)
end)
end
end)