summaryrefslogtreecommitdiff
path: root/spec/ruby/core/string/chomp_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/string/chomp_spec.rb')
-rw-r--r--spec/ruby/core/string/chomp_spec.rb79
1 files changed, 28 insertions, 51 deletions
diff --git a/spec/ruby/core/string/chomp_spec.rb b/spec/ruby/core/string/chomp_spec.rb
index 7400d71336..d27c84c6f6 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
@@ -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 be_an_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
@@ -63,10 +70,6 @@ describe "String#chomp" do
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
- end
-
it "returns an empty String when self is empty" do
"".chomp(nil).should == ""
end
@@ -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
@@ -115,10 +118,6 @@ describe "String#chomp" 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_error(TypeError)
end
end
@@ -151,14 +150,6 @@ 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
- end
-
it "returns an empty String when the argument equals self" do
"abc".chomp("abc").should == ""
end
@@ -169,11 +160,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 "modifies self" do
@@ -201,10 +194,6 @@ describe "String#chomp!" do
"".chomp!.should be_nil
end
- it "taints the result if self is tainted" do
- "abc\n".taint.chomp!.tainted?.should be_true
- 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)
@@ -247,10 +236,6 @@ 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
end
@@ -269,10 +254,6 @@ describe "String#chomp!" 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
end
@@ -288,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_error(TypeError)
end
end
@@ -304,39 +285,33 @@ describe "String#chomp!" do
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
- 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_error(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_error(FrozenError)
+ -> { a.chomp!("x") }.should raise_error(FrozenError)
end
end
describe "String#chomp" do
before :each do
+ @verbose, $VERBOSE = $VERBOSE, nil
@before_separator = $/
end
after :each do
$/ = @before_separator
+ $VERBOSE = @verbose
end
it "does not modify a multi-byte character" do
@@ -361,11 +336,13 @@ end
describe "String#chomp!" do
before :each do
+ @verbose, $VERBOSE = $VERBOSE, nil
@before_separator = $/
end
after :each do
$/ = @before_separator
+ $VERBOSE = @verbose
end
it "returns nil when the String is not modified" do