actionpack 1.8.1 → 1.9.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of actionpack might be problematic. Click here for more details.

Files changed (101) hide show
  1. data/CHANGELOG +309 -16
  2. data/README +1 -1
  3. data/lib/action_controller.rb +5 -0
  4. data/lib/action_controller/assertions.rb +57 -12
  5. data/lib/action_controller/auto_complete.rb +47 -0
  6. data/lib/action_controller/base.rb +288 -258
  7. data/lib/action_controller/benchmarking.rb +8 -3
  8. data/lib/action_controller/caching.rb +88 -42
  9. data/lib/action_controller/cgi_ext/cgi_ext.rb +1 -1
  10. data/lib/action_controller/cgi_ext/cgi_methods.rb +41 -11
  11. data/lib/action_controller/cgi_ext/multipart_progress.rb +169 -0
  12. data/lib/action_controller/cgi_ext/raw_post_data_fix.rb +30 -12
  13. data/lib/action_controller/cgi_process.rb +39 -11
  14. data/lib/action_controller/code_generation.rb +235 -0
  15. data/lib/action_controller/cookies.rb +14 -8
  16. data/lib/action_controller/deprecated_renders_and_redirects.rb +76 -0
  17. data/lib/action_controller/filters.rb +8 -7
  18. data/lib/action_controller/helpers.rb +41 -6
  19. data/lib/action_controller/layout.rb +45 -16
  20. data/lib/action_controller/request.rb +86 -23
  21. data/lib/action_controller/rescue.rb +1 -0
  22. data/lib/action_controller/response.rb +1 -1
  23. data/lib/action_controller/routing.rb +536 -272
  24. data/lib/action_controller/scaffolding.rb +30 -25
  25. data/lib/action_controller/session/active_record_store.rb +251 -50
  26. data/lib/action_controller/streaming.rb +133 -0
  27. data/lib/action_controller/templates/rescues/_request_and_response.rhtml +0 -7
  28. data/lib/action_controller/templates/scaffolds/edit.rhtml +2 -2
  29. data/lib/action_controller/templates/scaffolds/layout.rhtml +22 -18
  30. data/lib/action_controller/templates/scaffolds/list.rhtml +3 -3
  31. data/lib/action_controller/templates/scaffolds/new.rhtml +2 -2
  32. data/lib/action_controller/templates/scaffolds/show.rhtml +1 -1
  33. data/lib/action_controller/test_process.rb +68 -47
  34. data/lib/action_controller/upload_progress.rb +421 -0
  35. data/lib/action_controller/url_rewriter.rb +8 -11
  36. data/lib/action_controller/vendor/html-scanner/html/document.rb +6 -5
  37. data/lib/action_controller/vendor/html-scanner/html/node.rb +70 -14
  38. data/lib/action_controller/vendor/html-scanner/html/tokenizer.rb +17 -10
  39. data/lib/action_controller/vendor/html-scanner/html/version.rb +3 -3
  40. data/lib/action_controller/vendor/xml_simple.rb +1019 -0
  41. data/lib/action_controller/verification.rb +36 -30
  42. data/lib/action_view/base.rb +21 -14
  43. data/lib/action_view/helpers/active_record_helper.rb +15 -13
  44. data/lib/action_view/helpers/asset_tag_helper.rb +26 -9
  45. data/lib/action_view/helpers/benchmark_helper.rb +24 -0
  46. data/lib/action_view/helpers/capture_helper.rb +7 -5
  47. data/lib/action_view/helpers/date_helper.rb +63 -46
  48. data/lib/action_view/helpers/form_helper.rb +7 -1
  49. data/lib/action_view/helpers/form_options_helper.rb +19 -11
  50. data/lib/action_view/helpers/form_tag_helper.rb +5 -1
  51. data/lib/action_view/helpers/javascript_helper.rb +403 -35
  52. data/lib/action_view/helpers/javascripts/controls.js +261 -0
  53. data/lib/action_view/helpers/javascripts/dragdrop.js +476 -0
  54. data/lib/action_view/helpers/javascripts/effects.js +570 -0
  55. data/lib/action_view/helpers/javascripts/prototype.js +633 -371
  56. data/lib/action_view/helpers/number_helper.rb +11 -13
  57. data/lib/action_view/helpers/tag_helper.rb +1 -2
  58. data/lib/action_view/helpers/text_helper.rb +69 -6
  59. data/lib/action_view/helpers/upload_progress_helper.rb +433 -0
  60. data/lib/action_view/helpers/url_helper.rb +98 -3
  61. data/lib/action_view/partials.rb +14 -8
  62. data/lib/action_view/vendor/builder/xmlmarkup.rb +11 -0
  63. data/rakefile +13 -5
  64. data/test/abstract_unit.rb +1 -1
  65. data/test/controller/action_pack_assertions_test.rb +52 -9
  66. data/test/controller/active_record_assertions_test.rb +119 -120
  67. data/test/controller/active_record_store_test.rb +111 -0
  68. data/test/controller/addresses_render_test.rb +45 -0
  69. data/test/controller/caching_filestore.rb +92 -0
  70. data/test/controller/capture_test.rb +39 -0
  71. data/test/controller/cgi_test.rb +40 -3
  72. data/test/controller/helper_test.rb +65 -13
  73. data/test/controller/multipart_progress_testx.rb +365 -0
  74. data/test/controller/new_render_test.rb +263 -0
  75. data/test/controller/redirect_test.rb +64 -0
  76. data/test/controller/render_test.rb +20 -21
  77. data/test/controller/request_test.rb +83 -3
  78. data/test/controller/routing_test.rb +702 -0
  79. data/test/controller/send_file_test.rb +2 -0
  80. data/test/controller/test_test.rb +44 -8
  81. data/test/controller/upload_progress_testx.rb +89 -0
  82. data/test/controller/verification_test.rb +94 -29
  83. data/test/fixtures/addresses/list.rhtml +1 -0
  84. data/test/fixtures/test/capturing.rhtml +4 -0
  85. data/test/fixtures/test/list.rhtml +1 -1
  86. data/test/fixtures/test/update_element_with_capture.rhtml +9 -0
  87. data/test/template/active_record_helper_test.rb +30 -15
  88. data/test/template/asset_tag_helper_test.rb +12 -5
  89. data/test/template/benchmark_helper_test.rb +72 -0
  90. data/test/template/date_helper_test.rb +69 -0
  91. data/test/template/form_helper_test.rb +18 -10
  92. data/test/template/form_options_helper_test.rb +40 -5
  93. data/test/template/javascript_helper.rb +149 -2
  94. data/test/template/number_helper_test.rb +2 -0
  95. data/test/template/tag_helper_test.rb +4 -0
  96. data/test/template/text_helper_test.rb +36 -0
  97. data/test/template/upload_progress_helper_testx.rb +272 -0
  98. data/test/template/url_helper_test.rb +30 -0
  99. metadata +30 -6
  100. data/test/controller/layout_test.rb +0 -49
  101. data/test/controller/routing_tests.rb +0 -543
