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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
|
require "spec"
private class PartialReaderIO < IO
@slice : Bytes
def initialize(data : String)
@slice = data.to_slice
end
def read(slice : Bytes)
return 0 if @slice.size == 0
max_read_size = {slice.size, @slice.size}.min
read_size = rand(1..max_read_size)
slice.copy_from(@slice[0, read_size])
@slice += read_size
read_size
end
def write(slice : Bytes) : NoReturn
raise "write"
end
end
private class MemoryIOWithoutPeek < IO::Memory
def peek
nil
end
end
private class MemoryIOWithFixedPeek < IO::Memory
property peek_size = 0
def peek
Slice.new(@buffer + @pos, {@bytesize - @pos, peek_size}.min)
end
end
describe "IO::Delimited" do
describe "#read" do
context "without peeking" do
it "doesn't read past the limit" do
io = MemoryIOWithoutPeek.new("abcderzzrfgzr")
delimited = IO::Delimited.new(io, read_delimiter: "zr")
delimited.gets_to_end.should eq("abcderz")
io.gets_to_end.should eq("fgzr")
end
it "doesn't read past the limit (char-by-char)" do
io = MemoryIOWithoutPeek.new("abcderzzrfg")
delimited = IO::Delimited.new(io, read_delimiter: "zr")
delimited.read_char.should eq('a')
delimited.read_char.should eq('b')
delimited.read_char.should eq('c')
delimited.read_char.should eq('d')
delimited.read_char.should eq('e')
delimited.read_char.should eq('r')
delimited.read_char.should eq('z')
delimited.read_char.should eq(nil)
delimited.read_char.should eq(nil)
delimited.read_char.should eq(nil)
delimited.read_char.should eq(nil)
io.read_char.should eq('f')
io.read_char.should eq('g')
end
it "doesn't clobber active_delimiter_buffer" do
io = MemoryIOWithoutPeek.new("ab12312")
delimited = IO::Delimited.new(io, read_delimiter: "12345")
delimited.gets_to_end.should eq("ab12312")
end
it "handles the delimiter at the start" do
io = MemoryIOWithoutPeek.new("ab12312")
delimited = IO::Delimited.new(io, read_delimiter: "ab1")
delimited.read_char.should eq(nil)
end
it "handles the delimiter at the end" do
io = MemoryIOWithoutPeek.new("ab12312z")
delimited = IO::Delimited.new(io, read_delimiter: "z")
delimited.gets_to_end.should eq("ab12312")
end
it "handles nearly a delimiter at the end" do
io = MemoryIOWithoutPeek.new("ab12312")
delimited = IO::Delimited.new(io, read_delimiter: "122")
delimited.gets_to_end.should eq("ab12312")
end
it "doesn't clobber the buffer on closely-offset partial matches" do
io = MemoryIOWithoutPeek.new("abab1234abcdefgh")
delimited = IO::Delimited.new(io, read_delimiter: "abcdefgh")
delimited.gets_to_end.should eq("abab1234")
end
end
context "with partial read" do
it "handles partial reads" do
io = PartialReaderIO.new("abab1234abcdefgh")
delimited = IO::Delimited.new(io, read_delimiter: "abcdefgh")
delimited.gets_to_end.should eq("abab1234")
end
end
context "with peeking" do
it "returns empty when there's no data" do
io = IO::Memory.new("")
delimited = IO::Delimited.new(io, read_delimiter: "zr")
delimited.peek.should eq("".to_slice)
delimited.gets_to_end.should eq("")
io.gets_to_end.should eq("")
end
it "doesn't read past the limit" do
io = IO::Memory.new("abcderzzrfgzr")
delimited = IO::Delimited.new(io, read_delimiter: "zr")
delimited.peek.should eq("abcderz".to_slice)
delimited.gets_to_end.should eq("abcderz")
io.gets_to_end.should eq("fgzr")
end
it "doesn't read past the limit, single byte" do
io = IO::Memory.new("abcderzzrfgzr")
delimited = IO::Delimited.new(io, read_delimiter: "f")
delimited.peek.should eq("abcderzzr".to_slice)
delimited.gets_to_end.should eq("abcderzzr")
io.gets_to_end.should eq("gzr")
end
it "doesn't read past the limit (char-by-char)" do
io = IO::Memory.new("abcderzzrfg")
delimited = IO::Delimited.new(io, read_delimiter: "zr")
delimited.read_char.should eq('a')
delimited.read_char.should eq('b')
delimited.read_char.should eq('c')
delimited.read_char.should eq('d')
delimited.read_char.should eq('e')
delimited.read_char.should eq('r')
delimited.read_char.should eq('z')
delimited.read_char.should eq(nil)
delimited.read_char.should eq(nil)
delimited.read_char.should eq(nil)
delimited.read_char.should eq(nil)
io.read_char.should eq('f')
io.read_char.should eq('g')
end
it "doesn't clobber active_delimiter_buffer" do
io = IO::Memory.new("ab12312")
delimited = IO::Delimited.new(io, read_delimiter: "12345")
delimited.peek.should eq("ab123".to_slice)
delimited.gets_to_end.should eq("ab12312")
end
it "handles the delimiter at the start" do
io = IO::Memory.new("ab12312")
delimited = IO::Delimited.new(io, read_delimiter: "ab1")
delimited.peek.should eq(Bytes.empty)
delimited.read_char.should eq(nil)
end
it "handles the delimiter at the end" do
io = IO::Memory.new("ab12312z")
delimited = IO::Delimited.new(io, read_delimiter: "z")
delimited.peek.should eq("ab12312".to_slice)
delimited.gets_to_end.should eq("ab12312")
end
it "handles nearly a delimiter at the end" do
io = IO::Memory.new("ab12312")
delimited = IO::Delimited.new(io, read_delimiter: "122")
delimited.peek.should eq("ab123".to_slice)
delimited.gets_to_end.should eq("ab12312")
end
it "doesn't clobber the buffer on closely-offset partial matches" do
io = IO::Memory.new("abab1234abcdefgh")
delimited = IO::Delimited.new(io, read_delimiter: "abcdefgh")
delimited.peek.should eq("abab1234".to_slice)
delimited.gets_to_end.should eq("abab1234")
end
it "handles the case of peek matching first byte, not having enough room, but rest not matching" do
# not a delimiter
# ---
io = MemoryIOWithFixedPeek.new("abcdefwhi")
# -------
# peek
io.peek_size = 7
delimited = IO::Delimited.new(io, read_delimiter: "fgh")
delimited.peek.should eq("abcde".to_slice)
delimited.gets_to_end.should eq("abcdefwhi")
delimited.gets_to_end.should eq("")
io.gets_to_end.should eq("")
end
it "handles the case of peek matching first byte, not having enough room, but rest not immediately matching (with a potential match afterwards)" do
# not a delimiter, but the second 'f' will actually match the delimiter
# ---
io = MemoryIOWithFixedPeek.new("abcdeffghijk")
# -------
# peek
io.peek_size = 7
delimited = IO::Delimited.new(io, read_delimiter: "fgh")
delimited.peek.should eq("abcde".to_slice)
delimited.gets_to_end.should eq("abcdef")
delimited.gets_to_end.should eq("")
io.gets_to_end.should eq("ijk")
end
it "handles the case of peek matching first byte, not having enough room, but later matching" do
# delimiter
# ---
io = MemoryIOWithFixedPeek.new("abcdefghijk")
# -------
# peek
io.peek_size = 7
delimited = IO::Delimited.new(io, read_delimiter: "fgh")
delimited.peek.should eq("abcde".to_slice)
delimited.gets_to_end.should eq("abcde")
delimited.gets_to_end.should eq("")
io.gets_to_end.should eq("ijk")
end
it "handles the case of peek matching first byte, not having enough room, but later not matching" do
# not a delimiter
# ---
io = MemoryIOWithFixedPeek.new("abcdefgwijkfghhello")
# ------- ---
# peek delimiter
io.peek_size = 7
delimited = IO::Delimited.new(io, read_delimiter: "fgh")
delimited.peek.should eq("abcde".to_slice)
delimited.gets_to_end.should eq("abcdefgwijk")
delimited.gets_to_end.should eq("")
io.gets_to_end.should eq("hello")
end
it "handles the case of peek matching first byte, not having enough room, but later not matching (limted slice)" do
# not a delimiter
# ---
io = MemoryIOWithFixedPeek.new("abcdefgwijkfghhello")
# ------- ---
# peek delimiter
io.peek_size = 7
delimited = IO::Delimited.new(io, read_delimiter: "fgh")
delimited.peek.should eq("abcde".to_slice)
delimited.read_string(6).should eq "abcdef"
delimited.read_string(5).should eq("gwijk")
delimited.gets_to_end.should eq("")
io.gets_to_end.should eq("hello")
end
it "handles the case of peek matching first byte, not having enough room, later only partially matching" do
# delimiter
# ------------
io = MemoryIOWithFixedPeek.new("abcdefghijklmnopqrst")
# -------~~~~~~~
# peek peek
io.peek_size = 7
delimited = IO::Delimited.new(io, read_delimiter: "fghijklmnopq")
delimited.peek.should eq("abcde".to_slice)
delimited.gets_to_end.should eq("abcde")
delimited.gets_to_end.should eq("")
io.gets_to_end.should eq("rst")
end
it "peeks, everything matches but we can't know what will happen after that" do
io = MemoryIOWithFixedPeek.new("fgh")
io.peek_size = 2
delimited = IO::Delimited.new(io, read_delimiter: "fgh")
delimited.peek.should be_nil
end
it "handles the case of the active delimited buffer including the delimiter" do
# delimiter
# ---
io = MemoryIOWithFixedPeek.new("aaabcde")
# --
# peek
io.peek_size = 2
delimited = IO::Delimited.new(io, read_delimiter: "aab")
delimited.peek.should be_nil
delimited.gets_to_end.should eq("a")
delimited.gets_to_end.should eq("")
io.gets_to_end.should eq("cde")
end
it "can peek if first byte found but doesn't fully match, and there's that first byte again in the peek buffer" do
# delimiter
# -----
io = MemoryIOWithFixedPeek.new("holhhello")
# ----
# peek
io.peek_size = 4
delimited = IO::Delimited.new(io, read_delimiter: "hello")
delimited.peek.should eq("hol".to_slice)
end
it "can peek if first byte found but doesn't fully match, and the byte isn't there in the peek buffer" do
# delimiter
# -----
io = MemoryIOWithFixedPeek.new("holahello")
# ----
# peek
io.peek_size = 4
delimited = IO::Delimited.new(io, read_delimiter: "hello")
delimited.peek.should eq("hola".to_slice)
end
end
end
describe "#gets with peeking when can't peek" do
it "gets" do
io = MemoryIOWithFixedPeek.new("abcdefghel")
io.peek_size = 7
delimited = IO::Delimited.new(io, read_delimiter: "hello")
# We first peek "abcdefg".
# The delimiter's first byte isn't there so we read everything.
# Next we peek "hel". It matches the delimiter's beginning
# but `peek` can't tell whether there's more content or not
# after that, and it can't return an empty buffer because that
# means EOF. So it must return `nil`. So `IO#gets` should
# handle this case where `peek` becomes `nil`.
delimited.gets.should eq("abcdefghel")
end
it "peeks" do
io = MemoryIOWithFixedPeek.new("hel")
io.peek_size = 1
delimited = IO::Delimited.new(io, read_delimiter: "hello")
delimited.read(Bytes.new(1))
delimited.peek.should eq("el".to_slice)
end
end
describe "#write" do
it "raises" do
delimited = IO::Delimited.new(IO::Memory.new, read_delimiter: "zr")
expect_raises(IO::Error, "Can't write to IO::Delimited") do
delimited.puts "test string"
end
end
end
describe "#close" do
it "stops reading" do
io = IO::Memory.new "abcdefg"
delimited = IO::Delimited.new(io, read_delimiter: "zr")
delimited.read_char.should eq('a')
delimited.read_char.should eq('b')
delimited.close
delimited.closed?.should eq(true)
expect_raises(IO::Error, "Closed stream") do
delimited.read_char
end
end
it "closes the underlying stream if sync_close is true" do
io = IO::Memory.new "abcdefg"
delimited = IO::Delimited.new(io, read_delimiter: "zr", sync_close: true)
delimited.sync_close?.should eq(true)
io.closed?.should eq(false)
delimited.close
io.closed?.should eq(true)
end
end
end
|