aboutsummaryrefslogtreecommitdiffstats
path: root/spec/ruby/core/string/chop_spec.rb
diff options
context:
space:
mode:
authoreregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-09-20 20:18:52 +0000
committereregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-09-20 20:18:52 +0000
commitc530d0faf064d561bf7755c55a34921572d343ef (patch)
treea3785a79899302bc149e4a6e72f624ac27dc1f10 /spec/ruby/core/string/chop_spec.rb
parent77eb1c94ee74f11ebf347f0539d3949b2cf4e2b3 (diff)
downloadruby-c530d0faf064d561bf7755c55a34921572d343ef.tar.gz
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
Diffstat (limited to 'spec/ruby/core/string/chop_spec.rb')
-rw-r--r--spec/ruby/core/string/chop_spec.rb128
1 files changed, 128 insertions, 0 deletions
diff --git a/spec/ruby/core/string/chop_spec.rb b/spec/ruby/core/string/chop_spec.rb
new file mode 100644
index 0000000000..4e9a39f866
--- /dev/null
+++ b/spec/ruby/core/string/chop_spec.rb
@@ -0,0 +1,128 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/classes.rb', __FILE__)
+
+describe "String#chop" do
+ it "removes the final character" do
+ "abc".chop.should == "ab"
+ end
+
+ it "removes the final carriage return" do
+ "abc\r".chop.should == "abc"
+ end
+
+ it "removes the final newline" do
+ "abc\n".chop.should == "abc"
+ end
+
+ it "removes the final carriage return, newline" do
+ "abc\r\n".chop.should == "abc"
+ end
+
+ it "removes the carrige return, newline if they are the only characters" do
+ "\r\n".chop.should == ""
+ end
+
+ it "does not remove more than the final carriage return, newline" do
+ "abc\r\n\r\n".chop.should == "abc\r\n"
+ end
+
+ with_feature :encoding do
+ it "removes a multi-byte character" do
+ "あれ".chop.should == "あ"
+ end
+
+ it "removes the final carriage return, newline from a multibyte String" do
+ "あれ\r\n".chop.should == "あれ"
+ end
+
+ it "removes the final carriage return, newline from a non-ASCII String" do
+ str = "abc\r\n".encode "utf-32be"
+ str.chop.should == "abc".encode("utf-32be")
+ end
+ end
+
+ it "returns an empty string when applied to an empty string" do
+ "".chop.should == ""
+ end
+
+ it "returns a new string when applied to an empty string" do
+ s = ""
+ s.chop.should_not equal(s)
+ end
+
+ it "taints result when self is tainted" do
+ "hello".taint.chop.tainted?.should == true
+ "".taint.chop.tainted?.should == true
+ end
+
+ it "untrusts result when self is untrusted" do
+ "hello".untrust.chop.untrusted?.should == true
+ "".untrust.chop.untrusted?.should == true
+ end
+
+ it "returns subclass instances when called on a subclass" do
+ StringSpecs::MyString.new("hello\n").chop.should be_an_instance_of(StringSpecs::MyString)
+ end
+end
+
+describe "String#chop!" do
+ it "removes the final character" do
+ "abc".chop!.should == "ab"
+ end
+
+ it "removes the final carriage return" do
+ "abc\r".chop!.should == "abc"
+ end
+
+ it "removes the final newline" do
+ "abc\n".chop!.should == "abc"
+ end
+
+ it "removes the final carriage return, newline" do
+ "abc\r\n".chop!.should == "abc"
+ end
+
+ it "removes the carrige return, newline if they are the only characters" do
+ "\r\n".chop!.should == ""
+ end
+
+ it "does not remove more than the final carriage return, newline" do
+ "abc\r\n\r\n".chop!.should == "abc\r\n"
+ end
+
+ with_feature :encoding do
+ it "removes a multi-byte character" do
+ "あれ".chop!.should == "あ"
+ end
+
+ it "removes the final carriage return, newline from a multibyte String" do
+ "あれ\r\n".chop!.should == "あれ"
+ end
+
+ it "removes the final carriage return, newline from a non-ASCII String" do
+ str = "abc\r\n".encode "utf-32be"
+ str.chop!.should == "abc".encode("utf-32be")
+ end
+ end
+
+ it "returns self if modifications were made" do
+ str = "hello"
+ str.chop!.should equal(str)
+ end
+
+ it "returns nil when called on an empty string" do
+ "".chop!.should be_nil
+ end
+
+ it "raises a RuntimeError on a frozen instance that is modified" do
+ lambda { "string\n\r".freeze.chop! }.should raise_error(RuntimeError)
+ end
+
+ # see [ruby-core:23666]
+ it "raises a RuntimeError on a frozen instance that would not be modified" do
+ a = ""
+ a.freeze
+ lambda { a.chop! }.should raise_error(RuntimeError)
+ end
+end