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
@@ -0,0 +1,293 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe RuboCop::Cop::Style::FirstParameterIndentation, :config do
6
+ subject(:cop) { described_class.new(config) }
7
+ let(:config) do
8
+ RuboCop::Config
9
+ .new('Style/FirstParameterIndentation' => {
10
+ 'EnforcedStyle' => style,
11
+ 'SupportedStyles' =>
12
+ %w(consistent special_for_inner_method_call
13
+ special_for_inner_method_call_in_parentheses)
14
+ },
15
+ 'Style/IndentationWidth' => { 'Width' => indentation_width })
16
+ end
17
+
18
+ shared_examples 'common behavior' do
19
+ context 'when IndentationWidth:Width is 2' do
20
+ let(:indentation_width) { 2 }
21
+
22
+ it 'registers an offense for an over-indented first parameter' do
23
+ inspect_source(cop, ['run(',
24
+ ' :foo,',
25
+ ' bar: 3',
26
+ ')'])
27
+ expect(cop.messages).to eq(['Indent the first parameter one step ' \
28
+ 'more than the previous line.'])
29
+ expect(cop.highlights).to eq([':foo'])
30
+ end
31
+
32
+ it 'registers an offense for an under-indented first parameter' do
33
+ inspect_source(cop, ['run(',
34
+ ' :foo,',
35
+ ' bar: 3',
36
+ ')'])
37
+ expect(cop.highlights).to eq([':foo'])
38
+ end
39
+
40
+ context 'for assignment' do
41
+ it 'accepts a correctly indented first parameter and does not care ' \
42
+ 'about the second parameter' do
43
+ inspect_source(cop, ['x = run(',
44
+ ' :foo,',
45
+ ' bar: 3',
46
+ ')'])
47
+ expect(cop.offenses).to be_empty
48
+ end
49
+
50
+ context 'with line break' do
51
+ it 'accepts a correctly indented first parameter' do
52
+ inspect_source(cop, ['x =',
53
+ ' run(',
54
+ ' :foo)'])
55
+ expect(cop.offenses).to be_empty
56
+ end
57
+
58
+ it 'registers an offense for an under-indented first parameter' do
59
+ inspect_source(cop, ['@x =',
60
+ ' run(',
61
+ ' :foo)'])
62
+ expect(cop.highlights).to eq([':foo'])
63
+ end
64
+ end
65
+ end
66
+
67
+ it 'accepts a first parameter that is not preceded by a line break' do
68
+ inspect_source(cop, ['run :foo,',
69
+ ' bar: 3'])
70
+ expect(cop.offenses).to be_empty
71
+ end
72
+
73
+ context 'when the receiver contains a line break' do
74
+ it 'accepts a correctly indented first parameter' do
75
+ inspect_source(cop, ['puts x.',
76
+ ' merge(',
77
+ ' b: 2',
78
+ ' )'])
79
+ expect(cop.offenses).to be_empty
80
+ end
81
+
82
+ it 'registers an offense for an over-indented first parameter' do
83
+ inspect_source(cop, ['puts x.',
84
+ ' merge(',
85
+ ' b: 2',
86
+ ' )'])
87
+ expect(cop.messages).to eq(['Indent the first parameter one step ' \
88
+ 'more than the previous line.'])
89
+ expect(cop.highlights).to eq(['b: 2'])
90
+ end
91
+
92
+ it 'accepts a correctly indented first parameter preceded by an ' \
93
+ 'empty line' do
94
+ inspect_source(cop, ['puts x.',
95
+ ' merge(',
96
+ '',
97
+ ' b: 2',
98
+ ' )'])
99
+ expect(cop.offenses).to be_empty
100
+ end
101
+
102
+ context 'when preceded by a comment line' do
103
+ it 'accepts a correctly indented first parameter' do
104
+ inspect_source(cop, ['puts x.',
105
+ ' merge(',
106
+ ' # comment',
107
+ ' b: 2',
108
+ ' )'])
109
+ expect(cop.offenses).to be_empty
110
+ end
111
+
112
+ it 'registers an offense for an under-indented first parameter' do
113
+ inspect_source(cop, ['puts x.',
114
+ ' merge(',
115
+ ' # comment',
116
+ ' b: 2',
117
+ ' )'])
118
+ expect(cop.messages).to eq(['Indent the first parameter one ' \
119
+ 'step more than the previous line ' \
120
+ '(not counting the comment).'])
121
+ expect(cop.highlights).to eq(['b: 2'])
122
+ end
123
+ end
124
+ end
125
+
126
+ it 'accepts method calls with no parameters' do
127
+ inspect_source(cop, ['run()',
128
+ 'run_again'])
129
+ expect(cop.offenses).to be_empty
130
+ end
131
+
132
+ it 'accepts operator calls' do
133
+ inspect_source(cop, ['params = default_cfg.keys - %w(Description) -',
134
+ ' cfg.keys'])
135
+ expect(cop.offenses).to be_empty
136
+ end
137
+
138
+ it 'does not view []= as an outer method call' do
139
+ inspect_source(cop, ['@subject_results[subject] = original.update(',
140
+ ' mutation_results: (dup << mutation_result),',
141
+ ' tests: test_result.tests',
142
+ ')'])
143
+ expect(cop.offenses).to be_empty
144
+ end
145
+
146
+ it 'does not view chained call as an outer method call' do
147
+ inspect_source(cop, [' A = Regexp.union(',
148
+ ' /[A-Za-z_][A-Za-z\d_]*[!?=]?/,',
149
+ ' *AST::Types::OPERATOR_METHODS.map(&:to_s)',
150
+ ' ).freeze'])
151
+ expect(cop.offenses).to be_empty
152
+ end
153
+
154
+ it 'auto-corrects an under-indented first parameter' do
155
+ new_source = autocorrect_source(cop, ['x =',
156
+ ' run(',
157
+ ' :foo,',
158
+ ' bar: 3',
159
+ ')'])
160
+ expect(new_source).to eq(['x =',
161
+ ' run(',
162
+ ' :foo,',
163
+ ' bar: 3',
164
+ ')'].join("\n"))
165
+ end
166
+ end
167
+
168
+ context 'when IndentationWidth:Width is 4' do
169
+ let(:indentation_width) { 4 }
170
+
171
+ it 'auto-corrects an over-indented first parameter' do
172
+ new_source = autocorrect_source(cop, ['run(',
173
+ ' :foo,',
174
+ ' bar: 3)'])
175
+ expect(new_source).to eq(['run(',
176
+ ' :foo,',
177
+ ' bar: 3)'].join("\n"))
178
+ end
179
+ end
180
+ end
181
+
182
+ context 'when EnforcedStyle is special_for_inner_method_call' do
183
+ let(:style) { 'special_for_inner_method_call' }
184
+ let(:indentation_width) { 2 }
185
+
186
+ include_examples 'common behavior'
187
+
188
+ context 'for method calls within method calls' do
189
+ context 'with outer parentheses' do
190
+ it 'registers an offense for an over-indented first parameter' do
191
+ inspect_source(cop, ['run(:foo, defaults.merge(',
192
+ ' bar: 3))'])
193
+ expect(cop.messages).to eq(['Indent the first parameter one step ' \
194
+ 'more than `defaults.merge(`.'])
195
+ expect(cop.highlights).to eq(['bar: 3'])
196
+ end
197
+ end
198
+
199
+ context 'without outer parentheses' do
200
+ it 'accepts a first parameter with special indentation' do
201
+ inspect_source(cop, ['run :foo, defaults.merge(',
202
+ ' bar: 3)'])
203
+ expect(cop.offenses).to be_empty
204
+ end
205
+ end
206
+
207
+ it 'auto-corrects an over-indented first parameter' do
208
+ new_source = autocorrect_source(cop,
209
+ ['run(:foo, defaults.merge(',
210
+ ' bar: 3))'])
211
+ expect(new_source).to eq(['run(:foo, defaults.merge(',
212
+ ' bar: 3))'].join("\n"))
213
+ end
214
+ end
215
+ end
216
+
217
+ context 'when EnforcedStyle is ' \
218
+ 'special_for_inner_method_call_in_parentheses' do
219
+ let(:style) { 'special_for_inner_method_call_in_parentheses' }
220
+ let(:indentation_width) { 2 }
221
+
222
+ include_examples 'common behavior'
223
+
224
+ context 'for method calls within method calls' do
225
+ context 'with outer parentheses' do
226
+ it 'registers an offense for an over-indented first parameter' do
227
+ inspect_source(cop, ['run(:foo, defaults.merge(',
228
+ ' bar: 3))'])
229
+ expect(cop.messages).to eq(['Indent the first parameter one step ' \
230
+ 'more than `defaults.merge(`.'])
231
+ expect(cop.highlights).to eq(['bar: 3'])
232
+ end
233
+
234
+ it 'registers an offense for an under-indented first parameter' do
235
+ inspect_source(cop, ['run(:foo, defaults.',
236
+ ' merge(',
237
+ ' bar: 3))'])
238
+ expect(cop.messages).to eq(['Indent the first parameter one step ' \
239
+ 'more than the previous line.'])
240
+ expect(cop.highlights).to eq(['bar: 3'])
241
+ end
242
+ end
243
+
244
+ context 'without outer parentheses' do
245
+ it 'accepts a first parameter with consistent style indentation' do
246
+ inspect_source(cop, ['run :foo, defaults.merge(',
247
+ ' bar: 3)'])
248
+ expect(cop.offenses).to be_empty
249
+ end
250
+ end
251
+
252
+ it 'auto-corrects an over-indented first parameter' do
253
+ new_source = autocorrect_source(cop,
254
+ ['run(:foo, defaults.merge(',
255
+ ' bar: 3))'])
256
+ expect(new_source).to eq(['run(:foo, defaults.merge(',
257
+ ' bar: 3))'].join("\n"))
258
+ end
259
+ end
260
+ end
261
+
262
+ context 'when EnforcedStyle is consistent' do
263
+ let(:style) { 'consistent' }
264
+ let(:indentation_width) { 2 }
265
+
266
+ include_examples 'common behavior'
267
+
268
+ context 'for method calls within method calls' do
269
+ it 'registers an offense for an over-indented first parameter' do
270
+ inspect_source(cop, ['run(:foo, defaults.merge(',
271
+ ' bar: 3))'])
272
+ expect(cop.messages).to eq(['Indent the first parameter one step ' \
273
+ 'more than the previous line.'])
274
+ expect(cop.highlights).to eq(['bar: 3'])
275
+ end
276
+
277
+ it 'accepts first parameter indented relative to previous line' do
278
+ inspect_source(cop,
279
+ [' @diagnostics.process(Diagnostic.new(',
280
+ ' :error, :token, { :token => name }, location))'])
281
+ expect(cop.offenses).to be_empty
282
+ end
283
+
284
+ it 'auto-corrects an over-indented first parameter' do
285
+ new_source = autocorrect_source(cop,
286
+ ['run(:foo, defaults.merge(',
287
+ ' bar: 3))'])
288
+ expect(new_source).to eq(['run(:foo, defaults.merge(',
289
+ ' bar: 3))'].join("\n"))
290
+ end
291
+ end
292
+ end
293
+ end
@@ -45,12 +45,12 @@ describe RuboCop::Cop::Style::For, :config do
45
45
  end
