1 USING: io.streams.string csv tools.test kernel strings
2 io.pathnames io.files.temp io.files.unique io.encodings.utf8
3 io.files io.directories ;
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>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>csv ] named-unit-test
18 "keeps spaces in quotes"
19 [ { { "1997" "Ford" "E350" "Super, luxurious truck" } } ]
20 [ "1997,Ford,E350,\"Super, luxurious truck\"" string>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>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>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>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>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>csv ] named-unit-test
49 ! !!!!!!!! other tests
51 { { { "Phil Dawes" } } }
52 [ "\"Phil Dawes\"" string>csv ] unit-test
54 { { { "1" "2" "3" } { "4" "5" "6" } } }
55 [ "1,2,3\n4,5,6\n" string>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>csv ] named-unit-test
62 "allows setting of delimiting character"
63 [ { { "foo" "bah" "baz" } } ]
64 [ "foo\tbah\tbaz\n" CHAR: \t [ string>csv ] with-delimiter ] named-unit-test
66 "Quoted field followed immediately by newline"
69 [ "foo,\"bar\"\n1,2" string>csv ] named-unit-test
72 [ "foo1,bar1\nfoo2,bar2\n" ]
73 [ { { "foo1" "bar1" } { "foo2" "bar2" } } csv>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" } } csv>string ] named-unit-test ! "
80 { { { "writing" "some" "csv" "tests" } } }
83 "writing,some,csv,tests"
84 "csv-test1-" ".csv" unique-file utf8
85 [ set-file-contents ] [ file>csv ] [ drop delete-file ] 2tri
91 { { "writing,some,csv,tests" } } dup
92 "csv-test2-" ".csv" unique-file utf8
93 [ csv>file ] [ file>csv ] 2bi =
97 { { { "hello" "" "" "" "goodbye" "" } } }
98 [ "hello,,\"\",,goodbye," string>csv ] unit-test
100 { { { "asd\"f\"" "asdf" } } } [ "asd\"f\",asdf" string>csv ] unit-test
101 { { { "a\"sdf" "asdf" } } } [ "a\"sdf,asdf" string>csv ] unit-test
102 { { { "as\"df" "asdf" } } } [ " as\"df,asdf" string>csv ] unit-test
103 { { { "asd" "f" "asdf" } } } [ "\"as\"d,f,asdf" string>csv ] unit-test
104 { { { "as,df" "asdf" } } } [ "\"as,\"df,asdf" string>csv ] unit-test
105 ! FIXME: { { { "as,df" "asdf" } } } [ "\"as,\"df ,asdf" string>csv ] unit-test
106 ! FIXME: { { { "asd\"f\"" "asdf" } } } [ "\"asd\"\"\"f\",asdf" string>csv ] unit-test
107 { { { "as,d\"f" "asdf" } } } [ "\"as,\"d\"\"\"\"f,asdf" string>csv ] unit-test
109 { { } } [ "" string>csv ] unit-test
112 { { "Year" "Make" "Model" }
113 { "1997" "Ford" "E350" }
116 [ "Year,Make,\"Model\"\r\n1997,Ford,E350" string>csv ] unit-test