diff options
Diffstat (limited to 'spec/ruby/core/string/chomp_spec.rb')
| -rw-r--r-- | spec/ruby/core/string/chomp_spec.rb | 201 |
1 files changed, 90 insertions, 111 deletions
diff --git a/spec/ruby/core/string/chomp_spec.rb b/spec/ruby/core/string/chomp_spec.rb index 6fa8d7c6c5..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' @@ -6,11 +7,13 @@ describe "String#chomp" do describe "when passed no argument" do before do # Ensure that $/ is set to the default value + @verbose, $VERBOSE = $VERBOSE, nil @dollar_slash, $/ = $/, "\n" end after do $/ = @dollar_slash + $VERBOSE = @verbose end it "does not modify a String with no trailing carriage return or newline" do @@ -19,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 @@ -30,7 +33,7 @@ describe "String#chomp" do "abc\r\r".chomp.should == "abc\r" end - it "removes one trailing carrige return, newline pair" do + it "removes one trailing carriage return, newline pair" do "abc\r\n\r\n".chomp.should == "abc\r\n" end @@ -38,19 +41,23 @@ describe "String#chomp" do "".chomp.should == "" end - it "taints the result if self is tainted" do - "abc".taint.chomp.tainted?.should be_true + 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 @@ -60,11 +67,7 @@ describe "String#chomp" do it "returns a copy of the String" do str = "abc" - str.chomp(nil).should_not equal(str) - end - - it "taints the result if self is tainted" do - "abc".taint.chomp(nil).tainted?.should be_true + str.chomp(nil).should_not.equal?(str) end it "returns an empty String when self is empty" do @@ -93,13 +96,13 @@ describe "String#chomp" do "abc\r\n\r\n\r\n".chomp("").should == "abc" end - it "taints the result if self is tainted" do - "abc".taint.chomp("").tainted?.should be_true - 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 @@ -111,14 +114,10 @@ describe "String#chomp" do "abc\r\r".chomp("\n").should == "abc\r" end - it "removes one trailing carrige return, newline pair" do + it "removes one trailing carriage return, newline pair" do "abc\r\n\r\n".chomp("\n").should == "abc\r\n" end - it "taints the result if self is tainted" do - "abc".taint.chomp("\n").tainted?.should be_true - end - it "returns an empty String when self is empty" do "".chomp("\n").should == "" end @@ -134,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) - lambda { "abc".chomp(arg) }.should raise_error(TypeError) + -> { "abc".chomp(arg) }.should.raise(TypeError) end end @@ -151,12 +150,8 @@ describe "String#chomp" do "".chomp("abc").should == "" end - 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 + it "returns an empty String when the argument equals self" do + "abc".chomp("abc").should == "" end end end @@ -165,20 +160,22 @@ describe "String#chomp!" do describe "when passed no argument" do before do # Ensure that $/ is set to the default value + @verbose, $VERBOSE = $VERBOSE, nil @dollar_slash, $/ = $/, "\n" end after do $/ = @dollar_slash + $VERBOSE = @verbose end 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 @@ -189,21 +186,17 @@ describe "String#chomp!" do "abc\r\r".chomp!.should == "abc\r" end - it "removes one trailing carrige return, newline pair" do + it "removes one trailing carriage return, newline pair" do "abc\r\n\r\n".chomp!.should == "abc\r\n" end it "returns nil when self is empty" do - "".chomp!.should be_nil - end - - it "taints the result if self is tainted" do - "abc\n".taint.chomp!.tainted?.should be_true + "".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 @@ -214,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 @@ -232,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 @@ -243,12 +236,8 @@ describe "String#chomp!" do "abc\r\n\r\n\r\n".chomp!("").should == "abc" end - it "taints the result if self is tainted" do - "abc\n".taint.chomp!("").tainted?.should be_true - end - it "returns nil when self is empty" do - "".chomp!("").should be_nil + "".chomp!("").should == nil end end @@ -261,16 +250,12 @@ describe "String#chomp!" do "abc\r\r".chomp!("\n").should == "abc\r" end - it "removes one trailing carrige return, newline pair" do + it "removes one trailing carriage return, newline pair" do "abc\r\n\r\n".chomp!("\n").should == "abc\r\n" end - it "taints the result if self is tainted" do - "abc\n".taint.chomp!("\n").tainted?.should be_true - end - it "returns nil when self is empty" do - "".chomp!("\n").should be_nil + "".chomp!("\n").should == nil end end @@ -284,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) - lambda { "abc".chomp!(arg) }.should raise_error(TypeError) + -> { "abc".chomp!(arg) }.should.raise(TypeError) end end @@ -294,94 +279,88 @@ 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 - - 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 + "".chomp!("abc").should == nil end end - it "raises a #{frozen_error_class} on a frozen instance when it is modified" do + it "raises a FrozenError on a frozen instance when it is modified" do a = "string\n\r" a.freeze - lambda { a.chomp! }.should raise_error(frozen_error_class) + -> { a.chomp! }.should.raise(FrozenError) end # see [ruby-core:23666] - it "raises a #{frozen_error_class} on a frozen instance when it would not be modified" do + it "raises a FrozenError on a frozen instance when it would not be modified" do a = "string\n\r" a.freeze - lambda { a.chomp!(nil) }.should raise_error(frozen_error_class) - lambda { a.chomp!("x") }.should raise_error(frozen_error_class) + -> { a.chomp!(nil) }.should.raise(FrozenError) + -> { a.chomp!("x") }.should.raise(FrozenError) end end -with_feature :encoding do - describe "String#chomp" do - before :each do - @before_separator = $/ - end +describe "String#chomp" do + before :each do + @verbose, $VERBOSE = $VERBOSE, nil + @before_separator = $/ + end - after :each do - $/ = @before_separator - end + after :each do + $/ = @before_separator + $VERBOSE = @verbose + end - it "does not modify a multi-byte character" do - "あれ".chomp.should == "あれ" - end + it "does not modify a multi-byte character" do + "あれ".chomp.should == "あれ" + end - it "removes the final carriage return, newline from a multibyte String" do - "あれ\r\n".chomp.should == "あれ" - end + it "removes the final carriage return, newline from a multibyte String" do + "あれ\r\n".chomp.should == "あれ" + end - it "removes the final carriage return, newline from a non-ASCII String" do - str = "abc\r\n".encode "utf-32be" - str.chomp.should == "abc".encode("utf-32be") - end + it "removes the final carriage return, newline from a non-ASCII String" do + str = "abc\r\n".encode "utf-32be" + str.chomp.should == "abc".encode("utf-32be") + end - it "removes the final carriage return, newline from a non-ASCII String when the record separator is changed" do - $/ = "\n".encode("utf-8") - str = "abc\r\n".encode "utf-32be" - str.chomp.should == "abc".encode("utf-32be") - end + it "removes the final carriage return, newline from a non-ASCII String when the record separator is changed" do + $/ = "\n".encode("utf-8") + str = "abc\r\n".encode "utf-32be" + str.chomp.should == "abc".encode("utf-32be") end +end - describe "String#chomp!" do - before :each do - @before_separator = $/ - end +describe "String#chomp!" do + before :each do + @verbose, $VERBOSE = $VERBOSE, nil + @before_separator = $/ + end - after :each do - $/ = @before_separator - end + after :each do + $/ = @before_separator + $VERBOSE = @verbose + end - it "returns nil when the String is not modified" do - "あれ".chomp!.should be_nil - end + it "returns nil when the String is not modified" do + "あれ".chomp!.should == nil + end - it "removes the final carriage return, newline from a multibyte String" do - "あれ\r\n".chomp!.should == "あれ" - end + it "removes the final carriage return, newline from a multibyte String" do + "あれ\r\n".chomp!.should == "あれ" + end - it "removes the final carriage return, newline from a non-ASCII String" do - str = "abc\r\n".encode "utf-32be" - str.chomp!.should == "abc".encode("utf-32be") - end + it "removes the final carriage return, newline from a non-ASCII String" do + str = "abc\r\n".encode "utf-32be" + str.chomp!.should == "abc".encode("utf-32be") + end - it "removes the final carriage return, newline from a non-ASCII String when the record separator is changed" do - $/ = "\n".encode("utf-8") - str = "abc\r\n".encode "utf-32be" - str.chomp!.should == "abc".encode("utf-32be") - end + it "removes the final carriage return, newline from a non-ASCII String when the record separator is changed" do + $/ = "\n".encode("utf-8") + str = "abc\r\n".encode "utf-32be" + str.chomp!.should == "abc".encode("utf-32be") end end |
