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.

Files changed (155) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +5 -0
  3. data/.rubocop_todo.yml +10 -6
  4. data/.travis.yml +2 -0
  5. data/CHANGELOG.md +30 -0
  6. data/README.md +9 -2
  7. data/assets/logo.png +0 -0
  8. data/assets/output.html.erb +68 -65
  9. data/config/default.yml +42 -7
  10. data/config/disabled.yml +5 -0
  11. data/config/enabled.yml +32 -7
  12. data/lib/rubocop.rb +10 -2
  13. data/lib/rubocop/comment_config.rb +11 -17
  14. data/lib/rubocop/config.rb +20 -16
  15. data/lib/rubocop/config_loader.rb +8 -12
  16. data/lib/rubocop/cop/cop.rb +13 -12
  17. data/lib/rubocop/cop/lint/block_alignment.rb +4 -6
  18. data/lib/rubocop/cop/lint/def_end_alignment.rb +2 -2
  19. data/lib/rubocop/cop/lint/require_parentheses.rb +1 -1
  20. data/lib/rubocop/cop/lint/useless_access_modifier.rb +2 -3
  21. data/lib/rubocop/cop/lint/useless_setter_call.rb +2 -2
  22. data/lib/rubocop/cop/metrics/abc_size.rb +27 -0
  23. data/lib/rubocop/cop/metrics/block_nesting.rb +2 -4
  24. data/lib/rubocop/cop/metrics/class_length.rb +1 -1
  25. data/lib/rubocop/cop/metrics/line_length.rb +2 -5
  26. data/lib/rubocop/cop/metrics/method_length.rb +2 -2
  27. data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +24 -15
  28. data/lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb +15 -2
  29. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +63 -0
  30. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
  31. data/lib/rubocop/cop/mixin/if_node.rb +3 -1
  32. data/lib/rubocop/cop/mixin/method_complexity.rb +3 -3
  33. data/lib/rubocop/cop/mixin/{on_method.rb → on_method_def.rb} +3 -3
  34. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +2 -2
  35. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -1
  36. data/lib/rubocop/cop/mixin/statement_modifier.rb +2 -2
  37. data/lib/rubocop/cop/mixin/string_literals_help.rb +28 -0
  38. data/lib/rubocop/cop/rails/delegate.rb +2 -2
  39. data/lib/rubocop/cop/style/access_modifier_indentation.rb +2 -2
  40. data/lib/rubocop/cop/style/accessor_method_name.rb +2 -2
  41. data/lib/rubocop/cop/style/align_hash.rb +16 -12
  42. data/lib/rubocop/cop/style/align_parameters.rb +1 -1
  43. data/lib/rubocop/cop/style/and_or.rb +14 -6
  44. data/lib/rubocop/cop/style/array_join.rb +1 -1
  45. data/lib/rubocop/cop/style/block_comments.rb +16 -8
  46. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +6 -30
  47. data/lib/rubocop/cop/style/case_indentation.rb +20 -12
  48. data/lib/rubocop/cop/style/collection_methods.rb +4 -4
  49. data/lib/rubocop/cop/style/colon_method_call.rb +9 -0
  50. data/lib/rubocop/cop/style/comment_annotation.rb +1 -1
  51. data/lib/rubocop/cop/style/comment_indentation.rb +22 -22
  52. data/lib/rubocop/cop/style/def_with_parentheses.rb +2 -2
  53. data/lib/rubocop/cop/style/deprecated_hash_methods.rb +1 -1
  54. data/lib/rubocop/cop/style/double_negation.rb +6 -1
  55. data/lib/rubocop/cop/style/else_alignment.rb +93 -0
  56. data/lib/rubocop/cop/style/empty_line_between_defs.rb +1 -1
  57. data/lib/rubocop/cop/style/empty_lines.rb +1 -1
  58. data/lib/rubocop/cop/style/empty_lines_around_class_body.rb +34 -0
  59. data/lib/rubocop/cop/style/empty_lines_around_method_body.rb +37 -0
  60. data/lib/rubocop/cop/style/empty_lines_around_module_body.rb +30 -0
  61. data/lib/rubocop/cop/style/encoding.rb +1 -1
  62. data/lib/rubocop/cop/style/format_string.rb +4 -4
  63. data/lib/rubocop/cop/style/indent_array.rb +2 -2
  64. data/lib/rubocop/cop/style/indent_hash.rb +17 -12
  65. data/lib/rubocop/cop/style/indentation_width.rb +27 -19
  66. data/lib/rubocop/cop/style/method_call_parentheses.rb +3 -3
  67. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +1 -1
  68. data/lib/rubocop/cop/style/method_def_parentheses.rb +17 -11
  69. data/lib/rubocop/cop/style/method_name.rb +1 -1
  70. data/lib/rubocop/cop/style/multiline_operation_indentation.rb +174 -0
  71. data/lib/rubocop/cop/style/non_nil_check.rb +12 -15
  72. data/lib/rubocop/cop/style/not.rb +1 -1
  73. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +12 -17
  74. data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
  75. data/lib/rubocop/cop/style/predicate_name.rb +2 -2
  76. data/lib/rubocop/cop/style/redundant_begin.rb +2 -2
  77. data/lib/rubocop/cop/style/redundant_return.rb +3 -3
  78. data/lib/rubocop/cop/style/redundant_self.rb +3 -3
  79. data/lib/rubocop/cop/style/regexp_literal.rb +17 -13
  80. data/lib/rubocop/cop/style/rescue_modifier.rb +2 -2
  81. data/lib/rubocop/cop/style/single_line_methods.rb +7 -4
  82. data/lib/rubocop/cop/style/space_after_method_name.rb +2 -2
  83. data/lib/rubocop/cop/style/space_around_equals_in_parameter_default.rb +17 -11
  84. data/lib/rubocop/cop/style/space_before_block_braces.rb +1 -1
  85. data/lib/rubocop/cop/style/space_inside_block_braces.rb +17 -14
  86. data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +10 -6
  87. data/lib/rubocop/cop/style/string_literals.rb +13 -16
  88. data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +41 -0
  89. data/lib/rubocop/cop/style/trailing_comma.rb +1 -3
  90. data/lib/rubocop/cop/style/trivial_accessors.rb +3 -3
  91. data/lib/rubocop/cop/style/unneeded_capital_w.rb +1 -1
  92. data/lib/rubocop/cop/style/unneeded_percent_q.rb +2 -2
  93. data/lib/rubocop/cop/style/word_array.rb +23 -19
  94. data/lib/rubocop/cop/team.rb +13 -26
  95. data/lib/rubocop/cop/util.rb +5 -0
  96. data/lib/rubocop/cop/variable_force/locatable.rb +7 -13
  97. data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
  98. data/lib/rubocop/formatter/formatter_set.rb +9 -1
  99. data/lib/rubocop/formatter/html_formatter.rb +83 -55
  100. data/lib/rubocop/formatter/simple_text_formatter.rb +2 -2
  101. data/lib/rubocop/formatter/text_util.rb +25 -0
  102. data/lib/rubocop/options.rb +14 -7
  103. data/lib/rubocop/path_util.rb +11 -7
  104. data/lib/rubocop/runner.rb +7 -2
  105. data/lib/rubocop/version.rb +1 -1
  106. data/relnotes/v0.27.0.md +77 -0
  107. data/rubocop.gemspec +1 -1
  108. data/spec/fixtures/html_formatter/expected.html +495 -0
  109. data/spec/fixtures/html_formatter/project/app/controllers/application_controller.rb +5 -0
  110. data/spec/fixtures/html_formatter/project/app/controllers/books_controller.rb +74 -0
  111. data/spec/fixtures/html_formatter/project/app/models/book.rb +5 -0
  112. data/spec/rubocop/cli_spec.rb +56 -13
  113. data/spec/rubocop/cop/lint/invalid_character_literal_spec.rb +1 -1
  114. data/spec/rubocop/cop/metrics/abc_size_spec.rb +99 -0
  115. data/spec/rubocop/cop/rails/action_filter_spec.rb +1 -0
  116. data/spec/rubocop/cop/style/access_modifier_indentation_spec.rb +23 -1
  117. data/spec/rubocop/cop/style/align_hash_spec.rb +13 -0
  118. data/spec/rubocop/cop/style/align_parameters_spec.rb +44 -33
  119. data/spec/rubocop/cop/style/blocks_spec.rb +8 -0
  120. data/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb +9 -9
  121. data/spec/rubocop/cop/style/case_indentation_spec.rb +3 -2
  122. data/spec/rubocop/cop/style/colon_method_call_spec.rb +5 -0
  123. data/spec/rubocop/cop/style/comment_indentation_spec.rb +6 -1
  124. data/spec/rubocop/cop/style/else_alignment_spec.rb +437 -0
  125. data/spec/rubocop/cop/style/empty_lines_around_class_body_spec.rb +75 -0
  126. data/spec/rubocop/cop/style/{empty_lines_around_body_spec.rb → empty_lines_around_method_body_spec.rb} +9 -50
  127. data/spec/rubocop/cop/style/empty_lines_around_module_body_spec.rb +79 -0
  128. data/spec/rubocop/cop/style/file_name_spec.rb +1 -1
  129. data/spec/rubocop/cop/style/format_string_spec.rb +12 -0
  130. data/spec/rubocop/cop/style/indent_array_spec.rb +6 -1
  131. data/spec/rubocop/cop/style/indent_hash_spec.rb +2 -1
  132. data/spec/rubocop/cop/style/indentation_width_spec.rb +765 -722
  133. data/spec/rubocop/cop/style/multiline_operation_indentation_spec.rb +414 -0
  134. data/spec/rubocop/cop/style/non_nil_check_spec.rb +86 -55
  135. data/spec/rubocop/cop/style/single_line_methods_spec.rb +5 -1
  136. data/spec/rubocop/cop/style/space_before_block_braces_spec.rb +2 -1
  137. data/spec/rubocop/cop/style/space_inside_block_braces_spec.rb +2 -1
  138. data/spec/rubocop/cop/style/string_literals_in_interpolation_spec.rb +63 -0
  139. data/spec/rubocop/cop/style/string_literals_spec.rb +2 -2
  140. data/spec/rubocop/cop/style/word_array_spec.rb +15 -1
  141. data/spec/rubocop/formatter/base_formatter_spec.rb +1 -1
  142. data/spec/rubocop/formatter/disabled_lines_formatter_spec.rb +0 -1
  143. data/spec/rubocop/formatter/formatter_set_spec.rb +9 -0
  144. data/spec/rubocop/formatter/html_formatter_spec.rb +25 -122
  145. data/spec/rubocop/formatter/offense_count_formatter_spec.rb +0 -1
  146. data/spec/rubocop/runner_spec.rb +1 -1
  147. data/spec/spec_helper.rb +12 -130
  148. data/spec/support/cop_helper.rb +72 -0
  149. data/spec/support/coverage.rb +15 -0
  150. data/spec/support/{offenses_matcher.rb → custom_matchers.rb} +28 -0
  151. data/spec/support/jruby_workaround.rb +15 -0
  152. data/spec/support/{isolated_environment.rb → shared_contexts.rb} +19 -0
  153. metadata +49 -14
  154. data/lib/rubocop/cop/style/empty_lines_around_body.rb +0 -75
  155. data/spec/support/shared_context.rb +0 -20
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,74 @@
1
+ class BooksController < ApplicationController
2
+ before_action :set_book, only: [:show, :edit, :update, :destroy]
3
+
4
+ # GET /books
5
+ # GET /books.json
6
+ def index
7
+ @books = Book.all
8
+ end
9
+
10
+ # GET /books/1
11
+ # GET /books/1.json
12
+ def show
13
+ end
14
+
15
+ # GET /books/new
16
+ def new
17
+ @book = Book.new
18
+ end
19
+
20
+ # GET /books/1/edit
21
+ def edit
22
+ end
23
+
24
+ # POST /books
25
+ # POST /books.json
26
+ def create
27
+ @book = Book.new(book_params)
28
+
29
+ respond_to do |format|
30
+ if @book.save
31
+ format.html { redirect_to @book, notice: 'Book was successfully created.' }
32
+ format.json { render :show, status: :created, location: @book }
33
+ else
34
+ format.html { render :new }
35
+ format.json { render json: @book.errors, status: :unprocessable_entity }
36
+ end
37
+ end
38
+ end
39
+
40
+ # PATCH/PUT /books/1
41
+ # PATCH/PUT /books/1.json
42
+ def update
43
+ respond_to do |format|
44
+ if @book.update(book_params)
45
+ format.html { redirect_to @book, notice: 'Book was successfully updated.' }
46
+ format.json { render :show, status: :ok, location: @book }
47
+ else
48
+ format.html { render :edit }
49
+ format.json { render json: @book.errors, status: :unprocessable_entity }
50
+ end
51
+ end
52
+ end
53
+
54
+ # DELETE /books/1
55
+ # DELETE /books/1.json
56
+ def destroy
57
+ @book.destroy
58
+ respond_to do |format|
59
+ format.html { redirect_to books_url, notice: 'Book was successfully destroyed.' }
60
+ format.json { head :no_content }
61
+ end
62
+ end
63
+
64
+ private
65
+ # Use callbacks to share common setup or constraints between actions.
66
+ def set_book
67
+ @book = Book.find(params[:id])
68
+ end
69
+
70
+ # Never trust parameters from the scary internet, only allow the white list through.
71
+ def book_params
72
+ params.require(:book).permit(:name)
73
+ end
74
+ end
@@ -0,0 +1,5 @@
1
+ class Book < ActiveRecord::Base
2
+ def someMethod
3
+ foo = bar = baz
4
+ end
5
+ end
@@ -35,6 +35,43 @@ describe RuboCop::CLI, :isolated_environment do
35
35
  end
