1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
# -*- 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
ruby_version_is "1.9" do
it "untrusts result when self is untrusted" do
"hello".untrust.chop.untrusted?.should == true
"".untrust.chop.untrusted?.should == true
end
end
it "returns subclass instances when called on a subclass" do
StringSpecs::MyString.new("hello\n").chop.should be_kind_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
ruby_version_is ""..."1.9" do
it "raises a TypeError when self is frozen" do
lambda { "string\n\r".freeze.chop! }.should raise_error(TypeError)
end
it "does not raise an exception on a frozen instance that would not be modified" do
"".freeze.chop!.should be_nil
end
end
ruby_version_is "1.9" do
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
end
|