rubocop 0.28.0 → 0.29.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 (211) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop_todo.yml +7 -7
  3. data/.travis.yml +4 -0
  4. data/CHANGELOG.md +59 -0
  5. data/README.md +62 -28
  6. data/config/default.yml +31 -0
  7. data/config/disabled.yml +1 -1
  8. data/config/enabled.yml +17 -0
  9. data/lib/rubocop.rb +4 -0
  10. data/lib/rubocop/cli.rb +1 -1
  11. data/lib/rubocop/config.rb +12 -8
  12. data/lib/rubocop/config_loader.rb +20 -10
  13. data/lib/rubocop/cop/cop.rb +13 -7
  14. data/lib/rubocop/cop/corrector.rb +10 -10
  15. data/lib/rubocop/cop/lint/assignment_in_condition.rb +6 -1
  16. data/lib/rubocop/cop/lint/block_alignment.rb +9 -2
  17. data/lib/rubocop/cop/lint/debugger.rb +13 -1
  18. data/lib/rubocop/cop/lint/duplicate_methods.rb +104 -0
  19. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +1 -1
  20. data/lib/rubocop/cop/lint/require_parentheses.rb +3 -3
  21. data/lib/rubocop/cop/mixin/access_modifier_node.rb +27 -0
  22. data/lib/rubocop/cop/mixin/on_normal_if_unless.rb +0 -4
  23. data/lib/rubocop/cop/rails/delegate.rb +4 -5
  24. data/lib/rubocop/cop/rails/read_write_attribute.rb +33 -0
  25. data/lib/rubocop/cop/style/access_modifier_indentation.rb +0 -7
  26. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +4 -2
  27. data/lib/rubocop/cop/style/class_methods.rb +25 -9
  28. data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +7 -1
  29. data/lib/rubocop/cop/style/empty_literal.rb +25 -10
  30. data/lib/rubocop/cop/style/even_odd.rb +32 -14
  31. data/lib/rubocop/cop/style/first_parameter_indentation.rb +106 -0
  32. data/lib/rubocop/cop/style/format_string.rb +2 -2
  33. data/lib/rubocop/cop/style/global_vars.rb +1 -5
  34. data/lib/rubocop/cop/style/hash_syntax.rb +0 -4
  35. data/lib/rubocop/cop/style/indentation_consistency.rb +5 -5
  36. data/lib/rubocop/cop/style/indentation_width.rb +13 -14
  37. data/lib/rubocop/cop/style/lambda.rb +48 -2
  38. data/lib/rubocop/cop/style/line_end_concatenation.rb +43 -47
  39. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +3 -3
  40. data/lib/rubocop/cop/style/module_function.rb +3 -3
  41. data/lib/rubocop/cop/style/multiline_block_layout.rb +65 -17
  42. data/lib/rubocop/cop/style/multiline_operation_indentation.rb +2 -1
  43. data/lib/rubocop/cop/style/perl_backrefs.rb +2 -2
  44. data/lib/rubocop/cop/style/redundant_exception.rb +12 -0
  45. data/lib/rubocop/cop/style/self_assignment.rb +27 -0
  46. data/lib/rubocop/cop/style/semicolon.rb +2 -1
  47. data/lib/rubocop/cop/style/space_around_block_parameters.rb +92 -0
  48. data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +0 -8
  49. data/lib/rubocop/cop/style/struct_inheritance.rb +42 -0
  50. data/lib/rubocop/cop/style/trivial_accessors.rb +10 -4
  51. data/lib/rubocop/cop/util.rb +32 -7
  52. data/lib/rubocop/formatter/simple_text_formatter.rb +0 -15
  53. data/lib/rubocop/options.rb +28 -41
  54. data/lib/rubocop/processed_source.rb +6 -0
  55. data/lib/rubocop/rake_task.rb +6 -3
  56. data/lib/rubocop/runner.rb +55 -15
  57. data/lib/rubocop/version.rb +1 -1
  58. data/relnotes/v0.29.0.md +116 -0
  59. data/rubocop.gemspec +3 -3
  60. data/spec/rubocop/cli_spec.rb +329 -25
  61. data/spec/rubocop/config_loader_spec.rb +20 -0
  62. data/spec/rubocop/cop/lint/assignment_in_condition_spec.rb +27 -3
  63. data/spec/rubocop/cop/lint/block_alignment_spec.rb +3 -4
  64. data/spec/rubocop/cop/lint/condition_position_spec.rb +1 -1
  65. data/spec/rubocop/cop/lint/debugger_spec.rb +20 -7
  66. data/spec/rubocop/cop/lint/duplicate_methods_spec.rb +189 -0
  67. data/spec/rubocop/cop/lint/empty_interpolation_spec.rb +1 -1
  68. data/spec/rubocop/cop/lint/end_in_method_spec.rb +1 -1
  69. data/spec/rubocop/cop/lint/eval_spec.rb +4 -4
  70. data/spec/rubocop/cop/lint/literal_in_condition_spec.rb +18 -0
  71. data/spec/rubocop/cop/lint/literal_in_interpolation_spec.rb +2 -2
  72. data/spec/rubocop/cop/lint/loop_spec.rb +4 -4
  73. data/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb +7 -7
  74. data/spec/rubocop/cop/lint/space_before_first_arg_spec.rb +5 -4
  75. data/spec/rubocop/cop/lint/useless_assignment_spec.rb +3 -9
  76. data/spec/rubocop/cop/lint/useless_comparison_spec.rb +1 -1
  77. data/spec/rubocop/cop/lint/void_spec.rb +1 -1
  78. data/spec/rubocop/cop/metrics/abc_size_spec.rb +10 -0
  79. data/spec/rubocop/cop/metrics/line_length_spec.rb +2 -2
  80. data/spec/rubocop/cop/metrics/parameter_lists_spec.rb +2 -2
  81. data/spec/rubocop/cop/rails/action_filter_spec.rb +6 -12
  82. data/spec/rubocop/cop/rails/default_scope_spec.rb +5 -5
  83. data/spec/rubocop/cop/rails/delegate_spec.rb +8 -0
  84. data/spec/rubocop/cop/rails/has_and_belongs_to_many_spec.rb +1 -1
  85. data/spec/rubocop/cop/rails/read_write_attribute_spec.rb +119 -8
  86. data/spec/rubocop/cop/rails/scope_args_spec.rb +3 -3
  87. data/spec/rubocop/cop/rails/validation_spec.rb +3 -3
  88. data/spec/rubocop/cop/style/alias_spec.rb +5 -5
  89. data/spec/rubocop/cop/style/align_hash_spec.rb +1 -1
  90. data/spec/rubocop/cop/style/align_parameters_spec.rb +8 -8
  91. data/spec/rubocop/cop/style/and_or_spec.rb +15 -30
  92. data/spec/rubocop/cop/style/array_join_spec.rb +4 -4
  93. data/spec/rubocop/cop/style/ascii_comments_spec.rb +1 -2
  94. data/spec/rubocop/cop/style/ascii_identifiers_spec.rb +2 -2
  95. data/spec/rubocop/cop/style/begin_block_spec.rb +1 -1
  96. data/spec/rubocop/cop/style/block_comments_spec.rb +1 -1
  97. data/spec/rubocop/cop/style/block_end_newline_spec.rb +1 -1
  98. data/spec/rubocop/cop/style/blocks_spec.rb +2 -2
  99. data/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb +38 -33
  100. data/spec/rubocop/cop/style/case_equality_spec.rb +1 -1
  101. data/spec/rubocop/cop/style/character_literal_spec.rb +4 -4
  102. data/spec/rubocop/cop/style/class_and_module_children_spec.rb +4 -2
  103. data/spec/rubocop/cop/style/class_methods_spec.rb +12 -0
  104. data/spec/rubocop/cop/style/class_vars_spec.rb +2 -2
  105. data/spec/rubocop/cop/style/collection_methods_spec.rb +4 -4
  106. data/spec/rubocop/cop/style/colon_method_call_spec.rb +8 -8
  107. data/spec/rubocop/cop/style/comment_annotation_spec.rb +10 -10
  108. data/spec/rubocop/cop/style/constant_name_spec.rb +7 -7
  109. data/spec/rubocop/cop/style/def_with_parentheses_spec.rb +1 -1
  110. data/spec/rubocop/cop/style/deprecated_hash_methods_spec.rb +4 -4
  111. data/spec/rubocop/cop/style/dot_position_spec.rb +8 -6
  112. data/spec/rubocop/cop/style/each_with_object_spec.rb +2 -2
  113. data/spec/rubocop/cop/style/else_alignment_spec.rb +2 -4
  114. data/spec/rubocop/cop/style/empty_lines_around_access_modifier_spec.rb +78 -0
  115. data/spec/rubocop/cop/style/empty_lines_around_class_body_spec.rb +60 -0
  116. data/spec/rubocop/cop/style/empty_lines_spec.rb +3 -3
  117. data/spec/rubocop/cop/style/empty_literal_spec.rb +29 -12
  118. data/spec/rubocop/cop/style/encoding_spec.rb +3 -3
  119. data/spec/rubocop/cop/style/end_block_spec.rb +1 -1
  120. data/spec/rubocop/cop/style/end_of_line_spec.rb +2 -2
  121. data/spec/rubocop/cop/style/even_odd_spec.rb +109 -20
  122. data/spec/rubocop/cop/style/extra_spacing_spec.rb +3 -3
  123. data/spec/rubocop/cop/style/first_parameter_indentation_spec.rb +293 -0
  124. data/spec/rubocop/cop/style/for_spec.rb +2 -2
  125. data/spec/rubocop/cop/style/format_string_spec.rb +45 -21
  126. data/spec/rubocop/cop/style/global_vars_spec.rb +4 -4
  127. data/spec/rubocop/cop/style/guard_clause_spec.rb +17 -0
  128. data/spec/rubocop/cop/style/hash_syntax_spec.rb +15 -15
  129. data/spec/rubocop/cop/{metrics → style}/if_unless_modifier_spec.rb +2 -2
  130. data/spec/rubocop/cop/style/if_with_semicolon_spec.rb +2 -2
  131. data/spec/rubocop/cop/style/indent_array_spec.rb +3 -6
  132. data/spec/rubocop/cop/style/indent_hash_spec.rb +4 -4
  133. data/spec/rubocop/cop/style/indentation_consistency_spec.rb +1 -2
  134. data/spec/rubocop/cop/style/indentation_width_spec.rb +1 -2
  135. data/spec/rubocop/cop/style/infinite_loop_spec.rb +1 -1
  136. data/spec/rubocop/cop/style/lambda_call_spec.rb +4 -4
  137. data/spec/rubocop/cop/style/lambda_spec.rb +37 -2
  138. data/spec/rubocop/cop/style/leading_comment_space_spec.rb +7 -12
  139. data/spec/rubocop/cop/style/line_end_concatenation_spec.rb +41 -1
  140. data/spec/rubocop/cop/style/method_call_parentheses_spec.rb +4 -4
  141. data/spec/rubocop/cop/style/method_called_on_do_end_block_spec.rb +3 -3
  142. data/spec/rubocop/cop/style/method_name_spec.rb +1 -1
  143. data/spec/rubocop/cop/style/multiline_block_layout_spec.rb +61 -0
  144. data/spec/rubocop/cop/style/multiline_if_then_spec.rb +1 -3
  145. data/spec/rubocop/cop/style/multiline_operation_indentation_spec.rb +8 -0
  146. data/spec/rubocop/cop/style/multiline_ternary_operator_spec.rb +1 -1
  147. data/spec/rubocop/cop/style/nested_ternary_operator_spec.rb +1 -1
  148. data/spec/rubocop/cop/style/next_spec.rb +16 -0
  149. data/spec/rubocop/cop/style/numeric_literals_spec.rb +5 -5
  150. data/spec/rubocop/cop/style/one_line_conditional_spec.rb +1 -1
  151. data/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +22 -4
  152. data/spec/rubocop/cop/style/percent_literal_delimiters_spec.rb +31 -31
  153. data/spec/rubocop/cop/style/percent_q_literals_spec.rb +12 -12
  154. data/spec/rubocop/cop/style/perl_backrefs_spec.rb +3 -3
  155. data/spec/rubocop/cop/style/proc_spec.rb +3 -3
  156. data/spec/rubocop/cop/style/raise_args_spec.rb +9 -9
  157. data/spec/rubocop/cop/style/redundant_begin_spec.rb +1 -1
  158. data/spec/rubocop/cop/style/redundant_exception_spec.rb +36 -4
  159. data/spec/rubocop/cop/style/redundant_self_spec.rb +89 -45
  160. data/spec/rubocop/cop/style/regexp_literal_spec.rb +9 -9
  161. data/spec/rubocop/cop/style/rescue_modifier_spec.rb +2 -2
  162. data/spec/rubocop/cop/style/self_assignment_spec.rb +16 -10
  163. data/spec/rubocop/cop/style/semicolon_spec.rb +9 -9
  164. data/spec/rubocop/cop/style/single_line_block_params_spec.rb +2 -2
  165. data/spec/rubocop/cop/style/single_space_before_first_arg_spec.rb +1 -1
  166. data/spec/rubocop/cop/style/space_after_colon_spec.rb +5 -5
  167. data/spec/rubocop/cop/style/space_after_comma_spec.rb +3 -3
  168. data/spec/rubocop/cop/style/space_after_control_keyword_spec.rb +4 -4
  169. data/spec/rubocop/cop/style/space_after_not_spec.rb +2 -2
  170. data/spec/rubocop/cop/style/space_after_semicolon_spec.rb +2 -2
  171. data/spec/rubocop/cop/style/space_around_block_parameters_spec.rb +150 -0
  172. data/spec/rubocop/cop/style/space_around_equals_in_parameter_default_spec.rb +18 -9
  173. data/spec/rubocop/cop/style/space_around_operators_spec.rb +24 -21
  174. data/spec/rubocop/cop/style/space_before_block_braces_spec.rb +4 -4
  175. data/spec/rubocop/cop/style/space_before_comma_spec.rb +4 -4
  176. data/spec/rubocop/cop/style/space_before_comment_spec.rb +3 -3
  177. data/spec/rubocop/cop/style/space_before_semicolon_spec.rb +2 -2
  178. data/spec/rubocop/cop/style/space_inside_block_braces_spec.rb +33 -24
  179. data/spec/rubocop/cop/style/space_inside_brackets_spec.rb +2 -2
  180. data/spec/rubocop/cop/style/space_inside_hash_literal_braces_spec.rb +9 -9
  181. data/spec/rubocop/cop/style/space_inside_parens_spec.rb +1 -1
  182. data/spec/rubocop/cop/style/special_global_vars_spec.rb +6 -6
  183. data/spec/rubocop/cop/style/string_literals_in_interpolation_spec.rb +3 -3
  184. data/spec/rubocop/cop/style/string_literals_spec.rb +16 -16
  185. data/spec/rubocop/cop/style/struct_inheritance_spec.rb +44 -0
  186. data/spec/rubocop/cop/style/symbol_array_spec.rb +9 -9
  187. data/spec/rubocop/cop/style/symbol_proc_spec.rb +12 -12
  188. data/spec/rubocop/cop/style/tab_spec.rb +4 -4
  189. data/spec/rubocop/cop/style/trailing_blank_lines_spec.rb +2 -2
  190. data/spec/rubocop/cop/style/trailing_whitespace_spec.rb +2 -2
  191. data/spec/rubocop/cop/style/trivial_accessors_spec.rb +16 -0
  192. data/spec/rubocop/cop/style/unneeded_capital_w_spec.rb +11 -22
  193. data/spec/rubocop/cop/style/variable_interpolation_spec.rb +7 -7
  194. data/spec/rubocop/cop/style/while_until_do_spec.rb +2 -2
  195. data/spec/rubocop/cop/{metrics → style}/while_until_modifier_spec.rb +2 -2
  196. data/spec/rubocop/cop/style/word_array_spec.rb +11 -11
  197. data/spec/rubocop/cop/util_spec.rb +51 -0
  198. data/spec/rubocop/cop/variable_force/reference_spec.rb +19 -0
  199. data/spec/rubocop/cop/variable_force/variable_table_spec.rb +7 -0
  200. data/spec/rubocop/formatter/disabled_lines_formatter_spec.rb +7 -8
  201. data/spec/rubocop/formatter/text_util_spec.rb +55 -0
  202. data/spec/rubocop/options_spec.rb +26 -20
  203. data/spec/rubocop/rake_task_spec.rb +122 -0
  204. data/spec/rubocop/runner_spec.rb +37 -2
  205. data/spec/rubocop/token_spec.rb +5 -1
  206. data/spec/spec_helper.rb +5 -2
  207. data/spec/support/cop_helper.rb +3 -0
  208. data/spec/support/cops/class_must_be_a_module_cop.rb +19 -0
  209. data/spec/support/cops/module_must_be_a_class_cop.rb +19 -0
  210. data/spec/support/custom_matchers.rb +1 -1
  211. metadata +35 -12