36
36
 
37
37
  describe '--auto-correct' do
38
+ it 'corrects SymbolProc and SpaceBeforeBlockBraces offences' do
39
+ source = ['foo.map{ |a| a.nil? }']
40
+ create_file('example.rb', source)
41
+ expect(cli.run(['-D', '--auto-correct'])).to eq(1)
42
+ corrected = "foo.map(&:nil?)\n"
43
+ expect(IO.read('example.rb')).to eq(corrected)
44
+ end
45
+
46
+ it 'corrects complicated cases conservatively' do
47
+ # Two cops make corrections here; Style/BracesAroundHashParameters, and
48
+ # Style/AlignHash. Because they make minimal corrections relating only
49
+ # to their specific areas, and stay away from cleaning up extra
50
+ # whitespace in the process, the combined changes don't interfere with
51
+ # eachother and the result is semantically the same as the starting
52
+ # point.
53
+ source = ['# encoding: utf-8',
54
+ 'expect(subject[:address]).to eq({',
55
+ " street1: '1 Market',",
56
+ " street2: '#200',",
57
+ " city: 'Some Town',",
58
+ " state: 'CA',",
59
+ " postal_code: '99999-1111'",
60
+ '})']
61
+ create_file('example.rb', source)
62
+ expect(cli.run(['-D', '--auto-correct'])).to eq(1)
63
+ corrected =
64
+ ['# encoding: utf-8',
65
+ 'expect(subject[:address]).to eq(',
66
+ " street1: '1 Market',",
67
+ " street2: '#200',",
68
+ " city: 'Some Town',",
69
+ " state: 'CA',",
70
+ " postal_code: '99999-1111'",
71
+ ')']
72
+ expect(IO.read('example.rb')).to eq(corrected.join("\n") + "\n")
73
+ end
74
+
38
75
  it 'honors Exclude settings in individual cops' do
