PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/ruby/1.9.1/gems/journey-1.0.4/test/path/test_pattern.rb

https://github.com/KaylaStuebbe/uwsp-virtual-tour-server
Ruby | 282 lines | 249 code | 31 blank | 2 comment | 0 complexity | e528b86cb7f1da6c08082ec15c7c9ce6 MD5 | raw file
Possible License(s): Apache-2.0, MIT, GPL-2.0, BSD-3-Clause
  1. require 'helper'
  2. module Journey
  3. module Path
  4. class TestPattern < MiniTest::Unit::TestCase
  5. x = /.+/
  6. {
  7. '/:controller(/:action)' => %r{\A/(#{x})(?:/([^/.?]+))?\Z},
  8. '/:controller/foo' => %r{\A/(#{x})/foo\Z},
  9. '/:controller/:action' => %r{\A/(#{x})/([^/.?]+)\Z},
  10. '/:controller' => %r{\A/(#{x})\Z},
  11. '/:controller(/:action(/:id))' => %r{\A/(#{x})(?:/([^/.?]+)(?:/([^/.?]+))?)?\Z},
  12. '/:controller/:action.xml' => %r{\A/(#{x})/([^/.?]+)\.xml\Z},
  13. '/:controller.:format' => %r{\A/(#{x})\.([^/.?]+)\Z},
  14. '/:controller(.:format)' => %r{\A/(#{x})(?:\.([^/.?]+))?\Z},
  15. '/:controller/*foo' => %r{\A/(#{x})/(.+)\Z},
  16. '/:controller/*foo/bar' => %r{\A/(#{x})/(.+)/bar\Z},
  17. }.each do |path, expected|
  18. define_method(:"test_to_regexp_#{path}") do
  19. strexp = Router::Strexp.new(
  20. path,
  21. { :controller => /.+/ },
  22. ["/", ".", "?"]
  23. )
  24. path = Pattern.new strexp
  25. assert_equal(expected, path.to_regexp)
  26. end
  27. end
  28. {
  29. '/:controller(/:action)' => %r{\A/(#{x})(?:/([^/.?]+))?},
  30. '/:controller/foo' => %r{\A/(#{x})/foo},
  31. '/:controller/:action' => %r{\A/(#{x})/([^/.?]+)},
  32. '/:controller' => %r{\A/(#{x})},
  33. '/:controller(/:action(/:id))' => %r{\A/(#{x})(?:/([^/.?]+)(?:/([^/.?]+))?)?},
  34. '/:controller/:action.xml' => %r{\A/(#{x})/([^/.?]+)\.xml},
  35. '/:controller.:format' => %r{\A/(#{x})\.([^/.?]+)},
  36. '/:controller(.:format)' => %r{\A/(#{x})(?:\.([^/.?]+))?},
  37. '/:controller/*foo' => %r{\A/(#{x})/(.+)},
  38. '/:controller/*foo/bar' => %r{\A/(#{x})/(.+)/bar},
  39. }.each do |path, expected|
  40. define_method(:"test_to_non_anchored_regexp_#{path}") do
  41. strexp = Router::Strexp.new(
  42. path,
  43. { :controller => /.+/ },
  44. ["/", ".", "?"],
  45. false
  46. )
  47. path = Pattern.new strexp
  48. assert_equal(expected, path.to_regexp)
  49. end
  50. end
  51. {
  52. '/:controller(/:action)' => %w{ controller action },
  53. '/:controller/foo' => %w{ controller },
  54. '/:controller/:action' => %w{ controller action },
  55. '/:controller' => %w{ controller },
  56. '/:controller(/:action(/:id))' => %w{ controller action id },
  57. '/:controller/:action.xml' => %w{ controller action },
  58. '/:controller.:format' => %w{ controller format },
  59. '/:controller(.:format)' => %w{ controller format },
  60. '/:controller/*foo' => %w{ controller foo },
  61. '/:controller/*foo/bar' => %w{ controller foo },
  62. }.each do |path, expected|
  63. define_method(:"test_names_#{path}") do
  64. strexp = Router::Strexp.new(
  65. path,
  66. { :controller => /.+/ },
  67. ["/", ".", "?"]
  68. )
  69. path = Pattern.new strexp
  70. assert_equal(expected, path.names)
  71. end
  72. end
  73. def test_to_regexp_with_extended_group
  74. strexp = Router::Strexp.new(
  75. '/page/:name',
  76. { :name => /
  77. #ROFL
  78. (tender|love
  79. #MAO
  80. )/x },
  81. ["/", ".", "?"]
  82. )
  83. path = Pattern.new strexp
  84. assert_match('/page/tender', path)
  85. assert_match('/page/love', path)
  86. refute_match('/page/loving', path)
  87. end
  88. def test_optional_names
  89. [
  90. ['/:foo(/:bar(/:baz))', %w{ bar baz }],
  91. ['/:foo(/:bar)', %w{ bar }],
  92. ['/:foo(/:bar)/:lol(/:baz)', %w{ bar baz }],
  93. ].each do |pattern, list|
  94. path = Pattern.new pattern
  95. assert_equal list.sort, path.optional_names.sort
  96. end
  97. end
  98. def test_to_regexp_match_non_optional
  99. strexp = Router::Strexp.new(
  100. '/:name',
  101. { :name => /\d+/ },
  102. ["/", ".", "?"]
  103. )
  104. path = Pattern.new strexp
  105. assert_match('/123', path)
  106. refute_match('/', path)
  107. end
  108. def test_to_regexp_with_group
  109. strexp = Router::Strexp.new(
  110. '/page/:name',
  111. { :name => /(tender|love)/ },
  112. ["/", ".", "?"]
  113. )
  114. path = Pattern.new strexp
  115. assert_match('/page/tender', path)
  116. assert_match('/page/love', path)
  117. refute_match('/page/loving', path)
  118. end
  119. def test_ast_sets_regular_expressions
  120. requirements = { :name => /(tender|love)/, :value => /./ }
  121. strexp = Router::Strexp.new(
  122. '/page/:name/:value',
  123. requirements,
  124. ["/", ".", "?"]
  125. )
  126. assert_equal requirements, strexp.requirements
  127. path = Pattern.new strexp
  128. nodes = path.ast.grep(Nodes::Symbol)
  129. assert_equal 2, nodes.length
  130. nodes.each do |node|
  131. assert_equal requirements[node.to_sym], node.regexp
  132. end
  133. end
  134. def test_match_data_with_group
  135. strexp = Router::Strexp.new(
  136. '/page/:name',
  137. { :name => /(tender|love)/ },
  138. ["/", ".", "?"]
  139. )
  140. path = Pattern.new strexp
  141. match = path.match '/page/tender'
  142. assert_equal 'tender', match[1]
  143. assert_equal 2, match.length
  144. end
  145. def test_match_data_with_multi_group
  146. strexp = Router::Strexp.new(
  147. '/page/:name/:id',
  148. { :name => /t(((ender|love)))()/ },
  149. ["/", ".", "?"]
  150. )
  151. path = Pattern.new strexp
  152. match = path.match '/page/tender/10'
  153. assert_equal 'tender', match[1]
  154. assert_equal '10', match[2]
  155. assert_equal 3, match.length
  156. assert_equal %w{ tender 10 }, match.captures
  157. end
  158. def test_star_with_custom_re
  159. z = /\d+/
  160. strexp = Router::Strexp.new(
  161. '/page/*foo',
  162. { :foo => z },
  163. ["/", ".", "?"]
  164. )
  165. path = Pattern.new strexp
  166. assert_equal(%r{\A/page/(#{z})\Z}, path.to_regexp)
  167. end
  168. def test_insensitive_regexp_with_group
  169. strexp = Router::Strexp.new(
  170. '/page/:name/aaron',
  171. { :name => /(tender|love)/i },
  172. ["/", ".", "?"]
  173. )
  174. path = Pattern.new strexp
  175. assert_match('/page/TENDER/aaron', path)
  176. assert_match('/page/loVE/aaron', path)
  177. refute_match('/page/loVE/AAron', path)
  178. end
  179. def test_to_regexp_with_strexp
  180. strexp = Router::Strexp.new('/:controller', { }, ["/", ".", "?"])
  181. path = Pattern.new strexp
  182. x = %r{\A/([^/.?]+)\Z}
  183. assert_equal(x.source, path.source)
  184. end
  185. def test_to_regexp_defaults
  186. path = Pattern.new '/:controller(/:action(/:id))'
  187. expected = %r{\A/([^/.?]+)(?:/([^/.?]+)(?:/([^/.?]+))?)?\Z}
  188. assert_equal expected, path.to_regexp
  189. end
  190. def test_failed_match
  191. path = Pattern.new '/:controller(/:action(/:id(.:format)))'
  192. uri = 'content'
  193. refute path =~ uri
  194. end
  195. def test_match_controller
  196. path = Pattern.new '/:controller(/:action(/:id(.:format)))'
  197. uri = '/content'
  198. match = path =~ uri
  199. assert_equal %w{ controller action id format }, match.names
  200. assert_equal 'content', match[1]
  201. assert_nil match[2]
  202. assert_nil match[3]
  203. assert_nil match[4]
  204. end
  205. def test_match_controller_action
  206. path = Pattern.new '/:controller(/:action(/:id(.:format)))'
  207. uri = '/content/list'
  208. match = path =~ uri
  209. assert_equal %w{ controller action id format }, match.names
  210. assert_equal 'content', match[1]
  211. assert_equal 'list', match[2]
  212. assert_nil match[3]
  213. assert_nil match[4]
  214. end
  215. def test_match_controller_action_id
  216. path = Pattern.new '/:controller(/:action(/:id(.:format)))'
  217. uri = '/content/list/10'
  218. match = path =~ uri
  219. assert_equal %w{ controller action id format }, match.names
  220. assert_equal 'content', match[1]
  221. assert_equal 'list', match[2]
  222. assert_equal '10', match[3]
  223. assert_nil match[4]
  224. end
  225. def test_match_literal
  226. path = Path::Pattern.new "/books(/:action(.:format))"
  227. uri = '/books'
  228. match = path =~ uri
  229. assert_equal %w{ action format }, match.names
  230. assert_nil match[1]
  231. assert_nil match[2]
  232. end
  233. def test_match_literal_with_action
  234. path = Path::Pattern.new "/books(/:action(.:format))"
  235. uri = '/books/list'
  236. match = path =~ uri
  237. assert_equal %w{ action format }, match.names
  238. assert_equal 'list', match[1]
  239. assert_nil match[2]
  240. end
  241. def test_match_literal_with_action_and_format
  242. path = Path::Pattern.new "/books(/:action(.:format))"
  243. uri = '/books/list.rss'
  244. match = path =~ uri
  245. assert_equal %w{ action format }, match.names
  246. assert_equal 'list', match[1]
  247. assert_equal 'rss', match[2]
  248. end
  249. end
  250. end
  251. end