@@ -8,13 +8,13 @@ describe RuboCop::Cop::Lint::LiteralInInterpolation do
8
8
  %w(1 2.0 [1] {}).each do |lit|
9
9
  it "registers an offense for #{lit} in interpolation" do
10
10
  inspect_source(cop,
11
- ["\"this is the \#{#{lit}}\""])
11
+ "\"this is the \#{#{lit}}\"")
12
12
  expect(cop.offenses.size).to eq(1)
13
13
  end
14
14
 
15
15
  it "registers an offense only for final #{lit} in interpolation" do
16
16
  inspect_source(cop,
17
- ["\"this is the \#{#{lit};#{lit}}\""])
17
+ "\"this is the \#{#{lit};#{lit}}\"")
18
18
  expect(cop.offenses.size).to eq(1)
19
19
  end
20
20
  end
@@ -6,22 +6,22 @@ describe RuboCop::Cop::Lint::Loop do
6
6
  subject(:cop) { described_class.new }
7
7
 
8
8
  it 'registers an offense for begin/end/while' do
9
- inspect_source(cop, ['begin something; top; end while test'])
9
+ inspect_source(cop, 'begin something; top; end while test')
10
10
  expect(cop.offenses.size).to eq(1)
11
11
  end
12
12
 
13
13
  it 'registers an offense for begin/end/until' do
