1 USING: io.streams.string csv tools.test kernel strings
2 io.pathnames io.files.unique io.encodings.utf8 io.files
6 ! I like to name my unit tests
7 : named-unit-test ( name output input -- )
8 unit-test drop ; inline
10 "Fields are separated by commas"
11 [ { { "1997" "Ford" "E350" } } ]
12 [ "1997,Ford,E350" <string-reader> csv ] named-unit-test
14 "ignores whitespace before and after elements. n.b.specifically prohibited by RFC 4180, which states, 'Spaces are considered part of a field and should not be ignored.'"
15 [ { { "1997" "Ford" "E350" } } ]
16 [ "1997, Ford , E350" <string-reader> csv ] named-unit-test
18 "keeps spaces in quotes"
19 [ { { "1997" "Ford" "E350" "Super, luxurious truck" } } ]
20 [ "1997,Ford,E350,\"Super, luxurious truck\"" <string-reader> csv ] named-unit-test
22 "double quotes mean escaped in quotes"
23 [ { { "1997" "Ford" "E350" "Super \"luxurious\" truck" } } ]
24 [ "1997,Ford,E350,\"Super \"\"luxurious\"\" truck\""
25 <string-reader> csv ] named-unit-test
27 "Fields with embedded line breaks must be delimited by double-quote characters."
28 [ { { "1997" "Ford" "E350" "Go get one now\nthey are going fast" } } ]
29 [ "1997,Ford,E350,\"Go get one now\nthey are going fast\""
30 <string-reader> csv ] named-unit-test
32 "Fields with leading or trailing spaces must be delimited by double-quote characters. (See comment about leading and trailing spaces above)"
33 [ { { "1997" "Ford" "E350" " Super luxurious truck " } } ]
34 [ "1997,Ford,E350,\" Super luxurious truck \""
35 <string-reader> csv ] named-unit-test
37 "Fields may always be delimited by double-quote characters, whether necessary or not."
38 [ { { "1997" "Ford" "E350" } } ]
39 [ "\"1997\",\"Ford\",\"E350\"" <string-reader> csv ] named-unit-test
41 "The first record in a csv file may contain column names in each of the fields."
42 [ { { "Year" "Make" "Model" }
43 { "1997" "Ford" "E350" }
44 { "2000" "Mercury" "Cougar" } } ]
45 [ "Year,Make,Model\n1997,Ford,E350\n2000,Mercury,Cougar"
46 <string-reader> csv ] named-unit-test
49 ! !!!!!!!! other tests
51 [ { { "Phil Dawes" } } ]
52 [ "\"Phil Dawes\"" <string-reader> csv ] unit-test
54 [ { { "1" "2" "3" } { "4" "5" "6" } } ]
55 [ "1,2,3\n4,5,6\n" <string-reader> csv ] unit-test
57 "trims leading and trailing whitespace - n.b. this isn't really conformant, but lots of csv seems to assume this"
58 [ { { "foo yeah" "bah" "baz" } } ]
59 [ " foo yeah , bah ,baz\n" <string-reader> csv ] named-unit-test
62 "allows setting of delimiting character"
63 [ { { "foo" "bah" "baz" } } ]
64 [ "foo\tbah\tbaz\n" <string-reader> CHAR: \t [ csv ] with-delimiter ] named-unit-test
66 "Quoted field followed immediately by newline"
69 [ "foo,\"bar\"\n1,2" <string-reader> csv ] named-unit-test
72 [ "foo1,bar1\nfoo2,bar2\n" ]
73 [ { { "foo1" "bar1" } { "foo2" "bar2" } } <string-writer> [ write-csv ] keep >string ] named-unit-test
76 "escapes quotes commas and newlines when writing"
77 [ "\"fo\"\"o1\",bar1\n\"fo\no2\",\"b,ar2\"\n" ]
78 [ { { "fo\"o1" "bar1" } { "fo\no2" "b,ar2" } } <string-writer> [ write-csv ] keep >string ] named-unit-test ! "
80 [ { { "writing" "some" "csv" "tests" } } ]
82 "writing,some,csv,tests"
83 "csv-test1-" unique-file utf8
84 [ set-file-contents ] [ file>csv ] [ drop delete-file ] 2tri
88 { { "writing,some,csv,tests" } } dup "csv-test2-"
89 unique-file utf8 [ csv>file ] [ file>csv ] 2bi =
92 [ { { "hello" "" "" "" "goodbye" "" } } ] [ "hello,,\"\",,goodbye," <string-reader> csv ] unit-test