1 #!/usr/local/bin/ruby -w
5 # Created by James Edward Gray II on 2005-10-31.
6 # Copyright 2005 James Edward Gray II. You can redistribute or modify this code
7 # under the terms of Ruby's license.
13 class TestCSVWriting < Test::Unit::TestCase
16 ["foo,\"\"\"\"\"\",baz", ["foo", "\"\"", "baz"]],
17 ["foo,\"\"\"bar\"\"\",baz", ["foo", "\"bar\"", "baz"]],
18 ["\"\"\"\n\",\"\"\"\n\"", ["\"\n", "\"\n"]],
19 ["foo,\"\r\n\",baz", ["foo", "\r\n", "baz"]],
21 ["foo,\"\"\"\",baz", ["foo", "\"", "baz"]],
22 ["foo,\"\r.\n\",baz", ["foo", "\r.\n", "baz"]],
23 ["foo,\"\r\",baz", ["foo", "\r", "baz"]],
24 ["foo,\"\",baz", ["foo", "", "baz"]],
27 [",,", [nil, nil, nil]],
29 ["foo,\"\n\",baz", ["foo", "\n", "baz"]],
30 ["foo,,baz", ["foo", nil, "baz"]],
31 ["\"\"\"\r\",\"\"\"\r\"", ["\"\r", "\"\r"]],
32 ["\",\",\",\"", [",", ","]],
33 ["foo,bar,", ["foo", "bar", nil]],
34 [",foo,bar", [nil, "foo", "bar"]],
35 ["foo,bar", ["foo", "bar"]],
37 ["\t,\t", ["\t", "\t"]],
38 ["foo,\"\r\n\r\",baz", ["foo", "\r\n\r", "baz"]],
39 ["foo,\"\r\n\n\",baz", ["foo", "\r\n\n", "baz"]],
40 ["foo,\"foo,bar\",baz", ["foo", "foo,bar", "baz"]],
42 ["foo,\"\"\"\"\"\",baz", ["foo", "\"\"", "baz"]],
43 ["foo,\"\"\"bar\"\"\",baz", ["foo", "\"bar\"", "baz"]],
44 ["foo,\"\r\n\",baz", ["foo", "\r\n", "baz"]],
46 ["foo,\"\"\"\",baz", ["foo", "\"", "baz"]],
47 ["foo,\"\r.\n\",baz", ["foo", "\r.\n", "baz"]],
48 ["foo,\"\r\",baz", ["foo", "\r", "baz"]],
49 ["foo,\"\",baz", ["foo", "", "baz"]],
51 [",,", [nil, nil, nil]],
53 ["foo,\"\n\",baz", ["foo", "\n", "baz"]],
54 ["foo,,baz", ["foo", nil, "baz"]],
55 ["foo,bar", ["foo", "bar"]],
56 ["foo,\"\r\n\n\",baz", ["foo", "\r\n\n", "baz"]],
57 ["foo,\"foo,bar\",baz", ["foo", "foo,bar", "baz"]],
58 [%Q{a,b}, ["a", "b"]],
59 [%Q{a,"""b"""}, ["a", "\"b\""]],
60 [%Q{a,"""b"}, ["a", "\"b"]],
61 [%Q{a,"b"""}, ["a", "b\""]],
62 [%Q{a,"\nb"""}, ["a", "\nb\""]],
63 [%Q{a,"""\nb"}, ["a", "\"\nb"]],
64 [%Q{a,"""\nb\n"""}, ["a", "\"\nb\n\""]],
65 [%Q{a,"""\nb\n""",}, ["a", "\"\nb\n\"", nil]],
66 [%Q{a,,,}, ["a", nil, nil, nil]],
68 [%Q{"",""}, ["", ""]],
70 [%Q{"""",""}, ["\"",""]],
72 [%Q{,"\r"}, [nil,"\r"]],
73 [%Q{"\r\n,"}, ["\r\n,"]],
74 [%Q{"\r\n,",}, ["\r\n,", nil]] ].each do |test_case|
75 assert_equal(test_case.first + $/, CSV.generate_line(test_case.last))
80 assert_equal( "a;b;;c\n", CSV.generate_line( ["a", "b", nil, "c"],
82 assert_equal( "a\tb\t\tc\n", CSV.generate_line( ["a", "b", nil, "c"],
87 assert_equal( "a,b,,c\r\n", CSV.generate_line( ["a", "b", nil, "c"],
88 :row_sep => "\r\n" ) )
92 assert_equal( %Q{"1","b","","already ""quoted"""\n},
93 CSV.generate_line( [1, "b", nil, %Q{already "quoted"}],
94 :force_quotes => true ) )