14
- inspect_source(cop, ['begin something; top; end until test'])
14
+ inspect_source(cop, 'begin something; top; end until test')
15
15
  expect(cop.offenses.size).to eq(1)
16
16
  end
17
17
 
18
18
  it 'accepts normal while' do
19
- inspect_source(cop, ['while test; one; two; end'])
19
+ inspect_source(cop, 'while test; one; two; end')
20
20
  expect(cop.offenses).to be_empty
21
21
  end
22
22
 
23
23
  it 'accepts normal until' do
24
- inspect_source(cop, ['until test; one; two; end'])
24
+ inspect_source(cop, 'until test; one; two; end')
25
25
  expect(cop.offenses).to be_empty
26
26
  end
27
27
  end
@@ -7,28 +7,28 @@ describe RuboCop::Cop::Lint::ParenthesesAsGroupedExpression do
7
7
 
8
8
  it 'registers an offense for method call with space before the ' \
9
9
  'parenthesis' do
10
- inspect_source(cop, ['a.func (x)'])
10
+ inspect_source(cop, 'a.func (x)')
11
11
  expect(cop.offenses.size).to eq(1)
12
12
  end
13
13
 
14
14
  it 'registers an offense for predicate method call with space ' \
15
15
  'before the parenthesis' do
16
- inspect_source(cop, ['is? (x)'])
16
+ inspect_source(cop, 'is? (x)')
17
17
  expect(cop.offenses.size).to eq(1)
