summaryrefslogtreecommitdiff
path: root/test/csv/interface/test_read_write.rb
blob: 877e5f355ea30ff13f3beea48658774f68ffa00f (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
# frozen_string_literal: false

require_relative "../helper"

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

  def test_filter
    input = <<-CSV
1;2;3
4;5
    CSV
    output = ""
    CSV.filter(input, output,
               in_col_sep: ";",
               out_col_sep: ",",
               converters: :all) do |row|
      row.map! {|n| n * 2}
      row << "Added\r"
    end
    assert_equal(<<-CSV, output)
2,4,6,"Added\r"
8,10,"Added\r"
    CSV
  end

  def test_instance_same
    data = ""
    assert_equal(CSV.instance(data, col_sep: ";").object_id,
                 CSV.instance(data, col_sep: ";").object_id)
  end

  def test_instance_append
    output = ""
    CSV.instance(output, col_sep: ";") << ["a", "b", "c"]
    assert_equal(<<-CSV, output)
a;b;c
    CSV
    CSV.instance(output, col_sep: ";") << [1, 2, 3]
    assert_equal(<<-CSV, output)
a;b;c
1;2;3
    CSV
  end

  def test_instance_shortcut
    assert_equal(CSV.instance,
                 CSV {|csv| csv})
  end
end