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
|
require "spec"
private class NoPeekIO < IO
def read(slice : Bytes)
0
end
def write(slice : Bytes) : Nil
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
io = IO::Memory.new "abcdefg"
sized = IO::Sized.new(io, read_size: 5)
sized.read_char.should eq('a')
sized.read_char.should eq('b')
sized.read_char.should eq('c')
sized.read_remaining.should eq(2)
sized.read_char.should eq('d')
sized.read_char.should eq('e')
sized.read_remaining.should eq(0)
sized.read_char.should be_nil
sized.read_remaining.should eq(0)
sized.read_char.should be_nil
end
it "doesn't read past the limit when reading the correct size" do
io = IO::Memory.new("1234567")
sized = IO::Sized.new(io, read_size: 5)
slice = Bytes.new(5)
sized.read(slice).should eq(5)
String.new(slice).should eq("12345")
sized.read(slice).should eq(0)
String.new(slice).should eq("12345")
end
it "reads partially when supplied with a larger slice" do
io = IO::Memory.new("1234567")
sized = IO::Sized.new(io, read_size: 5)
slice = Bytes.new(10)
sized.read(slice).should eq(5)
String.new(slice).should eq("12345\0\0\0\0\0")
end
it "allows extending the size" do
io = IO::Memory.new("1234567890")
sized = IO::Sized.new(io, read_size: 5)
slice = Bytes.new(10)
sized.read(slice).should eq(5)
String.new(slice).should eq("12345\0\0\0\0\0")
sized.read_remaining = 5
sized.read(slice).should eq(5)
String.new(slice).should eq("67890\0\0\0\0\0")
end
it "raises on negative numbers" do
io = IO::Memory.new
expect_raises(ArgumentError, "Negative read_size") do
IO::Sized.new(io, read_size: -1)
end
end
end
describe "#write" do
it "raises" do
sized = IO::Sized.new(IO::Memory.new, read_size: 5)
expect_raises(IO::Error, "Can't write to IO::Sized") do
sized.puts "test string"
end
end
end
describe "#close" do
it "stops reading" do
io = IO::Memory.new "abcdefg"
sized = IO::Sized.new(io, read_size: 5)
sized.read_char.should eq('a')
sized.read_char.should eq('b')
sized.close
sized.closed?.should eq(true)
expect_raises(IO::Error, "Closed stream") do
sized.read_char
end
end
it "closes the underlying stream if sync_close is true" do
io = IO::Memory.new "abcdefg"
sized = IO::Sized.new(io, read_size: 5, sync_close: true)
sized.sync_close?.should eq(true)
io.closed?.should eq(false)
sized.close
io.closed?.should eq(true)
end
end
it "read_byte" do
io = IO::Memory.new "abcdefg"
sized = IO::Sized.new(io, read_size: 3)
sized.read_byte.should eq('a'.ord)
sized.read_byte.should eq('b'.ord)
sized.read_byte.should eq('c'.ord)
sized.read_byte.should be_nil
end
it "gets" do
io = IO::Memory.new "foo\nbar\nbaz"
sized = IO::Sized.new(io, read_size: 9)
sized.gets.should eq("foo")
sized.gets.should eq("bar")
sized.gets.should eq("b")
sized.gets.should be_nil
end
it "gets with chomp = false" do
io = IO::Memory.new "foo\nbar\nbaz"
sized = IO::Sized.new(io, read_size: 9)
sized.gets(chomp: false).should eq("foo\n")
sized.gets(chomp: false).should eq("bar\n")
sized.gets(chomp: false).should eq("b")
sized.gets(chomp: false).should be_nil
end
it "peeks" do
io = IO::Memory.new "123456789"
sized = IO::Sized.new(io, read_size: 6)
sized.peek.should eq("123456".to_slice)
sized.gets_to_end.should eq("123456")
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)
sized.skip(3)
sized.read_char.should eq('4')
expect_raises(IO::EOFError) do
sized.skip(6)
end
end
end
|