18
18
  end
19
19
 
20
20
  it 'registers an offense for math expression' do
21
- inspect_source(cop, ['puts (2 + 3) * 4'])
21
+ inspect_source(cop, 'puts (2 + 3) * 4')
22
22
  expect(cop.offenses.size).to eq(1)
23
23
  end
24
24
 
25
25
  it 'accepts a method call without arguments' do
26
- inspect_source(cop, ['func'])
26
+ inspect_source(cop, 'func')
27
27
  expect(cop.offenses).to be_empty
28
28
  end
29
29
 
30
30
  it 'accepts a method call with arguments but no parentheses' do
31
- inspect_source(cop, ['puts x'])
31
+ inspect_source(cop, 'puts x')
32
32
  expect(cop.offenses).to be_empty
33
33
  end
34
34
 
@@ -40,7 +40,7 @@ describe RuboCop::Cop::Lint::ParenthesesAsGroupedExpression do
40
40
  end
41
41
 
42
42
  it 'accepts method with parens as arg to method without' do
43
- inspect_source(cop, ['a b(c)'])
43
+ inspect_source(cop, 'a b(c)')
44
44
  expect(cop.offenses).to be_empty
45
45
  end
46
46
 
@@ -51,7 +51,7 @@ describe RuboCop::Cop::Lint::ParenthesesAsGroupedExpression do
51
51
  end
