File: regex_spec.rb

package info (click to toggle)
jruby 9.4.8.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 89,244 kB
  • sloc: ruby: 548,574; java: 276,189; yacc: 25,873; ansic: 6,178; xml: 6,111; sh: 1,855; sed: 94; makefile: 78; jsp: 48; tcl: 40; exp: 12
file content (123 lines) | stat: -rw-r--r-- 3,548 bytes parent folder | download | duplicates (3)
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
require File.dirname(__FILE__) + "/../spec_helper"

describe 'java.util.regex.Pattern' do

  it 'returns match start on match using =~' do
    i = java.util.regex.Pattern::CASE_INSENSITIVE
    p = java.util.regex.Pattern.compile("ab", i)
    expect( p =~ '0123a56b89abbab0ab' ).to eq 10
    expect( p =~ '0123a56b89' ).to be nil
  end

  it 'reports match with === as boolean' do
    p = java.util.regex.Pattern.compile("abb")
    expect( p === '0123a56b89abbab0ab' ).to be true
    expect( p === '0123a56b89' ).to be false
  end

  it 'returns matcher on match' do
    i = java.util.regex.Pattern::CASE_INSENSITIVE
    p = java.util.regex.Pattern.compile("ab", i)
    expect( p.match '0123a56b89abbab0AB' ).to be_a java.util.regex.Matcher
    expect( p.match '0123a56b89' ).to be nil
  end

  it 'reports casefold?' do
    i = java.util.regex.Pattern::CASE_INSENSITIVE
    p = java.util.regex.Pattern.compile("bar", i)
    expect( p.casefold? ).to be true
    p = java.util.regex.Pattern.compile("bar")
    expect( p.casefold? ).to be false
  end

end

describe 'java.util.regex.Matcher' do

  it 'returns regexp pattern' do
    p = java.util.regex.Pattern.compile("[a-f]")
    m = p.match('abcdef')
    expect( m.regexp ).to be p
  end

  it 'reports group begin/end and offset' do
    p = java.util.regex.Pattern.compile("(.)(.)(\\d+)(\\d)")
    m = p.match('THX1138.')
    expect( m.begin(0) ).to eq 1
    expect( m.begin(2) ).to eq 2
    expect( m.end(0) ).to eq 7
    expect( m.end(2) ).to eq 3
    expect( m.offset(0) ).to eq [1, 7]
    expect( m.offset(4) ).to eq [6, 7]

    p = java.util.regex.Pattern.compile("(?<foo>.)(.)(?<bar>.)")
    m = p.match('hoge')

    expect( m.begin(:foo) ).to eq 0
    expect( m.begin(:bar) ).to eq 2
    expect( m.end(:foo) ).to eq 1
    expect( m.end(:bar) ).to eq 3
    expect( m.offset(:bar) ).to eq [2, 3]
  end

  it 'reports size and string' do
    p = java.util.regex.Pattern.compile("(.)(.)(\\d+)(\\d)")
    m = p.match('THX1138.')
    expect( m.length ).to eq 5
    expect( m.size ).to eq 5
  end

  it 'returns matched string' do
    p = java.util.regex.Pattern.compile("(.)(.)(\\d+)(\\d)")
    m = p.match('THX1138.')

    # expect( m.string ).to eq 'HX1138' # different from Ruby
    expect( m.string ).to eq 'THX1138.'
  end

  it 'return captures as an array' do
    p = java.util.regex.Pattern.compile("(.)(.)(\\d+)(\\d)")
    m = p.match('THX1138.')

    expect( m.to_a ).to eq ['HX1138', 'H', 'X', '113', '8']
    expect( m.captures ).to eq ['H', 'X', '113', '8']

    # /([a-c]+)(b|d)?.*(\d+)/.match 'Xaaaeb111Z'
    # #<MatchData "aaaeb111" 1:"aaa" 2:nil 3:"1">
    p = java.util.regex.Pattern.compile("([a-c]+)(b|d)?.*?(\\d+)")
    m = p.match('Xaaaeb111Z')

    expect( m.to_a ).to eq ['aaaeb111', 'aaa', nil, '111']
    expect( m.captures ).to eq ['aaa', nil, '111']
  end

  it 'returns groups using []' do
    p = java.util.regex.Pattern.compile("(.)(.)(\\d+)(\\d)")
    m = p.match('THX1138.')

    expect( m[0] ).to eq 'HX1138'
    expect( m[1, 2] ).to eq ['H', 'X']
    expect( m[1..3] ).to eq ['H', 'X', '113']
  end

  it 'returns named groups using []' do
    p = java.util.regex.Pattern.compile("(?<foo>a+)b")
    m = p.match('ccaaab')

    expect( m[:foo] ).to eq 'aaa'
    expect( m['foo'] ).to eq 'aaa'
  end

  it "end does not blow stack" do
    # See https://jira.codehaus.org/browse/JRUBY-6571

    s = 'hello world'
    r = java.util.regex.Pattern.compile("[eo]")
    m = r.matcher(s)

    while m.find()
      expect{ m.end(0) }.not_to raise_error
    end
  end

end