Skip to content

Commit

Permalink
Showing 2 changed files with 23 additions and 0 deletions.
21 changes: 21 additions & 0 deletions spec/std/io/sized_spec.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
require "spec"

private class NoPeekIO
include IO

def read(bytes : Bytes)
0
end

def write(bytes : Bytes)
0
end

def peek
raise "shouldn't be invoked"
end
end

describe "IO::Sized" do
describe "#read" do
it "doesn't read past the limit when reading char-by-char" do
@@ -117,6 +133,11 @@ describe "IO::Sized" do
sized.peek.should eq(Bytes.empty)
end

it "doesn't peek when remaining = 0 (#4261)" do
sized = IO::Sized.new(NoPeekIO.new, read_size: 0)
sized.peek.should eq(Bytes.empty)
end

it "skips" do
io = IO::Memory.new "123456789"
sized = IO::Sized.new(io, read_size: 6)
2 changes: 2 additions & 0 deletions src/io/sized.cr
Original file line number Diff line number Diff line change
@@ -52,6 +52,8 @@ module IO
def peek
check_open

return Bytes.empty if @read_remaining == 0 # EOF

peek = @io.peek
return nil unless peek

0 comments on commit 3eef15a

Please sign in to comment.