shoulda_matchmakers 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/Rakefile +21 -0
- data/lib/controller/action_controller/action_controller_controller_sm_model.rb +53 -0
- data/lib/controller/action_controller/action_controller_controller_sm_model_helper.rb +73 -0
- data/lib/controller/action_controller/matchmakers/callbacks.rb +56 -0
- data/lib/controller/action_controller/matchmakers/filter_param.rb +69 -0
- data/lib/controller/action_controller/matchmakers/permit.rb +510 -0
- data/lib/controller/action_controller/matchmakers/redirect_to.rb +126 -0
- data/lib/controller/action_controller/matchmakers/render_template.rb +198 -0
- data/lib/controller/action_controller/matchmakers/render_with_layout.rb +72 -0
- data/lib/controller/action_controller/matchmakers/rescue_from.rb +96 -0
- data/lib/controller/action_controller/matchmakers/respond_with.rb +71 -0
- data/lib/controller/action_controller/matchmakers/route.rb +586 -0
- data/lib/controller/action_controller/matchmakers/set_flash.rb +136 -0
- data/lib/controller/action_controller/matchmakers/set_session.rb +115 -0
- data/lib/controller/action_controller/permitted_params_definition.rb +60 -0
- data/lib/generators/shoulda_matchmakers/controller_matcher_generator.rb +87 -0
- data/lib/generators/shoulda_matchmakers/controller_matcher_generator_helper.rb +92 -0
- data/lib/generators/shoulda_matchmakers/factory_generator.rb +86 -0
- data/lib/generators/shoulda_matchmakers/install_generator.rb +10 -0
- data/lib/generators/shoulda_matchmakers/model_matcher_generator.rb +87 -0
- data/lib/generators/shoulda_matchmakers/model_matcher_generator_helper.rb +107 -0
- data/lib/model/active_record/active_record_model_sm_model.rb +50 -0
- data/lib/model/active_record/active_record_model_sm_model_helper.rb +206 -0
- data/lib/model/active_record/factory_sm_model.rb +28 -0
- data/lib/model/active_record/factory_sm_model_helper.rb +124 -0
- data/lib/model/active_record/matchmakers/accept_nested_attributes_for.rb +66 -0
- data/lib/model/active_record/matchmakers/allow_value.rb +137 -0
- data/lib/model/active_record/matchmakers/associations.rb +52 -0
- data/lib/model/active_record/matchmakers/define_enum_for.rb +53 -0
- data/lib/model/active_record/matchmakers/delegate_method.rb +101 -0
- data/lib/model/active_record/matchmakers/have_db_column.rb +33 -0
- data/lib/model/active_record/matchmakers/have_db_index.rb +39 -0
- data/lib/model/active_record/matchmakers/have_readonly_attribute.rb +32 -0
- data/lib/model/active_record/matchmakers/have_secure_password.rb +32 -0
- data/lib/model/active_record/matchmakers/serialize.rb +67 -0
- data/lib/model/active_record/matchmakers/validations.rb +169 -0
- data/lib/model/active_record/options_definition.rb +160 -0
- data/lib/shoulda_matchmakers.rb +48 -0
- data/lib/shoulda_matchmakers/version.rb +3 -0
- data/lib/support/command_line_interface.rb +100 -0
- data/lib/support/controller_generator.rb +198 -0
- data/lib/support/indefinite_article.rb +70 -0
- data/lib/support/to_discard/belong_to.rb +94 -0
- data/lib/support/to_discard/have_and_belong_to_many.rb +83 -0
- data/lib/support/to_discard/have_many.rb +96 -0
- data/lib/support/to_discard/have_one.rb +98 -0
- data/lib/support/to_discard/permit_refactor.rb +270 -0
- data/lib/support/to_discard/render_template_new.rb +204 -0
- data/lib/support/to_discard/render_template_reconstruction.rb +188 -0
- data/lib/support/to_discard/use_around_action.rb +20 -0
- data/lib/support/to_discard/use_before_action.rb +20 -0
- data/lib/support/to_discard/validate_absence_of.rb +16 -0
- data/lib/support/to_discard/validate_acceptance_of.rb +16 -0
- data/lib/support/to_discard/validate_confirmation_of.rb +16 -0
- data/lib/support/to_discard/validate_exclusion_of.rb +145 -0
- data/lib/support/to_discard/validate_inclusion_of.rb +94 -0
- data/lib/support/to_discard/validate_length_of.rb +16 -0
- data/lib/support/to_discard/validate_numericality_of.rb +16 -0
- data/lib/support/to_discard/validate_presence_of.rb +16 -0
- data/lib/support/to_discard/validate_uniqueness_of.rb +16 -0
- data/lib/support/to_discard/validation_old.rb +37 -0
- data/lib/support/to_discard/validations.rb +41 -0
- data/lib/support/to_discard/validations_conditional.rb +146 -0
- data/lib/support/trace_debug_lines.txt +319 -0
- data/lib/support/util.rb +15 -0
- data/lib/templates/controller/action_controller/controller_spec_template.haml +146 -0
- data/lib/templates/factory/active_record/factory_template.haml +13 -0
- data/lib/templates/model/active_record/model_spec_template.haml +233 -0
- data/lib/templates/shoulda_matchmakers.rb +112 -0
- metadata +263 -0
@@ -0,0 +1,71 @@
|
|
1
|
+
module ShouldaMatchmakers
|
2
|
+
module Controller
|
3
|
+
module ActionController
|
4
|
+
module Matchmaker
|
5
|
+
module RespondWith
|
6
|
+
|
7
|
+
|
8
|
+
def respond_with_matcher_tests
|
9
|
+
respond_with_occurrences = get_respond_with_occurrences(@app_controller_name)
|
10
|
+
if respond_with_occurrences.present?
|
11
|
+
generate_respond_with_matcher_tests(@app_controller_name, respond_with_occurrences)
|
12
|
+
else
|
13
|
+
[]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def get_respond_with_occurrences(app_controller_name)
|
21
|
+
respond_with_occurrences = []
|
22
|
+
app_controller_file_path = compose_extended_app_controller_file_path(app_controller_name)
|
23
|
+
if File.exists?(app_controller_file_path)
|
24
|
+
respond_with_occurrences = parse_app_controller_for_respond_with_occurrences(app_controller_file_path)
|
25
|
+
end
|
26
|
+
respond_with_occurrences
|
27
|
+
end
|
28
|
+
|
29
|
+
def parse_app_controller_for_respond_with_occurrences(app_controller_file_path)
|
30
|
+
respond_with_occurrences = []
|
31
|
+
current_app_controller_method = nil
|
32
|
+
File.open(app_controller_file_path, 'r') do |app_controller_file|
|
33
|
+
app_controller_file.each_line do |app_controller_file_line|
|
34
|
+
if app_controller_file_line =~ /\s+def\s[A-Za-z0-9_][A-Za-z0-9_!\?=]+/
|
35
|
+
current_app_controller_method = app_controller_file_line.scan(/\s+def\s([A-Za-z0-9_][A-Za-z0-9_!\?=]+)/).flatten.first
|
36
|
+
elsif app_controller_file_line.include?(" render status: ")
|
37
|
+
status_code = app_controller_file_line.scan(/\s+render\sstatus:\s+([0-9:][a-z0-9_]+)/).flatten.first
|
38
|
+
if current_app_controller_method.present? && status_code.present?
|
39
|
+
respond_with_occurrences << { controller_action: current_app_controller_method, status_code: status_code }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
respond_with_occurrences
|
45
|
+
end
|
46
|
+
|
47
|
+
def generate_respond_with_matcher_tests(app_controller_name, respond_with_occurrences)
|
48
|
+
respond_with_tests = []
|
49
|
+
respond_with_occurrences.each do |respond_with_occurrence|
|
50
|
+
app_controller_route_controller = compose_route_controller(app_controller_name)
|
51
|
+
app_controller_action_routes = get_app_controller_routes_by_action(app_controller_route_controller, respond_with_occurrence[:controller_action])
|
52
|
+
app_controller_action_routes.each do |app_controller_action_route|
|
53
|
+
respond_with_test = generate_respond_with_test(app_controller_action_route, respond_with_occurrence)
|
54
|
+
respond_with_tests = append_element(respond_with_test, respond_with_tests)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
format_tests(respond_with_tests)
|
58
|
+
end
|
59
|
+
|
60
|
+
def generate_respond_with_test(app_controller_action_route, respond_with_occurrence)
|
61
|
+
app_controller_route_http_method = get_route_http_method(app_controller_action_route)
|
62
|
+
respond_with_test = " describe '#{ app_controller_route_http_method } ##{ respond_with_occurrence[:controller_action] }' do\n"
|
63
|
+
respond_with_test.concat(" before { #{ app_controller_route_http_method.downcase } :#{ respond_with_occurrence[:controller_action] } }\n\n")
|
64
|
+
respond_with_test.concat(" it { is_expected.to respond_with(#{ respond_with_occurrence[:status_code] }) }\n end\n")
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,586 @@
|
|
1
|
+
module ShouldaMatchmakers
|
2
|
+
module Controller
|
3
|
+
module ActionController
|
4
|
+
module Matchmaker
|
5
|
+
module Route
|
6
|
+
|
7
|
+
|
8
|
+
def route_matcher_tests
|
9
|
+
if journey_routes_present(@app_controller_name)
|
10
|
+
generate_route_matcher_tests(@app_controller_name)
|
11
|
+
else
|
12
|
+
[]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def journey_routes_present(app_controller_name)
|
20
|
+
app_controller_journey_routes_present = false
|
21
|
+
app_controller_path = get_app_controller_file_path(app_controller_name)
|
22
|
+
app_controller_name.constantize.action_methods.map do |app_controller_action_method|
|
23
|
+
app_controller_journey_routes = extract_journey_routes(app_controller_path, app_controller_action_method)
|
24
|
+
app_controller_journey_routes_present = true if app_controller_journey_routes.present?
|
25
|
+
end
|
26
|
+
app_controller_journey_routes_present
|
27
|
+
end
|
28
|
+
|
29
|
+
def extract_journey_routes(app_controller_path, app_controller_action_method)
|
30
|
+
Rails.application.routes.routes.routes.select do |route|
|
31
|
+
if route.defaults.present?
|
32
|
+
route.defaults[:controller].to_sym == app_controller_path.to_sym && route.defaults[:action].to_sym == app_controller_action_method.to_sym
|
33
|
+
else
|
34
|
+
false
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def generate_route_matcher_tests(app_controller_name)
|
40
|
+
route_tests = []
|
41
|
+
app_controller_path = get_app_controller_file_path(app_controller_name)
|
42
|
+
app_controller_name.constantize.action_methods.sort.each do |app_controller_action_method|
|
43
|
+
app_controller_journey_routes = extract_journey_routes(app_controller_path, app_controller_action_method)
|
44
|
+
app_controller_journey_routes.each do |app_controller_journey_route|
|
45
|
+
tests_for_one_route = generate_tests_for_one_route(app_controller_journey_route)
|
46
|
+
route_tests = route_tests + tests_for_one_route
|
47
|
+
end
|
48
|
+
end
|
49
|
+
format_tests(route_tests)
|
50
|
+
end
|
51
|
+
|
52
|
+
def generate_tests_for_one_route(app_controller_journey_route)
|
53
|
+
tests_for_one_route = []
|
54
|
+
route_parameters_hash = compose_route_parameters_hash(app_controller_journey_route)
|
55
|
+
route_url = get_route_url(app_controller_journey_route, route_parameters_hash)
|
56
|
+
request_methods = get_request_methods(app_controller_journey_route)
|
57
|
+
# CONFIRMATION TODO: Determine if this is the proper way to handle controller actions with multiple HTTP verbs
|
58
|
+
request_methods.each do |request_method|
|
59
|
+
tests_for_one_route << generate_route_test(app_controller_journey_route, route_url, request_method, route_parameters_hash)
|
60
|
+
end
|
61
|
+
tests_for_one_route
|
62
|
+
end
|
63
|
+
|
64
|
+
def compose_route_parameters_string(route_parameters_hash)
|
65
|
+
route_parameters_string = ""
|
66
|
+
route_parameters_hash.each do |param_key, param_value|
|
67
|
+
if param_key.to_s == "format" && param_value == nil
|
68
|
+
route_parameters_string.concat(", format: nil")
|
69
|
+
else
|
70
|
+
route_parameters_string.concat(", " + param_key.to_s + ": " + param_value.to_s)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
route_parameters_string
|
74
|
+
end
|
75
|
+
|
76
|
+
# IMPLEMENTATION TODO: Determine if possible to utilize 'regexp-examples' gem to provide values
|
77
|
+
def add_dummy_values_to_route_parameters_with_regexp_values(parameters_hash_with_regexp_values_handled)
|
78
|
+
parameters_hash_with_regexp_dummy_values = parameters_hash_with_regexp_values_handled
|
79
|
+
parameters_hash_with_regexp_dummy_values.each do |param_key, param_value|
|
80
|
+
if param_value.is_a?(Regexp)
|
81
|
+
parameters_hash_with_regexp_dummy_values[param_key] = "'<value matching regexp required>'"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
parameters_hash_with_regexp_dummy_values
|
85
|
+
end
|
86
|
+
|
87
|
+
def add_dummy_values_to_route_parameters_with_missing_values(parameters_hash_handled, parameter_keys_with_missing_values_handled)
|
88
|
+
parameters_hash_with_dummy_values = parameters_hash_handled
|
89
|
+
parameter_keys_with_missing_values_handled.each do |key_with_missing_value|
|
90
|
+
parameters_hash_with_dummy_values[key_with_missing_value] = "'<value required>'"
|
91
|
+
end
|
92
|
+
parameters_hash_with_dummy_values
|
93
|
+
end
|
94
|
+
|
95
|
+
def add_test_comment_for_route_parameters_with_regexp_values(parameters_hash_handled)
|
96
|
+
parameters_hash_with_regexp_values_comment_added = parameters_hash_handled
|
97
|
+
parameters_hash_with_regexp_values_comment_added.each do |param_key, param_value|
|
98
|
+
if param_value.is_a?(Regexp)
|
99
|
+
param_regexp_string = param_value.to_s.sub("(?-mix:","").gsub(/\)$/,"")
|
100
|
+
if param_regexp_string.length <= 40
|
101
|
+
parameters_hash_with_regexp_values_comment_added[:test_comment].concat("# A required value for route parameter ':#{ param_key }' matching regexp '#{ param_regexp_string }' needs to be provided.\n")
|
102
|
+
else
|
103
|
+
parameters_hash_with_regexp_values_comment_added[:test_comment].concat("# A required value for route parameter ':#{ param_key }' matching a regexp specified in routes.rb needs to be provided.\n")
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
parameters_hash_with_regexp_values_comment_added
|
108
|
+
end
|
109
|
+
|
110
|
+
def add_test_comment_for_route_parameters_with_missing_values(parameters_hash_with_missing_values_handled, parameter_keys_with_missing_values_handled)
|
111
|
+
parameters_hash_with_missing_values_comment_added = parameters_hash_with_missing_values_handled
|
112
|
+
if parameter_keys_with_missing_values_handled.size == 1
|
113
|
+
parameters_hash_with_missing_values_comment_added[:test_comment].concat("# A required value for route parameter ':#{ parameter_keys_with_missing_values_handled[0] }' needs to be provided.\n")
|
114
|
+
else
|
115
|
+
parameters_hash_with_missing_values_comment_added[:test_comment].concat("# Required values for route parameters '#{ parameter_keys_with_missing_values_handled.to_s }' need to be provided.\n")
|
116
|
+
end
|
117
|
+
parameters_hash_with_missing_values_comment_added
|
118
|
+
end
|
119
|
+
|
120
|
+
def compose_route_parameters_hash(journey_route)
|
121
|
+
route_required_parameter_keys_with_missing_values = journey_route.required_keys - journey_route.requirements.keys
|
122
|
+
route_parameters_hash = journey_route.requirements.select { |key, value| journey_route.parts.include? key } || {}
|
123
|
+
if special_handling_of_route_parameters_required(route_parameters_hash, route_required_parameter_keys_with_missing_values)
|
124
|
+
route_parameters_hash = handle_route_parameters_which_require_special_handling(route_parameters_hash, route_required_parameter_keys_with_missing_values)
|
125
|
+
end
|
126
|
+
route_parameters_hash
|
127
|
+
end
|
128
|
+
|
129
|
+
def generate_route_test(journey_route, route_url, request_method, route_parameters_hash)
|
130
|
+
route_test = generate_route_test_single_line(journey_route, route_url, request_method, route_parameters_hash)
|
131
|
+
if route_test.length > @working_generated_code_line_length
|
132
|
+
route_test = generate_route_test_multiple_line(route_test)
|
133
|
+
end
|
134
|
+
route_test
|
135
|
+
end
|
136
|
+
|
137
|
+
def generate_route_test_multiple_line(route_test)
|
138
|
+
route_test.sub("it { ", "it do\n ").gsub(").to(", ").\n to(").gsub(/\)\s\}$/, ")\n end")
|
139
|
+
end
|
140
|
+
|
141
|
+
def generate_route_test_single_line(journey_route, url, request_method, route_parameters_hash)
|
142
|
+
test_comment = route_parameters_hash.extract!(:test_comment)[:test_comment]
|
143
|
+
route_test = " it { is_expected.to route(#{ request_method }, '#{ url }').to(action: #{ journey_route.defaults[:action] }"
|
144
|
+
route_test.concat(compose_route_parameters_string(route_parameters_hash)).concat(") }")
|
145
|
+
if test_comment.present?
|
146
|
+
route_test.sub!(" it { ", " xit { ").prepend(test_comment)
|
147
|
+
end
|
148
|
+
route_test
|
149
|
+
end
|
150
|
+
|
151
|
+
def get_request_methods(journey_route)
|
152
|
+
request_method_regexp = journey_route.constraints[:request_method]
|
153
|
+
request_method_regexp.to_s.scan(/[A-Z]+/)
|
154
|
+
end
|
155
|
+
|
156
|
+
def get_route_url(journey_route, route_parameters_hash)
|
157
|
+
route_url_options_hash = { controller: journey_route.defaults[:controller], action: journey_route.defaults[:action], only_path: true }
|
158
|
+
route_url_options_hash = route_url_options_hash.merge(route_parameters_hash)
|
159
|
+
route_parameters_hash[:test_comment].present? ? "<url required>" : url_for(route_url_options_hash)
|
160
|
+
end
|
161
|
+
|
162
|
+
def handle_route_parameters_which_require_special_handling(route_parameters_hash, route_required_parameter_keys_with_missing_values)
|
163
|
+
parameters_hash_handled = route_parameters_hash
|
164
|
+
parameters_hash_handled[:test_comment] = ""
|
165
|
+
parameter_keys_with_missing_values_handled = route_required_parameter_keys_with_missing_values
|
166
|
+
# CONFIRMATION TODO: Determine if this is the proper way to handle route required ':id' parameter when value not provided
|
167
|
+
if parameter_keys_with_missing_values_handled.include? :id
|
168
|
+
parameters_hash_handled[:id] = 1
|
169
|
+
parameter_keys_with_missing_values_handled.delete(:id)
|
170
|
+
end
|
171
|
+
if parameter_keys_with_missing_values_handled.present?
|
172
|
+
parameters_hash_handled = handle_route_parameters_with_missing_values(parameters_hash_handled, parameter_keys_with_missing_values_handled)
|
173
|
+
end
|
174
|
+
if parameters_hash_handled.values.any? { |route_parameter_value| route_parameter_value.is_a?(Regexp) }
|
175
|
+
parameters_hash_handled = handle_route_parameters_with_regexp_values(parameters_hash_handled)
|
176
|
+
end
|
177
|
+
parameters_hash_handled[:test_comment].concat("# Unable to determine URL without values to still be provided.\n" +
|
178
|
+
"# Remove the 'x' from 'xit' once appropriate values have been supplied.\n")
|
179
|
+
parameters_hash_handled
|
180
|
+
end
|
181
|
+
|
182
|
+
def handle_route_parameters_with_regexp_values(parameters_hash_handled)
|
183
|
+
# CONFIRMATION TODO: Determine if this is the proper way to handle route required parameters that have a regexp value provided
|
184
|
+
parameters_hash_with_regexp_values_handled = add_test_comment_for_route_parameters_with_regexp_values(parameters_hash_handled)
|
185
|
+
add_dummy_values_to_route_parameters_with_regexp_values(parameters_hash_with_regexp_values_handled)
|
186
|
+
end
|
187
|
+
|
188
|
+
def handle_route_parameters_with_missing_values(parameters_hash_handled, parameter_keys_with_missing_values_handled)
|
189
|
+
# CONFIRMATION TODO: Determine if this is the proper way to handle route required parameters that have no value provided
|
190
|
+
parameters_hash_with_missing_values_handled = add_dummy_values_to_route_parameters_with_missing_values(parameters_hash_handled, parameter_keys_with_missing_values_handled)
|
191
|
+
add_test_comment_for_route_parameters_with_missing_values(parameters_hash_with_missing_values_handled, parameter_keys_with_missing_values_handled)
|
192
|
+
end
|
193
|
+
|
194
|
+
def special_handling_of_route_parameters_required(route_parameters_hash, route_required_parameter_keys_with_missing_values)
|
195
|
+
route_required_parameter_keys_with_missing_values.present? ||
|
196
|
+
route_parameters_hash.values.any? { |route_parameter_value| route_parameter_value.is_a?(Regexp) }
|
197
|
+
end
|
198
|
+
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
# module ShouldaMatchmakers
|
208
|
+
# module Controller
|
209
|
+
# module ActionController
|
210
|
+
# module Matchmaker
|
211
|
+
# module Route
|
212
|
+
#
|
213
|
+
#
|
214
|
+
# def route_matcher_tests
|
215
|
+
# if journey_routes_present(@sm_model_action_controller_controller)
|
216
|
+
# generate_route_matcher_tests(@sm_model_action_controller_controller)
|
217
|
+
# else
|
218
|
+
# []
|
219
|
+
# end
|
220
|
+
# end
|
221
|
+
#
|
222
|
+
#
|
223
|
+
# private
|
224
|
+
#
|
225
|
+
# def generate_route_matcher_tests(controller)
|
226
|
+
# route_tests = []
|
227
|
+
# controller_path = get_app_controller_file_path(controller)
|
228
|
+
# controller.constantize.action_methods.sort.each do |action_method|
|
229
|
+
# journey_routes = extract_journey_routes(controller_path, action_method)
|
230
|
+
# journey_routes.each do |journey_route|
|
231
|
+
# single_route_tests = generate_single_route_tests(journey_route)
|
232
|
+
# route_tests = route_tests + single_route_tests
|
233
|
+
# end
|
234
|
+
# end
|
235
|
+
# format_tests(route_tests)
|
236
|
+
# end
|
237
|
+
#
|
238
|
+
# def generate_single_route_tests(journey_route)
|
239
|
+
# single_route_tests = []
|
240
|
+
# route_parameters_hash = generate_route_parameters_hash(journey_route)
|
241
|
+
# test_comment = route_parameters_hash.extract!(:test_comment)[:test_comment]
|
242
|
+
# url_options_hash = { controller: journey_route.defaults[:controller], action: journey_route.defaults[:action], only_path: true }
|
243
|
+
# url_options_hash = url_options_hash.merge(route_parameters_hash)
|
244
|
+
#
|
245
|
+
# ## TRACE.debug " ROUTE PARAMETERS HASH:"
|
246
|
+
# ## TRACE.debug route_parameters_hash.inspect
|
247
|
+
# ## TRACE.debug " URL OPTIONS HASH:"
|
248
|
+
# ## TRACE.debug url_options_hash.inspect
|
249
|
+
#
|
250
|
+
# if test_comment.present?
|
251
|
+
# url = "<url required>"
|
252
|
+
# else
|
253
|
+
# url = url_for(url_options_hash)
|
254
|
+
# end
|
255
|
+
#
|
256
|
+
# # TRACE.debug " URL:"
|
257
|
+
# # TRACE.debug url.inspect
|
258
|
+
#
|
259
|
+
# request_methods = get_request_methods(journey_route)
|
260
|
+
# # CONFIRMATION TODO: Determine if this is the proper way to handle controller actions with multiple HTTP verbs
|
261
|
+
# request_methods.each do |request_method|
|
262
|
+
# single_route_test = generate_route_test_single_line(journey_route, url, request_method, route_parameters_hash, test_comment)
|
263
|
+
# if single_route_test.length > @working_generated_code_line_length
|
264
|
+
# single_route_test = generate_route_test_multiple_line(single_route_test)
|
265
|
+
# end
|
266
|
+
# single_route_tests << single_route_test
|
267
|
+
# end
|
268
|
+
# single_route_tests
|
269
|
+
# end
|
270
|
+
#
|
271
|
+
# def generate_route_test_single_line(journey_route, url, request_method, route_parameters_hash, test_comment)
|
272
|
+
# route_test = " it { is_expected.to route(#{ request_method }, '#{ url }').to(action: #{ journey_route.defaults[:action] }"
|
273
|
+
# route_parameters_hash.each do |param_key, param_value|
|
274
|
+
#
|
275
|
+
# ## TRACE.debug ""
|
276
|
+
# ## TRACE.debug " ROUTE PARAMETERS HASH KEY: " + param_key.inspect
|
277
|
+
# ## TRACE.debug " ROUTE PARAMETERS HASH VALUE: " + param_value.inspect
|
278
|
+
# ## TRACE.debug " ROUTE PARAMETERS HASH VALUE CLASS: " + param_value.class.name.to_s
|
279
|
+
#
|
280
|
+
# route_test.concat(", " + param_key.to_s + ": " + param_value.to_s )
|
281
|
+
# end
|
282
|
+
# # "#{ route_parameters_hash.to_s.gsub("{", "{ ").gsub("{ ", "{ ").gsub("}", " }").gsub(" }", " }") }")
|
283
|
+
# route_test.concat(") }")
|
284
|
+
# if test_comment.present?
|
285
|
+
# route_test.prepend(test_comment)
|
286
|
+
# route_test.sub!(" it { ", " xit { ")
|
287
|
+
# end
|
288
|
+
# route_test
|
289
|
+
# end
|
290
|
+
#
|
291
|
+
# def generate_route_test_multiple_line(route_test)
|
292
|
+
#
|
293
|
+
# ## TRACE.debug ""
|
294
|
+
# ## TRACE.debug " ROUTE TEST:"
|
295
|
+
# ## TRACE.debug route_test.to_s
|
296
|
+
#
|
297
|
+
# route_test.sub("it { ", "it do\n ").gsub(").to(", ").\n to(").gsub(/\)\s\}$/, ")\n end")
|
298
|
+
# end
|
299
|
+
#
|
300
|
+
# def get_request_methods(journey_route)
|
301
|
+
# request_method_regex = journey_route.constraints[:request_method]
|
302
|
+
# request_method_regex.to_s.scan(/[A-Z]+/)
|
303
|
+
# end
|
304
|
+
#
|
305
|
+
# def generate_route_parameters_hash(journey_route)
|
306
|
+
#
|
307
|
+
# # if journey_route.required_keys.size > 2
|
308
|
+
# ## TRACE.debug ""
|
309
|
+
# ## TRACE.debug ""
|
310
|
+
# ## TRACE.debug ""
|
311
|
+
# # ## TRACE.debug " ROUTE APP:"
|
312
|
+
# # ## TRACE.debug journey_route.app.inspect
|
313
|
+
# ## TRACE.debug " ROUTE CONSTRAINTS:"
|
314
|
+
# ## TRACE.debug journey_route.defaults.inspect
|
315
|
+
# # ## TRACE.debug " ROUTE DEFAULTS:"
|
316
|
+
# # ## TRACE.debug journey_route.defaults.inspect
|
317
|
+
# # ## TRACE.debug " ROUTE NAME:"
|
318
|
+
# # ## TRACE.debug journey_route.name.inspect
|
319
|
+
# # ## TRACE.debug " ROUTE PATH:"
|
320
|
+
# # ## TRACE.debug journey_route.path.inspect
|
321
|
+
# # ## TRACE.debug " ROUTE PRECENDENCE:"
|
322
|
+
# # ## TRACE.debug journey_route.precedence.inspect
|
323
|
+
# # ## TRACE.debug " ROUTE OPTIONAL PARTS:"
|
324
|
+
# # ## TRACE.debug journey_route.optional_parts.inspect
|
325
|
+
# # ## TRACE.debug " ROUTE PARTS:"
|
326
|
+
# # ## TRACE.debug journey_route.parts.inspect
|
327
|
+
# # ## TRACE.debug " ROUTE REQUIRED DEFAULTS:"
|
328
|
+
# # ## TRACE.debug journey_route.required_defaults.inspect
|
329
|
+
# ## TRACE.debug " ROUTE REQUIRED KEYS: " + journey_route.required_keys.inspect
|
330
|
+
# # ## TRACE.debug " ROUTE REQUIRED PARTS:"
|
331
|
+
# # ## TRACE.debug journey_route.required_parts.inspect
|
332
|
+
# # # journey_route.required_parts.each do |rp|
|
333
|
+
# # # # TRACE.debug " ROUTE REQUIRED PART: " + rp.to_s
|
334
|
+
# # # # TRACE.debug " ROUTE REQUIRED PART CLASS: " + rp.class.name.to_s
|
335
|
+
# # # end
|
336
|
+
# ## TRACE.debug " ROUTE REQUIREMENTS:"
|
337
|
+
# ## TRACE.debug journey_route.requirements.inspect
|
338
|
+
# # ## TRACE.debug " ROUTE REQUIREMENTS KEYS:"
|
339
|
+
# # ## TRACE.debug journey_route.requirements.keys.inspect
|
340
|
+
# # ## TRACE.debug " ROUTE SEGMENTS:"
|
341
|
+
# # ## TRACE.debug journey_route.segments.inspect
|
342
|
+
# # ## TRACE.debug " ROUTE VERB:"
|
343
|
+
# # ## TRACE.debug journey_route.verb.inspect
|
344
|
+
# #
|
345
|
+
# ## TRACE.debug ""
|
346
|
+
# ## TRACE.debug " ROUTE REQUIRED KEYS WITHOUT VALUES:"
|
347
|
+
# # require_keys_with_missing_values = journey_route.required_keys - journey_route.requirements.keys
|
348
|
+
# ## TRACE.debug require_keys_with_missing_values.inspect
|
349
|
+
# # end
|
350
|
+
#
|
351
|
+
# route_parameters_hash = journey_route.requirements.select { |key, value| journey_route.parts.include? key } || {}
|
352
|
+
# route_required_keys_with_missing_values = journey_route.required_keys - journey_route.requirements.keys
|
353
|
+
# # CONFIRMATION TODO: Determine if this is the proper way to handle route required ':id' parameter when value not provided
|
354
|
+
# if route_required_keys_with_missing_values.include? :id
|
355
|
+
# route_parameters_hash[:id] = 1
|
356
|
+
# route_required_keys_with_missing_values.delete(:id)
|
357
|
+
# end
|
358
|
+
# # CONFIRMATION TODO: Determine if this is the proper way to handle route required parameters that either have a regex or no value provided
|
359
|
+
# route_parameters_hash[:test_comment] = ""
|
360
|
+
# if route_required_keys_with_missing_values.present?
|
361
|
+
# route_required_keys_with_missing_values.each do |key_without_value|
|
362
|
+
# route_parameters_hash[key_without_value] = "'<value required>'"
|
363
|
+
# end
|
364
|
+
# if route_required_keys_with_missing_values.size == 1
|
365
|
+
# route_parameters_hash[:test_comment].concat("# A required value for route parameter ':#{ route_required_keys_with_missing_values[0] }' needs to be provided.\n")
|
366
|
+
# else
|
367
|
+
# route_parameters_hash[:test_comment].concat("# Required values for route parameters '#{ route_required_keys_with_missing_values.to_s }' need to be provided.\n")
|
368
|
+
# end
|
369
|
+
# end
|
370
|
+
# if route_parameters_hash.values.any? { |route_parameter_value| route_parameter_value.is_a?(Regexp) }
|
371
|
+
# route_parameters_hash.each do |param_key, param_value|
|
372
|
+
# if param_value.is_a? Regexp
|
373
|
+
# param_regex_string = param_value.to_s.sub("(?-mix:","").gsub(/\)$/,"")
|
374
|
+
# route_parameters_hash[:test_comment].concat("# A required value for route parameter ':#{ param_key }' matching regexp '#{ param_regex_string }' needs to be provided.\n")
|
375
|
+
# route_parameters_hash[param_key] = "'<value matching regex required>'"
|
376
|
+
# end
|
377
|
+
# end
|
378
|
+
# end
|
379
|
+
# if route_parameters_hash[:test_comment].present?
|
380
|
+
# # if route_parameters_hash[:test_comment].scan("values").count > 0 || route_parameters_hash[:test_comment].scan("# ").count > 1
|
381
|
+
# route_parameters_hash[:test_comment].concat("# Unable to determine URL without values to be provided.\n" +
|
382
|
+
# "# Remove the 'x' from 'xit' once you have supplied the appropriate values.\n")
|
383
|
+
# # else
|
384
|
+
# # route_parameters_hash[:test_comment].concat("# Remove the 'x' from 'xit' once you have supplied the appropriate value.\n")
|
385
|
+
# # end
|
386
|
+
# end
|
387
|
+
#
|
388
|
+
# ## TRACE.debug " ROUTE REQUIREMENTS HASH:"
|
389
|
+
# ## TRACE.debug route_parameters_hash.inspect
|
390
|
+
#
|
391
|
+
# route_parameters_hash
|
392
|
+
# end
|
393
|
+
#
|
394
|
+
# def extract_journey_routes(controller_path, action_method)
|
395
|
+
# Rails.application.routes.routes.routes.select do |route|
|
396
|
+
# if route.defaults.present?
|
397
|
+
# route.defaults[:controller].to_sym == controller_path.to_sym && route.defaults[:action].to_sym == action_method.to_sym
|
398
|
+
# else
|
399
|
+
# false
|
400
|
+
# end
|
401
|
+
# end
|
402
|
+
# end
|
403
|
+
#
|
404
|
+
# def journey_routes_present(controller)
|
405
|
+
# journey_routes_are_present = false
|
406
|
+
# controller_path = get_app_controller_file_path(controller)
|
407
|
+
# controller.constantize.action_methods.map do |action_method|
|
408
|
+
# journey_routes = extract_journey_routes(controller_path, action_method)
|
409
|
+
# journey_routes_are_present = true if journey_routes.present?
|
410
|
+
# end
|
411
|
+
# journey_routes_are_present
|
412
|
+
# end
|
413
|
+
#
|
414
|
+
# end
|
415
|
+
# end
|
416
|
+
# end
|
417
|
+
# end
|
418
|
+
# end
|
419
|
+
|
420
|
+
|
421
|
+
# def route_matcher_tests
|
422
|
+
# if journey_routes_present(@sm_model_action_controller_controller)
|
423
|
+
# generate_route_matcher_tests(@sm_model_action_controller_controller)
|
424
|
+
# else
|
425
|
+
# []
|
426
|
+
# end
|
427
|
+
# end
|
428
|
+
#
|
429
|
+
#
|
430
|
+
# private
|
431
|
+
#
|
432
|
+
# def generate_route_matcher_tests(controller)
|
433
|
+
# route_tests = []
|
434
|
+
# controller_path = get_app_controller_file_path(controller)
|
435
|
+
# controller.constantize.action_methods.map do |action_method|
|
436
|
+
# journey_routes = extract_journey_routes(controller_path, action_method)
|
437
|
+
# journey_routes.each do |journey_route|
|
438
|
+
# single_route_tests = generate_single_route_tests(journey_route, controller_path, action_method)
|
439
|
+
# route_tests = route_tests + single_route_tests
|
440
|
+
# end
|
441
|
+
# end
|
442
|
+
# format_tests(route_tests)
|
443
|
+
# end
|
444
|
+
#
|
445
|
+
# def generate_single_route_tests(journey_route, controller_path, action_method)
|
446
|
+
# single_route_tests = []
|
447
|
+
# route_parameters_hash = generate_route_parameters_hash(journey_route)
|
448
|
+
# url = url_for({controller: controller_path, action: action_method, only_path: true}.merge(route_parameters_hash.except(:test_comment)))
|
449
|
+
# if url.present?
|
450
|
+
# request_methods = get_request_methods(journey_route)
|
451
|
+
# # CONFIRMATION TODO: Determine if this is the proper way to handle controller actions with multiple HTTP verbs
|
452
|
+
# request_methods.each do |request_method|
|
453
|
+
# single_route_test = generate_route_test_single_line(journey_route, url, controller_path, action_method, request_method)
|
454
|
+
# if single_route_test.length > @working_generated_code_line_length
|
455
|
+
# single_route_test = generate_route_test_multiple_line(single_route_test)
|
456
|
+
# end
|
457
|
+
# single_route_tests << single_route_test
|
458
|
+
# end
|
459
|
+
# end
|
460
|
+
# single_route_tests
|
461
|
+
# end
|
462
|
+
#
|
463
|
+
# def generate_route_test_single_line(journey_route, url, controller_path, action_method, request_method)
|
464
|
+
# route_parameters_hash = generate_route_parameters_hash(journey_route)
|
465
|
+
# route_test = " it { is_expected.to route(#{ request_method }, '#{ url }').to('#{ controller_path }##{ action_method }'"
|
466
|
+
# if route_parameters_hash.present?
|
467
|
+
# route_test.concat(", #{ route_parameters_hash.except(:test_comment).to_s.gsub("{", "{ ").gsub("{ ", "{ ").gsub("}", " }").gsub(" }", " }") }")
|
468
|
+
# end
|
469
|
+
# route_test.concat(") }")
|
470
|
+
# if route_parameters_hash[:test_comment] =~ /#\s/
|
471
|
+
# route_test.prepend(route_parameters_hash[:test_comment])
|
472
|
+
# end
|
473
|
+
# route_test
|
474
|
+
# end
|
475
|
+
#
|
476
|
+
# def generate_route_test_multiple_line(route_test)
|
477
|
+
# route_test.gsub!(" it { ", " it do\n ").gsub!(").to(", ").\n to(").gsub!(/\)\s\}$/, ")\n end")
|
478
|
+
# end
|
479
|
+
#
|
480
|
+
# def get_request_methods(journey_route)
|
481
|
+
# request_method_regex = journey_route.constraints[:request_method]
|
482
|
+
# request_method_regex.to_s.scan(/[A-Z]+/)
|
483
|
+
# end
|
484
|
+
#
|
485
|
+
# def generate_route_parameters_hash(journey_route)
|
486
|
+
#
|
487
|
+
# if journey_route.required_keys.size > 2
|
488
|
+
# # TRACE.debug ""
|
489
|
+
# # TRACE.debug ""
|
490
|
+
# # TRACE.debug ""
|
491
|
+
# ## TRACE.debug " ROUTE APP:"
|
492
|
+
# ## TRACE.debug journey_route.app.inspect
|
493
|
+
# # TRACE.debug " ROUTE CONSTRAINTS:"
|
494
|
+
# # TRACE.debug journey_route.defaults.inspect
|
495
|
+
# ## TRACE.debug " ROUTE DEFAULTS:"
|
496
|
+
# ## TRACE.debug journey_route.defaults.inspect
|
497
|
+
# ## TRACE.debug " ROUTE NAME:"
|
498
|
+
# ## TRACE.debug journey_route.name.inspect
|
499
|
+
# ## TRACE.debug " ROUTE PATH:"
|
500
|
+
# ## TRACE.debug journey_route.path.inspect
|
501
|
+
# ## TRACE.debug " ROUTE PRECENDENCE:"
|
502
|
+
# ## TRACE.debug journey_route.precedence.inspect
|
503
|
+
# ## TRACE.debug " ROUTE OPTIONAL PARTS:"
|
504
|
+
# ## TRACE.debug journey_route.optional_parts.inspect
|
505
|
+
# ## TRACE.debug " ROUTE PARTS:"
|
506
|
+
# ## TRACE.debug journey_route.parts.inspect
|
507
|
+
# ## TRACE.debug " ROUTE REQUIRED DEFAULTS:"
|
508
|
+
# ## TRACE.debug journey_route.required_defaults.inspect
|
509
|
+
# # TRACE.debug " ROUTE REQUIRED KEYS: " + journey_route.required_keys.inspect
|
510
|
+
# ## TRACE.debug " ROUTE REQUIRED PARTS:"
|
511
|
+
# ## TRACE.debug journey_route.required_parts.inspect
|
512
|
+
# # journey_route.required_parts.each do |rp|
|
513
|
+
# # # TRACE.debug " ROUTE REQUIRED PART: " + rp.to_s
|
514
|
+
# # # TRACE.debug " ROUTE REQUIRED PART CLASS: " + rp.class.name.to_s
|
515
|
+
# # end
|
516
|
+
# # TRACE.debug " ROUTE REQUIREMENTS:"
|
517
|
+
# # TRACE.debug journey_route.requirements.inspect
|
518
|
+
# ## TRACE.debug " ROUTE REQUIREMENTS KEYS:"
|
519
|
+
# ## TRACE.debug journey_route.requirements.keys.inspect
|
520
|
+
# ## TRACE.debug " ROUTE SEGMENTS:"
|
521
|
+
# ## TRACE.debug journey_route.segments.inspect
|
522
|
+
# ## TRACE.debug " ROUTE VERB:"
|
523
|
+
# ## TRACE.debug journey_route.verb.inspect
|
524
|
+
#
|
525
|
+
# # TRACE.debug ""
|
526
|
+
# # TRACE.debug " ROUTE REQUIRED KEYS WITHOUT VALUES:"
|
527
|
+
# require_keys_with_missing_values = journey_route.required_keys - journey_route.requirements.keys
|
528
|
+
# # TRACE.debug require_keys_with_missing_values.inspect
|
529
|
+
#
|
530
|
+
#
|
531
|
+
# end
|
532
|
+
#
|
533
|
+
# route_parameters_hash = journey_route.requirements.select { |key, value| journey_route.parts.include? key }
|
534
|
+
# route_required_keys_with_missing_values = journey_route.required_keys - journey_route.requirements.keys
|
535
|
+
# if route_required_keys_with_missing_values.include? :id
|
536
|
+
# route_parameters_hash[:id] = 1
|
537
|
+
# route_required_keys_with_missing_values.delete(:id)
|
538
|
+
# end
|
539
|
+
# # CONFIRMATION TODO: Determine if this is the proper way to handle route parts that do no have a required value provided
|
540
|
+
# if route_required_keys_with_missing_values.present?
|
541
|
+
# route_required_keys_with_missing_values.each do |key_without_value|
|
542
|
+
# route_parameters_hash[key_without_value] = "<value required>"
|
543
|
+
# end
|
544
|
+
# if route_required_keys_with_missing_values.size == 1
|
545
|
+
# route_parameters_hash[:test_comment] = "# A required value needs to be provided for route part '#{ route_required_keys_with_missing_values[0] }'.\n" +
|
546
|
+
# "# Remove the 'x' from 'xit' once you have supplied an appropriate value.\n"
|
547
|
+
# else
|
548
|
+
# route_parameters_hash[:test_comment] = "# Required values need to be provided for route parts '#{ route_required_keys_with_missing_values.to_s }'.\n" +
|
549
|
+
# "# Remove the 'x' from 'xit' once you have supplied the appropriate values.\n"
|
550
|
+
# end
|
551
|
+
# else
|
552
|
+
# route_parameters_hash[:test_comment] = ""
|
553
|
+
# end
|
554
|
+
#
|
555
|
+
# # journey_route.requirements.each do |req_key, req_value |
|
556
|
+
# # route_parameters_hash[req_key] = req_value
|
557
|
+
# # end
|
558
|
+
# # if journey_route.defaults[:format]
|
559
|
+
# # route_parameters_hash.reverse_merge!({ format: journey_route.defaults[:format] })
|
560
|
+
# # end
|
561
|
+
#
|
562
|
+
# # TRACE.debug " ROUTE REQUIREMENTS HASH:"
|
563
|
+
# # TRACE.debug route_parameters_hash.inspect
|
564
|
+
#
|
565
|
+
# route_parameters_hash
|
566
|
+
# end
|
567
|
+
#
|
568
|
+
# def extract_journey_routes(controller_path, action_method)
|
569
|
+
# Rails.application.routes.routes.routes.select do |route|
|
570
|
+
# if route.defaults.present?
|
571
|
+
# route.defaults[:controller].to_sym == controller_path.to_sym && route.defaults[:action].to_sym == action_method.to_sym
|
572
|
+
# else
|
573
|
+
# false
|
574
|
+
# end
|
575
|
+
# end
|
576
|
+
# end
|
577
|
+
#
|
578
|
+
# def journey_routes_present(controller)
|
579
|
+
# journey_routes_are_present = false
|
580
|
+
# controller_path = get_app_controller_file_path(controller)
|
581
|
+
# controller.constantize.action_methods.map do |action_method|
|
582
|
+
# journey_routes = extract_journey_routes(controller_path, action_method)
|
583
|
+
# journey_routes_are_present = true if journey_routes.present?
|
584
|
+
# end
|
585
|
+
# journey_routes_are_present
|
586
|
+
# end
|