From a5db1488251304ec93256654859b430148f0c506 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 28 Jul 2008 13:41:42 -0500 Subject: Prepare Route#generate and Route#recognize early. Also refactor segments a bit to try to make immutable. --- actionpack/test/controller/routing_test.rb | 151 +++++++++++------------------ actionpack/test/controller/test_test.rb | 4 +- 2 files changed, 57 insertions(+), 98 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/routing_test.rb b/actionpack/test/controller/routing_test.rb index 22e394dc95..6cf134c26f 100644 --- a/actionpack/test/controller/routing_test.rb +++ b/actionpack/test/controller/routing_test.rb @@ -67,66 +67,56 @@ class SegmentTest < Test::Unit::TestCase end def test_interpolation_statement - s = ROUTING::StaticSegment.new - s.value = "Hello" + s = ROUTING::StaticSegment.new("Hello") assert_equal "Hello", eval(s.interpolation_statement([])) assert_equal "HelloHello", eval(s.interpolation_statement([s])) - s2 = ROUTING::StaticSegment.new - s2.value = "-" + s2 = ROUTING::StaticSegment.new("-") assert_equal "Hello-Hello", eval(s.interpolation_statement([s, s2])) - s3 = ROUTING::StaticSegment.new - s3.value = "World" + s3 = ROUTING::StaticSegment.new("World") assert_equal "Hello-World", eval(s3.interpolation_statement([s, s2])) end end class StaticSegmentTest < Test::Unit::TestCase def test_interpolation_chunk_should_respect_raw - s = ROUTING::StaticSegment.new - s.value = 'Hello World' - assert ! s.raw? + s = ROUTING::StaticSegment.new('Hello World') + assert !s.raw? assert_equal 'Hello%20World', s.interpolation_chunk - s.raw = true + s = ROUTING::StaticSegment.new('Hello World', :raw => true) assert s.raw? assert_equal 'Hello World', s.interpolation_chunk end def test_regexp_chunk_should_escape_specials - s = ROUTING::StaticSegment.new - - s.value = 'Hello*World' + s = ROUTING::StaticSegment.new('Hello*World') assert_equal 'Hello\*World', s.regexp_chunk - s.value = 'HelloWorld' + s = ROUTING::StaticSegment.new('HelloWorld') assert_equal 'HelloWorld', s.regexp_chunk end def test_regexp_chunk_should_add_question_mark_for_optionals - s = ROUTING::StaticSegment.new - s.value = "/" - s.is_optional = true + s = ROUTING::StaticSegment.new("/", :optional => true) assert_equal "/?", s.regexp_chunk - s.value = "hello" + s = ROUTING::StaticSegment.new("hello", :optional => true) assert_equal "(?:hello)?", s.regexp_chunk end end class DynamicSegmentTest < Test::Unit::TestCase - def segment + def segment(options = {}) unless @segment - @segment = ROUTING::DynamicSegment.new - @segment.key = :a + @segment = ROUTING::DynamicSegment.new(:a, options) end @segment end def test_extract_value - s = ROUTING::DynamicSegment.new - s.key = :a + s = ROUTING::DynamicSegment.new(:a) hash = {:a => '10', :b => '20'} assert_equal '10', eval(s.extract_value) @@ -149,31 +139,31 @@ class DynamicSegmentTest < Test::Unit::TestCase end def test_regexp_value_check_rejects_nil - segment.regexp = /\d+/ + segment = segment(:regexp => /\d+/) + a_value = nil - assert ! eval(segment.value_check) + assert !eval(segment.value_check) end def test_optional_regexp_value_check_should_accept_nil - segment.regexp = /\d+/ - segment.is_optional = true + segment = segment(:regexp => /\d+/, :optional => true) + a_value = nil assert eval(segment.value_check) end def test_regexp_value_check_rejects_no_match - segment.regexp = /\d+/ + segment = segment(:regexp => /\d+/) a_value = "Hello20World" - assert ! eval(segment.value_check) + assert !eval(segment.value_check) a_value = "20Hi" - assert ! eval(segment.value_check) + assert !eval(segment.value_check) end def test_regexp_value_check_accepts_match - segment.regexp = /\d+/ - + segment = segment(:regexp => /\d+/) a_value = "30" assert eval(segment.value_check) end @@ -184,14 +174,14 @@ class DynamicSegmentTest < Test::Unit::TestCase end def test_optional_value_needs_no_check - segment.is_optional = true + segment = segment(:optional => true) + a_value = nil assert_equal nil, segment.value_check end def test_regexp_value_check_should_accept_match_with_default - segment.regexp = /\d+/ - segment.default = '200' + segment = segment(:regexp => /\d+/, :default => '200') a_value = '100' assert eval(segment.value_check) @@ -234,7 +224,7 @@ class DynamicSegmentTest < Test::Unit::TestCase end def test_extraction_code_should_return_on_mismatch - segment.regexp = /\d+/ + segment = segment(:regexp => /\d+/) hash = merged = {:a => 'Hi', :b => '3'} options = {:b => '3'} a_value = nil @@ -292,7 +282,7 @@ class DynamicSegmentTest < Test::Unit::TestCase end def test_value_regexp_should_match_exacly - segment.regexp = /\d+/ + segment = segment(:regexp => /\d+/) assert_no_match segment.value_regexp, "Hello 10 World" assert_no_match segment.value_regexp, "Hello 10" assert_no_match segment.value_regexp, "10 World" @@ -300,40 +290,36 @@ class DynamicSegmentTest < Test::Unit::TestCase end def test_regexp_chunk_should_return_string - segment.regexp = /\d+/ + segment = segment(:regexp => /\d+/) assert_kind_of String, segment.regexp_chunk end def test_build_pattern_non_optional_with_no_captures # Non optional - a_segment = ROUTING::DynamicSegment.new - a_segment.regexp = /\d+/ #number_of_captures is 0 + a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /\d+/) assert_equal "(\\d+)stuff", a_segment.build_pattern('stuff') end def test_build_pattern_non_optional_with_captures # Non optional - a_segment = ROUTING::DynamicSegment.new - a_segment.regexp = /(\d+)(.*?)/ #number_of_captures is 2 + a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /(\d+)(.*?)/) assert_equal "((\\d+)(.*?))stuff", a_segment.build_pattern('stuff') end def test_optionality_implied - a_segment = ROUTING::DynamicSegment.new - a_segment.key = :id + a_segment = ROUTING::DynamicSegment.new(:id) assert a_segment.optionality_implied? - a_segment.key = :action + a_segment = ROUTING::DynamicSegment.new(:action) assert a_segment.optionality_implied? end def test_modifiers_must_be_handled_sensibly - a_segment = ROUTING::DynamicSegment.new - a_segment.regexp = /david|jamis/i + a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /david|jamis/i) assert_equal "((?i-mx:david|jamis))stuff", a_segment.build_pattern('stuff') - a_segment.regexp = /david|jamis/x + a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /david|jamis/x) assert_equal "((?x-mi:david|jamis))stuff", a_segment.build_pattern('stuff') - a_segment.regexp = /david|jamis/ + a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /david|jamis/) assert_equal "(david|jamis)stuff", a_segment.build_pattern('stuff') end end @@ -560,7 +546,7 @@ class RouteBuilderTest < Test::Unit::TestCase action = segments[-4] assert_equal :action, action.key - action.regexp = /show|in/ # Use 'in' to check partial matches + segments[-4] = ROUTING::DynamicSegment.new(:action, :regexp => /show|in/) builder.assign_default_route_options(segments) @@ -661,10 +647,10 @@ class RoutingTest < Test::Unit::TestCase ActionController::Routing.controller_paths = [] assert_equal [], ActionController::Routing.possible_controllers - ActionController::Routing::Routes.load! ActionController::Routing.controller_paths = [ root, root + '/app/controllers', root + '/vendor/plugins/bad_plugin/lib' ] + ActionController::Routing::Routes.load! assert_equal ["admin/user", "plugin", "user"], ActionController::Routing.possible_controllers.sort ensure @@ -1374,34 +1360,20 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do end def slash_segment(is_optional = false) - returning ROUTING::DividerSegment.new('/') do |s| - s.is_optional = is_optional - end + ROUTING::DividerSegment.new('/', :optional => is_optional) end def default_route unless defined?(@default_route) - @default_route = ROUTING::Route.new - - @default_route.segments << (s = ROUTING::StaticSegment.new) - s.value = '/' - s.raw = true - - @default_route.segments << (s = ROUTING::DynamicSegment.new) - s.key = :controller - - @default_route.segments << slash_segment(:optional) - @default_route.segments << (s = ROUTING::DynamicSegment.new) - s.key = :action - s.default = 'index' - s.is_optional = true - - @default_route.segments << slash_segment(:optional) - @default_route.segments << (s = ROUTING::DynamicSegment.new) - s.key = :id - s.is_optional = true - - @default_route.segments << slash_segment(:optional) + segments = [] + segments << ROUTING::StaticSegment.new('/', :raw => true) + segments << ROUTING::DynamicSegment.new(:controller) + segments << slash_segment(:optional) + segments << ROUTING::DynamicSegment.new(:action, :default => 'index', :optional => true) + segments << slash_segment(:optional) + segments << ROUTING::DynamicSegment.new(:id, :optional => true) + segments << slash_segment(:optional) + @default_route = ROUTING::Route.new(segments).freeze end @default_route end @@ -1489,29 +1461,16 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do end def test_significant_keys - user_url = ROUTING::Route.new - user_url.segments << (s = ROUTING::StaticSegment.new) - s.value = '/' - s.raw = true - - user_url.segments << (s = ROUTING::StaticSegment.new) - s.value = 'user' - - user_url.segments << (s = ROUTING::StaticSegment.new) - s.value = '/' - s.raw = true - s.is_optional = true - - user_url.segments << (s = ROUTING::DynamicSegment.new) - s.key = :user - - user_url.segments << (s = ROUTING::StaticSegment.new) - s.value = '/' - s.raw = true - s.is_optional = true + segments = [] + segments << ROUTING::StaticSegment.new('/', :raw => true) + segments << ROUTING::StaticSegment.new('user') + segments << ROUTING::StaticSegment.new('/', :raw => true, :optional => true) + segments << ROUTING::DynamicSegment.new(:user) + segments << ROUTING::StaticSegment.new('/', :raw => true, :optional => true) - user_url.requirements = {:controller => 'users', :action => 'show'} + requirements = {:controller => 'users', :action => 'show'} + user_url = ROUTING::Route.new(segments, requirements) keys = user_url.significant_keys.sort_by { |k| k.to_s } assert_equal [:action, :controller, :user], keys end diff --git a/actionpack/test/controller/test_test.rb b/actionpack/test/controller/test_test.rb index 61b8c83ee0..58d9ca537f 100644 --- a/actionpack/test/controller/test_test.rb +++ b/actionpack/test/controller/test_test.rb @@ -117,8 +117,8 @@ XML @controller = TestController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new - ActionController::Routing::Routes.reload ActionController::Routing.use_controllers! %w(content admin/user test_test/test) + ActionController::Routing::Routes.load_routes! end def teardown @@ -412,7 +412,7 @@ XML def test_assert_routing_with_method with_routing do |set| - set.draw { |map| map.resources(:content) } + set.draw { |map| map.resources(:content) } assert_routing({ :method => 'post', :path => 'content' }, { :controller => 'content', :action => 'create' }) end end -- cgit v1.2.3