39
76
  source = ['# encoding: utf-8',
40
77
  'puts %x(ls)']
@@ -274,13 +311,13 @@ describe RuboCop::CLI, :isolated_environment do
274
311
  'end',
275
312
  ''].join("\n"))
276
313
  expect($stdout.string).to eq(['',
277
- '6 Style/TrailingWhitespace',
278
- '4 Style/Semicolon',
279
- '2 Style/SingleLineMethods',
314
+ '10 Style/TrailingWhitespace',
315
+ '5 Style/Semicolon',
316
+ '3 Style/SingleLineMethods',
280
317
  '1 Style/DefWithParentheses',
281
318
  '1 Style/EmptyLineBetweenDefs',
282
319
  '--',
283
- '14 Total',
320
+ '20 Total',
284
321
  '',
285
322
  ''].join("\n"))
286
323
  end
@@ -347,7 +384,7 @@ describe RuboCop::CLI, :isolated_environment do
347
384
  'Offenses:',
348
385
  '',
349
386
  'example.rb:4:1: C: [Corrected] Extra empty line detected ' \
350
- 'at body beginning.',
387
+ 'at class body beginning.',
351
388
  'example.rb:4:1: C: [Corrected] Trailing whitespace ' \
352
389
  'detected.',
353
390
  '',