@@ -9,6 +9,14 @@ class RedirectController < ActionController::Base
9
9
  redirect_to :dashbord_url, 1, "hello"
10
10
  end
11
11
 
12
+ def host_redirect
13
+ redirect_to :action => "other_host", :only_path => false, :host => 'other.test.host'
14
+ end
15
+
16
+ def module_redirect
17
+ redirect_to :controller => 'module_test/module_redirect', :action => "hello_world"
18
+ end
19
+
12
20
  def rescue_errors(e) raise e end
13
21
 
14
22
  protected
@@ -33,4 +41,60 @@ class RedirectTest < Test::Unit::TestCase
33
41
  get :method_redirect
34
42
  assert_redirect_url "http://test.host/redirect/dashboard/1?message=hello"
35
43
  end
44
+
45
+ def test_simple_redirect_using_options
46
+ get :host_redirect
47
+ assert_redirected_to :action => "other_host", :only_path => false, :host => 'other.test.host'
48
+ end
49
+
50
+ def test_module_redirect
51
+ get :module_redirect
52
+ assert_redirect_url "http://test.host/module_test/module_redirect/hello_world"
53
+ end
54
+
55
+ def test_module_redirect_using_options
56
+ get :module_redirect
57
+ assert_redirected_to :controller => 'module_test/module_redirect', :action => 'hello_world'
58
+ end
59
+ end
60
+
61
+ module ModuleTest
62
+ class ModuleRedirectController < ::RedirectController
63
+ def module_redirect
64
+ redirect_to :controller => '/redirect', :action => "hello_world"
65
+ end
66
+ end
67
+
68
+ class ModuleRedirectTest < Test::Unit::TestCase
69
+ def setup
70
+ @controller = ModuleRedirectController.new
71
+ @request = ActionController::TestRequest.new
72
+ @response = ActionController::TestResponse.new
73
+ end
74
+
75
+ def test_simple_redirect
76
+ get :simple_redirect
77
+ assert_redirect_url "http://test.host/module_test/module_redirect/hello_world"
78
+ end
79
+
80
+ def test_redirect_with_method_reference_and_parameters
81
+ get :method_redirect
82
+ assert_redirect_url "http://test.host/module_test/module_redirect/dashboard/1?message=hello"
83
+ end
84
+
85
+ def test_simple_redirect_using_options
86
+ get :host_redirect
87
+ assert_redirected_to :action => "other_host", :only_path => false, :host => 'other.test.host'
88
+ end
89
+
90
+ def test_module_redirect
91
+ get :module_redirect
92
+ assert_redirect_url "http://test.host/redirect/hello_world"
93
+ end
94
+
95
+ def test_module_redirect_using_options
96
+ get :module_redirect
97
+ assert_redirected_to :controller => 'redirect', :action => "hello_world"
98
+ end
99
+ end
36
100
  end
@@ -1,6 +1,8 @@
1
1
  require File.dirname(__FILE__) + '/../abstract_unit'
2
2
 
3
- Customer = Struct.new("Customer", :name)
3
+ unless defined?(Customer)
4
+ Customer = Struct.new("Customer", :name)
5
+ end
4
6
 
5
7
  module Fun
6
8
  class GamesController < ActionController::Base
@@ -55,6 +57,7 @@ class TestController < ActionController::Base
55
57
  end
56
58
 
57
59
  def partials_list
60
+ @test_unchanged = 'hello'
58
61
  @customers = [ Customer.new("david"), Customer.new("mary") ]
59
62
  render_action "list"
60
63
  end
@@ -67,6 +70,10 @@ class TestController < ActionController::Base
67
70
  @customers = [ Customer.new("david"), Customer.new("mary") ]
68
71
  render_text "How's there? #{render_to_string("test/list")}"
69
72
  end
73
+
74
+ def accessing_params_in_template
75
+ render_template "Hello: <%= params[:name] %>"
76
+ end
70
77
 
71
78
  def rescue_action(e) raise end
72
79
 
@@ -82,20 +89,6 @@ end
82
89
  TestController.template_root = File.dirname(__FILE__) + "/../fixtures/"
83
90
  Fun::GamesController.template_root = File.dirname(__FILE__) + "/../fixtures/"
84
91
 
85
- class TestLayoutController < ActionController::Base
86
- layout "layouts/standard"
87
-
88
- def hello_world
89
- end
90
-
91
- def hello_world_outside_layout
92
- end
93
-
94
- def rescue_action(e)
95
- raise unless ActionController::MissingTemplate === e
96
- end
97
- end
98
-
99
92
  class RenderTest < Test::Unit::TestCase
100
93
  def setup
101
94
  @request = ActionController::TestRequest.new
@@ -180,10 +173,10 @@ class RenderTest < Test::Unit::TestCase
180
173
  assert_equal "<wrapper>\n<html>\n <p>Hello </p>\n<p>This is grand!</p>\n</html>\n</wrapper>\n", process_request.body
181
174
  end
182
175
 
183
- def test_partials_list
184
- @request.action = "partials_list"
185
- assert_equal "Hello: davidHello: mary", process_request.body
186
- end
176
+ # def test_partials_list
177
+ # @request.action = "partials_list"
178
+ # assert_equal "goodbyeHello: davidHello: marygoodbye\n", process_request.body
179
+ # end
187
180
 
188
181
  def test_partial_only
189
182
  @request.action = "partial_only"
@@ -192,7 +185,7 @@ class RenderTest < Test::Unit::TestCase
192
185
 
193
186
  def test_render_to_string
194
187
  @request.action = "hello_in_a_string"
195
- assert_equal "How's there? Hello: davidHello: mary", process_request.body
188
+ assert_equal "How's there? goodbyeHello: davidHello: marygoodbye\n", process_request.body
196
189
  end
197
190
 
198
191
  def test_nested_rendering
@@ -200,8 +193,14 @@ class RenderTest < Test::Unit::TestCase
200
193
  assert_equal "Living in a nested world", Fun::GamesController.process(@request, @response).body
201
194
  end
202
195
 
196
+ def test_accessing_params_in_template
197
+ @request.action = "accessing_params_in_template"
198
+ @request.query_parameters[:name] = "David"
199
+ assert_equal "Hello: David", process_request.body
200
+ end
201
+
203
202
  private
204
203
  def process_request
205
204
  TestController.process(@request, @response)
206
205
  end
207
- end
206
+ end
@@ -5,6 +5,39 @@ class RequestTest < Test::Unit::TestCase
5
5
  @request = ActionController::TestRequest.new
6
6
  end
7
7
 
