aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/journey/routes_test.rb
blob: b54d961f668b2389d9b0a11a18e52b36e2247be3 (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
require 'abstract_unit'

module ActionDispatch
  module Journey
    class TestRoutes < ActiveSupport::TestCase
      setup do
        @routes = Routes.new
      end

      def test_clear
        routes = Routes.new
        exp    = Router::Strexp.build '/foo(/:id)', {}, ['/.?']
        path   = Path::Pattern.new exp
        requirements = { :hello => /world/ }

        routes.add_route nil, path, requirements, {:id => nil}, {}
        assert_equal 1, routes.length

        routes.clear
        assert_equal 0, routes.length
      end

      def test_ast
        routes = Routes.new
        path   = Path::Pattern.from_string '/hello'

        routes.add_route nil, path, {}, {}, {}
        ast = routes.ast
        routes.add_route nil, path, {}, {}, {}
        assert_not_equal ast, routes.ast
      end

      def test_simulator_changes
        routes = Routes.new
        path   = Path::Pattern.from_string '/hello'

        routes.add_route nil, path, {}, {}, {}
        sim = routes.simulator
        routes.add_route nil, path, {}, {}, {}
        assert_not_equal sim, routes.simulator
      end

      def test_partition_route
        path   = Path::Pattern.from_string '/hello'

        anchored_route = @routes.add_route nil, path, {}, {}, {}
        assert_equal [anchored_route], @routes.anchored_routes
        assert_equal [], @routes.custom_routes

        strexp = Router::Strexp.build(
          "/hello/:who", { who: /\d/ }, ['/', '.', '?']
        )
        path  = Path::Pattern.new strexp

        custom_route = @routes.add_route nil, path, {}, {}, {}
        assert_equal [custom_route], @routes.custom_routes
        assert_equal [anchored_route], @routes.anchored_routes
      end

      def test_first_name_wins
        routes = Routes.new

        one   = Path::Pattern.from_string '/hello'
        two   = Path::Pattern.from_string '/aaron'

        routes.add_route nil, one, {}, {}, 'aaron'
        routes.add_route nil, two, {}, {}, 'aaron'

        assert_equal '/hello', routes.named_routes['aaron'].path.spec.to_s
      end
    end
  end
end