@@ -360,14 +397,14 @@ describe RuboCop::CLI, :isolated_environment do
360
397
  create_file('example.rb',
361
398
  ['# encoding: utf-8',
362
399
  'def primes limit',
363
- ' 1.upto(limit).find_all { |i| is_prime[i] }',
400
+ ' 1.upto(limit).select { |i| i.even? }',
364
401
  'end'])
365
402
  expect(cli.run(%w(-D --auto-correct))).to eq(1)
366
403
  expect($stderr.string).to eq('')
367
404
  expect(IO.read('example.rb'))
368
405
  .to eq(['# encoding: utf-8',
369
406
  'def primes(limit)',
370
- ' 1.upto(limit).select { |i| is_prime[i] }',
407
+ ' 1.upto(limit).select(&:even?)',
371
408
  'end',
372
409
  ''].join("\n"))
373
410
  expect($stdout.string)
@@ -381,10 +418,10 @@ describe RuboCop::CLI, :isolated_environment do
381
418
  'Use def with parentheses when there are parameters.',
382
419
  'def primes limit',
383
420
  ' ^^^^^',
384
- 'example.rb:3:17: C: [Corrected] Style/CollectionMethods: ' \
385
- 'Prefer select over find_all.',
386
- ' 1.upto(limit).find_all { |i| is_prime[i] }',
387
- ' ^^^^^^^^',
421
+ 'example.rb:3:3: C: [Corrected] Style/SymbolProc: ' \
422
+ 'Pass &:even? as an argument to select instead of a block.',
423
+ ' 1.upto(limit).select { |i| i.even? }',
424
+ ' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^',
388
425
  '',