8
+ def test_remote_ip
9
+ assert_equal '0.0.0.0', @request.remote_ip
10
+
11
+ @request.remote_addr = '1.2.3.4'
12
+ assert_equal '1.2.3.4', @request.remote_ip
13
+
14
+ @request.env['HTTP_CLIENT_IP'] = '2.3.4.5'
15
+ assert_equal '2.3.4.5', @request.remote_ip
16
+ @request.env.delete 'HTTP_CLIENT_IP'
17
+
18
+ @request.env['HTTP_X_FORWARDED_FOR'] = '3.4.5.6'
19
+ assert_equal '3.4.5.6', @request.remote_ip
20
+
21
+ @request.env['HTTP_X_FORWARDED_FOR'] = 'unknown,3.4.5.6'
22
+ assert_equal '3.4.5.6', @request.remote_ip
23
+
24
+ @request.env['HTTP_X_FORWARDED_FOR'] = '172.16.0.1,3.4.5.6'
25
+ assert_equal '3.4.5.6', @request.remote_ip
26
+
27
+ @request.env['HTTP_X_FORWARDED_FOR'] = '192.168.0.1,3.4.5.6'
28
+ assert_equal '3.4.5.6', @request.remote_ip
29
+
30
+ @request.env['HTTP_X_FORWARDED_FOR'] = '10.0.0.1,3.4.5.6'
31
+ assert_equal '3.4.5.6', @request.remote_ip
32
+
33
+ @request.env['HTTP_X_FORWARDED_FOR'] = '127.0.0.1,3.4.5.6'
34
+ assert_equal '127.0.0.1', @request.remote_ip
35
+
36
+ @request.env['HTTP_X_FORWARDED_FOR'] = 'unknown,192.168.0.1'
37
+ assert_equal '1.2.3.4', @request.remote_ip
38
+ @request.env.delete 'HTTP_X_FORWARDED_FOR'
39
+ end
40
+
8
41
  def test_domains
9
42
  @request.host = "www.rubyonrails.org"
10
43
  assert_equal "rubyonrails.org", @request.domain
@@ -32,7 +65,12 @@ class RequestTest < Test::Unit::TestCase
32
65
  assert_equal ":8080", @request.port_string
33
66
  end
34
67
 
35
- def test_relative_url_root
68
+ def test_relative_url_root
69
+ @request.env['SERVER_SOFTWARE'] = 'lighttpd/1.2.3'
70
+ assert_nil @request.relative_url_root, "relative_url_root should be disabled on lighttpd"
71
+
72
+ @request.env['SERVER_SOFTWARE'] = 'apache/1.2.3 some random text'
73
+
36
74
  @request.env['SCRIPT_NAME'] = nil
37
75
  assert_equal "", @request.relative_url_root
38
76
 
@@ -42,92 +80,110 @@ class RequestTest < Test::Unit::TestCase
42
80
  @request.env['SCRIPT_NAME'] = "/myapp.rb"
43
81
  assert_equal "", @request.relative_url_root
44
82
 
83
+ @request.relative_url_root = nil
45
84
  @request.env['SCRIPT_NAME'] = "/hieraki/dispatch.cgi"
46
85
  assert_equal "/hieraki", @request.relative_url_root
47
86
 
87
+ @request.relative_url_root = nil
48
88
  @request.env['SCRIPT_NAME'] = "/collaboration/hieraki/dispatch.cgi"
49
89
  assert_equal "/collaboration/hieraki", @request.relative_url_root
50
90
  end
51
91
 
52
92
  def test_request_uri
93
+ @request.env['SERVER_SOFTWARE'] = 'Apache 42.342.3432'
94
+
95
+ @request.relative_url_root = nil
53
96
  @request.set_REQUEST_URI "http://www.rubyonrails.org/path/of/some/uri?mapped=1"
54
97
  assert_equal "/path/of/some/uri?mapped=1", @request.request_uri
55
98
  assert_equal "/path/of/some/uri", @request.path
56
99
 
100
+ @request.relative_url_root = nil
57
101
  @request.set_REQUEST_URI "http://www.rubyonrails.org/path/of/some/uri"
58
102
  assert_equal "/path/of/some/uri", @request.request_uri
59
103
  assert_equal "/path/of/some/uri", @request.path
60
104
 
105
+ @request.relative_url_root = nil
61
106
  @request.set_REQUEST_URI "/path/of/some/uri"
62
107
  assert_equal "/path/of/some/uri", @request.request_uri
63
108
  assert_equal "/path/of/some/uri", @request.path
64
109
 
110
+ @request.relative_url_root = nil
65
111
  @request.set_REQUEST_URI "/"
66
112
  assert_equal "/", @request.request_uri
67
113
  assert_equal "/", @request.path
68
114
 
115
+ @request.relative_url_root = nil
69
116
  @request.set_REQUEST_URI "/?m=b"
70
117
  assert_equal "/?m=b", @request.request_uri
71
118
  assert_equal "/", @request.path
72
119
 
120
+ @request.relative_url_root = nil
73
121
  @request.set_REQUEST_URI "/"
74
122
  @request.env['SCRIPT_NAME'] = "/dispatch.cgi"
75
123
  assert_equal "/", @request.request_uri
76
124
  assert_equal "/", @request.path
77
125
 
126
+ @request.relative_url_root = nil
78
127
  @request.set_REQUEST_URI "/hieraki/"
79
128
  @request.env['SCRIPT_NAME'] = "/hieraki/dispatch.cgi"
80
129
  assert_equal "/hieraki/", @request.request_uri
81
130
  assert_equal "/", @request.path
82
131
 
132
+ @request.relative_url_root = nil
83
133
  @request.set_REQUEST_URI "/collaboration/hieraki/books/edit/2"
84
134
  @request.env['SCRIPT_NAME'] = "/collaboration/hieraki/dispatch.cgi"
85
135
  assert_equal "/collaboration/hieraki/books/edit/2", @request.request_uri
86
136
  assert_equal "/books/edit/2", @request.path
87
137
 
88
138
  # The following tests are for when REQUEST_URI is not supplied (as in IIS)
139
+ @request.relative_url_root = nil
89
140
  @request.set_REQUEST_URI nil
90
141
  @request.env['PATH_INFO'] = "/path/of/some/uri?mapped=1"
91
142
  @request.env['SCRIPT_NAME'] = nil #"/path/dispatch.rb"
92
143
  assert_equal "/path/of/some/uri?mapped=1", @request.request_uri
93
144
  assert_equal "/path/of/some/uri", @request.path
94
145
 
146
+ @request.relative_url_root = nil
95
147
  @request.env['PATH_INFO'] = "/path/of/some/uri?mapped=1"
96
148
  @request.env['SCRIPT_NAME'] = "/path/dispatch.rb"
97
149
  assert_equal "/path/of/some/uri?mapped=1", @request.request_uri
98
150
  assert_equal "/of/some/uri", @request.path
99
151
 
152
+ @request.relative_url_root = nil
100
153
  @request.env['PATH_INFO'] = "/path/of/some/uri"
101
154
  @request.env['SCRIPT_NAME'] = nil
102
155
  assert_equal "/path/of/some/uri", @request.request_uri
103
156
  assert_equal "/path/of/some/uri", @request.path
104
157
 
158
+ @request.relative_url_root = nil
105
159
  @request.env['PATH_INFO'] = "/"
106
160
  assert_equal "/", @request.request_uri
107
161
  assert_equal "/", @request.path
108
162
 
163
+ @request.relative_url_root = nil
109
164
  @request.env['PATH_INFO'] = "/?m=b"
