From 1d15d5f08032acf1b7bceacbb450d617ff6e0931 Mon Sep 17 00:00:00 2001 From: eregon Date: Wed, 20 Sep 2017 20:18:52 +0000 Subject: Move spec/rubyspec to spec/ruby for consistency * Other ruby implementations use the spec/ruby directory. [Misc #13792] [ruby-core:82287] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- spec/ruby/library/stringio/shared/codepoints.rb | 45 +++++++++ spec/ruby/library/stringio/shared/each.rb | 105 ++++++++++++++++++++ spec/ruby/library/stringio/shared/each_byte.rb | 48 ++++++++++ spec/ruby/library/stringio/shared/each_char.rb | 36 +++++++ spec/ruby/library/stringio/shared/eof.rb | 24 +++++ spec/ruby/library/stringio/shared/getc.rb | 43 +++++++++ spec/ruby/library/stringio/shared/isatty.rb | 5 + spec/ruby/library/stringio/shared/length.rb | 5 + spec/ruby/library/stringio/shared/read.rb | 121 ++++++++++++++++++++++++ spec/ruby/library/stringio/shared/readchar.rb | 29 ++++++ spec/ruby/library/stringio/shared/sysread.rb | 15 +++ spec/ruby/library/stringio/shared/tell.rb | 12 +++ spec/ruby/library/stringio/shared/write.rb | 87 +++++++++++++++++ 13 files changed, 575 insertions(+) create mode 100644 spec/ruby/library/stringio/shared/codepoints.rb create mode 100644 spec/ruby/library/stringio/shared/each.rb create mode 100644 spec/ruby/library/stringio/shared/each_byte.rb create mode 100644 spec/ruby/library/stringio/shared/each_char.rb create mode 100644 spec/ruby/library/stringio/shared/eof.rb create mode 100644 spec/ruby/library/stringio/shared/getc.rb create mode 100644 spec/ruby/library/stringio/shared/isatty.rb create mode 100644 spec/ruby/library/stringio/shared/length.rb create mode 100644 spec/ruby/library/stringio/shared/read.rb create mode 100644 spec/ruby/library/stringio/shared/readchar.rb create mode 100644 spec/ruby/library/stringio/shared/sysread.rb create mode 100644 spec/ruby/library/stringio/shared/tell.rb create mode 100644 spec/ruby/library/stringio/shared/write.rb (limited to 'spec/ruby/library/stringio/shared') diff --git a/spec/ruby/library/stringio/shared/codepoints.rb b/spec/ruby/library/stringio/shared/codepoints.rb new file mode 100644 index 0000000000..c8ca03329f --- /dev/null +++ b/spec/ruby/library/stringio/shared/codepoints.rb @@ -0,0 +1,45 @@ +# -*- encoding: utf-8 -*- +describe :stringio_codepoints, shared: true do + before :each do + @io = StringIO.new("∂φ/∂x = gaîté") + @enum = @io.send(@method) + end + + it "returns an Enumerator" do + @enum.should be_an_instance_of(Enumerator) + end + + it "yields each codepoint code in turn" do + @enum.to_a.should == [8706, 966, 47, 8706, 120, 32, 61, 32, 103, 97, 238, 116, 233] + end + + it "yields each codepoint starting from the current position" do + @io.pos = 15 + @enum.to_a.should == [238, 116, 233] + end + + it "raises an error if reading invalid sequence" do + @io.pos = 1 # inside of a multibyte sequence + lambda { @enum.first }.should raise_error(ArgumentError) + end + + it "raises an IOError if not readable" do + @io.close_read + lambda { @enum.to_a }.should raise_error(IOError) + + io = StringIO.new("xyz", "w") + lambda { io.send(@method).to_a }.should raise_error(IOError) + end + + + it "calls the given block" do + r = [] + @io.send(@method){|c| r << c } + r.should == [8706, 966, 47, 8706, 120, 32, 61, 32, 103, 97, 238, 116, 233] + end + + it "returns self" do + @io.send(@method) {|l| l }.should equal(@io) + end + +end diff --git a/spec/ruby/library/stringio/shared/each.rb b/spec/ruby/library/stringio/shared/each.rb new file mode 100644 index 0000000000..0fde23634e --- /dev/null +++ b/spec/ruby/library/stringio/shared/each.rb @@ -0,0 +1,105 @@ +describe :stringio_each_separator, shared: true do + before :each do + @io = StringIO.new("a b c d e\n1 2 3 4 5") + end + + it "uses the passed argument as the line separator" do + seen = [] + @io.send(@method, " ") {|s| seen << s} + seen.should == ["a ", "b ", "c ", "d ", "e\n1 ", "2 ", "3 ", "4 ", "5"] + end + + it "does not change $_" do + $_ = "test" + @io.send(@method, " ") { |s| s} + $_.should == "test" + end + + it "returns self" do + @io.send(@method) {|l| l }.should equal(@io) + end + + it "tries to convert the passed separator to a String using #to_str" do + obj = mock("to_str") + obj.stub!(:to_str).and_return(" ") + + seen = [] + @io.send(@method, obj) { |l| seen << l } + seen.should == ["a ", "b ", "c ", "d ", "e\n1 ", "2 ", "3 ", "4 ", "5"] + end + + it "yields self's content starting from the current position when the passed separator is nil" do + seen = [] + io = StringIO.new("1 2 1 2 1 2") + io.pos = 2 + io.send(@method, nil) {|s| seen << s} + seen.should == ["2 1 2 1 2"] + end + + it "yields each paragraph when passed an empty String as separator" do + seen = [] + io = StringIO.new("para1\n\npara2\n\n\npara3") + io.send(@method, "") {|s| seen << s} + seen.should == ["para1\n\n", "para2\n\n", "para3"] + end +end + +describe :stringio_each_no_arguments, shared: true do + before :each do + @io = StringIO.new("a b c d e\n1 2 3 4 5") + end + + it "yields each line to the passed block" do + seen = [] + @io.send(@method) {|s| seen << s } + seen.should == ["a b c d e\n", "1 2 3 4 5"] + end + + it "yields each line starting from the current position" do + seen = [] + @io.pos = 4 + @io.send(@method) {|s| seen << s } + seen.should == ["c d e\n", "1 2 3 4 5"] + end + + it "does not change $_" do + $_ = "test" + @io.send(@method) { |s| s} + $_.should == "test" + end + + it "uses $/ as the default line separator" do + seen = [] + begin + old_rs, $/ = $/, " " + @io.send(@method) {|s| seen << s } + seen.should eql(["a ", "b ", "c ", "d ", "e\n1 ", "2 ", "3 ", "4 ", "5"]) + ensure + $/ = old_rs + end + end + + it "returns self" do + @io.send(@method) {|l| l }.should equal(@io) + end + + it "returns an Enumerator when passed no block" do + enum = @io.send(@method) + enum.instance_of?(Enumerator).should be_true + + seen = [] + enum.each { |b| seen << b } + seen.should == ["a b c d e\n", "1 2 3 4 5"] + end +end + +describe :stringio_each_not_readable, shared: true do + it "raises an IOError" do + io = StringIO.new("a b c d e", "w") + lambda { io.send(@method) { |b| b } }.should raise_error(IOError) + + io = StringIO.new("a b c d e") + io.close_read + lambda { io.send(@method) { |b| b } }.should raise_error(IOError) + end +end diff --git a/spec/ruby/library/stringio/shared/each_byte.rb b/spec/ruby/library/stringio/shared/each_byte.rb new file mode 100644 index 0000000000..1dc48ee437 --- /dev/null +++ b/spec/ruby/library/stringio/shared/each_byte.rb @@ -0,0 +1,48 @@ +describe :stringio_each_byte, shared: true do + before :each do + @io = StringIO.new("xyz") + end + + it "yields each character code in turn" do + seen = [] + @io.send(@method) { |b| seen << b } + seen.should == [120, 121, 122] + end + + it "updates the position before each yield" do + seen = [] + @io.send(@method) { |b| seen << @io.pos } + seen.should == [1, 2, 3] + end + + it "does not yield if the current position is out of bounds" do + @io.pos = 1000 + seen = nil + @io.send(@method) { |b| seen = b } + seen.should be_nil + end + + it "returns self" do + @io.send(@method) {}.should equal(@io) + end + + it "returns an Enumerator when passed no block" do + enum = @io.send(@method) + enum.instance_of?(Enumerator).should be_true + + seen = [] + enum.each { |b| seen << b } + seen.should == [120, 121, 122] + end +end + +describe :stringio_each_byte_not_readable, shared: true do + it "raises an IOError" do + io = StringIO.new("xyz", "w") + lambda { io.send(@method) { |b| b } }.should raise_error(IOError) + + io = StringIO.new("xyz") + io.close_read + lambda { io.send(@method) { |b| b } }.should raise_error(IOError) + end +end diff --git a/spec/ruby/library/stringio/shared/each_char.rb b/spec/ruby/library/stringio/shared/each_char.rb new file mode 100644 index 0000000000..35efdcb749 --- /dev/null +++ b/spec/ruby/library/stringio/shared/each_char.rb @@ -0,0 +1,36 @@ +# -*- encoding: utf-8 -*- +describe :stringio_each_char, shared: true do + before :each do + @io = StringIO.new("xyz äöü") + end + + it "yields each character code in turn" do + seen = [] + @io.send(@method) { |c| seen << c } + seen.should == ["x", "y", "z", " ", "ä", "ö", "ü"] + end + + it "returns self" do + @io.send(@method) {}.should equal(@io) + end + + it "returns an Enumerator when passed no block" do + enum = @io.send(@method) + enum.instance_of?(Enumerator).should be_true + + seen = [] + enum.each { |c| seen << c } + seen.should == ["x", "y", "z", " ", "ä", "ö", "ü"] + end +end + +describe :stringio_each_char_not_readable, shared: true do + it "raises an IOError" do + io = StringIO.new("xyz", "w") + lambda { io.send(@method) { |b| b } }.should raise_error(IOError) + + io = StringIO.new("xyz") + io.close_read + lambda { io.send(@method) { |b| b } }.should raise_error(IOError) + end +end diff --git a/spec/ruby/library/stringio/shared/eof.rb b/spec/ruby/library/stringio/shared/eof.rb new file mode 100644 index 0000000000..e0368a2892 --- /dev/null +++ b/spec/ruby/library/stringio/shared/eof.rb @@ -0,0 +1,24 @@ +describe :stringio_eof, shared: true do + before :each do + @io = StringIO.new("eof") + end + + it "returns true when self's position is greater than or equal to self's size" do + @io.pos = 3 + @io.send(@method).should be_true + + @io.pos = 6 + @io.send(@method).should be_true + end + + it "returns false when self's position is less than self's size" do + @io.pos = 0 + @io.send(@method).should be_false + + @io.pos = 1 + @io.send(@method).should be_false + + @io.pos = 2 + @io.send(@method).should be_false + end +end diff --git a/spec/ruby/library/stringio/shared/getc.rb b/spec/ruby/library/stringio/shared/getc.rb new file mode 100644 index 0000000000..3e064f9c1e --- /dev/null +++ b/spec/ruby/library/stringio/shared/getc.rb @@ -0,0 +1,43 @@ +describe :stringio_getc, shared: true do + before :each do + @io = StringIO.new("example") + end + + it "increases self's position by one" do + @io.send(@method) + @io.pos.should eql(1) + + @io.send(@method) + @io.pos.should eql(2) + + @io.send(@method) + @io.pos.should eql(3) + end + + it "returns nil when called at the end of self" do + @io.pos = 7 + @io.send(@method).should be_nil + @io.send(@method).should be_nil + @io.send(@method).should be_nil + end + + it "does not increase self's position when called at the end of file" do + @io.pos = 7 + @io.send(@method) + @io.pos.should eql(7) + + @io.send(@method) + @io.pos.should eql(7) + end +end + +describe :stringio_getc_not_readable, shared: true do + it "raises an IOError" do + io = StringIO.new("xyz", "w") + lambda { io.send(@method) }.should raise_error(IOError) + + io = StringIO.new("xyz") + io.close_read + lambda { io.send(@method) }.should raise_error(IOError) + end +end diff --git a/spec/ruby/library/stringio/shared/isatty.rb b/spec/ruby/library/stringio/shared/isatty.rb new file mode 100644 index 0000000000..3da5999953 --- /dev/null +++ b/spec/ruby/library/stringio/shared/isatty.rb @@ -0,0 +1,5 @@ +describe :stringio_isatty, shared: true do + it "returns false" do + StringIO.new('tty').send(@method).should be_false + end +end diff --git a/spec/ruby/library/stringio/shared/length.rb b/spec/ruby/library/stringio/shared/length.rb new file mode 100644 index 0000000000..60a4eb1bdd --- /dev/null +++ b/spec/ruby/library/stringio/shared/length.rb @@ -0,0 +1,5 @@ +describe :stringio_length, shared: true do + it "returns the length of the wrapped string" do + StringIO.new("example").send(@method).should == 7 + end +end diff --git a/spec/ruby/library/stringio/shared/read.rb b/spec/ruby/library/stringio/shared/read.rb new file mode 100644 index 0000000000..025829a2b1 --- /dev/null +++ b/spec/ruby/library/stringio/shared/read.rb @@ -0,0 +1,121 @@ +describe :stringio_read, shared: true do + before :each do + @io = StringIO.new("example") + end + + it "returns the passed buffer String" do + # Note: Rubinius bug: + # @io.send(@method, 7, buffer = "").should equal(buffer) + ret = @io.send(@method, 7, buffer = "") + ret.should equal(buffer) + end + + it "reads length bytes and writes them to the buffer String" do + @io.send(@method, 7, buffer = "") + buffer.should == "example" + end + + it "tries to convert the passed buffer Object to a String using #to_str" do + obj = mock("to_str") + obj.should_receive(:to_str).and_return(buffer = "") + + @io.send(@method, 7, obj) + buffer.should == "example" + end + + it "raises a TypeError when the passed buffer Object can't be converted to a String" do + lambda { @io.send(@method, 7, Object.new) }.should raise_error(TypeError) + end + + it "raises an error when passed a frozen String as buffer" do + lambda { @io.send(@method, 7, "".freeze) }.should raise_error(RuntimeError) + end +end + +describe :stringio_read_length, shared: true do + before :each do + @io = StringIO.new("example") + end + + it "reads length bytes from the current position and returns them" do + @io.pos = 3 + @io.send(@method, 4).should == "mple" + end + + it "reads at most the whole content" do + @io.send(@method, 999).should == "example" + end + + it "correctly updates the position" do + @io.send(@method, 3) + @io.pos.should eql(3) + + @io.send(@method, 999) + @io.pos.should eql(7) + end + + it "tries to convert the passed length to an Integer using #to_int" do + obj = mock("to_int") + obj.should_receive(:to_int).and_return(7) + @io.send(@method, obj).should == "example" + end + + it "raises a TypeError when the passed length can't be converted to an Integer" do + lambda { @io.send(@method, Object.new) }.should raise_error(TypeError) + end + + it "raises a TypeError when the passed length is negative" do + lambda { @io.send(@method, -2) }.should raise_error(ArgumentError) + end + + it "returns a binary String" do + @io.send(@method, 4).encoding.should == Encoding::ASCII_8BIT + end +end + +describe :stringio_read_no_arguments, shared: true do + before :each do + @io = StringIO.new("example") + end + + it "reads the whole content starting from the current position" do + @io.send(@method).should == "example" + + @io.pos = 3 + @io.send(@method).should == "mple" + end + + it "correctly updates the current position" do + @io.send(@method) + @io.pos.should eql(7) + end +end + +describe :stringio_read_nil, shared: true do + before :each do + @io = StringIO.new("example") + end + + it "returns the remaining content from the current position" do + @io.send(@method, nil).should == "example" + + @io.pos = 4 + @io.send(@method, nil).should == "ple" + end + + it "updates the current position" do + @io.send(@method, nil) + @io.pos.should eql(7) + end +end + +describe :stringio_read_not_readable, shared: true do + it "raises an IOError" do + io = StringIO.new("test", "w") + lambda { io.send(@method) }.should raise_error(IOError) + + io = StringIO.new("test") + io.close_read + lambda { io.send(@method) }.should raise_error(IOError) + end +end diff --git a/spec/ruby/library/stringio/shared/readchar.rb b/spec/ruby/library/stringio/shared/readchar.rb new file mode 100644 index 0000000000..19194f0680 --- /dev/null +++ b/spec/ruby/library/stringio/shared/readchar.rb @@ -0,0 +1,29 @@ +describe :stringio_readchar, shared: true do + before :each do + @io = StringIO.new("example") + end + + it "correctly updates the current position" do + @io.send(@method) + @io.pos.should == 1 + + @io.send(@method) + @io.pos.should == 2 + end + + it "raises an EOFError when self is at the end" do + @io.pos = 7 + lambda { @io.send(@method) }.should raise_error(EOFError) + end +end + +describe :stringio_readchar_not_readable, shared: true do + it "raises an IOError" do + io = StringIO.new("a b c d e", "w") + lambda { io.send(@method) }.should raise_error(IOError) + + io = StringIO.new("a b c d e") + io.close_read + lambda { io.send(@method) }.should raise_error(IOError) + end +end diff --git a/spec/ruby/library/stringio/shared/sysread.rb b/spec/ruby/library/stringio/shared/sysread.rb new file mode 100644 index 0000000000..9800b2339b --- /dev/null +++ b/spec/ruby/library/stringio/shared/sysread.rb @@ -0,0 +1,15 @@ +describe :stringio_sysread_length, :shared => true do + before :each do + @io = StringIO.new("example") + end + + it "returns an empty String when passed 0 and no data remains" do + @io.send(@method, 8).should == "example" + @io.send(@method, 0).should == "" + end + + it "raises an EOFError when passed length > 0 and no data remains" do + @io.read.should == "example" + lambda { @io.sysread(1) }.should raise_error(EOFError) + end +end diff --git a/spec/ruby/library/stringio/shared/tell.rb b/spec/ruby/library/stringio/shared/tell.rb new file mode 100644 index 0000000000..852c51c192 --- /dev/null +++ b/spec/ruby/library/stringio/shared/tell.rb @@ -0,0 +1,12 @@ +describe :stringio_tell, shared: true do + before :each do + @io = StringIOSpecs.build + end + + it "returns the current byte offset" do + @io.getc + @io.send(@method).should == 1 + @io.read(7) + @io.send(@method).should == 8 + end +end diff --git a/spec/ruby/library/stringio/shared/write.rb b/spec/ruby/library/stringio/shared/write.rb new file mode 100644 index 0000000000..bcb548bbd0 --- /dev/null +++ b/spec/ruby/library/stringio/shared/write.rb @@ -0,0 +1,87 @@ +describe :stringio_write, shared: true do + before :each do + @io = StringIO.new('12345') + end + + it "tries to convert the passed Object to a String using #to_s" do + obj = mock("to_s") + obj.should_receive(:to_s).and_return("to_s") + @io.send(@method, obj) + @io.string.should == "to_s5" + end +end + +describe :stringio_write_string, shared: true do + before :each do + @io = StringIO.new('12345') + end + + # TODO: RDoc says that #write appends at the current position. + it "writes the passed String at the current buffer position" do + @io.pos = 2 + @io.send(@method, 'x').should == 1 + @io.string.should == '12x45' + @io.send(@method, 7).should == 1 + @io.string.should == '12x75' + end + + it "pads self with \\000 when the current position is after the end" do + @io.pos = 8 + @io.send(@method, 'x') + @io.string.should == "12345\000\000\000x" + @io.send(@method, 9) + @io.string.should == "12345\000\000\000x9" + end + + it "returns the number of bytes written" do + @io.send(@method, '').should == 0 + @io.send(@method, nil).should == 0 + str = "1" * 100 + @io.send(@method, str).should == 100 + end + + it "updates self's position" do + @io.send(@method, 'test') + @io.pos.should eql(4) + end + + it "taints self's String when the passed argument is tainted" do + @io.send(@method, "test".taint) + @io.string.tainted?.should be_true + end + + it "does not taint self when the passed argument is tainted" do + @io.send(@method, "test".taint) + @io.tainted?.should be_false + end +end + +describe :stringio_write_not_writable, shared: true do + it "raises an IOError" do + io = StringIO.new("test", "r") + lambda { io.send(@method, "test") }.should raise_error(IOError) + + io = StringIO.new("test") + io.close_write + lambda { io.send(@method, "test") }.should raise_error(IOError) + end +end + +describe :stringio_write_append, shared: true do + before :each do + @io = StringIO.new("example", "a") + end + + it "appends the passed argument to the end of self" do + @io.send(@method, ", just testing") + @io.string.should == "example, just testing" + + @io.send(@method, " and more testing") + @io.string.should == "example, just testing and more testing" + end + + it "correctly updates self's position" do + @io.send(@method, ", testing") + @io.pos.should eql(16) + end +end -- cgit v1.2.3