389
426
  '1 file inspected, 2 offenses detected, 2 offenses ' \
390
427
  'corrected',
@@ -1181,7 +1218,7 @@ describe RuboCop::CLI, :isolated_environment do
1181
1218
  'indentation.',
1182
1219
  ' puts',
1183
1220
  '^^^',
1184
- 'example3.rb:2:5: C: Use snake_case for methods.',
1221
+ 'example3.rb:2:5: C: Use snake_case for method names.',
1185
1222
  'def badName',
1186
1223
  ' ^^^^^^^',
1187
1224
  'example3.rb:3:3: C: Use a guard clause instead of ' \
@@ -1923,7 +1960,13 @@ describe RuboCop::CLI, :isolated_environment do
1923
1960
  ['Style/CollectionMethods:',
1924
1961
  ' PreferredMethods:',
1925
1962
  ' select: find_all'])
1926
- cli.run(['--format', 'simple', '-c', 'rubocop.yml', 'example1.rb'])
1963
+ cli.run(['--format',
1964
+ 'simple',
1965
+ '-c',
1966
+ 'rubocop.yml',
1967
+ '--only',
1968
+ 'CollectionMethods',
1969
+ 'example1.rb'])
1927
1970
  expect($stdout.string)
1928
1971
  .to eq(['== example1.rb ==',
1929
1972
  'C: 2: 5: Prefer find_all over select.',
@@ -26,7 +26,7 @@ describe RuboCop::Cop::Lint::InvalidCharacterLiteral do
26
26
 
27
27
  expect(cop.offenses.size).to eq(1)
28
28
  expect(cop.offenses.first.message)
29
- .to eq('Invalid character literal; use ?\s')
29
+ .to eq('Invalid character literal; use ?\s')
30
30
  expect(cop.highlights).to eq([' '])
31
31
  end
32
32
  end
@@ -0,0 +1,99 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe RuboCop::Cop::Metrics::AbcSize, :config do
6
+ subject(:cop) { described_class.new(config) }
7
+
8
+ context 'when Max is 0' do
9
+ let(:cop_config) { { 'Max' => 0 } }
10
+
11
+ it 'accepts an empty method' do
12
+ inspect_source(cop, ['def method_name',
13
+ 'end'])
14
+ expect(cop.offenses).to be_empty
15
+ end
16
+
17
+ it 'registers an offense for an if modifier' do
18
+ inspect_source(cop, ['def method_name',
19
+ ' call_foo if some_condition', # 0 + 2*2 + 1*1
20
+ 'end'])
21
+ expect(cop.messages)
22
+ .to eq(['Assignment Branch Condition size for method_name is too ' \
23
+ 'high. [2.24/0]'])
24
+ expect(cop.highlights).to eq(['def'])
25
+ expect(cop.config_to_allow_offenses).to eq('Max' => 3)
26
+ end
27
+
28
+ it 'registers an offense for an assignment of a local variable' do
29
+ inspect_source(cop, ['def method_name',
30
+ ' x = 1',
31
+ 'end'])
32
+ expect(cop.messages)
33
+ .to eq(['Assignment Branch Condition size for method_name is too ' \
34
+ 'high. [1/0]'])
35
+ expect(cop.config_to_allow_offenses).to eq('Max' => 1)
36
+ end
37
+
38
+ it 'registers an offense for complex content including A, B, and C ' \
39
+ 'scores' do
40
+ inspect_source(cop,
41
+ ['def method_name',
42
+ ' my_options = Hash.new if 1 == 1 || 2 == 2', # 1, 3, 2
43
+ ' my_options.each do |key, value|', # 0, 1, 0
44
+ ' p key', # 0, 1, 0
45
+ ' p value', # 0, 1, 0
46
+ ' end',
47
+ 'end'])
48
+ expect(cop.messages)
49
+ .to eq(['Assignment Branch Condition size for method_name is too ' \
50
+ 'high. [6.4/0]']) # square root of 1*1 + 5*5 + 2*2 => 6.4
51
+ end
52
+ end
53
+
54
+ context 'when Max is 2' do
55
+ let(:cop_config) { { 'Max' => 2 } }
56
+
57
+ it 'accepts two assignments' do
58
+ inspect_source(cop, ['def method_name',
59
+ ' x = 1',
60
+ ' y = 2',
61
+ 'end'])
62
+ expect(cop.offenses).to be_empty
63
+ end
64
+ end
65
+
66
+ context 'when Max is 1.8' do
67
+ let(:cop_config) { { 'Max' => 1.8 } }
68
+
69
+ it 'accepts a total score of 1.7' do
70
+ inspect_source(cop, ['def method_name',
71
+ ' y = 1 if y == 1',
72
+ 'end'])
73
+ expect(cop.offenses).to be_empty
74
+ end
75
+ end
76
+
77
+ {
78
+ 1.3 => '3.74/1.3', # no more than 2 decimals reported
79
+ 10.3 => '37.42/10.3',
80
+ 100.321 => '374.2/100.3', # 4 significant digits, so only 1 decimal here
81
+ 1000.3 => '3742/1000'
82
+ }.each do |max, presentation|
83
+ context "when Max is #{max}" do
84
+ let(:cop_config) { { 'Max' => max } }
85
+
86
+ it "reports size and max as #{presentation}" do
87
+ # Build an amount of code large enough to register an offense.
88
+ code = [' x = Hash.new if 1 == 1 || 2 == 2'] * max
89
+
90
+ inspect_source(cop, ['def method_name',
91
+ *code,
92
+ 'end'])
93
+ expect(cop.messages)
94
+ .to eq(['Assignment Branch Condition size for method_name is too ' \
95
+ "high. [#{presentation}]"])
96
+ end
97
+ end
98
+ end
99
+ end
@@ -1,6 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'spec_helper'
4
+ require 'tempfile'
4
5
 
5
6
  describe RuboCop::Cop::Rails::ActionFilter, :config do
6
7
  subject(:cop) { described_class.new(config) }
@@ -2,8 +2,15 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe RuboCop::Cop::Style::AccessModifierIndentation, :config do
5
+ describe RuboCop::Cop::Style::AccessModifierIndentation do
6
6
  subject(:cop) { described_class.new(config) }
7
+ let(:config) do
8
+ c = cop_config.merge('SupportedStyles' => %w(indent outdent))
9
+ RuboCop::Config
10
+ .new('Style/AccessModifierIndentation' => c,
11
+ 'Style/IndentationWidth' => { 'Width' => indentation_width })
12
+ end
13
+ let(:indentation_width) { 2 }
7
14
 
8
15
  context 'when EnforcedStyle is set to indent' do
9
16
  let(:cop_config) { { 'EnforcedStyle' => 'indent' } }
@@ -216,6 +223,21 @@ describe RuboCop::Cop::Style::AccessModifierIndentation, :config do
216
223
  ' def test; end',
217
224
  'end'].join("\n"))
