routing_test.rb 24.9 KB
Newer Older
1 2
require File.dirname(__FILE__) + '/../abstract_unit'
require 'test/unit'
3
require 'stringio'
4

5
RunTimeTests = ARGV.include? 'time'
6

7 8 9 10 11 12
module ActionController::CodeGeneration

class SourceTests < Test::Unit::TestCase
  attr_accessor :source
  def setup
    @source = Source.new
13
  end
14 15 16 17
  
  def test_initial_state
    assert_equal [], source.lines
    assert_equal 0, source.indentation_level
18
  end
19 20 21 22 23 24 25 26 27
  
  def test_trivial_operations
    source << "puts 'Hello World'"
    assert_equal ["puts 'Hello World'"], source.lines
    assert_equal "puts 'Hello World'", source.to_s
    
    source.line "puts 'Goodbye World'"
    assert_equal ["puts 'Hello World'", "puts 'Goodbye World'"], source.lines
    assert_equal "puts 'Hello World'\nputs 'Goodbye World'", source.to_s
28 29
  end

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  def test_indentation
    source << "x = gets.to_i"
    source << 'if x.odd?'
    source.indent { source << "puts 'x is odd!'" }
    source << 'else'
    source.indent { source << "puts 'x is even!'" }
    source << 'end'
    
    assert_equal ["x = gets.to_i", "if x.odd?", "  puts 'x is odd!'", 'else', "  puts 'x is even!'", 'end'], source.lines
    
    text = "x = gets.to_i
if x.odd?
  puts 'x is odd!'
else
  puts 'x is even!'
end"
46

47 48 49
    assert_equal text, source.to_s
  end 
end
50

51 52
class CodeGeneratorTests < Test::Unit::TestCase
  attr_accessor :generator
53
  def setup
54
    @generator = CodeGenerator.new
55
  end
56 57 58 59

  def test_initial_state
    assert_equal [], generator.source.lines
    assert_equal [], generator.locals
60
  end
61 62 63 64 65
    
  def test_trivial_operations
    ["puts 'Hello World'", "puts 'Goodbye World'"].each {|l| generator << l} 
    assert_equal ["puts 'Hello World'", "puts 'Goodbye World'"], generator.source.lines
    assert_equal "puts 'Hello World'\nputs 'Goodbye World'", generator.to_s
66 67
  end
  
68 69 70 71 72
  def test_if
    generator << "x = gets.to_i"
    generator.if("x.odd?") { generator << "puts 'x is odd!'" }
    
    assert_equal "x = gets.to_i\nif x.odd?\n  puts 'x is odd!'\nend", generator.to_s
73 74
  end
  
75 76 77 78 79
  def test_else
    test_if
    generator.else { generator << "puts 'x is even!'" }
    
    assert_equal "x = gets.to_i\nif x.odd?\n  puts 'x is odd!'\nelse \n  puts 'x is even!'\nend", generator.to_s
80
  end
81 82 83 84 85 86 87 88 89 90 91 92 93

  def test_dup
    generator << 'x = 2'
    generator.locals << :x
    
    g = generator.dup
    assert_equal generator.source, g.source
    assert_equal generator.locals, g.locals
    
    g << 'y = 3'
    g.locals << :y
    assert_equal [:x, :y], g.locals # Make sure they don't share the same array.
    assert_equal [:x], generator.locals
94
  end
95 96 97 98 99 100
end 

# XXX Extract to test/controller/fake_controllers.rb 
module Object::Controllers
  def self.const_available?(*args)
    const_defined?(*args)
101 102
  end
  
103
  class ContentController
104
  end
105 106 107 108 109 110 111
  module Admin
    def self.const_available?(*args)
      const_defined?(*args)
    end
    
    class UserController
    end
112
  end
113
end
114

115 116 117 118 119 120

class RecognitionTests < Test::Unit::TestCase
  attr_accessor :generator
  alias :g :generator
  def setup
    @generator = RecognitionGenerator.new
121
  end
122 123 124 125 126

  def go(components)
    g.current = components.first
    g.after = components[1..-1] || []
    g.go
127 128
  end
  
