routing_test.rb 30.3 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 113 114

    class NewsFeedController
    end
115
  end
116
end
117

118 119 120 121 122 123

class RecognitionTests < Test::Unit::TestCase
  attr_accessor :generator
  alias :g :generator
  def setup
    @generator = RecognitionGenerator.new
124
  end
125 126 127 128 129

  def go(components)
    g.current = components.first
    g.after = components[1..-1] || []
    g.go
130 131
  end
  
132 133 134 135 136 137
  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
138 139
  end
  
140 141 142 143
  Static = ::ActionController::Routing::StaticComponent
  Dynamic = ::ActionController::Routing::DynamicComponent
  Path = ::ActionController::Routing::PathComponent
  Controller = ::ActionController::Routing::ControllerComponent
144
  
145 146 147 148 149 150 151 152 153 154 155 156 157
  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'))
158 159
  end

160 161 162 163
  def test_basic_dynamic
    c = [Static.new("hi"), Dynamic.new(:action)]
    g.result :controller, "::Controllers::ContentController", true
    go c
D
David Heinemeier Hansson 已提交
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 193 194 195
    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 已提交
196 197
  end
  
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
  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 已提交
213
  
214 215 216 217 218 219 220 221 222 223 224 225 226 227
  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 已提交
228 229
  end

230 231 232 233 234 235 236 237 238 239 240 241 242 243
  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
244 245
  end
  
246 247 248 249 250 251 252 253 254 255 256 257 258 259
  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'))
260 261
  end
  
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 308 309 310
  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
311
  end
312 313 314 315 316 317 318 319 320 321 322 323

  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([]))
324
  end
325 326 327 328 329 330 331
end

class GenerationTests < Test::Unit::TestCase
  attr_accessor :generator
  alias :g :generator
  def setup
    @generator = GenerationGenerator.new # ha!
332 333
  end
  
334 335 336 337
  def go(components)
    g.current = components.first
    g.after = components[1..-1] || []
    g.go
338 339
  end
  
340 341 342 343 344 345 346 347 348
  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)
349 350
  end
  
351 352 353 354
  Static = ::ActionController::Routing::StaticComponent
  Dynamic = ::ActionController::Routing::DynamicComponent
  Path = ::ActionController::Routing::PathComponent
  Controller = ::ActionController::Routing::ControllerComponent
355
  
356 357 358 359
  def test_all_static_no_requirements
    c = [Static.new("hello"), Static.new("world")]
    go c
    
360
    assert_equal "/hello/world", execute({}, {})
361 362
  end
  
363 364 365 366
  def test_basic_dynamic
    c = [Static.new("hi"), Dynamic.new(:action)]
    go c
    
367 368 369
    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'})
370
    assert_nil execute({},{})
371 372
  end
  
373 374 375 376
  def test_dynamic_with_default
    c = [Static.new("hi"), Dynamic.new(:action, :default => 'index')]
    go c
    
377 378 379 380
    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({}, {})
381 382
  end
  
383 384 385 386
  def test_dynamic_with_regexp_condition
    c = [Static.new("hi"), Dynamic.new(:action, :condition => /^[a-z]+$/)]
    go c
    
387
    assert_equal '/hi/index', execute({:action => 'index'}, {:action => 'index'})
388 389 390
    assert_nil execute({:action => 'fox5'}, {:action => 'index'})
    assert_nil execute({:action => 'something_is_up'}, {:action => 'index'})
    assert_nil execute({}, {:action => 'list people'})
391
    assert_equal '/hi/abunchofcharacter', execute({:action => 'abunchofcharacter'}, {})
392
    assert_nil execute({}, {})
393
  end
394 395 396 397 398
  
  def test_dynamic_with_default_and_regexp_condition
    c = [Static.new("hi"), Dynamic.new(:action, :default => 'index', :condition => /^[a-z]+$/)]
    go c
    
399
    assert_equal '/hi', execute({:action => 'index'}, {:action => 'index'})
400 401 402
    assert_nil execute({:action => 'fox5'}, {:action => 'index'})
    assert_nil execute({:action => 'something_is_up'}, {:action => 'index'})
    assert_nil execute({}, {:action => 'list people'})
403 404
    assert_equal '/hi/abunchofcharacter', execute({:action => 'abunchofcharacter'}, {})
    assert_equal '/hi', execute({}, {})
405
  end
406 407 408 409 410

  def test_path
    c = [Static.new("hi"), Path.new(:file)]
    go c
    
411 412 413
    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)}, {})
414
    
415 416 417
    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'}, {})
418 419
  end
  