110
165
  assert_equal "/?m=b", @request.request_uri
111
166
  assert_equal "/", @request.path
112
167
 
168
+ @request.relative_url_root = nil
113
169
  @request.env['PATH_INFO'] = "/"
114
170
  @request.env['SCRIPT_NAME'] = "/dispatch.cgi"
115
171
  assert_equal "/", @request.request_uri
116
172
  assert_equal "/", @request.path
117
173
 
174
+ @request.relative_url_root = nil
118
175
  @request.env['PATH_INFO'] = "/hieraki/"
119
176
  @request.env['SCRIPT_NAME'] = "/hieraki/dispatch.cgi"
120
177
  assert_equal "/hieraki/", @request.request_uri
121
178
  assert_equal "/", @request.path
122
179
 
123
180
  # This test ensures that Rails uses REQUEST_URI over PATH_INFO
181
+ @request.relative_url_root = nil
124
182
  @request.env['REQUEST_URI'] = "/some/path"
125
183
  @request.env['PATH_INFO'] = "/another/path"
126
184
  @request.env['SCRIPT_NAME'] = "/dispatch.cgi"
127
185
  assert_equal "/some/path", @request.request_uri
128
186
  assert_equal "/some/path", @request.path
129
-
130
-
131
187
  end
132
188
 
133
189
 
@@ -144,4 +200,28 @@ class RequestTest < Test::Unit::TestCase
144
200
  @request.port = 81
145
201
  assert_equal "rubyonrails.org:81", @request.host_with_port
146
202
  end
203
+
204
+ def test_server_software
205
+ assert_equal nil, @request.server_software
206
+
207
+ @request.env['SERVER_SOFTWARE'] = 'Apache3.422'
208
+ assert_equal 'apache', @request.server_software
209
+
210
+ @request.env['SERVER_SOFTWARE'] = 'lighttpd(1.1.4)'
211
+ assert_equal 'lighttpd', @request.server_software
212
+ end
213
+
214
+ def test_xml_http_request
215
+ assert !@request.xml_http_request?
216
+ assert !@request.xhr?
217
+
218
+ @request.env['HTTP_X_REQUESTED_WITH'] = "DefinitelyNotAjax1.0"
219
+ assert !@request.xml_http_request?
220
+ assert !@request.xhr?
221
+
222
+ @request.env['HTTP_X_REQUESTED_WITH'] = "XMLHttpRequest"
223
+ assert @request.xml_http_request?
224
+ assert @request.xhr?
225
+ end
226
+
147
227
  end
