rubocop 0.21.0 → 0.22.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.

Files changed (159) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -1
  3. data/{rubocop-todo.yml → .rubocop_todo.yml} +5 -5
  4. data/CHANGELOG.md +41 -0
  5. data/README.md +21 -11
  6. data/config/default.yml +33 -10
  7. data/config/disabled.yml +0 -4
  8. data/config/enabled.yml +27 -1
  9. data/lib/rubocop.rb +17 -1
  10. data/lib/rubocop/config.rb +32 -27
  11. data/lib/rubocop/config_loader.rb +21 -8
  12. data/lib/rubocop/cop/cop.rb +8 -8
  13. data/lib/rubocop/cop/lint/block_alignment.rb +22 -22
  14. data/lib/rubocop/cop/lint/condition_position.rb +3 -5
  15. data/lib/rubocop/cop/lint/debugger.rb +4 -5
  16. data/lib/rubocop/cop/lint/end_alignment.rb +11 -10
  17. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +10 -11
  18. data/lib/rubocop/cop/lint/require_parentheses.rb +7 -7
  19. data/lib/rubocop/cop/lint/rescue_exception.rb +3 -3
  20. data/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb +6 -7
  21. data/lib/rubocop/cop/lint/unreachable_code.rb +2 -3
  22. data/lib/rubocop/cop/lint/unused_block_argument.rb +2 -15
  23. data/lib/rubocop/cop/lint/unused_method_argument.rb +2 -14
  24. data/lib/rubocop/cop/lint/useless_comparison.rb +4 -5
  25. data/lib/rubocop/cop/lint/void.rb +6 -8
  26. data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +4 -2
  27. data/lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb +1 -3
  28. data/lib/rubocop/cop/mixin/code_length.rb +5 -5
  29. data/lib/rubocop/cop/mixin/negative_conditional.rb +6 -6
  30. data/lib/rubocop/cop/mixin/percent_literal.rb +2 -3
  31. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +5 -5
  32. data/lib/rubocop/cop/mixin/space_inside.rb +10 -11
  33. data/lib/rubocop/cop/mixin/unused_argument.rb +35 -0
  34. data/lib/rubocop/cop/offense.rb +1 -1
  35. data/lib/rubocop/cop/rails/action_filter.rb +6 -8
  36. data/lib/rubocop/cop/rails/default_scope.rb +2 -4
  37. data/lib/rubocop/cop/rails/delegate.rb +2 -0
  38. data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +2 -3
  39. data/lib/rubocop/cop/rails/output.rb +2 -3
  40. data/lib/rubocop/cop/rails/read_write_attribute.rb +3 -4
  41. data/lib/rubocop/cop/rails/validation.rb +2 -3
  42. data/lib/rubocop/cop/style/alias.rb +3 -3
  43. data/lib/rubocop/cop/style/align_hash.rb +4 -4
  44. data/lib/rubocop/cop/style/align_parameters.rb +13 -1
  45. data/lib/rubocop/cop/style/and_or.rb +2 -5
  46. data/lib/rubocop/cop/style/array_join.rb +3 -4
  47. data/lib/rubocop/cop/style/ascii_identifiers.rb +2 -3
  48. data/lib/rubocop/cop/style/block_comments.rb +2 -3
  49. data/lib/rubocop/cop/style/block_nesting.rb +2 -3
  50. data/lib/rubocop/cop/style/blocks.rb +9 -9
  51. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +5 -5
  52. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  53. data/lib/rubocop/cop/style/class_methods.rb +3 -5
  54. data/lib/rubocop/cop/style/collection_methods.rb +8 -11
  55. data/lib/rubocop/cop/style/comment_annotation.rb +9 -9
  56. data/lib/rubocop/cop/style/comment_indentation.rb +66 -0
  57. data/lib/rubocop/cop/style/constant_name.rb +3 -3
  58. data/lib/rubocop/cop/style/cyclomatic_complexity.rb +5 -5
  59. data/lib/rubocop/cop/style/deprecated_hash_methods.rb +6 -6
  60. data/lib/rubocop/cop/style/each_with_object.rb +38 -0
  61. data/lib/rubocop/cop/style/empty_lines.rb +8 -8
  62. data/lib/rubocop/cop/style/empty_lines_around_body.rb +7 -7
  63. data/lib/rubocop/cop/style/encoding.rb +6 -7
  64. data/lib/rubocop/cop/style/end_of_line.rb +10 -10
  65. data/lib/rubocop/cop/style/file_name.rb +11 -7
  66. data/lib/rubocop/cop/style/guard_clause.rb +32 -22
  67. data/lib/rubocop/cop/style/indentation_width.rb +25 -26
  68. data/lib/rubocop/cop/style/lambda.rb +8 -9
  69. data/lib/rubocop/cop/style/leading_comment_space.rb +4 -5
  70. data/lib/rubocop/cop/style/line_end_concatenation.rb +29 -6
  71. data/lib/rubocop/cop/style/line_length.rb +9 -9
  72. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +7 -6
  73. data/lib/rubocop/cop/style/module_function.rb +3 -4
  74. data/lib/rubocop/cop/style/multiline_if_then.rb +3 -3
  75. data/lib/rubocop/cop/style/negated_if.rb +2 -0
  76. data/lib/rubocop/cop/style/next.rb +80 -0
  77. data/lib/rubocop/cop/style/nil_comparison.rb +2 -3
  78. data/lib/rubocop/cop/style/non_nil_check.rb +15 -3
  79. data/lib/rubocop/cop/style/not.rb +4 -4
  80. data/lib/rubocop/cop/style/numeric_literals.rb +7 -8
  81. data/lib/rubocop/cop/style/op_method.rb +3 -5
  82. data/lib/rubocop/cop/style/parameter_lists.rb +4 -4
  83. data/lib/rubocop/cop/style/parentheses_around_condition.rb +5 -6
  84. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +5 -5
  85. data/lib/rubocop/cop/style/predicate_name.rb +2 -4
  86. data/lib/rubocop/cop/style/redundant_self.rb +11 -12
  87. data/lib/rubocop/cop/style/self_assignment.rb +8 -13
  88. data/lib/rubocop/cop/style/semicolon.rb +16 -19
  89. data/lib/rubocop/cop/style/signal_exception.rb +10 -11
  90. data/lib/rubocop/cop/style/single_line_block_params.rb +2 -3
  91. data/lib/rubocop/cop/style/single_line_methods.rb +3 -4
  92. data/lib/rubocop/cop/style/space_after_colon.rb +9 -9
  93. data/lib/rubocop/cop/style/space_after_control_keyword.rb +3 -3
  94. data/lib/rubocop/cop/style/space_after_method_name.rb +3 -3
  95. data/lib/rubocop/cop/style/space_after_not.rb +3 -4
  96. data/lib/rubocop/cop/style/space_around_operators.rb +5 -4
  97. data/lib/rubocop/cop/style/space_before_comment.rb +29 -0
  98. data/lib/rubocop/cop/style/space_before_modifier_keyword.rb +6 -7
  99. data/lib/rubocop/cop/style/symbol_array.rb +2 -3
  100. data/lib/rubocop/cop/style/tab.rb +8 -8
  101. data/lib/rubocop/cop/style/trailing_blank_lines.rb +10 -10
  102. data/lib/rubocop/cop/style/trailing_whitespace.rb +6 -7
  103. data/lib/rubocop/cop/style/trivial_accessors.rb +3 -4
  104. data/lib/rubocop/cop/style/unless_else.rb +2 -3
  105. data/lib/rubocop/cop/style/unneeded_capital_w.rb +3 -3
  106. data/lib/rubocop/cop/style/unneeded_percent_x.rb +26 -0
  107. data/lib/rubocop/cop/style/when_then.rb +3 -3
  108. data/lib/rubocop/cop/style/while_until_do.rb +3 -6
  109. data/lib/rubocop/cop/style/word_array.rb +8 -7
  110. data/lib/rubocop/cop/team.rb +9 -11
  111. data/lib/rubocop/cop/util.rb +2 -3
  112. data/lib/rubocop/cop/variable_force.rb +2 -3
  113. data/lib/rubocop/cop/variable_force/variable_table.rb +1 -2
  114. data/lib/rubocop/file_inspector.rb +5 -0
  115. data/lib/rubocop/formatter/clang_style_formatter.rb +3 -4
  116. data/lib/rubocop/formatter/disabled_config_formatter.rb +6 -6
  117. data/lib/rubocop/options.rb +14 -11
  118. data/lib/rubocop/target_finder.rb +12 -1
  119. data/lib/rubocop/version.rb +1 -1
  120. data/relnotes/v0.21.0.md +11 -0
  121. data/relnotes/v0.22.0.md +77 -0
  122. data/spec/rubocop/cli_spec.rb +86 -40
  123. data/spec/rubocop/comment_config_spec.rb +1 -1
  124. data/spec/rubocop/config_loader_spec.rb +3 -3
  125. data/spec/rubocop/config_spec.rb +14 -3
  126. data/spec/rubocop/cop/force_spec.rb +1 -1
  127. data/spec/rubocop/cop/lint/unused_block_argument_spec.rb +164 -99
  128. data/spec/rubocop/cop/lint/unused_method_argument_spec.rb +195 -85
  129. data/spec/rubocop/cop/lint/void_spec.rb +8 -0
  130. data/spec/rubocop/cop/rails/delegate_spec.rb +17 -0
  131. data/spec/rubocop/cop/rails/output_spec.rb +3 -3
  132. data/spec/rubocop/cop/severity_spec.rb +5 -5
  133. data/spec/rubocop/cop/style/align_parameters_spec.rb +108 -0
  134. data/spec/rubocop/cop/style/block_nesting_spec.rb +3 -3
  135. data/spec/rubocop/cop/style/class_and_module_children_spec.rb +12 -10
  136. data/spec/rubocop/cop/style/comment_indentation_spec.rb +174 -0
  137. data/spec/rubocop/cop/style/each_with_object_spec.rb +44 -0
  138. data/spec/rubocop/cop/style/end_of_line_spec.rb +3 -5
  139. data/spec/rubocop/cop/style/guard_clause_spec.rb +135 -57
  140. data/spec/rubocop/cop/style/if_unless_modifier_spec.rb +7 -7
  141. data/spec/rubocop/cop/style/line_end_concatenation_spec.rb +75 -1
  142. data/spec/rubocop/cop/style/line_length_spec.rb +7 -7
  143. data/spec/rubocop/cop/style/negated_if_spec.rb +5 -1
  144. data/spec/rubocop/cop/style/next_spec.rb +210 -0
  145. data/spec/rubocop/cop/style/non_nil_check_spec.rb +44 -13
  146. data/spec/rubocop/cop/style/self_assignment_spec.rb +1 -1
  147. data/spec/rubocop/cop/style/space_around_operators_spec.rb +1 -0
  148. data/spec/rubocop/cop/style/space_before_comment_spec.rb +34 -0
  149. data/spec/rubocop/cop/style/unneeded_percent_x_spec.rb +34 -0
  150. data/spec/rubocop/cop/style/while_until_modifier_spec.rb +2 -2
  151. data/spec/rubocop/cop/util_spec.rb +2 -2
  152. data/spec/rubocop/cop/variable_force_spec.rb +1 -1
  153. data/spec/rubocop/formatter/disabled_config_formatter_spec.rb +4 -4
  154. data/spec/rubocop/formatter/disabled_lines_formatter_spec.rb +2 -2
  155. data/spec/rubocop/formatter/offense_count_formatter_spec.rb +2 -2
  156. data/spec/rubocop/options_spec.rb +3 -0
  157. data/spec/support/shared_context.rb +1 -3
  158. data/spec/support/statement_modifier_helper.rb +2 -2
  159. metadata +20 -3