129 130 131 132 133 134
  def execute(path, show = false)
    path = path.split('/') if path.is_a? String
    source = "index, path = 0, #{path.inspect}\n#{g.to_s}"
    puts source if show
    r = eval source
    r ? r.symbolize_keys : nil
135 136
  end
  
137 138 139 140
  Static = ::ActionController::Routing::StaticComponent
  Dynamic = ::ActionController::Routing::DynamicComponent
  Path = ::ActionController::Routing::PathComponent
  Controller = ::ActionController::Routing::ControllerComponent
141
  
142 143 144 145 146 147 148 149 150 151 152 153 154
  def test_all_static
    c = %w(hello world how are you).collect {|str| Static.new(str)}
    
    g.result :controller, "::Controllers::ContentController", true
    g.constant_result :action, 'index' 
    
    go c
    
    assert_nil execute('x')
    assert_nil execute('hello/world/how')
    assert_nil execute('hello/world/how/are')
    assert_nil execute('hello/world/how/are/you/today')
    assert_equal({:controller => ::Controllers::ContentController, :action => 'index'}, execute('hello/world/how/are/you'))
155 156
  end

157 158 159 160
  def test_basic_dynamic
    c = [Static.new("hi"), Dynamic.new(:action)]
    g.result :controller, "::Controllers::ContentController", true
    go c
D
David Heinemeier Hansson 已提交
161
    
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    assert_nil execute('boo')
    assert_nil execute('boo/blah')
    assert_nil execute('hi')
    assert_nil execute('hi/dude/what')
    assert_equal({:controller => ::Controllers::ContentController, :action => 'dude'}, execute('hi/dude'))
  end 

  def test_dynamic_with_default
    c = [Static.new("hi"), Dynamic.new(:action, :default => 'index')]
    g.result :controller, "::Controllers::ContentController", true
    go c
    
    assert_nil execute('boo')
    assert_nil execute('boo/blah')
    assert_nil execute('hi/dude/what')
    assert_equal({:controller => ::Controllers::ContentController, :action => 'index'}, execute('hi'))
    assert_equal({:controller => ::Controllers::ContentController, :action => 'index'}, execute('hi/index'))
    assert_equal({:controller => ::Controllers::ContentController, :action => 'dude'}, execute('hi/dude'))
  end 

  def test_dynamic_with_string_condition
    c = [Static.new("hi"), Dynamic.new(:action, :condition => 'index')]
    g.result :controller, "::Controllers::ContentController", true
    go c
    
    assert_nil execute('boo')
    assert_nil execute('boo/blah')
    assert_nil execute('hi')
    assert_nil execute('hi/dude/what')
    assert_equal({:controller => ::Controllers::ContentController, :action => 'index'}, execute('hi/index'))
    assert_nil execute('hi/dude')
D
David Heinemeier Hansson 已提交
193 194
  end
  
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
  def test_dynamic_with_regexp_condition
    c = [Static.new("hi"), Dynamic.new(:action, :condition => /^[a-z]+$/)]
    g.result :controller, "::Controllers::ContentController", true
    go c
    
    assert_nil execute('boo')
    assert_nil execute('boo/blah')
    assert_nil execute('hi')
    assert_nil execute('hi/FOXY')
    assert_nil execute('hi/138708jkhdf')
    assert_nil execute('hi/dkjfl8792343dfsf')
    assert_nil execute('hi/dude/what')
    assert_equal({:controller => ::Controllers::ContentController, :action => 'index'}, execute('hi/index'))
    assert_equal({:controller => ::Controllers::ContentController, :action => 'dude'}, execute('hi/dude'))
  end 
D
David Heinemeier Hansson 已提交
210
  
211 212 213 214 215 216 217 218 219 220 221 222 223 224
  def test_dynamic_with_regexp_and_default
    c = [Static.new("hi"), Dynamic.new(:action, :condition => /^[a-z]+$/, :default => 'index')]
    g.result :controller, "::Controllers::ContentController", true
    go c
    
    assert_nil execute('boo')
    assert_nil execute('boo/blah')
    assert_nil execute('hi/FOXY')
    assert_nil execute('hi/138708jkhdf')
    assert_nil execute('hi/dkjfl8792343dfsf')
    assert_equal({:controller => ::Controllers::ContentController, :action => 'index'}, execute('hi'))
    assert_equal({:controller => ::Controllers::ContentController, :action => 'index'}, execute('hi/index'))
    assert_equal({:controller => ::Controllers::ContentController, :action => 'dude'}, execute('hi/dude'))
    assert_nil execute('hi/dude/what')