420 421 422 423 424
  def test_controller
    c = [Static.new("hi"), Controller.new(:controller)]
    go c
    
    assert_nil execute({}, {})
425 426 427 428
    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'})
429
  end
430 431 432 433 434 435
  
  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
436 437 438
    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'}))
439

440 441 442
    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'}))
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463

    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
464
  end
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'})

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

class RouteTests < Test::Unit::TestCase
  def route(*args)
    @route = ::ActionController::Routing::Route.new(*args) unless args.empty?
    return @route
483 484
  end
  
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
  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)
    
504 505
  end
  
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({})
515 516
    assert_equal '/hello/world', gen(:known => 'known_value')
    assert_equal '/hello/world', gen(:known => 'known_value', :extra => 'hi')
517
    assert_equal [:extra], route.extra_keys(:known => 'known_value', :extra => 'hi')
518 519
  end
  
520 521 522 523 524 525 526 527 528 529 530
  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')
531 532
    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')
533
  end
534
  
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
  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'))
    
    
550 551 552
    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')
553
    
554 555 556 557
    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')
558
  end
559
end
560

561 562 563 564 565
class RouteSetTests < Test::Unit::TestCase
  attr_reader :rs
  def setup
    @rs = ::ActionController::Routing::RouteSet.new
    @rs.draw {|m| m.connect ':controller/:action/:id' }
566
  end
567
  
568 569 570 571 572 573 574
  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)))
    
575
    assert_equal ['/admin/user/show/10', {}], rs.generate({:controller => 'admin/user', :action => 'show', :id => 10})
576
    
577 578
    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'})
