File: slice.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 (410 lines) | stat: -rw-r--r-- 14,183 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
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
describe :string_slice, :shared => true do
  it "returns the character code of the character at the given index" do
    "hello".send(@method, 0).should == ?h
    "hello".send(@method, -1).should == ?o
  end

  it "returns nil if index is outside of self" do
    "hello".send(@method, 20).should == nil
    "hello".send(@method, -20).should == nil

    "".send(@method, 0).should == nil
    "".send(@method, -1).should == nil
  end

  it "calls to_int on the given index" do
    "hello".send(@method, 0.5).should == ?h

    obj = mock('1')
    obj.should_receive(:to_int).and_return(1)
    "hello".send(@method, obj).should == ?e
  end

  it "raises a TypeError if the given index is nil" do
    lambda { "hello".send(@method, nil) }.should raise_error(TypeError)
  end

  it "raises a TypeError if the given index can't be converted to an Integer" do
    lambda { "hello".send(@method, mock('x')) }.should raise_error(TypeError)
    lambda { "hello".send(@method, {})        }.should raise_error(TypeError)
    lambda { "hello".send(@method, [])        }.should raise_error(TypeError)
  end
end

describe :string_slice_index_length, :shared => true do
  it "returns the substring starting at the given index with the given length" do
    "hello there".send(@method, 0,0).should == ""
    "hello there".send(@method, 0,1).should == "h"
    "hello there".send(@method, 0,3).should == "hel"
    "hello there".send(@method, 0,6).should == "hello "
    "hello there".send(@method, 0,9).should == "hello the"
    "hello there".send(@method, 0,12).should == "hello there"

    "hello there".send(@method, 1,0).should == ""
    "hello there".send(@method, 1,1).should == "e"
    "hello there".send(@method, 1,3).should == "ell"
    "hello there".send(@method, 1,6).should == "ello t"
    "hello there".send(@method, 1,9).should == "ello ther"
    "hello there".send(@method, 1,12).should == "ello there"

    "hello there".send(@method, 3,0).should == ""
    "hello there".send(@method, 3,1).should == "l"
    "hello there".send(@method, 3,3).should == "lo "
    "hello there".send(@method, 3,6).should == "lo the"
    "hello there".send(@method, 3,9).should == "lo there"

    "hello there".send(@method, 4,0).should == ""
    "hello there".send(@method, 4,3).should == "o t"
    "hello there".send(@method, 4,6).should == "o ther"
    "hello there".send(@method, 4,9).should == "o there"

    "foo".send(@method, 2,1).should == "o"
    "foo".send(@method, 3,0).should == ""
    "foo".send(@method, 3,1).should == ""

    "".send(@method, 0,0).should == ""
    "".send(@method, 0,1).should == ""

    "x".send(@method, 0,0).should == ""
    "x".send(@method, 0,1).should == "x"
    "x".send(@method, 1,0).should == ""
    "x".send(@method, 1,1).should == ""

    "x".send(@method, -1,0).should == ""
    "x".send(@method, -1,1).should == "x"

    "hello there".send(@method, -3,2).should == "er"
  end

  it "always taints resulting strings when self is tainted" do
    str = "hello world"
    str.taint

    str.send(@method, 0,0).tainted?.should == true
    str.send(@method, 0,1).tainted?.should == true
    str.send(@method, 2,1).tainted?.should == true
  end

  it "returns nil if the offset falls outside of self" do
    "hello there".send(@method, 20,3).should == nil
    "hello there".send(@method, -20,3).should == nil

    "".send(@method, 1,0).should == nil
    "".send(@method, 1,1).should == nil

    "".send(@method, -1,0).should == nil
    "".send(@method, -1,1).should == nil

    "x".send(@method, 2,0).should == nil
    "x".send(@method, 2,1).should == nil

    "x".send(@method, -2,0).should == nil
    "x".send(@method, -2,1).should == nil
  end

  it "returns nil if the length is negative" do
    "hello there".send(@method, 4,-3).should == nil
    "hello there".send(@method, -4,-3).should == nil
  end

  it "calls to_int on the given index and the given length" do
    "hello".send(@method, 0.5, 1).should == "h"
    "hello".send(@method, 0.5, 2.5).should == "he"
    "hello".send(@method, 1, 2.5).should == "el"

    obj = mock('2')
    obj.should_receive(:to_int).exactly(4).times.and_return(2)

    "hello".send(@method, obj, 1).should == "l"
    "hello".send(@method, obj, obj).should == "ll"
    "hello".send(@method, 0, obj).should == "he"
  end

  it "raises a TypeError when idx or length can't be converted to an integer" do
    lambda { "hello".send(@method, mock('x'), 0) }.should raise_error(TypeError)
    lambda { "hello".send(@method, 0, mock('x')) }.should raise_error(TypeError)

    # I'm deliberately including this here.
    # It means that str.send(@method, other, idx) isn't supported.
    lambda { "hello".send(@method, "", 0) }.should raise_error(TypeError)
  end

  it "raises a TypeError when the given index or the given length is nil" do
    lambda { "hello".send(@method, 1, nil)   }.should raise_error(TypeError)
    lambda { "hello".send(@method, nil, 1)   }.should raise_error(TypeError)
    lambda { "hello".send(@method, nil, nil) }.should raise_error(TypeError)
  end

  it "returns subclass instances" do
    s = StringSpecs::MyString.new("hello")
    s.send(@method, 0,0).should be_kind_of(StringSpecs::MyString)
    s.send(@method, 0,4).should be_kind_of(StringSpecs::MyString)
    s.send(@method, 1,4).should be_kind_of(StringSpecs::MyString)
  end