46
46
 
47
47
  it 'accepts :for' do
48
- inspect_source(cop, ['[:for, :ala, :bala]'])
48
+ inspect_source(cop, '[:for, :ala, :bala]')
49
49
  expect(cop.offenses).to be_empty
50
50
  end
51
51
 
52
52
  it 'accepts def for' do
53
- inspect_source(cop, ['def for; end'])
53
+ inspect_source(cop, 'def for; end')
54
54
  expect(cop.offenses).to be_empty
55
55
  end
56
56
  end
@@ -9,7 +9,7 @@ describe RuboCop::Cop::Style::FormatString, :config do
9
9
  let(:cop_config) { { 'EnforcedStyle' => 'sprintf' } }
10
10
  it 'registers an offense for a string followed by something' do
11
11
  inspect_source(cop,
12
- ['puts "%d" % 10'])
12
+ 'puts "%d" % 10')
13
13
  expect(cop.offenses.size).to eq(1)
14
14
  expect(cop.messages)
15
15
  .to eq(['Favor `sprintf` over `String#%`.'])
@@ -17,7 +17,7 @@ describe RuboCop::Cop::Style::FormatString, :config do
17
17
 
18
18
  it 'registers an offense for something followed by an array' do
19
19
  inspect_source(cop,
20
- ['puts x % [10, 11]'])
20
+ 'puts x % [10, 11]')
21
21
  expect(cop.offenses.size).to eq(1)
