Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 139 lines (115 sloc) 4.592 kb
e9cf074 @brixen Standardize encoding magic comments.
brixen authored
1 # -*- encoding: utf-8 -*-
957c2c9 @brixen Standardize require lines.
brixen authored
2 require File.expand_path('../../../spec_helper', __FILE__)
3 require File.expand_path('../fixtures/classes.rb', __FILE__)
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
4
5 describe "String#tr" do
6 it "returns a new string with the characters from from_string replaced by the ones in to_string" do
7 "hello".tr('aeiou', '*').should == "h*ll*"
8 "hello".tr('el', 'ip').should == "hippo"
9 "Lisp".tr("Lisp", "Ruby").should == "Ruby"
10 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
11
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
12 it "accepts c1-c2 notation to denote ranges of characters" do
13 "hello".tr('a-y', 'b-z').should == "ifmmp"
14 "123456789".tr("2-5","abcdefg").should == "1abcd6789"
15 "hello ^-^".tr("e-", "__").should == "h_llo ^_^"
16 "hello ^-^".tr("---", "_").should == "hello ^_^"
17 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
18
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
19 it "pads to_str with its last char if it is shorter than from_string" do
20 "this".tr("this", "x").should == "xxxx"
21 "hello".tr("a-z", "A-H.").should == "HE..."
22 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
23
3f6c77d @nurse String#tr raises a ArgumentError from Ruby 1.9.2.
nurse authored
24 ruby_version_is '' ... '1.9.2' do
25 it "treats a descending range in the replacement as containing just the start character" do
26 "hello".tr("a-y", "z-b").should == "zzzzz"
27 end
28
29 it "treats a descending range in the source as empty" do
30 "hello".tr("l-a", "z").should == "hello"
31 end
a82f38a @matthewd String#tr treats descending ranges differently between from and to.
matthewd authored
32 end
33
3f6c77d @nurse String#tr raises a ArgumentError from Ruby 1.9.2.
nurse authored
34 ruby_version_is '1.9.2' do
35 it "raises a ArgumentError a descending range in the replacement as containing just the start character" do
36 lambda { "hello".tr("a-y", "z-b") }.should raise_error(ArgumentError)
37 end
38
39 it "raises a ArgumentError a descending range in the source as empty" do
40 lambda { "hello".tr("l-a", "z") }.should raise_error(ArgumentError)
41 end
a82f38a @matthewd String#tr treats descending ranges differently between from and to.
matthewd authored
42 end
43
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
44 it "translates chars not in from_string when it starts with a ^" do
45 "hello".tr('^aeiou', '*').should == "*e**o"
46 "123456789".tr("^345", "abc").should == "cc345cccc"
47 "abcdefghijk".tr("^d-g", "9131").should == "111defg1111"
326a538 @brixen Fixed use of guards in the specs.
brixen authored
48
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
49 "hello ^_^".tr("a-e^e", ".").should == "h.llo ._."
50 "hello ^_^".tr("^^", ".").should == "......^.^"
51 "hello ^_^".tr("^", "x").should == "hello x_x"
52 "hello ^-^".tr("^-^", "x").should == "xxxxxx^-^"
53 "hello ^-^".tr("^^-^", "x").should == "xxxxxx^x^"
54 "hello ^-^".tr("^---", "x").should == "xxxxxxx-x"
55 "hello ^-^".tr("^---l-o", "x").should == "xxlloxx-x"
56 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
57
05051c2 @jfirebaugh String#tr supports non-injective replacements
jfirebaugh authored
58 it "supports non-injective replacements" do
59 "hello".tr("helo", "1212").should == "12112"
60 end
61
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
62 it "tries to convert from_str and to_str to strings using to_str" do
63 from_str = mock('ab')
627d205 @brixen Removed specs mocking #respond_to? and #method_missing.
brixen authored
64 from_str.should_receive(:to_str).and_return("ab")
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
65
66 to_str = mock('AB')
627d205 @brixen Removed specs mocking #respond_to? and #method_missing.
brixen authored
67 to_str.should_receive(:to_str).and_return("AB")
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
68
69 "bla".tr(from_str, to_str).should == "BlA"
70 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
71
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
72 it "returns subclass instances when called on a subclass" do
3ccbfd1 @BanzaiMan Avoid result.class.should == Class
BanzaiMan authored
73 StringSpecs::MyString.new("hello").tr("e", "a").should be_kind_of(StringSpecs::MyString)
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
74 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
75
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
76 it "taints the result when self is tainted" do
77 ["h", "hello"].each do |str|
78 tainted_str = str.dup.taint
326a538 @brixen Fixed use of guards in the specs.
brixen authored
79
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
80 tainted_str.tr("e", "a").tainted?.should == true
326a538 @brixen Fixed use of guards in the specs.
brixen authored
81
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
82 str.tr("e".taint, "a").tainted?.should == false
83 str.tr("e", "a".taint).tainted?.should == false
84 end
85 end
d2910ab @runpaint String#tr: Can replace ASCII with multibyte on 1.9
runpaint authored
86
74e5245 @brixen Added with_feature guards to String for encoding.
brixen authored
87 with_feature :encoding do
d2910ab @runpaint String#tr: Can replace ASCII with multibyte on 1.9
runpaint authored
88 # http://redmine.ruby-lang.org/issues/show/1839
89 it "can replace a 7-bit ASCII character with a multibyte one" do
90 a = "uber"
91 a.encoding.should == Encoding::UTF_8
92 b = a.tr("u","ü")
93 b.should == "über"
94 b.encoding.should == Encoding::UTF_8
95 end
96 end
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
97 end
98
99 describe "String#tr!" do
100 it "modifies self in place" do
101 s = "abcdefghijklmnopqR"
102 s.tr!("cdefg", "12").should == "ab12222hijklmnopqR"
103 s.should == "ab12222hijklmnopqR"
104 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
105
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
106 it "returns nil if no modification was made" do
107 s = "hello"
108 s.tr!("za", "yb").should == nil
109 s.tr!("", "").should == nil
110 s.should == "hello"
111 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
112
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
113 it "does not modify self if from_str is empty" do
114 s = "hello"
115 s.tr!("", "").should == nil
116 s.should == "hello"
117 s.tr!("", "yb").should == nil
118 s.should == "hello"
119 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
120
680e962 @runpaint String: 1.9 methods raise RuntimeError when frozen
runpaint authored
121 ruby_version_is ""..."1.9" do
122 it "raises a TypeError if self is frozen" do
123 s = "abcdefghijklmnopqR".freeze
124 lambda { s.tr!("cdefg", "12") }.should raise_error(TypeError)
125 lambda { s.tr!("R", "S") }.should raise_error(TypeError)
126 lambda { s.tr!("", "") }.should raise_error(TypeError)
127 end
128 end
129
130 ruby_version_is "1.9" do
131 it "raises a RuntimeError if self is frozen" do
132 s = "abcdefghijklmnopqR".freeze
133 lambda { s.tr!("cdefg", "12") }.should raise_error(RuntimeError)
134 lambda { s.tr!("R", "S") }.should raise_error(RuntimeError)
135 lambda { s.tr!("", "") }.should raise_error(RuntimeError)
136 end
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
137 end
138 end
Something went wrong with that request. Please try again.