@@ -149,8 +149,8 @@ describe Rubocop::Cop::Style::BlockNesting, :config do
149
149
  expect(cop.offenses.map(&:line)).to eq(lines)
150
150
  expect(cop.messages).to eq(
151
151
  ['Avoid more than 2 levels of block nesting.'] * lines.length)
152
- if cop.offenses.size > 0
153
- expect(cop.config_to_allow_offenses['Max']).to eq(max_to_allow)
154
- end
152
+ return unless cop.offenses.size > 0
153
+
154
+ expect(cop.config_to_allow_offenses['Max']).to eq(max_to_allow)
155
155
  end
156
156
  end
@@ -6,11 +6,7 @@ describe Rubocop::Cop::Style::ClassAndModuleChildren, :config do
6
6
  subject(:cop) { described_class.new(config) }
7
7
 
8
8
  context 'nested style' do
9
- let(:cop_config) do
10
- {
11
- 'EnforcedStyle' => 'nested'
12
- }
13
- end
9
+ let(:cop_config) { { 'EnforcedStyle' => 'nested' } }
14
10
 
15
11
  it 'registers an offense for not nested classes' do
16
12
  inspect_source(cop, ['class FooClass::BarClass', 'end'])
@@ -62,11 +58,7 @@ describe Rubocop::Cop::Style::ClassAndModuleChildren, :config do
62
58
  end
