]> gitweb.factorcode.org Git - factor.git/blob - extra/csv/csv-tests.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / extra / csv / csv-tests.factor
1 IN: csv.tests
2 USING: io.streams.string csv tools.test shuffle kernel strings ;
3
4 ! I like to name my unit tests
5 : named-unit-test ( name output input -- ) 
6   nipd unit-test ; inline
7
8 ! tests nicked from the wikipedia csv article
9 ! http://en.wikipedia.org/wiki/Comma-separated_values
10
11 "Fields are separated by commas"
12 [ { { "1997" "Ford" "E350" } } ] 
13 [ "1997,Ford,E350" <string-reader> csv ] named-unit-test
14
15 "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.'"
16 [ { { "1997" "Ford" "E350" } } ]
17 [ "1997,   Ford   , E350" <string-reader> csv ] named-unit-test
18
19 "keeps spaces in quotes"
20 [ { { "1997" "Ford" "E350" "Super, luxurious truck" } } ]
21 [ "1997,Ford,E350,\"Super, luxurious truck\"" <string-reader> csv ] named-unit-test
22
23 "double quotes mean escaped in quotes"
24 [ { { "1997" "Ford" "E350" "Super \"luxurious\" truck" } } ]
25 [ "1997,Ford,E350,\"Super \"\"luxurious\"\" truck\"" 
26   <string-reader> csv ] named-unit-test
27
28 "Fields with embedded line breaks must be delimited by double-quote characters."
29 [ { { "1997" "Ford" "E350" "Go get one now\nthey are going fast" } } ]
30 [ "1997,Ford,E350,\"Go get one now\nthey are going fast\""
31   <string-reader> csv ] named-unit-test
32
33 "Fields with leading or trailing spaces must be delimited by double-quote characters. (See comment about leading and trailing spaces above)"
34 [ { { "1997" "Ford" "E350" "  Super luxurious truck    " } } ]
35 [ "1997,Ford,E350,\"  Super luxurious truck    \""
36   <string-reader> csv ] named-unit-test
37
38 "Fields may always be delimited by double-quote characters, whether necessary or not."
39 [ { { "1997" "Ford" "E350" } } ]
40 [ "\"1997\",\"Ford\",\"E350\"" <string-reader> csv ] named-unit-test
41
42 "The first record in a csv file may contain column names in each of the fields."
43 [ { { "Year" "Make" "Model" } 
44     { "1997" "Ford" "E350" }
45     { "2000" "Mercury" "Cougar" } } ]
46 [ "Year,Make,Model\n1997,Ford,E350\n2000,Mercury,Cougar" 
47    <string-reader> csv ] named-unit-test
48
49
50 ! !!!!!!!!  other tests
51    
52 [ { { "Phil Dawes" } } ] 
53 [ "\"Phil Dawes\"" <string-reader> csv ] unit-test
54
55 [ { { "1" "2" "3" } { "4" "5" "6" } } ] 
56 [ "1,2,3\n4,5,6\n" <string-reader> csv ] unit-test
57
58 "trims leading and trailing whitespace - n.b. this isn't really conformant, but lots of csv seems to assume this"
59 [ { { "foo yeah" "bah" "baz" } } ] 
60 [ "  foo yeah  , bah ,baz\n" <string-reader> csv ] named-unit-test
61
62
63 "allows setting of delimiting character"
64 [ { { "foo" "bah" "baz" } } ] 
65 [ "foo\tbah\tbaz\n" <string-reader> CHAR: \t [ csv ] with-delimiter ] named-unit-test
66
67 "Quoted field followed immediately by newline"
68 [ { { "foo" "bar" }
69     { "1"   "2" } } ]
70 [ "foo,\"bar\"\n1,2" <string-reader> csv ] named-unit-test
71
72 "can write csv too!"
73 [ "foo1,bar1\nfoo2,bar2\n" ]
74 [ { { "foo1" "bar1" } { "foo2" "bar2" } } <string-writer> tuck write-csv >string ] named-unit-test
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" } } <string-writer> tuck write-csv >string ] named-unit-test ! "