merb-core 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README +21 -0
- data/Rakefile +285 -0
- data/TODO +0 -0
- data/bin/merb +8 -0
- data/bin/merb-specs +5 -0
- data/docs/bootloading.dox +57 -0
- data/docs/documentation_standards +40 -0
- data/docs/new_render_api +51 -0
- data/lib/merb-core.rb +304 -0
- data/lib/merb-core/autoload.rb +29 -0
- data/lib/merb-core/bootloader.rb +601 -0
- data/lib/merb-core/config.rb +284 -0
- data/lib/merb-core/constants.rb +43 -0
- data/lib/merb-core/controller/abstract_controller.rb +531 -0
- data/lib/merb-core/controller/exceptions.rb +257 -0
- data/lib/merb-core/controller/merb_controller.rb +214 -0
- data/lib/merb-core/controller/mime.rb +88 -0
- data/lib/merb-core/controller/mixins/controller.rb +262 -0
- data/lib/merb-core/controller/mixins/render.rb +324 -0
- data/lib/merb-core/controller/mixins/responder.rb +464 -0
- data/lib/merb-core/controller/template.rb +205 -0
- data/lib/merb-core/core_ext.rb +12 -0
- data/lib/merb-core/core_ext/class.rb +192 -0
- data/lib/merb-core/core_ext/hash.rb +422 -0
- data/lib/merb-core/core_ext/kernel.rb +304 -0
- data/lib/merb-core/core_ext/mash.rb +154 -0
- data/lib/merb-core/core_ext/object.rb +136 -0
- data/lib/merb-core/core_ext/object_space.rb +14 -0
- data/lib/merb-core/core_ext/rubygems.rb +28 -0
- data/lib/merb-core/core_ext/set.rb +41 -0
- data/lib/merb-core/core_ext/string.rb +69 -0
- data/lib/merb-core/dispatch/cookies.rb +92 -0
- data/lib/merb-core/dispatch/dispatcher.rb +233 -0
- data/lib/merb-core/dispatch/exceptions.html.erb +297 -0
- data/lib/merb-core/dispatch/request.rb +560 -0
- data/lib/merb-core/dispatch/router.rb +141 -0
- data/lib/merb-core/dispatch/router/behavior.rb +777 -0
- data/lib/merb-core/dispatch/router/cached_proc.rb +52 -0
- data/lib/merb-core/dispatch/router/route.rb +212 -0
- data/lib/merb-core/dispatch/session.rb +28 -0
- data/lib/merb-core/dispatch/session/cookie.rb +166 -0
- data/lib/merb-core/dispatch/session/memcached.rb +161 -0
- data/lib/merb-core/dispatch/session/memory.rb +234 -0
- data/lib/merb-core/gem_ext/erubis.rb +19 -0
- data/lib/merb-core/logger.rb +230 -0
- data/lib/merb-core/plugins.rb +25 -0
- data/lib/merb-core/rack.rb +15 -0
- data/lib/merb-core/rack/adapter.rb +42 -0
- data/lib/merb-core/rack/adapter/ebb.rb +22 -0
- data/lib/merb-core/rack/adapter/evented_mongrel.rb +24 -0
- data/lib/merb-core/rack/adapter/fcgi.rb +16 -0
- data/lib/merb-core/rack/adapter/irb.rb +108 -0
- data/lib/merb-core/rack/adapter/mongrel.rb +25 -0
- data/lib/merb-core/rack/adapter/runner.rb +27 -0
- data/lib/merb-core/rack/adapter/thin.rb +27 -0
- data/lib/merb-core/rack/adapter/webrick.rb +35 -0
- data/lib/merb-core/rack/application.rb +77 -0
- data/lib/merb-core/rack/handler/mongrel.rb +97 -0
- data/lib/merb-core/server.rb +184 -0
- data/lib/merb-core/test.rb +10 -0
- data/lib/merb-core/test/helpers.rb +9 -0
- data/lib/merb-core/test/helpers/controller_helper.rb +8 -0
- data/lib/merb-core/test/helpers/multipart_request_helper.rb +175 -0
- data/lib/merb-core/test/helpers/request_helper.rb +257 -0
- data/lib/merb-core/test/helpers/route_helper.rb +33 -0
- data/lib/merb-core/test/helpers/view_helper.rb +121 -0
- data/lib/merb-core/test/matchers.rb +9 -0
- data/lib/merb-core/test/matchers/controller_matchers.rb +269 -0
- data/lib/merb-core/test/matchers/route_matchers.rb +136 -0
- data/lib/merb-core/test/matchers/view_matchers.rb +293 -0
- data/lib/merb-core/test/run_specs.rb +38 -0
- data/lib/merb-core/test/tasks/spectasks.rb +39 -0
- data/lib/merb-core/test/test_ext/hpricot.rb +32 -0
- data/lib/merb-core/test/test_ext/object.rb +14 -0
- data/lib/merb-core/vendor/facets.rb +2 -0
- data/lib/merb-core/vendor/facets/dictionary.rb +433 -0
- data/lib/merb-core/vendor/facets/inflect.rb +211 -0
- data/lib/merb-core/version.rb +11 -0
- data/spec/private/config/adapter_spec.rb +32 -0
- data/spec/private/config/config_spec.rb +139 -0
- data/spec/private/config/environment_spec.rb +13 -0
- data/spec/private/config/spec_helper.rb +1 -0
- data/spec/private/core_ext/hash_spec.rb +506 -0
- data/spec/private/core_ext/kernel_spec.rb +46 -0
- data/spec/private/core_ext/object_spec.rb +39 -0
- data/spec/private/core_ext/set_spec.rb +26 -0
- data/spec/private/core_ext/string_spec.rb +9 -0
- data/spec/private/dispatch/cookies_spec.rb +107 -0
- data/spec/private/dispatch/dispatch_spec.rb +26 -0
- data/spec/private/dispatch/fixture/app/controllers/application.rb +4 -0
- data/spec/private/dispatch/fixture/app/controllers/exceptions.rb +27 -0
- data/spec/private/dispatch/fixture/app/controllers/foo.rb +21 -0
- data/spec/private/dispatch/fixture/app/helpers/global_helpers.rb +8 -0
- data/spec/private/dispatch/fixture/app/views/exeptions/client_error.html.erb +37 -0
- data/spec/private/dispatch/fixture/app/views/exeptions/internal_server_error.html.erb +216 -0
- data/spec/private/dispatch/fixture/app/views/exeptions/not_acceptable.html.erb +38 -0
- data/spec/private/dispatch/fixture/app/views/exeptions/not_found.html.erb +40 -0
- data/spec/private/dispatch/fixture/app/views/foo/bar.html.erb +0 -0
- data/spec/private/dispatch/fixture/app/views/layout/application.html.erb +11 -0
- data/spec/private/dispatch/fixture/config/environments/development.rb +6 -0
- data/spec/private/dispatch/fixture/config/environments/production.rb +5 -0
- data/spec/private/dispatch/fixture/config/environments/test.rb +6 -0
- data/spec/private/dispatch/fixture/config/init.rb +45 -0
- data/spec/private/dispatch/fixture/config/rack.rb +1 -0
- data/spec/private/dispatch/fixture/config/router.rb +35 -0
- data/spec/private/dispatch/fixture/log/development.log +1 -0
- data/spec/private/dispatch/fixture/log/merb.4000.pid +1 -0
- data/spec/private/dispatch/fixture/log/merb_test.log +2040 -0
- data/spec/private/dispatch/fixture/log/production.log +1 -0
- data/spec/private/dispatch/fixture/merb.4000.pid +1 -0
- data/spec/private/dispatch/fixture/public/images/merb.jpg +0 -0
- data/spec/private/dispatch/fixture/public/merb.fcgi +4 -0
- data/spec/private/dispatch/fixture/public/stylesheets/master.css +119 -0
- data/spec/private/dispatch/route_params_spec.rb +24 -0
- data/spec/private/dispatch/spec_helper.rb +1 -0
- data/spec/private/plugins/plugin_spec.rb +81 -0
- data/spec/private/rack/application_spec.rb +43 -0
- data/spec/public/DEFINITIONS +11 -0
- data/spec/public/abstract_controller/controllers/alt_views/layout/application.erb +1 -0
- data/spec/public/abstract_controller/controllers/alt_views/layout/merb/test/fixtures/abstract/render_string_controller_layout.erb +1 -0
- data/spec/public/abstract_controller/controllers/alt_views/layout/merb/test/fixtures/abstract/render_template_controller_layout.erb +1 -0
- data/spec/public/abstract_controller/controllers/alt_views/merb/test/fixtures/abstract/display_object_with_multiple_roots/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/alt_views/merb/test/fixtures/abstract/display_object_with_multiple_roots/show.erb +1 -0
- data/spec/public/abstract_controller/controllers/alt_views/merb/test/fixtures/abstract/render_template_multiple_roots/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/alt_views/partial/basic_partial_with_multiple_roots/_partial.erb +1 -0
- data/spec/public/abstract_controller/controllers/alt_views/render_template_multiple_roots_and_custom_location/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/alt_views/render_template_multiple_roots_inherited/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/display.rb +54 -0
- data/spec/public/abstract_controller/controllers/filters.rb +167 -0
- data/spec/public/abstract_controller/controllers/helpers.rb +31 -0
- data/spec/public/abstract_controller/controllers/partial.rb +106 -0
- data/spec/public/abstract_controller/controllers/render.rb +86 -0
- data/spec/public/abstract_controller/controllers/views/helpers/capture/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/helpers/concat/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/layout/alt.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/layout/custom.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/display_object/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/display_object_with_action/new.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/render_template/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/render_template_app_layout/index.erb +0 -0
- data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/render_template_custom_layout/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/render_template_multiple_roots/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/render_template_multiple_roots/show.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/another_directory/_partial.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/basic_partial/_partial.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/basic_partial/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/basic_partial_with_multiple_roots/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/nested_partial/_first.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/nested_partial/_second.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/nested_partial/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_in_another_directory/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_both/_collection.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_both/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_collections/_collection.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_collections/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_collections_and_as/_collection.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_collections_and_as/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_locals/_variables.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_locals/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_with_and_locals/_both.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_with_and_locals/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/with_as_partial/_with_partial.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/with_as_partial/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/with_nil_partial/_with_partial.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/with_nil_partial/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/with_partial/_with_partial.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/with_partial/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/test_display/foo.html.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/test_render/foo.html.erb +0 -0
- data/spec/public/abstract_controller/controllers/views/wonderful/index.erb +1 -0
- data/spec/public/abstract_controller/display_spec.rb +33 -0
- data/spec/public/abstract_controller/filter_spec.rb +80 -0
- data/spec/public/abstract_controller/helper_spec.rb +13 -0
- data/spec/public/abstract_controller/partial_spec.rb +53 -0
- data/spec/public/abstract_controller/render_spec.rb +70 -0
- data/spec/public/abstract_controller/spec_helper.rb +27 -0
- data/spec/public/boot_loader/boot_loader_spec.rb +33 -0
- data/spec/public/boot_loader/spec_helper.rb +1 -0
- data/spec/public/controller/base_spec.rb +31 -0
- data/spec/public/controller/controllers/base.rb +41 -0
- data/spec/public/controller/controllers/display.rb +40 -0
- data/spec/public/controller/controllers/responder.rb +67 -0
- data/spec/public/controller/controllers/url.rb +7 -0
- data/spec/public/controller/controllers/views/layout/custom.html.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/class_provides/index.html.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/class_provides/index.xml.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/display_with_template/index.html.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/html_default/index.html.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/layout/custom.html.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/local_provides/index.html.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/local_provides/index.xml.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/multi_provides/index.html.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/multi_provides/index.js.erb +1 -0
- data/spec/public/controller/display_spec.rb +34 -0
- data/spec/public/controller/log/merb.4000.pid +1 -0
- data/spec/public/controller/responder_spec.rb +95 -0
- data/spec/public/controller/spec_helper.rb +9 -0
- data/spec/public/controller/url_spec.rb +152 -0
- data/spec/public/directory_structure/directory/app/controllers/application.rb +3 -0
- data/spec/public/directory_structure/directory/app/controllers/base.rb +13 -0
- data/spec/public/directory_structure/directory/app/controllers/custom.rb +19 -0
- data/spec/public/directory_structure/directory/app/views/base/template.html.erb +1 -0
- data/spec/public/directory_structure/directory/app/views/wonderful/template.erb +1 -0
- data/spec/public/directory_structure/directory/config/router.rb +3 -0
- data/spec/public/directory_structure/directory/log/merb.4000.pid +1 -0
- data/spec/public/directory_structure/directory/log/merb_test.log +265 -0
- data/spec/public/directory_structure/directory/merb.4000.pid +1 -0
- data/spec/public/directory_structure/directory_spec.rb +44 -0
- data/spec/public/logger/logger_spec.rb +175 -0
- data/spec/public/logger/spec_helper.rb +1 -0
- data/spec/public/reloading/directory/app/controllers/application.rb +3 -0
- data/spec/public/reloading/directory/app/controllers/reload.rb +6 -0
- data/spec/public/reloading/directory/config/init.rb +2 -0
- data/spec/public/reloading/directory/log/merb.4000.pid +1 -0
- data/spec/public/reloading/directory/log/merb_test.log +59 -0
- data/spec/public/reloading/directory/merb.4000.pid +1 -0
- data/spec/public/reloading/reload_spec.rb +80 -0
- data/spec/public/request/multipart_spec.rb +15 -0
- data/spec/public/request/request_spec.rb +207 -0
- data/spec/public/router/default_spec.rb +21 -0
- data/spec/public/router/deferred_spec.rb +22 -0
- data/spec/public/router/namespace_spec.rb +113 -0
- data/spec/public/router/nested_resources_spec.rb +34 -0
- data/spec/public/router/resource_spec.rb +45 -0
- data/spec/public/router/resources_spec.rb +57 -0
- data/spec/public/router/spec_helper.rb +72 -0
- data/spec/public/router/special_spec.rb +44 -0
- data/spec/public/router/string_spec.rb +61 -0
- data/spec/public/template/template_spec.rb +92 -0
- data/spec/public/template/templates/error.html.erb +2 -0
- data/spec/public/template/templates/template.html.erb +1 -0
- data/spec/public/template/templates/template.html.myt +1 -0
- data/spec/public/test/controller_matchers_spec.rb +378 -0
- data/spec/public/test/controllers/controller_assertion_mock.rb +7 -0
- data/spec/public/test/controllers/dispatch_controller.rb +11 -0
- data/spec/public/test/controllers/spec_helper_controller.rb +30 -0
- data/spec/public/test/multipart_request_helper_spec.rb +159 -0
- data/spec/public/test/multipart_upload_text_file.txt +1 -0
- data/spec/public/test/request_helper_spec.rb +153 -0
- data/spec/public/test/route_helper_spec.rb +54 -0
- data/spec/public/test/route_matchers_spec.rb +133 -0
- data/spec/public/test/view_helper_spec.rb +96 -0
- data/spec/public/test/view_matchers_spec.rb +107 -0
- data/spec/spec_helper.rb +71 -0
- metadata +488 -0
@@ -0,0 +1,136 @@
|
|
1
|
+
module Merb::Test::Rspec::RouteMatchers
|
2
|
+
|
3
|
+
class RouteToMatcher
|
4
|
+
|
5
|
+
# ==== Parameters
|
6
|
+
# klass_or_name<Class, String>::
|
7
|
+
# The controller class or class name to match routes for.
|
8
|
+
# action<~to_s>:: The name of the action to match routes for.
|
9
|
+
def initialize(klass_or_name, action)
|
10
|
+
@expected_controller = Class === klass_or_name ? klass_or_name.name : klass_or_name
|
11
|
+
@expected_action = action.to_s
|
12
|
+
end
|
13
|
+
|
14
|
+
# ==== Parameters
|
15
|
+
# target<Hash>:: The route parameters to match.
|
16
|
+
#
|
17
|
+
# ==== Returns
|
18
|
+
# Boolean:: True if the controller action and parameters match.
|
19
|
+
def matches?(target)
|
20
|
+
@target_env = target.dup
|
21
|
+
@target_controller, @target_action = @target_env.delete(:controller).to_s, @target_env.delete(:action).to_s
|
22
|
+
|
23
|
+
@target_controller = "#{target.delete(:namespace)}::#{@target_controller}" if target.has_key?(:namespace)
|
24
|
+
|
25
|
+
@expected_controller.snake_case == @target_controller.snake_case && @expected_action == @target_action && match_parameters(@target_env)
|
26
|
+
end
|
27
|
+
|
28
|
+
# ==== Parameters
|
29
|
+
# target<Hash>:: The route parameters to match.
|
30
|
+
#
|
31
|
+
# ==== Returns
|
32
|
+
# Boolean::
|
33
|
+
# True if the parameter matcher created with #with matches or if no
|
34
|
+
# parameter matcher exists.
|
35
|
+
def match_parameters(target)
|
36
|
+
@parameter_matcher.nil? ? true : @parameter_matcher.matches?(target)
|
37
|
+
end
|
38
|
+
|
39
|
+
# Creates a new paramter matcher.
|
40
|
+
#
|
41
|
+
# ==== Parameters
|
42
|
+
# parameters<Hash, ~to_param>:: The parameters to match.
|
43
|
+
#
|
44
|
+
# ==== Returns
|
45
|
+
# RouteToMatcher:: This matcher.
|
46
|
+
#
|
47
|
+
# ==== Alternatives
|
48
|
+
# If parameters is an object, then a new expected hash will be constructed
|
49
|
+
# with the key :id set to parameters.to_param.
|
50
|
+
def with(parameters)
|
51
|
+
@paramter_matcher = ParameterMatcher.new(parameters)
|
52
|
+
|
53
|
+
self
|
54
|
+
end
|
55
|
+
|
56
|
+
# ==== Returns
|
57
|
+
# String:: The failure message.
|
58
|
+
def failure_message
|
59
|
+
"expected the request to route to #{camelize(@expected_controller)}##{@expected_action}, but was #{camelize(@target_controller)}##{@target_action}"
|
60
|
+
end
|
61
|
+
|
62
|
+
# ==== Returns
|
63
|
+
# String:: The failure message to be displayed in negative matches.
|
64
|
+
def negative_failure_message
|
65
|
+
"expected the request not to route to #{camelize(@expected_controller)}##{@expected_action}, but it did"
|
66
|
+
end
|
67
|
+
|
68
|
+
# ==== Parameters
|
69
|
+
# word<~to_s>:: The word to camelize.
|
70
|
+
#
|
71
|
+
# ==== Returns
|
72
|
+
# String:: The word camelized.
|
73
|
+
def camelize(word)
|
74
|
+
word.to_s.gsub(/^[a-z]|(\_[a-z])/) { |a| a.upcase.gsub("_", "") }
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
class ParameterMatcher
|
79
|
+
|
80
|
+
# ==== Parameters
|
81
|
+
# hash_or_object<Hash, ~to_param>:: The parameters to match.
|
82
|
+
#
|
83
|
+
# ==== Alternatives
|
84
|
+
# If hash_or_object is an object, then a new expected hash will be
|
85
|
+
# constructed with the key :id set to hash_or_object.to_param.
|
86
|
+
def initialize(hash_or_object)
|
87
|
+
@expected = {}
|
88
|
+
case hash_or_object
|
89
|
+
when Hash then @expected = hash_or_object
|
90
|
+
else @expected[:id] = hash_or_object.to_param
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
# ==== Parameters
|
95
|
+
# parameter_hash<Hash>:: The route parameters to match.
|
96
|
+
#
|
97
|
+
# ==== Returns
|
98
|
+
# Boolean:: True if the route parameters match the expected ones.
|
99
|
+
def matches?(parameter_hash)
|
100
|
+
@actual = parameter_hash.dup.except(:controller, :action)
|
101
|
+
|
102
|
+
@expected.all? {|(k, v)| @actual.has_key?(k) && @actual[k] == v}
|
103
|
+
end
|
104
|
+
|
105
|
+
# ==== Returns
|
106
|
+
# String:: The failure message.
|
107
|
+
def failure_message
|
108
|
+
"expected the route to contain parameters #{@expected.inspect}, but instead contained #{@actual.inspect}"
|
109
|
+
end
|
110
|
+
|
111
|
+
# ==== Returns
|
112
|
+
# String:: The failure message to be displayed in negative matches.
|
113
|
+
def negative_failure_message
|
114
|
+
"expected the route not to contain parameters #{@expected.inspect}, but it did"
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
# Passes when the actual route parameters match the expected controller class
|
119
|
+
# and controller action. Exposes a +with+ method for specifying parameters.
|
120
|
+
#
|
121
|
+
# ==== Parameters
|
122
|
+
# klass_or_name<Class, String>::
|
123
|
+
# The controller class or class name to match routes for.
|
124
|
+
# action<~to_s>:: The name of the action to match routes for.
|
125
|
+
#
|
126
|
+
# ==== Example
|
127
|
+
# # Passes if a GET request to "/" is routed to the Widgets controller's
|
128
|
+
# # index action.
|
129
|
+
# request_to("/", :get).should route_to(Widgets, :index)
|
130
|
+
#
|
131
|
+
# # Use the 'with' method for parameter checks
|
132
|
+
# request_to("/123").should route_to(widgets, :show).with(:id => "123")
|
133
|
+
def route_to(klass_or_name, action)
|
134
|
+
RouteToMatcher.new(klass_or_name, action)
|
135
|
+
end
|
136
|
+
end
|
@@ -0,0 +1,293 @@
|
|
1
|
+
module Merb::Test::Rspec::ViewMatchers
|
2
|
+
class HaveSelector
|
3
|
+
|
4
|
+
# ==== Parameters
|
5
|
+
# expected<String>:: The string to look for.
|
6
|
+
def initialize(expected)
|
7
|
+
@expected = expected
|
8
|
+
end
|
9
|
+
|
10
|
+
# ==== Parameters
|
11
|
+
# stringlike<Hpricot::Elem, StringIO, String>:: The thing to search in.
|
12
|
+
#
|
13
|
+
# ==== Returns
|
14
|
+
# Boolean:: True if there was at least one match.
|
15
|
+
def matches?(stringlike)
|
16
|
+
@document = case stringlike
|
17
|
+
when Hpricot::Elem
|
18
|
+
stringlike
|
19
|
+
when StringIO
|
20
|
+
Hpricot.parse(stringlike.string)
|
21
|
+
else
|
22
|
+
Hpricot.parse(stringlike)
|
23
|
+
end
|
24
|
+
!@document.search(@expected).empty?
|
25
|
+
end
|
26
|
+
|
27
|
+
# ==== Returns
|
28
|
+
# String:: The failure message.
|
29
|
+
def failure_message
|
30
|
+
"expected following text to match selector #{@expected}:\n#{@document}"
|
31
|
+
end
|
32
|
+
|
33
|
+
# ==== Returns
|
34
|
+
# String:: The failure message to be displayed in negative matches.
|
35
|
+
def negative_failure_message
|
36
|
+
"expected following text to not match selector #{@expected}:\n#{@document}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class MatchTag
|
41
|
+
|
42
|
+
# ==== Parameters
|
43
|
+
# name<~to_s>:: The name of the tag to look for.
|
44
|
+
# attrs<Hash>:: Attributes to look for in the tag (see below).
|
45
|
+
#
|
46
|
+
# ==== Options (attrs)
|
47
|
+
# :content<String>:: Optional content to match.
|
48
|
+
def initialize(name, attrs)
|
49
|
+
@name, @attrs = name, attrs
|
50
|
+
@content = @attrs.delete(:content)
|
51
|
+
end
|
52
|
+
|
53
|
+
# ==== Parameters
|
54
|
+
# target<String>:: The string to look for the tag in.
|
55
|
+
#
|
56
|
+
# ==== Returns
|
57
|
+
# Boolean:: True if the tag matched.
|
58
|
+
def matches?(target)
|
59
|
+
@errors = []
|
60
|
+
unless target.include?("<#{@name}")
|
61
|
+
@errors << "Expected a <#{@name}>, but was #{target}"
|
62
|
+
end
|
63
|
+
@attrs.each do |attr, val|
|
64
|
+
unless target.include?("#{attr}=\"#{val}\"")
|
65
|
+
@errors << "Expected #{attr}=\"#{val}\", but was #{target}"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
if @content
|
69
|
+
unless target.include?(">#{@content}<")
|
70
|
+
@errors << "Expected #{target} to include #{@content}"
|
71
|
+
end
|
72
|
+
end
|
73
|
+
@errors.size == 0
|
74
|
+
end
|
75
|
+
|
76
|
+
# ==== Returns
|
77
|
+
# String:: The failure message.
|
78
|
+
def failure_message
|
79
|
+
@errors[0]
|
80
|
+
end
|
81
|
+
|
82
|
+
# ==== Returns
|
83
|
+
# String:: The failure message to be displayed in negative matches.
|
84
|
+
def negative_failure_message
|
85
|
+
"Expected not to match against <#{@name} #{@attrs.map{ |a,v| "#{a}=\"#{v}\"" }.join(" ")}> tag, but it matched"
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
class NotMatchTag
|
90
|
+
|
91
|
+
# === Parameters
|
92
|
+
# attrs<Hash>:: A set of attributes that must not be matched.
|
93
|
+
def initialize(attrs)
|
94
|
+
@attrs = attrs
|
95
|
+
end
|
96
|
+
|
97
|
+
# ==== Parameters
|
98
|
+
# target<String>:: The target to look for the match in.
|
99
|
+
#
|
100
|
+
# ==== Returns
|
101
|
+
# Boolean:: True if none of the attributes were matched.
|
102
|
+
def matches?(target)
|
103
|
+
@errors = []
|
104
|
+
@attrs.each do |attr, val|
|
105
|
+
if target.include?("#{attr}=\"#{val}\"")
|
106
|
+
@errors << "Should not include #{attr}=\"#{val}\", but was #{target}"
|
107
|
+
end
|
108
|
+
end
|
109
|
+
@errors.size == 0
|
110
|
+
end
|
111
|
+
|
112
|
+
# ==== Returns
|
113
|
+
# String:: The failure message.
|
114
|
+
def failure_message
|
115
|
+
@errors[0]
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
class HasTag
|
120
|
+
|
121
|
+
# ==== Parameters
|
122
|
+
# tag<~to_s>:: The tag to look for.
|
123
|
+
# attributes<Hash>:: Attributes for the tag (see below).
|
124
|
+
def initialize(tag, attributes = {})
|
125
|
+
@tag, @attributes = tag, attributes
|
126
|
+
@id, @class = @attributes.delete(:id), @attributes.delete(:class)
|
127
|
+
end
|
128
|
+
|
129
|
+
# ==== Parameters
|
130
|
+
# stringlike<Hpricot::Elem, StringIO, String>:: The thing to search in.
|
131
|
+
# &blk:: An optional block for searching in child elements using with_tag.
|
132
|
+
#
|
133
|
+
# ==== Returns
|
134
|
+
# Boolean:: True if there was at least one match.
|
135
|
+
def matches?(stringlike, &blk)
|
136
|
+
@document = case stringlike
|
137
|
+
when Hpricot::Elem
|
138
|
+
stringlike
|
139
|
+
when StringIO
|
140
|
+
Hpricot.parse(stringlike.string)
|
141
|
+
else
|
142
|
+
Hpricot.parse(stringlike)
|
143
|
+
end
|
144
|
+
|
145
|
+
if block_given?
|
146
|
+
!@document.search(selector).select do |ele|
|
147
|
+
begin
|
148
|
+
blk.call(ele)
|
149
|
+
rescue Spec::Expectations::ExpectationNotMetError
|
150
|
+
false
|
151
|
+
end
|
152
|
+
end.empty?
|
153
|
+
else
|
154
|
+
!@document.search(selector).empty?
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
# ==== Returns
|
159
|
+
# String:: The complete selector for element queries.
|
160
|
+
def selector
|
161
|
+
@selector = "//#{@tag}#{id_selector}#{class_selector}"
|
162
|
+
@selector << @attributes.map{|a, v| "[@#{a}=\"#{v}\"]"}.join
|
163
|
+
|
164
|
+
@selector << @inner_has_tag.selector unless @inner_has_tag.nil?
|
165
|
+
|
166
|
+
@selector
|
167
|
+
end
|
168
|
+
|
169
|
+
# ==== Returns
|
170
|
+
# String:: ID selector for use in element queries.
|
171
|
+
def id_selector
|
172
|
+
"##{@id}" if @id
|
173
|
+
end
|
174
|
+
|
175
|
+
# ==== Returns
|
176
|
+
# String:: Class selector for use in element queries.
|
177
|
+
def class_selector
|
178
|
+
".#{@class}" if @class
|
179
|
+
end
|
180
|
+
|
181
|
+
# ==== Returns
|
182
|
+
# String:: The failure message.
|
183
|
+
def failure_message
|
184
|
+
"expected following output to contain a #{tag_for_error} tag:\n#{@document}"
|
185
|
+
end
|
186
|
+
|
187
|
+
# ==== Returns
|
188
|
+
# String:: The failure message to be displayed in negative matches.
|
189
|
+
def negative_failure_message
|
190
|
+
"expected following output to omit a #{tag_for_error} tag:\n#{@document}"
|
191
|
+
end
|
192
|
+
|
193
|
+
# ==== Returns
|
194
|
+
# String:: The tag used in failure messages.
|
195
|
+
def tag_for_error
|
196
|
+
"#{inner_failure_message}<#{@tag}#{id_for_error}#{class_for_error}#{attributes_for_error}>"
|
197
|
+
end
|
198
|
+
|
199
|
+
# ==== Returns
|
200
|
+
# String::
|
201
|
+
# The failure message to be displayed in negative matches within the
|
202
|
+
# have_tag block.
|
203
|
+
def inner_failure_message
|
204
|
+
"#{@inner_has_tag.tag_for_error} tag within a " unless @inner_has_tag.nil?
|
205
|
+
end
|
206
|
+
|
207
|
+
# ==== Returns
|
208
|
+
# String:: ID for the error tag.
|
209
|
+
def id_for_error
|
210
|
+
" id=\"#{@id}\"" unless @id.nil?
|
211
|
+
end
|
212
|
+
|
213
|
+
# ==== Returns
|
214
|
+
# String:: Class for the error tag.
|
215
|
+
def class_for_error
|
216
|
+
" class=\"#{@class}\"" unless @class.nil?
|
217
|
+
end
|
218
|
+
|
219
|
+
# ==== Returns
|
220
|
+
# String:: Class for the error tag.
|
221
|
+
def attributes_for_error
|
222
|
+
@attributes.map{|a,v| " #{a}=\"#{v}\""}.join
|
223
|
+
end
|
224
|
+
|
225
|
+
# Search for a child tag within a have_tag block.
|
226
|
+
#
|
227
|
+
# ==== Parameters
|
228
|
+
# tag<~to_s>:: The tag to look for.
|
229
|
+
# attributes<Hash>:: Attributes for the tag (see below).
|
230
|
+
def with_tag(name, attrs={})
|
231
|
+
@inner_has_tag = HasTag.new(name, attrs)
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
# ==== Parameters
|
236
|
+
# name<~to_s>:: The name of the tag to look for.
|
237
|
+
# attrs<Hash>:: Attributes to look for in the tag (see below).
|
238
|
+
#
|
239
|
+
# ==== Options (attrs)
|
240
|
+
# :content<String>:: Optional content to match.
|
241
|
+
#
|
242
|
+
# ==== Returns
|
243
|
+
# MatchTag:: A new match tag matcher.
|
244
|
+
def match_tag(name, attrs={})
|
245
|
+
MatchTag.new(name, attrs)
|
246
|
+
end
|
247
|
+
|
248
|
+
# ==== Parameters
|
249
|
+
# attrs<Hash>:: A set of attributes that must not be matched.
|
250
|
+
#
|
251
|
+
# ==== Returns
|
252
|
+
# NotMatchTag:: A new not match tag matcher.
|
253
|
+
def not_match_tag(attrs)
|
254
|
+
NotMatchTag.new(attrs)
|
255
|
+
end
|
256
|
+
|
257
|
+
# ==== Parameters
|
258
|
+
# expected<String>:: The string to look for.
|
259
|
+
#
|
260
|
+
# ==== Returns
|
261
|
+
# HaveSelector:: A new have selector matcher.
|
262
|
+
def have_selector(expected)
|
263
|
+
HaveSelector.new(expected)
|
264
|
+
end
|
265
|
+
alias_method :match_selector, :have_selector
|
266
|
+
|
267
|
+
# RSpec matcher to test for the presence of tags.
|
268
|
+
#
|
269
|
+
# ==== Parameters
|
270
|
+
# tag<~to_s>:: The name of the tag.
|
271
|
+
# attributes<Hash>:: Tag attributes.
|
272
|
+
#
|
273
|
+
# ==== Returns
|
274
|
+
# HasTag:: A new has tag matcher.
|
275
|
+
#
|
276
|
+
# ==== Examples
|
277
|
+
# # Check for <div>
|
278
|
+
# body.should have_tag("div")
|
279
|
+
#
|
280
|
+
# # Check for <span id="notice">
|
281
|
+
# body.should have_tag("span", :id => :notice)
|
282
|
+
#
|
283
|
+
# # Check for <h1 id="foo" class="bar">
|
284
|
+
# body.should have_tag(:h2, :class => "bar", :id => "foo")
|
285
|
+
#
|
286
|
+
# # Check for <div attr="val">
|
287
|
+
# body.should have_tag(:div, :attr => :val)
|
288
|
+
def have_tag(tag, attributes = {})
|
289
|
+
HasTag.new(tag, attributes)
|
290
|
+
end
|
291
|
+
|
292
|
+
alias_method :with_tag, :have_tag
|
293
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'open3'
|
2
|
+
|
3
|
+
# Runs specs in all files matching the file pattern.
|
4
|
+
#
|
5
|
+
# ==== Parameters
|
6
|
+
# globs<String, Array[String]>:: File patterns to look for.
|
7
|
+
# spec_cmd<~to_s>:: The spec command. Defaults to "spec".
|
8
|
+
def run_specs(globs, spec_cmd='spec')
|
9
|
+
require "optparse"
|
10
|
+
require "spec"
|
11
|
+
globs = globs.is_a?(Array) ? globs : [globs]
|
12
|
+
examples, failures, errors, pending = 0, 0, 0, 0
|
13
|
+
globs.each do |glob|
|
14
|
+
Dir[glob].each do |spec|
|
15
|
+
response = Open3.popen3("#{spec_cmd} #{File.expand_path(spec)} -f s -c") do |i,o,e|
|
16
|
+
while out = o.gets
|
17
|
+
STDOUT.puts out
|
18
|
+
STDOUT.flush
|
19
|
+
if out =~ /\d+ example/
|
20
|
+
e, f, p = out.match(/(\d+) examples?, (\d+) failures?(?:, (\d+) pending?)?/)[1..-1]
|
21
|
+
examples += e.to_i; failures += f.to_i; pending += p.to_i
|
22
|
+
end
|
23
|
+
end
|
24
|
+
errors += 1 if e.is_a?(IO)
|
25
|
+
STDOUT.puts e.read if e.is_a?(IO)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
puts
|
30
|
+
puts "*** TOTALS ***"
|
31
|
+
if failures == 0
|
32
|
+
print "\e[32m"
|
33
|
+
else
|
34
|
+
print "\e[31m"
|
35
|
+
end
|
36
|
+
puts "#{examples} examples, #{failures} failures, #{errors} errors, #{pending} pending"
|
37
|
+
print "\e[0m"
|
38
|
+
end
|