merb-core 0.9.3 → 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (178) hide show
  1. data/LICENSE +1 -1
  2. data/README +3 -3
  3. data/Rakefile +144 -33
  4. data/bin/merb +0 -0
  5. data/bin/merb-specs +0 -0
  6. data/docs/bootloading.dox +1 -0
  7. data/docs/merb-core-call-stack-diagram.mmap +0 -0
  8. data/docs/merb-core-call-stack-diagram.pdf +0 -0
  9. data/docs/merb-core-call-stack-diagram.png +0 -0
  10. data/lib/merb-core.rb +159 -37
  11. data/lib/merb-core/autoload.rb +1 -0
  12. data/lib/merb-core/bootloader.rb +208 -92
  13. data/lib/merb-core/config.rb +20 -6
  14. data/lib/merb-core/controller/abstract_controller.rb +113 -61
  15. data/lib/merb-core/controller/exceptions.rb +28 -13
  16. data/lib/merb-core/controller/merb_controller.rb +73 -44
  17. data/lib/merb-core/controller/mime.rb +25 -7
  18. data/lib/merb-core/controller/mixins/authentication.rb +1 -1
  19. data/lib/merb-core/controller/mixins/controller.rb +44 -8
  20. data/lib/merb-core/controller/mixins/render.rb +191 -128
  21. data/lib/merb-core/controller/mixins/responder.rb +65 -63
  22. data/lib/merb-core/controller/template.rb +103 -54
  23. data/lib/merb-core/core_ext.rb +7 -12
  24. data/lib/merb-core/core_ext/kernel.rb +128 -136
  25. data/lib/merb-core/dispatch/cookies.rb +26 -4
  26. data/lib/merb-core/dispatch/default_exception/default_exception.rb +93 -0
  27. data/lib/merb-core/dispatch/default_exception/views/_css.html.erb +198 -0
  28. data/lib/merb-core/dispatch/default_exception/views/_javascript.html.erb +73 -0
  29. data/lib/merb-core/dispatch/default_exception/views/index.html.erb +92 -0
  30. data/lib/merb-core/dispatch/dispatcher.rb +156 -224
  31. data/lib/merb-core/dispatch/request.rb +126 -25
  32. data/lib/merb-core/dispatch/router.rb +61 -6
  33. data/lib/merb-core/dispatch/router/behavior.rb +122 -41
  34. data/lib/merb-core/dispatch/router/route.rb +147 -22
  35. data/lib/merb-core/dispatch/session.rb +52 -2
  36. data/lib/merb-core/dispatch/session/cookie.rb +4 -2
  37. data/lib/merb-core/dispatch/session/memcached.rb +38 -27
  38. data/lib/merb-core/dispatch/session/memory.rb +18 -11
  39. data/lib/merb-core/dispatch/worker.rb +28 -0
  40. data/lib/merb-core/gem_ext/erubis.rb +58 -0
  41. data/lib/merb-core/logger.rb +3 -31
  42. data/lib/merb-core/plugins.rb +25 -3
  43. data/lib/merb-core/rack.rb +18 -12
  44. data/lib/merb-core/rack/adapter.rb +10 -8
  45. data/lib/merb-core/rack/adapter/ebb.rb +2 -2
  46. data/lib/merb-core/rack/adapter/irb.rb +31 -21
  47. data/lib/merb-core/rack/adapter/swiftiplied_mongrel.rb +26 -0
  48. data/lib/merb-core/rack/adapter/thin.rb +19 -9
  49. data/lib/merb-core/rack/adapter/thin_turbo.rb +24 -0
  50. data/lib/merb-core/rack/application.rb +9 -84
  51. data/lib/merb-core/rack/middleware.rb +26 -0
  52. data/lib/merb-core/rack/middleware/path_prefix.rb +31 -0
  53. data/lib/merb-core/rack/middleware/profiler.rb +19 -0
  54. data/lib/merb-core/rack/middleware/static.rb +45 -0
  55. data/lib/merb-core/server.rb +27 -9
  56. data/lib/merb-core/tasks/audit.rake +68 -0
  57. data/lib/merb-core/tasks/merb.rb +1 -0
  58. data/lib/merb-core/tasks/merb_rake_helper.rb +12 -0
  59. data/lib/merb-core/tasks/stats.rake +71 -0
  60. data/lib/merb-core/test.rb +2 -1
  61. data/lib/merb-core/test/helpers/multipart_request_helper.rb +3 -3
  62. data/lib/merb-core/test/helpers/request_helper.rb +66 -24
  63. data/lib/merb-core/test/matchers/controller_matchers.rb +36 -4
  64. data/lib/merb-core/test/matchers/route_matchers.rb +12 -3
  65. data/lib/merb-core/test/matchers/view_matchers.rb +3 -3
  66. data/lib/merb-core/test/run_specs.rb +1 -0
  67. data/lib/merb-core/test/tasks/spectasks.rb +13 -5
  68. data/lib/merb-core/test/test_ext/string.rb +14 -0
  69. data/lib/merb-core/vendor/facets/dictionary.rb +3 -3
  70. data/lib/merb-core/vendor/facets/inflect.rb +82 -37
  71. data/lib/merb-core/version.rb +2 -2
  72. data/spec/private/config/config_spec.rb +39 -4
  73. data/spec/private/core_ext/kernel_spec.rb +3 -14
  74. data/spec/private/dispatch/bootloader_spec.rb +1 -1
  75. data/spec/private/dispatch/cookies_spec.rb +181 -69
  76. data/spec/private/dispatch/fixture/app/controllers/exceptions.rb +0 -2
  77. data/spec/private/dispatch/fixture/app/controllers/foo.rb +0 -2
  78. data/spec/private/dispatch/fixture/config/rack.rb +10 -0
  79. data/spec/private/dispatch/fixture/log/merb_test.log +7054 -1802
  80. data/spec/private/dispatch/route_params_spec.rb +2 -3
  81. data/spec/private/dispatch/session_mixin_spec.rb +47 -0
  82. data/spec/private/plugins/plugin_spec.rb +73 -59
  83. data/spec/private/router/behavior_spec.rb +60 -0
  84. data/spec/private/router/fixture/log/merb_test.log +1693 -0
  85. data/spec/private/router/route_spec.rb +414 -0
  86. data/spec/private/router/router_spec.rb +175 -0
  87. data/spec/private/vendor/facets/plural_spec.rb +564 -0
  88. data/spec/private/vendor/facets/singular_spec.rb +489 -0
  89. data/spec/public/abstract_controller/controllers/cousins.rb +41 -0
  90. data/spec/public/abstract_controller/controllers/helpers.rb +12 -2
  91. data/spec/public/abstract_controller/controllers/partial.rb +17 -2
  92. data/spec/public/abstract_controller/controllers/render.rb +16 -1
  93. data/spec/public/abstract_controller/controllers/views/helpers/capture_eq/index.erb +1 -0
  94. data/spec/public/abstract_controller/controllers/views/helpers/capture_with_args/index.erb +1 -0
  95. data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/render_two_throw_contents/index.erb +1 -0
  96. data/spec/public/abstract_controller/controllers/views/partial/partial_with_collections_and_counter/_collection.erb +1 -0
  97. data/spec/public/abstract_controller/controllers/views/partial/partial_with_collections_and_counter/index.erb +1 -0
  98. data/spec/public/abstract_controller/controllers/views/partial/with_absolute_partial/_partial.erb +1 -0
  99. data/spec/public/abstract_controller/controllers/views/partial/with_absolute_partial/index.erb +1 -0
  100. data/spec/public/abstract_controller/filter_spec.rb +20 -1
  101. data/spec/public/abstract_controller/helper_spec.rb +10 -2
  102. data/spec/public/abstract_controller/partial_spec.rb +8 -0
  103. data/spec/public/abstract_controller/render_spec.rb +8 -0
  104. data/spec/public/abstract_controller/spec_helper.rb +7 -3
  105. data/spec/public/boot_loader/boot_loader_spec.rb +2 -2
  106. data/spec/public/controller/base_spec.rb +10 -2
  107. data/spec/public/controller/config/init.rb +6 -0
  108. data/spec/public/controller/controllers/authentication.rb +9 -11
  109. data/spec/public/controller/controllers/base.rb +2 -8
  110. data/spec/public/controller/controllers/cookies.rb +16 -0
  111. data/spec/public/controller/controllers/dispatcher.rb +35 -0
  112. data/spec/public/controller/controllers/display.rb +62 -14
  113. data/spec/public/controller/controllers/redirect.rb +36 -0
  114. data/spec/public/controller/controllers/responder.rb +37 -11
  115. data/spec/public/controller/controllers/views/layout/custom_arg.json.erb +1 -0
  116. data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/class_and_local_provides/index.html.erb +1 -0
  117. data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/class_and_local_provides/index.xml.erb +1 -0
  118. data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/display_with_template/no_layout.html.erb +1 -0
  119. data/spec/public/controller/cookies_spec.rb +23 -0
  120. data/spec/public/controller/dispatcher_spec.rb +411 -0
  121. data/spec/public/controller/display_spec.rb +43 -10
  122. data/spec/public/controller/redirect_spec.rb +33 -0
  123. data/spec/public/controller/responder_spec.rb +79 -11
  124. data/spec/public/controller/spec_helper.rb +3 -1
  125. data/spec/public/controller/url_spec.rb +10 -0
  126. data/spec/public/core/merb_core_spec.rb +11 -0
  127. data/spec/public/core_ext/fixtures/core_ext_dependency.rb +2 -0
  128. data/spec/public/core_ext/kernel_spec.rb +9 -0
  129. data/spec/public/core_ext/spec_helper.rb +1 -0
  130. data/spec/public/directory_structure/directory/log/merb_test.log +3729 -272
  131. data/spec/public/directory_structure/directory_spec.rb +3 -4
  132. data/spec/public/logger/logger_spec.rb +4 -4
  133. data/spec/public/reloading/directory/log/merb_test.log +288066 -15
  134. data/spec/public/reloading/reload_spec.rb +49 -27
  135. data/spec/public/request/multipart_spec.rb +26 -0
  136. data/spec/public/request/request_spec.rb +21 -2
  137. data/spec/public/router/fixation_spec.rb +27 -0
  138. data/spec/public/router/fixture/log/merb_test.log +30050 -0
  139. data/spec/public/router/nested_matches_spec.rb +97 -0
  140. data/spec/public/router/resource_spec.rb +1 -9
  141. data/spec/public/router/resources_spec.rb +0 -20
  142. data/spec/public/router/spec_helper.rb +27 -9
  143. data/spec/public/router/special_spec.rb +21 -8
  144. data/spec/public/template/template_spec.rb +17 -5
  145. data/spec/public/test/controller_matchers_spec.rb +10 -0
  146. data/spec/public/test/request_helper_spec.rb +29 -0
  147. data/spec/public/test/route_helper_spec.rb +18 -1
  148. data/spec/public/test/route_matchers_spec.rb +28 -1
  149. data/spec/public/test/view_matchers_spec.rb +3 -3
  150. data/spec/spec_helper.rb +56 -12
  151. metadata +89 -47
  152. data/lib/merb-core/core_ext/class.rb +0 -299
  153. data/lib/merb-core/core_ext/hash.rb +0 -426
  154. data/lib/merb-core/core_ext/mash.rb +0 -154
  155. data/lib/merb-core/core_ext/object.rb +0 -147
  156. data/lib/merb-core/core_ext/object_space.rb +0 -14
  157. data/lib/merb-core/core_ext/rubygems.rb +0 -28
  158. data/lib/merb-core/core_ext/set.rb +0 -46
  159. data/lib/merb-core/core_ext/string.rb +0 -89
  160. data/lib/merb-core/core_ext/time.rb +0 -13
  161. data/lib/merb-core/dispatch/exceptions.html.erb +0 -297
  162. data/spec/private/core_ext/class_spec.rb +0 -22
  163. data/spec/private/core_ext/hash_spec.rb +0 -522
  164. data/spec/private/core_ext/object_spec.rb +0 -121
  165. data/spec/private/core_ext/set_spec.rb +0 -26
  166. data/spec/private/core_ext/string_spec.rb +0 -167
  167. data/spec/private/core_ext/time_spec.rb +0 -16
  168. data/spec/private/dispatch/dispatch_spec.rb +0 -26
  169. data/spec/private/dispatch/fixture/log/development.log +0 -1
  170. data/spec/private/dispatch/fixture/log/merb.4000.pid +0 -1
  171. data/spec/private/dispatch/fixture/log/production.log +0 -1
  172. data/spec/private/dispatch/fixture/merb.4000.pid +0 -1
  173. data/spec/private/rack/application_spec.rb +0 -43
  174. data/spec/public/controller/log/merb.4000.pid +0 -1
  175. data/spec/public/directory_structure/directory/log/merb.4000.pid +0 -1
  176. data/spec/public/directory_structure/directory/merb.4000.pid +0 -1
  177. data/spec/public/reloading/directory/log/merb.4000.pid +0 -1
  178. data/spec/public/reloading/directory/merb.4000.pid +0 -1