52
52
 
53
53
  it 'accepts a space inside opening paren followed by left paren' do
54
- inspect_source(cop, ['a( (b) )'])
54
+ inspect_source(cop, 'a( (b) )')
55
55
  expect(cop.offenses).to be_empty
56
56
  end
57
57
  end
@@ -23,7 +23,7 @@ describe RuboCop::Cop::Lint::SpaceBeforeFirstArg do
23
23
  end
24
24
 
25
25
  it 'accepts square brackets operator' do
26
- inspect_source(cop, ['something[:x]'])
26
+ inspect_source(cop, 'something[:x]')
27
27
  expect(cop.offenses).to be_empty
28
28
  end
29
29
 
@@ -33,7 +33,8 @@ describe RuboCop::Cop::Lint::SpaceBeforeFirstArg do
33
33
  end
34
34
 
35
35
  it 'accepts an assignment without space before first arg' do
36
- inspect_source(cop, ['a.something=c', 'a.something,b=c,d'])
36
+ inspect_source(cop, ['a.something=c',
37
+ 'a.something,b=c,d'])
37
38
  expect(cop.offenses).to be_empty
38
39
  end
39
40
 
@@ -53,12 +54,12 @@ describe RuboCop::Cop::Lint::SpaceBeforeFirstArg do
53
54
  end
54
55
 
55
56
  it 'accepts a method call with space after the left parenthesis' do
56
- inspect_source(cop, ['something?( x )'])
57
+ inspect_source(cop, 'something?( x )')
57
58
  expect(cop.offenses).to be_empty
58
59
  end
59
60
 
60
61
  it 'accepts setter call' do
61
- inspect_source(cop, ['self.class.controller_path=(path)'])
62
+ inspect_source(cop, 'self.class.controller_path=(path)')
62
63
  expect(cop.offenses).to be_empty
63
64
  end
64
65
  end
@@ -225,9 +225,7 @@ describe RuboCop::Cop::Lint::UselessAssignment do
225
225
  context 'when a variable is assigned with operator assignment ' \
226
226
  'in top level' do
227
227
  let(:source) do
228
- [
229
- 'foo ||= 1'
230
- ]
228
+ 'foo ||= 1'
231
229
  end
232
230
 
233
231
  it 'registers an offense' do
@@ -1354,9 +1352,7 @@ describe RuboCop::Cop::Lint::UselessAssignment do
1354
1352
 
