aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/journey/gtg/builder_test.rb
blob: 5861cd2414630008014c653e5a2035cf2ac8dbe8 (plain) (blame)
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
require "abstract_unit"

module ActionDispatch
  module Journey
    module GTG
      class TestBuilder < ActiveSupport::TestCase
        def test_following_states_multi
          table  = tt ["a|a"]
          assert_equal 1, table.move([0], "a").length
        end

        def test_following_states_multi_regexp
          table  = tt [":a|b"]
          assert_equal 1, table.move([0], "fooo").length
          assert_equal 2, table.move([0], "b").length
        end

        def test_multi_path
          table  = tt ["/:a/d", "/b/c"]

          [
            [1, "/"],
            [2, "b"],
            [2, "/"],
            [1, "c"],
          ].inject([0]) { |state, (exp, sym)|
            new = table.move(state, sym)
            assert_equal exp, new.length
            new
          }
        end

        def test_match_data_ambiguous
          table = tt %w{
            /articles(.:format)
            /articles/new(.:format)
            /articles/:id/edit(.:format)
            /articles/:id(.:format)
          }

          sim     = NFA::Simulator.new table

          match = sim.match "/articles/new"
          assert_equal 2, match.memos.length
        end

        ##
        # Identical Routes may have different restrictions.
        def test_match_same_paths
          table = tt %w{
            /articles/new(.:format)
            /articles/new(.:format)
          }

          sim     = NFA::Simulator.new table

          match = sim.match "/articles/new"
          assert_equal 2, match.memos.length
        end

        private
          def ast strings
            parser = Journey::Parser.new
            asts   = strings.map { |string|
              memo = Object.new
              ast  = parser.parse string
              ast.each { |n| n.memo = memo }
              ast
            }
            Nodes::Or.new asts
          end

          def tt strings
            Builder.new(ast(strings)).transition_table
          end
      end
    end
  end
end