diff options
Diffstat (limited to 'spec/ruby/core/string/chomp_spec.rb')
| -rw-r--r-- | spec/ruby/core/string/chomp_spec.rb | 113 |
1 files changed, 32 insertions, 81 deletions
diff --git a/spec/ruby/core/string/chomp_spec.rb b/spec/ruby/core/string/chomp_spec.rb index b3f64d7118..3a8550892f 100644 --- a/spec/ruby/core/string/chomp_spec.rb +++ b/spec/ruby/core/string/chomp_spec.rb @@ -1,4 +1,5 @@ # -*- encoding: utf-8 -*- +# frozen_string_literal: false require_relative '../../spec_helper' require_relative 'fixtures/classes' @@ -21,7 +22,7 @@ describe "String#chomp" do it "returns a copy of the String when it is not modified" do str = "abc" - str.chomp.should_not equal(str) + str.chomp.should_not.equal?(str) end it "removes one trailing newline" do @@ -40,21 +41,23 @@ describe "String#chomp" do "".chomp.should == "" end - ruby_version_is ''...'2.7' do - it "taints the result if self is tainted" do - "abc".taint.chomp.tainted?.should be_true - end + it "returns a String in the same encoding as self" do + "abc\n\n".encode("US-ASCII").chomp.encoding.should == Encoding::US_ASCII end - it "returns subclass instances when called on a subclass" do + it "returns String instances when called on a subclass" do str = StringSpecs::MyString.new("hello\n").chomp - str.should be_an_instance_of(StringSpecs::MyString) + str.should.instance_of?(String) end it "removes trailing characters that match $/ when it has been assigned a value" do $/ = "cdef" "abcdef".chomp.should == "ab" end + + it "removes one trailing newline for string with invalid encoding" do + "\xa0\xa1\n".chomp.should == "\xa0\xa1" + end end describe "when passed nil" do @@ -64,13 +67,7 @@ describe "String#chomp" do it "returns a copy of the String" do str = "abc" - str.chomp(nil).should_not equal(str) - end - - ruby_version_is ''...'2.7' do - it "taints the result if self is tainted" do - "abc".taint.chomp(nil).tainted?.should be_true - end + str.chomp(nil).should_not.equal?(str) end it "returns an empty String when self is empty" do @@ -99,15 +96,13 @@ describe "String#chomp" do "abc\r\n\r\n\r\n".chomp("").should == "abc" end - ruby_version_is ''...'2.7' do - it "taints the result if self is tainted" do - "abc".taint.chomp("").tainted?.should be_true - end - end - it "returns an empty String when self is empty" do "".chomp("").should == "" end + + it "removes one trailing newline for string with invalid encoding" do + "\xa0\xa1\n".chomp("").should == "\xa0\xa1" + end end describe "when passed '\\n'" do @@ -123,12 +118,6 @@ describe "String#chomp" do "abc\r\n\r\n".chomp("\n").should == "abc\r\n" end - ruby_version_is ''...'2.7' do - it "taints the result if self is tainted" do - "abc".taint.chomp("\n").tainted?.should be_true - end - end - it "returns an empty String when self is empty" do "".chomp("\n").should == "" end @@ -144,7 +133,7 @@ describe "String#chomp" do it "raises a TypeError if #to_str does not return a String" do arg = mock("string chomp") arg.should_receive(:to_str).and_return(1) - -> { "abc".chomp(arg) }.should raise_error(TypeError) + -> { "abc".chomp(arg) }.should.raise(TypeError) end end @@ -161,16 +150,6 @@ describe "String#chomp" do "".chomp("abc").should == "" end - ruby_version_is ''...'2.7' do - it "taints the result if self is tainted" do - "abc".taint.chomp("abc").tainted?.should be_true - end - - it "does not taint the result when the argument is tainted" do - "abc".chomp("abc".taint).tainted?.should be_false - end - end - it "returns an empty String when the argument equals self" do "abc".chomp("abc").should == "" end @@ -192,11 +171,11 @@ describe "String#chomp!" do it "modifies self" do str = "abc\n" - str.chomp!.should equal(str) + str.chomp!.should.equal?(str) end it "returns nil if self is not modified" do - "abc".chomp!.should be_nil + "abc".chomp!.should == nil end it "removes one trailing newline" do @@ -212,18 +191,12 @@ describe "String#chomp!" do end it "returns nil when self is empty" do - "".chomp!.should be_nil - end - - ruby_version_is ''...'2.7' do - it "taints the result if self is tainted" do - "abc\n".taint.chomp!.tainted?.should be_true - end + "".chomp!.should == nil end it "returns subclass instances when called on a subclass" do str = StringSpecs::MyString.new("hello\n").chomp! - str.should be_an_instance_of(StringSpecs::MyString) + str.should.instance_of?(StringSpecs::MyString) end it "removes trailing characters that match $/ when it has been assigned a value" do @@ -234,11 +207,11 @@ describe "String#chomp!" do describe "when passed nil" do it "returns nil" do - "abc\r\n".chomp!(nil).should be_nil + "abc\r\n".chomp!(nil).should == nil end it "returns nil when self is empty" do - "".chomp!(nil).should be_nil + "".chomp!(nil).should == nil end end @@ -252,7 +225,7 @@ describe "String#chomp!" do end it "does not remove a final carriage return" do - "abc\r".chomp!("").should be_nil + "abc\r".chomp!("").should == nil end it "removes more than one trailing newlines" do @@ -263,14 +236,8 @@ describe "String#chomp!" do "abc\r\n\r\n\r\n".chomp!("").should == "abc" end - ruby_version_is ''...'2.7' do - it "taints the result if self is tainted" do - "abc\n".taint.chomp!("").tainted?.should be_true - end - end - it "returns nil when self is empty" do - "".chomp!("").should be_nil + "".chomp!("").should == nil end end @@ -287,14 +254,8 @@ describe "String#chomp!" do "abc\r\n\r\n".chomp!("\n").should == "abc\r\n" end - ruby_version_is ''...'2.7' do - it "taints the result if self is tainted" do - "abc\n".taint.chomp!("\n").tainted?.should be_true - end - end - it "returns nil when self is empty" do - "".chomp!("\n").should be_nil + "".chomp!("\n").should == nil end end @@ -308,7 +269,7 @@ describe "String#chomp!" do it "raises a TypeError if #to_str does not return a String" do arg = mock("string chomp") arg.should_receive(:to_str).and_return(1) - -> { "abc".chomp!(arg) }.should raise_error(TypeError) + -> { "abc".chomp!(arg) }.should.raise(TypeError) end end @@ -318,21 +279,11 @@ describe "String#chomp!" do end it "returns nil if the argument does not match the trailing characters" do - "abc".chomp!("def").should be_nil + "abc".chomp!("def").should == nil end it "returns nil when self is empty" do - "".chomp!("abc").should be_nil - end - - ruby_version_is ''...'2.7' do - it "taints the result if self is tainted" do - "abc".taint.chomp!("abc").tainted?.should be_true - end - - it "does not taint the result when the argument is tainted" do - "abc".chomp!("abc".taint).tainted?.should be_false - end + "".chomp!("abc").should == nil end end @@ -340,15 +291,15 @@ describe "String#chomp!" do a = "string\n\r" a.freeze - -> { a.chomp! }.should raise_error(FrozenError) + -> { a.chomp! }.should.raise(FrozenError) end # see [ruby-core:23666] it "raises a FrozenError on a frozen instance when it would not be modified" do a = "string\n\r" a.freeze - -> { a.chomp!(nil) }.should raise_error(FrozenError) - -> { a.chomp!("x") }.should raise_error(FrozenError) + -> { a.chomp!(nil) }.should.raise(FrozenError) + -> { a.chomp!("x") }.should.raise(FrozenError) end end @@ -395,7 +346,7 @@ describe "String#chomp!" do end it "returns nil when the String is not modified" do - "あれ".chomp!.should be_nil + "あれ".chomp!.should == nil end it "removes the final carriage return, newline from a multibyte String" do |
