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
|
require "spec"
describe IO::Stapled do
it "combines two IOs" do
writer = IO::Memory.new
io = IO::Stapled.new IO::Memory.new("paul"), writer
io.gets.should eq "paul"
io << "peter"
writer.to_s.should eq "peter"
end
it "loops back" do
io = IO::Stapled.new(*IO.pipe)
io.puts "linus"
io.gets.should eq "linus"
end
describe "#close" do
it "does not close underlying IOs" do
reader, writer = IO::Memory.new, IO::Memory.new
io = IO::Stapled.new reader, writer
io.sync_close?.should be_false
io.close
io.closed?.should be_true
reader.closed?.should be_false
writer.closed?.should be_false
end
it "closes underlying IOs when sync_close is true" do
reader, writer = IO::Memory.new, IO::Memory.new
io = IO::Stapled.new reader, writer, sync_close: true
io.sync_close?.should be_true
io.close
io.closed?.should be_true
reader.closed?.should be_true
writer.closed?.should be_true
end
it "stops access to underlying IOs" do
reader, writer = IO::Memory.new("cle"), IO::Memory.new
io = IO::Stapled.new reader, writer
io.close
io.closed?.should be_true
reader.closed?.should be_false
writer.closed?.should be_false
expect_raises(IO::Error, "Closed stream") do
io.gets
end
expect_raises(IO::Error, "Closed stream") do
io.peek
end
expect_raises(IO::Error, "Closed stream") do
io << "closed"
end
end
end
it "#sync_close?" do
reader, writer = IO::Memory.new, IO::Memory.new
io = IO::Stapled.new reader, writer
io.sync_close = false
io.sync_close?.should be_false
io.sync_close = true
io.sync_close?.should be_true
io.close
reader.closed?.should be_true
writer.closed?.should be_true
end
it "#peek delegates to reader" do
reader = IO::Memory.new "cletus"
io = IO::Stapled.new reader, IO::Memory.new
io.peek.should eq "cletus".to_slice
io.gets
io.peek.should eq Bytes.empty
end
it "#skip delegates to reader" do
reader = IO::Memory.new "cletus"
io = IO::Stapled.new reader, IO::Memory.new
io.peek.should eq "cletus".to_slice
io.skip(4)
io.peek.should eq "us".to_slice
end
it "#skip_to_end delegates to reader" do
reader = IO::Memory.new "cletus"
io = IO::Stapled.new reader, IO::Memory.new
io.peek.should eq "cletus".to_slice
io.skip_to_end
io.peek.should eq Bytes.empty
end
describe ".pipe" do
it "creates a bidirectional pipe" do
a, b = IO::Stapled.pipe
begin
a.sync_close?.should be_true
b.sync_close?.should be_true
a.puts "john"
b.gets.should eq "john"
b.puts "paul"
a.gets.should eq "paul"
ensure
a.close
b.close
end
end
it "with block creates a bidirectional pipe" do
ext_a, ext_b = nil, nil
IO::Stapled.pipe do |a, b|
ext_a, ext_b = a, b
a.sync_close?.should be_true
b.sync_close?.should be_true
a.puts "john"
b.gets.should eq "john"
b.puts "paul"
a.gets.should eq "paul"
a.sync_close = false
b.sync_close = false
end
ext_a.not_nil!.closed?.should be_true
ext_b.not_nil!.closed?.should be_true
end
end
end
|