summaryrefslogtreecommitdiff
path: root/test/csv/parse/test_skip_lines.rb
blob: 196858f1b077e98e8247fd0ecc97e0d9070b7a78 (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
# frozen_string_literal: false

require_relative "../helper"

class TestCSVParseSkipLines < Test::Unit::TestCase
  extend DifferentOFS
  include Helper

  def test_default
    csv = CSV.new("a,b,c\n")
    assert_nil(csv.skip_lines)
  end

  def test_regexp
    csv = <<-CSV
1
#2
 #3
4
    CSV
    assert_equal([
                   ["1"],
                   ["4"],
                 ],
                 CSV.parse(csv, :skip_lines => /\A\s*#/))
  end

  def test_regexp_quoted
    csv = <<-CSV
1
#2
"#3"
4
    CSV
    assert_equal([
                   ["1"],
                   ["#3"],
                   ["4"],
                 ],
                 CSV.parse(csv, :skip_lines => /\A\s*#/))
  end

  def test_string
    csv = <<-CSV
1
.2
3.
4
    CSV
    assert_equal([
                   ["1"],
                   ["4"],
                 ],
                 CSV.parse(csv, :skip_lines => "."))
  end

  class RegexStub
  end

  def test_not_matchable
    regex_stub = RegexStub.new
    csv = CSV.new("1\n", :skip_lines => regex_stub)
    error = assert_raise(ArgumentError) do
      csv.shift
    end
    assert_equal(":skip_lines has to respond to #match: #{regex_stub.inspect}",
                 error.message)
  end

  class Matchable
    def initialize(pattern)
      @pattern = pattern
    end

    def match(line)
      @pattern.match(line)
    end
  end

  def test_matchable
    csv = <<-CSV
1
# 2
3
# 4
    CSV
    assert_equal([
                   ["1"],
                   ["3"],
                 ],
                 CSV.parse(csv, :skip_lines => Matchable.new(/\A#/)))
  end

  def test_multibyte_data
    # U+3042 HIRAGANA LETTER A
    # U+3044 HIRAGANA LETTER I
    # U+3046 HIRAGANA LETTER U
    value = "\u3042\u3044\u3046"
    with_chunk_size("5") do
      assert_equal([[value], [value]],
                   CSV.parse("#{value}\n#{value}\n",
                             :skip_lines => /\A#/))
    end
  end
end