1355
1353
  context 'when a named capture is unreferenced in top level' do
1356
1354
  let(:source) do
1357
- [
1358
- "/(?<foo>\w+)/ =~ 'FOO'"
1359
- ]
1355
+ "/(?<foo>\w+)/ =~ 'FOO'"
1360
1356
  end
1361
1357
 
1362
1358
  it 'registers an offense' do
@@ -1592,9 +1588,7 @@ describe RuboCop::Cop::Lint::UselessAssignment do
1592
1588
 
1593
1589
  context 'when there is only one AST node and it is unused variable' do
1594
1590
  let(:source) do
1595
- [
1596
- 'foo = 1'
1597
- ]
1591
+ 'foo = 1'
1598
1592
  end
1599
1593
 
1600
1594
  it 'registers an offense' do
@@ -24,7 +24,7 @@ describe RuboCop::Cop::Lint::UselessComparison do
24
24
  end
25
25
 
26
26
  it 'works with lambda.()' do
27
- inspect_source(cop, ['a.(x) > a.(x)'])
27
+ inspect_source(cop, 'a.(x) > a.(x)')
28
28
  expect(cop.offenses.size).to eq(1)
29
29
  end
30
30
  end
@@ -28,7 +28,7 @@ describe RuboCop::Cop::Lint::Void do
28
28
 
29
29
  described_class::OPS.each do |op|
30
30
  it "accepts void op #{op} by itself without a begin block" do
31
- inspect_source(cop, ["a #{op} b"])
31
+ inspect_source(cop, "a #{op} b")
32
32
  expect(cop.offenses).to be_empty
33
33
  end
34
34
  end
@@ -35,6 +35,16 @@ describe RuboCop::Cop::Metrics::AbcSize, :config do
35
35
  expect(cop.config_to_allow_offenses).to eq('Max' => 1)
36
36
  end
37
37
 
38
+ it 'registers an offense for an assignment of an element' do
39
+ inspect_source(cop, ['def method_name',
40
+ ' x[0] = 1',
41
+ 'end'])
42
+ expect(cop.messages)
43
+ .to eq(['Assignment Branch Condition size for method_name is too ' \
44
+ 'high. [2/0]'])
45
+ expect(cop.config_to_allow_offenses).to eq('Max' => 2)
46
+ end
47
+
38
48
  it 'registers an offense for complex content including A, B, and C ' \
39
49
  'scores' do
40
50
  inspect_source(cop,
@@ -7,7 +7,7 @@ describe RuboCop::Cop::Metrics::LineLength, :config do
7
7
  let(:cop_config) { { 'Max' => 80 } }
8
8
 
9
9
  it "registers an offense for a line that's 81 characters wide" do
10
- inspect_source(cop, ['#' * 81])
10
+ inspect_source(cop, '#' * 81)
11
11
  expect(cop.offenses.size).to eq(1)
12
12
  expect(cop.offenses.first.message).to eq('Line is too long. [81/80]')
13
13
  expect(cop.config_to_allow_offenses).to eq('Max' => 81)
@@ -19,7 +19,7 @@ describe RuboCop::Cop::Metrics::LineLength, :config do
19
19
  end
20
20
 
21
21
  it "accepts a line that's 80 characters wide" do
22
- inspect_source(cop, ['#' * 80])
22
+ inspect_source(cop, '#' * 80)
23
23
  expect(cop.offenses).to be_empty
24
24
  end
25
25
 
@@ -25,7 +25,7 @@ describe RuboCop::Cop::Metrics::ParameterLists, :config do
25
25
  end
26
26
 
27
27
  context 'When CountKeywordArgs is true' do
28
- it 'counts keyword arguments as well', ruby: 2.0 do
28
+ it 'counts keyword arguments as well', ruby: 2 do
29
29
  inspect_source(cop, ['def meth(a, b, c, d: 1, e: 2)',
30
30
  'end'])
31
31
  expect(cop.offenses.size).to eq(1)
@@ -35,7 +35,7 @@ describe RuboCop::Cop::Metrics::ParameterLists, :config do
35
35
  context 'When CountKeywordArgs is false' do
36
36
  before { cop_config['CountKeywordArgs'] = false }
37
37
 
38
- it 'it does not count keyword arguments', ruby: 2.0 do
38
+ it 'it does not count keyword arguments', ruby: 2 do
39
39
  inspect_source(cop, ['def meth(a, b, c, d: 1, e: 2)',
40
40
  'end'])
41
41
  expect(cop.offenses).to be_empty
@@ -11,22 +11,19 @@ describe RuboCop::Cop::Rails::ActionFilter, :config do
11
11
 
12
12
  described_class::FILTER_METHODS.each do |method|
13
13
  it "registers an offense for #{method}" do
14
- inspect_source_file(cop,
15
- ["#{method} :name"])
14
+ inspect_source_file(cop, "#{method} :name")
16
15
  expect(cop.offenses.size).to eq(1)
17
16
  end
18
17
 
19
18
  it "registers an offense for #{method} with block" do
20
- inspect_source_file(cop,
21
- ["#{method} { |controller| something }"])
19
+ inspect_source_file(cop, "#{method} { |controller| something }")
22
20
  expect(cop.offenses.size).to eq(1)
23
21
  end
24
22
  end
25
23
 
26
24
  described_class::ACTION_METHODS.each do |method|
27
25
  it "accepts #{method}" do
28
- inspect_source_file(cop,
29
- ["#{method} :something"])
26
+ inspect_source_file(cop, "#{method} :something")
30
27
  expect(cop.offenses).to be_empty
31
28
  end
32
29
  end
@@ -42,22 +39,19 @@ describe RuboCop::Cop::Rails::ActionFilter, :config do
42
39
 
43
40
  described_class::ACTION_METHODS.each do |method|
44
41
  it "registers an offense for #{method}" do
45
- inspect_source_file(cop,
46
- ["#{method} :name"])
42
+ inspect_source_file(cop, "#{method} :name")
47
43
  expect(cop.offenses.size).to eq(1)
48
44
  end
49
45
 
50
46
  it "registers an offense for #{method} with block" do
51
- inspect_source_file(cop,
52
- ["#{method} { |controller| something }"])
47
+ inspect_source_file(cop, "#{method} { |controller| something }")
53
48
  expect(cop.offenses.size).to eq(1)
54
49
  end
55
50
  end
56
51
 
57
52
  described_class::FILTER_METHODS.each do |method|
58
53
  it "accepts #{method}" do
59
- inspect_source_file(cop,
60
- ["#{method} :something"])
54
+ inspect_source_file(cop, "#{method} :something")
61
55
  expect(cop.offenses).to be_empty
62
56
  end
63
57
  end
@@ -7,31 +7,31 @@ describe RuboCop::Cop::Rails::DefaultScope do
7
7
 
8
8
  it 'registers an offense for default scope with a lambda arg' do
9
9
  inspect_source(cop,
10
- ['default_scope -> { something }'])
10
+ 'default_scope -> { something }')
11
11
  expect(cop.offenses.size).to eq(1)