22
22
  expect(cop.messages)
23
23
  .to eq(['Favor `sprintf` over `String#%`.'])
@@ -25,23 +25,23 @@ describe RuboCop::Cop::Style::FormatString, :config do
25
25
 
26
26
  it 'does not register an offense for numbers' do
27
27
  inspect_source(cop,
28
- ['puts 10 % 4'])
28
+ 'puts 10 % 4')
29
29
  expect(cop.offenses).to be_empty
30
30
  end
31
31
 
32
32
  it 'does not register an offense for ambiguous cases' do
33
33
  inspect_source(cop,
34
- ['puts x % 4'])
34
+ 'puts x % 4')
35
35
  expect(cop.offenses).to be_empty
36
36
 
37
37
  inspect_source(cop,
38
- ['puts x % Y'])
38
+ 'puts x % Y')
39
39
  expect(cop.offenses).to be_empty
40
40
  end
41
41
 
42
42
  it 'works if the first operand contains embedded expressions' do
43
43
  inspect_source(cop,
44
- ['puts "#{x * 5} %d #{@test}" % 10'])
44
+ 'puts "#{x * 5} %d #{@test}" % 10')
45
45
  expect(cop.offenses.size).to eq(1)
46
46
  expect(cop.messages)
47
47
  .to eq(['Favor `sprintf` over `String#%`.'])
