summaryrefslogtreecommitdiff
path: root/spec/ruby/core/string/sub_spec.rb
blob: 630f0d6b704de9cc596b39341d5d16426130c517 (plain)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe "String#sub with pattern, replacement" do
  it "returns a copy of self when no modification is made" do
    a = "hello"
    b = a.sub(/w.*$/, "*")

    b.should_not equal(a)
    b.should == "hello"
  end

  it "returns a copy of self with all occurrences of pattern replaced with replacement" do
    "hello".sub(/[aeiou]/, '*').should == "h*llo"
    "hello".sub(//, ".").should == ".hello"
  end

  it "ignores a block if supplied" do
    "food".sub(/f/, "g") { "w" }.should == "good"
  end

  it "supports \\G which matches at the beginning of the string" do
    "hello world!".sub(/\Ghello/, "hi").should == "hi world!"
  end

  it "supports /i for ignoring case" do
    "Hello".sub(/h/i, "j").should == "jello"
    "hello".sub(/H/i, "j").should == "jello"
  end

  it "doesn't interpret regexp metacharacters if pattern is a string" do
    "12345".sub('\d', 'a').should == "12345"
    '\d'.sub('\d', 'a').should == "a"
  end

  it "replaces \\1 sequences with the regexp's corresponding capture" do
    str = "hello"

    str.sub(/([aeiou])/, '<\1>').should == "h<e>llo"
    str.sub(/(.)/, '\1\1').should == "hhello"

    str.sub(/.(.?)/, '<\0>(\1)').should == "<he>(e)llo"

    str.sub(/.(.)+/, '\1').should == "o"

    str = "ABCDEFGHIJKL"
    re = /#{"(.)" * 12}/
    str.sub(re, '\1').should == "A"
    str.sub(re, '\9').should == "I"
    # Only the first 9 captures can be accessed in MRI
    str.sub(re, '\10').should == "A0"
  end

  it "treats \\1 sequences without corresponding captures as empty strings" do
    str = "hello!"

    str.sub("", '<\1>').should == "<>hello!"
    str.sub("h", '<\1>').should == "<>ello!"

    str.sub(//, '<\1>').should == "<>hello!"
    str.sub(/./, '\1\2\3').should == "ello!"
    str.sub(/.(.{20})?/, '\1').should == "ello!"
  end

  it "replaces \\& and \\0 with the complete match" do
    str = "hello!"

    str.sub("", '<\0>').should == "<>hello!"
    str.sub("", '<\&>').should == "<>hello!"
    str.sub("he", '<\0>').should == "<he>llo!"
    str.sub("he", '<\&>').should == "<he>llo!"
    str.sub("l", '<\0>').should == "he<l>lo!"
    str.sub("l", '<\&>').should == "he<l>lo!"

    str.sub(//, '<\0>').should == "<>hello!"
    str.sub(//, '<\&>').should == "<>hello!"
    str.sub(/../, '<\0>').should == "<he>llo!"
    str.sub(/../, '<\&>').should == "<he>llo!"
    str.sub(/(.)./, '<\0>').should == "<he>llo!"
  end

  it "replaces \\` with everything before the current match" do
    str = "hello!"

    str.sub("", '<\`>').should == "<>hello!"
    str.sub("h", '<\`>').should == "<>ello!"
    str.sub("l", '<\`>').should == "he<he>lo!"
    str.sub("!", '<\`>').should == "hello<hello>"

    str.sub(//, '<\`>').should == "<>hello!"
    str.sub(/..o/, '<\`>').should == "he<he>!"
  end

  it "replaces \\' with everything after the current match" do
    str = "hello!"

    str.sub("", '<\\\'>').should == "<hello!>hello!"
    str.sub("h", '<\\\'>').should == "<ello!>ello!"
    str.sub("ll", '<\\\'>').should == "he<o!>o!"
    str.sub("!", '<\\\'>').should == "hello<>"

    str.sub(//, '<\\\'>').should == "<hello!>hello!"
    str.sub(/../, '<\\\'>').should == "<llo!>llo!"
  end

  it "replaces \\\\\\+ with \\\\+" do
    "x".sub(/x/, '\\\+').should == "\\+"
  end

  it "replaces \\+ with the last paren that actually matched" do
    str = "hello!"

    str.sub(/(.)(.)/, '\+').should == "ello!"
    str.sub(/(.)(.)+/, '\+').should == "!"
    str.sub(/(.)()/, '\+').should == "ello!"
    str.sub(/(.)(.{20})?/, '<\+>').should == "<h>ello!"

    str = "ABCDEFGHIJKL"
    re = /#{"(.)" * 12}/
    str.sub(re, '\+').should == "L"
  end

  it "treats \\+ as an empty string if there was no captures" do
    "hello!".sub(/./, '\+').should == "ello!"
  end

  it "maps \\\\ in replacement to \\" do
    "hello".sub(/./, '\\\\').should == '\\ello'
  end

  it "leaves unknown \\x escapes in replacement untouched" do
    "hello".sub(/./, '\\x').should == '\\xello'
    "hello".sub(/./, '\\y').should == '\\yello'
  end

  it "leaves \\ at the end of replacement untouched" do
    "hello".sub(/./, 'hah\\').should == 'hah\\ello'
  end

  it "taints the result if the original string or replacement is tainted" do
    hello = "hello"
    hello_t = "hello"
    a = "a"
    a_t = "a"
    empty = ""
    empty_t = ""

    hello_t.taint; a_t.taint; empty_t.taint

    hello_t.sub(/./, a).tainted?.should == true
    hello_t.sub(/./, empty).tainted?.should == true

    hello.sub(/./, a_t).tainted?.should == true
    hello.sub(/./, empty_t).tainted?.should == true
    hello.sub(//, empty_t).tainted?.should == true

    hello.sub(//.taint, "foo").tainted?.should == false
  end

  it "tries to convert pattern to a string using to_str" do
    pattern = mock('.')
    pattern.should_receive(:to_str).and_return(".")

    "hello.".sub(pattern, "!").should == "hello!"
  end

  not_supported_on :opal do
    it "raises a TypeError when pattern is a Symbol" do
      -> { "hello".sub(:woot, "x") }.should raise_error(TypeError)
    end
  end

  it "raises a TypeError when pattern is an Array" do
    -> { "hello".sub([], "x") }.should raise_error(TypeError)
  end

  it "raises a TypeError when pattern can't be converted to a string" do
    -> { "hello".sub(Object.new, nil) }.should raise_error(TypeError)
  end

  it "tries to convert replacement to a string using to_str" do
    replacement = mock('hello_replacement')
    replacement.should_receive(:to_str).and_return("hello_replacement")

    "hello".sub(/hello/, replacement).should == "hello_replacement"
  end

  it "raises a TypeError when replacement can't be converted to a string" do
    -> { "hello".sub(/[aeiou]/, []) }.should raise_error(TypeError)
    -> { "hello".sub(/[aeiou]/, 99) }.should raise_error(TypeError)
  end

  it "returns subclass instances when called on a subclass" do
    StringSpecs::MyString.new("").sub(//, "").should be_an_instance_of(StringSpecs::MyString)
    StringSpecs::MyString.new("").sub(/foo/, "").should be_an_instance_of(StringSpecs::MyString)
    StringSpecs::MyString.new("foo").sub(/foo/, "").should be_an_instance_of(StringSpecs::MyString)
    StringSpecs::MyString.new("foo").sub("foo", "").should be_an_instance_of(StringSpecs::MyString)
  end

  it "sets $~ to MatchData of match and nil when there's none" do
    'hello.'.sub('hello', 'x')
    $~[0].should == 'hello'

    'hello.'.sub('not', 'x')
    $~.should == nil

    'hello.'.sub(/.(.)/, 'x')
    $~[0].should == 'he'

    'hello.'.sub(/not/, 'x')
    $~.should == nil
  end

  it "replaces \\\\\\1 with \\1" do
    "ababa".sub(/(b)/, '\\\1').should == "a\\1aba"
  end

  it "replaces \\\\\\\\1 with \\1" do
    "ababa".sub(/(b)/, '\\\\1').should == "a\\1aba"
  end

  it "replaces \\\\\\\\\\1 with \\" do
    "ababa".sub(/(b)/, '\\\\\1').should == "a\\baba"
  end

end

describe "String#sub with pattern and block" do
  it "returns a copy of self with the first occurrences of pattern replaced with the block's return value" do
    "hi".sub(/./) { |s| s + ' ' }.should == "h i"
    "hi!".sub(/(.)(.)/) { |*a| a.inspect }.should == '["hi"]!'
  end

  it "sets $~ for access from the block" do
    str = "hello"
    str.sub(/([aeiou])/) { "<#{$~[1]}>" }.should == "h<e>llo"
    str.sub(/([aeiou])/) { "<#{$1}>" }.should == "h<e>llo"
    str.sub("l") { "<#{$~[0]}>" }.should == "he<l>lo"

    offsets = []

    str.sub(/([aeiou])/) do
       md = $~
       md.string.should == str
       offsets << md.offset(0)
       str
    end.should == "hhellollo"

    offsets.should == [[1, 2]]
  end

  it "sets $~ to MatchData of last match and nil when there's none for access from outside" do
    'hello.'.sub('l') { 'x' }
    $~.begin(0).should == 2
    $~[0].should == 'l'

    'hello.'.sub('not') { 'x' }
    $~.should == nil

    'hello.'.sub(/.(.)/) { 'x' }
    $~[0].should == 'he'

    'hello.'.sub(/not/) { 'x' }
    $~.should == nil
  end

  it "doesn't raise a RuntimeError if the string is modified while substituting" do
    str = "hello"
    str.sub(//) { str[0] = 'x' }.should == "xhello"
    str.should == "xello"
  end

  it "doesn't interpolate special sequences like \\1 for the block's return value" do
    repl = '\& \0 \1 \` \\\' \+ \\\\ foo'
    "hello".sub(/(.+)/) { repl }.should == repl
  end

  it "converts the block's return value to a string using to_s" do
    obj = mock('hello_replacement')
    obj.should_receive(:to_s).and_return("hello_replacement")
    "hello".sub(/hello/) { obj }.should == "hello_replacement"

    obj = mock('ok')
    obj.should_receive(:to_s).and_return("ok")
    "hello".sub(/.+/) { obj }.should == "ok"
  end

  it "taints the result if the original string or replacement is tainted" do
    hello = "hello"
    hello_t = "hello"
    a = "a"
    a_t = "a"
    empty = ""
    empty_t = ""

    hello_t.taint; a_t.taint; empty_t.taint

    hello_t.sub(/./) { a }.tainted?.should == true
    hello_t.sub(/./) { empty }.tainted?.should == true

    hello.sub(/./) { a_t }.tainted?.should == true
    hello.sub(/./) { empty_t }.tainted?.should == true
    hello.sub(//) { empty_t }.tainted?.should == true

    hello.sub(//.taint) { "foo" }.tainted?.should == false
  end
end

describe "String#sub! with pattern, replacement" do
  it "modifies self in place and returns self" do
    a = "hello"
    a.sub!(/[aeiou]/, '*').should equal(a)
    a.should == "h*llo"
  end

  it "taints self if replacement is tainted" do
    a = "hello"
    a.sub!(/./.taint, "foo").tainted?.should == false
    a.sub!(/./, "foo".taint).tainted?.should == true
  end

  it "returns nil if no modifications were made" do
    a = "hello"
    a.sub!(/z/, '*').should == nil
    a.sub!(/z/, 'z').should == nil
    a.should == "hello"
  end

  it "raises a #{frozen_error_class} when self is frozen" do
    s = "hello"
    s.freeze

    -> { s.sub!(/ROAR/, "x")    }.should raise_error(frozen_error_class)
    -> { s.sub!(/e/, "e")       }.should raise_error(frozen_error_class)
    -> { s.sub!(/[aeiou]/, '*') }.should raise_error(frozen_error_class)
  end
end

describe "String#sub! with pattern and block" do
  it "modifies self in place and returns self" do
    a = "hello"
    a.sub!(/[aeiou]/) { '*' }.should equal(a)
    a.should == "h*llo"
  end

  it "sets $~ for access from the block" do
    str = "hello"
    str.dup.sub!(/([aeiou])/) { "<#{$~[1]}>" }.should == "h<e>llo"
    str.dup.sub!(/([aeiou])/) { "<#{$1}>" }.should == "h<e>llo"
    str.dup.sub!("l") { "<#{$~[0]}>" }.should == "he<l>lo"

    offsets = []

    str.dup.sub!(/([aeiou])/) do
       md = $~
       md.string.should == str
       offsets << md.offset(0)
       str
    end.should == "hhellollo"

    offsets.should == [[1, 2]]
  end

  it "taints self if block's result is tainted" do
    a = "hello"
    a.sub!(/./.taint) { "foo" }.tainted?.should == false
    a.sub!(/./) { "foo".taint }.tainted?.should == true
  end

  it "returns nil if no modifications were made" do
    a = "hello"
    a.sub!(/z/) { '*' }.should == nil
    a.sub!(/z/) { 'z' }.should == nil
    a.should == "hello"
  end

  it "raises a RuntimeError if the string is modified while substituting" do
    str = "hello"
    -> { str.sub!(//) { str << 'x' } }.should raise_error(RuntimeError)
  end

  it "raises a #{frozen_error_class} when self is frozen" do
    s = "hello"
    s.freeze

    -> { s.sub!(/ROAR/) { "x" }    }.should raise_error(frozen_error_class)
    -> { s.sub!(/e/) { "e" }       }.should raise_error(frozen_error_class)
    -> { s.sub!(/[aeiou]/) { '*' } }.should raise_error(frozen_error_class)
  end
end

describe "String#sub with pattern and Hash" do

  it "returns a copy of self with the first occurrence of pattern replaced with the value of the corresponding hash key" do
    "hello".sub(/./, 'l' => 'L').should == "ello"
    "hello!".sub(/(.)(.)/, 'he' => 'she ', 'll' => 'said').should == 'she llo!'
    "hello".sub('l', 'l' => 'el').should == 'heello'
  end

  it "removes keys that don't correspond to matches" do
    "hello".sub(/./, 'z' => 'b', 'o' => 'ow').should == "ello"
  end

  it "ignores non-String keys" do
    "tattoo".sub(/(tt)/, 'tt' => 'b', tt: 'z').should == "taboo"
  end

  it "uses a key's value only a single time" do
    "food".sub(/o/, 'o' => '0').should == "f0od"
  end

  it "uses the hash's default value for missing keys" do
    hsh = {}
    hsh.default='?'
    hsh['o'] = '0'
    "food".sub(/./, hsh).should == "?ood"
  end

  it "coerces the hash values with #to_s" do
    hsh = {}
    hsh.default=[]
    hsh['o'] = 0
    obj = mock('!')
    obj.should_receive(:to_s).and_return('!')
    hsh['f'] = obj
    "food!".sub(/./, hsh).should == "!ood!"
  end

  it "uses the hash's value set from default_proc for missing keys" do
    hsh = {}
    hsh.default_proc = -> k, v { 'lamb' }
    "food!".sub(/./, hsh).should == "lambood!"
  end

  it "sets $~ to MatchData of first match and nil when there's none for access from outside" do
    'hello.'.sub('l', 'l' => 'L')
    $~.begin(0).should == 2
    $~[0].should == 'l'

    'hello.'.sub('not', 'ot' => 'to')
    $~.should == nil

    'hello.'.sub(/.(.)/, 'o' => ' hole')
    $~[0].should == 'he'

    'hello.'.sub(/not/, 'z' => 'glark')
    $~.should == nil
  end

  it "doesn't interpolate special sequences like \\1 for the block's return value" do
    repl = '\& \0 \1 \` \\\' \+ \\\\ foo'
    "hello".sub(/(.+)/, 'hello' => repl ).should == repl
  end

  it "untrusts the result if the original string is untrusted" do
    str = "Ghana".untrust
    str.sub(/[Aa]na/, 'ana' => '').untrusted?.should be_true
  end

  it "untrusts the result if a hash value is untrusted" do
    str = "Ghana"
    str.sub(/a$/, 'a' => 'di'.untrust).untrusted?.should be_true
  end

  it "taints the result if the original string is tainted" do
    str = "Ghana".taint
    str.sub(/[Aa]na/, 'ana' => '').tainted?.should be_true
  end

  it "taints the result if a hash value is tainted" do
    str = "Ghana"
    str.sub(/a$/, 'a' => 'di'.taint).tainted?.should be_true
  end

end

describe "String#sub! with pattern and Hash" do

  it "returns self with the first occurrence of pattern replaced with the value of the corresponding hash key" do
    "hello".sub!(/./, 'l' => 'L').should == "ello"
    "hello!".sub!(/(.)(.)/, 'he' => 'she ', 'll' => 'said').should == 'she llo!'
    "hello".sub!('l', 'l' => 'el').should == 'heello'
  end

  it "removes keys that don't correspond to matches" do
    "hello".sub!(/./, 'z' => 'b', 'o' => 'ow').should == "ello"
  end

  it "ignores non-String keys" do
    "hello".sub!(/(ll)/, 'll' => 'r', ll: 'z').should == "hero"
  end

  it "uses a key's value only a single time" do
    "food".sub!(/o/, 'o' => '0').should == "f0od"
  end

  it "uses the hash's default value for missing keys" do
    hsh = {}
    hsh.default='?'
    hsh['o'] = '0'
    "food".sub!(/./, hsh).should == "?ood"
  end

  it "coerces the hash values with #to_s" do
    hsh = {}
    hsh.default=[]
    hsh['o'] = 0
    obj = mock('!')
    obj.should_receive(:to_s).and_return('!')
    hsh['f'] = obj
    "food!".sub!(/./, hsh).should == "!ood!"
  end

  it "uses the hash's value set from default_proc for missing keys" do
    hsh = {}
    hsh.default_proc = -> k, v { 'lamb' }
    "food!".sub!(/./, hsh).should == "lambood!"
  end

  it "sets $~ to MatchData of first match and nil when there's none for access from outside" do
    'hello.'.sub!('l', 'l' => 'L')
    $~.begin(0).should == 2
    $~[0].should == 'l'

    'hello.'.sub!('not', 'ot' => 'to')
    $~.should == nil

    'hello.'.sub!(/.(.)/, 'o' => ' hole')
    $~[0].should == 'he'

    'hello.'.sub!(/not/, 'z' => 'glark')
    $~.should == nil
  end

  it "doesn't interpolate special sequences like \\1 for the block's return value" do
    repl = '\& \0 \1 \` \\\' \+ \\\\ foo'
    "hello".sub!(/(.+)/, 'hello' => repl ).should == repl
  end

  it "keeps untrusted state" do
    str = "Ghana".untrust
    str.sub!(/[Aa]na/, 'ana' => '').untrusted?.should be_true
  end

  it "untrusts self if a hash value is untrusted" do
    str = "Ghana"
    str.sub!(/a$/, 'a' => 'di'.untrust).untrusted?.should be_true
  end

  it "keeps tainted state" do
    str = "Ghana".taint
    str.sub!(/[Aa]na/, 'ana' => '').tainted?.should be_true
  end

  it "taints self if a hash value is tainted" do
    str = "Ghana"
    str.sub!(/a$/, 'a' => 'di'.taint).tainted?.should be_true
  end
end

describe "String#sub with pattern and without replacement and block" do
  it "raises a ArgumentError" do
    -> { "abca".sub(/a/) }.should raise_error(ArgumentError)
  end
end

describe "String#sub! with pattern and without replacement and block" do
  it "raises a ArgumentError" do
    -> { "abca".sub!(/a/) }.should raise_error(ArgumentError)
  end
end