D
David Heinemeier Hansson 已提交
225 226
  end

227 228 229 230 231 232 233 234 235 236 237 238 239 240
  def test_path
    c = [Static.new("hi"), Path.new(:file)]
    g.result :controller, "::Controllers::ContentController", true
    g.constant_result :action, "download"
    
    go c
    
    assert_nil execute('boo')
    assert_nil execute('boo/blah')
    assert_equal({:controller => ::Controllers::ContentController, :action => 'download', :file => []}, execute('hi'))
    assert_equal({:controller => ::Controllers::ContentController, :action => 'download', :file => %w(books agile_rails_dev.pdf)},
                 execute('hi/books/agile_rails_dev.pdf'))
    assert_equal({:controller => ::Controllers::ContentController, :action => 'download', :file => ['dude']}, execute('hi/dude'))
    assert_equal 'dude/what', execute('hi/dude/what')[:file].to_s
241 242
  end
  
243 244 245 246 247 248 249 250 251 252 253 254 255 256
  def test_controller
    c = [Static.new("hi"), Controller.new(:controller)]
    g.constant_result :action, "hi"
    
    go c
    
    assert_nil execute('boo')
    assert_nil execute('boo/blah')
    assert_nil execute('hi/x')
    assert_nil execute('hi/13870948')
    assert_nil execute('hi/content/dog')
    assert_nil execute('hi/admin/user/foo')
    assert_equal({:controller => ::Controllers::ContentController, :action => 'hi'}, execute('hi/content'))
    assert_equal({:controller => ::Controllers::Admin::UserController, :action => 'hi'}, execute('hi/admin/user'))
257 258
  end
  
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
  def test_standard_route(time = ::RunTimeTests)
    c = [Controller.new(:controller), Dynamic.new(:action, :default => 'index'), Dynamic.new(:id, :default => nil)]
    go c
    
    # Make sure we get the right answers
    assert_equal({:controller => ::Controllers::ContentController, :action => 'index'}, execute('content'))
    assert_equal({:controller => ::Controllers::ContentController, :action => 'list'}, execute('content/list'))
    assert_equal({:controller => ::Controllers::ContentController, :action => 'show', :id => '10'}, execute('content/show/10'))
    
    assert_equal({:controller => ::Controllers::Admin::UserController, :action => 'index'}, execute('admin/user'))
    assert_equal({:controller => ::Controllers::Admin::UserController, :action => 'list'}, execute('admin/user/list'))
    assert_equal({:controller => ::Controllers::Admin::UserController, :action => 'show', :id => 'nseckar'}, execute('admin/user/show/nseckar'))
    
    assert_nil execute('content/show/10/20')
    assert_nil execute('food')

    if time
      source = "def self.execute(path)
        path = path.split('/') if path.is_a? String
        index = 0
        r = #{g.to_s}
      end"
      eval(source)

      GC.start
      n = 1000
      time = Benchmark.realtime do n.times {
        execute('content')
        execute('content/list')
        execute('content/show/10')
        
        execute('admin/user')
        execute('admin/user/list')
        execute('admin/user/show/nseckar')
        
        execute('admin/user/show/nseckar/dude')
        execute('admin/why/show/nseckar')
        execute('content/show/10/20')
        execute('food')
      } end
      time -= Benchmark.realtime do n.times { } end
    
      
      puts "\n\nRecognition:"
      per_url = time / (n * 10)
    
      puts "#{per_url * 1000} ms/url"
      puts "#{1 / per_url} urls/s\n\n"
    end
308
  end
309 310 311 312 313 314 315 316 317 318 319 320

  def test_default_route
    g.result :controller, "::Controllers::ContentController", true
    g.constant_result :action, 'index' 
    
    go []
    
    assert_nil execute('x')
    assert_nil execute('hello/world/how')
    assert_nil execute('hello/world/how/are')
    assert_nil execute('hello/world/how/are/you/today')
    assert_equal({:controller => ::Controllers::ContentController, :action => 'index'}, execute([]))
321
  end
