summaryrefslogtreecommitdiff
path: root/test/csv/parse/test_quote_char_nil.rb
blob: fc3b646759bffbf4ae70b66075420d02d97a6fe1 (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
# -*- coding: utf-8 -*-
# frozen_string_literal: false

require_relative "../helper"

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

  def test_full
    assert_equal(["a", "b"], CSV.parse_line(%Q{a,b}, quote_char: nil))
  end

  def test_end_with_nil
    assert_equal(["a", nil, nil, nil], CSV.parse_line(%Q{a,,,}, quote_char: nil))
  end

  def test_nil_nil
    assert_equal([nil, nil], CSV.parse_line(%Q{,}, quote_char: nil))
  end

  def test_unquoted_value_multiple_characters_col_sep
    data = %q{a<b<=>x}
    assert_equal([[%Q{a<b}, "x"]], CSV.parse(data, col_sep: "<=>", quote_char: nil))
  end

  def test_csv_header_string
    data = <<~DATA
      first,second,third
      A,B,C
      1,2,3
    DATA
    assert_equal(
      CSV::Table.new([
        CSV::Row.new(["my", "new", "headers"], ["first", "second", "third"]),
        CSV::Row.new(["my", "new", "headers"], ["A", "B", "C"]),
        CSV::Row.new(["my", "new", "headers"], ["1", "2", "3"])
      ]),
      CSV.parse(data, headers: "my,new,headers", quote_char: nil)
    )
  end

  def test_comma
    assert_equal([["a", "b", nil, "d"]],
                 CSV.parse("a,b,,d", col_sep: ",", quote_char: nil))
  end

  def test_space
    assert_equal([["a", "b", nil, "d"]],
                 CSV.parse("a b  d", col_sep: " ", quote_char: nil))
  end

  def encode_array(array, encoding)
    array.collect do |element|
      element ? element.encode(encoding) : element
    end
  end

  def test_space_no_ascii
    encoding = Encoding::UTF_16LE
    assert_equal([encode_array(["a", "b", nil, "d"], encoding)],
                 CSV.parse("a b  d".encode(encoding),
                           col_sep: " ".encode(encoding),
                           quote_char: nil))
  end

  def test_multiple_space
    assert_equal([["a b", nil, "d"]],
                 CSV.parse("a b    d", col_sep: "  ", quote_char: nil))
  end

  def test_multiple_characters_leading_empty_fields
    data = <<-CSV
<=><=>A<=>B<=>C
1<=>2<=>3
    CSV
    assert_equal([
                   [nil, nil, "A", "B", "C"],
                   ["1", "2", "3"],
                 ],
                 CSV.parse(data, col_sep: "<=>", quote_char: nil))
  end

  def test_line
    lines = [
      "abc,def\n",
    ]
    csv = CSV.new(lines.join(""), quote_char: nil)
    lines.each do |line|
      csv.shift
      assert_equal(line, csv.line)
    end
  end
end