From 4216c38779fa53da0a471822286557cc97521b82 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Mon, 24 May 2021 19:57:57 +0900 Subject: [PATCH] Support `in` pattern syntax for `Layout/CaseIndentation` This PR supports Ruby 2.7's `in` pattern syntax for `Layout/CaseIndentation`. It is extending `Layout/CaseIndentation` cop because I think it's expected that the indentation layout will be the same for `case ... when` and `case ... in`. --- ...tern_syntax_for_layout_case_indentation.md | 1 + config/default.yml | 3 +- lib/rubocop/cop/layout/case_indentation.rb | 66 +- .../cop/layout/case_indentation_spec.rb | 959 +++++++++++++----- 4 files changed, 741 insertions(+), 288 deletions(-) create mode 100644 changelog/new_support_in_pattern_syntax_for_layout_case_indentation.md diff --git a/changelog/new_support_in_pattern_syntax_for_layout_case_indentation.md b/changelog/new_support_in_pattern_syntax_for_layout_case_indentation.md new file mode 100644 index 00000000000..a1e8f291245 --- /dev/null +++ b/changelog/new_support_in_pattern_syntax_for_layout_case_indentation.md @@ -0,0 +1 @@ +* [#9818](https://github.com/rubocop/rubocop/pull/9818): Support Ruby 2.7's `in` pattern syntax for `Layout/CaseIndentation`. ([@koic][]) diff --git a/config/default.yml b/config/default.yml index 9e79746ca22..8372c36860c 100644 --- a/config/default.yml +++ b/config/default.yml @@ -377,10 +377,11 @@ Layout/BlockEndNewline: VersionAdded: '0.49' Layout/CaseIndentation: - Description: 'Indentation of when in a case/when/[else/]end.' + Description: 'Indentation of when in a case/(when|in)/[else/]end.' StyleGuide: '#indent-when-to-case' Enabled: true VersionAdded: '0.49' + VersionChanged: '<>' EnforcedStyle: case SupportedStyles: - case diff --git a/lib/rubocop/cop/layout/case_indentation.rb b/lib/rubocop/cop/layout/case_indentation.rb index 5ae835c4ec9..e0120ff3e4b 100644 --- a/lib/rubocop/cop/layout/case_indentation.rb +++ b/lib/rubocop/cop/layout/case_indentation.rb @@ -3,10 +3,10 @@ module RuboCop module Cop module Layout - # This cop checks how the ``when``s of a `case` expression + # This cop checks how the `when` and `in`s of a `case` expression # are indented in relation to its `case` or `end` keyword. # - # It will register a separate offense for each misaligned `when`. + # It will register a separate offense for each misaligned `when` and `in`. # # @example # # If Layout/EndAlignment is set to keyword style (default) @@ -22,6 +22,13 @@ module Layout # y / 3 # end # + # case n + # in pattern + # x * 2 + # else + # y / 3 + # end + # # # good for all styles # case n # when 0 @@ -30,6 +37,13 @@ module Layout # y / 3 # end # + # case n + # in pattern + # x * 2 + # else + # y / 3 + # end + # # @example EnforcedStyle: case (default) # # if EndAlignment is set to other style such as # # start_of_line (as shown below), then *when* alignment @@ -43,6 +57,13 @@ module Layout # y / 3 # end # + # a = case n + # in pattern + # x * 2 + # else + # y / 3 + # end + # # # good # a = case n # when 0 @@ -51,6 +72,13 @@ module Layout # y / 3 # end # + # a = case n + # in pattern + # x * 2 + # else + # y / 3 + # end + # # @example EnforcedStyle: end # # bad # a = case n @@ -60,6 +88,13 @@ module Layout # y / 3 # end # + # a = case n + # in pattern + # x * 2 + # else + # y / 3 + # end + # # # good # a = case n # when 0 @@ -67,30 +102,43 @@ module Layout # else # y / 3 # end + # + # a = case n + # in pattern + # x * 2 + # else + # y / 3 + # end class CaseIndentation < Base include Alignment include ConfigurableEnforcedStyle include RangeHelp extend AutoCorrector - MSG = 'Indent `when` %s `%s`.' + MSG = 'Indent `%s` %s `%s`.' def on_case(case_node) return if case_node.single_line? - case_node.each_when { |when_node| check_when(when_node) } + case_node.each_when { |when_node| check_when(when_node, 'when') } + end + + def on_case_match(case_match_node) + return if case_match_node.single_line? + + case_match_node.each_in_pattern { |in_pattern_node| check_when(in_pattern_node, 'in') } end private - def check_when(when_node) + def check_when(when_node, branch_type) when_column = when_node.loc.keyword.column base_column = base_column(when_node.parent, style) if when_column == base_column + indentation_width correct_style_detected else - incorrect_style(when_node) + incorrect_style(when_node, branch_type) end end @@ -102,9 +150,9 @@ def indentation_width indent_one_step? ? configured_indentation_width : 0 end - def incorrect_style(when_node) + def incorrect_style(when_node, branch_type) depth = indent_one_step? ? 'one step more than' : 'as deep as' - message = format(MSG, depth: depth, base: style) + message = format(MSG, branch_type: branch_type, depth: depth, base: style) add_offense(when_node.loc.keyword, message: message) do |corrector| detect_incorrect_style(when_node) @@ -141,7 +189,7 @@ def whitespace_range(node) end def replacement(node) - case_node = node.each_ancestor(:case).first + case_node = node.each_ancestor(:case, :case_match).first base_type = cop_config[style_parameter_name] == 'end' ? :end : :case column = base_column(case_node, base_type) diff --git a/spec/rubocop/cop/layout/case_indentation_spec.rb b/spec/rubocop/cop/layout/case_indentation_spec.rb index db03860c737..4c0113a8aab 100644 --- a/spec/rubocop/cop/layout/case_indentation_spec.rb +++ b/spec/rubocop/cop/layout/case_indentation_spec.rb @@ -12,294 +12,588 @@ context 'with IndentOneStep: false' do let(:cop_config) { { 'EnforcedStyle' => 'case', 'IndentOneStep' => false } } - context 'with everything on a single line' do - it 'does not register an offense' do - expect_no_offenses('case foo; when :bar then 1; else 0; end') + describe '`case` ... `when`' do + context 'with everything on a single line' do + it 'does not register an offense' do + expect_no_offenses('case foo; when :bar then 1; else 0; end') + end end - end - context 'regarding assignment where the right hand side is a case' do - it 'accepts a correctly indented assignment' do - expect_no_offenses(<<~RUBY) - output = case variable - when 'value1' - 'output1' - else - 'output2' - end - RUBY + context 'regarding assignment where the right hand side is a `case`' do + it 'accepts a correctly indented assignment' do + expect_no_offenses(<<~RUBY) + output = case variable + when 'value1' + 'output1' + else + 'output2' + end + RUBY + end + + it 'registers an offense and corrects assignment indented as end' do + expect_offense(<<~RUBY) + output = case variable + when 'value1' + ^^^^ Indent `when` as deep as `case`. + 'output1' + else + 'output2' + end + RUBY + + expect_correction(<<~RUBY) + output = case variable + when 'value1' + 'output1' + else + 'output2' + end + RUBY + end + + it 'registers an offense and corrects assignment indented some other way' do + expect_offense(<<~RUBY) + output = case variable + when 'value1' + ^^^^ Indent `when` as deep as `case`. + 'output1' + else + 'output2' + end + RUBY + + expect_correction(<<~RUBY) + output = case variable + when 'value1' + 'output1' + else + 'output2' + end + RUBY + end + + it 'registers an offense and corrects correct + opposite style' do + expect_offense(<<~RUBY) + output = case variable + when 'value1' + 'output1' + else + 'output2' + end + output = case variable + when 'value1' + ^^^^ Indent `when` as deep as `case`. + 'output1' + else + 'output2' + end + RUBY + + expect_correction(<<~RUBY) + output = case variable + when 'value1' + 'output1' + else + 'output2' + end + output = case variable + when 'value1' + 'output1' + else + 'output2' + end + RUBY + end end - it 'registers an offense and corrects assignment indented as end' do + it 'registers an offense and corrects a `when` clause that is indented deeper than `case`' do expect_offense(<<~RUBY) - output = case variable - when 'value1' - ^^^^ Indent `when` as deep as `case`. - 'output1' - else - 'output2' + case a + when 0 then return + ^^^^ Indent `when` as deep as `case`. + else + case b + when 1 then return + ^^^^ Indent `when` as deep as `case`. + end end RUBY expect_correction(<<~RUBY) - output = case variable - when 'value1' - 'output1' - else - 'output2' + case a + when 0 then return + else + case b + when 1 then return + end end RUBY end - it 'registers an offense and corrects assignment indented some other way' do - expect_offense(<<~RUBY) - output = case variable - when 'value1' - ^^^^ Indent `when` as deep as `case`. - 'output1' - else - 'output2' + it "accepts a `when` clause that's equally indented with `case`" do + expect_no_offenses(<<~RUBY) + y = case a + when 0 then break + when 0 then return + else + z = case b + when 1 then return + when 1 then break + end + end + case c + when 2 then encoding end RUBY + end - expect_correction(<<~RUBY) - output = case variable - when 'value1' - 'output1' - else - 'output2' + it "doesn't get confused by strings with `case` in them" do + expect_no_offenses(<<~RUBY) + a = "case" + case x + when 0 end RUBY end - it 'registers an offense and corrects correct + opposite style' do - expect_offense(<<~RUBY) - output = case variable - when 'value1' - 'output1' - else - 'output2' - end - output = case variable - when 'value1' - ^^^^ Indent `when` as deep as `case`. - 'output1' - else - 'output2' + it "doesn't get confused by symbols named `case` or `when`" do + expect_no_offenses(<<~RUBY) + KEYWORDS = { :case => true, :when => true } + case type + when 0 + ParameterNode + when 1 + MethodCallNode end RUBY + end - expect_correction(<<~RUBY) - output = case variable - when 'value1' - 'output1' - else - 'output2' - end - output = case variable - when 'value1' - 'output1' - else - 'output2' + it 'accepts correctly indented whens in complex combinations' do + expect_no_offenses(<<~RUBY) + each { + case state + when 0 + case name + when :a + end + when 1 + loop { + case name + when :b + end + } + end + } + case s + when Array end RUBY end end - it 'registers an offense and corrects a when clause that is indented deeper than case' do - expect_offense(<<~RUBY) - case a - when 0 then return - ^^^^ Indent `when` as deep as `case`. - else - case b - when 1 then return - ^^^^ Indent `when` as deep as `case`. - end + describe '`case` ... `in`', :ruby27 do + context 'with everything on a single line' do + it 'does not register an offense' do + expect_no_offenses('case foo; in pattern then 1; else 0; end') end - RUBY + end - expect_correction(<<~RUBY) - case a - when 0 then return - else - case b - when 1 then return - end + context 'regarding assignment where the right hand side is a `case`' do + it 'accepts a correctly indented assignment' do + expect_no_offenses(<<~RUBY) + output = case variable + in pattern + 'output1' + else + 'output2' + end + RUBY end - RUBY - end - it "accepts a when clause that's equally indented with case" do - expect_no_offenses(<<~RUBY) - y = case a - when 0 then break - when 0 then return + it 'registers an offense and corrects assignment indented as `end`' do + expect_offense(<<~RUBY) + output = case variable + in pattern + ^^ Indent `in` as deep as `case`. + 'output1' else - z = case b - when 1 then return - when 1 then break - end + 'output2' + end + RUBY + + expect_correction(<<~RUBY) + output = case variable + in pattern + 'output1' + else + 'output2' end - case c - when 2 then encoding + RUBY end - RUBY - end - it "doesn't get confused by strings with case in them" do - expect_no_offenses(<<~RUBY) - a = "case" - case x - when 0 + it 'registers an offense and corrects assignment indented some other way' do + expect_offense(<<~RUBY) + output = case variable + in pattern + ^^ Indent `in` as deep as `case`. + 'output1' + else + 'output2' + end + RUBY + + expect_correction(<<~RUBY) + output = case variable + in pattern + 'output1' + else + 'output2' + end + RUBY end - RUBY - end - it "doesn't get confused by symbols named case or when" do - expect_no_offenses(<<~RUBY) - KEYWORDS = { :case => true, :when => true } - case type - when 0 - ParameterNode - when 1 - MethodCallNode + it 'registers an offense and corrects correct + opposite style' do + expect_offense(<<~RUBY) + output = case variable + in pattern + 'output1' + else + 'output2' + end + output = case variable + in pattern + ^^ Indent `in` as deep as `case`. + 'output1' + else + 'output2' + end + RUBY + + expect_correction(<<~RUBY) + output = case variable + in pattern + 'output1' + else + 'output2' + end + output = case variable + in pattern + 'output1' + else + 'output2' + end + RUBY end - RUBY - end + end + + it 'registers an offense and corrects an `in` clause that is indented deeper than `case`' do + expect_offense(<<~RUBY) + case a + in 0 then return + ^^ Indent `in` as deep as `case`. + else + case b + in 1 then return + ^^ Indent `in` as deep as `case`. + end + end + RUBY + + expect_correction(<<~RUBY) + case a + in 0 then return + else + case b + in 1 then return + end + end + RUBY + end - it 'accepts correctly indented whens in complex combinations' do - expect_no_offenses(<<~RUBY) - each { - case state + it "accepts an `in` clause that's equally indented with `case`" do + expect_no_offenses(<<~RUBY) + y = case a + in 0 then break + in 0 then return + else + z = case b + in 1 then return + in 1 then break + end + end + case c + in 2 then encoding + end + RUBY + end + + it "doesn't get confused by strings with `case` in them" do + expect_no_offenses(<<~RUBY) + a = "case" + case x when 0 - case name - when :a - end - when 1 - loop { + end + RUBY + end + + it "doesn't get confused by symbols named `case` or `in`" do + expect_no_offenses(<<~RUBY) + KEYWORDS = { :case => true, :in => true } + case type + in 0 + ParameterNode + in 1 + MethodCallNode + end + RUBY + end + + it 'accepts correctly indented whens in complex combinations' do + expect_no_offenses(<<~RUBY) + each { + case state + in 0 case name - when :b + in :a end - } + in 1 + loop { + case name + in :b + end + } + end + } + case s + in Array end - } - case s - when Array - end - RUBY + RUBY + end end end context 'with IndentOneStep: true' do let(:cop_config) { { 'EnforcedStyle' => 'case', 'IndentOneStep' => true } } - context 'with everything on a single line' do - it 'does not register an offense' do - expect_no_offenses('case foo; when :bar then 1; else 0; end') + describe '`case` ... `when`' do + context 'with everything on a single line' do + it 'does not register an offense' do + expect_no_offenses('case foo; when :bar then 1; else 0; end') + end end - end - context 'regarding assignment where the right hand side is a case' do - it 'accepts a correctly indented assignment' do - expect_no_offenses(<<~RUBY) - output = case variable + context 'regarding assignment where the right hand side is a `case`' do + it 'accepts a correctly indented assignment' do + expect_no_offenses(<<~RUBY) + output = case variable + when 'value1' + 'output1' + else + 'output2' + end + RUBY + end + + it 'registers an offense and corrects an assignment indented some other way' do + expect_offense(<<~RUBY) + output = case variable when 'value1' + ^^^^ Indent `when` one step more than `case`. 'output1' else 'output2' - end + end + RUBY + + expect_correction(<<~RUBY) + output = case variable + when 'value1' + 'output1' + else + 'output2' + end + RUBY + end + end + + it "accepts a `when` clause that's 2 spaces deeper than `case`" do + expect_no_offenses(<<~RUBY) + case a + when 0 then return + else + case b + when 1 then return + end + end RUBY end - it 'registers an offense and corrects an assignment indented some other way' do + it 'registers an offense and corrects a `when` clause that is equally indented with `case`' do expect_offense(<<~RUBY) - output = case variable - when 'value1' - ^^^^ Indent `when` one step more than `case`. - 'output1' - else - 'output2' - end + y = case a + when 0 then break + ^^^^ Indent `when` one step more than `case`. + when 0 then return + ^^^^ Indent `when` one step more than `case`. + z = case b + when 1 then return + ^^^^ Indent `when` one step more than `case`. + when 1 then break + ^^^^ Indent `when` one step more than `case`. + end + end + case c + when 2 then encoding + ^^^^ Indent `when` one step more than `case`. + end RUBY expect_correction(<<~RUBY) - output = case variable - when 'value1' - 'output1' - else - 'output2' - end + y = case a + when 0 then break + when 0 then return + z = case b + when 1 then return + when 1 then break + end + end + case c + when 2 then encoding + end RUBY end - end - it "accepts a when clause that's 2 spaces deeper than case" do - expect_no_offenses(<<~RUBY) - case a - when 0 then return - else - case b - when 1 then return - end + context 'when indentation width is overridden for this cop only' do + let(:cop_config) do + { + 'EnforcedStyle' => 'case', + 'IndentOneStep' => true, + 'IndentationWidth' => 5 + } + end + + it 'respects cop-specific IndentationWidth' do + expect_no_offenses(<<~RUBY) + output = case variable + when 'value1' + 'output1' + else + 'output2' + end + RUBY end - RUBY + end end - it 'registers an offense and corrects a when clause that is equally indented with case' do - expect_offense(<<~RUBY) - y = case a - when 0 then break - ^^^^ Indent `when` one step more than `case`. - when 0 then return - ^^^^ Indent `when` one step more than `case`. - z = case b - when 1 then return - ^^^^ Indent `when` one step more than `case`. - when 1 then break - ^^^^ Indent `when` one step more than `case`. - end - end - case c - when 2 then encoding - ^^^^ Indent `when` one step more than `case`. + describe '`case` ... `in`', :ruby27 do + context 'with everything on a single line' do + it 'does not register an offense' do + expect_no_offenses('case foo; in pattern then 1; else 0; end') end - RUBY + end - expect_correction(<<~RUBY) - y = case a - when 0 then break - when 0 then return - z = case b - when 1 then return - when 1 then break - end - end - case c - when 2 then encoding + context 'regarding assignment where the right hand side is a `case`' do + it 'accepts a correctly indented assignment' do + expect_no_offenses(<<~RUBY) + output = case variable + in pattern + 'output1' + else + 'output2' + end + RUBY end - RUBY - end - context 'when indentation width is overridden for this cop only' do - let(:cop_config) do - { - 'EnforcedStyle' => 'case', - 'IndentOneStep' => true, - 'IndentationWidth' => 5 - } - end + it 'registers an offense and corrects an assignment indented some other way' do + expect_offense(<<~RUBY) + output = case variable + in pattern + ^^ Indent `in` one step more than `case`. + 'output1' + else + 'output2' + end + RUBY - it 'respects cop-specific IndentationWidth' do - expect_no_offenses(<<~RUBY) - output = case variable - when 'value1' + expect_correction(<<~RUBY) + output = case variable + in pattern 'output1' - else + else 'output2' - end + end + RUBY + end + end + + it "accepts an `in` clause that's 2 spaces deeper than `case`" do + expect_no_offenses(<<~RUBY) + case a + in 0 then return + else + case b + in 1 then return + end + end + RUBY + end + + it 'registers an offense and corrects an `in` clause that is equally indented with `case`' do + expect_offense(<<~RUBY) + y = case a + in 0 then break + ^^ Indent `in` one step more than `case`. + in 0 then return + ^^ Indent `in` one step more than `case`. + z = case b + in 1 then return + ^^ Indent `in` one step more than `case`. + in 1 then break + ^^ Indent `in` one step more than `case`. + end + end + case c + in 2 then encoding + ^^ Indent `in` one step more than `case`. + end + RUBY + + expect_correction(<<~RUBY) + y = case a + in 0 then break + in 0 then return + z = case b + in 1 then return + in 1 then break + end + end + case c + in 2 then encoding + end RUBY end + + context 'when indentation width is overridden for this cop only' do + let(:cop_config) do + { + 'EnforcedStyle' => 'case', + 'IndentOneStep' => true, + 'IndentationWidth' => 5 + } + end + + it 'respects cop-specific IndentationWidth' do + expect_no_offenses(<<~RUBY) + output = case variable + in pattern + 'output1' + else + 'output2' + end + RUBY + end + end end end end @@ -308,43 +602,87 @@ context 'with IndentOneStep: false' do let(:cop_config) { { 'EnforcedStyle' => 'end', 'IndentOneStep' => false } } - context 'with everything on a single line' do - it 'does not register an offense' do - expect_no_offenses('case foo; when :bar then 1; else 0; end') - end - end - - context 'regarding assignment where the right hand side is a case' do - it 'accepts a correctly indented assignment' do - expect_no_offenses(<<~RUBY) - output = case variable - when 'value1' - 'output1' - else - 'output2' - end - RUBY + describe '`case` ... `when`' do + context 'with everything on a single line' do + it 'does not register an offense' do + expect_no_offenses('case foo; when :bar then 1; else 0; end') + end end - it 'registers an offense and corrects an assignment indented some other way' do - expect_offense(<<~RUBY) - output = case variable + context 'regarding assignment where the right hand side is a `case`' do + it 'accepts a correctly indented assignment' do + expect_no_offenses(<<~RUBY) + output = case variable when 'value1' - ^^^^ Indent `when` as deep as `end`. 'output1' else 'output2' - end - RUBY + end + RUBY + end - expect_correction(<<~RUBY) - output = case variable - when 'value1' + it 'registers an offense and corrects an assignment indented some other way' do + expect_offense(<<~RUBY) + output = case variable + when 'value1' + ^^^^ Indent `when` as deep as `end`. + 'output1' + else + 'output2' + end + RUBY + + expect_correction(<<~RUBY) + output = case variable + when 'value1' + 'output1' + else + 'output2' + end + RUBY + end + end + end + + describe '`case` ... `in`', :ruby27 do + context 'with everything on a single line' do + it 'does not register an offense' do + expect_no_offenses('case foo; in pattern then 1; else 0; end') + end + end + + context 'regarding assignment where the right hand side is a `case`' do + it 'accepts a correctly indented assignment' do + expect_no_offenses(<<~RUBY) + output = case variable + in pattern 'output1' else 'output2' - end - RUBY + end + RUBY + end + + it 'registers an offense and corrects an assignment indented some other way' do + expect_offense(<<~RUBY) + output = case variable + in pattern + ^^ Indent `in` as deep as `end`. + 'output1' + else + 'output2' + end + RUBY + + expect_correction(<<~RUBY) + output = case variable + in pattern + 'output1' + else + 'output2' + end + RUBY + end end end end @@ -352,70 +690,135 @@ context 'with IndentOneStep: true' do let(:cop_config) { { 'EnforcedStyle' => 'end', 'IndentOneStep' => true } } - context 'with everything on a single line' do - it 'does not register an offense' do - expect_no_offenses('case foo; when :bar then 1; else 0; end') + describe '`case` ... `when`' do + context 'with everything on a single line' do + it 'does not register an offense' do + expect_no_offenses('case foo; when :bar then 1; else 0; end') + end end - end - context 'regarding assignment where the right hand side is a case' do - it 'accepts a correctly indented assignment' do - expect_no_offenses(<<~RUBY) - output = case variable - when 'value1' - 'output1' - else - 'output2' - end - RUBY - end + context 'regarding assignment where the right hand side is a `case`' do + it 'accepts a correctly indented assignment' do + expect_no_offenses(<<~RUBY) + output = case variable + when 'value1' + 'output1' + else + 'output2' + end + RUBY + end - it 'registers an offense and corrects an assignment indented as case' do - expect_offense(<<~RUBY) - output = case variable + it 'registers an offense and corrects an assignment indented as `case`' do + expect_offense(<<~RUBY) + output = case variable + when 'value1' + ^^^^ Indent `when` one step more than `end`. + 'output1' + else + 'output2' + end + RUBY + + expect_correction(<<~RUBY) + output = case variable + when 'value1' + 'output1' + else + 'output2' + end + RUBY + end + + it 'registers an offense and corrects an assignment indented some other way' do + expect_offense(<<~RUBY) + output = case variable when 'value1' ^^^^ Indent `when` one step more than `end`. 'output1' else 'output2' end - RUBY + RUBY - expect_correction(<<~RUBY) - output = case variable + expect_correction(<<~RUBY) + output = case variable when 'value1' 'output1' else 'output2' end - RUBY + RUBY + end end + end - it 'registers an offense and corrects an assignment indented some other way' do - expect_offense(<<~RUBY) - output = case variable - when 'value1' - ^^^^ Indent `when` one step more than `end`. - 'output1' - else - 'output2' - end - RUBY + describe '`case` ... `in`', :ruby27 do + context 'with everything on a single line' do + it 'does not register an offense' do + expect_no_offenses('case foo; in pattern then 1; else 0; end') + end + end - expect_correction(<<~RUBY) - output = case variable - when 'value1' - 'output1' - else - 'output2' - end - RUBY + context 'regarding assignment where the right hand side is a `case`' do + it 'accepts a correctly indented assignment' do + expect_no_offenses(<<~RUBY) + output = case variable + in pattern + 'output1' + else + 'output2' + end + RUBY + end + + it 'registers an offense and corrects an assignment indented as `case`' do + expect_offense(<<~RUBY) + output = case variable + in pattern + ^^ Indent `in` one step more than `end`. + 'output1' + else + 'output2' + end + RUBY + + expect_correction(<<~RUBY) + output = case variable + in pattern + 'output1' + else + 'output2' + end + RUBY + end + + it 'registers an offense and corrects an assignment indented some other way' do + expect_offense(<<~RUBY) + output = case variable + in pattern + ^^ Indent `in` one step more than `end`. + 'output1' + else + 'output2' + end + RUBY + + expect_correction(<<~RUBY) + output = case variable + in pattern + 'output1' + else + 'output2' + end + RUBY + end end end end end - context 'when case is preceded by something else than whitespace' do + context 'when `case` is preceded by something else than whitespace' do let(:cop_config) { {} } it 'registers an offense and auto-corrects' do