@@ -0,0 +1,702 @@
1
+ require File.dirname(__FILE__) + '/../abstract_unit'
2
+ require 'test/unit'
3
+ require 'stringio'
4
+
5
+ RunTimeTests = ARGV.include? 'time'
6
+
7
+ module ActionController::CodeGeneration
8
+
9
+ class SourceTests < Test::Unit::TestCase
10
+ attr_accessor :source
11
+ def setup
12
+ @source = Source.new
13
+ end
14
+
15
+ def test_initial_state
16
+ assert_equal [], source.lines
17
+ assert_equal 0, source.indentation_level
18
+ end
19
+
20
+ def test_trivial_operations
21
+ source << "puts 'Hello World'"
22
+ assert_equal ["puts 'Hello World'"], source.lines
23
+ assert_equal "puts 'Hello World'", source.to_s
24
+
25
+ source.line "puts 'Goodbye World'"
26
+ assert_equal ["puts 'Hello World'", "puts 'Goodbye World'"], source.lines
27
+ assert_equal "puts 'Hello World'\nputs 'Goodbye World'", source.to_s
28
+ end
29
+
30
+ def test_indentation
31
+ source << "x = gets.to_i"
32
+ source << 'if x.odd?'
33
+ source.indent { source << "puts 'x is odd!'" }
34
+ source << 'else'
35
+ source.indent { source << "puts 'x is even!'" }
36
+ source << 'end'
37
+
38
+ assert_equal ["x = gets.to_i", "if x.odd?", " puts 'x is odd!'", 'else', " puts 'x is even!'", 'end'], source.lines
39
+
40
+ text = "x = gets.to_i
41
+ if x.odd?
42
+ puts 'x is odd!'
43
+ else
44
+ puts 'x is even!'
45
+ end"
46
+
47
+ assert_equal text, source.to_s
48
+ end
49
+ end
50
+
51
+ class CodeGeneratorTests < Test::Unit::TestCase
52
+ attr_accessor :generator
53
+ def setup
54
+ @generator = CodeGenerator.new
55
+ end
56
+
57
+ def test_initial_state
58
+ assert_equal [], generator.source.lines
59
+ assert_equal [], generator.locals
60
+ end
61
+
62
+ def test_trivial_operations
63
+ ["puts 'Hello World'", "puts 'Goodbye World'"].each {|l| generator << l}
64
+ assert_equal ["puts 'Hello World'", "puts 'Goodbye World'"], generator.source.lines
65
+ assert_equal "puts 'Hello World'\nputs 'Goodbye World'", generator.to_s
66
+ end
67
+
68
+ def test_if
69
+ generator << "x = gets.to_i"
70
+ generator.if("x.odd?") { generator << "puts 'x is odd!'" }
71
+
72
+ assert_equal "x = gets.to_i\nif x.odd?\n puts 'x is odd!'\nend", generator.to_s
73
+ end
74
+
75
+ def test_else
76
+ test_if
77
+ generator.else { generator << "puts 'x is even!'" }
78
+
79
+ 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
+ def test_dup
83
+ generator << 'x = 2'
84
+ generator.locals << :x
85
+
86
+ g = generator.dup
87
+ assert_equal generator.source, g.source
88
+ assert_equal generator.locals, g.locals
89
+
90
+ g << 'y = 3'
91
+ g.locals << :y
92
+ assert_equal [:x, :y], g.locals # Make sure they don't share the same array.
93
+ assert_equal [:x], generator.locals
94
+ end
95
+ end
96
+
97
+ # XXX Extract to test/controller/fake_controllers.rb
98
+ module Object::Controllers
99
+ def self.const_available?(*args)
100
+ const_defined?(*args)
101
+ end
102
+
103
+ class ContentController
104
+ end
105
+ module Admin
106
+ def self.const_available?(*args)
107
+ const_defined?(*args)
108
+ end
109
+
110
+ class UserController
111
+ end
112
+ end
113
+ end
114
+
115
+
116
+ class RecognitionTests < Test::Unit::TestCase
117
+ attr_accessor :generator
118
+ alias :g :generator
119
+ def setup
120
+ @generator = RecognitionGenerator.new
121
+ end
122
+
123
+ def go(components)
124
+ g.current = components.first
125
+ g.after = components[1..-1] || []
126
+ g.go
127
+ end
128
+
129
+ def execute(path, show = false)
130
+ path = path.split('/') if path.is_a? String
131
+ source = "index, path = 0, #{path.inspect}\n#{g.to_s}"
132
+ puts source if show
133
+ r = eval source
134
+ r ? r.symbolize_keys : nil
135
+ end
136
+
137
+ Static = ::ActionController::Routing::StaticComponent
138
+ Dynamic = ::ActionController::Routing::DynamicComponent
139
+ Path = ::ActionController::Routing::PathComponent
140
+ Controller = ::ActionController::Routing::ControllerComponent
141
+
142
+ def test_all_static
143
+ c = %w(hello world how are you).collect {|str| Static.new(str)}
144
+
145
+ g.result :controller, "::Controllers::ContentController", true
146
+ g.constant_result :action, 'index'
147
+
148
+ go c
149
+
150
+ assert_nil execute('x')
151
+ assert_nil execute('hello/world/how')
152
+ assert_nil execute('hello/world/how/are')
153
+ assert_nil execute('hello/world/how/are/you/today')
154
+ assert_equal({:controller => ::Controllers::ContentController, :action => 'index'}, execute('hello/world/how/are/you'))
155
+ end
156
+
157
+ def test_basic_dynamic
158
+ c = [Static.new("hi"), Dynamic.new(:action)]
159
+ g.result :controller, "::Controllers::ContentController", true
160
+ go c
161
+
162
+ assert_nil execute('boo')
163
+ assert_nil execute('boo/blah')
164
+ assert_nil execute('hi')
165
+ assert_nil execute('hi/dude/what')
166
+ assert_equal({:controller => ::Controllers::ContentController, :action => 'dude'}, execute('hi/dude'))
167
+ end
168
+
169
+ def test_dynamic_with_default
170
+ c = [Static.new("hi"), Dynamic.new(:action, :default => 'index')]
171
+ g.result :controller, "::Controllers::ContentController", true
172
+ go c
173
+
174
+ assert_nil execute('boo')
175
+ assert_nil execute('boo/blah')
176
+ assert_nil execute('hi/dude/what')
177
+ assert_equal({:controller => ::Controllers::ContentController, :action => 'index'}, execute('hi'))
178
+ assert_equal({:controller => ::Controllers::ContentController, :action => 'index'}, execute('hi/index'))
179
+ assert_equal({:controller => ::Controllers::ContentController, :action => 'dude'}, execute('hi/dude'))
180
+ end
181
+
182
+ def test_dynamic_with_string_condition
183
+ c = [Static.new("hi"), Dynamic.new(:action, :condition => 'index')]
184
+ g.result :controller, "::Controllers::ContentController", true
185
+ go c
186
+
187
+ assert_nil execute('boo')
188
+ assert_nil execute('boo/blah')
189
+ assert_nil execute('hi')
190
+ assert_nil execute('hi/dude/what')
191
+ assert_equal({:controller => ::Controllers::ContentController, :action => 'index'}, execute('hi/index'))
192
+ assert_nil execute('hi/dude')
193
+ end
194
+
195
+ def test_dynamic_with_regexp_condition
196
+ c = [Static.new("hi"), Dynamic.new(:action, :condition => /^[a-z]+$/)]
197
+ g.result :controller, "::Controllers::ContentController", true
198
+ go c
199
+
200
+ assert_nil execute('boo')
201
+ assert_nil execute('boo/blah')
202
+ assert_nil execute('hi')
203
+ assert_nil execute('hi/FOXY')
204
+ assert_nil execute('hi/138708jkhdf')
205
+ assert_nil execute('hi/dkjfl8792343dfsf')
206
+ assert_nil execute('hi/dude/what')
207
+ assert_equal({:controller => ::Controllers::ContentController, :action => 'index'}, execute('hi/index'))
208
+ assert_equal({:controller => ::Controllers::ContentController, :action => 'dude'}, execute('hi/dude'))
209
+ end
210
+
211
+ def test_dynamic_with_regexp_and_default
212
+ c = [Static.new("hi"), Dynamic.new(:action, :condition => /^[a-z]+$/, :default => 'index')]
213
+ g.result :controller, "::Controllers::ContentController", true
214
+ go c
215
+
216
+ assert_nil execute('boo')
217
+ assert_nil execute('boo/blah')
218
+ assert_nil execute('hi/FOXY')
219
+ assert_nil execute('hi/138708jkhdf')
220
+ assert_nil execute('hi/dkjfl8792343dfsf')
221
+ assert_equal({:controller => ::Controllers::ContentController, :action => 'index'}, execute('hi'))
222
+ assert_equal({:controller => ::Controllers::ContentController, :action => 'index'}, execute('hi/index'))
223
+ assert_equal({:controller => ::Controllers::ContentController, :action => 'dude'}, execute('hi/dude'))
224
+ assert_nil execute('hi/dude/what')
225
+ end
226
+
227
+ def test_path
228
+ c = [Static.new("hi"), Path.new(:file)]
229
+ g.result :controller, "::Controllers::ContentController", true
230
+ g.constant_result :action, "download"
231
+
232
+ go c
233
+
234
+ assert_nil execute('boo')
235
+ assert_nil execute('boo/blah')
236
+ assert_equal({:controller => ::Controllers::ContentController, :action => 'download', :file => []}, execute('hi'))
237
+ assert_equal({:controller => ::Controllers::ContentController, :action => 'download', :file => %w(books agile_rails_dev.pdf)},
238
+ execute('hi/books/agile_rails_dev.pdf'))
239
+ assert_equal({:controller => ::Controllers::ContentController, :action => 'download', :file => ['dude']}, execute('hi/dude'))
240
+ assert_equal 'dude/what', execute('hi/dude/what')[:file].to_s
241
+ end
242
+
243
+ def test_controller
244
+ c = [Static.new("hi"), Controller.new(:controller)]
245
+ g.constant_result :action, "hi"
246
+
247
+ go c
248
+
249
+ assert_nil execute('boo')
250
+ assert_nil execute('boo/blah')
251
+ assert_nil execute('hi/x')
252
+ assert_nil execute('hi/13870948')
253
+ assert_nil execute('hi/content/dog')
254
+ assert_nil execute('hi/admin/user/foo')
255
+ assert_equal({:controller => ::Controllers::ContentController, :action => 'hi'}, execute('hi/content'))
256
+ assert_equal({:controller => ::Controllers::Admin::UserController, :action => 'hi'}, execute('hi/admin/user'))
257
+ end
258
+
259
+ def test_standard_route(time = ::RunTimeTests)
260
+ c = [Controller.new(:controller), Dynamic.new(:action, :default => 'index'), Dynamic.new(:id, :default => nil)]
261
+ go c
262
+
263
+ # Make sure we get the right answers
264
+ assert_equal({:controller => ::Controllers::ContentController, :action => 'index'}, execute('content'))
265
+ assert_equal({:controller => ::Controllers::ContentController, :action => 'list'}, execute('content/list'))
266
+ assert_equal({:controller => ::Controllers::ContentController, :action => 'show', :id => '10'}, execute('content/show/10'))
267
+
268
+ assert_equal({:controller => ::Controllers::Admin::UserController, :action => 'index'}, execute('admin/user'))
269
+ assert_equal({:controller => ::Controllers::Admin::UserController, :action => 'list'}, execute('admin/user/list'))
270
+ assert_equal({:controller => ::Controllers::Admin::UserController, :action => 'show', :id => 'nseckar'}, execute('admin/user/show/nseckar'))
271
+
272
+ assert_nil execute('content/show/10/20')
273
+ assert_nil execute('food')
274
+
275
+ if time
276
+ source = "def self.execute(path)
277
+ path = path.split('/') if path.is_a? String
278
+ index = 0
279
+ r = #{g.to_s}
280
+ end"
281
+ eval(source)
282
+
283
+ GC.start
284
+ n = 1000
285
+ time = Benchmark.realtime do n.times {
286
+ execute('content')
287
+ execute('content/list')
288
+ execute('content/show/10')
289
+
290
+ execute('admin/user')
291
+ execute('admin/user/list')
292
+ execute('admin/user/show/nseckar')
293
+
294
+ execute('admin/user/show/nseckar/dude')
295
+ execute('admin/why/show/nseckar')
296
+ execute('content/show/10/20')
297
+ execute('food')
298
+ } end
299
+ time -= Benchmark.realtime do n.times { } end
300
+
301
+
302
+ puts "\n\nRecognition:"
303
+ per_url = time / (n * 10)
304
+
305
+ puts "#{per_url * 1000} ms/url"
306
+ puts "#{1 / per_url} urls/s\n\n"
307
+ end
308
+ end
309
+
310
+ def test_default_route
311
+ g.result :controller, "::Controllers::ContentController", true
312
+ g.constant_result :action, 'index'
313
+
314
+ go []
315
+
316
+ assert_nil execute('x')
317
+ assert_nil execute('hello/world/how')
318
+ assert_nil execute('hello/world/how/are')
319
+ assert_nil execute('hello/world/how/are/you/today')
320
+ assert_equal({:controller => ::Controllers::ContentController, :action => 'index'}, execute([]))
321
+ end
322
+ end
323
+
324
+ class GenerationTests < Test::Unit::TestCase
325
+ attr_accessor :generator
326
+ alias :g :generator
327
+ def setup
328
+ @generator = GenerationGenerator.new # ha!
329
+ end
330
+
331
+ def go(components)
332
+ g.current = components.first
333
+ g.after = components[1..-1] || []
334
+ g.go
335
+ end
336
+
337
+ def execute(options, recall, show = false)
338
+ source = "\n
339
+ expire_on = ::ActionController::Routing.expiry_hash(options, recall)
340
+ hash = merged = recall.merge(options)
341
+ not_expired = true
342
+
343
+ #{g.to_s}\n\n"
344
+ puts source if show
345
+ eval(source)
346
+ end
347
+
348
+ Static = ::ActionController::Routing::StaticComponent
349
+ Dynamic = ::ActionController::Routing::DynamicComponent
350
+ Path = ::ActionController::Routing::PathComponent
351
+ Controller = ::ActionController::Routing::ControllerComponent
352
+
353
+ def test_all_static_no_requirements
354
+ c = [Static.new("hello"), Static.new("world")]
355
+ go c
356
+
357
+ assert_equal "/hello/world", execute({}, {})
358
+ end
359
+
360
+ def test_basic_dynamic
361
+ c = [Static.new("hi"), Dynamic.new(:action)]
362
+ go c
363
+
364
+ assert_equal '/hi/index', execute({:action => 'index'}, {:action => 'index'})
365
+ assert_equal '/hi/show', execute({:action => 'show'}, {:action => 'index'})
366
+ assert_equal '/hi/list+people', execute({}, {:action => 'list people'})
367
+ assert_nil execute({},{})
368
+ end
369
+
370
+ def test_dynamic_with_default
371
+ c = [Static.new("hi"), Dynamic.new(:action, :default => 'index')]
372
+ go c
373
+
374
+ assert_equal '/hi', execute({:action => 'index'}, {:action => 'index'})
375
+ assert_equal '/hi/show', execute({:action => 'show'}, {:action => 'index'})
376
+ assert_equal '/hi/list+people', execute({}, {:action => 'list people'})
377
+ assert_equal '/hi', execute({}, {})
378
+ end
379
+
380
+ def test_dynamic_with_regexp_condition
381
+ c = [Static.new("hi"), Dynamic.new(:action, :condition => /^[a-z]+$/)]
382
+ go c
383
+
384
+ assert_equal '/hi/index', execute({:action => 'index'}, {:action => 'index'})
385
+ assert_nil execute({:action => 'fox5'}, {:action => 'index'})
386
+ assert_nil execute({:action => 'something_is_up'}, {:action => 'index'})
387
+ assert_nil execute({}, {:action => 'list people'})
388
+ assert_equal '/hi/abunchofcharacter', execute({:action => 'abunchofcharacter'}, {})
389
+ assert_nil execute({}, {})
390
+ end
391
+
392
+ def test_dynamic_with_default_and_regexp_condition
393
+ c = [Static.new("hi"), Dynamic.new(:action, :default => 'index', :condition => /^[a-z]+$/)]
394
+ go c
395
+
396
+ assert_equal '/hi', execute({:action => 'index'}, {:action => 'index'})
397
+ assert_nil execute({:action => 'fox5'}, {:action => 'index'})
398
+ assert_nil execute({:action => 'something_is_up'}, {:action => 'index'})
399
+ assert_nil execute({}, {:action => 'list people'})
400
+ assert_equal '/hi/abunchofcharacter', execute({:action => 'abunchofcharacter'}, {})
401
+ assert_equal '/hi', execute({}, {})
402
+ end
403
+
404
+ def test_path
405
+ c = [Static.new("hi"), Path.new(:file)]
406
+ go c
407
+
408
+ assert_equal '/hi', execute({:file => []}, {})
409
+ assert_equal '/hi/books/agile_rails_dev.pdf', execute({:file => %w(books agile_rails_dev.pdf)}, {})
410
+ assert_equal '/hi/books/development%26whatever/agile_rails_dev.pdf', execute({:file => %w(books development&whatever agile_rails_dev.pdf)}, {})
411
+
412
+ assert_equal '/hi', execute({:file => ''}, {})
413
+ assert_equal '/hi/books/agile_rails_dev.pdf', execute({:file => 'books/agile_rails_dev.pdf'}, {})
414
+ assert_equal '/hi/books/development%26whatever/agile_rails_dev.pdf', execute({:file => 'books/development&whatever/agile_rails_dev.pdf'}, {})
415
+ end
416
+
417
+ def test_controller
418
+ c = [Static.new("hi"), Controller.new(:controller)]
419
+ go c
420
+
421
+ assert_nil execute({}, {})
422
+ assert_equal '/hi/content', execute({:controller => 'content'}, {})
423
+ assert_equal '/hi/admin/user', execute({:controller => 'admin/user'}, {})
424
+ assert_equal '/hi/content', execute({}, {:controller => 'content'})
425
+ assert_equal '/hi/admin/user', execute({}, {:controller => 'admin/user'})
426
+ end
427
+
428
+ def test_standard_route(time = ::RunTimeTests)
429
+ c = [Controller.new(:controller), Dynamic.new(:action, :default => 'index'), Dynamic.new(:id, :default => nil)]
430
+ go c
431
+
432
+ # Make sure we get the right answers
433
+ assert_equal('/content', execute({:action => 'index'}, {:controller => 'content', :action => 'list'}))
434
+ assert_equal('/content/list', execute({:action => 'list'}, {:controller => 'content', :action => 'index'}))
435
+ assert_equal('/content/show/10', execute({:action => 'show', :id => '10'}, {:controller => 'content', :action => 'list'}))
436
+
437
+ assert_equal('/admin/user', execute({:action => 'index'}, {:controller => 'admin/user', :action => 'list'}))
438
+ assert_equal('/admin/user/list', execute({:action => 'list'}, {:controller => 'admin/user', :action => 'index'}))
439
+ assert_equal('/admin/user/show/10', execute({:action => 'show', :id => '10'}, {:controller => 'admin/user', :action => 'list'}))
440
+
441
+ if time
442
+ GC.start
443
+ n = 1000
444
+ time = Benchmark.realtime do n.times {
445
+ execute({:action => 'index'}, {:controller => 'content', :action => 'list'})
446
+ execute({:action => 'list'}, {:controller => 'content', :action => 'index'})
447
+ execute({:action => 'show', :id => '10'}, {:controller => 'content', :action => 'list'})
448
+
449
+ execute({:action => 'index'}, {:controller => 'admin/user', :action => 'list'})
450
+ execute({:action => 'list'}, {:controller => 'admin/user', :action => 'index'})
451
+ execute({:action => 'show', :id => '10'}, {:controller => 'admin/user', :action => 'list'})
452
+ } end
453
+ time -= Benchmark.realtime do n.times { } end
454
+
455
+ puts "\n\nGeneration:"
456
+ per_url = time / (n * 6)
457
+
458
+ puts "#{per_url * 1000} ms/url"
459
+ puts "#{1 / per_url} urls/s\n\n"
460
+ end
461
+ end
462
+
463
+ def test_default_route
464
+ g.if(g.check_conditions(:controller => 'content', :action => 'welcome')) { go [] }
465
+
466
+ assert_nil execute({:controller => 'foo', :action => 'welcome'}, {})
467
+ assert_nil execute({:controller => 'content', :action => 'elcome'}, {})
468
+ assert_nil execute({:action => 'elcome'}, {:controller => 'content'})
469
+
470
+ assert_equal '/', execute({:controller => 'content', :action => 'welcome'}, {})
471
+ assert_equal '/', execute({:action => 'welcome'}, {:controller => 'content'})
472
+ assert_equal '/', execute({:action => 'welcome', :id => '10'}, {:controller => 'content'})
473
+ end
474
+ end
475
+
476
+ class RouteTests < Test::Unit::TestCase
477
+ def route(*args)
478
+ @route = ::ActionController::Routing::Route.new(*args) unless args.empty?
479
+ return @route
480
+ end
481
+
482
+ def rec(path, show = false)
483
+ path = path.split('/') if path.is_a? String
484
+ index = 0
485
+ source = route.write_recognition.to_s
486
+ puts "\n\n#{source}\n\n" if show
487
+ r = eval(source)
488
+ r ? r.symbolize_keys : r
489
+ end
490
+ def gen(options, recall = nil, show = false)
491
+ recall ||= options.dup
492
+
493
+ expire_on = ::ActionController::Routing.expiry_hash(options, recall)
494
+ hash = merged = recall.merge(options)
495
+ not_expired = true
496
+
497
+ source = route.write_generation.to_s
498
+ puts "\n\n#{source}\n\n" if show
499
+ eval(source)
500
+
501
+ end
502
+
503
+ def test_static
504
+ route 'hello/world', :known => 'known_value'
505
+
506
+ assert_nil rec('hello/turn')
507
+ assert_nil rec('turn/world')
508
+ assert_equal({:known => 'known_value'}, rec('hello/world'))
509
+
510
+ assert_nil gen(:known => 'foo')
511
+ assert_nil gen({})
512
+ assert_equal '/hello/world', gen(:known => 'known_value')
513
+ assert_equal '/hello/world', gen(:known => 'known_value', :extra => 'hi')
514
+ assert_equal [:extra], route.extra_keys(:known => 'known_value', :extra => 'hi')
515
+ end
516
+
517
+ def test_dynamic
518
+ route 'hello/:name', :controller => 'content', :action => 'show_person'
519
+
520
+ assert_nil rec('hello')
521
+ assert_nil rec('foo/bar')
522
+ assert_equal({:controller => ::Controllers::ContentController, :action => 'show_person', :name => 'rails'}, rec('hello/rails'))
523
+ assert_equal({:controller => ::Controllers::ContentController, :action => 'show_person', :name => 'Nicholas Seckar'}, rec('hello/Nicholas+Seckar'))
524
+
525
+ assert_nil gen(:controller => 'content', :action => 'show_dude', :name => 'rails')
526
+ assert_nil gen(:controller => 'content', :action => 'show_person')
527
+ assert_nil gen(:controller => 'admin/user', :action => 'show_person', :name => 'rails')
528
+ assert_equal '/hello/rails', gen(:controller => 'content', :action => 'show_person', :name => 'rails')
529
+ assert_equal '/hello/Nicholas+Seckar', gen(:controller => 'content', :action => 'show_person', :name => 'Nicholas Seckar')
530
+ end
531
+
532
+ def test_typical
533
+ route ':controller/:action/:id', :action => 'index', :id => nil
534
+ assert_nil rec('hello')
535
+ assert_nil rec('foo bar')
536
+ assert_equal({:controller => ::Controllers::ContentController, :action => 'index'}, rec('content'))
537
+ assert_equal({:controller => ::Controllers::Admin::UserController, :action => 'index'}, rec('admin/user'))
538
+
539
+ assert_equal({:controller => ::Controllers::Admin::UserController, :action => 'index'}, rec('admin/user/index'))
540
+ assert_equal({:controller => ::Controllers::Admin::UserController, :action => 'list'}, rec('admin/user/list'))
541
+ assert_equal({:controller => ::Controllers::Admin::UserController, :action => 'show', :id => '10'}, rec('admin/user/show/10'))
542
+
543
+ assert_equal({:controller => ::Controllers::ContentController, :action => 'list'}, rec('content/list'))
544
+ assert_equal({:controller => ::Controllers::ContentController, :action => 'show', :id => '10'}, rec('content/show/10'))
545
+
546
+
547
+ assert_equal '/content', gen(:controller => 'content', :action => 'index')
548
+ assert_equal '/content/list', gen(:controller => 'content', :action => 'list')
549
+ assert_equal '/content/show/10', gen(:controller => 'content', :action => 'show', :id => '10')
550
+
551
+ assert_equal '/admin/user', gen(:controller => 'admin/user', :action => 'index')
552
+ assert_equal '/admin/user', gen(:controller => 'admin/user')
553
+ assert_equal '/admin/user', gen({:controller => 'admin/user'}, {:controller => 'content', :action => 'list', :id => '10'})
554
+ assert_equal '/admin/user/show/10', gen(:controller => 'admin/user', :action => 'show', :id => '10')
555
+ end
556
+ end
557
+
558
+ class RouteSetTests < Test::Unit::TestCase
559
+ attr_reader :rs
560
+ def setup
561
+ @rs = ::ActionController::Routing::RouteSet.new
562
+ @rs.draw {|m| m.connect ':controller/:action/:id' }
563
+ end
564
+
565
+ def test_default_setup
566
+ assert_equal({:controller => ::Controllers::ContentController, :action => 'index'}.stringify_keys, rs.recognize_path(%w(content)))
567
+ assert_equal({:controller => ::Controllers::ContentController, :action => 'list'}.stringify_keys, rs.recognize_path(%w(content list)))
568
+ assert_equal({:controller => ::Controllers::ContentController, :action => 'show', :id => '10'}.stringify_keys, rs.recognize_path(%w(content show 10)))
569
+
570
+ assert_equal({:controller => ::Controllers::Admin::UserController, :action => 'show', :id => '10'}.stringify_keys, rs.recognize_path(%w(admin user show 10)))
571
+
572
+ assert_equal ['/admin/user/show/10', {}], rs.generate({:controller => 'admin/user', :action => 'show', :id => 10})
573
+
574
+ assert_equal ['/admin/user/show', {}], rs.generate({:action => 'show'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
575
+ assert_equal ['/admin/user/list/10', {}], rs.generate({}, {:controller => 'admin/user', :action => 'list', :id => '10'})
576
+
577
+ assert_equal ['/admin/stuff', {}], rs.generate({:controller => 'stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
578
+ assert_equal ['/stuff', {}], rs.generate({:controller => '/stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
579
+ end
580
+
581
+ def test_ignores_leading_slash
582
+ @rs.draw {|m| m.connect '/:controller/:action/:id'}
583
+ test_default_setup
584
+ end
585
+
586
+ def test_time_recognition
587
+ n = 10000
588
+ if RunTimeTests
589
+ GC.start
590
+ rectime = Benchmark.realtime do
591
+ n.times do
592
+ rs.recognize_path(%w(content))
593
+ rs.recognize_path(%w(content list))
594
+ rs.recognize_path(%w(content show 10))
595
+ rs.recognize_path(%w(admin user))
596
+ rs.recognize_path(%w(admin user list))
597
+ rs.recognize_path(%w(admin user show 10))
598
+ end
599
+ end
600
+ puts "\n\nRecognition (RouteSet):"
601
+ per_url = rectime / (n * 6)
602
+ puts "#{per_url * 1000} ms/url"
603
+ puts "#{1 / per_url} url/s\n\n"
604
+ end
605
+ end
606
+ def test_time_generation
607
+ n = 5000
608
+ if RunTimeTests
609
+ GC.start
610
+ pairs = [
611
+ [{:controller => 'content', :action => 'index'}, {:controller => 'content', :action => 'show'}],
612
+ [{:controller => 'content'}, {:controller => 'content', :action => 'index'}],
613
+ [{:controller => 'content', :action => 'list'}, {:controller => 'content', :action => 'index'}],
614
+ [{:controller => 'content', :action => 'show', :id => '10'}, {:controller => 'content', :action => 'list'}],
615
+ [{:controller => 'admin/user', :action => 'index'}, {:controller => 'admin/user', :action => 'show'}],
616
+ [{:controller => 'admin/user'}, {:controller => 'admin/user', :action => 'index'}],
617
+ [{:controller => 'admin/user', :action => 'list'}, {:controller => 'admin/user', :action => 'index'}],
618
+ [{:controller => 'admin/user', :action => 'show', :id => '10'}, {:controller => 'admin/user', :action => 'list'}],
619
+ ]
620
+ p = nil
621
+ gentime = Benchmark.realtime do
622
+ n.times do
623
+ pairs.each {|(a, b)| rs.generate(a, b)}
624
+ end
625
+ end
626
+
627
+ puts "\n\nGeneration (RouteSet): (#{(n * 8)} urls)"
628
+ per_url = gentime / (n * 8)
629
+ puts "#{per_url * 1000} ms/url"
630
+ puts "#{1 / per_url} url/s\n\n"
631
+ end
632
+ end
633
+
634
+ def test_route_with_colon_first
635
+ rs.draw do |map|
636
+ map.connect '/:controller/:action/:id', :action => 'index', :id => nil
637
+ map.connect ':url', :controller => 'tiny_url', :action => 'translate'
638
+ end
639
+ end
640
+
641
+ def test_route_generating_string_literal_in_comparison_warning
642
+ old_stderr = $stderr
643
+ $stderr = StringIO.new
644
+ rs.draw do |map|
645
+ map.connect 'subscriptions/:action/:subscription_type', :controller => "subscriptions"
646
+ end
647
+ assert_equal "", $stderr.string
648
+ ensure
649
+ $stderr = old_stderr
650
+ end
651
+
652
+ def test_basic_named_route
653
+ rs.home '', :controller => 'content', :action => 'list'
654
+ x = setup_for_named_route
655
+ assert_equal({:controller => '/content', :action => 'list'},
656
+ x.new.send(:home_url))
657
+ end
658
+
659
+ def test_named_route_with_option
660
+ rs.page 'page/:title', :controller => 'content', :action => 'show_page'
661
+ x = setup_for_named_route
662
+ assert_equal({:controller => '/content', :action => 'show_page', :title => 'new stuff'},
663
+ x.new.send(:page_url, :title => 'new stuff'))
664
+ end
665
+
666
+ def setup_for_named_route
667
+ x = Class.new
668
+ x.send(:define_method, :url_for) {|x| x}
669
+ x.send :include, ::ActionController::Routing::NamedRoutes
670
+ x
671
+ end
672
+
673
+ def test_named_route_without_hash
674
+ rs.draw do |map|
675
+ rs.normal ':controller/:action/:id'
676
+ end
677
+ end
678
+
679
+ def test_changing_controller
680
+ assert_equal ['/admin/stuff/show/10', {}], rs.generate(
681
+ {:controller => 'stuff', :action => 'show', :id => 10},
682
+ {:controller => 'admin/user', :action => 'index'}
683
+ )
684
+ end
685
+
686
+ def test_backwards
687
+ rs.draw do |map|
688
+ rs.connect 'page/:id/:action', :controller => 'pages', :action => 'show'
689
+ rs.connect ':controller/:action/:id'
690
+ end
691
+
692
+ assert_equal ['/page/20', {}], rs.generate({:id => 20}, {:controller => 'pages'})
693
+ assert_equal ['/page/20', {}], rs.generate(:controller => 'pages', :id => 20, :action => 'show')
694
+ assert_equal ['/pages/boo', {}], rs.generate(:controller => 'pages', :action => 'boo')
695
+ end
696
+
697
+ def test_action_expiry
698
+ assert_equal ['/content', {}], rs.generate({:controller => 'content'}, {:controller => 'content', :action => 'show'})
699
+ end
700
+ end
701
+
702
+ end