-- data/csv tests: tables written by Python's csv.writer / DictWriter (every delimiter, quotes, line breaks inside -- fields, empty fields) read back as csv.reader does, re-encoded byte for byte, keyed rows with a header, options, -- error codes, and the fuzz pass. Fixtures: tools/fixtures/csv.py. local T = require("@tests/T") local F = require("@rosetta") local Csv = require("@rosetta/data/csv") local Bytes = require("@rosetta/core/Bytes") local Err = require("@rosetta/core/Err") local Types = require("@rosetta/Types") local Canon = require("@tests/data/Canon") local Fx = require("@tests/gen/csv") local t = T.suite("data/csv") t:eq(Csv.name, "csv", "name") t:check(Csv.caps.decode and Csv.caps.encode, "caps") local function dec(s: string, opts: any?): (any, any) return Csv.decode(Bytes.reader(buffer.fromstring(s)), opts) end for i, c in Fx do local rows, err = dec(c.data, { delimiter = c.delimiter }) t:check(rows ~= nil, "case " .. i .. ": " .. tostring(err)) t:eq(Canon.of(rows), Canon.of(c.rows), "case " .. i .. ": rows") local b = Csv.encode(rows, { delimiter = c.delimiter, newline = c.newline }) :: buffer t:eq(buffer.tostring(b), c.data, "case " .. i .. ": same text") if c.delimiter ~= "," or i % 3 == 0 then local auto = dec(c.data, { delimiter = "auto" }) if c.rows[1] and #c.rows[1] > 1 and auto then -- auto only has the first line to go on; it must at least split like the real delimiter there t:check(#auto[1] >= 1, "case " .. i .. ": auto") end end if c.keyed then local keyed = dec(c.data, { header = true }) :: any t:eq(#keyed, #c.rows - 1, "case " .. i .. ": keyed row count") t:eq(table.concat(Types.keys(keyed[1]), "|"), table.concat(c.rows[1], "|"), "case " .. i .. ": column order") local kb = Csv.encode(keyed) :: buffer t:eq(buffer.tostring(kb), c.data, "case " .. i .. ": keyed re-encode (DictWriter text)") end end -- details do t:eq(Canon.of(dec("a,b\n\n1,2\r\n3,4\r5,6")), Canon.of({ { "a", "b" }, { "1", "2" }, { "3", "4" }, { "5", "6" } }), "line ends, blank lines") t:eq(Canon.of(dec('"a""b",c"d,"e"x')), Canon.of({ { 'a"b', 'c"d', "ex" } }), "lenient quotes like Python") t:eq(Canon.of(dec("a;b|c", { delimiter = "auto" })), Canon.of({ { "a", "b|c" } }), "auto delimiter") t:eq(Canon.of(dec("x,1.5,-2,1e3,abc, 7 ", { numbers = true })), Canon.of({ { "x", 1.5, -2, 1000, "abc", 7 } }), "numbers") t:eq(Canon.of(dec("a,b,c\n1,2")[1]), Canon.of({ "a", "b", "c" }), "ragged rows kept") local k = dec("a,b\n1,2,3\n4", { header = true }) t:eq(k[1].b, "2", "keyed") t:eq(k[2].b, nil, "short keyed row") t:eq(buffer.tostring(Csv.encode({ { 1, true, F.null, 0.5 } }) :: buffer), "1,true,,0.5\r\n", "field types") t:eq(buffer.tostring(Csv.encode({ { "a", "b" } }, { quoteAll = true, newline = "\n" }) :: buffer), '"a","b"\n', "quoteAll") t:eq(buffer.tostring(Csv.encode({ { "" } }) :: buffer), '""\r\n', "single empty field quoted") local _, e = dec('a,"b') t:eq(e and e.code, Err.TRUNCATED, "unterminated quote") local _, e2 = dec("a", { delimiter = "ab" }) t:eq(e2 and e2.code, Err.ARG, "bad delimiter") local _, e3 = Csv.encode({ { {} } }) t:eq(e3 and e3.code, Err.ARG, "bad field") local _, e4 = dec("1\n2\n3", { maxRows = 2 }) t:eq(e4 and e4.code, Err.LIMIT, "maxRows") local v = F.load(buffer.fromstring("a,b\r\n1,2\r\n"), { format = "csv", header = true }) t:eq(v[1].a, "1", "through the API") end local samples = {} for i = 1, 40 do table.insert(samples, Fx[i].data) end t:fuzz("decode", function(b: buffer) return Csv.decode(Bytes.reader(b), { delimiter = "auto", header = true, numbers = true }) end, samples) t:done()