end

describe :string_slice_range, :shared => true do
  it "returns the substring given by the offsets of the range" do
    "hello there".send(@method, 1..1).should == "e"
    "hello there".send(@method, 1..3).should == "ell"
    "hello there".send(@method, 1...3).should == "el"
    "hello there".send(@method, -4..-2).should == "her"
    "hello there".send(@method, -4...-2).should == "he"
    "hello there".send(@method, 5..-1).should == " there"
    "hello there".send(@method, 5...-1).should == " ther"

    "".send(@method, 0..0).should == ""

    "x".send(@method, 0..0).should == "x"
    "x".send(@method, 0..1).should == "x"
    "x".send(@method, 0...1).should == "x"
    "x".send(@method, 0..-1).should == "x"

    "x".send(@method, 1..1).should == ""
    "x".send(@method, 1..-1).should == ""
  end

  it "returns nil if the beginning of the range falls outside of self" do
    "hello there".send(@method, 12..-1).should == nil
    "hello there".send(@method, 20..25).should == nil
    "hello there".send(@method, 20..1).should == nil
    "hello there".send(@method, -20..1).should == nil
    "hello there".send(@method, -20..-1).should == nil

    "".send(@method, -1..-1).should == nil
    "".send(@method, -1...-1).should == nil
    "".send(@method, -1..0).should == nil
    "".send(@method, -1...0).should == nil
  end

  it "returns an empty string if range.begin is inside self and > real end" do
    "hello there".send(@method, 1...1).should == ""
    "hello there".send(@method, 4..2).should == ""
    "hello".send(@method, 4..-4).should == ""
    "hello there".send(@method, -5..-6).should == ""
    "hello there".send(@method, -2..-4).should == ""
    "hello there".send(@method, -5..-6).should == ""
    "hello there".send(@method, -5..2).should == ""

    "".send(@method, 0...0).should == ""
    "".send(@method, 0..-1).should == ""
    "".send(@method, 0...-1).should == ""

    "x".send(@method, 0...0).should == ""
    "x".send(@method, 0...-1).should == ""
    "x".send(@method, 1...1).should == ""
    "x".send(@method, 1...-1).should == ""
  end

  it "always taints resulting strings when self is tainted" do
    str = "hello world"
    str.taint

    str.send(@method, 0..0).tainted?.should == true
    str.send(@method, 0...0).tainted?.should == true
    str.send(@method, 0..1).tainted?.should == true
    str.send(@method, 0...1).tainted?.should == true
    str.send(@method, 2..3).tainted?.should == true
    str.send(@method, 2..0).tainted?.should == true
  end

  it "returns subclass instances" do
    s = StringSpecs::MyString.new("hello")
    s.send(@method, 0...0).should be_kind_of(StringSpecs::MyString)
    s.send(@method, 0..4).should be_kind_of(StringSpecs::MyString)
    s.send(@method, 1..4).should be_kind_of(StringSpecs::MyString)
  end

  it "calls to_int on range arguments" do
    from = mock('from')
    to = mock('to')

    # So we can construct a range out of them...
    from.should_receive(:<=>).twice.and_return(0)

    from.should_receive(:to_int).twice.and_return(1)
    to.should_receive(:to_int).twice.and_return(-2)

    "hello there".send(@method, from..to).should == "ello ther"
    "hello there".send(@method, from...to).should == "ello the"
  end

  it "works with Range subclasses" do
    a = "GOOD"
    range_incl = StringSpecs::MyRange.new(1, 2)
    range_excl = StringSpecs::MyRange.new(-3, -1, true)

    a.send(@method, range_incl).should == "OO"
    a.send(@method, range_excl).should == "OO"
  end
end

