File: delete_suffix_spec.rb

package info (click to toggle)
ruby2.5 2.5.5-3%2Bdeb10u4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 101,532 kB
  • sloc: ruby: 732,598; ansic: 669,262; xml: 25,363; yacc: 20,963; javascript: 6,680; sh: 3,610; lisp: 2,627; makefile: 596; python: 198; sed: 76; perl: 62; awk: 36; asm: 35
file content (81 lines) | stat: -rw-r--r-- 2,341 bytes parent folder | download
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
# -*- encoding: utf-8 -*-
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes.rb', __FILE__)

ruby_version_is '2.5' do
  describe "String#delete_suffix" do
    it "returns a copy of the string, with the given suffix removed" do
      'hello'.delete_suffix('ello').should == 'h'
      'hello'.delete_suffix('hello').should == ''
    end

    it "returns a copy of the string, when the suffix isn't found" do
      s = 'hello'
      r = s.delete_suffix('!hello')
      r.should_not equal s
      r.should == s
      r = s.delete_suffix('ell')
      r.should_not equal s
      r.should == s
      r = s.delete_suffix('')
      r.should_not equal s
      r.should == s
    end

    it "taints resulting strings when other is tainted" do
      'hello'.taint.delete_suffix('ello').tainted?.should == true
      'hello'.taint.delete_suffix('').tainted?.should == true
    end

    it "doesn't set $~" do
      $~ = nil

      'hello'.delete_suffix('ello')
      $~.should == nil
    end

    it "calls to_str on its argument" do
      o = mock('x')
      o.should_receive(:to_str).and_return 'ello'
      'hello'.delete_suffix(o).should == 'h'
    end

    it "returns a subclass instance when called on a subclass instance" do
      s = StringSpecs::MyString.new('hello')
      s.delete_suffix('ello').should be_an_instance_of(StringSpecs::MyString)
    end
  end

  describe "String#delete_suffix!" do
    it "removes the found prefix" do
      s = 'hello'
      s.delete_suffix!('ello').should equal(s)
      s.should == 'h'
    end

    it "returns nil if no change is made" do
      s = 'hello'
      s.delete_suffix!('ell').should == nil
      s.delete_suffix!('').should == nil
    end

    it "doesn't set $~" do
      $~ = nil

      'hello'.delete_suffix!('ello')
      $~.should == nil
    end

    it "calls to_str on its argument" do
      o = mock('x')
      o.should_receive(:to_str).and_return 'ello'
      'hello'.delete_suffix!(o).should == 'h'
    end

    it "raises a RuntimeError when self is frozen" do
      lambda { 'hello'.freeze.delete_suffix!('ello') }.should raise_error(RuntimeError)
      lambda { 'hello'.freeze.delete_suffix!('') }.should raise_error(RuntimeError)
      lambda { ''.freeze.delete_suffix!('') }.should raise_error(RuntimeError)
    end
  end
end