2017-05-07 08:04:49 -04:00
|
|
|
# -*- encoding: utf-8 -*-
|
2018-03-04 10:09:32 -05:00
|
|
|
require_relative '../../spec_helper'
|
|
|
|
require_relative 'fixtures/classes'
|
2017-05-07 08:04:49 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-02-07 11:35:33 -05:00
|
|
|
it "removes the carriage return, newline if they are the only characters" do
|
2017-05-07 08:04:49 -04:00
|
|
|
"\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
|
|
|
|
|
2019-04-28 17:20:11 -04:00
|
|
|
it "removes a multi-byte character" do
|
|
|
|
"あれ".chop.should == "あ"
|
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
|
2019-04-28 17:20:11 -04:00
|
|
|
it "removes the final carriage return, newline from a multibyte String" do
|
|
|
|
"あれ\r\n".chop.should == "あれ"
|
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
|
2019-04-28 17:20:11 -04:00
|
|
|
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")
|
2017-05-07 08:04:49 -04:00
|
|
|
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
|
|
|
|
|
2019-09-24 23:59:12 -04:00
|
|
|
ruby_version_is ''...'2.7' do
|
|
|
|
it "taints result when self is tainted" do
|
2020-05-03 06:28:29 -04:00
|
|
|
"hello".taint.chop.should.tainted?
|
|
|
|
"".taint.chop.should.tainted?
|
2019-09-24 23:59:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "untrusts result when self is untrusted" do
|
2020-05-03 06:28:29 -04:00
|
|
|
"hello".untrust.chop.should.untrusted?
|
|
|
|
"".untrust.chop.should.untrusted?
|
2019-09-24 23:59:12 -04:00
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
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
|
|
|
|
|
2019-02-07 11:35:33 -05:00
|
|
|
it "removes the carriage return, newline if they are the only characters" do
|
2017-05-07 08:04:49 -04:00
|
|
|
"\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
|
|
|
|
|
2019-04-28 17:20:11 -04:00
|
|
|
it "removes a multi-byte character" do
|
|
|
|
"あれ".chop!.should == "あ"
|
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
|
2019-04-28 17:20:11 -04:00
|
|
|
it "removes the final carriage return, newline from a multibyte String" do
|
|
|
|
"あれ\r\n".chop!.should == "あれ"
|
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
|
2019-04-28 17:20:11 -04:00
|
|
|
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")
|
2017-05-07 08:04:49 -04:00
|
|
|
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
|
|
|
|
|
2020-02-08 21:07:01 -05:00
|
|
|
it "raises a FrozenError on a frozen instance that is modified" do
|
|
|
|
-> { "string\n\r".freeze.chop! }.should raise_error(FrozenError)
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# see [ruby-core:23666]
|
2020-02-08 21:07:01 -05:00
|
|
|
it "raises a FrozenError on a frozen instance that would not be modified" do
|
2017-05-07 08:04:49 -04:00
|
|
|
a = ""
|
|
|
|
a.freeze
|
2020-02-08 21:07:01 -05:00
|
|
|
-> { a.chop! }.should raise_error(FrozenError)
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
end
|