579 580 581

    assert_equal ['/admin/stuff', {}], rs.generate({:controller => 'stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
    assert_equal ['/stuff', {}], rs.generate({:controller => '/stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
582
  end
583 584 585 586 587

  def test_ignores_leading_slash
    @rs.draw {|m| m.connect '/:controller/:action/:id'}
    test_default_setup
  end
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
  
  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
608
  end
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
  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
635
  end
636

637 638 639 640 641 642 643
  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

644 645 646 647 648 649 650 651 652 653 654
  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

655 656 657
  def test_basic_named_route
    rs.home '', :controller => 'content', :action => 'list' 
    x = setup_for_named_route
D
David Heinemeier Hansson 已提交
658
    assert_equal({:controller => '/content', :action => 'list'},
659
                 x.new.send(:home_url))
660
  end
661 662 663 664

  def test_named_route_with_option
    rs.page 'page/:title', :controller => 'content', :action => 'show_page'
    x = setup_for_named_route
D
David Heinemeier Hansson 已提交
665
    assert_equal({:controller => '/content', :action => 'show_page', :title => 'new stuff'},
666
                 x.new.send(:page_url, :title => 'new stuff'))
667 668 669 670 671 672 673 674 675 676 677
  end

  def test_named_route_with_default
    rs.page 'page/:title', :controller => 'content', :action => 'show_page', :title => 'AboutPage'
    x = setup_for_named_route
    assert_equal({:controller => '/content', :action => 'show_page', :title => 'AboutPage'},
                 x.new.send(:page_url))
    assert_equal({:controller => '/content', :action => 'show_page', :title => 'AboutRails'},
                 x.new.send(:page_url, :title => "AboutRails"))

  end
678 679 680 681 682 683

  def setup_for_named_route
    x = Class.new
    x.send(:define_method, :url_for) {|x| x}
    x.send :include, ::ActionController::Routing::NamedRoutes
    x
684
  end
685 686 687 688 689 690

  def test_named_route_without_hash
    rs.draw do |map|
      rs.normal ':controller/:action/:id'
    end
  end
691

692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
  def test_named_route_with_regexps
    rs.draw do |map|
      rs.article 'page/:year/:month/:day/:title', :controller => 'page', :action => 'show',
        :year => /^\d+$/, :month => /^\d+$/, :day => /^\d+$/
      rs.connect ':controller/:action/:id'
    end
    x = setup_for_named_route
    assert_equal(
      {:controller => '/page', :action => 'show', :title => 'hi'},
      x.new.send(:article_url, :title => 'hi')
    )
    assert_equal(
      {:controller => '/page', :action => 'show', :title => 'hi', :day => 10, :year => 2005, :month => 6},
      x.new.send(:article_url, :title => 'hi', :day => 10, :year => 2005, :month => 6)
    )
  end

709
  def test_changing_controller
710
    assert_equal ['/admin/stuff/show/10', {}], rs.generate(
711 712 713 714
      {:controller => 'stuff', :action => 'show', :id => 10},
      {:controller => 'admin/user', :action => 'index'}
    )
  end  
J
Jamis Buck 已提交
715

716 717 718 719 720 721 722 723 724
  def test_paths_escaped
    rs.draw do |map|
      rs.path 'file/*path', :controller => 'content', :action => 'show_file'
      rs.connect ':controller/:action/:id'
    end
    
    results = rs.recognize_path %w(file hello+world how+are+you%3F)
    assert results, "Recognition should have succeeded"
    assert_equal ['hello world', 'how are you?'], results['path']
725 726 727 728

    results = rs.recognize_path %w(file)
    assert results, "Recognition should have succeeded"
    assert_equal [], results['path']
729 730
  end

731 732 733 734 735 736 737 738 739 740 741 742 743 744
  def test_paths_do_not_accept_defaults
    assert_raises(ActionController::RoutingError) do
      rs.draw do |map|
        rs.path 'file/*path', :controller => 'content', :action => 'show_file', :path => %w(fake default)
        rs.connect ':controller/:action/:id'
      end
    end
    
    rs.draw do |map|
      rs.path 'file/*path', :controller => 'content', :action => 'show_file', :path => []
      rs.connect ':controller/:action/:id'
    end
  end
  
J
Jamis Buck 已提交
745 746 747 748 749 750
  def test_backwards
    rs.draw do |map|
      rs.connect 'page/:id/:action', :controller => 'pages', :action => 'show'
      rs.connect ':controller/:action/:id'
    end

751
    assert_equal ['/page/20', {}], rs.generate({:id => 20}, {:controller => 'pages'})
J
Jamis Buck 已提交
752 753 754
    assert_equal ['/page/20', {}], rs.generate(:controller => 'pages', :id => 20, :action => 'show')
    assert_equal ['/pages/boo', {}], rs.generate(:controller => 'pages', :action => 'boo')
  end
755

756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
  def test_route_with_fixnum_default
    rs.draw do |map|
      rs.connect 'page/:id', :controller => 'content', :action => 'show_page', :id => 1
      rs.connect ':controller/:action/:id'
    end

    assert_equal ['/page', {}], rs.generate(:controller => 'content', :action => 'show_page')
    assert_equal ['/page', {}], rs.generate(:controller => 'content', :action => 'show_page', :id => 1)
    assert_equal ['/page', {}], rs.generate(:controller => 'content', :action => 'show_page', :id => '1')
    assert_equal ['/page/10', {}], rs.generate(:controller => 'content', :action => 'show_page', :id => 10)

    ctrl = ::Controllers::ContentController

    assert_equal({'controller' => ctrl, 'action' => 'show_page', 'id' => 1}, rs.recognize_path(%w(page)))
    assert_equal({'controller' => ctrl, 'action' => 'show_page', 'id' => '1'}, rs.recognize_path(%w(page 1)))
    assert_equal({'controller' => ctrl, 'action' => 'show_page', 'id' => '10'}, rs.recognize_path(%w(page 10)))
  end

774 775 776
  def test_action_expiry
    assert_equal ['/content', {}], rs.generate({:controller => 'content'}, {:controller => 'content', :action => 'show'})
  end
777 778 779 780 781 782 783 784 785

  def test_recognition_with_uppercase_controller_name
    assert_equal({'controller' => ::Controllers::ContentController, 'action' => 'index'}, rs.recognize_path(%w(Content)))
    assert_equal({'controller' => ::Controllers::ContentController, 'action' => 'list'}, rs.recognize_path(%w(Content list)))
    assert_equal({'controller' => ::Controllers::ContentController, 'action' => 'show', 'id' => '10'}, rs.recognize_path(%w(Content show 10)))

    assert_equal({'controller' => ::Controllers::Admin::NewsFeedController, 'action' => 'index'}, rs.recognize_path(%w(Admin NewsFeed)))
    assert_equal({'controller' => ::Controllers::Admin::NewsFeedController, 'action' => 'index'}, rs.recognize_path(%w(Admin News_Feed)))
  end
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804

  def test_both_requirement_and_optional
    rs.draw do
      rs.blog('test/:year', :controller => 'post', :action => 'show',
        :defaults => { :year => nil },
        :requirements => { :year => /\d{4}/ }
      )
      rs.connect ':controller/:action/:id'
    end

    assert_equal ['/test', {}], rs.generate(:controller => 'post', :action => 'show')
    assert_equal ['/test', {}], rs.generate(:controller => 'post', :action => 'show', :year => nil)
    
    x = setup_for_named_route
    assert_equal({:controller => '/post', :action => 'show'},
                 x.new.send(:blog_url))
  end


805 806 807
end

end