12
12
  end
13
13
 
14
14
  it 'registers an offense for default scope with a proc arg' do
15
15
  inspect_source(cop,
16
- ['default_scope proc { something }'])
16
+ 'default_scope proc { something }')
17
17
  expect(cop.offenses.size).to eq(1)
18
18
  end
19
19
 
20
20
  it 'registers an offense for default scope with a proc(Proc.new) arg' do
21
21
  inspect_source(cop,
22
- ['default_scope Proc.new { something }'])
22
+ 'default_scope Proc.new { something }')
23
23
  expect(cop.offenses.size).to eq(1)
24
24
  end
25
25
 
26
26
  it 'registers an offense for non blocks' do
27
27
  inspect_source(cop,
28
- ['default_scope order: "position"'])
28
+ 'default_scope order: "position"')
29
29
  expect(cop.offenses.size).to eq(1)
30
30
  end
31
31
 
32
32
  it 'accepts a block arg' do
33
33
  inspect_source(cop,
34
- ['default_scope { something }'])
34
+ 'default_scope { something }')
35
35
  expect(cop.offenses).to be_empty
36
36
  end
37
37
  end
@@ -44,6 +44,14 @@ describe RuboCop::Cop::Rails::Delegate do
44
44
  expect(cop.highlights).to eq(['def'])
45
45
  end
46
46
 
47
+ it 'ignores class methods' do
48
+ inspect_source(cop,
49
+ ['def self.fox',
50
+ ' new.fox',
51
+ 'end'])
52
+ expect(cop.offenses).to be_empty
53
+ end
54
+
47
55
  it 'ignores non trivial delegate' do
