test/io_test.rb in bindata-2.2.0 vs test/io_test.rb in bindata-2.3.0
- old
+ new
@@ -132,12 +132,61 @@
io.with_buffer(10) do
io.seekbytes(15)
end
io.offset.must_equal(10)
end
+
+ it "restricts large -ve seeks" do
+ io.readbytes(2)
+ io.with_buffer(10) do
+ io.seekbytes(-1)
+ io.offset.must_equal(2)
+ end
+ end
end
+describe BinData::IO::Write, "writing to non seekable stream" do
+ before do
+ @rd, @wr = IO::pipe
+ @io = BinData::IO::Write.new(@wr)
+ end
+
+ after do
+ @rd.close
+ @wr.close
+ end
+
+ it "writes data" do
+ @io.writebytes("1234567890")
+ @rd.read(10).must_equal "1234567890"
+ end
+
+ it "has correct offset" do
+ @io.writebytes("1234567890")
+ @io.offset.must_equal 10
+ end
+
+ it "does not seek backwards" do
+ @io.writebytes("1234567890")
+ lambda {
+ @io.seekbytes(-5)
+ }.must_raise IOError
+ end
+
+ it "does not seek forwards" do
+ lambda {
+ @io.seekbytes(5)
+ }.must_raise IOError
+ end
+
+ it "#num_bytes_remaining raises IOError" do
+ lambda {
+ @io.num_bytes_remaining
+ }.must_raise IOError
+ end
+end
+
describe BinData::IO::Write, "when writing" do
let(:stream) { StringIO.new }
let(:io) { BinData::IO::Write.new(stream) }
it "raises error when io is BinData::IO" do
@@ -202,9 +251,24 @@
end
io.writebytes "de"
end
stream.value.must_equal "abc\0\0de\0\0\0"
+ end
+
+ it "restricts large seeks" do
+ io.with_buffer(10) do
+ io.seekbytes(15)
+ end
+ io.offset.must_equal(10)
+ end
+
+ it "restricts large -ve seeks" do
+ io.writebytes("12")
+ io.with_buffer(10) do
+ io.seekbytes(-1)
+ io.offset.must_equal(2)
+ end
end
end
describe BinData::IO::Read, "reading bits in big endian" do
let(:b1) { 0b1111_1010 }