322 323 324 325 326 327 328
end

class GenerationTests < Test::Unit::TestCase
  attr_accessor :generator
  alias :g :generator
  def setup
    @generator = GenerationGenerator.new # ha!
329 330
  end
  
331 332 333 334
  def go(components)
    g.current = components.first
    g.after = components[1..-1] || []
    g.go
335 336
  end
  
337 338 339 340 341 342 343 344 345
  def execute(options, recall, show = false)
    source = "\n
expire_on = ::ActionController::Routing.expiry_hash(options, recall)
hash = merged = recall.merge(options)
not_expired = true

#{g.to_s}\n\n"
    puts source if show
    eval(source)
346 347
  end
  
348 349 350 351
  Static = ::ActionController::Routing::StaticComponent
  Dynamic = ::ActionController::Routing::DynamicComponent
  Path = ::ActionController::Routing::PathComponent
  Controller = ::ActionController::Routing::ControllerComponent
352
  
353 354 355 356 357
  def test_all_static_no_requirements
    c = [Static.new("hello"), Static.new("world")]
    go c
    
    assert_equal "hello/world", execute({}, {})
358 359
  end
  
360 361 362 363 364 365 366 367
  def test_basic_dynamic
    c = [Static.new("hi"), Dynamic.new(:action)]
    go c
    
    assert_equal 'hi/index', execute({:action => 'index'}, {:action => 'index'})
    assert_equal 'hi/show', execute({:action => 'show'}, {:action => 'index'})
    assert_equal 'hi/list+people', execute({}, {:action => 'list people'})
    assert_nil execute({},{})
368 369
  end
  
370 371 372 373 374 375 376 377
  def test_dynamic_with_default
    c = [Static.new("hi"), Dynamic.new(:action, :default => 'index')]
    go c
    
    assert_equal 'hi', execute({:action => 'index'}, {:action => 'index'})
    assert_equal 'hi/show', execute({:action => 'show'}, {:action => 'index'})
    assert_equal 'hi/list+people', execute({}, {:action => 'list people'})
    assert_equal 'hi', execute({}, {})
378 379
  end
  
380 381 382 383 384 385 386 387 388 389
  def test_dynamic_with_regexp_condition
    c = [Static.new("hi"), Dynamic.new(:action, :condition => /^[a-z]+$/)]
    go c
    
    assert_equal 'hi/index', execute({:action => 'index'}, {:action => 'index'})
    assert_nil execute({:action => 'fox5'}, {:action => 'index'})
    assert_nil execute({:action => 'something_is_up'}, {:action => 'index'})
    assert_nil execute({}, {:action => 'list people'})
    assert_equal 'hi/abunchofcharacter', execute({:action => 'abunchofcharacter'}, {})
    assert_nil execute({}, {})
390
  end
391 392 393 394 395 396 397 398 399 400 401
  
  def test_dynamic_with_default_and_regexp_condition
    c = [Static.new("hi"), Dynamic.new(:action, :default => 'index', :condition => /^[a-z]+$/)]
    go c
    
    assert_equal 'hi', execute({:action => 'index'}, {:action => 'index'})
    assert_nil execute({:action => 'fox5'}, {:action => 'index'})
    assert_nil execute({:action => 'something_is_up'}, {:action => 'index'})
    assert_nil execute({}, {:action => 'list people'})
    assert_equal 'hi/abunchofcharacter', execute({:action => 'abunchofcharacter'}, {})
    assert_equal 'hi', execute({}, {})
402
  end
403 404 405 406 407 408 409 410 411 412 413 414

  def test_path
    c = [Static.new("hi"), Path.new(:file)]
    go c
    
    assert_equal 'hi', execute({:file => []}, {})
    assert_equal 'hi/books/agile_rails_dev.pdf', execute({:file => %w(books agile_rails_dev.pdf)}, {})
    assert_equal 'hi/books/development%26whatever/agile_rails_dev.pdf', execute({:file => %w(books development&whatever agile_rails_dev.pdf)}, {})
    
    assert_equal 'hi', execute({:file => ''}, {})
    assert_equal 'hi/books/agile_rails_dev.pdf', execute({:file => 'books/agile_rails_dev.pdf'}, {})
    assert_equal 'hi/books/development%26whatever/agile_rails_dev.pdf', execute({:file => 'books/development&whatever/agile_rails_dev.pdf'}, {})
