Skip to content
This repository
Newer
Older
100644 663 lines (520 sloc) 20.389 kb
24ce35c0 »
2010-02-22 Added encoding comments for files containing utf-8 chars.
1 # -*- encoding: utf-8 -*-
957c2c9c »
2010-02-17 Standardize require lines.
2 require File.expand_path('../../../spec_helper', __FILE__)
3 require File.expand_path('../fixtures/classes.rb', __FILE__)
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
4
5 describe "String#gsub with pattern and replacement" do
6
7f0cd857 »
2010-02-17 Add spec for String#gsub + $KCODE
7 before :each do
24ce35c0 »
2010-02-22 Added encoding comments for files containing utf-8 chars.
8 @kcode = $KCODE
7f0cd857 »
2010-02-17 Add spec for String#gsub + $KCODE
9 end
10
11 after :each do
24ce35c0 »
2010-02-22 Added encoding comments for files containing utf-8 chars.
12 $KCODE = @kcode
7f0cd857 »
2010-02-17 Add spec for String#gsub + $KCODE
13 end
14
15 it "inserts the replacement around every character when the pattern collapses" do
16 "hello".gsub(//, ".").should == ".h.e.l.l.o."
17 end
18
19 it "respects $KCODE when the pattern collapses" do
20 str = "こにちわ"
21 reg = %r!!
22
24ce35c0 »
2010-02-22 Added encoding comments for files containing utf-8 chars.
23 $KCODE = "utf-8"
7f0cd857 »
2010-02-17 Add spec for String#gsub + $KCODE
24 str.gsub(reg, ".").should == ".こ.に.ち.わ."
25 end
26
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
27 it "doesn't freak out when replacing ^" do
28 "Text\n".gsub(/^/, ' ').should == " Text\n"
10a667ac »
2008-10-28 added ironruby to guards in String#gsub specs
29 "Text\nFoo".gsub(/^/, ' ').should == " Text\n Foo"
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
30 end
31
32 it "returns a copy of self with all occurrences of pattern replaced with replacement" do
33 "hello".gsub(/[aeiou]/, '*').should == "h*ll*"
34
35 str = "hello homely world. hah!"
36 str.gsub(/\Ah\S+\s*/, "huh? ").should == "huh? homely world. hah!"
37 end
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
38
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
39 it "ignores a block if supplied" do
40 "food".gsub(/f/, "g") { "w" }.should == "good"
41 end
42
43 it "supports \\G which matches at the beginning of the remaining (non-matched) string" do
44 str = "hello homely world. hah!"
45 str.gsub(/\Gh\S+\s*/, "huh? ").should == "huh? huh? world. hah!"
46 end
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
47
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
48 it "supports /i for ignoring case" do
49 str = "Hello. How happy are you?"
50 str.gsub(/h/i, "j").should == "jello. jow jappy are you?"
51 str.gsub(/H/i, "j").should == "jello. jow jappy are you?"
52 end
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
53
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
54 it "doesn't interpret regexp metacharacters if pattern is a string" do
55 "12345".gsub('\d', 'a').should == "12345"
56 '\d'.gsub('\d', 'a').should == "a"
57 end
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
58
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
59 it "replaces \\1 sequences with the regexp's corresponding capture" do
60 str = "hello"
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
61
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
62 str.gsub(/([aeiou])/, '<\1>').should == "h<e>ll<o>"
63 str.gsub(/(.)/, '\1\1').should == "hheelllloo"
64
65 str.gsub(/.(.?)/, '<\0>(\1)').should == "<he>(e)<ll>(l)<o>()"
66
67 str.gsub(/.(.)+/, '\1').should == "o"
68
69 str = "ABCDEFGHIJKLabcdefghijkl"
70 re = /#{"(.)" * 12}/
71 str.gsub(re, '\1').should == "Aa"
72 str.gsub(re, '\9').should == "Ii"
73 # Only the first 9 captures can be accessed in MRI
74 str.gsub(re, '\10').should == "A0a0"
75 end
76
77 it "treats \\1 sequences without corresponding captures as empty strings" do
78 str = "hello!"
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
79
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
80 str.gsub("", '<\1>').should == "<>h<>e<>l<>l<>o<>!<>"
81 str.gsub("h", '<\1>').should == "<>ello!"
82
83 str.gsub(//, '<\1>').should == "<>h<>e<>l<>l<>o<>!<>"
84 str.gsub(/./, '\1\2\3').should == ""
85 str.gsub(/.(.{20})?/, '\1').should == ""
86 end
87
88 it "replaces \\& and \\0 with the complete match" do
89 str = "hello!"
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
90
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
91 str.gsub("", '<\0>').should == "<>h<>e<>l<>l<>o<>!<>"
92 str.gsub("", '<\&>').should == "<>h<>e<>l<>l<>o<>!<>"
93 str.gsub("he", '<\0>').should == "<he>llo!"
94 str.gsub("he", '<\&>').should == "<he>llo!"
95 str.gsub("l", '<\0>').should == "he<l><l>o!"
96 str.gsub("l", '<\&>').should == "he<l><l>o!"
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
97
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
98 str.gsub(//, '<\0>').should == "<>h<>e<>l<>l<>o<>!<>"
99 str.gsub(//, '<\&>').should == "<>h<>e<>l<>l<>o<>!<>"
100 str.gsub(/../, '<\0>').should == "<he><ll><o!>"
101 str.gsub(/../, '<\&>').should == "<he><ll><o!>"
102 str.gsub(/(.)./, '<\0>').should == "<he><ll><o!>"
103 end
104
105 it "replaces \\` with everything before the current match" do
106 str = "hello!"
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
107
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
108 str.gsub("", '<\`>').should == "<>h<h>e<he>l<hel>l<hell>o<hello>!<hello!>"
109 str.gsub("h", '<\`>').should == "<>ello!"
110 str.gsub("l", '<\`>').should == "he<he><hel>o!"
111 str.gsub("!", '<\`>').should == "hello<hello>"
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
112
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
113 str.gsub(//, '<\`>').should == "<>h<h>e<he>l<hel>l<hell>o<hello>!<hello!>"
114 str.gsub(/../, '<\`>').should == "<><he><hell>"
115 end
116
117 it "replaces \\' with everything after the current match" do
118 str = "hello!"
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
119
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
120 str.gsub("", '<\\\'>').should == "<hello!>h<ello!>e<llo!>l<lo!>l<o!>o<!>!<>"
121 str.gsub("h", '<\\\'>').should == "<ello!>ello!"
122 str.gsub("ll", '<\\\'>').should == "he<o!>o!"
123 str.gsub("!", '<\\\'>').should == "hello<>"
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
124
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
125 str.gsub(//, '<\\\'>').should == "<hello!>h<ello!>e<llo!>l<lo!>l<o!>o<!>!<>"
126 str.gsub(/../, '<\\\'>').should == "<llo!><o!><>"
127 end
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
128
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
129 it "replaces \\+ with the last paren that actually matched" do
130 str = "hello!"
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
131
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
132 str.gsub(/(.)(.)/, '\+').should == "el!"
133 str.gsub(/(.)(.)+/, '\+').should == "!"
134 str.gsub(/(.)()/, '\+').should == ""
135 str.gsub(/(.)(.{20})?/, '<\+>').should == "<h><e><l><l><o><!>"
136
137 str = "ABCDEFGHIJKLabcdefghijkl"
138 re = /#{"(.)" * 12}/
139 str.gsub(re, '\+').should == "Ll"
140 end
141
142 it "treats \\+ as an empty string if there was no captures" do
143 "hello!".gsub(/./, '\+').should == ""
144 end
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
145
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
146 it "maps \\\\ in replacement to \\" do
147 "hello".gsub(/./, '\\\\').should == '\\' * 5
148 end
149
150 it "leaves unknown \\x escapes in replacement untouched" do
151 "hello".gsub(/./, '\\x').should == '\\x' * 5
152 "hello".gsub(/./, '\\y').should == '\\y' * 5
153 end
154
155 it "leaves \\ at the end of replacement untouched" do
156 "hello".gsub(/./, 'hah\\').should == 'hah\\' * 5
157 end
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
158
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
159 it "taints the result if the original string or replacement is tainted" do
160 hello = "hello"
161 hello_t = "hello"
162 a = "a"
163 a_t = "a"
164 empty = ""
165 empty_t = ""
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
166
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
167 hello_t.taint; a_t.taint; empty_t.taint
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
168
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
169 hello_t.gsub(/./, a).tainted?.should == true
170 hello_t.gsub(/./, empty).tainted?.should == true
171
172 hello.gsub(/./, a_t).tainted?.should == true
173 hello.gsub(/./, empty_t).tainted?.should == true
174 hello.gsub(//, empty_t).tainted?.should == true
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
175
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
176 hello.gsub(//.taint, "foo").tainted?.should == false
177 end
178
5e103b49 »
2009-02-23 Add untrust propagation to String#gsub specs
179 ruby_version_is "1.9" do
180 it "untrusts the result if the original string or replacement is untrusted" do
181 hello = "hello"
182 hello_t = "hello"
183 a = "a"
184 a_t = "a"
185 empty = ""
186 empty_t = ""
187
188 hello_t.untrust; a_t.untrust; empty_t.untrust
189
190 hello_t.gsub(/./, a).untrusted?.should == true
191 hello_t.gsub(/./, empty).untrusted?.should == true
192
193 hello.gsub(/./, a_t).untrusted?.should == true
194 hello.gsub(/./, empty_t).untrusted?.should == true
195 hello.gsub(//, empty_t).untrusted?.should == true
196
197 hello.gsub(//.untrust, "foo").untrusted?.should == false
198 end
199 end
200
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
201 it "tries to convert pattern to a string using to_str" do
202 pattern = mock('.')
203 def pattern.to_str() "." end
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
204
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
205 "hello.".gsub(pattern, "!").should == "hello!"
206 end
207
208 it "raises a TypeError when pattern can't be converted to a string" do
a192a276 »
2009-06-02 String#gsub: Fix TypeError tests for 1.9.
209 lambda { "hello".gsub([], "x") }.should raise_error(TypeError)
210 lambda { "hello".gsub(Object.new, "x") }.should raise_error(TypeError)
211 lambda { "hello".gsub(nil, "x") }.should raise_error(TypeError)
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
212 end
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
213
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
214 it "tries to convert replacement to a string using to_str" do
215 replacement = mock('hello_replacement')
216 def replacement.to_str() "hello_replacement" end
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
217
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
218 "hello".gsub(/hello/, replacement).should == "hello_replacement"
219 end
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
220
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
221 it "raises a TypeError when replacement can't be converted to a string" do
a192a276 »
2009-06-02 String#gsub: Fix TypeError tests for 1.9.
222 lambda { "hello".gsub(/[aeiou]/, []) }.should raise_error(TypeError)
223 lambda { "hello".gsub(/[aeiou]/, Object.new) }.should raise_error(TypeError)
224 lambda { "hello".gsub(/[aeiou]/, nil) }.should raise_error(TypeError)
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
225 end
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
226
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
227 it "returns subclass instances when called on a subclass" do
3ccbfd12 »
2010-01-04 Avoid result.class.should == Class
228 StringSpecs::MyString.new("").gsub(//, "").should be_kind_of(StringSpecs::MyString)
229 StringSpecs::MyString.new("").gsub(/foo/, "").should be_kind_of(StringSpecs::MyString)
230 StringSpecs::MyString.new("foo").gsub(/foo/, "").should be_kind_of(StringSpecs::MyString)
231 StringSpecs::MyString.new("foo").gsub("foo", "").should be_kind_of(StringSpecs::MyString)
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
232 end
233
234 # Note: $~ cannot be tested because mspec messes with it
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
235
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
236 it "sets $~ to MatchData of last match and nil when there's none" do
237 'hello.'.gsub('hello', 'x')
238 $~[0].should == 'hello'
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
239
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
240 'hello.'.gsub('not', 'x')
241 $~.should == nil
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
242
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
243 'hello.'.gsub(/.(.)/, 'x')
244 $~[0].should == 'o.'
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
245
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
246 'hello.'.gsub(/not/, 'x')
247 $~.should == nil
248 end
249 end
250
71187048 »
2009-08-03 String#gsub(/pat/, hash): Initially specify 1.9 feature
251 ruby_version_is "1.9" do
252
253 describe "String#gsub with pattern and Hash" do
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
254
71187048 »
2009-08-03 String#gsub(/pat/, hash): Initially specify 1.9 feature
255 it "returns a copy of self with all occurrences of pattern replaced with the value of the corresponding hash key" do
256 "hello".gsub(/./, 'l' => 'L').should == "LL"
257 "hello!".gsub(/(.)(.)/, 'he' => 'she ', 'll' => 'said').should == 'she said'
258 "hello".gsub('l', 'l' => 'el').should == 'heelelo'
259 end
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
260
71187048 »
2009-08-03 String#gsub(/pat/, hash): Initially specify 1.9 feature
261 it "ignores keys that don't correspond to matches" do
262 "hello".gsub(/./, 'z' => 'L', 'h' => 'b', 'o' => 'ow').should == "bow"
263 end
264
265 it "returns an empty string if the pattern matches but the hash specifies no replacements" do
266 "hello".gsub(/./, 'z' => 'L').should == ""
267 end
268
269 it "ignores non-String keys" do
270 "hello".gsub(/(ll)/, 'll' => 'r', :ll => 'z').should == "hero"
271 end
272
273 it "uses a key's value as many times as needed" do
274 "food".gsub(/o/, 'o' => '0').should == "f00d"
275 end
276
277 it "uses the hash's default value for missing keys" do
278 hsh = new_hash
279 hsh.default='?'
280 hsh['o'] = '0'
281 "food".gsub(/./, hsh).should == "?00?"
282 end
283
284 it "coerces the hash values with #to_s" do
285 hsh = new_hash
286 hsh.default=[]
287 hsh['o'] = 0
288 obj = mock('!')
289 obj.should_receive(:to_s).and_return('!')
290 hsh['!'] = obj
291 "food!".gsub(/./, hsh).should == "[]00[]!"
292 end
293
6cfd7329 »
2011-10-27 Fixes specs for String#{gsub,gsub!} with Hash with default_proc
294 it "uses the hash's value set from default_proc for missing keys" do
71187048 »
2009-08-03 String#gsub(/pat/, hash): Initially specify 1.9 feature
295 hsh = new_hash
cd9bb410 »
2009-10-24 String#gsub: Fix example using Hash#default_proc (#2212)
296 hsh.default_proc = lambda { |k,v| 'lamb' }
6cfd7329 »
2011-10-27 Fixes specs for String#{gsub,gsub!} with Hash with default_proc
297 "food!".gsub(/./, hsh).should == "lamblamblamblamblamb"
71187048 »
2009-08-03 String#gsub(/pat/, hash): Initially specify 1.9 feature
298 end
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
299
71187048 »
2009-08-03 String#gsub(/pat/, hash): Initially specify 1.9 feature
300 it "sets $~ to MatchData of last match and nil when there's none for access from outside" do
301 'hello.'.gsub('l', 'l' => 'L')
302 $~.begin(0).should == 3
303 $~[0].should == 'l'
304
305 'hello.'.gsub('not', 'ot' => 'to')
306 $~.should == nil
307
308 'hello.'.gsub(/.(.)/, 'o' => ' hole')
309 $~[0].should == 'o.'
310
311 'hello.'.gsub(/not/, 'z' => 'glark')
312 $~.should == nil
313 end
314
315 it "doesn't interpolate special sequences like \\1 for the block's return value" do
316 repl = '\& \0 \1 \` \\\' \+ \\\\ foo'
317 "hello".gsub(/(.+)/, 'hello' => repl ).should == repl
318 end
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
319
71187048 »
2009-08-03 String#gsub(/pat/, hash): Initially specify 1.9 feature
320 it "untrusts the result if the original string is untrusted" do
321 str = "Ghana".untrust
322 str.gsub(/[Aa]na/, 'ana' => '').untrusted?.should be_true
323 end
324
325 it "untrusts the result if a hash value is untrusted" do
326 str = "Ghana"
327 str.gsub(/a$/, 'a' => 'di'.untrust).untrusted?.should be_true
328 end
329
330 it "taints the result if the original string is tainted" do
331 str = "Ghana".taint
332 str.gsub(/[Aa]na/, 'ana' => '').tainted?.should be_true
333 end
334
335 it "taints the result if a hash value is tainted" do
336 str = "Ghana"
337 str.gsub(/a$/, 'a' => 'di'.taint).tainted?.should be_true
338 end
339
340 end
f72f2130 »
2011-10-27 Adds specs for String#gsub! with Hash as replacement
341
342 describe "String#gsub! with pattern and Hash" do
343
344 it "returns self with all occurrences of pattern replaced with the value of the corresponding hash key" do
345 "hello".gsub!(/./, 'l' => 'L').should == "LL"
346 "hello!".gsub!(/(.)(.)/, 'he' => 'she ', 'll' => 'said').should == 'she said'
347 "hello".gsub!('l', 'l' => 'el').should == 'heelelo'
348 end
349
350 it "ignores keys that don't correspond to matches" do
351 "hello".gsub!(/./, 'z' => 'L', 'h' => 'b', 'o' => 'ow').should == "bow"
352 end
353
354 it "replaces self with an empty string if the pattern matches but the hash specifies no replacements" do
355 "hello".gsub!(/./, 'z' => 'L').should == ""
356 end
357
358 it "ignores non-String keys" do
359 "hello".gsub!(/(ll)/, 'll' => 'r', :ll => 'z').should == "hero"
360 end
361
362 it "uses a key's value as many times as needed" do
363 "food".gsub!(/o/, 'o' => '0').should == "f00d"
364 end
365
366 it "uses the hash's default value for missing keys" do
367 hsh = new_hash
368 hsh.default='?'
369 hsh['o'] = '0'
370 "food".gsub!(/./, hsh).should == "?00?"
371 end
372
373 it "coerces the hash values with #to_s" do
374 hsh = new_hash
375 hsh.default=[]
376 hsh['o'] = 0
377 obj = mock('!')
378 obj.should_receive(:to_s).and_return('!')
379 hsh['!'] = obj
380 "food!".gsub!(/./, hsh).should == "[]00[]!"
381 end
382
6cfd7329 »
2011-10-27 Fixes specs for String#{gsub,gsub!} with Hash with default_proc
383 it "uses the hash's value set from default_proc for missing keys" do
f72f2130 »
2011-10-27 Adds specs for String#gsub! with Hash as replacement
384 hsh = new_hash
385 hsh.default_proc = lambda { |k,v| 'lamb' }
6cfd7329 »
2011-10-27 Fixes specs for String#{gsub,gsub!} with Hash with default_proc
386 "food!".gsub!(/./, hsh).should == "lamblamblamblamblamb"
f72f2130 »
2011-10-27 Adds specs for String#gsub! with Hash as replacement
387 end
388
389 it "sets $~ to MatchData of last match and nil when there's none for access from outside" do
390 'hello.'.gsub!('l', 'l' => 'L')
391 $~.begin(0).should == 3
392 $~[0].should == 'l'
393
394 'hello.'.gsub!('not', 'ot' => 'to')
395 $~.should == nil
396
397 'hello.'.gsub!(/.(.)/, 'o' => ' hole')
398 $~[0].should == 'o.'
399
400 'hello.'.gsub!(/not/, 'z' => 'glark')
401 $~.should == nil
402 end
403
404 it "doesn't interpolate special sequences like \\1 for the block's return value" do
405 repl = '\& \0 \1 \` \\\' \+ \\\\ foo'
406 "hello".gsub!(/(.+)/, 'hello' => repl ).should == repl
407 end
408
409 it "keeps untrusted state" do
410 str = "Ghana".untrust
411 str.gsub!(/[Aa]na/, 'ana' => '').untrusted?.should be_true
412 end
413
414 it "untrusts self if a hash value is untrusted" do
415 str = "Ghana"
416 str.gsub!(/a$/, 'a' => 'di'.untrust).untrusted?.should be_true
417 end
418
419 it "keeps tainted state" do
420 str = "Ghana".taint
421 str.gsub!(/[Aa]na/, 'ana' => '').tainted?.should be_true
422 end
423
424 it "taints self if a hash value is tainted" do
425 str = "Ghana"
426 str.gsub!(/a$/, 'a' => 'di'.taint).tainted?.should be_true
427 end
428
429 end
71187048 »
2009-08-03 String#gsub(/pat/, hash): Initially specify 1.9 feature
430 end
431
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
432 describe "String#gsub with pattern and block" do
433 it "returns a copy of self with all occurrences of pattern replaced with the block's return value" do
434 "hello".gsub(/./) { |s| s.succ + ' ' }.should == "i f m m p "
435 "hello!".gsub(/(.)(.)/) { |*a| a.inspect }.should == '["he"]["ll"]["o!"]'
58425822 »
2008-10-28 spec single character String#gsub replacement and behavior of gsub if…
436 "hello".gsub('l') { 'x'}.should == 'hexxo'
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
437 end
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
438
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
439 it "sets $~ for access from the block" do
440 str = "hello"
441 str.gsub(/([aeiou])/) { "<#{$~[1]}>" }.should == "h<e>ll<o>"
442 str.gsub(/([aeiou])/) { "<#{$1}>" }.should == "h<e>ll<o>"
443 str.gsub("l") { "<#{$~[0]}>" }.should == "he<l><l>o"
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
444
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
445 offsets = []
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
446
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
447 str.gsub(/([aeiou])/) do
8d4e4531 »
2008-06-08 Fixed arbitrary three-space indenting in String#gsub specs.
448 md = $~
449 md.string.should == str
450 offsets << md.offset(0)
451 str
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
452 end.should == "hhellollhello"
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
453
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
454 offsets.should == [[1, 2], [4, 5]]
455 end
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
456
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
457 it "restores $~ after leaving the block" do
458 [/./, "l"].each do |pattern|
459 old_md = nil
460 "hello".gsub(pattern) do
461 old_md = $~
462 "ok".match(/./)
463 "x"
464 end
8d4e4531 »
2008-06-08 Fixed arbitrary three-space indenting in String#gsub specs.
465
b66aa7d1 »
2009-08-03 String#gsub: Fix 1.9 test failure involving $~
466 $~[0].should == old_md[0]
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
467 $~.string.should == "hello"
468 end
469 end
8d4e4531 »
2008-06-08 Fixed arbitrary three-space indenting in String#gsub specs.
470
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
471 it "sets $~ to MatchData of last match and nil when there's none for access from outside" do
472 'hello.'.gsub('l') { 'x' }
473 $~.begin(0).should == 3
474 $~[0].should == 'l'
475
476 'hello.'.gsub('not') { 'x' }
477 $~.should == nil
478
479 'hello.'.gsub(/.(.)/) { 'x' }
480 $~[0].should == 'o.'
481
482 'hello.'.gsub(/not/) { 'x' }
483 $~.should == nil
484 end
8d4e4531 »
2008-06-08 Fixed arbitrary three-space indenting in String#gsub specs.
485
2eae99c2 »
2009-06-30 String#gsub: self can be modified inside block on 1.9
486 ruby_version_is ""..."1.9" do
487 it "raises a RuntimeError if the string is modified while substituting" do
488 str = "hello"
489 lambda { str.gsub(//) { str[0] = 'x' } }.should raise_error(RuntimeError)
490 end
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
491 end
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
492
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
493 it "doesn't interpolate special sequences like \\1 for the block's return value" do
494 repl = '\& \0 \1 \` \\\' \+ \\\\ foo'
495 "hello".gsub(/(.+)/) { repl }.should == repl
496 end
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
497
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
498 it "converts the block's return value to a string using to_s" do
499 replacement = mock('hello_replacement')
500 def replacement.to_s() "hello_replacement" end
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
501
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
502 "hello".gsub(/hello/) { replacement }.should == "hello_replacement"
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
503
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
504 obj = mock('ok')
505 def obj.to_s() "ok" end
356fa4fb »
2011-04-14 Finally cleaned up trailing whitespace in spec files.
506
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
507 "hello".gsub(/.+/) { obj }.should == "ok"
508 end
509
b333dee8 »
2009-02-23 Add missing version guards for 1.9 and add missing taint spec for gsub!
510 ruby_version_is "1.9" do
511 it "untrusts the result if the original string or replacement is untrusted" do
512 hello = "hello"
513 hello_t = "hello"
514 a = "a"
515 a_t = "a"
516 empty = ""
517 empty_t = ""
518
519 hello_t.untrust; a_t.untrust; empty_t.untrust
520
521 hello_t.gsub(/./) { a }.untrusted?.should == true
522 hello_t.gsub(/./) { empty }.untrusted?.should == true
523
524 hello.gsub(/./) { a_t }.untrusted?.should == true
525 hello.gsub(/./) { empty_t }.untrusted?.should == true
526 hello.gsub(//) { empty_t }.untrusted?.should == true
527
528 hello.gsub(//.untrust) { "foo" }.untrusted?.should == false
529 end
530 end
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
531 end
532
533 describe "String#gsub! with pattern and replacement" do
534 it "modifies self in place and returns self" do
535 a = "hello"
5a1271e1 »
2008-05-18 Use 'obj.should equal(other_obj)' instead of 'obj.equal?(other_obj).s…
536 a.gsub!(/[aeiou]/, '*').should equal(a)
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
537 a.should == "h*ll*"
538 end
539
b333dee8 »
2009-02-23 Add missing version guards for 1.9 and add missing taint spec for gsub!
540 it "taints self if replacement is tainted" do
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
541 a = "hello"
b333dee8 »
2009-02-23 Add missing version guards for 1.9 and add missing taint spec for gsub!
542 a.gsub!(/./.taint, "foo").tainted?.should == false
543 a.gsub!(/./, "foo".taint).tainted?.should == true
544 end
545
546 ruby_version_is "1.9" do
547 it "untrusts self if replacement is untrusted" do
548 a = "hello"
549 a.gsub!(/./.untrust, "foo").untrusted?.should == false
550 a.gsub!(/./, "foo".untrust).untrusted?.should == true
551 end
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
552 end
aa31117b »
2010-01-28 Add a proper guard, expand the 1.8 case
553
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
554 it "returns nil if no modifications were made" do
555 a = "hello"
556 a.gsub!(/z/, '*').should == nil
557 a.gsub!(/z/, 'z').should == nil
558 a.should == "hello"
559 end
aa31117b »
2010-01-28 Add a proper guard, expand the 1.8 case
560
5a78f98c »
2010-02-02 Redo String#gsub! specs for frozen strings.
561 ruby_version_is ""..."1.9" do
562 it "does not raise an error if the frozen string would not be modified" do
563 s = "hello"
564 s.freeze
565
566 s.gsub!(/ROAR/, "x").should be_nil
567 end
568
569 it "raises a TypeError if the frozen string would be modified" do
570 s = "hello"
571 s.freeze
572
573 lambda { s.gsub!(/e/, "e") }.should raise_error(TypeError)
574 lambda { s.gsub!(/[aeiou]/, '*') }.should raise_error(TypeError)
575 end
576 end
577
578 # See [ruby-core:23666]
579 ruby_version_is "1.9" do
10f861de »
2010-02-01 Some cleanups for specs from Rubinius.
580 it "raises a RuntimeError when self is frozen" do
680e9622 »
2009-06-01 String: 1.9 methods raise RuntimeError when frozen
581 s = "hello"
582 s.freeze
aa31117b »
2010-01-28 Add a proper guard, expand the 1.8 case
583
10f861de »
2010-02-01 Some cleanups for specs from Rubinius.
584 lambda { s.gsub!(/ROAR/, "x") }.should raise_error(RuntimeError)
585 lambda { s.gsub!(/e/, "e") }.should raise_error(RuntimeError)
586 lambda { s.gsub!(/[aeiou]/, '*') }.should raise_error(RuntimeError)
680e9622 »
2009-06-01 String: 1.9 methods raise RuntimeError when frozen
587 end
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
588 end
589 end
590
591 describe "String#gsub! with pattern and block" do
592 it "modifies self in place and returns self" do
593 a = "hello"
5a1271e1 »
2008-05-18 Use 'obj.should equal(other_obj)' instead of 'obj.equal?(other_obj).s…
594 a.gsub!(/[aeiou]/) { '*' }.should equal(a)
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
595 a.should == "h*ll*"
596 end
597
b333dee8 »
2009-02-23 Add missing version guards for 1.9 and add missing taint spec for gsub!
598 it "taints self if block's result is tainted" do
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
599 a = "hello"
b333dee8 »
2009-02-23 Add missing version guards for 1.9 and add missing taint spec for gsub!
600 a.gsub!(/./.taint) { "foo" }.tainted?.should == false
601 a.gsub!(/./) { "foo".taint }.tainted?.should == true
5e103b49 »
2009-02-23 Add untrust propagation to String#gsub specs
602 end
603
604 ruby_version_is "1.9" do
605 it "untrusts self if block's result is untrusted" do
606 a = "hello"
607 a.gsub!(/./.untrust) { "foo" }.untrusted?.should == false
608 a.gsub!(/./) { "foo".untrust }.untrusted?.should == true
609 end
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
610 end
aa31117b »
2010-01-28 Add a proper guard, expand the 1.8 case
611
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
612 it "returns nil if no modifications were made" do
613 a = "hello"
614 a.gsub!(/z/) { '*' }.should == nil
615 a.gsub!(/z/) { 'z' }.should == nil
616 a.should == "hello"
617 end
aa31117b »
2010-01-28 Add a proper guard, expand the 1.8 case
618
5a78f98c »
2010-02-02 Redo String#gsub! specs for frozen strings.
619 ruby_version_is ""..."1.9" do
620 it "does not raise an error if the frozen string would not be modified" do
621 s = "hello"
622 s.freeze
623
624 s.gsub!(/ROAR/) { "x" }.should be_nil
625 end
626
dc4cce30 »
2010-11-24 Fixed some String specs.
627 deviates_on :rubinius do
628 # MRI 1.8.x is inconsistent here, raising a TypeError when not passed
629 # a block and a RuntimeError when passed a block. This is arguably a
630 # bug in MRI. In 1.9, both situations raise a RuntimeError.
631 it "raises a TypeError if the frozen string would be modified" do
632 s = "hello"
633 s.freeze
634
635 lambda { s.gsub!(/e/) { "e" } }.should raise_error(TypeError)
636 lambda { s.gsub!(/[aeiou]/) { '*' } }.should raise_error(TypeError)
637 end
638 end
5a78f98c »
2010-02-02 Redo String#gsub! specs for frozen strings.
639
dc4cce30 »
2010-11-24 Fixed some String specs.
640 not_compliant_on :rubinius do
641 it "raises a RuntimeError if the frozen string would be modified" do
642 s = "hello"
643 s.freeze
644
645 lambda { s.gsub!(/e/) { "e" } }.should raise_error(RuntimeError)
646 lambda { s.gsub!(/[aeiou]/) { '*' } }.should raise_error(RuntimeError)
647 end
5a78f98c »
2010-02-02 Redo String#gsub! specs for frozen strings.
648 end
649 end
650
651 # See [ruby-core:23663]
652 ruby_version_is "1.9" do
10f861de »
2010-02-01 Some cleanups for specs from Rubinius.
653 it "raises a RuntimeError when self is frozen" do
663f8bdd »
2009-06-02 String#gsub! Guard frozen behav. [ruby-core:23666]
654 s = "hello"
655 s.freeze
326a5384 »
2009-02-08 Fixed use of guards in the specs.
656
10f861de »
2010-02-01 Some cleanups for specs from Rubinius.
657 lambda { s.gsub!(/ROAR/) { "x" } }.should raise_error(RuntimeError)
658 lambda { s.gsub!(/e/) { "e" } }.should raise_error(RuntimeError)
659 lambda { s.gsub!(/[aeiou]/) { '*' } }.should raise_error(RuntimeError)
663f8bdd »
2009-06-02 String#gsub! Guard frozen behav. [ruby-core:23666]
660 end
6c1c34a6 »
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
661 end
662 end
Something went wrong with that request. Please try again.