* process.c (rb_spawn_internal): new function to specify
[ruby-svn.git] / test / csv / tc_csv_writing.rb
blob4677284306fa033987e4c5ec57f8b16d579c0435
1 #!/usr/local/bin/ruby -w
3 # tc_csv_writing.rb
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.
9 require "test/unit"
11 require "csv"
13 class TestCSVWriting < Test::Unit::TestCase
14   def test_writing
15     [ ["\t",                      ["\t"]],
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"]],
20       ["\"\"",                    [""]],
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"]],
25       ["\",\"",                   [","]],
26       ["foo",                     ["foo"]],
27       [",,",                      [nil, nil, nil]],
28       [",",                       [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"]],
36       [";",                       [";"]],
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"]],
41       [";,;",                     [";", ";"]],
42       ["foo,\"\"\"\"\"\",baz",    ["foo", "\"\"", "baz"]],
43       ["foo,\"\"\"bar\"\"\",baz", ["foo", "\"bar\"", "baz"]],
44       ["foo,\"\r\n\",baz",        ["foo", "\r\n", "baz"]],
45       ["\"\"",                    [""]],
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"]],
50       ["foo",                     ["foo"]],
51       [",,",                      [nil, nil, nil]],
52       [",",                       [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]],
67       [%Q{,},                     [nil, nil]],
68       [%Q{"",""},                 ["", ""]],
69       [%Q{""""},                  ["\""]],
70       [%Q{"""",""},               ["\"",""]],
71       [%Q{,""},                   [nil,""]],
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))
76       end
77   end
78   
79   def test_col_sep
80     assert_equal( "a;b;;c\n", CSV.generate_line( ["a", "b", nil, "c"],
81                                                  :col_sep => ";" ) )
82     assert_equal( "a\tb\t\tc\n", CSV.generate_line( ["a", "b", nil, "c"],
83                                                     :col_sep => "\t" ) )
84   end
85   
86   def test_row_sep
87     assert_equal( "a,b,,c\r\n", CSV.generate_line( ["a", "b", nil, "c"],
88                                                    :row_sep => "\r\n" ) )
89   end
90   
91   def test_force_quotes
92     assert_equal( %Q{"1","b","","already ""quoted"""\n},
93                   CSV.generate_line( [1, "b", nil, %Q{already "quoted"}],
94                                      :force_quotes => true ) )
95   end
96 end