415 416
  end
  
417 418 419 420 421 422 423 424 425
  def test_controller
    c = [Static.new("hi"), Controller.new(:controller)]
    go c
    
    assert_nil execute({}, {})
    assert_equal 'hi/content', execute({:controller => 'content'}, {})
    assert_equal 'hi/admin/user', execute({:controller => 'admin/user'}, {})
    assert_equal 'hi/content', execute({}, {:controller => 'content'}) 
    assert_equal 'hi/admin/user', execute({}, {:controller => 'admin/user'})
426
  end
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
  
  def test_standard_route(time = ::RunTimeTests)
    c = [Controller.new(:controller), Dynamic.new(:action, :default => 'index'), Dynamic.new(:id, :default => nil)]
    go c
    
    # Make sure we get the right answers
    assert_equal('content', execute({:action => 'index'}, {:controller => 'content', :action => 'list'}))
    assert_equal('content/list', execute({:action => 'list'}, {:controller => 'content', :action => 'index'}))
    assert_equal('content/show/10', execute({:action => 'show', :id => '10'}, {:controller => 'content', :action => 'list'}))

    assert_equal('admin/user', execute({:action => 'index'}, {:controller => 'admin/user', :action => 'list'}))
    assert_equal('admin/user/list', execute({:action => 'list'}, {:controller => 'admin/user', :action => 'index'}))
    assert_equal('admin/user/show/10', execute({:action => 'show', :id => '10'}, {:controller => 'admin/user', :action => 'list'}))

    if time
      GC.start
      n = 1000
      time = Benchmark.realtime do n.times {
        execute({:action => 'index'}, {:controller => 'content', :action => 'list'})
        execute({:action => 'list'}, {:controller => 'content', :action => 'index'})
        execute({:action => 'show', :id => '10'}, {:controller => 'content', :action => 'list'})

        execute({:action => 'index'}, {:controller => 'admin/user', :action => 'list'})
        execute({:action => 'list'}, {:controller => 'admin/user', :action => 'index'})
        execute({:action => 'show', :id => '10'}, {:controller => 'admin/user', :action => 'list'})
      } end
      time -= Benchmark.realtime do n.times { } end
    
      puts "\n\nGeneration:"
      per_url = time / (n * 6)
    
      puts "#{per_url * 1000} ms/url"
      puts "#{1 / per_url} urls/s\n\n"
    end
461
  end
462 463 464 465 466 467 468 469 470 471 472

  def test_default_route
    g.if(g.check_conditions(:controller => 'content', :action => 'welcome')) { go [] }
    
    assert_nil execute({:controller => 'foo', :action => 'welcome'}, {})
    assert_nil execute({:controller => 'content', :action => 'elcome'}, {})
    assert_nil execute({:action => 'elcome'}, {:controller => 'content'})

    assert_equal '', execute({:controller => 'content', :action => 'welcome'}, {})
    assert_equal '', execute({:action => 'welcome'}, {:controller => 'content'})
    assert_equal '', execute({:action => 'welcome', :id => '10'}, {:controller => 'content'})
473
  end
474 475 476 477 478 479
end

class RouteTests < Test::Unit::TestCase
  def route(*args)
    @route = ::ActionController::Routing::Route.new(*args) unless args.empty?
    return @route
480 481
  end
  
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
  def rec(path, show = false)
    path = path.split('/') if path.is_a? String
    index = 0
    source = route.write_recognition.to_s
    puts "\n\n#{source}\n\n" if show
    r = eval(source)
    r ? r.symbolize_keys : r
  end
  def gen(options, recall = nil, show = false)
    recall ||= options.dup
    
    expire_on = ::ActionController::Routing.expiry_hash(options, recall)
    hash = merged = recall.merge(options)
    not_expired = true
    
    source = route.write_generation.to_s
    puts "\n\n#{source}\n\n" if show
    eval(source)
    
501 502
  end
  