@@ -0,0 +1,414 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe Merb::Router::Route, "initially" do
4
+ predicate_matchers[:allow_fixation] = :allow_fixation?
5
+
6
+ before :each do
7
+ Merb::Router.prepare do |r|
8
+ r.resources :continents do |c|
9
+ c.resources :countries
10
+ end
11
+ end
12
+
13
+ @r1 = Merb::Router.routes.first
14
+ @r5 = Merb::Router.routes[4]
15
+ end
16
+
17
+ after :each do
18
+ Merb::Router.reset!
19
+ end
20
+
21
+ it "has reader for conditions" do
22
+ @r1.conditions
23
+ end
24
+
25
+ it "has reader for params" do
26
+ @r1.params
27
+ end
28
+
29
+ it "has reader for behavior" do
30
+ @r5.behavior
31
+ end
32
+
33
+ it "has reader for conditional block" do
34
+ @r5.conditional_block
35
+ end
36
+
37
+ it "has reader for segments" do
38
+ @r5.segments
39
+ end
40
+
41
+ it "has reader for index" do
42
+ @r1.index.should == 0
43
+ end
44
+
45
+ it "has reader for symbol" do
46
+ @r1.symbol
47
+ end
48
+
49
+ it "does not allow fixation" do
50
+ @r1.should_not allow_fixation
51
+ end
52
+ end
53
+
54
+ describe Merb::Router::Route, "#fixatable" do
55
+ predicate_matchers[:allow_fixation] = :allow_fixation?
56
+
57
+ before :each do
58
+ Merb::Router.prepare do |r|
59
+ r.resources :continents do |c|
60
+ c.resources :countries
61
+ end
62
+ end
63
+
64
+ @route = Merb::Router.routes[4]
65
+ end
66
+
67
+ after :each do
68
+ Merb::Router.reset!
69
+ end
70
+
71
+ it "allows fixation when called with true" do
72
+ @route.fixatable(true)
73
+
74
+ @route.should allow_fixation
75
+ end
76
+
77
+ it "allows fixation when called with true" do
78
+ @route.fixatable(true)
79
+ @route.should allow_fixation
80
+
81
+ @route.fixatable(false)
82
+ @route.should_not allow_fixation
83
+ end
84
+
85
+ it "allows fixation when called with default argument" do
86
+ @route.fixatable
87
+
88
+ @route.should allow_fixation
89
+ end
90
+ end
91
+
92
+
93
+
94
+ describe Merb::Router::Route, "#to_s" do
95
+ before :each do
96
+ Merb::Router.prepare do |r|
97
+ r.resources :continents do |c|
98
+ c.resources :countries
99
+ end
100
+ end
101
+
102
+ @route = Merb::Router.routes[4]
103
+ end
104
+
105
+ after :each do
106
+ Merb::Router.reset!
107
+ end
108
+
109
+ it "concatenates route segments" do
110
+ @route.stub!(:segments).and_return(["continents/", "new"])
111
+ @route.to_s.should == "continents/new"
112
+ end
113
+
114
+ it "prefixes symbol segments with colon" do
115
+ @route.stub!(:segments).and_return(["continents/", :id])
116
+ @route.to_s.should == "continents/:id"
117
+ end
118
+ end
119
+
120
+
121
+
122
+ describe Merb::Router::Route, "#register" do
123
+ before :each do
124
+ Merb::Router.prepare do |r|
125
+ r.resources :continents do |c|
126
+ c.resources :countries
127
+ end
128
+ end
129
+
130
+ @route = Merb::Router.routes[4]
131
+ end
132
+
133
+ after :each do
134
+ Merb::Router.reset!
135
+ end
136
+
137
+ it "adds route to Merb routes set" do
138
+ Merb::Router.routes = []
139
+ @route.register
140
+
141
+ Merb::Router.routes.should include(@route)
142
+ end
143
+
144
+ it "sets index on route" do
145
+ Merb::Router.routes = []
146
+ @route.register
147
+
148
+ @route.index.should == 0
149
+ end
150
+
151
+ it "returns route itself" do
152
+ Merb::Router.routes = []
153
+ @route.register.should == @route
154
+ end
155
+ end
156
+
157
+
158
+
159
+ describe Merb::Router::Route, "#symbol_segments" do
160
+ before :each do
161
+ Merb::Router.prepare do |r|
162
+ r.resources :continents do |c|
163
+ c.resources :countries
164
+ end
165
+ end
166
+
167
+ @route = Merb::Router.routes[4]
168
+ end
169
+
170
+ after :each do
171
+ Merb::Router.reset!
172
+ end
173
+
174
+ it "cherrypicks segments that are Symbols" do
175
+ @route.stub!(:segments).and_return(["prefix/", :controller, "ping"])
176
+ @route.symbol_segments.should == [:controller]
177
+ end
178
+ end
179
+
180
+
181
+
182
+ describe Merb::Router::Route, "#segments_from_path" do
183
+ before :each do
184
+ Merb::Router.prepare do |r|
185
+ r.resources :continents do |c|
186
+ c.resources :countries
187
+ end
188
+ end
189
+
190
+ @route = Merb::Router.routes[4]
191
+ end
192
+
193
+ after :each do
194
+ Merb::Router.reset!
195
+ end
196
+
197
+ it "turns path into string and symbol segments" do
198
+ @route.segments_from_path("prefix/:controller/:action/:id").should == ["prefix/", :controller, "/", :action, "/", :id]
199
+ end
200
+
201
+ it "handles slash edge case just fine" do
202
+ @route.segments_from_path("/").should == ["/"]
203
+ end
204
+ end
205
+
206
+
207
+
208
+ describe Merb::Router::Route, "#name" do
209
+ before :each do
210
+ Merb::Router.prepare do |r|
211
+ r.match("/").to(:controller => "home")
212
+ end
213
+
214
+ @route = Merb::Router.routes.first
215
+ end
216
+
217
+ after :each do
218
+ Merb::Router.reset!
219
+ end
220
+
221
+ it "places the route into named routes collection" do
222
+ @route.name(:home)
223
+
224
+ Merb::Router.named_routes[:home].should == @route
225
+ end
226
+
227
+ it "only accepts Symbols" do
228
+ lambda { @route.name("home") }.should raise_error(ArgumentError)
229
+ end
230
+
231
+ end
232
+
233
+
234
+ describe Merb::Router::Route, "#regexp?" do
235
+ before :each do
236
+ Merb::Router.prepare do |r|
237
+ r.match(/api\/(.*)/).to(:controller => "api", :token => "[1]").name(:regexpy)
238
+
239
+ r.match("/what-is-regexps-dude").to(:controller => "plain_strings").name(:non_regexpy)
240
+ end
241
+
242
+ @regexp_route = Merb::Router.named_routes[:regexpy]
243
+ @non_regexp_route = Merb::Router.named_routes[:non_regexpy]
244
+ end
245
+
246
+ it "is true for routes that use regular expressions" do
247
+ @regexp_route.should be_regexp
248
+ end
249
+
250
+ it "is false for routes that do not explicitly use regular expressions" do
251
+ @non_regexp_route.should_not be_regexp
252
+ end
253
+
254
+ it "is true if behavior returns true when sent regexp?" do
255
+ @non_regexp_route.stub!(:behavior).and_return(stub("regexp_behavior", :regexp? => true))
256
+ @non_regexp_route.should be_regexp
257
+ end
258
+
259
+ it "is true if any of behavior's anscestors return true when sent regexp?" do
260
+ @regexp_ancestor = stub("regexp_ancestor", :regexp? => true)
261
+ @not_regexp_ancestor = stub("not_regexp_ancestor", :regexp? => false)
262
+ @behavior_with_ancestors = stub("regexp_behavior", :regexp? => false, :ancestors => [@regexp_ancestor, @not_regexp_ancestor])
263
+
264
+ @non_regexp_route.stub!(:behavior).and_return(@behavior_with_ancestors)
265
+ @non_regexp_route.should be_regexp
266
+ end
267
+
268
+ after :each do
269
+ Merb::Router.reset!
270
+ end
271
+ end
272
+
273
+
274
+
275
+ describe Merb::Router::Route, "#generate" do
276
+ before :each do
277
+ Merb::Router.prepare do |r|
278
+ r.match(/api\/(.*)/).to(:controller => "api", :token => "[1]").name(:regexpy)
279
+ r.match("/world/countries/:name").to(:controller => "countries").name(:non_regexpy)
280
+ end
281
+
282
+ @regexp_route = Merb::Router.named_routes[:regexpy]
283
+ @non_regexp_route = Merb::Router.named_routes[:non_regexpy]
284
+ end
285
+
286
+ after :each do
287
+ Merb::Router.reset!
288
+ end
289
+
290
+ it "does not work for regexp routes" do
291
+ lambda { @regexp_route.generate({ :token => "apitoken" }) }.should raise_error(RuntimeError, /regexp/)
292
+ end
293
+
294
+ it "replaces symbol segments in the URL with values from given Hash parameters" do
295
+ @non_regexp_route.generate({ :name => "Italy" }).should == "/world/countries/Italy"
296
+ @non_regexp_route.generate({ :name => "Brazil" }).should == "/world/countries/Brazil"
297
+ end
298
+
299
+ it "replaces symbol segments with Fixnum values just fine" do
300
+ @non_regexp_route.generate({ :name => 101 }).should == "/world/countries/101"
301
+ end
302
+
303
+ it "appends unknown symbol segments after ?" do
304
+ @non_regexp_route.generate({ :name => 101, :area => 10101 }).should == "/world/countries/101?area=10101"
305
+ end
306
+
307
+ it "calls #to_param on segments that respond to it" do
308
+ @non_regexp_route.generate({ :name => stub('US', :to_param => 'USA') }).should == "/world/countries/USA"
309
+ end
310
+ end
311
+
312
+
313
+
314
+ describe Merb::Router::Route, "#if_conditions" do
315
+ before :each do
316
+ Merb::Router.prepare do |r|
317
+ r.match(/api\/(.*)/).to(:controller => "api", :token => "[1]").name(:regexpy)
318
+ r.match("/world/countries/:name").to(:controller => "countries").name(:non_regexpy)
319
+ r.match("/world/continents/:continent/countries/:country").to(:controller => "world_map").name(:two_symbol_segments)
320
+ end
321
+
322
+ @regexp_route = Merb::Router.named_routes[:regexpy]
323
+ @non_regexp_route = Merb::Router.named_routes[:non_regexpy]
324
+ @two_symbol_route = Merb::Router.named_routes[:two_symbol_segments]
325
+ end
326
+
327
+ after :each do
328
+ Merb::Router.reset!
329
+ end
330
+
331
+ it "returns array with =~ statements" do
332
+ # just to show you what it looks like
333
+ @non_regexp_route.if_conditions({}).should == [" (/^\\/world\\/countries\\/([^\\/.,;?]+)$/ =~ cached_path) && (path1 = $1)"]
334
+ end
335
+
336
+ it "returns array of syntactically valid Ruby code statements that can be evaluated" do
337
+ cached_path = "/world/countries/Italy"
338
+ eval(@non_regexp_route.if_conditions({}).first)
339
+ # raises nothing and passes
340
+ end
341
+
342
+ it "works with regexp routes" do
343
+ cached_path = "/api/signin"
344
+ eval(@regexp_route.if_conditions({}).first)
345
+ # raises nothing and passes
346
+ end
347
+
348
+ it "adds conditions and match group for each symbol segment of route" do
349
+ # again, just to show you what it looks like
350
+ @two_symbol_route.if_conditions({}).should == [" (/^\\/world\\/continents\\/([^\\/.,;?]+)\\/countries\\/([^\\/.,;?]+)$/ =~ cached_path) && (path1, path2 = $1, $2)"]
351
+ end
352
+ end
353
+
354
+ describe Merb::Router::Route, "#if_conditions" do
355
+ before :each do
356
+ Merb::Router.prepare do |r|
357
+ r.match(/api\/(.*)/).to(:controller => "api", :token => "[1]").name(:regexpy)
358
+ r.match("/world/countries/:name").to(:controller => "countries").name(:non_regexpy)
359
+ r.match("/world/continents/:continent/countries/:country").to(:controller => "world_map").name(:two_symbol_segments)
360
+ end
361
+
362
+ @regexp_route = Merb::Router.named_routes[:regexpy]
363
+ @non_regexp_route = Merb::Router.named_routes[:non_regexpy]
364
+ @two_symbol_route = Merb::Router.named_routes[:two_symbol_segments]
365
+ end
366
+
367
+ after :each do
368
+ Merb::Router.reset!
369
+ end
370
+
371
+ it "uses if in compiled statement when argument is false" do
372
+ @regexp_route.compile(false).should =~ /\s*elsif/
373
+ end
374
+
375
+ it "uses elsif in compiled statement when argument is true" do
376
+ @non_regexp_route.compile(true).should =~ /^if/
377
+ end
378
+
379
+ it "uses if in compiled statement by default" do
380
+ @regexp_route.compile.should =~ /\s*elsif/
381
+ end
382
+ end
383
+
384
+ describe "Merb::Router::Route.capture" do
385
+
386
+ after :each do
387
+ Merb::Router.reset!
388
+ end
389
+
390
+ it "should capture routes that have been added in the block context" do
391
+ routes, named, route = [], {}, nil
392
+ Merb::Router.prepare do |r|
393
+ routes, named = Merb::Router.capture do
394
+ route = r.match("/overview").to(:controller => "home", :action => 'overview')
395
+ end
396
+ r.match("/").to(:controller => "home").name(:home)
397
+ end
398
+ routes.should include(route)
399
+ named.should_not include(route)
400
+ end
401
+
402
+ it "should capture named routes that have been added in the block context" do
403
+ routes, named, route = [], {}, nil
404
+ Merb::Router.prepare do |r|
405
+ r.match("/overview").to(:controller => "home", :action => 'overview')
406
+ routes, named = Merb::Router.capture do
407
+ route = r.match("/").to(:controller => "home").name(:home)
408
+ end
409
+ end
410
+ routes.should include(route)
411
+ named[:home].should == route
412
+ end
413
+
414
+ end
@@ -0,0 +1,175 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe Merb::Router, "initially" do
4
+ it "has empty routes array" do
5
+ Merb::Router.routes.should be_empty
6
+ end
7
+
8
+ it "has empty named routes" do
9
+ Merb::Router.named_routes.should be_empty
10
+ end
11
+
12
+ it "stores named routes in a Hash" do
13
+ Merb::Router.named_routes.should be_kind_of(Hash)
14
+ end
15
+
16
+ it "has no .match method" do
17
+ Merb::Router.should_not respond_to(:match)
18
+ end
19
+ end
20
+
21
+
22
+
23
+ describe Merb::Router, ".prepare" do
24
+ before :each do
25
+ Merb::Router.routes = []
26
+ end
27
+
28
+ it "adds prepared routes to existing" do
29
+ Merb::Router.prepare do |r|
30
+ r.match("/").to(:controller => "home")
31
+ end
32
+
33
+ Merb::Router.routes.should_not be_empty
34
+ end
35
+
36
+ it "(re-)compiles routes" do
37
+ Merb::Router.should_receive(:compile)
38
+
39
+ Merb::Router.prepare do |r|
40
+ r.match("/").to(:controller => "home")
41
+ end
42
+ end
43
+
44
+ it "defines match method" do
45
+ Merb::Router.prepare do |r|
46
+ r.match("/").to(:controller => "home")
47
+ end
48
+
49
+ Merb::Router.should respond_to(:match)
50
+ end
51
+ end
52
+
53
+
54
+
55
+ describe Merb::Router, ".append" do
56
+ before :each do
57
+ Merb::Router.prepare do |r|
58
+ r.match("/").to(:controller => "home")
59
+ end
60
+ end
61
+
62
+ it "appends routes to existing" do
63
+ Merb::Router.append do |r|
64
+ r.match("/append").to(:controller => "appended_by_router").name(:appended)
65
+ end
66
+
67
+ Merb::Router.named_routes[:appended].index.should == 0
68
+ end
69
+ end
70
+
71
+
72
+
73
+ describe Merb::Router, ".prepend" do
74
+ before :each do
75
+ Merb::Router.prepare do |r|
76
+ r.match("/").to(:controller => "home")
77
+ end
78
+ end
79
+
80
+ it "prepends routes to existing" do
81
+ Merb::Router.prepend do |r|
82
+ r.match("/prepend").to(:controller => "prepended_by_router").name(:prepended)
83
+ end
84
+
85
+ Merb::Router.named_routes[:appended].index.should == 0
86
+ end
87
+ end
88
+
89
+
90
+
91
+ describe Merb::Router, ".generate", "given a Symbol" do
92
+ before :each do
93
+ Merb::Router.prepare do |r|
94
+ r.match("/elements/").to(:controller => "elements").name(:elements)
95
+ end
96
+ end
97
+
98
+ it "searches among named routes" do
99
+ Merb::Router.generate(:elements).should == "/elements/"
100
+ end
101
+
102
+ it "raises RuntimeError when named route is not found" do
103
+ lambda { Merb::Router.generate(:mountains) }.should raise_error(RuntimeError, /Named route not found/)
104
+ end
105
+ end
106
+
107
+
108
+
109
+ describe Merb::Router, ".generate", "given a Hash" do
110
+ before :each do
111
+ Merb::Router.prepare do |r|
112
+ r.match("/elements/").to(:controller => "elements").name(:elements)
113
+ end
114
+ end
115
+
116
+ it "uses default routes mapping" do
117
+ Merb::Router.generate({ :controller => "elements", :action => "show", :id => "fire" }).should == "/elements/show/fire"
118
+ end
119
+
120
+ it "appends extra parameters to URL" do
121
+ Merb::Router.generate({ :controller => "elements", :action => "search" }, { :q => "water" }).should == "/elements/search?q=water"
122
+ end
123
+
124
+ it "uses fallback hash when controller is not available" do
125
+ Merb::Router.generate({ :action => "show", :id => "Olympus" }, {}, { :controller => "mountains" }).should == "/mountains/show/Olympus"
126
+ end
127
+
128
+ it "uses fallback hash when action is not available" do
129
+ Merb::Router.generate({ :controller => "mountains", :id => "Olympus" }, {}, { :action => "show" }).should == "/mountains/show/Olympus"
130
+ end
131
+
132
+ it "ignores nil parameters" do
133
+ Merb::Router.generate({ :controller => "mountains", :action => nil, :id => "Olympus" }, {}, { :action => "show" }).should == "/mountains/show/Olympus"
134
+ end
135
+
136
+ it "respects format parameter" do
137
+ Merb::Router.generate({ :controller => "elements", :action => "show", :id => "fire", :format => :json }).should == "/elements/show/fire.json"
138
+ end
139
+ end
140
+
141
+
142
+
143
+ describe Merb::Router, ".generate_for_default_route", "given a Hash" do
144
+ before :each do
145
+ Merb::Router.prepare do |r|
146
+ r.match("/elements/").to(:controller => "elements").name(:elements)
147
+ end
148
+ end
149
+
150
+ it "uses default routes mapping" do
151
+ Merb::Router.generate_for_default_route({ :controller => "elements", :action => "show", :id => "fire" }, {}).should == "/elements/show/fire"
152
+ end
153
+
154
+ it "respects format parameter" do
155
+ Merb::Router.generate({ :controller => "elements", :action => "show", :id => "fire", :format => :json }).should == "/elements/show/fire.json"
156
+ end
157
+
158
+ it "requires both parameters to be present" do
159
+ lambda {
160
+ Merb::Router.generate_for_default_route({ :controller => "elements", :action => "show", :id => "fire" })
161
+ }.should raise_error(ArgumentError)
162
+ end
163
+
164
+ it "uses fallback hash when controller is not available" do
165
+ Merb::Router.generate_for_default_route({ :action => "show", :id => "Olympus" }, { :controller => "mountains" }).should == "/mountains/show/Olympus"
166
+ end
167
+
168
+ it "uses fallback hash when action is not available" do
169
+ Merb::Router.generate_for_default_route({ :controller => "mountains", :id => "Olympus" }, { :action => "show" }).should == "/mountains/show/Olympus"
170
+ end
171
+
172
+ it "ignores nil parameters" do
173
+ Merb::Router.generate_for_default_route({ :controller => "mountains", :action => nil, :id => "Olympus" }, { :action => "show" }).should == "/mountains/show/Olympus"
174
+ end
175
+ end