2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
2012-12-19 15:54:47 -05:00
|
|
|
|
|
|
|
module ActionDispatch
|
|
|
|
module Journey
|
2012-12-31 12:36:30 -05:00
|
|
|
class TestRoutes < ActiveSupport::TestCase
|
2015-08-15 17:31:53 -04:00
|
|
|
attr_reader :routes, :mapper
|
2015-08-14 19:25:03 -04:00
|
|
|
|
|
|
|
def setup
|
2016-10-28 23:05:58 -04:00
|
|
|
@route_set = ActionDispatch::Routing::RouteSet.new
|
2015-08-14 21:04:49 -04:00
|
|
|
@routes = @route_set.router.routes
|
|
|
|
@router = @route_set.router
|
2015-08-15 17:31:53 -04:00
|
|
|
@mapper = ActionDispatch::Routing::Mapper.new @route_set
|
2015-08-14 19:25:03 -04:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2012-12-19 15:54:47 -05:00
|
|
|
def test_clear
|
2016-08-06 12:54:50 -04:00
|
|
|
mapper.get "/foo(/:id)", to: "foo#bar", as: "aaron"
|
2018-01-25 18:16:57 -05:00
|
|
|
assert_not_empty routes
|
2012-12-19 15:54:47 -05:00
|
|
|
assert_equal 1, routes.length
|
|
|
|
|
|
|
|
routes.clear
|
2018-01-25 18:16:57 -05:00
|
|
|
assert_empty routes
|
2012-12-19 15:54:47 -05:00
|
|
|
assert_equal 0, routes.length
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ast
|
2016-08-06 12:54:50 -04:00
|
|
|
mapper.get "/foo(/:id)", to: "foo#bar", as: "aaron"
|
2012-12-19 15:54:47 -05:00
|
|
|
ast = routes.ast
|
2016-08-06 12:54:50 -04:00
|
|
|
mapper.get "/foo(/:id)", to: "foo#bar", as: "gorby"
|
2012-12-28 18:49:41 -05:00
|
|
|
assert_not_equal ast, routes.ast
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_simulator_changes
|
2016-08-06 12:54:50 -04:00
|
|
|
mapper.get "/foo(/:id)", to: "foo#bar", as: "aaron"
|
2012-12-19 15:54:47 -05:00
|
|
|
sim = routes.simulator
|
2016-08-06 12:54:50 -04:00
|
|
|
mapper.get "/foo(/:id)", to: "foo#bar", as: "gorby"
|
2012-12-28 18:49:41 -05:00
|
|
|
assert_not_equal sim, routes.simulator
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
2014-06-18 22:16:30 -04:00
|
|
|
def test_partition_route
|
2016-08-06 12:54:50 -04:00
|
|
|
mapper.get "/foo(/:id)", to: "foo#bar", as: "aaron"
|
2014-06-18 22:16:30 -04:00
|
|
|
|
2015-08-15 17:31:53 -04:00
|
|
|
assert_equal 1, @routes.anchored_routes.length
|
2018-01-25 18:16:57 -05:00
|
|
|
assert_empty @routes.custom_routes
|
2014-06-18 22:16:30 -04:00
|
|
|
|
2020-12-15 16:57:10 -05:00
|
|
|
mapper.get "/not_anchored/hello/:who-notanchored", to: "foo#bar", as: "bar", who: /\d/, anchor: false
|
2014-06-18 22:16:30 -04:00
|
|
|
|
2015-08-15 17:31:53 -04:00
|
|
|
assert_equal 1, @routes.custom_routes.length
|
|
|
|
assert_equal 1, @routes.anchored_routes.length
|
2014-06-18 22:16:30 -04:00
|
|
|
end
|
|
|
|
|
2020-12-15 16:57:10 -05:00
|
|
|
def test_custom_anchored_not_partition_route
|
|
|
|
mapper.get "/foo/:bar", to: "foo#bar", as: "aaron"
|
|
|
|
|
|
|
|
assert_equal 1, @routes.anchored_routes.length
|
|
|
|
assert_empty @routes.custom_routes
|
|
|
|
|
|
|
|
mapper.get "/:user/:repo", to: "foo#bar", as: "bar", repo: /[\w.]+/
|
|
|
|
|
|
|
|
assert_equal 2, @routes.anchored_routes.length
|
|
|
|
assert_empty @routes.custom_routes
|
|
|
|
end
|
|
|
|
|
2012-12-19 15:54:47 -05:00
|
|
|
def test_first_name_wins
|
2016-08-06 12:54:50 -04:00
|
|
|
mapper.get "/hello", to: "foo#bar", as: "aaron"
|
2015-08-14 21:04:49 -04:00
|
|
|
assert_raise(ArgumentError) do
|
2016-08-06 12:54:50 -04:00
|
|
|
mapper.get "/aaron", to: "foo#bar", as: "aaron"
|
2015-08-14 21:04:49 -04:00
|
|
|
end
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|