summaryrefslogtreecommitdiff
path: root/spec/ruby/library/zlib/gzipreader
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/library/zlib/gzipreader')
-rw-r--r--spec/ruby/library/zlib/gzipreader/each_byte_spec.rb51
-rw-r--r--spec/ruby/library/zlib/gzipreader/each_char_spec.rb51
-rw-r--r--spec/ruby/library/zlib/gzipreader/each_line_spec.rb6
-rw-r--r--spec/ruby/library/zlib/gzipreader/each_spec.rb6
-rw-r--r--spec/ruby/library/zlib/gzipreader/eof_spec.rb54
-rw-r--r--spec/ruby/library/zlib/gzipreader/getc_spec.rb39
-rw-r--r--spec/ruby/library/zlib/gzipreader/gets_spec.rb22
-rw-r--r--spec/ruby/library/zlib/gzipreader/mtime_spec.rb11
-rw-r--r--spec/ruby/library/zlib/gzipreader/pos_spec.rb24
-rw-r--r--spec/ruby/library/zlib/gzipreader/read_spec.rb66
-rw-r--r--spec/ruby/library/zlib/gzipreader/readpartial_spec.rb17
-rw-r--r--spec/ruby/library/zlib/gzipreader/rewind_spec.rb47
-rw-r--r--spec/ruby/library/zlib/gzipreader/shared/each.rb49
-rw-r--r--spec/ruby/library/zlib/gzipreader/ungetbyte_spec.rb120
-rw-r--r--spec/ruby/library/zlib/gzipreader/ungetc_spec.rb284
15 files changed, 847 insertions, 0 deletions
diff --git a/spec/ruby/library/zlib/gzipreader/each_byte_spec.rb b/spec/ruby/library/zlib/gzipreader/each_byte_spec.rb
new file mode 100644
index 0000000000..48821dc833
--- /dev/null
+++ b/spec/ruby/library/zlib/gzipreader/each_byte_spec.rb
@@ -0,0 +1,51 @@
+require_relative '../../../spec_helper'
+require 'stringio'
+require 'zlib'
+
+describe "Zlib::GzipReader#each_byte" do
+
+ before :each do
+ @data = '12345abcde'
+ @zip = [31, 139, 8, 0, 44, 220, 209, 71, 0, 3, 51, 52, 50, 54, 49, 77,
+ 76, 74, 78, 73, 5, 0, 157, 5, 0, 36, 10, 0, 0, 0].pack('C*')
+
+ @io = StringIO.new @zip
+ ScratchPad.clear
+ end
+
+ it "calls the given block for each byte in the stream, passing the byte as an argument" do
+ gz = Zlib::GzipReader.new @io
+
+ ScratchPad.record []
+ gz.each_byte { |b| ScratchPad << b }
+
+ ScratchPad.recorded.should == [49, 50, 51, 52, 53, 97, 98, 99, 100, 101]
+ end
+
+ it "returns an enumerator, which yields each byte in the stream, when no block is passed" do
+ gz = Zlib::GzipReader.new @io
+ enum = gz.each_byte
+
+ ScratchPad.record []
+ while true
+ begin
+ ScratchPad << enum.next
+ rescue StopIteration
+ break
+ end
+ end
+
+ ScratchPad.recorded.should == [49, 50, 51, 52, 53, 97, 98, 99, 100, 101]
+ end
+
+ it "increments position before calling the block" do
+ gz = Zlib::GzipReader.new @io
+
+ i = 1
+ gz.each_byte do |ignore|
+ gz.pos.should == i
+ i += 1
+ end
+ end
+
+end
diff --git a/spec/ruby/library/zlib/gzipreader/each_char_spec.rb b/spec/ruby/library/zlib/gzipreader/each_char_spec.rb
new file mode 100644
index 0000000000..de6396da7e
--- /dev/null
+++ b/spec/ruby/library/zlib/gzipreader/each_char_spec.rb
@@ -0,0 +1,51 @@
+require_relative '../../../spec_helper'
+require 'stringio'
+require 'zlib'
+
+describe "Zlib::GzipReader#each_char" do
+
+ before :each do
+ @data = '12345abcde'
+ @zip = [31, 139, 8, 0, 44, 220, 209, 71, 0, 3, 51, 52, 50, 54, 49, 77,
+ 76, 74, 78, 73, 5, 0, 157, 5, 0, 36, 10, 0, 0, 0].pack('C*')
+
+ @io = StringIO.new @zip
+ ScratchPad.clear
+ end
+
+ it "calls the given block for each char in the stream, passing the char as an argument" do
+ gz = Zlib::GzipReader.new @io
+
+ ScratchPad.record []
+ gz.each_char { |b| ScratchPad << b }
+
+ ScratchPad.recorded.should == ["1", "2", "3", "4", "5", "a", "b", "c", "d", "e"]
+ end
+
+ it "returns an enumerator, which yields each char in the stream, when no block is passed" do
+ gz = Zlib::GzipReader.new @io
+ enum = gz.each_char
+
+ ScratchPad.record []
+ while true
+ begin
+ ScratchPad << enum.next
+ rescue StopIteration
+ break
+ end
+ end
+
+ ScratchPad.recorded.should == ["1", "2", "3", "4", "5", "a", "b", "c", "d", "e"]
+ end
+
+ it "increments position before calling the block" do
+ gz = Zlib::GzipReader.new @io
+
+ i = 1
+ gz.each_char do |ignore|
+ gz.pos.should == i
+ i += 1
+ end
+ end
+
+end
diff --git a/spec/ruby/library/zlib/gzipreader/each_line_spec.rb b/spec/ruby/library/zlib/gzipreader/each_line_spec.rb
new file mode 100644
index 0000000000..6f17365879
--- /dev/null
+++ b/spec/ruby/library/zlib/gzipreader/each_line_spec.rb
@@ -0,0 +1,6 @@
+require_relative "../../../spec_helper"
+require_relative 'shared/each'
+
+describe "Zlib::GzipReader#each_line" do
+ it_behaves_like :gzipreader_each, :each_line
+end
diff --git a/spec/ruby/library/zlib/gzipreader/each_spec.rb b/spec/ruby/library/zlib/gzipreader/each_spec.rb
new file mode 100644
index 0000000000..3b98391a87
--- /dev/null
+++ b/spec/ruby/library/zlib/gzipreader/each_spec.rb
@@ -0,0 +1,6 @@
+require_relative "../../../spec_helper"
+require_relative 'shared/each'
+
+describe "Zlib::GzipReader#each" do
+ it_behaves_like :gzipreader_each, :each
+end
diff --git a/spec/ruby/library/zlib/gzipreader/eof_spec.rb b/spec/ruby/library/zlib/gzipreader/eof_spec.rb
new file mode 100644
index 0000000000..a38e144c72
--- /dev/null
+++ b/spec/ruby/library/zlib/gzipreader/eof_spec.rb
@@ -0,0 +1,54 @@
+require_relative '../../../spec_helper'
+require 'stringio'
+require 'zlib'
+
+describe "Zlib::GzipReader#eof?" do
+ before :each do
+ @data = '{"a":1234}'
+ @zip = [31, 139, 8, 0, 0, 0, 0, 0, 0, 3, 171, 86, 74, 84, 178, 50,
+ 52, 50, 54, 169, 5, 0, 196, 20, 118, 213, 10, 0, 0, 0].pack('C*')
+ @io = StringIO.new @zip
+ end
+
+ it "returns true when at EOF" do
+ gz = Zlib::GzipReader.new @io
+ gz.eof?.should == false
+ gz.read
+ gz.eof?.should == true
+ end
+
+ it "returns true when at EOF with the exact length of uncompressed data" do
+ gz = Zlib::GzipReader.new @io
+ gz.eof?.should == false
+ gz.read(10)
+ gz.eof?.should == true
+ end
+
+ it "returns true when at EOF with a length greater than the size of uncompressed data" do
+ gz = Zlib::GzipReader.new @io
+ gz.eof?.should == false
+ gz.read(11)
+ gz.eof?.should == true
+ end
+
+ it "returns false when at EOF when there's data left in the buffer to read" do
+ gz = Zlib::GzipReader.new @io
+ gz.read(9)
+ gz.eof?.should == false
+ gz.read
+ gz.eof?.should == true
+ end
+
+ # This is especially important for JRuby, since eof? there
+ # is more than just a simple accessor.
+ it "does not affect the reading data" do
+ gz = Zlib::GzipReader.new @io
+ 0.upto(9) do |i|
+ gz.eof?.should == false
+ gz.read(1).should == @data[i, 1]
+ end
+ gz.eof?.should == true
+ gz.read.should == ""
+ gz.eof?.should == true
+ end
+end
diff --git a/spec/ruby/library/zlib/gzipreader/getc_spec.rb b/spec/ruby/library/zlib/gzipreader/getc_spec.rb
new file mode 100644
index 0000000000..be13592189
--- /dev/null
+++ b/spec/ruby/library/zlib/gzipreader/getc_spec.rb
@@ -0,0 +1,39 @@
+require_relative '../../../spec_helper'
+require 'stringio'
+require 'zlib'
+
+describe "Zlib::GzipReader#getc" do
+ before :each do
+ @data = '12345abcde'
+ @zip = [31, 139, 8, 0, 44, 220, 209, 71, 0, 3, 51, 52, 50, 54, 49, 77,
+ 76, 74, 78, 73, 5, 0, 157, 5, 0, 36, 10, 0, 0, 0].pack('C*')
+ @io = StringIO.new @zip
+ end
+
+ it "returns the next character from the stream" do
+ gz = Zlib::GzipReader.new @io
+ gz.pos.should == 0
+
+ gz.getc.should == '1'
+ gz.getc.should == '2'
+ gz.getc.should == '3'
+ gz.getc.should == '4'
+ gz.getc.should == '5'
+ end
+
+ it "increments position" do
+ gz = Zlib::GzipReader.new @io
+ (0..@data.size).each do |i|
+ gz.pos.should == i
+ gz.getc
+ end
+ end
+
+ it "returns nil at the end of the stream" do
+ gz = Zlib::GzipReader.new @io
+ gz.read
+ pos = gz.pos
+ gz.getc.should == nil
+ gz.pos.should == pos
+ end
+end
diff --git a/spec/ruby/library/zlib/gzipreader/gets_spec.rb b/spec/ruby/library/zlib/gzipreader/gets_spec.rb
new file mode 100644
index 0000000000..5d0809f833
--- /dev/null
+++ b/spec/ruby/library/zlib/gzipreader/gets_spec.rb
@@ -0,0 +1,22 @@
+require_relative '../../../spec_helper'
+require 'zlib'
+require 'stringio'
+
+describe 'Zlib::GzipReader#gets' do
+ describe 'with "" separator' do
+ it 'reads paragraphs skipping newlines' do
+ # gz contains "\n\n\n\n\n123\n45\n\n\n\n\nabc\nde\n\n\n\n\n"
+ gz = Zlib::GzipReader.new(
+ StringIO.new(
+ [31, 139, 8, 0, 223, 152, 48, 89, 0, 3, 227, 226, 2, 2, 67, 35,
+ 99, 46, 19, 83, 16, 139, 43, 49, 41, 153, 43, 37, 21, 204, 4, 0,
+ 32, 119, 45, 184, 27, 0, 0, 0].pack('C*')
+ )
+ )
+
+ gz.gets('').should == "123\n45\n\n"
+ gz.gets('').should == "abc\nde\n\n"
+ gz.eof?.should == true
+ end
+ end
+end
diff --git a/spec/ruby/library/zlib/gzipreader/mtime_spec.rb b/spec/ruby/library/zlib/gzipreader/mtime_spec.rb
new file mode 100644
index 0000000000..e8e71fa72e
--- /dev/null
+++ b/spec/ruby/library/zlib/gzipreader/mtime_spec.rb
@@ -0,0 +1,11 @@
+require_relative '../../../spec_helper'
+require 'zlib'
+require 'stringio'
+
+describe "Zlib::GzipReader#mtime" do
+ it "returns the timestamp from the Gzip header" do
+ io = StringIO.new "\x1f\x8b\x08\x00\x44\x33\x22\x11\x00\xff\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00"
+ gz = Zlib::GzipReader.new(io)
+ gz.mtime.to_i.should == 0x11223344
+ end
+end
diff --git a/spec/ruby/library/zlib/gzipreader/pos_spec.rb b/spec/ruby/library/zlib/gzipreader/pos_spec.rb
new file mode 100644
index 0000000000..8586faec92
--- /dev/null
+++ b/spec/ruby/library/zlib/gzipreader/pos_spec.rb
@@ -0,0 +1,24 @@
+require_relative '../../../spec_helper'
+require 'stringio'
+require 'zlib'
+
+describe "Zlib::GzipReader#pos" do
+ before :each do
+ @data = '12345abcde'
+ @zip = [31, 139, 8, 0, 44, 220, 209, 71, 0, 3, 51, 52, 50, 54, 49, 77,
+ 76, 74, 78, 73, 5, 0, 157, 5, 0, 36, 10, 0, 0, 0].pack('C*')
+ @io = StringIO.new @zip
+ end
+
+ it "returns the position" do
+ gz = Zlib::GzipReader.new @io
+
+ gz.pos.should == 0
+
+ gz.read 5
+ gz.pos.should == 5
+
+ gz.read
+ gz.pos.should == @data.length
+ end
+end
diff --git a/spec/ruby/library/zlib/gzipreader/read_spec.rb b/spec/ruby/library/zlib/gzipreader/read_spec.rb
new file mode 100644
index 0000000000..b07d433bdd
--- /dev/null
+++ b/spec/ruby/library/zlib/gzipreader/read_spec.rb
@@ -0,0 +1,66 @@
+require_relative '../../../spec_helper'
+require 'stringio'
+require 'zlib'
+
+describe "Zlib::GzipReader#read" do
+ before :each do
+ @data = '12345abcde'
+ @zip = [31, 139, 8, 0, 44, 220, 209, 71, 0, 3, 51, 52, 50, 54, 49, 77,
+ 76, 74, 78, 73, 5, 0, 157, 5, 0, 36, 10, 0, 0, 0].pack('C*')
+ @io = StringIO.new @zip
+ end
+
+ it "with no arguments reads the entire content of a gzip file" do
+ gz = Zlib::GzipReader.new @io
+ gz.read.should == @data
+ end
+
+ it "with nil length argument reads the entire content of a gzip file" do
+ gz = Zlib::GzipReader.new @io
+ gz.read(nil).should == @data
+ end
+
+ it "reads the contents up to a certain size" do
+ gz = Zlib::GzipReader.new @io
+ gz.read(5).should == @data[0...5]
+ gz.read(5).should == @data[5...10]
+ end
+
+ it "does not accept a negative length to read" do
+ gz = Zlib::GzipReader.new @io
+ -> {
+ gz.read(-1)
+ }.should.raise(ArgumentError)
+ end
+
+ it "returns an empty string if a 0 length is given" do
+ gz = Zlib::GzipReader.new @io
+ gz.read(0).should == ""
+ end
+
+ it "respects :external_encoding option" do
+ gz = Zlib::GzipReader.new(@io, external_encoding: 'UTF-8')
+ gz.read.encoding.should == Encoding::UTF_8
+
+ @io.rewind
+ gz = Zlib::GzipReader.new(@io, external_encoding: 'UTF-16LE')
+ gz.read.encoding.should == Encoding::UTF_16LE
+ end
+
+ describe "at the end of data" do
+ it "returns empty string if length parameter is not specified or 0" do
+ gz = Zlib::GzipReader.new @io
+ gz.read # read till the end
+ gz.read(0).should == ""
+ gz.read().should == ""
+ gz.read(nil).should == ""
+ end
+
+ it "returns nil if length parameter is positive" do
+ gz = Zlib::GzipReader.new @io
+ gz.read # read till the end
+ gz.read(1).should == nil
+ gz.read(2**16).should == nil
+ end
+ end
+end
diff --git a/spec/ruby/library/zlib/gzipreader/readpartial_spec.rb b/spec/ruby/library/zlib/gzipreader/readpartial_spec.rb
new file mode 100644
index 0000000000..559ce9f841
--- /dev/null
+++ b/spec/ruby/library/zlib/gzipreader/readpartial_spec.rb
@@ -0,0 +1,17 @@
+require_relative '../../../spec_helper'
+require 'stringio'
+require 'zlib'
+
+describe "Zlib::GzipReader#readpartial" do
+ before :each do
+ @data = '12345abcde'
+ @zip = [31, 139, 8, 0, 44, 220, 209, 71, 0, 3, 51, 52, 50, 54, 49, 77,
+ 76, 74, 78, 73, 5, 0, 157, 5, 0, 36, 10, 0, 0, 0].pack('C*')
+ @io = StringIO.new(@zip)
+ end
+
+ it 'accepts nil buffer' do
+ gz = Zlib::GzipReader.new(@io)
+ gz.readpartial(5, nil).should == '12345'
+ end
+end
diff --git a/spec/ruby/library/zlib/gzipreader/rewind_spec.rb b/spec/ruby/library/zlib/gzipreader/rewind_spec.rb
new file mode 100644
index 0000000000..b31abb6abf
--- /dev/null
+++ b/spec/ruby/library/zlib/gzipreader/rewind_spec.rb
@@ -0,0 +1,47 @@
+require_relative '../../../spec_helper'
+require 'stringio'
+require 'zlib'
+
+describe "Zlib::GzipReader#rewind" do
+ before :each do
+ @data = '12345abcde'
+ @zip = [31, 139, 8, 0, 44, 220, 209, 71, 0, 3, 51, 52, 50, 54, 49, 77,
+ 76, 74, 78, 73, 5, 0, 157, 5, 0, 36, 10, 0, 0, 0].pack('C*')
+ @io = StringIO.new @zip
+ ScratchPad.clear
+ end
+
+ it "resets the position of the stream pointer" do
+ gz = Zlib::GzipReader.new @io
+ gz.read
+ gz.pos.should == @data.length
+
+ gz.rewind
+ gz.pos.should == 0
+ gz.lineno.should == 0
+ end
+
+ it "resets the position of the stream pointer to data previously read" do
+ gz = Zlib::GzipReader.new @io
+ first_read = gz.read
+ gz.rewind
+ first_read.should == gz.read
+ end
+
+ it "invokes seek method on the associated IO object" do
+ # first, prepare the mock object:
+ (obj = mock("io")).should_receive(:get_io).any_number_of_times.and_return(@io)
+ def obj.read(args); get_io.read(args); end
+ def obj.seek(pos, whence = 0)
+ ScratchPad.record :seek
+ get_io.seek(pos, whence)
+ end
+
+ gz = Zlib::GzipReader.new(obj)
+ gz.rewind()
+
+ ScratchPad.recorded.should == :seek
+ gz.pos.should == 0
+ gz.read.should == "12345abcde"
+ end
+end
diff --git a/spec/ruby/library/zlib/gzipreader/shared/each.rb b/spec/ruby/library/zlib/gzipreader/shared/each.rb
new file mode 100644
index 0000000000..71608e04ab
--- /dev/null
+++ b/spec/ruby/library/zlib/gzipreader/shared/each.rb
@@ -0,0 +1,49 @@
+require_relative '../../../../spec_helper'
+require 'stringio'
+require 'zlib'
+
+describe :gzipreader_each, shared: true do
+ before :each do
+ @data = "firstline\nsecondline\n\nforthline"
+ @zip = [31, 139, 8, 0, 244, 125, 128, 88, 2, 255, 75, 203, 44, 42, 46, 201,
+ 201, 204, 75, 229, 42, 78, 77, 206, 207, 75, 1, 51, 185, 210,242,
+ 139, 74, 50, 64, 76, 0, 180, 54, 61, 111, 31, 0, 0, 0].pack('C*')
+
+ @io = StringIO.new @zip
+ @gzreader = Zlib::GzipReader.new @io
+ end
+
+ after :each do
+ ScratchPad.clear
+ end
+
+ it "calls the given block for each line in the stream, passing the line as an argument" do
+ ScratchPad.record []
+ @gzreader.send(@method) { |b| ScratchPad << b }
+
+ ScratchPad.recorded.should == ["firstline\n", "secondline\n", "\n", "forthline"]
+ end
+
+ it "returns an enumerator, which yields each byte in the stream, when no block is passed" do
+ enum = @gzreader.send(@method)
+
+ ScratchPad.record []
+ while true
+ begin
+ ScratchPad << enum.next
+ rescue StopIteration
+ break
+ end
+ end
+
+ ScratchPad.recorded.should == ["firstline\n", "secondline\n", "\n", "forthline"]
+ end
+
+ it "increments position before calling the block" do
+ i = 0
+ @gzreader.send(@method) do |line|
+ i += line.length
+ @gzreader.pos.should == i
+ end
+ end
+end
diff --git a/spec/ruby/library/zlib/gzipreader/ungetbyte_spec.rb b/spec/ruby/library/zlib/gzipreader/ungetbyte_spec.rb
new file mode 100644
index 0000000000..53870b9177
--- /dev/null
+++ b/spec/ruby/library/zlib/gzipreader/ungetbyte_spec.rb
@@ -0,0 +1,120 @@
+require_relative '../../../spec_helper'
+require 'stringio'
+require 'zlib'
+
+describe "Zlib::GzipReader#ungetbyte" do
+ before :each do
+ @data = '12345abcde'
+ @zip = [31, 139, 8, 0, 44, 220, 209, 71, 0, 3, 51, 52, 50, 54, 49, 77,
+ 76, 74, 78, 73, 5, 0, 157, 5, 0, 36, 10, 0, 0, 0].pack('C*')
+ @io = StringIO.new @zip
+ end
+
+ describe 'at the start of the stream' do
+ before :each do
+ @gz = Zlib::GzipReader.new(@io)
+ end
+
+ describe 'with an integer' do
+ it 'prepends the byte to the stream' do
+ @gz.ungetbyte 0x21
+ @gz.read.should == '!12345abcde'
+ end
+
+ it 'decrements pos' do
+ @gz.ungetbyte 0x21
+ @gz.pos.should == -1
+ end
+ end
+
+ quarantine! do # https://bugs.ruby-lang.org/issues/13675
+ describe 'with nil' do
+ it 'does not prepend anything to the stream' do
+ @gz.ungetbyte nil
+ @gz.read.should == '12345abcde'
+ end
+
+ it 'does not decrement pos' do
+ @gz.ungetbyte nil
+ @gz.pos.should == 0
+ end
+ end
+ end
+ end
+
+ describe 'in the middle of the stream' do
+ before :each do
+ @gz = Zlib::GzipReader.new(@io)
+ @gz.read 5
+ end
+
+ describe 'with an integer' do
+ it 'inserts the corresponding character into the stream' do
+ @gz.ungetbyte 0x21
+ @gz.read.should == '!abcde'
+ end
+
+ it 'decrements pos' do
+ @gz.ungetbyte 0x21
+ @gz.pos.should == 4
+ end
+ end
+
+ quarantine! do # https://bugs.ruby-lang.org/issues/13675
+ describe 'with nil' do
+ it 'does not insert anything into the stream' do
+ @gz.ungetbyte nil
+ @gz.read.should == 'abcde'
+ end
+
+ it 'does not decrement pos' do
+ @gz.ungetbyte nil
+ @gz.pos.should == 5
+ end
+ end
+ end
+ end
+
+ describe 'at the end of the stream' do
+ before :each do
+ @gz = Zlib::GzipReader.new(@io)
+ @gz.read
+ end
+
+ describe 'with an integer' do
+ it 'appends the corresponding character to the stream' do
+ @gz.ungetbyte 0x21
+ @gz.read.should == '!'
+ end
+
+ it 'decrements pos' do
+ @gz.ungetbyte 0x21
+ @gz.pos.should == 9
+ end
+
+ it 'makes eof? false' do
+ @gz.ungetbyte 0x21
+ @gz.eof?.should == false
+ end
+ end
+
+ quarantine! do # https://bugs.ruby-lang.org/issues/13675
+ describe 'with nil' do
+ it 'does not append anything to the stream' do
+ @gz.ungetbyte nil
+ @gz.read.should == ''
+ end
+
+ it 'does not decrement pos' do
+ @gz.ungetbyte nil
+ @gz.pos.should == 10
+ end
+
+ it 'does not make eof? false' do
+ @gz.ungetbyte nil
+ @gz.eof?.should == true
+ end
+ end
+ end
+ end
+end
diff --git a/spec/ruby/library/zlib/gzipreader/ungetc_spec.rb b/spec/ruby/library/zlib/gzipreader/ungetc_spec.rb
new file mode 100644
index 0000000000..46dcfde989
--- /dev/null
+++ b/spec/ruby/library/zlib/gzipreader/ungetc_spec.rb
@@ -0,0 +1,284 @@
+require_relative '../../../spec_helper'
+require 'stringio'
+require 'zlib'
+
+describe "Zlib::GzipReader#ungetc" do
+ before :each do
+ @data = '12345abcde'
+ @zip = [31, 139, 8, 0, 44, 220, 209, 71, 0, 3, 51, 52, 50, 54, 49, 77,
+ 76, 74, 78, 73, 5, 0, 157, 5, 0, 36, 10, 0, 0, 0].pack('C*')
+ @io = StringIO.new @zip
+ end
+
+ describe 'at the start of the stream' do
+ before :each do
+ @gz = Zlib::GzipReader.new(@io, external_encoding: Encoding::UTF_8)
+ end
+
+ describe 'with a single-byte character' do
+ it 'prepends the character to the stream' do
+ @gz.ungetc 'x'
+ @gz.read.should == 'x12345abcde'
+ end
+
+ it 'decrements pos' do
+ @gz.ungetc 'x'
+ @gz.pos.should == -1
+ end
+ end
+
+ describe 'with a multi-byte character' do
+ it 'prepends the character to the stream' do
+ @gz.ungetc 'ŷ'
+ @gz.read.should == 'ŷ12345abcde'
+ end
+
+ it 'decrements pos' do
+ @gz.ungetc 'ŷ'
+ @gz.pos.should == -2
+ end
+ end
+
+ describe 'with a multi-character string' do
+ it 'prepends the characters to the stream' do
+ @gz.ungetc 'xŷž'
+ @gz.read.should == 'xŷž12345abcde'
+ end
+
+ it 'decrements pos' do
+ @gz.ungetc 'xŷž'
+ @gz.pos.should == -5
+ end
+ end
+
+ describe 'with an integer' do
+ it 'prepends the corresponding character to the stream' do
+ @gz.ungetc 0x21
+ @gz.read.should == '!12345abcde'
+ end
+
+ it 'decrements pos' do
+ @gz.ungetc 0x21
+ @gz.pos.should == -1
+ end
+ end
+
+ describe 'with an empty string' do
+ it 'does not prepend anything to the stream' do
+ @gz.ungetc ''
+ @gz.read.should == '12345abcde'
+ end
+
+ it 'does not decrement pos' do
+ @gz.ungetc ''
+ @gz.pos.should == 0
+ end
+ end
+
+ quarantine! do # https://bugs.ruby-lang.org/issues/13675
+ describe 'with nil' do
+ it 'does not prepend anything to the stream' do
+ @gz.ungetc nil
+ @gz.read.should == '12345abcde'
+ end
+
+ it 'does not decrement pos' do
+ @gz.ungetc nil
+ @gz.pos.should == 0
+ end
+ end
+ end
+ end
+
+ describe 'in the middle of the stream' do
+ before :each do
+ @gz = Zlib::GzipReader.new(@io, external_encoding: Encoding::UTF_8)
+ @gz.read 5
+ end
+
+ describe 'with a single-byte character' do
+ it 'inserts the character into the stream' do
+ @gz.ungetc 'x'
+ @gz.read.should == 'xabcde'
+ end
+
+ it 'decrements pos' do
+ @gz.ungetc 'x'
+ @gz.pos.should == 4
+ end
+ end
+
+ describe 'with a multi-byte character' do
+ it 'inserts the character into the stream' do
+ @gz.ungetc 'ŷ'
+ @gz.read.should == 'ŷabcde'
+ end
+
+ it 'decrements pos' do
+ @gz.ungetc 'ŷ'
+ @gz.pos.should == 3
+ end
+ end
+
+ describe 'with a multi-character string' do
+ it 'inserts the characters into the stream' do
+ @gz.ungetc 'xŷž'
+ @gz.read.should == 'xŷžabcde'
+ end
+
+ it 'decrements pos' do
+ @gz.ungetc 'xŷž'
+ @gz.pos.should == 0
+ end
+ end
+
+ describe 'with an integer' do
+ it 'inserts the corresponding character into the stream' do
+ @gz.ungetc 0x21
+ @gz.read.should == '!abcde'
+ end
+
+ it 'decrements pos' do
+ @gz.ungetc 0x21
+ @gz.pos.should == 4
+ end
+ end
+
+ describe 'with an empty string' do
+ it 'does not insert anything into the stream' do
+ @gz.ungetc ''
+ @gz.read.should == 'abcde'
+ end
+
+ it 'does not decrement pos' do
+ @gz.ungetc ''
+ @gz.pos.should == 5
+ end
+ end
+
+ quarantine! do # https://bugs.ruby-lang.org/issues/13675
+ describe 'with nil' do
+ it 'does not insert anything into the stream' do
+ @gz.ungetc nil
+ @gz.read.should == 'abcde'
+ end
+
+ it 'does not decrement pos' do
+ @gz.ungetc nil
+ @gz.pos.should == 5
+ end
+ end
+ end
+ end
+
+ describe 'at the end of the stream' do
+ before :each do
+ @gz = Zlib::GzipReader.new(@io, external_encoding: Encoding::UTF_8)
+ @gz.read
+ end
+
+ describe 'with a single-byte character' do
+ it 'appends the character to the stream' do
+ @gz.ungetc 'x'
+ @gz.read.should == 'x'
+ end
+
+ it 'decrements pos' do
+ @gz.ungetc 'x'
+ @gz.pos.should == 9
+ end
+
+ it 'makes eof? false' do
+ @gz.ungetc 'x'
+ @gz.eof?.should == false
+ end
+ end
+
+ describe 'with a multi-byte character' do
+ it 'appends the character to the stream' do
+ @gz.ungetc 'ŷ'
+ @gz.read.should == 'ŷ'
+ end
+
+ it 'decrements pos' do
+ @gz.ungetc 'ŷ'
+ @gz.pos.should == 8
+ end
+
+ it 'makes eof? false' do
+ @gz.ungetc 'ŷ'
+ @gz.eof?.should == false
+ end
+ end
+
+ describe 'with a multi-character string' do
+ it 'appends the characters to the stream' do
+ @gz.ungetc 'xŷž'
+ @gz.read.should == 'xŷž'
+ end
+
+ it 'decrements pos' do
+ @gz.ungetc 'xŷž'
+ @gz.pos.should == 5
+ end
+
+ it 'makes eof? false' do
+ @gz.ungetc 'xŷž'
+ @gz.eof?.should == false
+ end
+ end
+
+ describe 'with an integer' do
+ it 'appends the corresponding character to the stream' do
+ @gz.ungetc 0x21
+ @gz.read.should == '!'
+ end
+
+ it 'decrements pos' do
+ @gz.ungetc 0x21
+ @gz.pos.should == 9
+ end
+
+ it 'makes eof? false' do
+ @gz.ungetc 0x21
+ @gz.eof?.should == false
+ end
+ end
+
+ describe 'with an empty string' do
+ it 'does not append anything to the stream' do
+ @gz.ungetc ''
+ @gz.read.should == ''
+ end
+
+ it 'does not decrement pos' do
+ @gz.ungetc ''
+ @gz.pos.should == 10
+ end
+
+ it 'does not make eof? false' do
+ @gz.ungetc ''
+ @gz.eof?.should == true
+ end
+ end
+
+ quarantine! do # https://bugs.ruby-lang.org/issues/13675
+ describe 'with nil' do
+ it 'does not append anything to the stream' do
+ @gz.ungetc nil
+ @gz.read.should == ''
+ end
+
+ it 'does not decrement pos' do
+ @gz.ungetc nil
+ @gz.pos.should == 10
+ end
+
+ it 'does not make eof? false' do
+ @gz.ungetc nil
+ @gz.eof?.should == true
+ end
+ end
+ end
+ end
+end