diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-07 12:04:49 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-07 12:04:49 +0000 |
commit | a3736e97a6ca517c2cd7d3d93a8f2ef86e39e5b5 (patch) | |
tree | 9eef7f720314ebaff56845a74e203770e62284e4 /spec/rubyspec/library/zlib/gzipreader | |
parent | 52df1d0d3370919711c0577aaa42d1a864709885 (diff) | |
download | ruby-a3736e97a6ca517c2cd7d3d93a8f2ef86e39e5b5.tar.gz |
Add in-tree mspec and ruby/spec
* For easier modifications of ruby/spec by MRI developers.
* .gitignore: track changes under spec.
* spec/mspec, spec/rubyspec: add in-tree mspec and ruby/spec.
These files can therefore be updated like any other file in MRI.
Instructions are provided in spec/README.
[Feature #13156] [ruby-core:79246]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58595 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/rubyspec/library/zlib/gzipreader')
19 files changed, 362 insertions, 0 deletions
diff --git a/spec/rubyspec/library/zlib/gzipreader/each_byte_spec.rb b/spec/rubyspec/library/zlib/gzipreader/each_byte_spec.rb new file mode 100644 index 0000000000..6da9ac8323 --- /dev/null +++ b/spec/rubyspec/library/zlib/gzipreader/each_byte_spec.rb @@ -0,0 +1,51 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'stringio' +require 'zlib' + +describe "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/rubyspec/library/zlib/gzipreader/each_line_spec.rb b/spec/rubyspec/library/zlib/gzipreader/each_line_spec.rb new file mode 100644 index 0000000000..7ff116a258 --- /dev/null +++ b/spec/rubyspec/library/zlib/gzipreader/each_line_spec.rb @@ -0,0 +1,5 @@ +require File.expand_path('../shared/each', __FILE__) + +describe "GzipReader#each_line" do + it_behaves_like :gzipreader_each, :each_line +end diff --git a/spec/rubyspec/library/zlib/gzipreader/each_spec.rb b/spec/rubyspec/library/zlib/gzipreader/each_spec.rb new file mode 100644 index 0000000000..dd780e4083 --- /dev/null +++ b/spec/rubyspec/library/zlib/gzipreader/each_spec.rb @@ -0,0 +1,5 @@ +require File.expand_path('../shared/each', __FILE__) + +describe "GzipReader#each" do + it_behaves_like :gzipreader_each, :each +end diff --git a/spec/rubyspec/library/zlib/gzipreader/eof_spec.rb b/spec/rubyspec/library/zlib/gzipreader/eof_spec.rb new file mode 100644 index 0000000000..446cbfec37 --- /dev/null +++ b/spec/rubyspec/library/zlib/gzipreader/eof_spec.rb @@ -0,0 +1,56 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'stringio' +require 'zlib' + +describe "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 be_false + gz.read + gz.eof?.should be_true + end + + it "returns true when at EOF with the exact length of uncompressed data" do + gz = Zlib::GzipReader.new @io + gz.eof?.should be_false + gz.read(10) + gz.eof?.should be_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 be_false + gz.read(11) + gz.eof?.should be_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 be_false + gz.read + gz.eof?.should be_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 be_false + gz.read(1).should == @data[i, 1] + end + gz.eof?.should be_true + gz.read().should == "" + gz.eof?.should be_true + end + +end diff --git a/spec/rubyspec/library/zlib/gzipreader/getc_spec.rb b/spec/rubyspec/library/zlib/gzipreader/getc_spec.rb new file mode 100644 index 0000000000..a3c4aecf76 --- /dev/null +++ b/spec/rubyspec/library/zlib/gzipreader/getc_spec.rb @@ -0,0 +1,41 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'stringio' +require 'zlib' + +describe "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 be_nil + gz.pos.should == pos + end + +end diff --git a/spec/rubyspec/library/zlib/gzipreader/gets_spec.rb b/spec/rubyspec/library/zlib/gzipreader/gets_spec.rb new file mode 100644 index 0000000000..6a4c1dadb4 --- /dev/null +++ b/spec/rubyspec/library/zlib/gzipreader/gets_spec.rb @@ -0,0 +1 @@ +require File.expand_path('../../../../spec_helper', __FILE__) diff --git a/spec/rubyspec/library/zlib/gzipreader/lineno_spec.rb b/spec/rubyspec/library/zlib/gzipreader/lineno_spec.rb new file mode 100644 index 0000000000..6a4c1dadb4 --- /dev/null +++ b/spec/rubyspec/library/zlib/gzipreader/lineno_spec.rb @@ -0,0 +1 @@ +require File.expand_path('../../../../spec_helper', __FILE__) diff --git a/spec/rubyspec/library/zlib/gzipreader/new_spec.rb b/spec/rubyspec/library/zlib/gzipreader/new_spec.rb new file mode 100644 index 0000000000..6a4c1dadb4 --- /dev/null +++ b/spec/rubyspec/library/zlib/gzipreader/new_spec.rb @@ -0,0 +1 @@ +require File.expand_path('../../../../spec_helper', __FILE__) diff --git a/spec/rubyspec/library/zlib/gzipreader/open_spec.rb b/spec/rubyspec/library/zlib/gzipreader/open_spec.rb new file mode 100644 index 0000000000..6a4c1dadb4 --- /dev/null +++ b/spec/rubyspec/library/zlib/gzipreader/open_spec.rb @@ -0,0 +1 @@ +require File.expand_path('../../../../spec_helper', __FILE__) diff --git a/spec/rubyspec/library/zlib/gzipreader/pos_spec.rb b/spec/rubyspec/library/zlib/gzipreader/pos_spec.rb new file mode 100644 index 0000000000..66fbf388d8 --- /dev/null +++ b/spec/rubyspec/library/zlib/gzipreader/pos_spec.rb @@ -0,0 +1,27 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'stringio' +require 'zlib' + +describe "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/rubyspec/library/zlib/gzipreader/read_spec.rb b/spec/rubyspec/library/zlib/gzipreader/read_spec.rb new file mode 100644 index 0000000000..337f507502 --- /dev/null +++ b/spec/rubyspec/library/zlib/gzipreader/read_spec.rb @@ -0,0 +1,68 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'stringio' +require 'zlib' + +describe "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 + lambda { + gz.read(-1) + }.should raise_error(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 prameter 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 prameter is positive" do + gz = Zlib::GzipReader.new @io + gz.read # read till the end + gz.read(1).should be_nil + gz.read(2**16).should be_nil + end + end + +end diff --git a/spec/rubyspec/library/zlib/gzipreader/readchar_spec.rb b/spec/rubyspec/library/zlib/gzipreader/readchar_spec.rb new file mode 100644 index 0000000000..6a4c1dadb4 --- /dev/null +++ b/spec/rubyspec/library/zlib/gzipreader/readchar_spec.rb @@ -0,0 +1 @@ +require File.expand_path('../../../../spec_helper', __FILE__) diff --git a/spec/rubyspec/library/zlib/gzipreader/readline_spec.rb b/spec/rubyspec/library/zlib/gzipreader/readline_spec.rb new file mode 100644 index 0000000000..6a4c1dadb4 --- /dev/null +++ b/spec/rubyspec/library/zlib/gzipreader/readline_spec.rb @@ -0,0 +1 @@ +require File.expand_path('../../../../spec_helper', __FILE__) diff --git a/spec/rubyspec/library/zlib/gzipreader/readlines_spec.rb b/spec/rubyspec/library/zlib/gzipreader/readlines_spec.rb new file mode 100644 index 0000000000..6a4c1dadb4 --- /dev/null +++ b/spec/rubyspec/library/zlib/gzipreader/readlines_spec.rb @@ -0,0 +1 @@ +require File.expand_path('../../../../spec_helper', __FILE__) diff --git a/spec/rubyspec/library/zlib/gzipreader/rewind_spec.rb b/spec/rubyspec/library/zlib/gzipreader/rewind_spec.rb new file mode 100644 index 0000000000..70bee3372d --- /dev/null +++ b/spec/rubyspec/library/zlib/gzipreader/rewind_spec.rb @@ -0,0 +1,48 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'stringio' +require 'zlib' + +describe "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/rubyspec/library/zlib/gzipreader/shared/each.rb b/spec/rubyspec/library/zlib/gzipreader/shared/each.rb new file mode 100644 index 0000000000..47cd284b6a --- /dev/null +++ b/spec/rubyspec/library/zlib/gzipreader/shared/each.rb @@ -0,0 +1,51 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +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/rubyspec/library/zlib/gzipreader/tell_spec.rb b/spec/rubyspec/library/zlib/gzipreader/tell_spec.rb new file mode 100644 index 0000000000..6a4c1dadb4 --- /dev/null +++ b/spec/rubyspec/library/zlib/gzipreader/tell_spec.rb @@ -0,0 +1 @@ +require File.expand_path('../../../../spec_helper', __FILE__) diff --git a/spec/rubyspec/library/zlib/gzipreader/ungetc_spec.rb b/spec/rubyspec/library/zlib/gzipreader/ungetc_spec.rb new file mode 100644 index 0000000000..6a4c1dadb4 --- /dev/null +++ b/spec/rubyspec/library/zlib/gzipreader/ungetc_spec.rb @@ -0,0 +1 @@ +require File.expand_path('../../../../spec_helper', __FILE__) diff --git a/spec/rubyspec/library/zlib/gzipreader/unused_spec.rb b/spec/rubyspec/library/zlib/gzipreader/unused_spec.rb new file mode 100644 index 0000000000..6a4c1dadb4 --- /dev/null +++ b/spec/rubyspec/library/zlib/gzipreader/unused_spec.rb @@ -0,0 +1 @@ +require File.expand_path('../../../../spec_helper', __FILE__) |