218
225
  end
226
+
227
+ context 'when 4 spaces per indent level are used' do
228
+ let(:indentation_width) { 4 }
229
+
230
+ it 'accepts properly indented private' do
231
+ inspect_source(cop,
232
+ ['class Test',
233
+ '',
234
+ ' private',
235
+ '',
236
+ ' def test; end',
237
+ 'end'])
238
+ expect(cop.offenses).to be_empty
239
+ end
240
+ end
219
241
  end
220
242
 
221
243
  context 'when EnforcedStyle is set to outdent' do
@@ -129,6 +129,12 @@ describe RuboCop::Cop::Style::AlignHash, :config do
129
129
  "'dddd' => 2"])
130
130
  end
131
131
 
132
+ it 'registers an offense for misaligned mixed multiline hash keys' do
133
+ inspect_source(cop, ['hash = { a: 1, b: 2,',
134
+ ' c: 3 }'])
135
+ expect(cop.offenses.size).to eq(1)
136
+ end
137
+
132
138
  it 'accepts aligned hash keys' do
133
139
  inspect_source(cop, ['hash1 = {',
134
140
  ' a: 0,',
@@ -192,6 +198,13 @@ describe RuboCop::Cop::Style::AlignHash, :config do
192
198
  # in 'key' mode.
193
199
  ' :ccc =>2 }'].join("\n"))
