Skip to main content
Module

std/encoding/csv_test.ts

Deno standard library
Go to Latest
File
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
// Test ported from Golang// https://github.com/golang/go/blob/2cc15b1/src/encoding/csv/reader_test.go// Copyright 2011 The Go Authors. All rights reserved. BSD license.// https://github.com/golang/go/blob/master/LICENSE// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assertEquals, assertRejects } from "../testing/asserts.ts";import { parse, ParseError } from "./csv.ts";import { StringReader } from "../io/readers.ts";import { BufReader } from "../io/buffer.ts";
Deno.test({ name: "Simple", async fn() { const input = "a,b,c\n"; assertEquals( await parse(input), [["a", "b", "c"]], ); },});Deno.test({ name: "CRLF", async fn() { const input = "a,b\r\nc,d\r\n"; assertEquals( await parse(input), [ ["a", "b"], ["c", "d"], ], ); },});
Deno.test({ name: "BareCR", async fn() { const input = "a,b\rc,d\r\n"; assertEquals( await parse(input), [["a", "b\rc", "d"]], ); },});
Deno.test({ name: "RFC4180test", async fn() { const input = '#field1,field2,field3\n"aaa","bbb","ccc"\n"a,a","bbb","ccc"\nzzz,yyy,xxx'; assertEquals( await parse(input), [ ["#field1", "field2", "field3"], ["aaa", "bbb", "ccc"], ["a,a", `bbb`, "ccc"], ["zzz", "yyy", "xxx"], ], ); },});Deno.test({ name: "NoEOLTest", async fn() { const input = "a,b,c"; assertEquals( await parse(input), [["a", "b", "c"]], ); },});
Deno.test({ name: "Semicolon", async fn() { const input = "a;b;c\n"; assertEquals( await parse(input, { separator: ";" }), [["a", "b", "c"]], ); },});
Deno.test({ name: "MultiLine", async fn() { const input = '"two\nline","one line","three\nline\nfield"'; assertEquals( await parse(input), [["two\nline", "one line", "three\nline\nfield"]], ); },});
Deno.test({ name: "BlankLine", async fn() { const input = "a,b,c\n\nd,e,f\n\n"; assertEquals( await parse(input), [ ["a", "b", "c"], ["d", "e", "f"], ], ); },});
Deno.test({ name: "BlankLineFieldCount", async fn() { const input = "a,b,c\n\nd,e,f\n\n"; assertEquals( await parse(input, { fieldsPerRecord: 0 }), [ ["a", "b", "c"], ["d", "e", "f"], ], ); },});
Deno.test({ name: "TrimSpace", async fn() { const input = " a, b, c\n"; assertEquals( await parse(input, { trimLeadingSpace: true }), [["a", "b", "c"]], ); },});
Deno.test({ name: "LeadingSpace", async fn() { const input = " a, b, c\n"; const output = [[" a", " b", " c"]]; assertEquals(await parse(input), output); },});Deno.test({ name: "Comment", async fn() { const input = "#1,2,3\na,b,c\n#comment"; const output = [["a", "b", "c"]]; assertEquals(await parse(input, { comment: "#" }), output); },});Deno.test({ name: "NoComment", async fn() { const input = "#1,2,3\na,b,c"; const output = [ ["#1", "2", "3"], ["a", "b", "c"], ]; assertEquals(await parse(input), output); },});Deno.test({ name: "LazyQuotes", async fn() { const input = `a "word","1"2",a","b`; const output = [[`a "word"`, `1"2`, `a"`, `b`]]; assertEquals(await parse(input, { lazyQuotes: true }), output); },});Deno.test({ name: "BareQuotes", async fn() { const input = `a "word","1"2",a"`; const output = [[`a "word"`, `1"2`, `a"`]]; assertEquals(await parse(input, { lazyQuotes: true }), output); },});Deno.test({ name: "BareDoubleQuotes", async fn() { const input = `a""b,c`; const output = [[`a""b`, `c`]]; assertEquals(await parse(input, { lazyQuotes: true }), output); },});Deno.test({ name: "BadDoubleQuotes", async fn() { const input = `a""b,c`; await assertRejects( async () => await parse(input), ParseError, 'parse error on line 1, column 1: bare " in non-quoted-field', ); },});Deno.test({ name: "TrimQuote", async fn() { const input = ` "a"," b",c`; const output = [["a", " b", "c"]]; assertEquals(await parse(input, { trimLeadingSpace: true }), output); },});Deno.test({ name: "BadBareQuote", async fn() { const input = `a "word","b"`; await assertRejects( async () => await parse(input), ParseError, 'parse error on line 1, column 2: bare " in non-quoted-field', ); },});Deno.test({ name: "BadTrailingQuote", async fn() { const input = `"a word",b"`; await assertRejects( async () => await parse(input), ParseError, 'parse error on line 1, column 10: bare " in non-quoted-field', ); },});Deno.test({ name: "ExtraneousQuote", async fn() { const input = `"a "word","b"`; await assertRejects( async () => await parse(input), ParseError, `parse error on line 1, column 3: extraneous or missing " in quoted-field`, ); },});Deno.test({ name: "BadFieldCount", async fn() { const input = "a,b,c\nd,e"; await assertRejects( async () => await parse(input, { fieldsPerRecord: 0 }), ParseError, "record on line 2: wrong number of fields", ); },});Deno.test({ name: "BadFieldCount1", async fn() { const input = `a,b,c`; await assertRejects( async () => await parse(input, { fieldsPerRecord: 2 }), ParseError, "record on line 1: wrong number of fields", ); },});Deno.test({ name: "FieldCount", async fn() { const input = "a,b,c\nd,e"; const output = [ ["a", "b", "c"], ["d", "e"], ]; assertEquals(await parse(input), output); },});Deno.test({ name: "TrailingCommaEOF", async fn() { const input = "a,b,c,"; const output = [["a", "b", "c", ""]]; assertEquals(await parse(input), output); },});Deno.test({ name: "TrailingCommaEOL", async fn() { const input = "a,b,c,\n"; const output = [["a", "b", "c", ""]]; assertEquals(await parse(input), output); },});Deno.test({ name: "TrailingCommaSpaceEOF", async fn() { const input = "a,b,c, "; const output = [["a", "b", "c", ""]]; assertEquals(await parse(input, { trimLeadingSpace: true }), output); },});Deno.test({ name: "TrailingCommaSpaceEOL", async fn() { const input = "a,b,c, \n"; const output = [["a", "b", "c", ""]]; assertEquals(await parse(input, { trimLeadingSpace: true }), output); },});Deno.test({ name: "TrailingCommaLine3", async fn() { const input = "a,b,c\nd,e,f\ng,hi,"; const output = [ ["a", "b", "c"], ["d", "e", "f"], ["g", "hi", ""], ]; assertEquals(await parse(input, { trimLeadingSpace: true }), output); },});Deno.test({ name: "NotTrailingComma3", async fn() { const input = "a,b,c, \n"; const output = [["a", "b", "c", " "]]; assertEquals(await parse(input), output); },});Deno.test({ name: "CommaFieldTest", async fn() { const input = `x,y,z,w\nx,y,z,\nx,y,,\nx,,,\n,,,\n"x","y","z","w"\n"x","y","z",""\n"x","y","",""\n"x","","",""\n"","","",""\n`; const output = [ ["x", "y", "z", "w"], ["x", "y", "z", ""], ["x", "y", "", ""], ["x", "", "", ""], ["", "", "", ""], ["x", "y", "z", "w"], ["x", "y", "z", ""], ["x", "y", "", ""], ["x", "", "", ""], ["", "", "", ""], ]; assertEquals(await parse(input), output); },});Deno.test({ name: "TrailingCommaIneffective1", async fn() { const input = "a,b,\nc,d,e"; const output = [ ["a", "b", ""], ["c", "d", "e"], ]; assertEquals(await parse(input, { trimLeadingSpace: true }), output); },});Deno.test({ name: "ReadAllReuseRecord", async fn() { const input = "a,b\nc,d"; const output = [ ["a", "b"], ["c", "d"], ]; assertEquals(await parse(input), output); // ReuseRecord: true, },});Deno.test({ name: "StartLine1", // Issue 19019 async fn() { const input = 'a,"b\nc"d,e'; await assertRejects( async () => await parse(input, { fieldsPerRecord: 2 }), ParseError, 'record on line 1; parse error on line 2, column 1: extraneous or missing " in quoted-field', ); },});Deno.test({ name: "StartLine2", async fn() { const input = 'a,b\n"d\n\n,e'; await assertRejects( async () => await parse(input, { fieldsPerRecord: 2 }), ParseError, 'record on line 2; parse error on line 5, column 0: extraneous or missing " in quoted-field', ); },});Deno.test({ name: "CRLFInQuotedField", // Issue 21201 async fn() { const input = 'A,"Hello\r\nHi",B\r\n'; const output = [["A", "Hello\nHi", "B"]]; assertEquals(await parse(input), output); },});Deno.test({ name: "BinaryBlobField", // Issue 19410 async fn() { const input = "x09\x41\xb4\x1c,aktau"; const output = [["x09A\xb4\x1c", "aktau"]]; assertEquals(await parse(input), output); },});Deno.test({ name: "TrailingCR", async fn() { const input = "field1,field2\r"; const output = [["field1", "field2"]]; assertEquals(await parse(input), output); },});Deno.test({ name: "QuotedTrailingCR", async fn() { const input = '"field"\r'; const output = [["field"]]; assertEquals(await parse(input), output); },});Deno.test({ name: "QuotedTrailingCRCR", async fn() { const input = '"field"\r\r'; await assertRejects( async () => await parse(input, { fieldsPerRecord: 2 }), ParseError, 'parse error on line 1, column 6: extraneous or missing " in quoted-field', ); },});Deno.test({ name: "FieldCR", async fn() { const input = "field\rfield\r"; const output = [["field\rfield"]]; assertEquals(await parse(input), output); },});Deno.test({ name: "FieldCRCR", async fn() { const input = "field\r\rfield\r\r"; const output = [["field\r\rfield\r"]]; assertEquals(await parse(input), output); },});Deno.test({ name: "FieldCRCRLF", async fn() { const input = "field\r\r\nfield\r\r\n"; const output = [["field\r"], ["field\r"]]; assertEquals(await parse(input), output); },});Deno.test({ name: "FieldCRCRLFCR", async fn() { const input = "field\r\r\n\rfield\r\r\n\r"; const output = [["field\r"], ["\rfield\r"]]; assertEquals(await parse(input), output); },});Deno.test({ name: "FieldCRCRLFCRCR", async fn() { const input = "field\r\r\n\r\rfield\r\r\n\r\r"; const output = [["field\r"], ["\r\rfield\r"], ["\r"]]; assertEquals(await parse(input), output); },});Deno.test({ name: "MultiFieldCRCRLFCRCR", async fn() { const input = "field1,field2\r\r\n\r\rfield1,field2\r\r\n\r\r,"; const output = [ ["field1", "field2\r"], ["\r\rfield1", "field2\r"], ["\r\r", ""], ]; assertEquals(await parse(input), output); },});Deno.test({ name: "NonASCIICommaAndComment", async fn() { const input = "a£b,c£ \td,e\n€ comment\n"; const output = [["a", "b,c", "d,e"]]; assertEquals( await parse(input, { trimLeadingSpace: true, separator: "£", comment: "€", }), output, ); },});Deno.test({ name: "NonASCIICommaAndCommentWithQuotes", async fn() { const input = 'a€" b,"€ c\nλ comment\n'; const output = [["a", " b,", " c"]]; assertEquals( await parse(input, { separator: "€", comment: "λ" }), output, ); },});Deno.test( { // λ and θ start with the same byte. // This tests that the parser doesn't confuse such characters. name: "NonASCIICommaConfusion", async fn() { const input = '"abθcd"λefθgh'; const output = [["abθcd", "efθgh"]]; assertEquals( await parse(input, { separator: "λ", comment: "€" }), output, ); }, },);Deno.test({ name: "NonASCIICommentConfusion", async fn() { const input = "λ\nλ\nθ\nλ\n"; const output = [["λ"], ["λ"], ["λ"]]; assertEquals(await parse(input, { comment: "θ" }), output); },});Deno.test({ name: "QuotedFieldMultipleLF", async fn() { const input = '"\n\n\n\n"'; const output = [["\n\n\n\n"]]; assertEquals(await parse(input), output); },});Deno.test({ name: "MultipleCRLF", async fn() { const input = "\r\n\r\n\r\n\r\n"; const output: string[][] = []; assertEquals(await parse(input), output); }, /** * The implementation may read each line in several chunks if * it doesn't fit entirely. * in the read buffer, so we should test the code to handle that condition. */} /* TODO(kt3k): Enable this test case) Deno.test({ name: "HugeLines", async fn() { const input = "#ignore\n".repeat(10000) + "@".repeat(5000) + "," "*".repeat(5000), const output = [["@".repeat(5000), "*".repeat(5000)]] assertEquals(await parse(input), output) Comment: "#", }, }*/);Deno.test({ name: "QuoteWithTrailingCRLF", async fn() { const input = '"foo"bar"\r\n'; await assertRejects( async () => await parse(input), ParseError, `parse error on line 1, column 4: extraneous or missing " in quoted-field`, ); },});Deno.test({ name: "LazyQuoteWithTrailingCRLF", async fn() { const input = '"foo"bar"\r\n'; const output = [[`foo"bar`]]; assertEquals(await parse(input, { lazyQuotes: true }), output); },});Deno.test({ name: "DoubleQuoteWithTrailingCRLF", async fn() { const input = '"foo""bar"\r\n'; const output = [[`foo"bar`]]; assertEquals(await parse(input), output); },});Deno.test({ name: "EvenQuotes", async fn() { const input = `""""""""`; const output = [[`"""`]]; assertEquals(await parse(input), output); },});Deno.test({ name: "OddQuotes", async fn() { const input = `"""""""`; await assertRejects( async () => await parse(input), ParseError, `parse error on line 1, column 7: extraneous or missing " in quoted-field`, ); },});Deno.test({ name: "LazyOddQuotes", async fn() { const input = `"""""""`; const output = [[`"""`]]; assertEquals(await parse(input, { lazyQuotes: true }), output); },});Deno.test({ name: "BadComma1", async fn() { const input = ""; await assertRejects( async () => await parse(input, { separator: "\n" }), Error, "Invalid Delimiter", ); },});Deno.test({ name: "BadComma2", async fn() { const input = ""; await assertRejects( async () => await parse(input, { separator: "\r" }), Error, "Invalid Delimiter", ); },});Deno.test({ name: "BadComma3", async fn() { const input = ""; await assertRejects( async () => await parse(input, { separator: '"' }), Error, "Invalid Delimiter", ); },});Deno.test({ name: "BadComment1", async fn() { const input = ""; await assertRejects( async () => await parse(input, { comment: "\n" }), Error, "Invalid Delimiter", ); },});Deno.test({ name: "BadComment2", async fn() { const input = ""; await assertRejects( async () => await parse(input, { comment: "\r" }), Error, "Invalid Delimiter", ); },});Deno.test({ name: "BadCommaComment", async fn() { const input = ""; await assertRejects( async () => await parse(input, { separator: "X", comment: "X" }), Error, "Invalid Delimiter", ); },});
Deno.test({ name: "simple", async fn() { const input = "a,b,c"; const output = [["a", "b", "c"]]; assertEquals(await parse(input, { skipFirstRow: false }), output); },});Deno.test({ name: "simple Bufreader", async fn() { const input = new BufReader(new StringReader("a,b,c")); const output = [["a", "b", "c"]]; assertEquals(await parse(input, { skipFirstRow: false }), output); },});Deno.test({ name: "multiline", async fn() { const input = "a,b,c\ne,f,g\n"; const output = [ ["a", "b", "c"], ["e", "f", "g"], ]; assertEquals(await parse(input, { skipFirstRow: false }), output); },});Deno.test({ name: "header mapping boolean", async fn() { const input = "a,b,c\ne,f,g\n"; const output = [{ a: "e", b: "f", c: "g" }]; assertEquals(await parse(input, { skipFirstRow: true }), output); },});Deno.test({ name: "header mapping array", async fn() { const input = "a,b,c\ne,f,g\n"; const output = [ { this: "a", is: "b", sparta: "c" }, { this: "e", is: "f", sparta: "g" }, ]; assertEquals( await parse(input, { columns: ["this", "is", "sparta"] }), output, ); },});Deno.test({ name: "header mapping object", async fn() { const input = "a,b,c\ne,f,g\n"; const output = [ { this: "a", is: "b", sparta: "c" }, { this: "e", is: "f", sparta: "g" }, ]; assertEquals( await parse(input, { columns: [{ name: "this" }, { name: "is" }, { name: "sparta" }], }), output, ); },});Deno.test({ name: "provides both opts.skipFirstRow and opts.columns", async fn() { const input = "a,b,1\nc,d,2\ne,f,3"; const output = [ { foo: "c", bar: "d", baz: "2" }, { foo: "e", bar: "f", baz: "3" }, ]; assertEquals( await parse(input, { skipFirstRow: true, columns: [{ name: "foo" }, { name: "bar" }, { name: "baz" }], }), output, ); },});Deno.test({ name: "mismatching number of headers and fields", async fn() { const input = "a,b,c\nd,e"; await assertRejects( async () => await parse(input, { skipFirstRow: true, columns: [{ name: "foo" }, { name: "bar" }, { name: "baz" }], }), Error, "Error number of fields line: 1\nNumber of fields found: 3\nExpected number of fields: 2", ); },});