48
56
  inspect_source(cop,
49
57
  ['def fox',
@@ -7,7 +7,7 @@ describe RuboCop::Cop::Rails::HasAndBelongsToMany do
7
7
 
8
8
  it 'registers an offense for has_and_belongs_to_many' do
9
9
  inspect_source(cop,
10
- ['has_and_belongs_to_many :groups'])
10
+ 'has_and_belongs_to_many :groups')
11
11
  expect(cop.offenses.size).to eq(1)
12
12
  end
13
13
  end
@@ -5,15 +5,126 @@ require 'spec_helper'
5
5
  describe RuboCop::Cop::Rails::ReadWriteAttribute do
6
6
  subject(:cop) { described_class.new }
7
7
 
8
- it 'registers an offense for read_attribute' do
9
- inspect_source(cop, 'res = read_attribute(:test)')
10
- expect(cop.offenses.size).to eq(1)
11
- expect(cop.highlights).to eq(['read_attribute'])
8
+ context 'read_attribute' do
9
+ it 'registers an offense' do
10
+ inspect_source(cop, 'res = read_attribute(:test)')
11
+ expect(cop.offenses.size).to eq(1)
12
+ expect(cop.highlights).to eq(['read_attribute'])
13
+ end
14
+
15
+ it 'registers no offense with explicit receiver' do
16
+ inspect_source(cop, 'res = object.read_attribute(:test)')
17
+ expect(cop.offenses.size).to eq(0)
18
+ end
19
+ end
20
+
21
+ context 'write_attribute' do
22
+ it 'registers an offense' do
23
+ inspect_source(cop, 'write_attribute(:test, val)')
24
+ expect(cop.offenses.size).to eq(1)
25
+ expect(cop.highlights).to eq(['write_attribute'])
26
+ end
27
+
28
+ it 'registers no offense with explicit receiver' do
29
+ inspect_source(cop, 'object.write_attribute(:test, val)')
30
+ expect(cop.offenses.size).to eq(0)
31
+ end
12
32
  end
13
33
 
14
- it 'registers an offense for write_attribute' do
15
- inspect_source(cop, 'write_attribute(:test, val)')
16
- expect(cop.offenses.size).to eq(1)
17
- expect(cop.highlights).to eq(['write_attribute'])
34
+ describe '#autocorrect' do
35
+ context 'write_attribute' do
36
+ it 'autocorrects symbol' do
37
+ source = 'write_attribute(:attr, var)'
38
+ corrected_source = 'self[:attr] = var'
39
+
40
+ expect(autocorrect_source(cop, source)).to eq(corrected_source)
41
+ end
42
+
43
+ it 'autocorrects string' do
44
+ source = "write_attribute('attr', 'test')"
45
+ corrected_source = "self['attr'] = 'test'"
46
+
47
+ expect(autocorrect_source(cop, source)).to eq(corrected_source)
48
+ end
49
+
50
+ it 'autocorrects without parentheses' do
51
+ source = "write_attribute 'attr', 'test'"
52
+ corrected_source = "self['attr'] = 'test'"
53
+
54
+ expect(autocorrect_source(cop, source)).to eq(corrected_source)
55
+ end
56
+
57
+ it 'autocorrects expression' do
58
+ source = "write_attribute(:attr, 'test_' + postfix)"
59
+ corrected_source = "self[:attr] = 'test_' + postfix"
60
+
61
+ expect(autocorrect_source(cop, source)).to eq(corrected_source)
62
+ end
63
+
64
+ it 'autocorrects multiline' do
65
+ source = [
66
+ 'write_attribute(',
67
+ ':attr, ',
68
+ '(',
69
+ "'test_' + postfix",
70
+ ').to_sym',
71
+ ')'
72
+ ]
73
+ corrected_source = [
74
+ 'self[:attr] = (',
75
+ "'test_' + postfix",
76
+ ').to_sym'
77
+ ].join("\n")
78
+
79
+ expect(autocorrect_source(cop, source)).to eq(corrected_source)
80
+ end
81
+ end
82
+
83
+ context 'read_attribute' do
84
+ it 'autocorrects symbol' do
85
+ source = 'res = read_attribute(:test)'
86
+ corrected_source = 'res = self[:test]'
87
+
88
+ expect(autocorrect_source(cop, source)).to eq(corrected_source)
89
+ end
90
+
91
+ it 'autocorrects string' do
92
+ source = "res = read_attribute('test')"
93
+ corrected_source = "res = self['test']"
94
+
95
+ expect(autocorrect_source(cop, source)).to eq(corrected_source)
96
+ end
97
+
98
+ it 'autocorrects without parentheses' do
99
+ source = "res = read_attribute 'test'"
100
+ corrected_source = "res = self['test']"
101
+
102
+ expect(autocorrect_source(cop, source)).to eq(corrected_source)
103
+ end
104
+
105
+ it 'autocorrects expression' do
106
+ source = "res = read_attribute('test_' + postfix)"
107
+ corrected_source = "res = self['test_' + postfix]"
108
+
109
+ expect(autocorrect_source(cop, source)).to eq(corrected_source)
110
+ end
111
+
112
+ it 'autocorrects multiline' do
113
+ source = [
114
+ 'res = read_attribute(',
115
+ '(',
116
+ "'test_' + postfix",
117
+ ').to_sym',
118
+ ')'
119
+ ]
120
+ corrected_source = [
121
+ 'res = self[(',
122
+ "'test_' + postfix",
123
+ ').to_sym]'
124
+ ].join("\n")
125
+
126
+ expect(autocorrect_source(cop, source)).to eq(corrected_source)
127
+ end
128
+ end
18
129
  end
19
130
  end