503 504 505 506 507 508 509 510 511 512 513 514
  def test_static
    route 'hello/world', :known => 'known_value'
    
    assert_nil rec('hello/turn')
    assert_nil rec('turn/world')
    assert_equal({:known => 'known_value'}, rec('hello/world'))
    
    assert_nil gen(:known => 'foo')
    assert_nil gen({})
    assert_equal 'hello/world', gen(:known => 'known_value')
    assert_equal 'hello/world', gen(:known => 'known_value', :extra => 'hi')
    assert_equal [:extra], route.extra_keys(:known => 'known_value', :extra => 'hi')
515 516
  end
  
517 518 519 520 521 522 523 524 525 526 527 528 529
  def test_dynamic
    route 'hello/:name', :controller => 'content', :action => 'show_person'
    
    assert_nil rec('hello')
    assert_nil rec('foo/bar')
    assert_equal({:controller => ::Controllers::ContentController, :action => 'show_person', :name => 'rails'}, rec('hello/rails'))
    assert_equal({:controller => ::Controllers::ContentController, :action => 'show_person', :name => 'Nicholas Seckar'}, rec('hello/Nicholas+Seckar'))
    
    assert_nil gen(:controller => 'content', :action => 'show_dude', :name => 'rails')
    assert_nil gen(:controller => 'content', :action => 'show_person')
    assert_nil gen(:controller => 'admin/user', :action => 'show_person', :name => 'rails')
    assert_equal 'hello/rails', gen(:controller => 'content', :action => 'show_person', :name => 'rails')
    assert_equal 'hello/Nicholas+Seckar', gen(:controller => 'content', :action => 'show_person', :name => 'Nicholas Seckar')
530
  end
531
  
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
  def test_typical
    route ':controller/:action/:id', :action => 'index', :id => nil
    assert_nil rec('hello')
    assert_nil rec('foo bar')
    assert_equal({:controller => ::Controllers::ContentController, :action => 'index'}, rec('content'))
    assert_equal({:controller => ::Controllers::Admin::UserController, :action => 'index'}, rec('admin/user'))
    
    assert_equal({:controller => ::Controllers::Admin::UserController, :action => 'index'}, rec('admin/user/index'))
    assert_equal({:controller => ::Controllers::Admin::UserController, :action => 'list'}, rec('admin/user/list'))
    assert_equal({:controller => ::Controllers::Admin::UserController, :action => 'show', :id => '10'}, rec('admin/user/show/10'))
    
    assert_equal({:controller => ::Controllers::ContentController, :action => 'list'}, rec('content/list'))
    assert_equal({:controller => ::Controllers::ContentController, :action => 'show', :id => '10'}, rec('content/show/10'))
    
    
    assert_equal 'content', gen(:controller => 'content', :action => 'index')
    assert_equal 'content/list', gen(:controller => 'content', :action => 'list')
    assert_equal 'content/show/10', gen(:controller => 'content', :action => 'show', :id => '10')
    
    assert_equal 'admin/user', gen(:controller => 'admin/user', :action => 'index')
    assert_equal 'admin/user', gen(:controller => 'admin/user')
    assert_equal 'admin/user', gen({:controller => 'admin/user'}, {:controller => 'content', :action => 'list', :id => '10'})
    assert_equal 'admin/user/show/10', gen(:controller => 'admin/user', :action => 'show', :id => '10')
555
  end
556
end
557

558 559 560 561 562
class RouteSetTests < Test::Unit::TestCase
  attr_reader :rs
  def setup
    @rs = ::ActionController::Routing::RouteSet.new
    @rs.draw {|m| m.connect ':controller/:action/:id' }
563
  end
564
  
