summaryrefslogtreecommitdiff
path: root/spec/ruby/core/string/tr_s_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/string/tr_s_spec.rb')
-rw-r--r--spec/ruby/core/string/tr_s_spec.rb105
1 files changed, 50 insertions, 55 deletions
diff --git a/spec/ruby/core/string/tr_s_spec.rb b/spec/ruby/core/string/tr_s_spec.rb
index ea2ffa71b9..22a193ec4b 100644
--- a/spec/ruby/core/string/tr_s_spec.rb
+++ b/spec/ruby/core/string/tr_s_spec.rb
@@ -1,6 +1,7 @@
# -*- encoding: utf-8 -*-
-require File.expand_path('../../../spec_helper', __FILE__)
-require File.expand_path('../fixtures/classes.rb', __FILE__)
+# frozen_string_literal: false
+require_relative '../../spec_helper'
+require_relative 'fixtures/classes'
describe "String#tr_s" do
it "returns a string processed according to tr with newly duplicate characters removed" do
@@ -17,6 +18,13 @@ describe "String#tr_s" do
"hello ^--^".tr_s("---", "_").should == "hello ^_^"
end
+ it "accepts c1-c1 notation to denote range of one character" do
+ "hello".tr_s('e-e', 'x').should == "hxllo"
+ "123456789".tr_s("2-23","xy").should == "1xy456789"
+ "hello ^-^".tr_s("e-", "a-a_").should == "hallo ^_^"
+ "hello ^-^".tr_s("---o", "_a").should == "hella ^_^"
+ end
+
it "pads to_str with its last char if it is shorter than from_string" do
"this".tr_s("this", "x").should == "x"
end
@@ -45,64 +53,51 @@ describe "String#tr_s" do
"bla".tr_s(from_str, to_str).should == "BlA"
end
- it "returns subclass instances when called on a subclass" do
- StringSpecs::MyString.new("hello").tr_s("e", "a").should be_an_instance_of(StringSpecs::MyString)
+ it "returns String instances when called on a subclass" do
+ StringSpecs::MyString.new("hello").tr_s("e", "a").should.instance_of?(String)
end
- it "taints the result when self is tainted" do
- ["h", "hello"].each do |str|
- tainted_str = str.dup.taint
-
- tainted_str.tr_s("e", "a").tainted?.should == true
-
- str.tr_s("e".taint, "a").tainted?.should == false
- str.tr_s("e", "a".taint).tainted?.should == false
- end
+ # http://redmine.ruby-lang.org/issues/show/1839
+ it "can replace a 7-bit ASCII character with a multibyte one" do
+ a = "uber"
+ a.encoding.should == Encoding::UTF_8
+ b = a.tr_s("u","ü")
+ b.should == "über"
+ b.encoding.should == Encoding::UTF_8
end
- with_feature :encoding do
- # http://redmine.ruby-lang.org/issues/show/1839
- it "can replace a 7-bit ASCII character with a multibyte one" do
- a = "uber"
- a.encoding.should == Encoding::UTF_8
- b = a.tr_s("u","ü")
- b.should == "über"
- b.encoding.should == Encoding::UTF_8
- end
-
- it "can replace multiple 7-bit ASCII characters with a multibyte one" do
- a = "uuuber"
- a.encoding.should == Encoding::UTF_8
- b = a.tr_s("u","ü")
- b.should == "über"
- b.encoding.should == Encoding::UTF_8
- end
-
- it "can replace a multibyte character with a single byte one" do
- a = "über"
- a.encoding.should == Encoding::UTF_8
- b = a.tr_s("ü","u")
- b.should == "uber"
- b.encoding.should == Encoding::UTF_8
- end
+ it "can replace multiple 7-bit ASCII characters with a multibyte one" do
+ a = "uuuber"
+ a.encoding.should == Encoding::UTF_8
+ b = a.tr_s("u","ü")
+ b.should == "über"
+ b.encoding.should == Encoding::UTF_8
+ end
- it "can replace multiple multibyte characters with a single byte one" do
- a = "üüüber"
- a.encoding.should == Encoding::UTF_8
- b = a.tr_s("ü","u")
- b.should == "uber"
- b.encoding.should == Encoding::UTF_8
- end
+ it "can replace a multibyte character with a single byte one" do
+ a = "über"
+ a.encoding.should == Encoding::UTF_8
+ b = a.tr_s("ü","u")
+ b.should == "uber"
+ b.encoding.should == Encoding::UTF_8
+ end
- it "does not replace a multibyte character where part of the bytes match the tr string" do
- str = "椎名深夏"
- a = "\u0080\u0082\u0083\u0084\u0085\u0086\u0087\u0088\u0089\u008A\u008B\u008C\u008E\u0091\u0092\u0093\u0094\u0095\u0096\u0097\u0098\u0099\u009A\u009B\u009C\u009E\u009F"
- b = "€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ"
- str.tr_s(a, b).should == "椎名深夏"
- end
+ it "can replace multiple multibyte characters with a single byte one" do
+ a = "üüüber"
+ a.encoding.should == Encoding::UTF_8
+ b = a.tr_s("ü","u")
+ b.should == "uber"
+ b.encoding.should == Encoding::UTF_8
+ end
+ it "does not replace a multibyte character where part of the bytes match the tr string" do
+ str = "椎名深夏"
+ a = "\u0080\u0082\u0083\u0084\u0085\u0086\u0087\u0088\u0089\u008A\u008B\u008C\u008E\u0091\u0092\u0093\u0094\u0095\u0096\u0097\u0098\u0099\u009A\u009B\u009C\u009E\u009F"
+ b = "€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ"
+ str.tr_s(a, b).should == "椎名深夏"
end
+
end
describe "String#tr_s!" do
@@ -127,10 +122,10 @@ describe "String#tr_s!" do
s.should == "hello"
end
- it "raises a RuntimeError if self is frozen" do
+ it "raises a FrozenError if self is frozen" do
s = "hello".freeze
- lambda { s.tr_s!("el", "ar") }.should raise_error(RuntimeError)
- lambda { s.tr_s!("l", "r") }.should raise_error(RuntimeError)
- lambda { s.tr_s!("", "") }.should raise_error(RuntimeError)
+ -> { s.tr_s!("el", "ar") }.should.raise(FrozenError)
+ -> { s.tr_s!("l", "r") }.should.raise(FrozenError)
+ -> { s.tr_s!("", "") }.should.raise(FrozenError)
end
end