63
59
 
64
60
  context 'compact style' do
65
- let(:cop_config) do
66
- {
67
- 'EnforcedStyle' => 'compact'
68
- }
69
- end
61
+ let(:cop_config) { { 'EnforcedStyle' => 'compact' } }
70
62
 
71
63
  it 'registers a offense for classes with nested children' do
72
64
  inspect_source(cop,
@@ -125,5 +117,15 @@ describe Rubocop::Cop::Style::ClassAndModuleChildren, :config do
125
117
  ])
126
118
  expect(cop.offenses).to be_empty
127
119
  end
120
+
121
+ it 'accepts class / module with single method' do
122
+ inspect_source(cop,
123
+ ['class FooClass',
124
+ ' def bar_method',
125
+ ' end',
126
+ 'end'
127
+ ])
128
+ expect(cop.offenses).to be_empty
129
+ end
128
130
  end
129
131
  end
@@ -0,0 +1,174 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Rubocop::Cop::Style::CommentIndentation do
6
+ subject(:cop) { described_class.new }
7
+
8
+ context 'on outer level' do
9
+ it 'accepts a correctly indented comment' do
10
+ inspect_source(cop, '# comment')
11
+ expect(cop.offenses).to be_empty
12
+ end
13
+
14
+ it 'accepts a comment that follows code' do
15
+ inspect_source(cop, 'hello # comment')
16
+ expect(cop.offenses).to be_empty
17
+ end
18
+
19
+ it 'accepts a documentation comment' do
20
+ inspect_source(cop, ['=begin',
21
+ 'Doc comment',
22
+ '=end',
23
+ ' hello',
24
+ ' #',
25
+ 'hi'])
26
+ expect(cop.highlights).to eq(['#'])
27
+ end
28
+
29
+ it 'registers an offense for an incorrectly indented (1) comment' do
30
+ inspect_source(cop, ' # comment')
31
+ expect(cop.messages)
32
+ .to eq(['Incorrect indentation detected (column 1 instead of 0).'])
33
+ expect(cop.highlights).to eq(['# comment'])
34
+ end
35
+
36
+ it 'registers an offense for an incorrectly indented (2) comment' do
37
+ inspect_source(cop, ' # comment')
38
+ expect(cop.messages)
39
+ .to eq(['Incorrect indentation detected (column 2 instead of 0).'])
40
+ end
41
+
42
+ it 'registers an offense for each incorrectly indented comment' do
43
+ inspect_source(cop, ['# a',
44
+ ' # b',
45
+ ' # c',
46
+ '# d',
47
+ 'def test; end'])
48
+ expect(cop.messages)
49
+ .to eq(['Incorrect indentation detected (column 0 instead of 2).',
50
+ 'Incorrect indentation detected (column 2 instead of 4).',
51
+ 'Incorrect indentation detected (column 4 instead of 0).'])
52
+ end
53
+ end
54
+
55
+ context 'around program structure keywords' do
56
+ it 'accepts correctly indented comments' do
57
+ inspect_source(cop, ['#',
58
+ 'def m',
59
+ ' #',
60
+ ' if a',
61
+ ' #',
62
+ ' b',
63
+ ' # this is accepted',
64
+ ' elsif aa',
65
+ ' # this is accepted',
66
+ ' else',
67
+ ' #',
68
+ ' end',
69
+ ' #',
70
+ ' case a',
71
+ ' # this is accepted',
72
+ ' when 0',
73
+ ' #',
74
+ ' b',
75
+ ' end',
76
+ ' # this is accepted',
77
+ 'rescue',
78
+ '# this is accepted',
79
+ 'ensure',
80
+ ' #',
81
+ 'end',
82
+ '#'])
83
+ expect(cop.offenses).to eq([])
84
+ end
85
+
86
+ context 'with a blank line following the comment' do
87
+ it 'accepts a correctly indented comment' do
88
+ inspect_source(cop, ['def m',
89
+ ' # comment',
90
+ '',
91
+ 'end'])
92
+ expect(cop.offenses).to be_empty
93
+ end
94
+ end
95
+ end
96
+
97
+ context 'near various kinds of brackets' do
98
+ it 'accepts correctly indented comments' do
99
+ inspect_source(cop, ['#',
100
+ 'a = {',
101
+ ' #',
102
+ ' x: [',
103
+ ' 1',
104
+ ' #',
105
+ ' ],',
106
+ ' #',
107
+ ' y: func(',
108
+ ' 1',
109
+ ' #',
110
+ ' )',
111
+ ' #',
112
+ '}',
113
+ '#'])
114
+ expect(cop.offenses).to eq([])
115
+ end
116
+
117
+ it 'is unaffected by closing bracket that does not begin a line' do
118
+ inspect_source(cop, ['#',
119
+ 'result = []'])
120
+ expect(cop.messages).to eq([])
121
+ end
122
+ end
123
+
124
+ it 'auto-corrects' do
125
+ new_source = autocorrect_source(cop, [' # comment',
126
+ 'hash1 = { a: 0,',
127
+ ' # comment',
128
+ ' bb: 1,',
129
+ ' ccc: 2 }',
130
+ ' if a',
131
+ ' #',
132
+ ' b',
133
+ ' # this is accepted',
134
+ ' elsif aa',
135
+ ' # so is this',
136
+ ' elsif bb',
137
+ '#',
138
+ ' else',
139
+ ' #',
140
+ ' end',
141
+ ' case a',
142
+ ' # this is accepted',
143
+ ' when 0',
144
+ ' # so is this',
145
+ ' when 1',
146
+ ' #',
147
+ ' b',
148
+ ' end'])
149
+ expect(new_source).to eq(['# comment',
150
+ 'hash1 = { a: 0,',
151
+ ' # comment',
152
+ ' bb: 1,',
153
+ ' ccc: 2 }',
154
+ ' if a',
155
+ ' #',
156
+ ' b',
157
+ ' # this is accepted',
158
+ ' elsif aa',
159
+ ' # so is this',
160
+ ' elsif bb',
161
+ ' #',
162
+ ' else',
163
+ ' #',
164
+ ' end',
165
+ ' case a',
166
+ ' # this is accepted',
167
+ ' when 0',
168
+ ' # so is this',
169
+ ' when 1',
170
+ ' #',
171
+ ' b',
172
+ ' end'].join("\n"))
173
+ end
174
+ end
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Rubocop::Cop::Style::EachWithObject do
6
+ subject(:cop) { described_class.new }
7
+
8
+ it 'finds inject and reduce with passed in and returned hash' do
9
+ inspect_source(cop,
10
+ ['[].inject({}) do |a, e|',
11
+ ' a[e] = 1',
12
+ ' a',
13
+ 'end',
14
+ '',
15
+ '[].reduce({}) do |a, e|',
16
+ ' a[e] = 1',
17
+ ' a[e] = 1',
18
+ ' a',
19
+ 'end'])
20
+ expect(cop.offenses.size).to eq(2)
21
+ expect(cop.offenses.map(&:line).sort).to eq([1, 6])
22
+ expect(cop.messages)
23
+ .to eq(['Use `each_with_object` instead of `inject`.',
24
+ 'Use `each_with_object` instead of `reduce`.'])
25
+ expect(cop.highlights).to eq(%w(inject reduce))
26
+ end
27
+
28
+ it 'ignores inject and reduce with passed in, but not returned hash' do
29
+ inspect_source(cop,
30
+ ['[].inject({}) do |a, e|',
31
+ ' a + e',
32
+ 'end',
33
+ '',
34
+ '[].reduce({}) do |a, e|',
35
+ ' a + e',
36
+ 'end'])
37
+ expect(cop.offenses).to be_empty
38
+ end
39
+
40
+ it 'ignores inject and reduce passed in symbol' do
41
+ inspect_source(cop, ['[].inject(:+)', '[].reduce(:+)'])
42
+ expect(cop.offenses).to be_empty
43
+ end
44
+ end
@@ -29,11 +29,9 @@ describe Rubocop::Cop::Style::EndOfLine do
29
29
  end
