summaryrefslogtreecommitdiff
path: root/test/csv/test_csv_writing.rb
blob: 704c1d7ddc32f933012bb43c4bdae66f18184488 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env ruby -w
# encoding: UTF-8

# tc_csv_writing.rb
#
#  Created by James Edward Gray II on 2005-10-31.
#  Copyright 2005 James Edward Gray II. You can redistribute or modify this code
#  under the terms of Ruby's license.

require_relative "base"

class TestCSV::Writing < TestCSV
  extend DifferentOFS

  def test_writing
    [ ["\t",                      ["\t"]],
      ["foo,\"\"\"\"\"\",baz",    ["foo", "\"\"", "baz"]],
      ["foo,\"\"\"bar\"\"\",baz", ["foo", "\"bar\"", "baz"]],
      ["\"\"\"\n\",\"\"\"\n\"",   ["\"\n", "\"\n"]],
      ["foo,\"\r\n\",baz",        ["foo", "\r\n", "baz"]],
      ["\"\"",                    [""]],
      ["foo,\"\"\"\",baz",        ["foo", "\"", "baz"]],
      ["foo,\"\r.\n\",baz",       ["foo", "\r.\n", "baz"]],
      ["foo,\"\r\",baz",          ["foo", "\r", "baz"]],
      ["foo,\"\",baz",            ["foo", "", "baz"]],
      ["\",\"",                   [","]],
      ["foo",                     ["foo"]],
      [",,",                      [nil, nil, nil]],
      [",",                       [nil, nil]],
      ["foo,\"\n\",baz",          ["foo", "\n", "baz"]],
      ["foo,,baz",                ["foo", nil, "baz"]],
      ["\"\"\"\r\",\"\"\"\r\"",   ["\"\r", "\"\r"]],
      ["\",\",\",\"",             [",", ","]],
      ["foo,bar,",                ["foo", "bar", nil]],
      [",foo,bar",                [nil, "foo", "bar"]],
      ["foo,bar",                 ["foo", "bar"]],
      [";",                       [";"]],
      ["\t,\t",                   ["\t", "\t"]],
      ["foo,\"\r\n\r\",baz",      ["foo", "\r\n\r", "baz"]],
      ["foo,\"\r\n\n\",baz",      ["foo", "\r\n\n", "baz"]],
      ["foo,\"foo,bar\",baz",     ["foo", "foo,bar", "baz"]],
      [";,;",                     [";", ";"]],
      ["foo,\"\"\"\"\"\",baz",    ["foo", "\"\"", "baz"]],
      ["foo,\"\"\"bar\"\"\",baz", ["foo", "\"bar\"", "baz"]],
      ["foo,\"\r\n\",baz",        ["foo", "\r\n", "baz"]],
      ["\"\"",                    [""]],
      ["foo,\"\"\"\",baz",        ["foo", "\"", "baz"]],
      ["foo,\"\r.\n\",baz",       ["foo", "\r.\n", "baz"]],
      ["foo,\"\r\",baz",          ["foo", "\r", "baz"]],
      ["foo,\"\",baz",            ["foo", "", "baz"]],
      ["foo",                     ["foo"]],
      [",,",                      [nil, nil, nil]],
      [",",                       [nil, nil]],
      ["foo,\"\n\",baz",          ["foo", "\n", "baz"]],
      ["foo,,baz",                ["foo", nil, "baz"]],
      ["foo,bar",                 ["foo", "bar"]],
      ["foo,\"\r\n\n\",baz",      ["foo", "\r\n\n", "baz"]],
      ["foo,\"foo,bar\",baz",     ["foo", "foo,bar", "baz"]],
      [%Q{a,b},                   ["a", "b"]],
      [%Q{a,"""b"""},             ["a", "\"b\""]],
      [%Q{a,"""b"},               ["a", "\"b"]],
      [%Q{a,"b"""},               ["a", "b\""]],
      [%Q{a,"\nb"""},             ["a", "\nb\""]],
      [%Q{a,"""\nb"},             ["a", "\"\nb"]],
      [%Q{a,"""\nb\n"""},         ["a", "\"\nb\n\""]],
      [%Q{a,"""\nb\n""",},        ["a", "\"\nb\n\"", nil]],
      [%Q{a,,,},                  ["a", nil, nil, nil]],
      [%Q{,},                     [nil, nil]],
      [%Q{"",""},                 ["", ""]],
      [%Q{""""},                  ["\""]],
      [%Q{"""",""},               ["\"",""]],
      [%Q{,""},                   [nil,""]],
      [%Q{,"\r"},                 [nil,"\r"]],
      [%Q{"\r\n,"},               ["\r\n,"]],
      [%Q{"\r\n,",},              ["\r\n,", nil]] ].each do |test_case|
        assert_equal(test_case.first + $/, CSV.generate_line(test_case.last))
      end
  end

  def test_col_sep
    assert_equal( "a;b;;c\n", CSV.generate_line( ["a", "b", nil, "c"],
                                                 col_sep: ";" ) )
    assert_equal( "a\tb\t\tc\n", CSV.generate_line( ["a", "b", nil, "c"],
                                                    col_sep: "\t" ) )
  end

  def test_row_sep
    assert_equal( "a,b,,c\r\n", CSV.generate_line( ["a", "b", nil, "c"],
                                                   row_sep: "\r\n" ) )
  end

  def test_force_quotes
    assert_equal( %Q{"1","b","","already ""quoted"""\n},
                  CSV.generate_line( [1, "b", nil, %Q{already "quoted"}],
                                     force_quotes: true ) )
  end
end