File: test_simulator.rb

package info (click to toggle)
ruby-journey 1.0.4-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 288 kB
  • sloc: ruby: 2,830; javascript: 113; yacc: 42; makefile: 2
file content (96 lines) | stat: -rw-r--r-- 2,439 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
require 'helper'

module Journey
  module NFA
    class TestSimulator < MiniTest::Unit::TestCase
      def test_simulate_simple
        sim = simulator_for ['/foo']
        assert_match sim, '/foo'
      end

      def test_simulate_simple_no_match
        sim = simulator_for ['/foo']
        refute_match sim, 'foo'
      end

      def test_simulate_simple_no_match_too_long
        sim = simulator_for ['/foo']
        refute_match sim, '/foo/bar'
      end

      def test_simulate_simple_no_match_wrong_string
        sim = simulator_for ['/foo']
        refute_match sim, '/bar'
      end

      def test_simulate_regex
        sim = simulator_for ['/:foo/bar']
        assert_match sim, '/bar/bar'
        assert_match sim, '/foo/bar'
      end

      def test_simulate_or
        sim = simulator_for ['/foo', '/bar']
        assert_match sim, '/bar'
        assert_match sim, '/foo'
        refute_match sim, '/baz'
      end

      def test_simulate_optional
        sim = simulator_for ['/foo(/bar)']
        assert_match sim, '/foo'
        assert_match sim, '/foo/bar'
        refute_match sim, '/foo/'
      end

      def test_matchdata_has_memos
        paths   = %w{ /foo /bar }
        parser  = Journey::Parser.new
        asts    = paths.map { |x|
          ast = parser.parse x
          ast.each { |n| n.memo = ast}
          ast
        }

        expected = asts.first

        builder = Builder.new Nodes::Or.new asts

        sim = Simulator.new builder.transition_table

        md = sim.match '/foo'
        assert_equal [expected], md.memos
      end

      def test_matchdata_memos_on_merge
        parser = Journey::Parser.new
        routes = [
          '/articles(.:format)',
          '/articles/new(.:format)',
          '/articles/:id/edit(.:format)',
          '/articles/:id(.:format)',
        ].map { |path|
          ast = parser.parse path
          ast.each { |n| n.memo = ast }
          ast
        }

        asts = routes.dup

        ast = Nodes::Or.new routes

        nfa   = Journey::NFA::Builder.new ast
        sim = Simulator.new nfa.transition_table
        md = sim.match '/articles'
        assert_equal [asts.first], md.memos
      end

      def simulator_for paths
        parser  = Journey::Parser.new
        asts    = paths.map { |x| parser.parse x }
        builder = Builder.new Nodes::Or.new asts
        Simulator.new builder.transition_table
      end
    end
  end
end