rubocop 0.26.1 → 0.27.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rubocop might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/.rubocop.yml +5 -0
- data/.rubocop_todo.yml +10 -6
- data/.travis.yml +2 -0
- data/CHANGELOG.md +30 -0
- data/README.md +9 -2
- data/assets/logo.png +0 -0
- data/assets/output.html.erb +68 -65
- data/config/default.yml +42 -7
- data/config/disabled.yml +5 -0
- data/config/enabled.yml +32 -7
- data/lib/rubocop.rb +10 -2
- data/lib/rubocop/comment_config.rb +11 -17
- data/lib/rubocop/config.rb +20 -16
- data/lib/rubocop/config_loader.rb +8 -12
- data/lib/rubocop/cop/cop.rb +13 -12
- data/lib/rubocop/cop/lint/block_alignment.rb +4 -6
- data/lib/rubocop/cop/lint/def_end_alignment.rb +2 -2
- data/lib/rubocop/cop/lint/require_parentheses.rb +1 -1
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +2 -3
- data/lib/rubocop/cop/lint/useless_setter_call.rb +2 -2
- data/lib/rubocop/cop/metrics/abc_size.rb +27 -0
- data/lib/rubocop/cop/metrics/block_nesting.rb +2 -4
- data/lib/rubocop/cop/metrics/class_length.rb +1 -1
- data/lib/rubocop/cop/metrics/line_length.rb +2 -5
- data/lib/rubocop/cop/metrics/method_length.rb +2 -2
- data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +24 -15
- data/lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb +15 -2
- data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +63 -0
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/if_node.rb +3 -1
- data/lib/rubocop/cop/mixin/method_complexity.rb +3 -3
- data/lib/rubocop/cop/mixin/{on_method.rb → on_method_def.rb} +3 -3
- data/lib/rubocop/cop/mixin/space_after_punctuation.rb +2 -2
- data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -1
- data/lib/rubocop/cop/mixin/statement_modifier.rb +2 -2
- data/lib/rubocop/cop/mixin/string_literals_help.rb +28 -0
- data/lib/rubocop/cop/rails/delegate.rb +2 -2
- data/lib/rubocop/cop/style/access_modifier_indentation.rb +2 -2
- data/lib/rubocop/cop/style/accessor_method_name.rb +2 -2
- data/lib/rubocop/cop/style/align_hash.rb +16 -12
- data/lib/rubocop/cop/style/align_parameters.rb +1 -1
- data/lib/rubocop/cop/style/and_or.rb +14 -6
- data/lib/rubocop/cop/style/array_join.rb +1 -1
- data/lib/rubocop/cop/style/block_comments.rb +16 -8
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +6 -30
- data/lib/rubocop/cop/style/case_indentation.rb +20 -12
- data/lib/rubocop/cop/style/collection_methods.rb +4 -4
- data/lib/rubocop/cop/style/colon_method_call.rb +9 -0
- data/lib/rubocop/cop/style/comment_annotation.rb +1 -1
- data/lib/rubocop/cop/style/comment_indentation.rb +22 -22
- data/lib/rubocop/cop/style/def_with_parentheses.rb +2 -2
- data/lib/rubocop/cop/style/deprecated_hash_methods.rb +1 -1
- data/lib/rubocop/cop/style/double_negation.rb +6 -1
- data/lib/rubocop/cop/style/else_alignment.rb +93 -0
- data/lib/rubocop/cop/style/empty_line_between_defs.rb +1 -1
- data/lib/rubocop/cop/style/empty_lines.rb +1 -1
- data/lib/rubocop/cop/style/empty_lines_around_class_body.rb +34 -0
- data/lib/rubocop/cop/style/empty_lines_around_method_body.rb +37 -0
- data/lib/rubocop/cop/style/empty_lines_around_module_body.rb +30 -0
- data/lib/rubocop/cop/style/encoding.rb +1 -1
- data/lib/rubocop/cop/style/format_string.rb +4 -4
- data/lib/rubocop/cop/style/indent_array.rb +2 -2
- data/lib/rubocop/cop/style/indent_hash.rb +17 -12
- data/lib/rubocop/cop/style/indentation_width.rb +27 -19
- data/lib/rubocop/cop/style/method_call_parentheses.rb +3 -3
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +1 -1
- data/lib/rubocop/cop/style/method_def_parentheses.rb +17 -11
- data/lib/rubocop/cop/style/method_name.rb +1 -1
- data/lib/rubocop/cop/style/multiline_operation_indentation.rb +174 -0
- data/lib/rubocop/cop/style/non_nil_check.rb +12 -15
- data/lib/rubocop/cop/style/not.rb +1 -1
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +12 -17
- data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
- data/lib/rubocop/cop/style/predicate_name.rb +2 -2
- data/lib/rubocop/cop/style/redundant_begin.rb +2 -2
- data/lib/rubocop/cop/style/redundant_return.rb +3 -3
- data/lib/rubocop/cop/style/redundant_self.rb +3 -3
- data/lib/rubocop/cop/style/regexp_literal.rb +17 -13
- data/lib/rubocop/cop/style/rescue_modifier.rb +2 -2
- data/lib/rubocop/cop/style/single_line_methods.rb +7 -4
- data/lib/rubocop/cop/style/space_after_method_name.rb +2 -2
- data/lib/rubocop/cop/style/space_around_equals_in_parameter_default.rb +17 -11
- data/lib/rubocop/cop/style/space_before_block_braces.rb +1 -1
- data/lib/rubocop/cop/style/space_inside_block_braces.rb +17 -14
- data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +10 -6
- data/lib/rubocop/cop/style/string_literals.rb +13 -16
- data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +41 -0
- data/lib/rubocop/cop/style/trailing_comma.rb +1 -3
- data/lib/rubocop/cop/style/trivial_accessors.rb +3 -3
- data/lib/rubocop/cop/style/unneeded_capital_w.rb +1 -1
- data/lib/rubocop/cop/style/unneeded_percent_q.rb +2 -2
- data/lib/rubocop/cop/style/word_array.rb +23 -19
- data/lib/rubocop/cop/team.rb +13 -26
- data/lib/rubocop/cop/util.rb +5 -0
- data/lib/rubocop/cop/variable_force/locatable.rb +7 -13
- data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
- data/lib/rubocop/formatter/formatter_set.rb +9 -1
- data/lib/rubocop/formatter/html_formatter.rb +83 -55
- data/lib/rubocop/formatter/simple_text_formatter.rb +2 -2
- data/lib/rubocop/formatter/text_util.rb +25 -0
- data/lib/rubocop/options.rb +14 -7
- data/lib/rubocop/path_util.rb +11 -7
- data/lib/rubocop/runner.rb +7 -2
- data/lib/rubocop/version.rb +1 -1
- data/relnotes/v0.27.0.md +77 -0
- data/rubocop.gemspec +1 -1
- data/spec/fixtures/html_formatter/expected.html +495 -0
- data/spec/fixtures/html_formatter/project/app/controllers/application_controller.rb +5 -0
- data/spec/fixtures/html_formatter/project/app/controllers/books_controller.rb +74 -0
- data/spec/fixtures/html_formatter/project/app/models/book.rb +5 -0
- data/spec/rubocop/cli_spec.rb +56 -13
- data/spec/rubocop/cop/lint/invalid_character_literal_spec.rb +1 -1
- data/spec/rubocop/cop/metrics/abc_size_spec.rb +99 -0
- data/spec/rubocop/cop/rails/action_filter_spec.rb +1 -0
- data/spec/rubocop/cop/style/access_modifier_indentation_spec.rb +23 -1
- data/spec/rubocop/cop/style/align_hash_spec.rb +13 -0
- data/spec/rubocop/cop/style/align_parameters_spec.rb +44 -33
- data/spec/rubocop/cop/style/blocks_spec.rb +8 -0
- data/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb +9 -9
- data/spec/rubocop/cop/style/case_indentation_spec.rb +3 -2
- data/spec/rubocop/cop/style/colon_method_call_spec.rb +5 -0
- data/spec/rubocop/cop/style/comment_indentation_spec.rb +6 -1
- data/spec/rubocop/cop/style/else_alignment_spec.rb +437 -0
- data/spec/rubocop/cop/style/empty_lines_around_class_body_spec.rb +75 -0
- data/spec/rubocop/cop/style/{empty_lines_around_body_spec.rb → empty_lines_around_method_body_spec.rb} +9 -50
- data/spec/rubocop/cop/style/empty_lines_around_module_body_spec.rb +79 -0
- data/spec/rubocop/cop/style/file_name_spec.rb +1 -1
- data/spec/rubocop/cop/style/format_string_spec.rb +12 -0
- data/spec/rubocop/cop/style/indent_array_spec.rb +6 -1
- data/spec/rubocop/cop/style/indent_hash_spec.rb +2 -1
- data/spec/rubocop/cop/style/indentation_width_spec.rb +765 -722
- data/spec/rubocop/cop/style/multiline_operation_indentation_spec.rb +414 -0
- data/spec/rubocop/cop/style/non_nil_check_spec.rb +86 -55
- data/spec/rubocop/cop/style/single_line_methods_spec.rb +5 -1
- data/spec/rubocop/cop/style/space_before_block_braces_spec.rb +2 -1
- data/spec/rubocop/cop/style/space_inside_block_braces_spec.rb +2 -1
- data/spec/rubocop/cop/style/string_literals_in_interpolation_spec.rb +63 -0
- data/spec/rubocop/cop/style/string_literals_spec.rb +2 -2
- data/spec/rubocop/cop/style/word_array_spec.rb +15 -1
- data/spec/rubocop/formatter/base_formatter_spec.rb +1 -1
- data/spec/rubocop/formatter/disabled_lines_formatter_spec.rb +0 -1
- data/spec/rubocop/formatter/formatter_set_spec.rb +9 -0
- data/spec/rubocop/formatter/html_formatter_spec.rb +25 -122
- data/spec/rubocop/formatter/offense_count_formatter_spec.rb +0 -1
- data/spec/rubocop/runner_spec.rb +1 -1
- data/spec/spec_helper.rb +12 -130
- data/spec/support/cop_helper.rb +72 -0
- data/spec/support/coverage.rb +15 -0
- data/spec/support/{offenses_matcher.rb → custom_matchers.rb} +28 -0
- data/spec/support/jruby_workaround.rb +15 -0
- data/spec/support/{isolated_environment.rb → shared_contexts.rb} +19 -0
- metadata +49 -14
- data/lib/rubocop/cop/style/empty_lines_around_body.rb +0 -75
- data/spec/support/shared_context.rb +0 -20
@@ -0,0 +1,75 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe RuboCop::Cop::Style::EmptyLinesAroundClassBody, :config do
|
6
|
+
subject(:cop) { described_class.new(config) }
|
7
|
+
|
8
|
+
context 'when EnforcedStyle is no_empty_lines' do
|
9
|
+
let(:cop_config) { { 'EnforcedStyle' => 'no_empty_lines' } }
|
10
|
+
|
11
|
+
it 'registers an offense for class body starting with a blank' do
|
12
|
+
inspect_source(cop,
|
13
|
+
['class SomeClass',
|
14
|
+
'',
|
15
|
+
' do_something',
|
16
|
+
'end'])
|
17
|
+
expect(cop.messages)
|
18
|
+
.to eq(['Extra empty line detected at class body beginning.'])
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'autocorrects class body containing only a blank' do
|
22
|
+
corrected = autocorrect_source(cop,
|
23
|
+
['class SomeClass',
|
24
|
+
'',
|
25
|
+
'end'])
|
26
|
+
expect(corrected).to eq ['class SomeClass',
|
27
|
+
'end'].join("\n")
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'registers an offense for class body ending with a blank' do
|
31
|
+
inspect_source(cop,
|
32
|
+
['class SomeClass',
|
33
|
+
' do_something',
|
34
|
+
'',
|
35
|
+
'end'])
|
36
|
+
expect(cop.messages)
|
37
|
+
.to eq(['Extra empty line detected at class body end.'])
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'when EnforcedStyle is empty_lines' do
|
42
|
+
let(:cop_config) { { 'EnforcedStyle' => 'empty_lines' } }
|
43
|
+
|
44
|
+
it 'registers an offense for class body not starting or ending with a ' \
|
45
|
+
'blank' do
|
46
|
+
inspect_source(cop,
|
47
|
+
['class SomeClass',
|
48
|
+
' do_something',
|
49
|
+
'end'])
|
50
|
+
expect(cop.messages).to eq(['Empty line missing at class body beginning.',
|
51
|
+
'Empty line missing at class body end.'])
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'autocorrects class body containing nothing' do
|
55
|
+
corrected = autocorrect_source(cop,
|
56
|
+
['class SomeClass',
|
57
|
+
'end'])
|
58
|
+
expect(corrected).to eq ['class SomeClass',
|
59
|
+
'',
|
60
|
+
'end'].join("\n")
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'autocorrects beginning and end' do
|
64
|
+
new_source = autocorrect_source(cop,
|
65
|
+
['class SomeClass',
|
66
|
+
' do_something',
|
67
|
+
'end'])
|
68
|
+
expect(new_source).to eq(['class SomeClass',
|
69
|
+
'',
|
70
|
+
' do_something',
|
71
|
+
'',
|
72
|
+
'end'].join("\n"))
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
describe RuboCop::Cop::Style::
|
5
|
+
describe RuboCop::Cop::Style::EmptyLinesAroundMethodBody do
|
6
6
|
subject(:cop) { described_class.new }
|
7
7
|
|
8
8
|
it 'registers an offense for method body starting with a blank' do
|
@@ -11,7 +11,8 @@ describe RuboCop::Cop::Style::EmptyLinesAroundBody do
|
|
11
11
|
'',
|
12
12
|
' do_something',
|
13
13
|
'end'])
|
14
|
-
expect(cop.
|
14
|
+
expect(cop.messages)
|
15
|
+
.to eq(['Extra empty line detected at method body beginning.'])
|
15
16
|
end
|
16
17
|
|
17
18
|
# The cop only registers an offense if the extra line is completely emtpy. If
|
@@ -44,7 +45,8 @@ describe RuboCop::Cop::Style::EmptyLinesAroundBody do
|
|
44
45
|
'',
|
45
46
|
' do_something',
|
46
47
|
'end'])
|
47
|
-
expect(cop.
|
48
|
+
expect(cop.messages)
|
49
|
+
.to eq(['Extra empty line detected at method body beginning.'])
|
48
50
|
end
|
49
51
|
|
50
52
|
it 'autocorrects class method body starting with a blank' do
|
@@ -64,7 +66,8 @@ describe RuboCop::Cop::Style::EmptyLinesAroundBody do
|
|
64
66
|
' do_something',
|
65
67
|
'',
|
66
68
|
'end'])
|
67
|
-
expect(cop.
|
69
|
+
expect(cop.messages)
|
70
|
+
.to eq(['Extra empty line detected at method body end.'])
|
68
71
|
end
|
69
72
|
|
70
73
|
it 'registers an offense for class method body ending with a blank' do
|
@@ -73,52 +76,8 @@ describe RuboCop::Cop::Style::EmptyLinesAroundBody do
|
|
73
76
|
' do_something',
|
74
77
|
'',
|
75
78
|
'end'])
|
76
|
-
expect(cop.
|
77
|
-
|
78
|
-
|
79
|
-
it 'registers an offense for class body starting with a blank' do
|
80
|
-
inspect_source(cop,
|
81
|
-
['class SomeClass',
|
82
|
-
'',
|
83
|
-
' do_something',
|
84
|
-
'end'])
|
85
|
-
expect(cop.offenses.size).to eq(1)
|
86
|
-
end
|
87
|
-
|
88
|
-
it 'autocorrects class body containing only a blank' do
|
89
|
-
corrected = autocorrect_source(cop,
|
90
|
-
['class SomeClass',
|
91
|
-
'',
|
92
|
-
'end'])
|
93
|
-
expect(corrected).to eq ['class SomeClass',
|
94
|
-
'end'].join("\n")
|
95
|
-
end
|
96
|
-
|
97
|
-
it 'registers an offense for module body starting with a blank' do
|
98
|
-
inspect_source(cop,
|
99
|
-
['module SomeModule',
|
100
|
-
'',
|
101
|
-
' do_something',
|
102
|
-
'end'])
|
103
|
-
expect(cop.offenses.size).to eq(1)
|
104
|
-
end
|
105
|
-
|
106
|
-
it 'registers an offense for class body ending with a blank' do
|
107
|
-
inspect_source(cop,
|
108
|
-
['class SomeClass',
|
109
|
-
' do_something',
|
110
|
-
'',
|
111
|
-
'end'])
|
112
|
-
expect(cop.offenses.size).to eq(1)
|
113
|
-
end
|
114
|
-
|
115
|
-
it 'registers an offense for module body ending with a blank' do
|
116
|
-
inspect_source(cop,
|
117
|
-
['module SomeModule',
|
118
|
-
' do_something',
|
119
|
-
'',
|
120
|
-
'end'])
|
121
|
-
expect(cop.offenses.size).to eq(1)
|
79
|
+
expect(cop.messages)
|
80
|
+
.to eq(['Extra empty line detected at method body end.'])
|
122
81
|
end
|
123
82
|
|
124
83
|
it 'is not fooled by single line methods' do
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe RuboCop::Cop::Style::EmptyLinesAroundModuleBody, :config do
|
6
|
+
subject(:cop) { described_class.new(config) }
|
7
|
+
|
8
|
+
context 'when EnforcedStyle is no_empty_lines' do
|
9
|
+
let(:cop_config) { { 'EnforcedStyle' => 'no_empty_lines' } }
|
10
|
+
|
11
|
+
it 'registers an offense for module body starting with a blank' do
|
12
|
+
inspect_source(cop,
|
13
|
+
['module SomeModule',
|
14
|
+
'',
|
15
|
+
' do_something',
|
16
|
+
'end'])
|
17
|
+
expect(cop.messages)
|
18
|
+
.to eq(['Extra empty line detected at module body beginning.'])
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'registers an offense for module body ending with a blank' do
|
22
|
+
inspect_source(cop,
|
23
|
+
['module SomeModule',
|
24
|
+
' do_something',
|
25
|
+
'',
|
26
|
+
'end'])
|
27
|
+
expect(cop.messages)
|
28
|
+
.to eq(['Extra empty line detected at module body end.'])
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'autocorrects beginning and end' do
|
32
|
+
new_source = autocorrect_source(cop,
|
33
|
+
['module SomeModule',
|
34
|
+
'',
|
35
|
+
' do_something',
|
36
|
+
'',
|
37
|
+
'end'])
|
38
|
+
expect(new_source).to eq(['module SomeModule',
|
39
|
+
' do_something',
|
40
|
+
'end'].join("\n"))
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'when EnforcedStyle is empty_lines' do
|
45
|
+
let(:cop_config) { { 'EnforcedStyle' => 'empty_lines' } }
|
46
|
+
|
47
|
+
it 'registers an offense for module body not starting or ending with a ' \
|
48
|
+
'blank' do
|
49
|
+
inspect_source(cop,
|
50
|
+
['module SomeModule',
|
51
|
+
' do_something',
|
52
|
+
'end'])
|
53
|
+
expect(cop.messages)
|
54
|
+
.to eq(['Empty line missing at module body beginning.',
|
55
|
+
'Empty line missing at module body end.'])
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'registers an offense for module body not ending with a blank' do
|
59
|
+
inspect_source(cop,
|
60
|
+
['module SomeModule',
|
61
|
+
'',
|
62
|
+
' do_something',
|
63
|
+
'end'])
|
64
|
+
expect(cop.messages).to eq(['Empty line missing at module body end.'])
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'autocorrects beginning and end' do
|
68
|
+
new_source = autocorrect_source(cop,
|
69
|
+
['module SomeModule',
|
70
|
+
' do_something',
|
71
|
+
'end'])
|
72
|
+
expect(new_source).to eq(['module SomeModule',
|
73
|
+
'',
|
74
|
+
' do_something',
|
75
|
+
'',
|
76
|
+
'end'].join("\n"))
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -139,6 +139,18 @@ describe RuboCop::Cop::Style::FormatString, :config do
|
|
139
139
|
expect(cop.offenses).to be_empty
|
140
140
|
end
|
141
141
|
|
142
|
+
it 'accepts format without arguments' do
|
143
|
+
inspect_source(cop,
|
144
|
+
['format'])
|
145
|
+
expect(cop.offenses).to be_empty
|
146
|
+
end
|
147
|
+
|
148
|
+
it 'accepts sprintf without arguments' do
|
149
|
+
inspect_source(cop,
|
150
|
+
['sprintf'])
|
151
|
+
expect(cop.offenses).to be_empty
|
152
|
+
end
|
153
|
+
|
142
154
|
it 'accepts String#%' do
|
143
155
|
inspect_source(cop,
|
144
156
|
['puts "%d" % 10'])
|
@@ -3,7 +3,12 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
5
|
describe RuboCop::Cop::Style::IndentArray do
|
6
|
-
subject(:cop) { described_class.new }
|
6
|
+
subject(:cop) { described_class.new(config) }
|
7
|
+
let(:config) do
|
8
|
+
RuboCop::Config
|
9
|
+
.new('Style/IndentationWidth' => { 'Width' => indentation_width })
|
10
|
+
end
|
11
|
+
let(:indentation_width) { 2 }
|
7
12
|
|
8
13
|
it 'accepts multi-assignments' do
|
9
14
|
inspect_source(cop, 'a, b = b, a')
|
@@ -10,7 +10,8 @@ describe RuboCop::Cop::Style::IndentHash do
|
|
10
10
|
}
|
11
11
|
RuboCop::Config.new('Style/AlignHash' => align_hash_config,
|
12
12
|
'Style/IndentHash' =>
|
13
|
-
cop_config.merge(supported_styles)
|
13
|
+
cop_config.merge(supported_styles),
|
14
|
+
'Style/IndentationWidth' => { 'Width' => 2 })
|
14
15
|
end
|
15
16
|
let(:align_hash_config) do
|
16
17
|
{
|
@@ -5,66 +5,25 @@ require 'spec_helper'
|
|
5
5
|
describe RuboCop::Cop::Style::IndentationWidth do
|
6
6
|
subject(:cop) { described_class.new(config) }
|
7
7
|
let(:config) do
|
8
|
-
RuboCop::Config.new('
|
8
|
+
RuboCop::Config.new('Style/IndentationWidth' => cop_config,
|
9
|
+
'Lint/EndAlignment' => end_alignment_config)
|
9
10
|
end
|
10
11
|
let(:end_alignment_config) do
|
11
12
|
{ 'Enabled' => true, 'AlignWith' => 'variable' }
|
12
13
|
end
|
13
14
|
|
14
|
-
context 'with
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
['if cond',
|
27
|
-
' func1',
|
28
|
-
'else',
|
29
|
-
' func2',
|
30
|
-
'end'])
|
31
|
-
expect(cop.messages).to eq(['Use 2 (not 1) spaces for indentation.'])
|
32
|
-
expect(cop.highlights).to eq([' '])
|
33
|
-
end
|
34
|
-
|
35
|
-
it 'registers an offense for bad indentation of an elsif body' do
|
36
|
-
inspect_source(cop,
|
37
|
-
['if a1',
|
38
|
-
' b1',
|
39
|
-
'elsif a2',
|
40
|
-
' b2',
|
41
|
-
'else',
|
42
|
-
' c',
|
43
|
-
'end'])
|
44
|
-
expect(cop.messages).to eq(['Use 2 (not 1) spaces for indentation.'])
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'registers offense for bad indentation of ternary inside else' do
|
48
|
-
inspect_source(cop,
|
49
|
-
['if a',
|
50
|
-
' b',
|
51
|
-
'else',
|
52
|
-
' x ? y : z',
|
53
|
-
'end'])
|
54
|
-
expect(cop.messages)
|
55
|
-
.to eq(['Use 2 (not 5) spaces for indentation.'])
|
56
|
-
expect(cop.highlights).to eq([' '])
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'registers offense for bad indentation of modifier if in else' do
|
60
|
-
inspect_source(cop,
|
61
|
-
['if a',
|
62
|
-
' b',
|
63
|
-
'else',
|
64
|
-
' x if y',
|
65
|
-
'end'])
|
66
|
-
expect(cop.messages)
|
67
|
-
.to eq(['Use 2 (not 3) spaces for indentation.'])
|
15
|
+
context 'with Width set to 4' do
|
16
|
+
let(:cop_config) { { 'Width' => 4 } }
|
17
|
+
|
18
|
+
context 'with if statement' do
|
19
|
+
it 'registers an offense for bad indentation of an if body' do
|
20
|
+
inspect_source(cop,
|
21
|
+
['if cond',
|
22
|
+
' func',
|
23
|
+
'end'])
|
24
|
+
expect(cop.messages).to eq(['Use 4 (not 1) spaces for indentation.'])
|
25
|
+
expect(cop.highlights).to eq([' '])
|
26
|
+
end
|
68
27
|
end
|
69
28
|
|
70
29
|
describe '#autocorrect' do
|
@@ -80,764 +39,848 @@ describe RuboCop::Cop::Style::IndentationWidth do
|
|
80
39
|
'end'])
|
81
40
|
expect(corrected)
|
82
41
|
.to eq ['if a1',
|
83
|
-
'
|
42
|
+
' b1',
|
84
43
|
' b1', # Will be corrected by IndentationConsistency.
|
85
44
|
'elsif a2',
|
86
|
-
'
|
45
|
+
' b2',
|
87
46
|
'else',
|
88
|
-
'
|
47
|
+
' c',
|
89
48
|
'end'].join("\n")
|
90
49
|
end
|
50
|
+
end
|
51
|
+
end
|
91
52
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
'end',
|
104
|
-
'end',
|
105
|
-
'end'])
|
106
|
-
expect(corrected).to eq ['module Foo',
|
107
|
-
# The class has a block comment within, so
|
108
|
-
# it's not corrected.
|
109
|
-
'class Bar',
|
110
|
-
'=begin',
|
111
|
-
'This is a nice long',
|
112
|
-
'comment',
|
113
|
-
'which spans a few lines',
|
114
|
-
'=end',
|
115
|
-
# The method has no block comment inside, so
|
116
|
-
# it's corrected.
|
117
|
-
' def baz',
|
118
|
-
# Method contents are also corrected.
|
119
|
-
' do_something',
|
120
|
-
' end',
|
121
|
-
'end',
|
122
|
-
'end'].join("\n")
|
123
|
-
end
|
124
|
-
|
125
|
-
it 'does not indent heredoc strings' do
|
126
|
-
corrected = autocorrect_source(cop,
|
127
|
-
['module Foo',
|
128
|
-
'module Bar',
|
129
|
-
' SOMETHING = <<GOO',
|
130
|
-
'text',
|
131
|
-
'more text',
|
132
|
-
'foo',
|
133
|
-
'GOO',
|
134
|
-
' def baz',
|
135
|
-
' do_something("#{x}")',
|
136
|
-
' end',
|
137
|
-
'end',
|
138
|
-
'end'])
|
139
|
-
expect(corrected).to eq ['module Foo',
|
140
|
-
' module Bar',
|
141
|
-
' SOMETHING = <<GOO',
|
142
|
-
'text',
|
143
|
-
'more text',
|
144
|
-
'foo',
|
145
|
-
'GOO',
|
146
|
-
' def baz',
|
147
|
-
' do_something("#{x}")',
|
148
|
-
' end',
|
149
|
-
' end',
|
150
|
-
'end'].join("\n")
|
151
|
-
end
|
152
|
-
|
153
|
-
it 'indents parenthesized expressions' do
|
154
|
-
src = ['var1 = nil',
|
155
|
-
'array_list = []',
|
156
|
-
'if var1.attr1 != 0 || array_list.select{ |w|',
|
157
|
-
' (w.attr2 == var1.attr2)',
|
158
|
-
' }.blank?',
|
159
|
-
' array_list << var1',
|
160
|
-
'end']
|
161
|
-
corrected = autocorrect_source(cop, src)
|
162
|
-
expect(corrected)
|
163
|
-
.to eq ['var1 = nil',
|
164
|
-
'array_list = []',
|
165
|
-
'if var1.attr1 != 0 || array_list.select{ |w|',
|
166
|
-
' (w.attr2 == var1.attr2)',
|
167
|
-
' }.blank?',
|
168
|
-
' array_list << var1',
|
169
|
-
'end'].join("\n")
|
53
|
+
context 'with Width set to 2' do
|
54
|
+
let(:cop_config) { { 'Width' => 2 } }
|
55
|
+
|
56
|
+
context 'with if statement' do
|
57
|
+
it 'registers an offense for bad indentation of an if body' do
|
58
|
+
inspect_source(cop,
|
59
|
+
['if cond',
|
60
|
+
' func',
|
61
|
+
'end'])
|
62
|
+
expect(cop.messages).to eq(['Use 2 (not 1) spaces for indentation.'])
|
63
|
+
expect(cop.highlights).to eq([' '])
|
170
64
|
end
|
171
65
|
|
172
|
-
it '
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
expect(
|
66
|
+
it 'registers an offense for bad indentation of an else body' do
|
67
|
+
inspect_source(cop,
|
68
|
+
['if cond',
|
69
|
+
' func1',
|
70
|
+
'else',
|
71
|
+
' func2',
|
72
|
+
'end'])
|
73
|
+
expect(cop.messages).to eq(['Use 2 (not 1) spaces for indentation.'])
|
74
|
+
expect(cop.highlights).to eq([' '])
|
180
75
|
end
|
181
76
|
|
182
|
-
it '
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
expect(
|
77
|
+
it 'registers an offense for bad indentation of an elsif body' do
|
78
|
+
inspect_source(cop,
|
79
|
+
['if a1',
|
80
|
+
' b1',
|
81
|
+
'elsif a2',
|
82
|
+
' b2',
|
83
|
+
'else',
|
84
|
+
' c',
|
85
|
+
'end'])
|
86
|
+
expect(cop.messages).to eq(['Use 2 (not 1) spaces for indentation.'])
|
192
87
|
end
|
193
|
-
end
|
194
88
|
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
' b2',
|
207
|
-
'else',
|
208
|
-
' c',
|
209
|
-
'end'])
|
210
|
-
expect(cop.offenses).to be_empty
|
211
|
-
end
|
212
|
-
|
213
|
-
it 'accepts a correctly aligned if/elsif/else/end as a method argument' do
|
214
|
-
inspect_source(cop,
|
215
|
-
['foo(',
|
216
|
-
' if a1',
|
217
|
-
' b1',
|
218
|
-
' elsif a2',
|
219
|
-
' b2',
|
220
|
-
' else',
|
221
|
-
' c',
|
222
|
-
' end',
|
223
|
-
')'])
|
224
|
-
expect(cop.offenses).to be_empty
|
225
|
-
end
|
226
|
-
|
227
|
-
it 'accepts if/elsif/else/end laid out as a table' do
|
228
|
-
inspect_source(cop,
|
229
|
-
['if @io == $stdout then str << "$stdout"',
|
230
|
-
'elsif @io == $stdin then str << "$stdin"',
|
231
|
-
'elsif @io == $stderr then str << "$stderr"',
|
232
|
-
'else str << @io.class.to_s',
|
233
|
-
'end'])
|
234
|
-
expect(cop.offenses).to be_empty
|
235
|
-
end
|
236
|
-
|
237
|
-
it 'accepts if/then/else/end laid out as another table' do
|
238
|
-
inspect_source(cop,
|
239
|
-
["if File.exist?('config.save')",
|
240
|
-
'then ConfigTable.load',
|
241
|
-
'else ConfigTable.new',
|
242
|
-
'end'])
|
243
|
-
expect(cop.offenses).to be_empty
|
244
|
-
end
|
89
|
+
it 'registers offense for bad indentation of ternary inside else' do
|
90
|
+
inspect_source(cop,
|
91
|
+
['if a',
|
92
|
+
' b',
|
93
|
+
'else',
|
94
|
+
' x ? y : z',
|
95
|
+
'end'])
|
96
|
+
expect(cop.messages)
|
97
|
+
.to eq(['Use 2 (not 5) spaces for indentation.'])
|
98
|
+
expect(cop.highlights).to eq([' '])
|
99
|
+
end
|
245
100
|
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
101
|
+
it 'registers offense for bad indentation of modifier if in else' do
|
102
|
+
inspect_source(cop,
|
103
|
+
['if a',
|
104
|
+
' b',
|
105
|
+
'else',
|
106
|
+
' x if y',
|
107
|
+
'end'])
|
108
|
+
expect(cop.messages)
|
109
|
+
.to eq(['Use 2 (not 3) spaces for indentation.'])
|
110
|
+
end
|
253
111
|
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
112
|
+
describe '#autocorrect' do
|
113
|
+
it 'corrects bad indentation' do
|
114
|
+
corrected = autocorrect_source(cop,
|
115
|
+
['if a1',
|
116
|
+
' b1',
|
117
|
+
' b1',
|
118
|
+
'elsif a2',
|
119
|
+
' b2',
|
120
|
+
'else',
|
121
|
+
' c',
|
122
|
+
'end'])
|
123
|
+
expect(corrected)
|
124
|
+
.to eq ['if a1',
|
125
|
+
' b1',
|
126
|
+
' b1', # Will be corrected by IndentationConsistency.
|
127
|
+
'elsif a2',
|
128
|
+
' b2',
|
129
|
+
'else',
|
130
|
+
' c',
|
131
|
+
'end'].join("\n")
|
132
|
+
end
|
264
133
|
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
134
|
+
it 'does not correct in scopes that contain block comments' do
|
135
|
+
corrected = autocorrect_source(cop,
|
136
|
+
['module Foo',
|
137
|
+
'class Bar',
|
138
|
+
'=begin',
|
139
|
+
'This is a nice long',
|
140
|
+
'comment',
|
141
|
+
'which spans a few lines',
|
142
|
+
'=end',
|
143
|
+
'def baz',
|
144
|
+
'do_something',
|
145
|
+
'end',
|
146
|
+
'end',
|
147
|
+
'end'])
|
148
|
+
expect(corrected).to eq ['module Foo',
|
149
|
+
# The class has a block comment within, so
|
150
|
+
# it's not corrected.
|
151
|
+
'class Bar',
|
152
|
+
'=begin',
|
153
|
+
'This is a nice long',
|
154
|
+
'comment',
|
155
|
+
'which spans a few lines',
|
156
|
+
'=end',
|
157
|
+
# The method has no block comment inside, so
|
158
|
+
# it's corrected.
|
159
|
+
' def baz',
|
160
|
+
# Method contents are also corrected.
|
161
|
+
' do_something',
|
162
|
+
' end',
|
163
|
+
'end',
|
164
|
+
'end'].join("\n")
|
165
|
+
end
|
272
166
|
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
167
|
+
it 'does not indent heredoc strings' do
|
168
|
+
corrected = autocorrect_source(cop,
|
169
|
+
['module Foo',
|
170
|
+
'module Bar',
|
171
|
+
' SOMETHING = <<GOO',
|
172
|
+
'text',
|
173
|
+
'more text',
|
174
|
+
'foo',
|
175
|
+
'GOO',
|
176
|
+
' def baz',
|
177
|
+
' do_something("#{x}")',
|
178
|
+
' end',
|
179
|
+
'end',
|
180
|
+
'end'])
|
181
|
+
expect(corrected).to eq ['module Foo',
|
182
|
+
' module Bar',
|
183
|
+
' SOMETHING = <<GOO',
|
184
|
+
'text',
|
185
|
+
'more text',
|
186
|
+
'foo',
|
187
|
+
'GOO',
|
188
|
+
' def baz',
|
189
|
+
' do_something("#{x}")',
|
190
|
+
' end',
|
191
|
+
' end',
|
192
|
+
'end'].join("\n")
|
193
|
+
end
|
298
194
|
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
195
|
+
it 'indents parenthesized expressions' do
|
196
|
+
src = ['var1 = nil',
|
197
|
+
'array_list = []',
|
198
|
+
'if var1.attr1 != 0 || array_list.select{ |w|',
|
199
|
+
' (w.attr2 == var1.attr2)',
|
200
|
+
' }.blank?',
|
201
|
+
' array_list << var1',
|
202
|
+
'end']
|
203
|
+
corrected = autocorrect_source(cop, src)
|
204
|
+
expect(corrected)
|
205
|
+
.to eq ['var1 = nil',
|
206
|
+
'array_list = []',
|
207
|
+
'if var1.attr1 != 0 || array_list.select{ |w|',
|
208
|
+
' (w.attr2 == var1.attr2)',
|
209
|
+
' }.blank?',
|
210
|
+
' array_list << var1',
|
211
|
+
'end'].join("\n")
|
212
|
+
end
|
308
213
|
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
214
|
+
it 'leaves rescue ; end unchanged' do
|
215
|
+
src = ['if variable',
|
216
|
+
' begin',
|
217
|
+
' do_something',
|
218
|
+
' rescue ; end # consume any exception',
|
219
|
+
'end']
|
220
|
+
corrected = autocorrect_source(cop, src)
|
221
|
+
expect(corrected).to eq src.join("\n")
|
222
|
+
end
|
318
223
|
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
224
|
+
it 'leaves block unchanged if block end is not on its own line' do
|
225
|
+
src = ['a_function {',
|
226
|
+
' # a comment',
|
227
|
+
' result = AObject.find_by_attr(attr) if attr',
|
228
|
+
' result || AObject.make(',
|
229
|
+
' :attr => attr,',
|
230
|
+
' :attr2 => Other.get_value(),',
|
231
|
+
' :attr3 => Another.get_value()) }']
|
232
|
+
corrected = autocorrect_source(cop, src)
|
233
|
+
expect(corrected).to eq src.join("\n")
|
328
234
|
end
|
235
|
+
end
|
329
236
|
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
' end'])
|
336
|
-
expect(cop.messages)
|
337
|
-
.to eq(['Use 2 (not 12) spaces for indentation.'])
|
338
|
-
end
|
237
|
+
it 'accepts a one line if statement' do
|
238
|
+
inspect_source(cop,
|
239
|
+
['if cond then func1 else func2 end'])
|
240
|
+
expect(cop.offenses).to be_empty
|
241
|
+
end
|
339
242
|
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
243
|
+
it 'accepts a correctly aligned if/elsif/else/end' do
|
244
|
+
inspect_source(cop,
|
245
|
+
['if a1',
|
246
|
+
' b1',
|
247
|
+
'elsif a2',
|
248
|
+
' b2',
|
249
|
+
'else',
|
250
|
+
' c',
|
251
|
+
'end'])
|
252
|
+
expect(cop.offenses).to be_empty
|
253
|
+
end
|
348
254
|
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
255
|
+
it 'accepts a correctly aligned if/elsif/else/end as a method argument' do
|
256
|
+
inspect_source(cop,
|
257
|
+
['foo(',
|
258
|
+
' if a1',
|
259
|
+
' b1',
|
260
|
+
' elsif a2',
|
261
|
+
' b2',
|
262
|
+
' else',
|
263
|
+
' c',
|
264
|
+
' end',
|
265
|
+
')'])
|
266
|
+
expect(cop.offenses).to be_empty
|
267
|
+
end
|
357
268
|
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
269
|
+
it 'accepts if/elsif/else/end laid out as a table' do
|
270
|
+
inspect_source(cop,
|
271
|
+
['if @io == $stdout then str << "$stdout"',
|
272
|
+
'elsif @io == $stdin then str << "$stdin"',
|
273
|
+
'elsif @io == $stderr then str << "$stderr"',
|
274
|
+
'else str << @io.class.to_s',
|
275
|
+
'end'])
|
276
|
+
expect(cop.offenses).to be_empty
|
277
|
+
end
|
366
278
|
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
end
|
375
|
-
end
|
279
|
+
it 'accepts if/then/else/end laid out as another table' do
|
280
|
+
inspect_source(cop,
|
281
|
+
["if File.exist?('config.save')",
|
282
|
+
'then ConfigTable.load',
|
283
|
+
'else ConfigTable.new',
|
284
|
+
'end'])
|
285
|
+
expect(cop.offenses).to be_empty
|
376
286
|
end
|
377
287
|
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
expect(cop.messages)
|
386
|
-
.to eq(['Use 2 (not -4) spaces for indentation.'])
|
387
|
-
end
|
288
|
+
it 'accepts an empty if' do
|
289
|
+
inspect_source(cop,
|
290
|
+
['if a',
|
291
|
+
'else',
|
292
|
+
'end'])
|
293
|
+
expect(cop.offenses).to be_empty
|
294
|
+
end
|
388
295
|
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
296
|
+
context 'with assignment' do
|
297
|
+
context 'when alignment style is variable' do
|
298
|
+
context 'and end is aligned with variable' do
|
299
|
+
it 'accepts an if with end aligned with setter' do
|
300
|
+
inspect_source(cop,
|
301
|
+
['foo.bar = if baz',
|
302
|
+
' derp',
|
303
|
+
'end'])
|
304
|
+
expect(cop.offenses).to be_empty
|
305
|
+
end
|
306
|
+
|
307
|
+
it 'accepts an if with end aligned with element assignment' do
|
308
|
+
inspect_source(cop,
|
309
|
+
['foo[bar] = if baz',
|
310
|
+
' derp',
|
311
|
+
'end'])
|
312
|
+
expect(cop.offenses).to be_empty
|
313
|
+
end
|
314
|
+
|
315
|
+
it 'accepts an if with end aligned with variable' do
|
316
|
+
inspect_source(cop,
|
317
|
+
['var = if a',
|
318
|
+
' 0',
|
319
|
+
'end',
|
320
|
+
'@var = if a',
|
321
|
+
' 0',
|
322
|
+
'end',
|
323
|
+
'$var = if a',
|
324
|
+
' 0',
|
325
|
+
'end',
|
326
|
+
'var ||= if a',
|
327
|
+
' 0',
|
328
|
+
'end',
|
329
|
+
'var &&= if a',
|
330
|
+
' 0',
|
331
|
+
'end',
|
332
|
+
'var -= if a',
|
333
|
+
' 0',
|
334
|
+
'end',
|
335
|
+
'VAR = if a',
|
336
|
+
' 0',
|
337
|
+
'end'])
|
338
|
+
expect(cop.offenses).to be_empty
|
339
|
+
end
|
340
|
+
|
341
|
+
it 'accepts an if/else' do
|
342
|
+
inspect_source(cop,
|
343
|
+
['var = if a',
|
344
|
+
' 0',
|
345
|
+
'else',
|
346
|
+
' 1',
|
347
|
+
'end'])
|
348
|
+
expect(cop.offenses).to be_empty
|
349
|
+
end
|
350
|
+
|
351
|
+
it 'accepts an if/else with chaining after the end' do
|
352
|
+
inspect_source(cop,
|
353
|
+
['var = if a',
|
354
|
+
' 0',
|
355
|
+
'else',
|
356
|
+
' 1',
|
357
|
+
'end.abc.join("")'])
|
358
|
+
expect(cop.offenses).to be_empty
|
359
|
+
end
|
360
|
+
|
361
|
+
it 'accepts an if/else with chaining with a block after the end' do
|
362
|
+
inspect_source(cop,
|
363
|
+
['var = if a',
|
364
|
+
' 0',
|
365
|
+
'else',
|
366
|
+
' 1',
|
367
|
+
'end.abc.tap {}'])
|
368
|
+
expect(cop.offenses).to be_empty
|
369
|
+
end
|
396
370
|
end
|
397
371
|
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
372
|
+
context 'and end is aligned with keyword' do
|
373
|
+
it 'registers an offense for an if with setter' do
|
374
|
+
inspect_source(cop,
|
375
|
+
['foo.bar = if baz',
|
376
|
+
' derp',
|
377
|
+
' end'])
|
378
|
+
expect(cop.messages)
|
379
|
+
.to eq(['Use 2 (not 12) spaces for indentation.'])
|
380
|
+
end
|
381
|
+
|
382
|
+
it 'registers an offense for an if with element assignment' do
|
383
|
+
inspect_source(cop,
|
384
|
+
['foo[bar] = if baz',
|
385
|
+
' derp',
|
386
|
+
' end'])
|
387
|
+
expect(cop.messages)
|
388
|
+
.to eq(['Use 2 (not 13) spaces for indentation.'])
|
389
|
+
end
|
390
|
+
|
391
|
+
it 'registers an offense for an if' do
|
392
|
+
inspect_source(cop,
|
393
|
+
['var = if a',
|
394
|
+
' 0',
|
395
|
+
' end'])
|
396
|
+
expect(cop.messages)
|
397
|
+
.to eq(['Use 2 (not 8) spaces for indentation.'])
|
398
|
+
end
|
399
|
+
|
400
|
+
it 'registers an offense for a while' do
|
401
|
+
inspect_source(cop,
|
402
|
+
['var = while a',
|
403
|
+
' b',
|
404
|
+
' end'])
|
405
|
+
expect(cop.messages)
|
406
|
+
.to eq(['Use 2 (not 8) spaces for indentation.'])
|
407
|
+
end
|
408
|
+
|
409
|
+
it 'registers an offense for an until' do
|
410
|
+
inspect_source(cop,
|
411
|
+
['var = until a',
|
412
|
+
' b',
|
413
|
+
' end'])
|
414
|
+
expect(cop.messages)
|
415
|
+
.to eq(['Use 2 (not 8) spaces for indentation.'])
|
416
|
+
end
|
414
417
|
end
|
415
418
|
end
|
416
419
|
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
420
|
+
shared_examples 'assignment and if with keyword alignment' do
|
421
|
+
context 'and end is aligned with variable' do
|
422
|
+
it 'registers an offense for an if' do
|
423
|
+
inspect_source(cop,
|
424
|
+
['var = if a',
|
425
|
+
' 0',
|
426
|
+
'end'])
|
427
|
+
expect(cop.messages)
|
428
|
+
.to eq(['Use 2 (not -4) spaces for indentation.'])
|
429
|
+
end
|
430
|
+
|
431
|
+
it 'registers an offense for a while' do
|
432
|
+
inspect_source(cop,
|
433
|
+
['var = while a',
|
434
|
+
' b',
|
435
|
+
'end'])
|
436
|
+
expect(cop.messages)
|
437
|
+
.to eq(['Use 2 (not -4) spaces for indentation.'])
|
438
|
+
end
|
439
|
+
|
440
|
+
it 'autocorrects bad indentation' do
|
441
|
+
corrected = autocorrect_source(cop,
|
442
|
+
['var = if a',
|
443
|
+
' b',
|
444
|
+
'end',
|
445
|
+
'',
|
446
|
+
'var = while a',
|
447
|
+
' b',
|
448
|
+
'end'])
|
449
|
+
expect(corrected).to eq ['var = if a',
|
450
|
+
' b',
|
451
|
+
'end', # Not this cop's job to fix end.
|
452
|
+
'',
|
453
|
+
'var = while a',
|
454
|
+
' b',
|
455
|
+
'end'].join("\n")
|
456
|
+
end
|
424
457
|
end
|
425
458
|
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
459
|
+
context 'and end is aligned with keyword' do
|
460
|
+
it 'accepts an if in assignment' do
|
461
|
+
inspect_source(cop,
|
462
|
+
['var = if a',
|
463
|
+
' 0',
|
464
|
+
' end'])
|
465
|
+
expect(cop.offenses).to be_empty
|
466
|
+
end
|
467
|
+
|
468
|
+
it 'accepts an if/else in assignment' do
|
469
|
+
inspect_source(cop,
|
470
|
+
['var = if a',
|
471
|
+
' 0',
|
472
|
+
' else',
|
473
|
+
' 1',
|
474
|
+
' end'])
|
475
|
+
expect(cop.offenses).to be_empty
|
476
|
+
end
|
477
|
+
|
478
|
+
it 'accepts an if/else in assignment on next line' do
|
479
|
+
inspect_source(cop,
|
480
|
+
['var =',
|
481
|
+
' if a',
|
482
|
+
' 0',
|
483
|
+
' else',
|
484
|
+
' 1',
|
485
|
+
' end'])
|
486
|
+
expect(cop.offenses).to be_empty
|
487
|
+
end
|
488
|
+
|
489
|
+
it 'accepts a while in assignment' do
|
490
|
+
inspect_source(cop,
|
491
|
+
['var = while a',
|
492
|
+
' b',
|
493
|
+
' end'])
|
494
|
+
expect(cop.offenses).to be_empty
|
495
|
+
end
|
496
|
+
|
497
|
+
it 'accepts an until in assignment' do
|
498
|
+
inspect_source(cop,
|
499
|
+
['var = until a',
|
500
|
+
' b',
|
501
|
+
' end'])
|
502
|
+
expect(cop.offenses).to be_empty
|
503
|
+
end
|
434
504
|
end
|
505
|
+
end
|
435
506
|
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
' if a',
|
440
|
-
' 0',
|
441
|
-
' else',
|
442
|
-
' 1',
|
443
|
-
' end'])
|
444
|
-
expect(cop.offenses).to be_empty
|
507
|
+
context 'when alignment style is keyword by choice' do
|
508
|
+
let(:end_alignment_config) do
|
509
|
+
{ 'Enabled' => true, 'AlignWith' => 'keyword' }
|
445
510
|
end
|
446
511
|
|
447
|
-
|
448
|
-
|
449
|
-
['var = while a',
|
450
|
-
' b',
|
451
|
-
' end'])
|
452
|
-
expect(cop.offenses).to be_empty
|
453
|
-
end
|
512
|
+
include_examples 'assignment and if with keyword alignment'
|
513
|
+
end
|
454
514
|
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
' b',
|
459
|
-
' end'])
|
460
|
-
expect(cop.offenses).to be_empty
|
515
|
+
context 'when alignment style is keyword by default' do
|
516
|
+
let(:end_alignment_config) do
|
517
|
+
{ 'Enabled' => false, 'AlignWith' => 'variable' }
|
461
518
|
end
|
462
|
-
end
|
463
|
-
end
|
464
519
|
|
465
|
-
|
466
|
-
let(:end_alignment_config) do
|
467
|
-
{ 'Enabled' => true, 'AlignWith' => 'keyword' }
|
520
|
+
include_examples 'assignment and if with keyword alignment'
|
468
521
|
end
|
469
|
-
|
470
|
-
include_examples 'assignment and if with keyword alignment'
|
471
522
|
end
|
472
523
|
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
524
|
+
it 'accepts an if/else branches with rescue clauses' do
|
525
|
+
# Because of how the rescue clauses come out of Parser, these are
|
526
|
+
# special and need to be tested.
|
527
|
+
inspect_source(cop,
|
528
|
+
['if a',
|
529
|
+
' a rescue nil',
|
530
|
+
'else',
|
531
|
+
' a rescue nil',
|
532
|
+
'end'])
|
533
|
+
expect(cop.offenses).to be_empty
|
479
534
|
end
|
480
535
|
end
|
481
536
|
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
'end'])
|
491
|
-
expect(cop.offenses).to be_empty
|
492
|
-
end
|
493
|
-
end
|
494
|
-
|
495
|
-
context 'with unless' do
|
496
|
-
it 'registers an offense for bad indentation of an unless body' do
|
497
|
-
inspect_source(cop,
|
498
|
-
['unless cond',
|
499
|
-
' func',
|
500
|
-
'end'])
|
501
|
-
expect(cop.messages).to eq(['Use 2 (not 1) spaces for indentation.'])
|
502
|
-
end
|
537
|
+
context 'with unless' do
|
538
|
+
it 'registers an offense for bad indentation of an unless body' do
|
539
|
+
inspect_source(cop,
|
540
|
+
['unless cond',
|
541
|
+
' func',
|
542
|
+
'end'])
|
543
|
+
expect(cop.messages).to eq(['Use 2 (not 1) spaces for indentation.'])
|
544
|
+
end
|
503
545
|
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
546
|
+
it 'accepts an empty unless' do
|
547
|
+
inspect_source(cop,
|
548
|
+
['unless a',
|
549
|
+
'else',
|
550
|
+
'end'])
|
551
|
+
expect(cop.offenses).to be_empty
|
552
|
+
end
|
510
553
|
end
|
511
|
-
end
|
512
554
|
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
555
|
+
context 'with case' do
|
556
|
+
it 'registers an offense for bad indentation in a case/when body' do
|
557
|
+
inspect_source(cop,
|
558
|
+
['case a',
|
559
|
+
'when b',
|
560
|
+
' c',
|
561
|
+
'end'])
|
562
|
+
expect(cop.messages).to eq(['Use 2 (not 1) spaces for indentation.'])
|
563
|
+
end
|
522
564
|
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
565
|
+
it 'registers an offense for bad indentation in a case/else body' do
|
566
|
+
inspect_source(cop,
|
567
|
+
['case a',
|
568
|
+
'when b',
|
569
|
+
' c',
|
570
|
+
'when d',
|
571
|
+
' e',
|
572
|
+
'else',
|
573
|
+
' f',
|
574
|
+
'end'])
|
575
|
+
expect(cop.messages).to eq(['Use 2 (not 3) spaces for indentation.'])
|
576
|
+
end
|
535
577
|
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
578
|
+
it 'accepts correctly indented case/when/else' do
|
579
|
+
inspect_source(cop,
|
580
|
+
['case a',
|
581
|
+
'when b',
|
582
|
+
' c',
|
583
|
+
' c',
|
584
|
+
'when d',
|
585
|
+
'else',
|
586
|
+
' f',
|
587
|
+
'end'])
|
588
|
+
expect(cop.offenses).to be_empty
|
589
|
+
end
|
548
590
|
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
591
|
+
it 'accepts aligned values in when clause' do
|
592
|
+
inspect_source(cop,
|
593
|
+
['case superclass',
|
594
|
+
'when /\A(#{NAMESPACEMATCH})(?:\s|\Z)/,',
|
595
|
+
' /\A(Struct|OStruct)\.new/,',
|
596
|
+
' /\ADelegateClass\((.+?)\)\s*\Z/,',
|
597
|
+
' /\A(#{NAMESPACEMATCH})\(/',
|
598
|
+
' $1',
|
599
|
+
'when "self"',
|
600
|
+
' namespace.path',
|
601
|
+
'end'])
|
602
|
+
expect(cop.offenses).to be_empty
|
603
|
+
end
|
562
604
|
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
605
|
+
it 'accepts case/when/else laid out as a table' do
|
606
|
+
inspect_source(cop,
|
607
|
+
['case sexp.loc.keyword.source',
|
608
|
+
"when 'if' then cond, body, _else = *sexp",
|
609
|
+
"when 'unless' then cond, _else, body = *sexp",
|
610
|
+
'else cond, body = *sexp',
|
611
|
+
'end'])
|
612
|
+
expect(cop.offenses).to be_empty
|
613
|
+
end
|
572
614
|
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
615
|
+
it 'accepts case/when/else with then beginning a line' do
|
616
|
+
inspect_source(cop,
|
617
|
+
['case sexp.loc.keyword.source',
|
618
|
+
"when 'if'",
|
619
|
+
'then cond, body, _else = *sexp',
|
620
|
+
'end'])
|
621
|
+
expect(cop.offenses).to be_empty
|
622
|
+
end
|
581
623
|
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
624
|
+
it 'accepts indented when/else plus indented body' do
|
625
|
+
# "Indent when as deep as case" is the job of another cop.
|
626
|
+
inspect_source(cop,
|
627
|
+
['case code_type',
|
628
|
+
" when 'ruby', 'sql', 'plain'",
|
629
|
+
' code_type',
|
630
|
+
" when 'erb'",
|
631
|
+
" 'ruby; html-script: true'",
|
632
|
+
' when "html"',
|
633
|
+
" 'xml'",
|
634
|
+
' else',
|
635
|
+
" 'plain'",
|
636
|
+
'end'])
|
637
|
+
expect(cop.offenses).to be_empty
|
638
|
+
end
|
596
639
|
end
|
597
|
-
end
|
598
640
|
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
641
|
+
context 'with while/until' do
|
642
|
+
it 'registers an offense for bad indentation of a while body' do
|
643
|
+
inspect_source(cop,
|
644
|
+
['while cond',
|
645
|
+
' func',
|
646
|
+
'end'])
|
647
|
+
expect(cop.messages).to eq(['Use 2 (not 1) spaces for indentation.'])
|
648
|
+
end
|
607
649
|
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
650
|
+
it 'registers an offense for bad indentation of begin/end/while' do
|
651
|
+
inspect_source(cop,
|
652
|
+
['something = begin',
|
653
|
+
' func1',
|
654
|
+
' func2',
|
655
|
+
'end while cond'])
|
656
|
+
expect(cop.messages).to eq(['Use 2 (not 1) spaces for indentation.'])
|
657
|
+
end
|
616
658
|
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
659
|
+
it 'registers an offense for bad indentation of an until body' do
|
660
|
+
inspect_source(cop,
|
661
|
+
['until cond',
|
662
|
+
' func',
|
663
|
+
'end'])
|
664
|
+
expect(cop.messages).to eq(['Use 2 (not 1) spaces for indentation.'])
|
665
|
+
end
|
624
666
|
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
667
|
+
it 'accepts an empty while' do
|
668
|
+
inspect_source(cop,
|
669
|
+
['while a',
|
670
|
+
'end'])
|
671
|
+
expect(cop.offenses).to be_empty
|
672
|
+
end
|
630
673
|
end
|
631
|
-
end
|
632
674
|
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
675
|
+
context 'with for' do
|
676
|
+
it 'registers an offense for bad indentation of a for body' do
|
677
|
+
inspect_source(cop,
|
678
|
+
['for var in 1..10',
|
679
|
+
' func',
|
680
|
+
'end'])
|
681
|
+
expect(cop.messages).to eq(['Use 2 (not 1) spaces for indentation.'])
|
682
|
+
end
|
641
683
|
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
684
|
+
it 'accepts an empty for' do
|
685
|
+
inspect_source(cop,
|
686
|
+
['for var in 1..10',
|
687
|
+
'end'])
|
688
|
+
expect(cop.offenses).to be_empty
|
689
|
+
end
|
647
690
|
end
|
648
|
-
end
|
649
691
|
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
692
|
+
context 'with def/defs' do
|
693
|
+
it 'registers an offense for bad indentation of a def body' do
|
694
|
+
inspect_source(cop,
|
695
|
+
['def test',
|
696
|
+
' func1',
|
697
|
+
' func2', # No offense registered for this.
|
698
|
+
'end'])
|
699
|
+
expect(cop.messages).to eq(['Use 2 (not 4) spaces for indentation.'])
|
700
|
+
end
|
659
701
|
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
|
702
|
+
it 'registers an offense for bad indentation of a defs body' do
|
703
|
+
inspect_source(cop,
|
704
|
+
['def self.test',
|
705
|
+
' func',
|
706
|
+
'end'])
|
707
|
+
expect(cop.messages).to eq(['Use 2 (not 3) spaces for indentation.'])
|
708
|
+
end
|
667
709
|
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
710
|
+
it 'accepts an empty def body' do
|
711
|
+
inspect_source(cop,
|
712
|
+
['def test',
|
713
|
+
'end'])
|
714
|
+
expect(cop.offenses).to be_empty
|
715
|
+
end
|
674
716
|
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
717
|
+
it 'accepts an empty defs body' do
|
718
|
+
inspect_source(cop,
|
719
|
+
['def self.test',
|
720
|
+
'end'])
|
721
|
+
expect(cop.offenses).to be_empty
|
722
|
+
end
|
681
723
|
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
724
|
+
if RUBY_VERSION >= '2.1'
|
725
|
+
context 'when modifier and def are on the same line' do
|
726
|
+
it 'accepts a correctly aligned body' do
|
727
|
+
inspect_source(cop,
|
728
|
+
['private def test',
|
729
|
+
' something',
|
730
|
+
'end'])
|
731
|
+
expect(cop.offenses).to be_empty
|
732
|
+
end
|
691
733
|
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
734
|
+
it 'registers an offense for bad indentation of a def body' do
|
735
|
+
inspect_source(cop,
|
736
|
+
['private def test',
|
737
|
+
' something',
|
738
|
+
' end'])
|
739
|
+
expect(cop.messages)
|
740
|
+
.to eq(['Use 2 (not 10) spaces for indentation.'])
|
741
|
+
end
|
699
742
|
end
|
700
743
|
end
|
701
744
|
end
|
702
|
-
end
|
703
745
|
|
704
|
-
|
705
|
-
|
706
|
-
|
707
|
-
|
708
|
-
|
709
|
-
|
710
|
-
|
711
|
-
|
712
|
-
|
746
|
+
context 'with class' do
|
747
|
+
it 'registers an offense for bad indentation of a class body' do
|
748
|
+
inspect_source(cop,
|
749
|
+
['class Test',
|
750
|
+
' def func',
|
751
|
+
' end',
|
752
|
+
'end'])
|
753
|
+
expect(cop.messages).to eq(['Use 2 (not 4) spaces for indentation.'])
|
754
|
+
end
|
713
755
|
|
714
|
-
|
715
|
-
|
716
|
-
|
717
|
-
|
718
|
-
|
719
|
-
|
756
|
+
it 'accepts an empty class body' do
|
757
|
+
inspect_source(cop,
|
758
|
+
['class Test',
|
759
|
+
'end'])
|
760
|
+
expect(cop.offenses).to be_empty
|
761
|
+
end
|
720
762
|
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
|
733
|
-
|
734
|
-
|
735
|
-
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
|
763
|
+
it 'accepts indented public, protected, and private' do
|
764
|
+
inspect_source(cop,
|
765
|
+
['class Test',
|
766
|
+
' public',
|
767
|
+
'',
|
768
|
+
' def e',
|
769
|
+
' end',
|
770
|
+
'',
|
771
|
+
' protected',
|
772
|
+
'',
|
773
|
+
' def f',
|
774
|
+
' end',
|
775
|
+
'',
|
776
|
+
' private',
|
777
|
+
'',
|
778
|
+
' def g',
|
779
|
+
' end',
|
780
|
+
'end'])
|
781
|
+
expect(cop.offenses).to be_empty
|
782
|
+
end
|
740
783
|
end
|
741
|
-
end
|
742
784
|
|
743
|
-
|
744
|
-
|
745
|
-
|
746
|
-
|
747
|
-
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
|
785
|
+
context 'with module' do
|
786
|
+
it 'registers an offense for bad indentation of a module body' do
|
787
|
+
inspect_source(cop,
|
788
|
+
['module Test',
|
789
|
+
' def func',
|
790
|
+
' end',
|
791
|
+
'end'])
|
792
|
+
expect(cop.messages).to eq(['Use 2 (not 4) spaces for indentation.'])
|
793
|
+
end
|
752
794
|
|
753
|
-
|
754
|
-
|
755
|
-
|
756
|
-
|
757
|
-
|
795
|
+
it 'accepts an empty module body' do
|
796
|
+
inspect_source(cop,
|
797
|
+
['module Test',
|
798
|
+
'end'])
|
799
|
+
expect(cop.offenses).to be_empty
|
800
|
+
end
|
758
801
|
end
|
759
|
-
end
|
760
802
|
|
761
|
-
|
762
|
-
|
763
|
-
|
764
|
-
|
765
|
-
|
766
|
-
|
767
|
-
|
768
|
-
|
769
|
-
|
770
|
-
|
771
|
-
|
772
|
-
|
773
|
-
|
774
|
-
|
775
|
-
|
776
|
-
|
777
|
-
|
778
|
-
|
779
|
-
|
780
|
-
|
781
|
-
|
782
|
-
|
803
|
+
context 'with begin/rescue/else/ensure/end' do
|
804
|
+
it 'registers an offense for bad indentation of bodies' do
|
805
|
+
inspect_source(cop,
|
806
|
+
['def my_func',
|
807
|
+
" puts 'do something outside block'",
|
808
|
+
' begin',
|
809
|
+
" puts 'do something error prone'",
|
810
|
+
' rescue SomeException, SomeOther => e',
|
811
|
+
" puts 'wrongly intended error handling'",
|
812
|
+
' rescue',
|
813
|
+
" puts 'wrongly intended error handling'",
|
814
|
+
' else',
|
815
|
+
" puts 'wrongly intended normal case handling'",
|
816
|
+
' ensure',
|
817
|
+
" puts 'wrongly intended common handling'",
|
818
|
+
' end',
|
819
|
+
'end'])
|
820
|
+
expect(cop.messages).to eq(['Use 2 (not 0) spaces for indentation.',
|
821
|
+
'Use 2 (not 1) spaces for indentation.',
|
822
|
+
'Use 2 (not 1) spaces for indentation.',
|
823
|
+
'Use 2 (not 3) spaces for indentation.',
|
824
|
+
'Use 2 (not 4) spaces for indentation.'])
|
825
|
+
end
|
783
826
|
end
|
784
|
-
end
|
785
827
|
|
786
|
-
|
787
|
-
|
788
|
-
|
789
|
-
|
790
|
-
|
791
|
-
|
792
|
-
|
793
|
-
|
794
|
-
|
795
|
-
|
796
|
-
|
797
|
-
|
828
|
+
context 'with def/rescue/end' do
|
829
|
+
it 'registers an offense for bad indentation of bodies' do
|
830
|
+
inspect_source(cop,
|
831
|
+
['def my_func',
|
832
|
+
" puts 'do something error prone'",
|
833
|
+
'rescue SomeException',
|
834
|
+
" puts 'wrongly intended error handling'",
|
835
|
+
'rescue',
|
836
|
+
" puts 'wrongly intended error handling'",
|
837
|
+
'end'])
|
838
|
+
expect(cop.messages).to eq(['Use 2 (not 1) spaces for indentation.',
|
839
|
+
'Use 2 (not 1) spaces for indentation.'])
|
840
|
+
end
|
798
841
|
end
|
799
|
-
end
|
800
842
|
|
801
|
-
|
802
|
-
|
803
|
-
|
804
|
-
|
805
|
-
|
806
|
-
|
807
|
-
|
808
|
-
|
843
|
+
context 'with block' do
|
844
|
+
it 'registers an offense for bad indentation of a do/end body' do
|
845
|
+
inspect_source(cop,
|
846
|
+
['a = func do',
|
847
|
+
' b',
|
848
|
+
'end'])
|
849
|
+
expect(cop.messages).to eq(['Use 2 (not 1) spaces for indentation.'])
|
850
|
+
end
|
809
851
|
|
810
|
-
|
811
|
-
|
812
|
-
|
813
|
-
|
814
|
-
|
815
|
-
|
816
|
-
|
852
|
+
it 'registers an offense for bad indentation of a {} body' do
|
853
|
+
inspect_source(cop,
|
854
|
+
['func {',
|
855
|
+
' b',
|
856
|
+
'}'])
|
857
|
+
expect(cop.messages).to eq(['Use 2 (not 3) spaces for indentation.'])
|
858
|
+
end
|
817
859
|
|
818
|
-
|
819
|
-
|
820
|
-
|
821
|
-
|
822
|
-
|
823
|
-
|
824
|
-
|
860
|
+
it 'accepts a correctly indented block body' do
|
861
|
+
inspect_source(cop,
|
862
|
+
['a = func do',
|
863
|
+
' b',
|
864
|
+
'end'])
|
865
|
+
expect(cop.offenses).to be_empty
|
866
|
+
end
|
825
867
|
|
826
|
-
|
827
|
-
|
828
|
-
|
829
|
-
|
830
|
-
|
831
|
-
|
868
|
+
it 'accepts an empty block body' do
|
869
|
+
inspect_source(cop,
|
870
|
+
['a = func do',
|
871
|
+
'end'])
|
872
|
+
expect(cop.offenses).to be_empty
|
873
|
+
end
|
832
874
|
|
833
|
-
|
834
|
-
|
835
|
-
|
836
|
-
|
837
|
-
|
838
|
-
|
839
|
-
|
840
|
-
|
875
|
+
# The cop uses the block end/} as the base for indentation, so if it's not
|
876
|
+
# on its own line, all bets are off.
|
877
|
+
it 'accepts badly indented code if block end is not on separate line' do
|
878
|
+
inspect_source(cop,
|
879
|
+
['foo {',
|
880
|
+
'def baz',
|
881
|
+
'end }'])
|
882
|
+
expect(cop.offenses).to be_empty
|
883
|
+
end
|
841
884
|
end
|
842
885
|
end
|
843
886
|
end
|