194
200
  end
201
+
202
+ it 'auto-corrects alignment for mixed multiline hash keys' do
203
+ new_sources = autocorrect_source(cop, ['hash = { a: 1, b: 2,',
204
+ ' c: 3 }'])
205
+ expect(new_sources).to eq(['hash = { a: 1, b: 2,',
206
+ ' c: 3 }'].join("\n"))
207
+ end
195
208
  end
196
209
 
197
210
  include_examples 'not on separate lines'
@@ -2,8 +2,15 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe RuboCop::Cop::Style::AlignParameters, :config do
5
+ describe RuboCop::Cop::Style::AlignParameters do
6
6
  subject(:cop) { described_class.new(config) }
7
+ let(:config) do
8
+ RuboCop::Config.new('Style/AlignParameters' => cop_config,
9
+ 'Style/IndentationWidth' => {
10
+ 'Width' => indentation_width
11
+ })
12
+ end
13
+ let(:indentation_width) { 2 }
7
14
 
8
15
  context 'aligned with first parameter' do
9
16
  let(:cop_config) do
@@ -382,38 +389,42 @@ describe RuboCop::Cop::Style::AlignParameters, :config do
382
389
  end
383
390
 
384
391
  context 'assigned methods' do
385
- it 'accepts the first parameter being on a new row' do
386
- inspect_source(cop, [' assigned_value = match(',
387
- ' a,',
388
- ' b,',
389
- ' c',
390
- ' )'])
391
- expect(cop.offenses).to be_empty
392
- end
393
-
394
- it 'accepts the first parameter being on method row' do
395
- inspect_source(cop, [' assigned_value = match(a,',
396
- ' b,',
397
- ' c',
398
- ' )'])
399
- expect(cop.offenses).to be_empty
400
- end
401
-
402
- it 'autocorrects even when first argument is in wrong position' do
403
- original_source = [' assigned_value = match(',
404
- ' a,',
405
- ' b,',
406
- ' c',
407
- ' )']
408
-
409
- correct_source = [' assigned_value = match(',
410
- ' a,',
411
- ' b,',
412
- ' c',
413
- ' )']
414
-
415
- expect(autocorrect_source(cop, original_source))
416
- .to eq(correct_source.join("\n"))
392
+ context 'with IndentationWidth:Width set to 4' do
393
+ let(:indentation_width) { 4 }
394
+
395
+ it 'accepts the first parameter being on a new row' do
396
+ inspect_source(cop, [' assigned_value = match(',
397
+ ' a,',
398
+ ' b,',
399
+ ' c',
400
+ ' )'])
401
+ expect(cop.offenses).to be_empty
402
+ end
403
+
404
+ it 'accepts the first parameter being on method row' do
405
+ inspect_source(cop, [' assigned_value = match(a,',
406
+ ' b,',
407
+ ' c',
408
+ ' )'])
409
+ expect(cop.offenses).to be_empty
410
+ end
411
+
412
+ it 'autocorrects even when first argument is in wrong position' do
413
+ original_source = [' assigned_value = match(',
414
+ ' a,',
415
+ ' b,',
416
+ ' c',
417
+ ' )']
418
+
419
+ correct_source = [' assigned_value = match(',
420
+ ' a,',
421
+ ' b,',
422
+ ' c',
423
+ ' )']
424
+
425
+ expect(autocorrect_source(cop, original_source))
426
+ .to eq(correct_source.join("\n"))
427
+ end
417
428
  end
418
429
  end
419
430
  end