File: repeat_test.rb

package info (click to toggle)
ruby-citrus 3.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 420 kB
  • sloc: ruby: 3,417; makefile: 5
file content (125 lines) | stat: -rw-r--r-- 2,844 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
124
125
require File.expand_path('../helper', __FILE__)

class RepeatTest < Test::Unit::TestCase
  def test_terminal?
    rule = Repeat.new
    assert_equal(false, rule.terminal?)
  end

  def test_exec_zero_or_one
    abc = Rule.for('abc')
    rule = Repeat.new(abc, 0, 1)

    events = rule.exec(Input.new(''))
    assert_equal([rule, CLOSE, 0], events)

    events = rule.exec(Input.new('abc'))
    assert_equal([rule, abc, CLOSE, 3, CLOSE, 3], events)

    events = rule.exec(Input.new('abc' * 3))
    assert_equal([rule, abc, CLOSE, 3, CLOSE, 3], events)
  end

  def test_exec_zero_or_more
    abc = Rule.for('abc')
    rule = Repeat.new(abc, 0, Infinity)

    events = rule.exec(Input.new(''))
    assert_equal([rule, CLOSE, 0], events)

    events = rule.exec(Input.new('abc'))
    assert_equal([rule, abc, CLOSE, 3, CLOSE, 3], events)

    expected_events = [
      rule,
        abc, CLOSE, 3,
        abc, CLOSE, 3,
        abc, CLOSE, 3,
      CLOSE, 9
    ]

    events = rule.exec(Input.new('abc' * 3))
    assert_equal(expected_events, events)
  end

  def test_exec_one_or_more
    abc = Rule.for('abc')
    rule = Repeat.new(abc, 1, Infinity)

    events = rule.exec(Input.new(''))
    assert_equal([], events)

    events = rule.exec(Input.new('abc'))
    assert_equal([rule, abc, CLOSE, 3, CLOSE, 3], events)

    expected_events = [
      rule,
        abc, CLOSE, 3,
        abc, CLOSE, 3,
        abc, CLOSE, 3,
      CLOSE, 9
    ]

    events = rule.exec(Input.new('abc' * 3))
    assert_equal(expected_events, events)
  end

  def test_exec_one
    abc = Rule.for('abc')
    rule = Repeat.new(abc, 1, 1)

    events = rule.exec(Input.new(''))
    assert_equal([], events)

    events = rule.exec(Input.new('abc'))
    assert_equal([rule, abc, CLOSE, 3, CLOSE, 3], events)

    events = rule.exec(Input.new('abc' * 3))
    assert_equal([rule, abc, CLOSE, 3, CLOSE, 3], events)
  end

  def test_operator
    rule = Repeat.new('', 1, 2)
    assert_equal('1*2', rule.operator)
  end

  def test_operator_empty
    rule = Repeat.new('', 0, 0)
    assert_equal('', rule.operator)
  end

  def test_operator_asterisk
    rule = Repeat.new('', 0, Infinity)
    assert_equal('*', rule.operator)
  end

  def test_operator_question_mark
    rule = Repeat.new('', 0, 1)
    assert_equal('?', rule.operator)
  end

  def test_operator_plus
    rule = Repeat.new('', 1, Infinity)
    assert_equal('+', rule.operator)
  end

  def test_to_s
    rule = Repeat.new(/a/, 1, 2)
    assert_equal('/a/1*2', rule.to_s)
  end

  def test_to_s_asterisk
    rule = Repeat.new('a', 0, Infinity)
    assert_equal('"a"*', rule.to_s)
  end

  def test_to_s_question_mark
    rule = Repeat.new('a', 0, 1)
    assert_equal('"a"?', rule.to_s)
  end

  def test_to_s_plus
    rule = Repeat.new('a', 1, Infinity)
    assert_equal('"a"+', rule.to_s)
  end
end