summaryrefslogtreecommitdiff
path: root/spec/ruby/core/io/shared/each.rb
blob: 607e7de03e86a4b48bbc2299a1711718fe0d8526 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# -*- encoding: utf-8 -*-
require_relative '../fixtures/classes'

describe :io_each, shared: true do
  before :each do
    @io = IOSpecs.io_fixture "lines.txt"
    ScratchPad.record []
  end

  after :each do
    @io.close if @io
  end

  describe "with no separator" do
    it "yields each line to the passed block" do
      @io.send(@method) { |s| ScratchPad << s }
      ScratchPad.recorded.should == IOSpecs.lines
    end

    it "yields each line starting from the current position" do
      @io.pos = 41
      @io.send(@method) { |s| ScratchPad << s }
      ScratchPad.recorded.should == IOSpecs.lines[2..-1]
    end

    it "returns self" do
      @io.send(@method) { |l| l }.should equal(@io)
    end

    it "does not change $_" do
      $_ = "test"
      @io.send(@method) { |s| s }
      $_.should == "test"
    end

    it "returns self" do
      @io.send(@method) { |l| l }.should equal(@io)
    end

    it "raises an IOError when self is not readable" do
      -> { IOSpecs.closed_io.send(@method) {} }.should raise_error(IOError)
    end

    it "makes line count accessible via lineno" do
      @io.send(@method) { ScratchPad << @io.lineno }
      ScratchPad.recorded.should == [ 1,2,3,4,5,6,7,8,9 ]
    end

    it "makes line count accessible via $." do
      @io.send(@method) { ScratchPad << $. }
      ScratchPad.recorded.should == [ 1,2,3,4,5,6,7,8,9 ]
    end

    describe "when no block is given" do
      it "returns an Enumerator" do
        enum = @io.send(@method)
        enum.should be_an_instance_of(Enumerator)

        enum.each { |l| ScratchPad << l }
        ScratchPad.recorded.should == IOSpecs.lines
      end

      describe "returned Enumerator" do
        describe "size" do
          it "should return nil" do
            @io.send(@method).size.should == nil
          end
        end
      end
    end
  end

  describe "with limit" do
    describe "when limit is 0" do
      it "raises an ArgumentError" do
        # must pass block so Enumerator is evaluated and raises
        -> { @io.send(@method, 0){} }.should raise_error(ArgumentError)
      end
    end
  end

  describe "when passed a String containing one space as a separator" do
    it "uses the passed argument as the line separator" do
      @io.send(@method, " ") { |s| ScratchPad << s }
      ScratchPad.recorded.should == IOSpecs.lines_space_separator
    end

    it "does not change $_" do
      $_ = "test"
      @io.send(@method, " ") { |s| }
      $_.should == "test"
    end

    it "tries to convert the passed separator to a String using #to_str" do
      obj = mock("to_str")
      obj.stub!(:to_str).and_return(" ")

      @io.send(@method, obj) { |l| ScratchPad << l }
      ScratchPad.recorded.should == IOSpecs.lines_space_separator
    end
  end

  describe "when passed nil as a separator" do
    it "yields self's content starting from the current position when the passed separator is nil" do
      @io.pos = 100
      @io.send(@method, nil) { |s| ScratchPad << s }
      ScratchPad.recorded.should == ["qui a linha cinco.\nHere is line six.\n"]
    end
  end

  describe "when passed an empty String as a separator" do
    it "yields each paragraph" do
      @io.send(@method, "") { |s| ScratchPad << s }
      ScratchPad.recorded.should == IOSpecs.paragraphs
    end
  end

  describe "with both separator and limit" do
    describe "when no block is given" do
      it "returns an Enumerator" do
        enum = @io.send(@method, nil, 1024)
        enum.should be_an_instance_of(Enumerator)

        enum.each { |l| ScratchPad << l }
        ScratchPad.recorded.should == [IOSpecs.lines.join]
      end

      describe "returned Enumerator" do
        describe "size" do
          it "should return nil" do
            @io.send(@method, nil, 1024).size.should == nil
          end
        end
      end
    end

    describe "when a block is given" do
      it "accepts an empty block" do
        @io.send(@method, nil, 1024) {}.should equal(@io)
      end

      describe "when passed nil as a separator" do
        it "yields self's content starting from the current position when the passed separator is nil" do
          @io.pos = 100
          @io.send(@method, nil, 1024) { |s| ScratchPad << s }
          ScratchPad.recorded.should == ["qui a linha cinco.\nHere is line six.\n"]
        end
      end

      describe "when passed an empty String as a separator" do
        it "yields each paragraph" do
          @io.send(@method, "", 1024) { |s| ScratchPad << s }
          ScratchPad.recorded.should == IOSpecs.paragraphs
        end
      end
    end
  end

  describe "when passed chomp" do
    it "yields each line without trailing newline characters to the passed block" do
      @io.send(@method, chomp: true) { |s| ScratchPad << s }
      ScratchPad.recorded.should == IOSpecs.lines_without_newline_characters
    end

    ruby_version_is "3.0" do
      it "raises exception when options passed as Hash" do
        -> {
          @io.send(@method, { chomp: true }) { |s| }
        }.should raise_error(TypeError)

        -> {
          @io.send(@method, "\n", 1, { chomp: true }) { |s| }
        }.should raise_error(ArgumentError, "wrong number of arguments (given 3, expected 0..2)")
      end
    end
  end

  describe "when passed chomp and a separator" do
    it "yields each line without separator to the passed block" do
      @io.send(@method, " ", chomp: true) { |s| ScratchPad << s }
      ScratchPad.recorded.should == IOSpecs.lines_space_separator_without_trailing_spaces
    end
  end

  describe "when passed chomp and empty line as a separator" do
    it "yields each paragraph without trailing new line characters" do
      @io.send(@method, "", 1024, chomp: true) { |s| ScratchPad << s }
      ScratchPad.recorded.should == IOSpecs.paragraphs_without_trailing_new_line_characters
    end
  end

  describe "when passed chomp and nil as a separator" do
    ruby_version_is "3.2" do
      it "yields self's content" do
        @io.pos = 100
        @io.send(@method, nil, chomp: true) { |s| ScratchPad << s }
        ScratchPad.recorded.should == ["qui a linha cinco.\nHere is line six.\n"]
      end
    end

    ruby_version_is ""..."3.2" do
      it "yields self's content without trailing new line character" do
        @io.pos = 100
        @io.send(@method, nil, chomp: true) { |s| ScratchPad << s }
        ScratchPad.recorded.should == ["qui a linha cinco.\nHere is line six."]
      end
    end
  end

  describe "when passed chomp, nil as a separator, and a limit" do
    it "yields each line of limit size without truncating trailing new line character" do
      # 43 - is a size of the 1st paragraph in the file
      @io.send(@method, nil, 43, chomp: true) { |s| ScratchPad << s }

      ScratchPad.recorded.should == [
        "Voici la ligne une.\nQui è la linea due.\n\n\n",
        "Aquí está la línea tres.\n" + "Hier ist Zeile ",
        "vier.\n\nEstá aqui a linha cinco.\nHere is li",
        "ne six.\n"
      ]
    end
  end
end

describe :io_each_default_separator, shared: true do
  before :each do
    @io = IOSpecs.io_fixture "lines.txt"
    ScratchPad.record []
    suppress_warning {@sep, $/ = $/, " "}
  end

  after :each do
    @io.close if @io
    suppress_warning {$/ = @sep}
  end

  it "uses $/ as the default line separator" do
    @io.send(@method) { |s| ScratchPad << s }
    ScratchPad.recorded.should == IOSpecs.lines_space_separator
  end
end