~ubuntu-branches/ubuntu/trusty/ruby1.9/trusty

« back to all changes in this revision

Viewing changes to test/ruby/test_regexp.rb

  • Committer: Bazaar Package Importer
  • Author(s): Stephan Hermann
  • Date: 2008-01-24 11:42:29 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20080124114229-jw2f87rdxlq6gp11
Tags: 1.9.0.0-2ubuntu1
* Merge from debian unstable, remaining changes:
  - Robustify check for target_os, fixing build failure on lpia.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
class TestRegexp < Test::Unit::TestCase
4
4
  def test_ruby_dev_24643
5
 
    assert_nothing_raised("[ruby-dev:24643]") { /(?:(?:[a]*[a])?b)*a*$/ =~ "aabaaca" }
 
5
    assert_nothing_raised("[ruby-dev:24643]") {
 
6
      /(?:(?:[a]*[a])?b)*a*$/ =~ "aabaaca"
 
7
    }
6
8
  end
7
9
 
8
10
  def test_ruby_talk_116455
20
22
 
21
23
  def test_yoshidam_net_20041111_2
22
24
    assert_raise(RegexpError) do
23
 
      s = "[\xFF-\xFF]"
 
25
      s = "[\xFF-\xFF]".force_encoding("utf-8")
24
26
      Regexp.new(s, nil, "u")
25
27
    end
26
28
  end
28
30
  def test_ruby_dev_31309
29
31
    assert_equal('Ruby', 'Ruby'.sub(/[^a-z]/i, '-'))
30
32
  end
 
33
 
 
34
  def test_assert_normal_exit
 
35
    # moved from knownbug.  It caused core.
 
36
    Regexp.union("a", "a")
 
37
  end
 
38
 
 
39
  def test_to_s
 
40
    assert_equal '(?-mix:\x00)', Regexp.new("\0").to_s
 
41
  end
 
42
 
 
43
  def test_union
 
44
    assert_equal :ok, begin
 
45
      Regexp.union(
 
46
        "a",
 
47
        Regexp.new("\xc2\xa1".force_encoding("euc-jp")),
 
48
        Regexp.new("\xc2\xa1".force_encoding("utf-8")))
 
49
      :ng
 
50
    rescue ArgumentError
 
51
      :ok
 
52
    end
 
53
  end
 
54
 
 
55
  def test_named_capture
 
56
    m = /&(?<foo>.*?);/.match("aaa &amp; yyy")
 
57
    assert_equal("amp", m["foo"])
 
58
    assert_equal("amp", m[:foo])
 
59
    assert_equal(5, m.begin(:foo))
 
60
    assert_equal(8, m.end(:foo))
 
61
    assert_equal([5,8], m.offset(:foo))
 
62
 
 
63
    assert_equal("aaa [amp] yyy",
 
64
      "aaa &amp; yyy".sub(/&(?<foo>.*?);/, '[\k<foo>]'))
 
65
 
 
66
    assert_equal('#<MatchData "&amp; y" foo:"amp">',
 
67
      /&(?<foo>.*?); (y)/.match("aaa &amp; yyy").inspect)
 
68
    assert_equal('#<MatchData "&amp; y" 1:"amp" 2:"y">',
 
69
      /&(.*?); (y)/.match("aaa &amp; yyy").inspect)
 
70
    assert_equal('#<MatchData "&amp; y" foo:"amp" bar:"y">',
 
71
      /&(?<foo>.*?); (?<bar>y)/.match("aaa &amp; yyy").inspect)
 
72
    assert_equal('#<MatchData "&amp; y" foo:"amp" foo:"y">',
 
73
      /&(?<foo>.*?); (?<foo>y)/.match("aaa &amp; yyy").inspect)
 
74
 
 
75
    /(?<id>[A-Za-z_]+)/ =~ "!abc"
 
76
    assert_equal("abc", Regexp.last_match(:id))
 
77
 
 
78
    /a/ =~ "b" # doesn't match.
 
79
    assert_equal(nil, Regexp.last_match)
 
80
    assert_equal(nil, Regexp.last_match(1))
 
81
    assert_equal(nil, Regexp.last_match(:foo))
 
82
 
 
83
    assert_equal(["foo", "bar"], /(?<foo>.)(?<bar>.)/.names)
 
84
    assert_equal(["foo"], /(?<foo>.)(?<foo>.)/.names)
 
85
    assert_equal([], /(.)(.)/.names)
 
86
 
 
87
    assert_equal(["foo", "bar"], /(?<foo>.)(?<bar>.)/.match("ab").names)
 
88
    assert_equal(["foo"], /(?<foo>.)(?<foo>.)/.match("ab").names)
 
89
    assert_equal([], /(.)(.)/.match("ab").names)
 
90
 
 
91
    assert_equal({"foo"=>[1], "bar"=>[2]},
 
92
                 /(?<foo>.)(?<bar>.)/.named_captures)
 
93
    assert_equal({"foo"=>[1, 2]},
 
94
                 /(?<foo>.)(?<foo>.)/.named_captures)
 
95
    assert_equal({}, /(.)(.)/.named_captures)
 
96
 
 
97
    assert_equal("a[b]c", "abc".sub(/(?<x>[bc])/, "[\\k<x>]"))
 
98
  end
 
99
 
 
100
  def test_assign_named_capture
 
101
    assert_equal("a", eval('/(?<foo>.)/ =~ "a"; foo'))
 
102
    assert_equal("a", eval('foo = 1; /(?<foo>.)/ =~ "a"; foo'))
 
103
    assert_equal("a", eval('1.times {|foo| /(?<foo>.)/ =~ "a"; break foo }'))
 
104
    assert_nothing_raised { eval('/(?<Foo>.)/ =~ "a"') }
 
105
    assert_nil(eval('/(?<Foo>.)/ =~ "a"; defined? Foo'))
 
106
  end
 
107
 
 
108
  def test_assign_named_capture_to_reserved_word
 
109
    /(?<nil>.)/ =~ "a"
 
110
    assert(!local_variables.include?(:nil), "[ruby-dev:32675]")
 
111
  end
 
112
 
 
113
  def test_match_regexp
 
114
    r = /./
 
115
    m = r.match("a")
 
116
    assert_equal(r, m.regexp)
 
117
  end
31
118
end