File: test_regexp.rb

package info (click to toggle)
jruby 1.5.1-1
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze
  • size: 46,252 kB
  • ctags: 72,039
  • sloc: ruby: 398,155; java: 169,482; yacc: 3,782; xml: 2,469; ansic: 415; sh: 279; makefile: 78; tcl: 40
file content (130 lines) | stat: -rw-r--r-- 3,351 bytes parent folder | download | duplicates (5)
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
require 'test/unit'


class TestRegexp < Test::Unit::TestCase

  def test_EQUAL # '=='
    assert_equal(/.foo.*([a-z])/,/.foo.*([a-z])/)
    a=Regexp.new(".foo.*([a-z])", Regexp::IGNORECASE)
    b=Regexp.new(".foo.*([a-z])")
    assert(!(a == b))
    a=Regexp.new(".foo.*([a-z])", Regexp::IGNORECASE)
    b=Regexp.new(".foo.*([a-z])", Regexp::IGNORECASE, "S")
    assert(!(a == b))
  end

  def test_MATCH # '=~'
    assert_equal(nil,/SIT/ =~ "insensitive")
    assert_equal(5,/SIT/i =~ "insensitive")
  end

  def test_REV # '~'
    $_ = "sit on it"
    assert_equal(nil,~ /SIT/)
    assert_equal(0,~ /SIT/i)
  end

  def test_VERY_EQUAL # '==='
    gotit=false
    case "insensitive"
      when /SIT/
        fail("Shouldn't have matched")
      when /SIT/i
        gotit = true
    end
    assert(gotit)
  end

  def test_casefold?
    a=Regexp.new(".foo.*([a-z])", Regexp::IGNORECASE)
    b=Regexp.new(".foo.*([a-z])")
    assert_equal(true, a.casefold?)
    assert_equal(false, b.casefold?)
  end

  def test_clone
    for taint in [ false, true ]
      for frozen in [ false, true ]
        a = /SIT/.dup
        a.taint  if taint
        a.freeze if frozen
        b = a.clone

        assert_equal(a, b)
        assert_equal(a.frozen?, b.frozen?)
        assert_equal(a.tainted?, b.tainted?)
      end
    end
  end

  def test_kcode
    a = Regexp.new(".foo.*([a-z])", Regexp::IGNORECASE)
    b = Regexp.new(".foo.*([a-z])", Regexp::IGNORECASE, "S")
    c = Regexp.new(".foo.*([a-z])", Regexp::IGNORECASE, "n")
    assert_equal(nil, a.kcode) if $KCODE == "NONE"
    assert_equal("sjis", b.kcode)
    assert_equal("none",    c.kcode)
  end

  def test_s_last_match
    a = /SIT/
    "STAND OUT" =~ a
    "SIT IT OUT" =~ a
    m = Regexp.last_match
    assert_instance_of(MatchData, m)
    assert_equal([0,3], m.offset(0))
    assert_equal(1, m.length)
    assert_equal(" IT OUT", m.post_match)
  end

  def test_match
    a = Regexp.new("sit")
    m = a.match("howsit going?")
    assert_instance_of(MatchData,m)
    assert_equal([3,6],m.offset(0))
    assert_equal(1, m.length)
  end

  def test_source
    a=Regexp.new(".foo.*([a-z])", Regexp::IGNORECASE)
    assert_equal(".foo.*([a-z])", a.source)
  end

  def test_s_compile
    a=Regexp.new(".foo.*([a-z])", Regexp::IGNORECASE)
    b=Regexp.new("sit", true)
    c=Regexp.new("sit")
    assert(("POSIT" =~ b) != nil)
    assert(("POSIT" =~ c) == nil)
  end

  def test_s_escape
    assert_equal('\\\\\[\]\*\?\{\}\.', Regexp.escape('\\[]*?{}.'))
  end

  def test_s_new
    a=Regexp.new(".foo.*([a-z])", Regexp::IGNORECASE)
    b=Regexp.new("sit", true)
    c=Regexp.new("sit")
    assert(("POSIT" =~ b) != nil)
    assert(("POSIT" =~ c) == nil)
  end

  def test_s_quote
    assert_equal('\\\\\[\]\*\?\{\}\.', Regexp.quote('\\[]*?{}.'))
    assert_raises(TypeError) { Regexp.quote(nil) }
  end

  def test_unicode_match
    "yés!héy!héllo" =~ /!([^!]*)/u
    assert_equal("yés", $~.pre_match)
    assert_equal("!héllo", $~.post_match)
    assert_equal("héy", $~[1])
    assert_equal("!héy", $~[0])
    assert_equal("!héy", $~.to_s)
    assert_equal(["héy"], $~.captures)
    assert_equal(["!héy", "héy"], $~.to_a)
    # couldn't get this to pass right...JRuby and MRI seem to handle it differently, and not like below
    #assert_equal("y̩s!h̩y!h̩llo", $~.string)
  end
end