test_route.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. require 'helper'
  2. module Journey
  3. class TestRoute < MiniTest::Unit::TestCase
  4. def test_initialize
  5. app = Object.new
  6. path = Path::Pattern.new '/:controller(/:action(/:id(.:format)))'
  7. defaults = Object.new
  8. route = Route.new("name", app, path, {}, defaults)
  9. assert_equal app, route.app
  10. assert_equal path, route.path
  11. assert_equal defaults, route.defaults
  12. end
  13. def test_route_adds_itself_as_memo
  14. app = Object.new
  15. path = Path::Pattern.new '/:controller(/:action(/:id(.:format)))'
  16. defaults = Object.new
  17. route = Route.new("name", app, path, {}, defaults)
  18. route.ast.grep(Nodes::Terminal).each do |node|
  19. assert_equal route, node.memo
  20. end
  21. end
  22. def test_ip_address
  23. path = Path::Pattern.new '/messages/:id(.:format)'
  24. route = Route.new("name", nil, path, {:ip => '192.168.1.1'},
  25. { :controller => 'foo', :action => 'bar' })
  26. assert_equal '192.168.1.1', route.ip
  27. end
  28. def test_default_ip
  29. path = Path::Pattern.new '/messages/:id(.:format)'
  30. route = Route.new("name", nil, path, {},
  31. { :controller => 'foo', :action => 'bar' })
  32. assert_equal(//, route.ip)
  33. end
  34. def test_format_empty
  35. path = Path::Pattern.new '/messages/:id(.:format)'
  36. route = Route.new("name", nil, path, {},
  37. { :controller => 'foo', :action => 'bar' })
  38. assert_equal '/messages', route.format({})
  39. end
  40. def test_format_with_star
  41. path = Path::Pattern.new '/:controller/*extra'
  42. route = Route.new("name", nil, path, {},
  43. { :controller => 'foo', :action => 'bar' })
  44. assert_equal '/foo/himom', route.format({
  45. :controller => 'foo',
  46. :extra => 'himom',
  47. })
  48. end
  49. def test_connects_all_match
  50. path = Path::Pattern.new '/:controller(/:action(/:id(.:format)))'
  51. route = Route.new("name", nil, path, {:action => 'bar'}, { :controller => 'foo' })
  52. assert_equal '/foo/bar/10', route.format({
  53. :controller => 'foo',
  54. :action => 'bar',
  55. :id => 10
  56. })
  57. end
  58. def test_extras_are_not_included_if_optional
  59. path = Path::Pattern.new '/page/:id(/:action)'
  60. route = Route.new("name", nil, path, { }, { :action => 'show' })
  61. assert_equal '/page/10', route.format({ :id => 10 })
  62. end
  63. def test_extras_are_not_included_if_optional_with_parameter
  64. path = Path::Pattern.new '(/sections/:section)/pages/:id'
  65. route = Route.new("name", nil, path, { }, { :action => 'show' })
  66. assert_equal '/pages/10', route.format({:id => 10})
  67. end
  68. def test_extras_are_not_included_if_optional_parameter_is_nil
  69. path = Path::Pattern.new '(/sections/:section)/pages/:id'
  70. route = Route.new("name", nil, path, { }, { :action => 'show' })
  71. assert_equal '/pages/10', route.format({:id => 10, :section => nil})
  72. end
  73. def test_score
  74. path = Path::Pattern.new "/page/:id(/:action)(.:format)"
  75. specific = Route.new "name", nil, path, {}, {:controller=>"pages", :action=>"show"}
  76. path = Path::Pattern.new "/:controller(/:action(/:id))(.:format)"
  77. generic = Route.new "name", nil, path, {}
  78. knowledge = {:id=>20, :controller=>"pages", :action=>"show"}
  79. routes = [specific, generic]
  80. refute_equal specific.score(knowledge), generic.score(knowledge)
  81. found = routes.sort_by { |r| r.score(knowledge) }.last
  82. assert_equal specific, found
  83. end
  84. end
  85. end