@@ -49,7 +49,15 @@ describe RuboCop::Cop::Style::FormatString, :config do
49
49
 
50
50
  it 'registers an offense for format' do
51
51
  inspect_source(cop,
52
- ['format(something, a, b)'])
52
+ 'format(something, a, b)')
53
+ expect(cop.offenses.size).to eq(1)
54
+ expect(cop.messages)
55
+ .to eq(['Favor `sprintf` over `format`.'])
56
+ end
57
+
58
+ it 'registers an offense for format with 2 arguments' do
59
+ inspect_source(cop,
60
+ 'format("%X", 123)')
53
61
  expect(cop.offenses.size).to eq(1)
54
62
  expect(cop.messages)
55
63
  .to eq(['Favor `sprintf` over `format`.'])
@@ -61,7 +69,7 @@ describe RuboCop::Cop::Style::FormatString, :config do
61
69
 
62
70
  it 'registers an offense for a string followed by something' do
63
71
  inspect_source(cop,
64
- ['puts "%d" % 10'])
72
+ 'puts "%d" % 10')
65
73
  expect(cop.offenses.size).to eq(1)
66
74
  expect(cop.messages)
67
75
  .to eq(['Favor `format` over `String#%`.'])
@@ -69,7 +77,7 @@ describe RuboCop::Cop::Style::FormatString, :config do
69
77
 
70
78
  it 'registers an offense for something followed by an array' do
71
79
  inspect_source(cop,
72
- ['puts x % [10, 11]'])
80
+ 'puts x % [10, 11]')
73
81
  expect(cop.offenses.size).to eq(1)
74
82
  expect(cop.messages)
75
83
  .to eq(['Favor `format` over `String#%`.'])
@@ -77,23 +85,23 @@ describe RuboCop::Cop::Style::FormatString, :config do
77
85
 
78
86
  it 'does not register an offense for numbers' do
79
87
  inspect_source(cop,
80
- ['puts 10 % 4'])
88
+ 'puts 10 % 4')
81
89
  expect(cop.offenses).to be_empty
82
90
  end
83
91
 
84
92
  it 'does not register an offense for ambiguous cases' do