30
30
 
31
31
  context 'when the default external encoding is US_ASCII' do
32
- before(:each) do
33
- @orig_encoding = Encoding.default_external
34
- Encoding.default_external = Encoding::US_ASCII
35
- end
36
- after(:each) { Encoding.default_external = @orig_encoding }
32
+ let(:orig_encoding) { Encoding.default_external }
33
+ before(:each) { Encoding.default_external = Encoding::US_ASCII }
34
+ after(:each) { Encoding.default_external = orig_encoding }
37
35
 
38
36
  it 'does not crash on UTF-8 encoded non-ascii characters' do
39
37
  inspect_source_file(cop,
@@ -2,76 +2,154 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe Rubocop::Cop::Style::GuardClause do
6
- let(:cop) { described_class.new }
5
+ describe Rubocop::Cop::Style::GuardClause, :config do
6
+ let(:cop) { described_class.new(config) }
7
+ let(:cop_config) { {} }
7
8
 
8
- it 'reports an offense if method body is if without else' do
9
- src = ['def func',
10
- ' if something',
11
- ' work',
12
- ' work_more',
13
- ' end',
14
- 'end']
15
- inspect_source(cop, src)
16
- expect(cop.offenses.size).to eq(1)
9
+ it 'reports an offense if method body is if / unless without else' do
10
+ inspect_source(cop,
11
+ ['def func',
12
+ ' if something',
13
+ ' work',
14
+ ' end',
15
+ 'end',
16
+ '',
17
+ 'def func',
18
+ ' unless something',
19
+ ' work',
20
+ ' end',
21
+ 'end'])
22
+ expect(cop.offenses.size).to eq(2)
23
+ expect(cop.offenses.map(&:line).sort).to eq([2, 8])
24
+ expect(cop.messages)
25
+ .to eq(['Use a guard clause instead of wrapping ' \
26
+ 'the code inside a conditional expression.'] * 2)
27
+ expect(cop.highlights).to eq(%w(if unless))
17
28
  end
18
29
 
19
- it 'reports an offense if method body ends with if without else' do
20
- src = ['def func',
21
- ' test',
22
- ' if something',
23
- ' work',
24
- ' work_more',
25
- ' end',
26
- 'end']
27
- inspect_source(cop, src)
28
- expect(cop.offenses.size).to eq(1)
30
+ it 'reports an offense if method body ends with if / unless without else' do
31
+ inspect_source(cop,
32
+ ['def func',
33
+ ' test',
34
+ ' if something',
35
+ ' work',
36
+ ' end',
37
+ 'end',
38
+ '',
39
+ 'def func',
40
+ ' test',
41
+ ' unless something',
42
+ ' work',
43
+ ' end',
44
+ 'end'])
45
+ expect(cop.offenses.size).to eq(2)
46
+ expect(cop.offenses.map(&:line).sort).to eq([3, 10])
47
+ expect(cop.messages)
48
+ .to eq(['Use a guard clause instead of wrapping ' \
49
+ 'the code inside a conditional expression.'] * 2)
50
+ expect(cop.highlights).to eq(%w(if unless))
29
51
  end
30
52
 
31
- it 'accepts a method which body is if with else' do
32
- src = ['def func',
33
- ' if something',
34
- ' work',
35
- ' work_more',
36
- ' else',
37
- ' test',
38
- ' end',
39
- 'end']
40
- inspect_source(cop, src)
53
+ it 'accepts a method which body is if / unless with else' do
54
+ inspect_source(cop,
55
+ ['def func',
56
+ ' if something',
57
+ ' work',
58
+ ' else',
59
+ ' test',
60
+ ' end',
61
+ 'end',
62
+ '',
63
+ 'def func',
64
+ ' unless something',
65
+ ' work',
66
+ ' else',
67
+ ' test',
68
+ ' end',
69
+ 'end'])
41
70
  expect(cop.offenses).to be_empty
42
71
  end
43
72
 
44
- it 'accepts a method which body does not end with if' do
45
- src = ['def func',
46
- ' if something',
47
- ' work',
48
- ' work_more',
49
- ' end',
50
- ' test',
51
- 'end']
52
- inspect_source(cop, src)
73
+ it 'accepts a method which body does not end with if / unless' do
74
+ inspect_source(cop,
75
+ ['def func',
76
+ ' if something',
77
+ ' work',
78
+ ' end',
79
+ ' test',
80
+ 'end',
81
+ '',
82
+ 'def func',
83
+ ' unless something',
84
+ ' work',
85
+ ' end',
86
+ ' test',
87
+ 'end'])
53
88
  expect(cop.offenses).to be_empty
54
89
  end
55
90
 
56
- it 'accepts a method which body does not end with if' do
57
- src = ['def func',
58
- ' if something',
59
- ' work',
60
- ' work_more',
61
- ' end',
62
- ' test',
63
- 'end']
64
- inspect_source(cop, src)
91
+ it 'accepts a method whose body is a modifier if / unless' do
92
+ inspect_source(cop,
93
+ ['def func',
94
+ ' work if something',
95
+ 'end',
96
+ '',
97
+ 'def func',
98
+ ' work if something',
99
+ 'end'])
65
100
  expect(cop.offenses).to be_empty
66
101
  end
67
102
 
68
- it 'accepts a method whose body is an if with a one-line body' do
69
- src = ['def func',
70
- ' if something',
71
- ' work',
72
- ' end',
73
- 'end']
74
- inspect_source(cop, src)
75
- expect(cop.offenses).to be_empty
103
+ context 'MinBodyLength: 1' do
104
+ let(:cop_config) do
105
+ { 'EnforcedStyle' => 'MinBodyLength: 1' }
106
+ end
107
+
108
+ it 'reports an offense for if whose body has 1 line' do
109
+ inspect_source(cop,
110
+ ['def func',
111
+ ' if something',
112
+ ' work',
113
+ ' end',
114
+ 'end',
115
+ '',
116
+ 'def func',
117
+ ' unless something',
118
+ ' work',
119
+ ' end',
120
+ 'end'])
121
+ expect(cop.offenses.size).to eq(2)
122
+ expect(cop.offenses.map(&:line).sort).to eq([2, 8])
123
+ expect(cop.messages)
124
+ .to eq(['Use a guard clause instead of wrapping ' \
125
+ 'the code inside a conditional expression.'] * 2)
126
+ expect(cop.highlights).to eq(%w(if unless))
127
+ end
128
+ end
129
+
130
+ context 'MinBodyLength: 4' do
131
+ let(:cop_config) do
132
+ { 'MinBodyLength' => 4 }
133
+ end
134
+
135
+ it 'accepts a method whose body has 3 line' do
136
+ inspect_source(cop,
137
+ ['def func',
138
+ ' if something',
139
+ ' work',
140
+ ' work',
141
+ ' work',
142
+ ' end',
143
+ 'end',
144
+ '',
145
+ 'def func',
146
+ ' unless something',
147
+ ' work',
148
+ ' work',
149
+ ' work',
150
+ ' end',
151
+ 'end'])
152
+ expect(cop.offenses).to be_empty
153
+ end
76
154
  end
77
155
  end