describe :string_slice_regexp, :shared => true do
  it "returns the matching portion of self" do
    "hello there".send(@method, /[aeiou](.)\1/).should == "ell"
    "".send(@method, //).should == ""
  end

  it "returns nil if there is no match" do
    "hello there".send(@method, /xyz/).should == nil
  end

  it "always taints resulting strings when self or regexp is tainted" do
    strs = ["hello world"]
    strs += strs.map { |s| s.dup.taint }

    strs.each do |str|
      str.send(@method, //).tainted?.should == str.tainted?
      str.send(@method, /hello/).tainted?.should == str.tainted?

      tainted_re = /./
      tainted_re.taint

      str.send(@method, tainted_re).tainted?.should == true
    end
  end

  it "returns subclass instances" do
    s = StringSpecs::MyString.new("hello")
    s.send(@method, //).should be_kind_of(StringSpecs::MyString)
    s.send(@method, /../).should be_kind_of(StringSpecs::MyString)
  end

  it "sets $~ to MatchData when there is a match and nil when there's none" do
    'hello'.send(@method, /./)
    $~[0].should == 'h'

    'hello'.send(@method, /not/)
    $~.should == nil
  end
end

describe :string_slice_regexp_index, :shared => true do
  it "returns the capture for the given index" do
    "hello there".send(@method, /[aeiou](.)\1/, 0).should == "ell"
    "hello there".send(@method, /[aeiou](.)\1/, 1).should == "l"
    "hello there".send(@method, /[aeiou](.)\1/, -1).should == "l"

    "har".send(@method, /(.)(.)(.)/, 0).should == "har"
    "har".send(@method, /(.)(.)(.)/, 1).should == "h"
    "har".send(@method, /(.)(.)(.)/, 2).should == "a"
    "har".send(@method, /(.)(.)(.)/, 3).should == "r"
    "har".send(@method, /(.)(.)(.)/, -1).should == "r"
    "har".send(@method, /(.)(.)(.)/, -2).should == "a"
    "har".send(@method, /(.)(.)(.)/, -3).should == "h"
  end

  it "always taints resulting strings when self or regexp is tainted" do
    strs = ["hello world"]
    strs += strs.map { |s| s.dup.taint }

    strs.each do |str|
      str.send(@method, //, 0).tainted?.should == str.tainted?
      str.send(@method, /hello/, 0).tainted?.should == str.tainted?

      str.send(@method, /(.)(.)(.)/, 0).tainted?.should == str.tainted?
      str.send(@method, /(.)(.)(.)/, 1).tainted?.should == str.tainted?
      str.send(@method, /(.)(.)(.)/, -1).tainted?.should == str.tainted?
      str.send(@method, /(.)(.)(.)/, -2).tainted?.should == str.tainted?

      tainted_re = /(.)(.)(.)/
      tainted_re.taint

      str.send(@method, tainted_re, 0).tainted?.should == true
      str.send(@method, tainted_re, 1).tainted?.should == true
      str.send(@method, tainted_re, -1).tainted?.should == true
    end
  end

  it "returns nil if there is no match" do
    "hello there".send(@method, /(what?)/, 1).should == nil
  end

  it "returns nil if there is no capture for the given index" do
    "hello there".send(@method, /[aeiou](.)\1/, 2).should == nil
    # You can't refer to 0 using negative indices
    "hello there".send(@method, /[aeiou](.)\1/, -2).should == nil
  end

  it "calls to_int on the given index" do
    obj = mock('2')
    obj.should_receive(:to_int).and_return(2)

    "har".send(@method, /(.)(.)(.)/, 1.5).should == "h"
    "har".send(@method, /(.)(.)(.)/, obj).should == "a"
  end

  it "raises a TypeError when the given index can't be converted to Integer" do
    lambda { "hello".send(@method, /(.)(.)(.)/, mock('x')) }.should raise_error(TypeError)
    lambda { "hello".send(@method, /(.)(.)(.)/, {})        }.should raise_error(TypeError)
    lambda { "hello".send(@method, /(.)(.)(.)/, [])        }.should raise_error(TypeError)
  end

  it "raises a TypeError when the given index is nil" do
    lambda { "hello".send(@method, /(.)(.)(.)/, nil) }.should raise_error(TypeError)
  end

  it "returns subclass instances" do
    s = StringSpecs::MyString.new("hello")
    s.send(@method, /(.)(.)/, 0).should be_kind_of(StringSpecs::MyString)
    s.send(@method, /(.)(.)/, 1).should be_kind_of(StringSpecs::MyString)
  end

  it "sets $~ to MatchData when there is a match and nil when there's none" do
    'hello'.send(@method, /.(.)/, 0)
    $~[0].should == 'he'

    'hello'.send(@method, /.(.)/, 1)
    $~[1].should == 'e'

    'hello'.send(@method, /not/, 0)
    $~.should == nil
  end
end

describe :string_slice_string, :shared => true do
  it "returns other_str if it occurs in self" do
    s = "lo"
    "hello there".send(@method, s).should == s
  end

  it "taints resulting strings when other is tainted" do
    strs = ["", "hello world", "hello"]
    strs += strs.map { |s| s.dup.taint }

    strs.each do |str|
      strs.each do |other|
        r = str.send(@method, other)

        r.tainted?.should == !r.nil? & other.tainted?
      end
    end
  end

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

    'hello'.send(@method, 'll')
    $~.should == nil
  end

  it "returns nil if there is no match" do
    "hello there".send(@method, "bye").should == nil
  end

  it "doesn't call to_str on its argument" do
    o = mock('x')
    o.should_not_receive(:to_str)

    lambda { "hello".send(@method, o) }.should raise_error(TypeError)
  end

  it "returns a subclass instance when given a subclass instance" do
    s = StringSpecs::MyString.new("el")
    r = "hello".send(@method, s)
    r.should == "el"
    r.should be_kind_of(StringSpecs::MyString)
  end
end

language_version __FILE__, "slice"