85
93
  inspect_source(cop,
86
- ['puts x % 4'])
94
+ 'puts x % 4')
87
95
  expect(cop.offenses).to be_empty
88
96
 
89
97
  inspect_source(cop,
90
- ['puts x % Y'])
98
+ 'puts x % Y')
91
99
  expect(cop.offenses).to be_empty
92
100
  end
93
101
 
94
102
  it 'works if the first operand contains embedded expressions' do
95
103
  inspect_source(cop,
96
- ['puts "#{x * 5} %d #{@test}" % 10'])
104
+ 'puts "#{x * 5} %d #{@test}" % 10')
97
105
  expect(cop.offenses.size).to eq(1)
98
106
  expect(cop.messages)
99
107
  .to eq(['Favor `format` over `String#%`.'])
@@ -101,7 +109,15 @@ describe RuboCop::Cop::Style::FormatString, :config do
101
109
 
102
110
  it 'registers an offense for sprintf' do
103
111
  inspect_source(cop,
104
- ['sprintf(something, a, b)'])
112
+ 'sprintf(something, a, b)')
113
+ expect(cop.offenses.size).to eq(1)
114
+ expect(cop.messages)
115
+ .to eq(['Favor `format` over `sprintf`.'])
116
+ end
117
+
118
+ it 'registers an offense for sprintf with 2 arguments' do
119
+ inspect_source(cop,
120
+ "sprintf('%020d', 123)")
105
121
  expect(cop.offenses.size).to eq(1)
106
122
  expect(cop.messages)
107
123
  .to eq(['Favor `format` over `sprintf`.'])
@@ -113,7 +129,7 @@ describe RuboCop::Cop::Style::FormatString, :config do
113
129
 
114
130
  it 'registers an offense for format' do
115
131
  inspect_source(cop,
116
- ['format(something, a, b)'])
132
+ 'format(something, a, b)')
117
133
  expect(cop.offenses.size).to eq(1)
118
134
  expect(cop.messages)
119
135
  .to eq(['Favor `String#%` over `format`.'])
@@ -121,39 +137,47 @@ describe RuboCop::Cop::Style::FormatString, :config do
121
137
 
122
138
  it 'registers an offense for sprintf' do
123
139
  inspect_source(cop,
124
- ['sprintf(something, a, b)'])
140
+ 'sprintf(something, a, b)')
125
141
  expect(cop.offenses.size).to eq(1)
126
142
  expect(cop.messages)
127
143
  .to eq(['Favor `String#%` over `sprintf`.'])
128
144
  end
129
145
 
146
+ it 'registers an offense for sprintf with 3 arguments' do
147
+ inspect_source(cop,
148
+ 'format("%d %04x", 123, 123)')
149
+ expect(cop.offenses.size).to eq(1)
150
+ expect(cop.messages)
151
+ .to eq(['Favor `String#%` over `format`.'])
152
+ end
153
+
130
154
  it 'accepts format with 1 argument' do
131
155
  inspect_source(cop,
132
- ['format :xml'])
156
+ 'format :xml')
133
157
  expect(cop.offenses).to be_empty
134
158
  end
135
159
 
136
160
  it 'accepts sprintf with 1 argument' do
137
161
  inspect_source(cop,
138
- ['sprintf :xml'])
162
+ 'sprintf :xml')
139
163
  expect(cop.offenses).to be_empty
140
164
  end
141
165
 
142
166
  it 'accepts format without arguments' do
143
167
  inspect_source(cop,
144
- ['format'])
168
+ 'format')
145
169
  expect(cop.offenses).to be_empty
146
170
  end
147
171
 
148
172
  it 'accepts sprintf without arguments' do
149
173
  inspect_source(cop,
150
- ['sprintf'])
174
+ 'sprintf')
151
175
  expect(cop.offenses).to be_empty
152
176
  end
153
177
 
154
178
  it 'accepts String#%' do
155
179
  inspect_source(cop,
156
- ['puts "%d" % 10'])
180
+ 'puts "%d" % 10')
157
181
  expect(cop.offenses).to be_empty
158
182
  end
159
183
  end