summaryrefslogtreecommitdiff
path: root/spec/ruby/core/string/chomp_spec.rb
blob: 3d6207f876a3c01b7bf7fe37e3d9306bce7e1ed1 (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
# -*- encoding: utf-8 -*-
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe "String#chomp" do
  describe "when passed no argument" do
    before do
      # Ensure that $/ is set to the default value
      @verbose, $VERBOSE = $VERBOSE, nil
      @dollar_slash, $/ = $/, "\n"
    end

    after do
      $/ = @dollar_slash
      $VERBOSE = @verbose
    end

    it "does not modify a String with no trailing carriage return or newline" do
      "abc".chomp.should == "abc"
    end

    it "returns a copy of the String when it is not modified" do
      str = "abc"
      str.chomp.should_not equal(str)
    end

    it "removes one trailing newline" do
      "abc\n\n".chomp.should == "abc\n"
    end

    it "removes one trailing carriage return" do
      "abc\r\r".chomp.should == "abc\r"
    end

    it "removes one trailing carriage return, newline pair" do
      "abc\r\n\r\n".chomp.should == "abc\r\n"
    end

    it "returns an empty String when self is empty" do
      "".chomp.should == ""
    end

    ruby_version_is ''...'2.7' do
      it "taints the result if self is tainted" do
        "abc".taint.chomp.tainted?.should be_true
      end
    end

    ruby_version_is ''...'3.0' do
      it "returns subclass instances when called on a subclass" do
        str = StringSpecs::MyString.new("hello\n").chomp
        str.should be_an_instance_of(StringSpecs::MyString)
      end
    end

    ruby_version_is '3.0' do
      it "returns String instances when called on a subclass" do
        str = StringSpecs::MyString.new("hello\n").chomp
        str.should be_an_instance_of(String)
      end
    end

    it "removes trailing characters that match $/ when it has been assigned a value" do
      $/ = "cdef"
      "abcdef".chomp.should == "ab"
    end

    it "removes one trailing newline for string with invalid encoding" do
      "\xa0\xa1\n".chomp.should == "\xa0\xa1"
    end
  end

  describe "when passed nil" do
    it "does not modify the String" do
      "abc\r\n".chomp(nil).should == "abc\r\n"
    end

    it "returns a copy of the String" do
      str = "abc"
      str.chomp(nil).should_not equal(str)
    end

    ruby_version_is ''...'2.7' do
      it "taints the result if self is tainted" do
        "abc".taint.chomp(nil).tainted?.should be_true
      end
    end

    it "returns an empty String when self is empty" do
      "".chomp(nil).should == ""
    end
  end

  describe "when passed ''" do
    it "removes a final newline" do
      "abc\n".chomp("").should == "abc"
    end

    it "removes a final carriage return, newline" do
      "abc\r\n".chomp("").should == "abc"
    end

    it "does not remove a final carriage return" do
      "abc\r".chomp("").should == "abc\r"
    end

    it "removes more than one trailing newlines" do
      "abc\n\n\n".chomp("").should == "abc"
    end

    it "removes more than one trailing carriage return, newline pairs" do
      "abc\r\n\r\n\r\n".chomp("").should == "abc"
    end

    ruby_version_is ''...'2.7' do
      it "taints the result if self is tainted" do
        "abc".taint.chomp("").tainted?.should be_true
      end
    end

    it "returns an empty String when self is empty" do
      "".chomp("").should == ""
    end

    it "removes one trailing newline for string with invalid encoding" do
      "\xa0\xa1\n".chomp("").should == "\xa0\xa1"
    end
  end

  describe "when passed '\\n'" do
    it "removes one trailing newline" do
      "abc\n\n".chomp("\n").should == "abc\n"
    end

    it "removes one trailing carriage return" do
      "abc\r\r".chomp("\n").should == "abc\r"
    end

    it "removes one trailing carriage return, newline pair" do
      "abc\r\n\r\n".chomp("\n").should == "abc\r\n"
    end

    ruby_version_is ''...'2.7' do
      it "taints the result if self is tainted" do
        "abc".taint.chomp("\n").tainted?.should be_true
      end
    end

    it "returns an empty String when self is empty" do
      "".chomp("\n").should == ""
    end
  end

  describe "when passed an Object" do
    it "calls #to_str to convert to a String" do
      arg = mock("string chomp")
      arg.should_receive(:to_str).and_return("bc")
      "abc".chomp(arg).should == "a"
    end

    it "raises a TypeError if #to_str does not return a String" do
      arg = mock("string chomp")
      arg.should_receive(:to_str).and_return(1)
      -> { "abc".chomp(arg) }.should raise_error(TypeError)
    end
  end

  describe "when passed a String" do
    it "removes the trailing characters if they match the argument" do
      "abcabc".chomp("abc").should == "abc"
    end

    it "does not modify the String if the argument does not match the trailing characters" do
      "abc".chomp("def").should == "abc"
    end

    it "returns an empty String when self is empty" do
      "".chomp("abc").should == ""
    end

    ruby_version_is ''...'2.7' do
      it "taints the result if self is tainted" do
        "abc".taint.chomp("abc").tainted?.should be_true
      end

      it "does not taint the result when the argument is tainted" do
        "abc".chomp("abc".taint).tainted?.should be_false
      end
    end

    it "returns an empty String when the argument equals self" do
      "abc".chomp("abc").should == ""
    end
  end
end

describe "String#chomp!" do
  describe "when passed no argument" do
    before do
      # Ensure that $/ is set to the default value
      @verbose, $VERBOSE = $VERBOSE, nil
      @dollar_slash, $/ = $/, "\n"
    end

    after do
      $/ = @dollar_slash
      $VERBOSE = @verbose
    end

    it "modifies self" do
      str = "abc\n"
      str.chomp!.should equal(str)
    end

    it "returns nil if self is not modified" do
      "abc".chomp!.should be_nil
    end

    it "removes one trailing newline" do
      "abc\n\n".chomp!.should == "abc\n"
    end

    it "removes one trailing carriage return" do
      "abc\r\r".chomp!.should == "abc\r"
    end

    it "removes one trailing carriage return, newline pair" do
      "abc\r\n\r\n".chomp!.should == "abc\r\n"
    end

    it "returns nil when self is empty" do
      "".chomp!.should be_nil
    end

    ruby_version_is ''...'2.7' do
      it "taints the result if self is tainted" do
        "abc\n".taint.chomp!.tainted?.should be_true
      end
    end

    it "returns subclass instances when called on a subclass" do
      str = StringSpecs::MyString.new("hello\n").chomp!
      str.should be_an_instance_of(StringSpecs::MyString)
    end

    it "removes trailing characters that match $/ when it has been assigned a value" do
      $/ = "cdef"
      "abcdef".chomp!.should == "ab"
    end
  end

  describe "when passed nil" do
    it "returns nil" do
      "abc\r\n".chomp!(nil).should be_nil
    end

    it "returns nil when self is empty" do
      "".chomp!(nil).should be_nil
    end
  end

  describe "when passed ''" do
    it "removes a final newline" do
      "abc\n".chomp!("").should == "abc"
    end

    it "removes a final carriage return, newline" do
      "abc\r\n".chomp!("").should == "abc"
    end

    it "does not remove a final carriage return" do
      "abc\r".chomp!("").should be_nil
    end

    it "removes more than one trailing newlines" do
      "abc\n\n\n".chomp!("").should == "abc"
    end

    it "removes more than one trailing carriage return, newline pairs" do
      "abc\r\n\r\n\r\n".chomp!("").should == "abc"
    end

    ruby_version_is ''...'2.7' do
      it "taints the result if self is tainted" do
        "abc\n".taint.chomp!("").tainted?.should be_true
      end
    end

    it "returns nil when self is empty" do
      "".chomp!("").should be_nil
    end
  end

  describe "when passed '\\n'" do
    it "removes one trailing newline" do
      "abc\n\n".chomp!("\n").should == "abc\n"
    end

    it "removes one trailing carriage return" do
      "abc\r\r".chomp!("\n").should == "abc\r"
    end

    it "removes one trailing carriage return, newline pair" do
      "abc\r\n\r\n".chomp!("\n").should == "abc\r\n"
    end

    ruby_version_is ''...'2.7' do
      it "taints the result if self is tainted" do
        "abc\n".taint.chomp!("\n").tainted?.should be_true
      end
    end

    it "returns nil when self is empty" do
      "".chomp!("\n").should be_nil
    end
  end

  describe "when passed an Object" do
    it "calls #to_str to convert to a String" do
      arg = mock("string chomp")
      arg.should_receive(:to_str).and_return("bc")
      "abc".chomp!(arg).should == "a"
    end

    it "raises a TypeError if #to_str does not return a String" do
      arg = mock("string chomp")
      arg.should_receive(:to_str).and_return(1)
      -> { "abc".chomp!(arg) }.should raise_error(TypeError)
    end
  end

  describe "when passed a String" do
    it "removes the trailing characters if they match the argument" do
      "abcabc".chomp!("abc").should == "abc"
    end

    it "returns nil if the argument does not match the trailing characters" do
      "abc".chomp!("def").should be_nil
    end

    it "returns nil when self is empty" do
      "".chomp!("abc").should be_nil
    end

    ruby_version_is ''...'2.7' do
      it "taints the result if self is tainted" do
        "abc".taint.chomp!("abc").tainted?.should be_true
      end

      it "does not taint the result when the argument is tainted" do
        "abc".chomp!("abc".taint).tainted?.should be_false
      end
    end
  end

  it "raises a FrozenError on a frozen instance when it is modified" do
    a = "string\n\r"
    a.freeze

    -> { a.chomp! }.should raise_error(FrozenError)
  end

  # see [ruby-core:23666]
  it "raises a FrozenError on a frozen instance when it would not be modified" do
    a = "string\n\r"
    a.freeze
    -> { a.chomp!(nil) }.should raise_error(FrozenError)
    -> { a.chomp!("x") }.should raise_error(FrozenError)
  end
end

describe "String#chomp" do
  before :each do
    @verbose, $VERBOSE = $VERBOSE, nil
    @before_separator = $/
  end

  after :each do
    $/ = @before_separator
    $VERBOSE = @verbose
  end

  it "does not modify a multi-byte character" do
    "あれ".chomp.should == "あれ"
  end

  it "removes the final carriage return, newline from a multibyte String" do
    "あれ\r\n".chomp.should == "あれ"
  end

  it "removes the final carriage return, newline from a non-ASCII String" do
    str = "abc\r\n".encode "utf-32be"
    str.chomp.should == "abc".encode("utf-32be")
  end

  it "removes the final carriage return, newline from a non-ASCII String when the record separator is changed" do
    $/ = "\n".encode("utf-8")
    str = "abc\r\n".encode "utf-32be"
    str.chomp.should == "abc".encode("utf-32be")
  end
end

describe "String#chomp!" do
  before :each do
    @verbose, $VERBOSE = $VERBOSE, nil
    @before_separator = $/
  end

  after :each do
    $/ = @before_separator
    $VERBOSE = @verbose
  end

  it "returns nil when the String is not modified" do
    "あれ".chomp!.should be_nil
  end

  it "removes the final carriage return, newline from a multibyte String" do
    "あれ\r\n".chomp!.should == "あれ"
  end

  it "removes the final carriage return, newline from a non-ASCII String" do
    str = "abc\r\n".encode "utf-32be"
    str.chomp!.should == "abc".encode("utf-32be")
  end

  it "removes the final carriage return, newline from a non-ASCII String when the record separator is changed" do
    $/ = "\n".encode("utf-8")
    str = "abc\r\n".encode "utf-32be"
    str.chomp!.should == "abc".encode("utf-32be")
  end
end