]> gitweb.factorcode.org Git - factor.git/blob - basis/csv/csv-tests.factor
Append input history to ~/.factor-history upon UI Listener ending
[factor.git] / basis / csv / csv-tests.factor
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 ;
4 IN: csv.tests
5
6 ! I like to name my unit tests
7 : named-unit-test ( name output input -- )
8     unit-test drop ; inline
9
10 "Fields are separated by commas"
11 [ { { "1997" "Ford" "E350" } } ]
12 [ "1997,Ford,E350" string>csv ] named-unit-test
13
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
17
18 "keeps spaces in quotes"
19 [ { { "1997" "Ford" "E350" "Super, luxurious truck" } } ]
20 [ "1997,Ford,E350,\"Super, luxurious truck\"" string>csv ] named-unit-test
21
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
26
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
31
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
36
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
40
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
47
48
49 ! !!!!!!!!  other tests
50
51 { { { "Phil Dawes" } } }
52 [ "\"Phil Dawes\"" string>csv ] unit-test
53
54 { { { "1" "2" "3" } { "4" "5" "6" } } }
55 [ "1,2,3\n4,5,6\n" string>csv ] unit-test
56
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
60
61
62 "allows setting of delimiting character"
63 [ { { "foo" "bah" "baz" } } ]
64 [ "foo\tbah\tbaz\n" CHAR: \t [ string>csv ] with-delimiter ] named-unit-test
65
66 "Quoted field followed immediately by newline"
67 [ { { "foo" "bar" }
68     { "1"   "2" } } ]
69 [ "foo,\"bar\"\n1,2" string>csv ] named-unit-test
70
71 "can write csv too!"
72 [ "foo1,bar1\nfoo2,bar2\n" ]
73 [ { { "foo1" "bar1" } { "foo2" "bar2" } } csv>string ] named-unit-test
74
75
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 ! "
79
80 { { { "writing" "some" "csv" "tests" } } }
81 [
82     [
83         "writing,some,csv,tests"
84         "csv-test1-" ".csv" unique-file utf8
85         [ set-file-contents ] [ file>csv ] [ drop delete-file ] 2tri
86     ] with-temp-directory
87 ] unit-test
88
89 { t } [
90     [
91         { { "writing,some,csv,tests" } } dup
92         "csv-test2-" ".csv" unique-file utf8
93         [ csv>file ] [ file>csv ] 2bi =
94     ] with-temp-directory
95 ] unit-test
96
97 { { { "hello" "" "" "" "goodbye" "" } } }
98 [ "hello,,\"\",,goodbye," string>csv ] unit-test
99
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
108
109 { { } } [ "" string>csv ] unit-test
110
111 {
112     { { "Year" "Make" "Model" }
113       { "1997" "Ford" "E350" }
114     }
115 }
116 [ "Year,Make,\"Model\"\r\n1997,Ford,E350" string>csv ] unit-test