File: delete_spec.rb

package info (click to toggle)
jruby 1.7.26-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,572 kB
  • sloc: ruby: 669,910; java: 253,056; xml: 35,152; ansic: 9,187; yacc: 7,267; cpp: 5,244; sh: 1,036; makefile: 345; jsp: 48; tcl: 40
file content (131 lines) | stat: -rw-r--r-- 4,137 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
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
# -*- encoding: US-ASCII -*-
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes.rb', __FILE__)

describe "String#delete" do
  it "returns a new string with the chars from the intersection of sets removed" do
    s = "hello"
    s.delete("lo").should == "he"
    s.should == "hello"

    "hello".delete("l", "lo").should == "heo"

    "hell yeah".delete("").should == "hell yeah"
  end

  it "raises an ArgumentError when given no arguments" do
    lambda { "hell yeah".delete }.should raise_error(ArgumentError)
  end

  it "negates sets starting with ^" do
    "hello".delete("aeiou", "^e").should == "hell"
    "hello".delete("^leh").should == "hell"
    "hello".delete("^o").should == "o"
    "hello".delete("^").should == "hello"
    "^_^".delete("^^").should == "^^"
    "oa^_^o".delete("a^").should == "o_o"
  end

  it "deletes all chars in a sequence" do
    "hello".delete("\x00-\xFF").should == ""
    "hello".delete("ej-m").should == "ho"
    "hello".delete("e-h").should == "llo"
    "hel-lo".delete("e-").should == "hllo"
    "hel-lo".delete("-h").should == "ello"
    "hel-lo".delete("---").should == "hello"
    "hel-012".delete("--2").should == "hel"
    "hel-()".delete("(--").should == "hel"
    "hello".delete("^e-h").should == "he"
    "hello^".delete("^^-^").should == "^"
    "hel--lo".delete("^---").should == "--"

    "abcdefgh".delete("a-ce-fh").should == "dg"
    "abcdefgh".delete("he-fa-c").should == "dg"
    "abcdefgh".delete("e-fha-c").should == "dg"

    "abcde".delete("ac-e").should == "b"
    "abcde".delete("^ac-e").should == "acde"

    "ABCabc[]".delete("A-a").should == "bc"
  end

  it "respects backslash for escaping a -" do
    'Non-Authoritative Information'.delete(' \-\'').should ==
      'NonAuthoritativeInformation'
  end

  ruby_version_is ""..."1.9" do
    it "regards invalid ranges as nothing" do
      "hello".delete("h-e").should == "hello"
      "hello".delete("^h-e").should == ""
    end
  end

  ruby_version_is "1.9" do
    it "raises if the given ranges are invalid" do
      lambda { "hello".delete("h-e") }.should raise_error(ArgumentError)
      lambda { "hello".delete("^h-e") }.should raise_error(ArgumentError)
    end
  end

  it "taints result when self is tainted" do
    "hello".taint.delete("e").tainted?.should == true
    "hello".taint.delete("a-z").tainted?.should == true

    "hello".delete("e".taint).tainted?.should == false
  end

  it "tries to convert each set arg to a string using to_str" do
    other_string = mock('lo')
    other_string.should_receive(:to_str).and_return("lo")

    other_string2 = mock('o')
    other_string2.should_receive(:to_str).and_return("o")

    "hello world".delete(other_string, other_string2).should == "hell wrld"
  end

  it "raises a TypeError when one set arg can't be converted to a string" do
    lambda { "hello world".delete(100)       }.should raise_error(TypeError)
    lambda { "hello world".delete([])        }.should raise_error(TypeError)
    lambda { "hello world".delete(mock('x')) }.should raise_error(TypeError)
  end

  it "returns subclass instances when called on a subclass" do
    StringSpecs::MyString.new("oh no!!!").delete("!").should be_kind_of(StringSpecs::MyString)
  end
end

describe "String#delete!" do
  it "modifies self in place and returns self" do
    a = "hello"
    a.delete!("aeiou", "^e").should equal(a)
    a.should == "hell"
  end

  it "returns nil if no modifications were made" do
    a = "hello"
    a.delete!("z").should == nil
    a.should == "hello"
  end

  ruby_version_is ""..."1.9" do
    it "raises a TypeError when self is frozen" do
      a = "hello"
      a.freeze

      lambda { a.delete!("")            }.should raise_error(TypeError)
      lambda { a.delete!("aeiou", "^e") }.should raise_error(TypeError)
    end
  end

  ruby_version_is "1.9" do
    it "raises a RuntimeError when self is frozen" do
      a = "hello"
      a.freeze

      lambda { a.delete!("")            }.should raise_error(RuntimeError)
      lambda { a.delete!("aeiou", "^e") }.should raise_error(RuntimeError)
    end
  end
end