Path::Pattern is instantiated internally, so make the contructor require a strexp object

This commit is contained in:
Aaron Patterson 2014-05-29 15:44:54 -07:00
parent 5682596db7
commit bb207ea7b6
5 changed files with 49 additions and 46 deletions

View File

@ -1,18 +1,17 @@
require 'action_dispatch/journey/router/strexp'
module ActionDispatch
module Journey # :nodoc:
module Path # :nodoc:
class Pattern # :nodoc:
attr_reader :spec, :requirements, :anchored
def initialize(strexp)
@anchored = true
def self.from_string string
new Journey::Router::Strexp.build(string, {}, ["/.?"], true)
end
def initialize(strexp)
case strexp
when String
parser = Journey::Parser.new
@spec = parser.parse(strexp)
@requirements = {}
@separators = "/.?"
when Router::Strexp
@spec = strexp.ast
@requirements = strexp.requirements

View File

@ -101,7 +101,7 @@ module ActionDispatch
['/:foo(/:bar)', %w{ bar }],
['/:foo(/:bar)/:lol(/:baz)', %w{ bar baz }],
].each do |pattern, list|
path = Pattern.new pattern
path = Pattern.from_string pattern
assert_equal list.sort, path.optional_names.sort
end
end
@ -205,20 +205,20 @@ module ActionDispatch
end
def test_to_regexp_defaults
path = Pattern.new '/:controller(/:action(/:id))'
path = Pattern.from_string '/:controller(/:action(/:id))'
expected = %r{\A/([^/.?]+)(?:/([^/.?]+)(?:/([^/.?]+))?)?\Z}
assert_equal expected, path.to_regexp
end
def test_failed_match
path = Pattern.new '/:controller(/:action(/:id(.:format)))'
path = Pattern.from_string '/:controller(/:action(/:id(.:format)))'
uri = 'content'
assert_not path =~ uri
end
def test_match_controller
path = Pattern.new '/:controller(/:action(/:id(.:format)))'
path = Pattern.from_string '/:controller(/:action(/:id(.:format)))'
uri = '/content'
match = path =~ uri
@ -230,7 +230,7 @@ module ActionDispatch
end
def test_match_controller_action
path = Pattern.new '/:controller(/:action(/:id(.:format)))'
path = Pattern.from_string '/:controller(/:action(/:id(.:format)))'
uri = '/content/list'
match = path =~ uri
@ -242,7 +242,7 @@ module ActionDispatch
end
def test_match_controller_action_id
path = Pattern.new '/:controller(/:action(/:id(.:format)))'
path = Pattern.from_string '/:controller(/:action(/:id(.:format)))'
uri = '/content/list/10'
match = path =~ uri
@ -254,7 +254,7 @@ module ActionDispatch
end
def test_match_literal
path = Path::Pattern.new "/books(/:action(.:format))"
path = Path::Pattern.from_string "/books(/:action(.:format))"
uri = '/books'
match = path =~ uri
@ -264,7 +264,7 @@ module ActionDispatch
end
def test_match_literal_with_action
path = Path::Pattern.new "/books(/:action(.:format))"
path = Path::Pattern.from_string "/books(/:action(.:format))"
uri = '/books/list'
match = path =~ uri
@ -274,7 +274,7 @@ module ActionDispatch
end
def test_match_literal_with_action_and_format
path = Path::Pattern.new "/books(/:action(.:format))"
path = Path::Pattern.from_string "/books(/:action(.:format))"
uri = '/books/list.rss'
match = path =~ uri

View File

