diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e33dad..aaf54a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Development +* Fix a bug where nested example groups in `RSpec.describe` are wrongly converted to non-monkey-patch form when dynamic analysis is skipped. ([#89](https://github.com/yujinakayama/transpec/issues/89)) + ## v2.3.6 * Fix error `Unresolved dependency found during sorting - parser` on `gem install transpec`. diff --git a/lib/transpec/static_context_inspector.rb b/lib/transpec/static_context_inspector.rb index e879771..e9de78f 100644 --- a/lib/transpec/static_context_inspector.rb +++ b/lib/transpec/static_context_inspector.rb @@ -101,12 +101,19 @@ def scope_type(node) end end - def special_block_type(block_node) + def special_block_type(block_node) # rubocop:disable MethodLength, CyclomaticComplexity send_node = block_node.children.first receiver_node, method_name, *_ = *send_node - if const_name(receiver_node) == 'RSpec' && method_name == :configure - :rspec_configure + if const_name(receiver_node) == 'RSpec' + case method_name + when :configure + :rspec_configure + when *EXAMPLE_GROUP_METHODS + :example_group + else + nil + end elsif HOOK_METHODS.include?(method_name) hook_type(send_node) elsif receiver_node diff --git a/spec/transpec/static_context_inspector_spec.rb b/spec/transpec/static_context_inspector_spec.rb index edab88c..aed3361 100644 --- a/spec/transpec/static_context_inspector_spec.rb +++ b/spec/transpec/static_context_inspector_spec.rb @@ -49,6 +49,10 @@ def some_method(some_arg) end end + RSpec.describe 'something' do + in_rspec_describe + end + feature 'Capybara DSL' do background do in_background @@ -118,6 +122,10 @@ def some_method(some_arg) 'send nil :in_block', 'in normal block in #describe in module', [:module] + ], [ + 'send nil :in_rspec_describe', + 'in RSpec.describe', + [:example_group] ], [ 'send nil :in_background', 'in #background block in #feature', diff --git a/spec/transpec/syntax/example_group_spec.rb b/spec/transpec/syntax/example_group_spec.rb index 4c694b5..ef3b108 100644 --- a/spec/transpec/syntax/example_group_spec.rb +++ b/spec/transpec/syntax/example_group_spec.rb @@ -126,6 +126,42 @@ module SomeModule end end + context 'when the #describe is in another RSpec.describe' do + include_context 'multiple #describes' + + let(:source) do + <<-END + RSpec.describe 'something' do + describe '#some_method' do + end + end + END + end + + let(:expected_source) do + <<-END + RSpec.describe 'something' do + describe '#some_method' do + end + end + END + end + + context 'without runtime information' do + it 'does nothing' do + rewritten_source.should == source + end + end + + context 'with runtime information' do + include_context 'dynamic analysis objects' + + it 'does nothing' do + rewritten_source.should == source + end + end + end + context 'when logical-inner #describe is placed outside of the outer #describe in source' do include_context 'multiple #describes'