Skip to content

Commit 1c41811

Browse files
committed
Add specs for StringIO#{each,each_line} when passed separator and limit parameters
1 parent 216ba23 commit 1c41811

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed

spec/ruby/library/stringio/each_line_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@
2121
describe "StringIO#each_line when passed limit" do
2222
it_behaves_like :stringio_each_limit, :each_line
2323
end
24+
25+
describe "StringIO#each when passed separator and limit" do
26+
it_behaves_like :stringio_each_separator_and_limit, :each_line
27+
end

spec/ruby/library/stringio/each_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@
2525
describe "StringIO#each when passed limit" do
2626
it_behaves_like :stringio_each_limit, :each
2727
end
28+
29+
describe "StringIO#each when passed separator and limit" do
30+
it_behaves_like :stringio_each_separator_and_limit, :each
31+
end

spec/ruby/library/stringio/shared/each.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,60 @@
150150
seen.should == ["a b ", "c d ", "e\n", "1 2 ", "3 4 ", "5"]
151151
end
152152
end
153+
154+
describe :stringio_each_separator_and_limit, shared: true do
155+
before :each do
156+
@io = StringIO.new("this>is>an>example")
157+
end
158+
159+
it "returns the data read until the limit is consumed or the separator is met" do
160+
@io.send(@method, '>', 8) { |s| break s }.should == "this>"
161+
@io.send(@method, '>', 2) { |s| break s }.should == "is"
162+
@io.send(@method, '>', 10) { |s| break s }.should == ">"
163+
@io.send(@method, '>', 6) { |s| break s }.should == "an>"
164+
@io.send(@method, '>', 5) { |s| break s }.should == "examp"
165+
end
166+
167+
it "truncates the multi-character separator at the end to meet the limit" do
168+
@io.send(@method, "is>an", 7) { |s| break s }.should == "this>is"
169+
end
170+
171+
it "does not change $_" do
172+
$_ = "test"
173+
@io.send(@method, '>', 8) { |s| s }
174+
$_.should == "test"
175+
end
176+
177+
it "updates self's lineno by one" do
178+
@io.send(@method, '>', 3) { |s| break s }
179+
@io.lineno.should eql(1)
180+
181+
@io.send(@method, '>', 3) { |s| break s }
182+
@io.lineno.should eql(2)
183+
184+
@io.send(@method, '>', 3) { |s| break s }
185+
@io.lineno.should eql(3)
186+
end
187+
188+
it "tries to convert the passed separator to a String using #to_str" do # TODO
189+
obj = mock('to_str')
190+
obj.should_receive(:to_str).and_return('>')
191+
192+
seen = []
193+
@io.send(@method, obj, 5) { |s| seen << s }
194+
seen.should == ["this>", "is>", "an>", "examp", "le"]
195+
end
196+
197+
it "does not raise TypeError if passed separator is nil" do
198+
@io.send(@method, nil, 5) { |s| break s }.should == "this>"
199+
end
200+
201+
it "tries to convert the passed limit to an Integer using #to_int" do # TODO
202+
obj = mock('to_int')
203+
obj.should_receive(:to_int).and_return(5)
204+
205+
seen = []
206+
@io.send(@method, '>', obj) { |s| seen << s }
207+
seen.should == ["this>", "is>", "an>", "examp", "le"]
208+
end
209+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
fails:StringIO#each_line when passed a separator yields each paragraph with all separation characters when passed an empty String as separator
2+
fails:StringIO#each when passed separator and limit tries to convert the passed separator to a String using #to_str
3+
fails:StringIO#each when passed separator and limit tries to convert the passed limit to an Integer using #to_int
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
fails:StringIO#each when passed a separator yields each paragraph with all separation characters when passed an empty String as separator
2+
fails:StringIO#each when passed separator and limit tries to convert the passed separator to a String using #to_str
3+
fails:StringIO#each when passed separator and limit tries to convert the passed limit to an Integer using #to_int

0 commit comments

Comments
 (0)