@ -5,7 +5,7 @@ module ActionDispatch
class TestRoute < ActiveSupport::TestCase
def test_initialize
app = Object.new
path = Path::Pattern.new '/:controller(/:action(/:id(.:format)))'
path = Path::Pattern.from_string '/:controller(/:action(/:id(.:format)))'
defaults = {}
route = Route.new("name", app, path, {}, defaults)
@ -16,7 +16,7 @@ module ActionDispatch
def test_route_adds_itself_as_memo
app = Object.new
path = Path::Pattern.new '/:controller(/:action(/:id(.:format)))'
path = Path::Pattern.from_string '/:controller(/:action(/:id(.:format)))'
defaults = {}
route = Route.new("name", app, path, {}, defaults)
@ -26,21 +26,21 @@ module ActionDispatch
end
def test_ip_address
path = Path::Pattern.new '/messages/:id(.:format)'
path = Path::Pattern.from_string '/messages/:id(.:format)'
route = Route.new("name", nil, path, {:ip => '192.168.1.1'},
{ :controller => 'foo', :action => 'bar' })
assert_equal '192.168.1.1', route.ip
end
def test_default_ip
path = Path::Pattern.new '/messages/:id(.:format)'
path = Path::Pattern.from_string '/messages/:id(.:format)'
route = Route.new("name", nil, path, {},
{ :controller => 'foo', :action => 'bar' })
assert_equal(//, route.ip)
end
def test_format_with_star
path = Path::Pattern.new '/:controller/*extra'
path = Path::Pattern.from_string '/:controller/*extra'
route = Route.new("name", nil, path, {},
{ :controller => 'foo', :action => 'bar' })
assert_equal '/foo/himom', route.format({
@ -50,7 +50,7 @@ module ActionDispatch
end
def test_connects_all_match
path = Path::Pattern.new '/:controller(/:action(/:id(.:format)))'
path = Path::Pattern.from_string '/:controller(/:action(/:id(.:format)))'
route = Route.new("name", nil, path, {:action => 'bar'}, { :controller => 'foo' })
assert_equal '/foo/bar/10', route.format({
@ -61,21 +61,21 @@ module ActionDispatch
end
def test_extras_are_not_included_if_optional
path = Path::Pattern.new '/page/:id(/:action)'
path = Path::Pattern.from_string '/page/:id(/:action)'
route = Route.new("name", nil, path, { }, { :action => 'show' })
assert_equal '/page/10', route.format({ :id => 10 })
end
def test_extras_are_not_included_if_optional_with_parameter
path = Path::Pattern.new '(/sections/:section)/pages/:id'
path = Path::Pattern.from_string '(/sections/:section)/pages/:id'
route = Route.new("name", nil, path, { }, { :action => 'show' })
assert_equal '/pages/10', route.format({:id => 10})
end
def test_extras_are_not_included_if_optional_parameter_is_nil
path = Path::Pattern.new '(/sections/:section)/pages/:id'
path = Path::Pattern.from_string '(/sections/:section)/pages/:id'
route = Route.new("name", nil, path, { }, { :action => 'show' })
assert_equal '/pages/10', route.format({:id => 10, :section => nil})
@ -85,10 +85,10 @@ module ActionDispatch
constraints = {:required_defaults => [:controller, :action]}
defaults = {:controller=>"pages", :action=>"show"}
path = Path::Pattern.new "/page/:id(/:action)(.:format)"
path = Path::Pattern.from_string "/page/:id(/:action)(.:format)"
specific = Route.new "name", nil, path, constraints, defaults
path = Path::Pattern.new "/:controller(/:action(/:id))(.:format)"
path = Path::Pattern.from_string "/:controller(/:action(/:id))(.:format)"
generic = Route.new "name", nil, path, constraints
knowledge = {:id=>20, :controller=>"pages", :action=>"show"}

View File

@ -225,7 +225,7 @@ module ActionDispatch
end
def test_defaults_merge_correctly
path = Path::Pattern.new '/foo(/:id)'
path = Path::Pattern.from_string '/foo(/:id)'
@router.routes.add_route nil, path, {}, {:id => nil}, {}
env = rails_env 'PATH_INFO' => '/foo/10'
@ -308,7 +308,7 @@ module ActionDispatch
end
def test_nil_path_parts_are_ignored
path = Path::Pattern.new "/:controller(/:action(.:format))"
path = Path::Pattern.from_string "/:controller(/:action(.:format))"
@router.routes.add_route @app, path, {}, {}, {}
params = { :controller => "tasks", :format => nil }
@ -331,7 +331,7 @@ module ActionDispatch
end
def test_generate_calls_param_proc
path = Path::Pattern.new '/:controller(/:action)'
path = Path::Pattern.from_string '/:controller(/:action)'
@router.routes.add_route @app, path, {}, {}, {}
parameterized = []
@ -348,7 +348,7 @@ module ActionDispatch
end
def test_generate_id
path = Path::Pattern.new '/:controller(/:action)'
path = Path::Pattern.from_string '/:controller(/:action)'
@router.routes.add_route @app, path, {}, {}, {}
path, params = @formatter.generate(
@ -358,7 +358,7 @@ module ActionDispatch
end
def test_generate_escapes
path = Path::Pattern.new '/:controller(/:action)'
path = Path::Pattern.from_string '/:controller(/:action)'
@router.routes.add_route @app, path, {}, {}, {}
path, _ = @formatter.generate(nil,
@ -369,7 +369,7 @@ module ActionDispatch
end
def test_generate_escapes_with_namespaced_controller
path = Path::Pattern.new '/:controller(/:action)'
path = Path::Pattern.from_string '/:controller(/:action)'
@router.routes.add_route @app, path, {}, {}, {}
path, _ = @formatter.generate(
@ -380,7 +380,7 @@ module ActionDispatch
end
def test_generate_extra_params
path = Path::Pattern.new '/:controller(/:action)'
path = Path::Pattern.from_string '/:controller(/:action)'
@router.routes.add_route @app, path, {}, {}, {}
path, params = @formatter.generate(
@ -394,7 +394,7 @@ module ActionDispatch
end
def test_generate_uses_recall_if_needed
path = Path::Pattern.new '/:controller(/:action(/:id))'
path = Path::Pattern.from_string '/:controller(/:action(/:id))'
@router.routes.add_route @app, path, {}, {}, {}
path, params = @formatter.generate(
@ -406,7 +406,7 @@ module ActionDispatch
end
def test_generate_with_name
path = Path::Pattern.new '/:controller(/:action)'
path = Path::Pattern.from_string '/:controller(/:action)'
@router.routes.add_route @app, path, {}, {}, {}
path, params = @formatter.generate(
@ -423,7 +423,7 @@ module ActionDispatch
'/content/show/10' => { :controller => 'content', :action => 'show', :id => "10" },
}.each do |request_path, expected|
define_method("test_recognize_#{expected.keys.map(&:to_s).join('_')}") do
path = Path::Pattern.new "/:controller(/:action(/:id))"
path = Path::Pattern.from_string "/:controller(/:action(/:id))"
app = Object.new
route = @router.routes.add_route(app, path, {}, {}, {})
@ -445,7 +445,7 @@ module ActionDispatch
:splat => ['/segment/a/b%20c+d', { :segment => 'segment', :splat => 'a/b c+d' }]
}.each do |name, (request_path, expected)|
define_method("test_recognize_#{name}") do
path = Path::Pattern.new '/:segment/*splat'
path = Path::Pattern.from_string '/:segment/*splat'
app = Object.new
route = @router.routes.add_route(app, path, {}, {}, {})
@ -489,7 +489,7 @@ module ActionDispatch
end
def test_recognize_literal
path = Path::Pattern.new "/books(/:action(.:format))"
path = Path::Pattern.from_string "/books(/:action(.:format))"
app = Object.new
route = @router.routes.add_route(app, path, {}, {:controller => 'books'})
@ -506,7 +506,7 @@ module ActionDispatch
end
def test_recognize_head_request_as_get_route
path = Path::Pattern.new "/books(/:action(.:format))"
path = Path::Pattern.from_string "/books(/:action(.:format))"
app = Object.new
conditions = {
:request_method => 'GET'
@ -525,7 +525,7 @@ module ActionDispatch
end
def test_recognize_cares_about_verbs
path = Path::Pattern.new "/books(/:action(.:format))"
path = Path::Pattern.from_string "/books(/:action(.:format))"
app = Object.new
conditions = {
:request_method => 'GET'
@ -553,7 +553,11 @@ module ActionDispatch
def add_routes router, paths
paths.each do |path|
path = Path::Pattern.new path
if String === path
path = Path::Pattern.from_string path
else
path = Path::Pattern.new path
end
router.routes.add_route @app, path, {}, {}, {}
end
end

View File

@ -18,7 +18,7 @@ module ActionDispatch
def test_ast
routes = Routes.new
path = Path::Pattern.new '/hello'
path = Path::Pattern.from_string '/hello'
routes.add_route nil, path, {}, {}, {}
ast = routes.ast
@ -28,7 +28,7 @@ module ActionDispatch
def test_simulator_changes
routes = Routes.new
path = Path::Pattern.new '/hello'
path = Path::Pattern.from_string '/hello'
routes.add_route nil, path, {}, {}, {}
sim = routes.simulator
@ -40,8 +40,8 @@ module ActionDispatch
#def add_route app, path, conditions, defaults, name = nil
routes = Routes.new
one = Path::Pattern.new '/hello'
two = Path::Pattern.new '/aaron'
one = Path::Pattern.from_string '/hello'
two = Path::Pattern.from_string '/aaron'
routes.add_route nil, one, {}, {}, 'aaron'
routes.add_route nil, two, {}, {}, 'aaron'