Skip to content

Commit db8a130

Browse files
committed
extract format for rails/convert_rails_test_request_methods_4_2_to_5_0
1 parent cdc7dbe commit db8a130

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

lib/rails/convert_rails_test_request_methods_4_2_to_5_0.rb

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
functional test:
88
99
```ruby
10-
get :show, { id: user.id }, { notice: 'Welcome' }, { admin: user.admin? }
10+
get :show, { id: user.id, format: :json }, { notice: 'Welcome' }, { admin: user.admin? }
1111
```
1212
1313
=>
1414
1515
```ruby
16-
get :show, params: { id: user.id }, flash: { notice: 'Welcome' }, session: { admin: user.admin? }.
16+
get :show, params: { id: user.id }, flash: { notice: 'Welcome' }, session: { admin: user.admin? }, as: :json
1717
```
1818
1919
integration test:
@@ -29,13 +29,17 @@
2929
```
3030
EOS
3131

32+
if_gem 'rails', { gte: '5.0' }
33+
3234
helper_method :make_up_hash_pair do |key, argument_node|
33-
if argument_node.to_source != 'nil'
34-
if argument_node.type == :hash
35-
"#{key}: #{add_curly_brackets_if_necessary(argument_node.to_source)}"
36-
else
37-
"#{key}: #{argument_node.to_source}"
38-
end
35+
next if argument_node.to_source == 'nil'
36+
37+
if argument_node.type == :hash
38+
new_value = argument_node.children.reject { |pair_node| pair_node.key.to_value == :format }
39+
.map(&:to_source).join(', ')
40+
"#{key}: #{add_curly_brackets_if_necessary(new_value)}"
41+
else
42+
"#{key}: #{argument_node.to_source}"
3943
end
4044
end
4145

@@ -47,12 +51,14 @@
4751
with_node type: 'send', message: message do
4852
next unless node.arguments.size > 1
4953
next unless node.arguments[1].type == :hash
50-
next if node.arguments[1].has_key?(:params)
54+
next if node.arguments[1].key?(:params)
5155

56+
format_value = node.arguments[1].hash_value(:format)
5257
options = []
5358
options << make_up_hash_pair('params', node.arguments[1])
5459
options << make_up_hash_pair('flash', node.arguments[2]) if node.arguments.size > 2
5560
options << make_up_hash_pair('session', node.arguments[3]) if node.arguments.size > 3
61+
options << "as: #{format_value.to_source}" if format_value
5662
replace_with "#{message} {{arguments.first}}, #{options.compact.join(', ')}"
5763
end
5864
end
@@ -66,7 +72,7 @@
6672
with_node type: 'send', message: message do
6773
next unless node.arguments.size > 1
6874
if node.arguments[1].type == :hash &&
69-
(node.arguments[1].has_key?(:params) || node.arguments[1].has_key?(:headers))
75+
(node.arguments[1].key?(:params) || node.arguments[1].key?(:headers))
7076
next
7177
end
7278

spec/rails/convert_rails_test_request_methods_4_2_to_5_0_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"
1212
class PostsControllerTest < ActionController::TestCase
1313
def test_show
14-
get :show, { id: user.id }, { notice: 'Welcome' }, { admin: user.admin? }
14+
get :show, { id: user.id, format: :json }, { notice: 'Welcome' }, { admin: user.admin? }
1515
end
1616
1717
def test_index
@@ -32,7 +32,7 @@ def test_destroy
3232
"
3333
class PostsControllerTest < ActionController::TestCase
3434
def test_show
35-
get :show, params: { id: user.id }, flash: { notice: 'Welcome' }, session: { admin: user.admin? }
35+
get :show, params: { id: user.id }, flash: { notice: 'Welcome' }, session: { admin: user.admin? }, as: :json
3636
end
3737
3838
def test_index

0 commit comments

Comments
 (0)