-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use pipe implemenation for infinite IO
- Loading branch information
1 parent
d9a6bc8
commit 3ddccac
Showing
4 changed files
with
98 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
require 'spec_helper' | ||
|
||
|
||
|
||
RSpec.describe "Piperator.infinite_io" do | ||
repeated_string = "foobar\n" | ||
infinite_foobar = Enumerator.new { |y| loop { y << repeated_string } } | ||
|
||
describe '#read' do | ||
subject { proc { |&b| Piperator.infinite_io(infinite_foobar) { |x| b.(x) } } } | ||
|
||
it 'reads specific number of bytes' do | ||
subject.call do |io| | ||
expect(io.read(4)).to eq('foob') | ||
end | ||
end | ||
|
||
it 'buffers rest and returns on next read' do | ||
subject.call do |io| | ||
expect(io.read(2)).to eq('fo') | ||
expect(io.read(2)).to eq('ob') | ||
expect(io.read(2)).to eq('ar') | ||
end | ||
end | ||
|
||
it 'does not try to reach the end before working' do | ||
n = 100000 | ||
subject.call do |io| | ||
expect(io.read(n * repeated_string.length)).to eq(repeated_string * n) | ||
end | ||
end | ||
end | ||
|
||
describe '#gets' do | ||
it 'returns characters until the separator' do | ||
Piperator.infinite_io(infinite_foobar) do |io| | ||
expect(io.gets).to eq("foobar\n") | ||
end | ||
end | ||
|
||
it 'responds to gets with nil when enumerable is exhausted' do | ||
n = 2 | ||
Piperator.infinite_io((["foobar\n"] * n).each) do |io| | ||
n.times { expect(io.gets).to eq("foobar\n") } | ||
expect(io.gets).to be_nil | ||
end | ||
end | ||
end | ||
|
||
describe '#eof?' do | ||
it 'returns eof when enumerable is exhausted' do | ||
n = 2 | ||
Piperator.infinite_io((["foobar\n"] * n).each) do |io| | ||
expect(io.eof?).to be_falsey | ||
n.times { expect(io.gets).to eq("foobar\n") } | ||
expect(io.eof?).to be_truthy | ||
end | ||
end | ||
end | ||
|
||
describe "#each_line" do | ||
it 'reevaluates line breaks' do | ||
Piperator.infinite_io(["foo\n", "bar\n", "baz\nbmp"].lazy.each) do |io| | ||
expect(io.each_line.map(&:strip)).to eq(["foo", "bar", "baz", "bmp"]) | ||
end | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters