File: slice_2.0.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 (78 lines) | stat: -rw-r--r-- 3,050 bytes parent folder | download | duplicates (2)
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

describe :string_slice_regexp_group, :shared => true do

  it "returns the capture for the given name" do
    "hello there".send(@method, /(?<g>[aeiou](.))/, 'g').should == "el"
    "hello there".send(@method, /[aeiou](?<g>.)/, 'g').should == "l"

    "har".send(@method, /(?<g>(.)(.)(.))/, 'g').should == "har"
    "har".send(@method, /(?<h>.)(.)(.)/, 'h').should == "h"
    "har".send(@method, /(.)(?<a>.)(.)/, 'a').should == "a"
    "har".send(@method, /(.)(.)(?<r>.)/, 'r').should == "r"
    "har".send(@method, /(?<h>.)(?<a>.)(?<r>.)/, 'r').should == "r"
  end

  it "returns the last capture for duplicate names" do
    "hello there".send(@method, /(?<g>h)(?<g>.)/, 'g').should == "e"
    "hello there".send(@method, /(?<g>h)(?<g>.)(?<f>.)/, 'g').should == "e"
  end

  it "returns the innermost capture for nested duplicate names" do
    "hello there".send(@method, /(?<g>h(?<g>.))/, 'g').should == "e"
  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, /(?<hi>hello)/, 'hi').tainted?.should == str.tainted?

      str.send(@method, /(?<g>(.)(.)(.))/, 'g').tainted?.should == str.tainted?
      str.send(@method, /(?<h>.)(.)(.)/, 'h').tainted?.should == str.tainted?
      str.send(@method, /(.)(?<a>.)(.)/, 'a').tainted?.should == str.tainted?
      str.send(@method, /(.)(.)(?<r>.)/, 'r').tainted?.should == str.tainted?
      str.send(@method, /(?<h>.)(?<a>.)(?<r>.)/, 'r').tainted?.should == str.tainted?

      tainted_re = /(?<a>.)(?<b>.)(?<c>.)/
      tainted_re.taint

      str.send(@method, tainted_re, 'a').tainted?.should be_true
      str.send(@method, tainted_re, 'b').tainted?.should be_true
      str.send(@method, tainted_re, 'c').tainted?.should be_true
    end
  end

  it "returns nil if there is no match" do
    "hello there".send(@method, /(?<whut>what?)/, 'whut').should be_nil
  end

  it "raises an IndexError if there is no capture for the given name" do
    lambda do
      "hello there".send(@method, /[aeiou](.)\1/, 'non')
    end.should raise_error(IndexError)
  end

  it "raises a TypeError when the given name is not a String" do
    lambda { "hello".send(@method, /(?<q>.)/, mock('x')) }.should raise_error(TypeError)
    lambda { "hello".send(@method, /(?<q>.)/, {})        }.should raise_error(TypeError)
    lambda { "hello".send(@method, /(?<q>.)/, [])        }.should raise_error(TypeError)
  end

  it "raises an IndexError when given the empty String as a group name" do
    lambda { "hello".send(@method, /(?<q>)/, '') }.should raise_error(IndexError)
  end

  it "returns subclass instances" do
    s = StringSpecs::MyString.new("hello")
    s.send(@method, /(?<q>.)/, 'q').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, /(?<hi>.(.))/, 'hi')
    $~[0].should == 'he'

    'hello'.send(@method, /(?<non>not)/, 'non')
    $~.should be_nil
  end
end