summaryrefslogtreecommitdiff
path: root/spec/ruby/core/string/split_spec.rb
blob: a373be360d90c38ea0ae71b34e4ad7cba3cba5dc (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
572
573
574
575
576
577
# -*- encoding: utf-8 -*-
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe "String#split with String" do
  it "throws an ArgumentError if the pattern is not a valid string" do
    str = 'проверка'
    broken_str = 'проверка'
    broken_str.force_encoding('binary')
    broken_str.chop!
    broken_str.force_encoding('utf-8')
    -> { str.split(broken_str) }.should raise_error(ArgumentError)
  end

  it "splits on multibyte characters" do
    "ありがりがとう".split("が").should == ["あり", "り", "とう"]
  end

  it "returns an array of substrings based on splitting on the given string" do
    "mellow yellow".split("ello").should == ["m", "w y", "w"]
  end

  it "suppresses trailing empty fields when limit isn't given or 0" do
    "1,2,,3,4,,".split(',').should == ["1", "2", "", "3", "4"]
    "1,2,,3,4,,".split(',', 0).should == ["1", "2", "", "3", "4"]
    "  a  b  c\nd  ".split("  ").should == ["", "a", "b", "c\nd"]
    "hai".split("hai").should == []
    ",".split(",").should == []
    ",".split(",", 0).should == []
  end

  it "returns an array with one entry if limit is 1: the original string" do
    "hai".split("hai", 1).should == ["hai"]
    "x.y.z".split(".", 1).should == ["x.y.z"]
    "hello world ".split(" ", 1).should == ["hello world "]
    "hi!".split("", 1).should == ["hi!"]
  end

  it "returns at most limit fields when limit > 1" do
    "hai".split("hai", 2).should == ["", ""]

    "1,2".split(",", 3).should == ["1", "2"]

    "1,2,,3,4,,".split(',', 2).should == ["1", "2,,3,4,,"]
    "1,2,,3,4,,".split(',', 3).should == ["1", "2", ",3,4,,"]
    "1,2,,3,4,,".split(',', 4).should == ["1", "2", "", "3,4,,"]
    "1,2,,3,4,,".split(',', 5).should == ["1", "2", "", "3", "4,,"]
    "1,2,,3,4,,".split(',', 6).should == ["1", "2", "", "3", "4", ","]

    "x".split('x', 2).should == ["", ""]
    "xx".split('x', 2).should == ["", "x"]
    "xx".split('x', 3).should == ["", "", ""]
    "xxx".split('x', 2).should == ["", "xx"]
    "xxx".split('x', 3).should == ["", "", "x"]
    "xxx".split('x', 4).should == ["", "", "", ""]
  end

  it "doesn't suppress or limit fields when limit is negative" do
    "1,2,,3,4,,".split(',', -1).should == ["1", "2", "", "3", "4", "", ""]
    "1,2,,3,4,,".split(',', -5).should == ["1", "2", "", "3", "4", "", ""]
    "  a  b  c\nd  ".split("  ", -1).should == ["", "a", "b", "c\nd", ""]
    ",".split(",", -1).should == ["", ""]
  end

  it "defaults to $; when string isn't given or nil" do
    suppress_warning do
      old_fs = $;
      begin
        [",", ":", "", "XY", nil].each do |fs|
          $; = fs

          ["x,y,z,,,", "1:2:", "aXYbXYcXY", ""].each do |str|
            expected = str.split(fs || " ")

            str.split(nil).should == expected
            str.split.should == expected

            str.split(nil, -1).should == str.split(fs || " ", -1)
            str.split(nil, 0).should == str.split(fs || " ", 0)
            str.split(nil, 2).should == str.split(fs || " ", 2)
          end
        end
      ensure
        $; = old_fs
      end
    end

    ruby_version_is "2.7" do
      context "when $; is not nil" do
        before do
          suppress_warning do
            @old_value, $; = $;, 'foobar'
          end
        end

        after do
          $; = @old_value
        end

        it "warns" do
          -> { "".split }.should complain(/warning: \$; is set to non-nil value/)
        end
      end
    end
  end

  it "ignores leading and continuous whitespace when string is a single space" do
    " now's  the time  ".split(' ').should == ["now's", "the", "time"]
    " now's  the time  ".split(' ', -1).should == ["now's", "the", "time", ""]
    " now's  the time  ".split(' ', 3).should == ["now's", "the", "time  "]

    "\t\n a\t\tb \n\r\r\nc\v\vd\v ".split(' ').should == ["a", "b", "c", "d"]
    "a\x00a b".split(' ').should == ["a\x00a", "b"]
  end

  describe "when limit is zero" do
    it "ignores leading and continuous whitespace when string is a single space" do
      " now's  the time  ".split(' ', 0).should == ["now's", "the", "time"]
    end
  end

  it "splits between characters when its argument is an empty string" do
    "hi!".split("").should == ["h", "i", "!"]
    "hi!".split("", -1).should == ["h", "i", "!", ""]
    "hi!".split("", 0).should == ["h", "i", "!"]
    "hi!".split("", 1).should == ["hi!"]
    "hi!".split("", 2).should == ["h", "i!"]
    "hi!".split("", 3).should == ["h", "i", "!"]
    "hi!".split("", 4).should == ["h", "i", "!", ""]
    "hi!".split("", 5).should == ["h", "i", "!", ""]
  end

  it "tries converting its pattern argument to a string via to_str" do
    obj = mock('::')
    obj.should_receive(:to_str).and_return("::")

    "hello::world".split(obj).should == ["hello", "world"]
  end

  it "tries converting limit to an integer via to_int" do
    obj = mock('2')
    obj.should_receive(:to_int).and_return(2)

    "1.2.3.4".split(".", obj).should == ["1", "2.3.4"]
  end

  it "doesn't set $~" do
    $~ = nil
    "x.y.z".split(".")
    $~.should == nil
  end

  it "returns the original string if no matches are found" do
    "foo".split("bar").should == ["foo"]
    "foo".split("bar", -1).should == ["foo"]
    "foo".split("bar", 0).should == ["foo"]
    "foo".split("bar", 1).should == ["foo"]
    "foo".split("bar", 2).should == ["foo"]
    "foo".split("bar", 3).should == ["foo"]
  end

  ruby_version_is ''...'3.0' do
    it "returns subclass instances based on self" do
      ["", "x.y.z.", "  x  y  "].each do |str|
        ["", ".", " "].each do |pat|
          [-1, 0, 1, 2].each do |limit|
            StringSpecs::MyString.new(str).split(pat, limit).each do |x|
              x.should be_an_instance_of(StringSpecs::MyString)
            end

            str.split(StringSpecs::MyString.new(pat), limit).each do |x|
              x.should be_an_instance_of(String)
            end
          end
        end
      end
    end

    it "does not call constructor on created subclass instances" do
      # can't call should_not_receive on an object that doesn't yet exist
      # so failure here is signalled by exception, not expectation failure

      s = StringSpecs::StringWithRaisingConstructor.new('silly:string')
      s.split(':').first.should == 'silly'
    end
  end

  ruby_version_is '3.0' do
    it "returns String instances based on self" do
      ["", "x.y.z.", "  x  y  "].each do |str|
        ["", ".", " "].each do |pat|
          [-1, 0, 1, 2].each do |limit|
            StringSpecs::MyString.new(str).split(pat, limit).each do |x|
              x.should be_an_instance_of(String)
            end

            str.split(StringSpecs::MyString.new(pat), limit).each do |x|
              x.should be_an_instance_of(String)
            end
          end
        end
      end
    end
  end

  ruby_version_is ''...'2.7' do
    it "taints the resulting strings if self is tainted" do
      ["", "x.y.z.", "  x  y  "].each do |str|
        ["", ".", " "].each do |pat|
          [-1, 0, 1, 2].each do |limit|
            str.dup.taint.split(pat).each do |x|
              x.should.tainted?
            end

            str.split(pat.dup.taint).each do |x|
              x.should_not.tainted?
            end
          end
        end
      end
    end
  end
end

describe "String#split with Regexp" do
  it "divides self on regexp matches" do
    " now's  the time".split(/ /).should == ["", "now's", "", "the", "time"]
    " x\ny ".split(/ /).should == ["", "x\ny"]
    "1, 2.34,56, 7".split(/,\s*/).should == ["1", "2.34", "56", "7"]
    "1x2X3".split(/x/i).should == ["1", "2", "3"]
  end

  it "treats negative limits as no limit" do
    "".split(%r!/+!, -1).should == []
  end

  it "suppresses trailing empty fields when limit isn't given or 0" do
    "1,2,,3,4,,".split(/,/).should == ["1", "2", "", "3", "4"]
    "1,2,,3,4,,".split(/,/, 0).should == ["1", "2", "", "3", "4"]
    "  a  b  c\nd  ".split(/\s+/).should == ["", "a", "b", "c", "d"]
    "hai".split(/hai/).should == []
    ",".split(/,/).should == []
    ",".split(/,/, 0).should == []
  end

  it "returns an array with one entry if limit is 1: the original string" do
    "hai".split(/hai/, 1).should == ["hai"]
    "xAyBzC".split(/[A-Z]/, 1).should == ["xAyBzC"]
    "hello world ".split(/\s+/, 1).should == ["hello world "]
    "hi!".split(//, 1).should == ["hi!"]
  end

  it "returns at most limit fields when limit > 1" do
    "hai".split(/hai/, 2).should == ["", ""]

    "1,2".split(/,/, 3).should == ["1", "2"]

    "1,2,,3,4,,".split(/,/, 2).should == ["1", "2,,3,4,,"]
    "1,2,,3,4,,".split(/,/, 3).should == ["1", "2", ",3,4,,"]
    "1,2,,3,4,,".split(/,/, 4).should == ["1", "2", "", "3,4,,"]
    "1,2,,3,4,,".split(/,/, 5).should == ["1", "2", "", "3", "4,,"]
    "1,2,,3,4,,".split(/,/, 6).should == ["1", "2", "", "3", "4", ","]

    "x".split(/x/, 2).should == ["", ""]
    "xx".split(/x/, 2).should == ["", "x"]
    "xx".split(/x/, 3).should == ["", "", ""]
    "xxx".split(/x/, 2).should == ["", "xx"]
    "xxx".split(/x/, 3).should == ["", "", "x"]
    "xxx".split(/x/, 4).should == ["", "", "", ""]
  end

  it "doesn't suppress or limit fields when limit is negative" do
    "1,2,,3,4,,".split(/,/, -1).should == ["1", "2", "", "3", "4", "", ""]
    "1,2,,3,4,,".split(/,/, -5).should == ["1", "2", "", "3", "4", "", ""]
    "  a  b  c\nd  ".split(/\s+/, -1).should == ["", "a", "b", "c", "d", ""]
    ",".split(/,/, -1).should == ["", ""]
  end

  it "defaults to $; when regexp isn't given or nil" do
    suppress_warning do
      old_fs = $;
      begin
        [/,/, /:/, //, /XY/, /./].each do |fs|
          $; = fs

          ["x,y,z,,,", "1:2:", "aXYbXYcXY", ""].each do |str|
            expected = str.split(fs)

            str.split(nil).should == expected
            str.split.should == expected

            str.split(nil, -1).should == str.split(fs, -1)
            str.split(nil, 0).should == str.split(fs, 0)
            str.split(nil, 2).should == str.split(fs, 2)
          end
        end
      ensure
        $; = old_fs
      end
    end
  end

  it "splits between characters when regexp matches a zero-length string" do
    "hello".split(//).should == ["h", "e", "l", "l", "o"]
    "hello".split(//, -1).should == ["h", "e", "l", "l", "o", ""]
    "hello".split(//, 0).should == ["h", "e", "l", "l", "o"]
    "hello".split(//, 1).should == ["hello"]
    "hello".split(//, 2).should == ["h", "ello"]
    "hello".split(//, 5).should == ["h", "e", "l", "l", "o"]
    "hello".split(//, 6).should == ["h", "e", "l", "l", "o", ""]
    "hello".split(//, 7).should == ["h", "e", "l", "l", "o", ""]

    "hi mom".split(/\s*/).should == ["h", "i", "m", "o", "m"]

    "AABCCBAA".split(/(?=B)/).should == ["AA", "BCC", "BAA"]
    "AABCCBAA".split(/(?=B)/, -1).should == ["AA", "BCC", "BAA"]
    "AABCCBAA".split(/(?=B)/, 2).should == ["AA", "BCCBAA"]
  end

  it "respects unicode when splitting between characters" do
    str = "こにちわ"
    reg = %r!!
    ary = str.split(reg)
    ary.size.should == 4
    ary.should == ["こ", "に", "ち", "わ"]
  end

  it "respects the encoding of the regexp when splitting between characters" do
    str = "\303\202"
    ary = str.split(//u)
    ary.size.should == 1
    ary.should == ["\303\202"]
  end

  it "includes all captures in the result array" do
    "hello".split(/(el)/).should == ["h", "el", "lo"]
    "hi!".split(/()/).should == ["h", "", "i", "", "!"]
    "hi!".split(/()/, -1).should == ["h", "", "i", "", "!", "", ""]
    "hello".split(/((el))()/).should == ["h", "el", "el", "", "lo"]
    "AabB".split(/([a-z])+/).should == ["A", "b", "B"]
  end

  it "applies the limit to the number of split substrings, without counting captures" do
    "aBaBa".split(/(B)()()/, 2).should == ["a", "B", "", "", "aBa"]
  end

  it "does not include non-matching captures in the result array" do
    "hello".split(/(el)|(xx)/).should == ["h", "el", "lo"]
  end

  it "tries converting limit to an integer via to_int" do
    obj = mock('2')
    obj.should_receive(:to_int).and_return(2)

    "1.2.3.4".split(".", obj).should == ["1", "2.3.4"]
  end

  it "returns a type error if limit can't be converted to an integer" do
    -> {"1.2.3.4".split(".", "three")}.should raise_error(TypeError)
    -> {"1.2.3.4".split(".", nil)    }.should raise_error(TypeError)
  end

  it "doesn't set $~" do
    $~ = nil
    "x:y:z".split(/:/)
    $~.should == nil
  end

  it "returns the original string if no matches are found" do
    "foo".split(/bar/).should == ["foo"]
    "foo".split(/bar/, -1).should == ["foo"]
    "foo".split(/bar/, 0).should == ["foo"]
    "foo".split(/bar/, 1).should == ["foo"]
    "foo".split(/bar/, 2).should == ["foo"]
    "foo".split(/bar/, 3).should == ["foo"]
  end

  ruby_version_is ''...'3.0' do
    it "returns subclass instances based on self" do
      ["", "x:y:z:", "  x  y  "].each do |str|
        [//, /:/, /\s+/].each do |pat|
          [-1, 0, 1, 2].each do |limit|
            StringSpecs::MyString.new(str).split(pat, limit).each do |x|
              x.should be_an_instance_of(StringSpecs::MyString)
            end
          end
        end
      end
    end

    it "does not call constructor on created subclass instances" do
      # can't call should_not_receive on an object that doesn't yet exist
      # so failure here is signalled by exception, not expectation failure

      s = StringSpecs::StringWithRaisingConstructor.new('silly:string')
      s.split(/:/).first.should == 'silly'
    end
  end

  ruby_version_is '3.0' do
    it "returns String instances based on self" do
      ["", "x:y:z:", "  x  y  "].each do |str|
        [//, /:/, /\s+/].each do |pat|
          [-1, 0, 1, 2].each do |limit|
            StringSpecs::MyString.new(str).split(pat, limit).each do |x|
              x.should be_an_instance_of(String)
            end
          end
        end
      end
    end
  end

  ruby_version_is ''...'2.7' do
    it "taints the resulting strings if self is tainted" do
      ["", "x:y:z:", "  x  y  "].each do |str|
        [//, /:/, /\s+/].each do |pat|
          [-1, 0, 1, 2].each do |limit|
            str.dup.taint.split(pat, limit).each do |x|
              # See the spec below for why the conditional is here
              x.tainted?.should be_true unless x.empty?
            end
          end
        end
      end
    end

    it "taints an empty string if self is tainted" do
      ":".taint.split(//, -1).last.tainted?.should be_true
    end

    it "doesn't taints the resulting strings if the Regexp is tainted" do
      ["", "x:y:z:", "  x  y  "].each do |str|
        [//, /:/, /\s+/].each do |pat|
          [-1, 0, 1, 2].each do |limit|
            str.split(pat.dup.taint, limit).each do |x|
              x.tainted?.should be_false
            end
          end
        end
      end
    end
  end

  it "retains the encoding of the source string" do
    ary = "а б в".split
    encodings = ary.map { |s| s.encoding }
    encodings.should == [Encoding::UTF_8, Encoding::UTF_8, Encoding::UTF_8]
  end


  it "splits a string on each character for a multibyte encoding and empty split" do
    "That's why efficiency could not be helped".split("").size.should == 39
  end

  it "returns an ArgumentError if an invalid UTF-8 string is supplied" do
    broken_str = 'проверка' # in russian, means "test"
    broken_str.force_encoding('binary')
    broken_str.chop!
    broken_str.force_encoding('utf-8')
    ->{ broken_str.split(/\r\n|\r|\n/) }.should raise_error(ArgumentError)
  end

  # See https://bugs.ruby-lang.org/issues/12689 and https://github.com/jruby/jruby/issues/4868
  it "allows concurrent Regexp calls in a shared context" do
    str = 'a,b,c,d,e'

    p = proc { str.split(/,/) }
    results = 10.times.map { Thread.new { x = nil; 100.times { x = p.call }; x } }.map(&:value)

    results.should == [%w[a b c d e]] * 10
  end

  context "when a block is given" do
    it "yields each split substring with default pattern" do
      a = []
      returned_object = "chunky bacon".split { |str| a << str.capitalize }

      returned_object.should == "chunky bacon"
      a.should == ["Chunky", "Bacon"]
    end

    it "yields each split substring with default pattern for a non-ASCII string" do
      a = []
      returned_object = "l'été arrive bientôt".split { |str| a << str }

      returned_object.should == "l'été arrive bientôt"
      a.should == ["l'été", "arrive", "bientôt"]
    end

    it "yields the string when limit is 1" do
      a = []
      returned_object = "chunky bacon".split("", 1) { |str| a << str.capitalize }

      returned_object.should == "chunky bacon"
      a.should == ["Chunky bacon"]
    end

    it "yields each split letter" do
      a = []
      returned_object = "chunky".split("", 0) { |str| a << str.capitalize }

      returned_object.should == "chunky"
      a.should == %w(C H U N K Y)
    end

    it "yields each split substring with a pattern" do
      a = []
      returned_object = "chunky-bacon".split("-", 0) { |str| a << str.capitalize }

      returned_object.should == "chunky-bacon"
      a.should == ["Chunky", "Bacon"]
    end

    it "yields each split substring with empty regexp pattern" do
      a = []
      returned_object = "chunky".split(//) { |str| a << str.capitalize }

      returned_object.should == "chunky"
      a.should == %w(C H U N K Y)
    end

    it "yields each split substring with empty regexp pattern and limit" do
      a = []
      returned_object = "chunky".split(//, 3) { |str| a << str.capitalize }

      returned_object.should == "chunky"
      a.should == %w(C H Unky)
    end

    it "yields each split substring with a regexp pattern" do
      a = []
      returned_object = "chunky:bacon".split(/:/) { |str| a << str.capitalize }

      returned_object.should == "chunky:bacon"
      a.should == ["Chunky", "Bacon"]
    end

    it "returns a string as is (and doesn't call block) if it is empty" do
      a = []
      returned_object = "".split { |str| a << str.capitalize }

      returned_object.should == ""
      a.should == []
    end
  end

  describe "for a String subclass" do
    ruby_version_is ''...'3.0' do
      it "yields instances of the same subclass" do
        a = []
        StringSpecs::MyString.new("a|b").split("|") { |str| a << str }
        first, last = a

        first.should be_an_instance_of(StringSpecs::MyString)
        first.should == "a"

        last.should be_an_instance_of(StringSpecs::MyString)
        last.should == "b"
      end
    end

    ruby_version_is '3.0' do
      it "yields instances of String" do
        a = []
        StringSpecs::MyString.new("a|b").split("|") { |str| a << str }
        first, last = a

        first.should be_an_instance_of(String)
        first.should == "a"

        last.should be_an_instance_of(String)
        last.should == "b"
      end
    end
  end
end