565 566 567 568 569 570 571 572 573 574 575
  def test_default_setup
    assert_equal({:controller => ::Controllers::ContentController, :action => 'index'}.stringify_keys, rs.recognize_path(%w(content)))
    assert_equal({:controller => ::Controllers::ContentController, :action => 'list'}.stringify_keys, rs.recognize_path(%w(content list)))
    assert_equal({:controller => ::Controllers::ContentController, :action => 'show', :id => '10'}.stringify_keys, rs.recognize_path(%w(content show 10)))
    
    assert_equal({:controller => ::Controllers::Admin::UserController, :action => 'show', :id => '10'}.stringify_keys, rs.recognize_path(%w(admin user show 10)))
    
    assert_equal ['admin/user/show/10', {}], rs.generate({:controller => 'admin/user', :action => 'show', :id => 10})
    
    assert_equal ['admin/user/show', {}], rs.generate({:action => 'show'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
    assert_equal ['admin/user/list/10', {}], rs.generate({}, {:controller => 'admin/user', :action => 'list', :id => '10'})
576
  end
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
  
  def test_time_recognition
    n = 10000
    if RunTimeTests
      GC.start
      rectime = Benchmark.realtime do
        n.times do
          rs.recognize_path(%w(content))
          rs.recognize_path(%w(content list))
          rs.recognize_path(%w(content show 10))
          rs.recognize_path(%w(admin user))
          rs.recognize_path(%w(admin user list))
          rs.recognize_path(%w(admin user show 10))
        end
      end
      puts "\n\nRecognition (RouteSet):"
      per_url = rectime / (n * 6)
      puts "#{per_url * 1000} ms/url"
      puts "#{1 / per_url} url/s\n\n"
    end
597
  end
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
  def test_time_generation
    n = 5000
    if RunTimeTests
      GC.start
      pairs = [
        [{:controller => 'content', :action => 'index'}, {:controller => 'content', :action => 'show'}],
        [{:controller => 'content'}, {:controller => 'content', :action => 'index'}],   
        [{:controller => 'content', :action => 'list'}, {:controller => 'content', :action => 'index'}],
        [{:controller => 'content', :action => 'show', :id => '10'}, {:controller => 'content', :action => 'list'}],
        [{:controller => 'admin/user', :action => 'index'}, {:controller => 'admin/user', :action => 'show'}],
        [{:controller => 'admin/user'}, {:controller => 'admin/user', :action => 'index'}],
        [{:controller => 'admin/user', :action => 'list'}, {:controller => 'admin/user', :action => 'index'}],
        [{:controller => 'admin/user', :action => 'show', :id => '10'}, {:controller => 'admin/user', :action => 'list'}],
      ]
      p = nil
      gentime = Benchmark.realtime do
        n.times do
        pairs.each {|(a, b)| rs.generate(a, b)}
        end
      end
      
      puts "\n\nGeneration (RouteSet): (#{(n * 8)} urls)"
      per_url = gentime / (n * 8)
      puts "#{per_url * 1000} ms/url"
      puts "#{1 / per_url} url/s\n\n"
    end
624
  end
625

626 627 628 629 630 631 632
  def test_route_with_colon_first
    rs.draw do |map|
      map.connect '/:controller/:action/:id', :action => 'index', :id => nil
      map.connect ':url', :controller => 'tiny_url', :action => 'translate'
    end
  end

633 634 635 636 637 638 639 640 641 642 643
  def test_route_generating_string_literal_in_comparison_warning
    old_stderr = $stderr
    $stderr = StringIO.new
    rs.draw do |map|
      map.connect 'subscriptions/:action/:subscription_type', :controller => "subscriptions"
    end
    assert_equal "", $stderr.string
  ensure
    $stderr = old_stderr
  end

644 645 646 647 648
  def test_basic_named_route
    rs.home '', :controller => 'content', :action => 'list' 
    x = setup_for_named_route
    assert_equal({:controller => 'content', :action => 'list'},
                 x.new.send(:home_url))
649
  end
650 651 652 653 654 655 656 657 658 659 660 661 662

  def test_named_route_with_option
    rs.page 'page/:title', :controller => 'content', :action => 'show_page'
    x = setup_for_named_route
    assert_equal({:controller => 'content', :action => 'show_page', :title => 'new stuff'},
                 x.new.send(:page_url, :title => 'new stuff'))
  end  

  def setup_for_named_route
    x = Class.new
    x.send(:define_method, :url_for) {|x| x}
    x.send :include, ::ActionController::Routing::NamedRoutes
    x
663
  end
664 665 666 667 668 669

  def test_named_route_without_hash
    rs.draw do |map|
      rs.normal ':controller/:action/:id'
    end
  end
670 671 672 673 674 675 676

  def test_changing_controller
    assert_equal ['admin/stuff/show/10', {}], rs.generate(
      {:controller => 'stuff', :action => 'show', :id => 10},
      {:controller => 'admin/user', :action => 'index'}
    )
  end  
677 678 679
end

end