summaryrefslogtreecommitdiff
path: root/test/csv/interface/test_write.rb
blob: 8650ecd624a2343f036f41b6e6de3d111441c12c (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# frozen_string_literal: false

require_relative "../helper"

class TestCSVInterfaceWrite < Test::Unit::TestCase
  extend DifferentOFS

  def setup
    super
    @output = Tempfile.new(["interface-write", ".csv"])
  end

  def teardown
    @output.close(true)
    super
  end

  def test_generate_default
    csv_text = CSV.generate do |csv|
      csv << [1, 2, 3] << [4, nil, 5]
    end
    assert_equal(<<-CSV, csv_text)
1,2,3
4,,5
    CSV
  end

  def test_generate_append
    csv_text = <<-CSV
1,2,3
4,,5
    CSV
    CSV.generate(csv_text) do |csv|
      csv << ["last", %Q{"row"}]
    end
    assert_equal(<<-CSV, csv_text)
1,2,3
4,,5
last,"""row"""
    CSV
  end

  def test_generate_no_new_line
    csv_text = CSV.generate("test") do |csv|
      csv << ["row"]
    end
    assert_equal(<<-CSV, csv_text)
testrow
    CSV
  end

  def test_generate_line_col_sep
    line = CSV.generate_line(["1", "2", "3"], col_sep: ";")
    assert_equal(<<-LINE, line)
1;2;3
    LINE
  end

  def test_generate_line_row_sep
    line = CSV.generate_line(["1", "2"], row_sep: nil)
    assert_equal(<<-LINE.chomp, line)
1,2
    LINE
  end

  def test_generate_line_shortcut
    line = ["1", "2", "3"].to_csv(col_sep: ";")
    assert_equal(<<-LINE, line)
1;2;3
    LINE
  end

  def test_headers_detection
    headers = ["a", "b", "c"]
    CSV.open(@output.path, "w", headers: true) do |csv|
      csv << headers
      csv << ["1", "2", "3"]
      assert_equal(headers, csv.headers)
    end
  end

  def test_lineno
    CSV.open(@output.path, "w") do |csv|
      n_lines = 20
      n_lines.times do
        csv << ["a", "b", "c"]
      end
      assert_equal(n_lines, csv.lineno)
    end
  end

  def test_append_row
    CSV.open(@output.path, "wb") do |csv|
      csv <<
        CSV::Row.new([], ["1", "2", "3"]) <<
        CSV::Row.new([], ["a", "b", "c"])
    end
    assert_equal(<<-CSV, File.read(@output.path, mode: "rb"))
1,2,3
a,b,c
    CSV
  end

  def test_append_hash
    CSV.open(@output.path, "wb", headers: true) do |csv|
      csv << [:a, :b, :c]
      csv << {a: 1, b: 2, c: 3}
      csv << {a: 4, b: 5, c: 6}
    end
    assert_equal(<<-CSV, File.read(@output.path, mode: "rb"))
a,b,c
1,2,3
4,5,6
    CSV
  end

  def test_append_hash_headers_array
    CSV.open(@output.path, "wb", headers: [:b, :a, :c]) do |csv|
      csv << {a: 1, b: 2, c: 3}
      csv << {a: 4, b: 5, c: 6}
    end
    assert_equal(<<-CSV, File.read(@output.path, mode: "rb"))
2,1,3
5,4,6
    CSV
  end

  def test_append_hash_headers_string
    CSV.open(@output.path, "wb", headers: "b|a|c", col_sep: "|") do |csv|
      csv << {"a" => 1, "b" => 2, "c" => 3}
      csv << {"a" => 4, "b" => 5, "c" => 6}
    end
    assert_equal(<<-CSV, File.read(@output.path, mode: "rb"))
2|1|3
5|4|6
    CSV
  end

  def test_write_headers
    CSV.open(@output.path,
             "wb",
             headers:       "b|a|c",
             write_headers: true,
             col_sep:       "|" ) do |csv|
      csv << {"a" => 1, "b" => 2, "c" => 3}
      csv << {"a" => 4, "b" => 5, "c" => 6}
    end
    assert_equal(<<-CSV, File.read(@output.path, mode: "rb"))
b|a|c
2|1|3
5|4|6
    CSV
  end

  def test_write_headers_empty
    CSV.open(@output.path,
             "wb",
             headers:       "b|a|c",
             write_headers: true,
             col_sep:       "|" ) do |csv|
    end
    assert_equal(<<-CSV, File.read(@output.path, mode: "rb"))
b|a|c
    CSV
  end

  def test_options_not_modified
    options = {}.freeze
    CSV.generate(**options) {}
    CSV.generate_line([], **options)
    CSV.filter("", "", **options)
    CSV.instance("", **options)
  end
end