File: input_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 (117 lines) | stat: -rw-r--r-- 2,423 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
require File.expand_path('../helper', __FILE__)

class InputTest < Test::Unit::TestCase
  def test_new
    # to_str
    assert_equal('abc', Input.new('abc').string)

    # read
    selftext = ::File.read(__FILE__)
    ::File.open(__FILE__, 'r') do |io|
      assert_equal(selftext, Input.new(io).string)
    end

    # to_path
    path = Struct.new(:to_path).new(__FILE__)
    assert_equal(selftext, Input.new(path).string)
  end

  def test_memoized?
    assert_equal(false, Input.new('').memoized?)
  end

  def test_offsets_new
    input = Input.new("abc\ndef\nghi")
    assert_equal(0, input.line_offset)
    assert_equal(0, input.line_index)
    assert_equal(1, input.line_number)
    assert_equal("abc\n", input.line)
  end

  def test_offsets_advanced
    input = Input.new("abc\ndef\nghi")
    input.pos = 6
    assert_equal(2, input.line_offset)
    assert_equal(1, input.line_index)
    assert_equal(2, input.line_number)
    assert_equal("def\n", input.line)
  end

  def test_exec
    a = Rule.for('a')
    b = Rule.for('b')
    c = Rule.for('c')
    s = Rule.for([ a, b, c ])
    r = Repeat.new(s, 0, Infinity)

    input = Input.new("abcabcabc")
    events = input.exec(r)

    expected_events = [
      r,
        s,
          a, CLOSE, 1,
          b, CLOSE, 1,
          c, CLOSE, 1,
        CLOSE, 3,
        s,
          a, CLOSE, 1,
          b, CLOSE, 1,
          c, CLOSE, 1,
        CLOSE, 3,
        s,
          a, CLOSE, 1,
          b, CLOSE, 1,
          c, CLOSE, 1,
        CLOSE, 3,
      CLOSE, 9
    ]

    assert_equal(expected_events, events)
  end

  def test_exec2
    a = Rule.for('a')
    b = Rule.for('b')
    c = Choice.new([ a, b ])
    r = Repeat.new(c, 0, Infinity)
    s = Rule.for([ a, r ])

    input = Input.new('abbababba')
    events = input.exec(s)

    expected_events = [
      s,
        a, CLOSE, 1,
        r,
          c,
            b, CLOSE, 1,
          CLOSE, 1,
          c,
            b, CLOSE, 1,
          CLOSE, 1,
          c,
            a, CLOSE, 1,
          CLOSE, 1,
          c,
            b, CLOSE, 1,
          CLOSE, 1,
          c,
            a, CLOSE, 1,
          CLOSE, 1,
          c,
            b, CLOSE, 1,
          CLOSE, 1,
          c,
            b, CLOSE, 1,
          CLOSE, 1,
          c,
            a, CLOSE, 1,
          CLOSE, 1,
        CLOSE, 8,
      CLOSE, 9
    ]

    assert_equal(expected_events, events)
  end
end