rubocop 0.9.1 → 0.10.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 (149) hide show
  1. data/.travis.yml +3 -1
  2. data/CHANGELOG.md +38 -0
  3. data/README.md +34 -0
  4. data/Rakefile +3 -0
  5. data/config/default.yml +14 -1
  6. data/config/enabled.yml +30 -7
  7. data/lib/rubocop.rb +15 -0
  8. data/lib/rubocop/cli.rb +48 -154
  9. data/lib/rubocop/config.rb +19 -22
  10. data/lib/rubocop/config_store.rb +2 -4
  11. data/lib/rubocop/cop/commissioner.rb +90 -0
  12. data/lib/rubocop/cop/cop.rb +38 -31
  13. data/lib/rubocop/cop/corrector.rb +84 -0
  14. data/lib/rubocop/cop/lint/assignment_in_condition.rb +0 -3
  15. data/lib/rubocop/cop/lint/block_alignment.rb +151 -0
  16. data/lib/rubocop/cop/lint/empty_ensure.rb +18 -0
  17. data/lib/rubocop/cop/lint/end_alignment.rb +0 -124
  18. data/lib/rubocop/cop/lint/end_in_method.rb +0 -2
  19. data/lib/rubocop/cop/lint/ensure_return.rb +3 -3
  20. data/lib/rubocop/cop/lint/eval.rb +0 -2
  21. data/lib/rubocop/cop/lint/handle_exceptions.rb +0 -2
  22. data/lib/rubocop/cop/lint/literal_in_condition.rb +0 -10
  23. data/lib/rubocop/cop/lint/loop.rb +0 -2
  24. data/lib/rubocop/cop/lint/rescue_exception.rb +0 -2
  25. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +2 -2
  26. data/lib/rubocop/cop/lint/unreachable_code.rb +0 -2
  27. data/lib/rubocop/cop/lint/unused_local_variable.rb +2 -2
  28. data/lib/rubocop/cop/lint/void.rb +0 -2
  29. data/lib/rubocop/cop/offence.rb +9 -0
  30. data/lib/rubocop/cop/rails/validation.rb +2 -1
  31. data/lib/rubocop/cop/style/access_control.rb +4 -3
  32. data/lib/rubocop/cop/style/alias.rb +2 -4
  33. data/lib/rubocop/cop/style/align_parameters.rb +0 -2
  34. data/lib/rubocop/cop/style/and_or.rb +4 -6
  35. data/lib/rubocop/cop/style/ascii_comments.rb +2 -2
  36. data/lib/rubocop/cop/style/ascii_identifiers.rb +2 -2
  37. data/lib/rubocop/cop/style/attr.rb +0 -2
  38. data/lib/rubocop/cop/style/avoid_class_vars.rb +0 -1
  39. data/lib/rubocop/cop/style/avoid_for.rb +0 -2
  40. data/lib/rubocop/cop/style/avoid_global_vars.rb +3 -7
  41. data/lib/rubocop/cop/style/avoid_perl_backrefs.rb +0 -2
  42. data/lib/rubocop/cop/style/avoid_perlisms.rb +2 -4
  43. data/lib/rubocop/cop/style/begin_block.rb +0 -2
  44. data/lib/rubocop/cop/style/block_comments.rb +2 -2
  45. data/lib/rubocop/cop/style/block_nesting.rb +3 -3
  46. data/lib/rubocop/cop/style/blocks.rb +0 -2
  47. data/lib/rubocop/cop/style/case_equality.rb +0 -2
  48. data/lib/rubocop/cop/style/case_indentation.rb +0 -2
  49. data/lib/rubocop/cop/style/character_literal.rb +10 -6
  50. data/lib/rubocop/cop/style/class_and_module_camel_case.rb +0 -4
  51. data/lib/rubocop/cop/style/class_methods.rb +1 -1
  52. data/lib/rubocop/cop/style/collection_methods.rb +3 -5
  53. data/lib/rubocop/cop/style/colon_method_call.rb +3 -3
  54. data/lib/rubocop/cop/style/comment_annotation.rb +44 -0
  55. data/lib/rubocop/cop/style/constant_name.rb +0 -2
  56. data/lib/rubocop/cop/style/def_parentheses.rb +0 -8
  57. data/lib/rubocop/cop/style/documentation.rb +6 -2
  58. data/lib/rubocop/cop/style/dot_position.rb +0 -2
  59. data/lib/rubocop/cop/style/empty_line_between_defs.rb +0 -2
  60. data/lib/rubocop/cop/style/empty_lines.rb +10 -8
  61. data/lib/rubocop/cop/style/empty_literal.rb +3 -1
  62. data/lib/rubocop/cop/style/encoding.rb +7 -6
  63. data/lib/rubocop/cop/style/end_block.rb +0 -2
  64. data/lib/rubocop/cop/style/end_of_line.rb +4 -3
  65. data/lib/rubocop/cop/style/favor_join.rb +0 -2
  66. data/lib/rubocop/cop/style/favor_modifier.rb +9 -9
  67. data/lib/rubocop/cop/style/favor_sprintf.rb +0 -2
  68. data/lib/rubocop/cop/style/favor_unless_over_negated_if.rb +0 -2
  69. data/lib/rubocop/cop/style/hash_syntax.rb +0 -2
  70. data/lib/rubocop/cop/style/if_then_else.rb +0 -2
  71. data/lib/rubocop/cop/style/lambda.rb +0 -2
  72. data/lib/rubocop/cop/style/leading_comment_space.rb +2 -2
  73. data/lib/rubocop/cop/style/line_continuation.rb +4 -3
  74. data/lib/rubocop/cop/style/line_length.rb +4 -3
  75. data/lib/rubocop/cop/style/method_and_variable_snake_case.rb +4 -3
  76. data/lib/rubocop/cop/style/method_call_parentheses.rb +0 -2
  77. data/lib/rubocop/cop/style/method_length.rb +0 -4
  78. data/lib/rubocop/cop/style/not.rb +0 -2
  79. data/lib/rubocop/cop/style/op_method.rb +0 -2
  80. data/lib/rubocop/cop/style/parameter_lists.rb +0 -2
  81. data/lib/rubocop/cop/style/parentheses_around_condition.rb +12 -6
  82. data/lib/rubocop/cop/style/proc.rb +0 -2
  83. data/lib/rubocop/cop/style/reduce_arguments.rb +0 -2
  84. data/lib/rubocop/cop/style/redundant_begin.rb +45 -0
  85. data/lib/rubocop/cop/style/redundant_return.rb +59 -0
  86. data/lib/rubocop/cop/style/redundant_self.rb +83 -0
  87. data/lib/rubocop/cop/style/regexp_literal.rb +0 -2
  88. data/lib/rubocop/cop/style/rescue_modifier.rb +13 -21
  89. data/lib/rubocop/cop/style/semicolon.rb +15 -9
  90. data/lib/rubocop/cop/style/single_line_methods.rb +0 -4
  91. data/lib/rubocop/cop/style/space_after_comma_etc.rb +2 -2
  92. data/lib/rubocop/cop/style/space_after_control_keyword.rb +0 -1
  93. data/lib/rubocop/cop/style/string_literals.rb +5 -2
  94. data/lib/rubocop/cop/style/surrounding_space.rb +106 -91
  95. data/lib/rubocop/cop/style/tab.rb +4 -3
  96. data/lib/rubocop/cop/style/ternary_operator.rb +0 -4
  97. data/lib/rubocop/cop/style/trailing_whitespace.rb +4 -3
  98. data/lib/rubocop/cop/style/trivial_accessors.rb +51 -6
  99. data/lib/rubocop/cop/style/unless_else.rb +0 -2
  100. data/lib/rubocop/cop/style/variable_interpolation.rb +0 -2
  101. data/lib/rubocop/cop/style/when_then.rb +3 -3
  102. data/lib/rubocop/cop/style/while_until_do.rb +3 -5
  103. data/lib/rubocop/cop/style/word_array.rb +0 -2
  104. data/lib/rubocop/cop/util.rb +0 -4
  105. data/lib/rubocop/formatter/file_list_formatter.rb +18 -0
  106. data/lib/rubocop/formatter/formatter_set.rb +2 -1
  107. data/lib/rubocop/processed_source.rb +27 -0
  108. data/lib/rubocop/rake_task.rb +50 -0
  109. data/lib/rubocop/source_parser.rb +105 -0
  110. data/lib/rubocop/target_finder.rb +67 -0
  111. data/lib/rubocop/token.rb +22 -0
  112. data/lib/rubocop/version.rb +1 -1
  113. data/rubocop.gemspec +5 -3
  114. data/spec/project_spec.rb +0 -11
  115. data/spec/rubocop/cli_spec.rb +112 -6
  116. data/spec/rubocop/config_spec.rb +13 -17
  117. data/spec/rubocop/config_store_spec.rb +8 -23
  118. data/spec/rubocop/cops/commissioner_spec.rb +72 -0
  119. data/spec/rubocop/cops/corrector_spec.rb +63 -0
  120. data/spec/rubocop/cops/lint/assignment_in_condition_spec.rb +2 -2
  121. data/spec/rubocop/cops/lint/block_alignment_spec.rb +357 -0
  122. data/spec/rubocop/cops/lint/empty_ensure_spec.rb +33 -0
  123. data/spec/rubocop/cops/lint/end_alignment_spec.rb +0 -263
  124. data/spec/rubocop/cops/lint/ensure_return_spec.rb +6 -9
  125. data/spec/rubocop/cops/offence_spec.rb +28 -0
  126. data/spec/rubocop/cops/style/and_or_spec.rb +21 -11
  127. data/spec/rubocop/cops/style/ascii_identifiers_spec.rb +14 -0
  128. data/spec/rubocop/cops/style/avoid_global_vars_spec.rb +10 -14
  129. data/spec/rubocop/cops/style/character_literal_spec.rb +17 -2
  130. data/spec/rubocop/cops/style/colon_method_call_spec.rb +20 -15
  131. data/spec/rubocop/cops/style/comment_annotation_spec.rb +62 -0
  132. data/spec/rubocop/cops/style/encoding_spec.rb +7 -0
  133. data/spec/rubocop/cops/style/parentheses_around_condition_spec.rb +37 -9
  134. data/spec/rubocop/cops/style/redundant_begin_spec.rb +63 -0
  135. data/spec/rubocop/cops/style/redundant_return_spec.rb +64 -0
  136. data/spec/rubocop/cops/style/redundant_self_spec.rb +76 -0
  137. data/spec/rubocop/cops/style/string_literals_spec.rb +18 -13
  138. data/spec/rubocop/cops/style/trivial_accessors_spec.rb +110 -52
  139. data/spec/rubocop/cops/style/when_then_spec.rb +14 -7
  140. data/spec/rubocop/cops/style/while_until_do_spec.rb +12 -0
  141. data/spec/rubocop/cops/variable_inspector_spec.rb +3 -5
  142. data/spec/rubocop/formatter/file_list_formatter_spec.rb +33 -0
  143. data/spec/rubocop/processed_source_spec.rb +67 -0
  144. data/spec/rubocop/source_parser_spec.rb +141 -0
  145. data/spec/rubocop/target_finder_spec.rb +180 -0
  146. data/spec/rubocop/token_spec.rb +27 -0
  147. data/spec/spec_helper.rb +24 -4
  148. metadata +108 -18
  149. checksums.yaml +0 -7
@@ -6,41 +6,38 @@ module Rubocop
6
6
  module Cop
7
7
  module Lint
8
8
  describe EnsureReturn do
9
- let(:er) { EnsureReturn.new }
9
+ let(:cop) { EnsureReturn.new }
10
10
 
11
11
  it 'registers an offence for return in ensure' do
12
- inspect_source(er,
12
+ inspect_source(cop,
13
13
  ['begin',
14
14
  ' something',
15
15
  'ensure',
16
16
  ' file.close',
17
17
  ' return',
18
18
  'end'])
19
- expect(er.offences.size).to eq(1)
20
- expect(er.offences.map(&:message))
21
- .to eq([EnsureReturn::MSG])
19
+ expect(cop.offences).to have(1).item
22
20
  end
23
21
 
24
22
  it 'does not register an offence for return outside ensure' do
25
- inspect_source(er,
23
+ inspect_source(cop,
26
24
  ['begin',
27
25
  ' something',
28
26
  ' return',
29
27
  'ensure',
30
28
  ' file.close',
31
29
  'end'])
32
- expect(er.offences).to be_empty
30
+ expect(cop.offences).to be_empty
33
31
  end
34
32
 
35
33
  it 'does not check when ensure block has no body' do
36
34
  expect do
37
- inspect_source(er,
35
+ inspect_source(cop,
38
36
  ['begin',
39
37
  ' something',
40
38
  'ensure',
41
39
  'end'])
42
40
  end.to_not raise_exception
43
-
44
41
  end
45
42
  end
46
43
  end
@@ -43,6 +43,34 @@ module Rubocop
43
43
  expect(offence).to be_frozen
44
44
  end
45
45
 
46
+ describe '.from_diagnostic' do
47
+ subject(:offence) { Offence.from_diagnostic(diagnostic) }
48
+ let(:diagnostic) { Parser::Diagnostic.new(level, message, location) }
49
+ let(:level) { :warning }
50
+ let(:message) { 'This is a message' }
51
+ let(:location) { double('location').as_null_object }
52
+
53
+ it 'returns an offence' do
54
+ expect(offence).to be_a(Offence)
55
+ end
56
+
57
+ it "sets diagnostic's level to offence's severity" do
58
+ expect(offence.severity).to eq(level)
59
+ end
60
+
61
+ it "sets diagnostic's message to offence's message" do
62
+ expect(offence.message).to eq(message)
63
+ end
64
+
65
+ it "sets diagnostic's location to offence's location" do
66
+ expect(offence.location).to eq(location)
67
+ end
68
+
69
+ it 'sets Sytanx as cop name' do
70
+ expect(offence.cop_name).to eq('Syntax')
71
+ end
72
+ end
73
+
46
74
  [:severity, :location, :line, :column, :message, :cop_name].each do |a|
47
75
  describe "##{a}" do
48
76
  it 'is frozen' do
@@ -6,32 +6,42 @@ module Rubocop
6
6
  module Cop
7
7
  module Style
8
8
  describe AndOr do
9
- let(:amp) { AndOr.new }
9
+ let(:cop) { AndOr.new }
10
10
 
11
11
  it 'registers an offence for OR' do
12
- inspect_source(amp,
12
+ inspect_source(cop,
13
13
  ['test if a or b'])
14
- expect(amp.offences.size).to eq(1)
15
- expect(amp.messages).to eq(['Use || instead of or.'])
14
+ expect(cop.offences.size).to eq(1)
15
+ expect(cop.messages).to eq(['Use || instead of or.'])
16
16
  end
17
17
 
18
18
  it 'registers an offence for AND' do
19
- inspect_source(amp,
19
+ inspect_source(cop,
20
20
  ['test if a and b'])
21
- expect(amp.offences.size).to eq(1)
22
- expect(amp.messages).to eq(['Use && instead of and.'])
21
+ expect(cop.offences.size).to eq(1)
22
+ expect(cop.messages).to eq(['Use && instead of and.'])
23
23
  end
24
24
 
25
25
  it 'accepts ||' do
26
- inspect_source(amp,
26
+ inspect_source(cop,
27
27
  ['test if a || b'])
28
- expect(amp.offences).to be_empty
28
+ expect(cop.offences).to be_empty
29
29
  end
30
30
 
31
31
  it 'accepts &&' do
32
- inspect_source(amp,
32
+ inspect_source(cop,
33
33
  ['test if a && b'])
34
- expect(amp.offences).to be_empty
34
+ expect(cop.offences).to be_empty
35
+ end
36
+
37
+ it 'auto-corrects "and" with &&' do
38
+ new_source = autocorrect_source(cop, 'true and false')
39
+ expect(new_source).to eq('true && false')
40
+ end
41
+
42
+ it 'auto-corrects "or" with ||' do
43
+ new_source = autocorrect_source(cop, 'true or false')
44
+ expect(new_source).to eq('true || false')
35
45
  end
36
46
  end
37
47
  end
@@ -22,6 +22,20 @@ module Rubocop
22
22
  ['x.empty?'])
23
23
  expect(ascii.offences).to be_empty
24
24
  end
25
+
26
+ it 'does not get confused by a byte order mark' do
27
+ bom = "\xef\xbb\xbf"
28
+ inspect_source(ascii,
29
+ [bom + '# encoding: utf-8',
30
+ "puts 'foo'"])
31
+ expect(ascii.offences).to be_empty
32
+ end
33
+
34
+ it 'does not get confused by an empty file' do
35
+ inspect_source(ascii,
36
+ [''])
37
+ expect(ascii.offences).to be_empty
38
+ end
25
39
  end
26
40
  end
27
41
  end
@@ -6,27 +6,23 @@ module Rubocop
6
6
  module Cop
7
7
  module Style
8
8
  describe AvoidGlobalVars do
9
- let(:ap) { AvoidGlobalVars.new }
9
+ let(:cop) { AvoidGlobalVars.new }
10
10
 
11
11
  it 'registers an offence for $custom' do
12
- inspect_source(ap, ['puts $custom'])
13
- expect(ap.offences.size).to eq(1)
12
+ inspect_source(cop, ['puts $custom'])
13
+ expect(cop.offences).to have(1).item
14
14
  end
15
15
 
16
- it 'does not register an offence for $"' do
17
- inspect_source(ap, ['puts $"'])
18
-
19
- expect(ap.offences).to be_empty
20
- end
21
-
22
- it 'does not register an offence for $ORS' do
23
- inspect_source(ap, ['puts $0'])
24
- expect(ap.offences).to be_empty
16
+ AvoidGlobalVars::BUILT_IN_VARS.each do |var|
17
+ it "does not register an offence for built-in variable #{var}" do
18
+ inspect_source(cop, ["puts #{var}"])
19
+ expect(cop.offences).to be_empty
20
+ end
25
21
  end
26
22
 
27
23
  it 'does not register an offence for backrefs like $1' do
28
- inspect_source(ap, ['puts $1'])
29
- expect(ap.offences).to be_empty
24
+ inspect_source(cop, ['puts $1'])
25
+ expect(cop.offences).to be_empty
30
26
  end
31
27
  end
32
28
  end
@@ -10,18 +10,33 @@ module Rubocop
10
10
 
11
11
  it 'registers an offence for character literals' do
12
12
  inspect_source(cop, ['x = ?x'])
13
- expect(cop.offences.size).to eq(1)
13
+ expect(cop.offences).to have(1).item
14
14
  end
15
15
 
16
16
  it 'registers an offence for literals like \n' do
17
17
  inspect_source(cop, ['x = ?\n'])
18
- expect(cop.offences.size).to eq(1)
18
+ expect(cop.offences).to have(1).item
19
19
  end
20
20
 
21
21
  it 'accepts literals like ?\C-\M-d' do
22
22
  inspect_source(cop, ['x = ?\C-\M-d'])
23
23
  expect(cop.offences).to be_empty
24
24
  end
25
+
26
+ it 'accepts ? in a %w literal' do
27
+ inspect_source(cop, ['%w{? A}'])
28
+ expect(cop.offences).to be_empty
29
+ end
30
+
31
+ it "auto-corrects ?x to 'x'" do
32
+ new_source = autocorrect_source(cop, 'x = ?x')
33
+ expect(new_source).to eq("x = 'x'")
34
+ end
35
+
36
+ it 'auto-corrects ?\n to "\\n"' do
37
+ new_source = autocorrect_source(cop, 'x = ?\n')
38
+ expect(new_source).to eq('x = "\\n"')
39
+ end
25
40
  end
26
41
  end
27
42
  end
@@ -6,48 +6,53 @@ module Rubocop
6
6
  module Cop
7
7
  module Style
8
8
  describe ColonMethodCall do
9
- let(:smc) { ColonMethodCall.new }
9
+ let(:cop) { ColonMethodCall.new }
10
10
 
11
11
  it 'registers an offence for instance method call' do
12
- inspect_source(smc,
12
+ inspect_source(cop,
13
13
  ['test::method_name'])
14
- expect(smc.offences.size).to eq(1)
14
+ expect(cop.offences.size).to eq(1)
15
15
  end
16
16
 
17
17
  it 'registers an offence for instance method call with arg' do
18
- inspect_source(smc,
18
+ inspect_source(cop,
19
19
  ['test::method_name(arg)'])
20
- expect(smc.offences.size).to eq(1)
20
+ expect(cop.offences.size).to eq(1)
21
21
  end
22
22
 
23
23
  it 'registers an offence for class method call' do
24
- inspect_source(smc,
24
+ inspect_source(cop,
25
25
  ['Class::method_name'])
26
- expect(smc.offences.size).to eq(1)
26
+ expect(cop.offences.size).to eq(1)
27
27
  end
28
28
 
29
29
  it 'registers an offence for class method call with arg' do
30
- inspect_source(smc,
30
+ inspect_source(cop,
31
31
  ['Class::method_name(arg, arg2)'])
32
- expect(smc.offences.size).to eq(1)
32
+ expect(cop.offences.size).to eq(1)
33
33
  end
34
34
 
35
35
  it 'does not register an offence for constant access' do
36
- inspect_source(smc,
36
+ inspect_source(cop,
37
37
  ['Tip::Top::SOME_CONST'])
38
- expect(smc.offences).to be_empty
38
+ expect(cop.offences).to be_empty
39
39
  end
40
40
 
41
41
  it 'does not register an offence for nested class' do
42
- inspect_source(smc,
42
+ inspect_source(cop,
43
43
  ['Tip::Top.some_method'])
44
- expect(smc.offences).to be_empty
44
+ expect(cop.offences).to be_empty
45
45
  end
46
46
 
47
47
  it 'does not register an offence for op methods' do
48
- inspect_source(smc,
48
+ inspect_source(cop,
49
49
  ['Tip::Top.some_method[3]'])
50
- expect(smc.offences).to be_empty
50
+ expect(cop.offences).to be_empty
51
+ end
52
+
53
+ it 'auto-corrects "::" with "."' do
54
+ new_source = autocorrect_source(cop, 'test::method')
55
+ expect(new_source).to eq('test.method')
51
56
  end
52
57
  end
53
58
  end
@@ -0,0 +1,62 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ module Style
8
+ describe CommentAnnotation do
9
+ let(:cop) { CommentAnnotation.new }
10
+
11
+ it 'registers an offence for a missing colon' do
12
+ inspect_source(cop, ['# TODO make better'])
13
+ expect(cop.offences).to have(1).item
14
+ end
15
+
16
+ context 'when used with the clang formatter' do
17
+ let(:formatter) { Formatter::ClangStyleFormatter.new(output) }
18
+ let(:output) { StringIO.new }
19
+
20
+ it 'marks the annotation keyword' do
21
+ inspect_source(cop, ['# TODO:make better'])
22
+ formatter.report_file('t', cop.offences)
23
+ expect(output.string).to eq(["t:1:3: C: #{CommentAnnotation::MSG}",
24
+ '# TODO:make better',
25
+ ' ^^^^^',
26
+ ''].join("\n"))
27
+ end
28
+ end
29
+
30
+ it 'registers an offence for lower case' do
31
+ inspect_source(cop, ['# fixme: does not work'])
32
+ expect(cop.offences).to have(1).item
33
+ end
34
+
35
+ it 'registers an offence for capitalized annotation keyword' do
36
+ inspect_source(cop, ['# Optimize: does not work'])
37
+ expect(cop.offences).to have(1).item
38
+ end
39
+
40
+ it 'registers an offence for upper case with colon but no note' do
41
+ inspect_source(cop, ['# HACK:'])
42
+ expect(cop.offences).to have(1).item
43
+ end
44
+
45
+ it 'accepts upper case keyword with colon, space and note' do
46
+ inspect_source(cop, ['# REVIEW: not sure about this'])
47
+ expect(cop.offences).to be_empty
48
+ end
49
+
50
+ it 'accepts upper case keyword alone' do
51
+ inspect_source(cop, ['# OPTIMIZE'])
52
+ expect(cop.offences).to be_empty
53
+ end
54
+
55
+ it 'accepts a comment that is obviously a code example' do
56
+ inspect_source(cop, ['# Todo.destroy(1)'])
57
+ expect(cop.offences).to be_empty
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -43,6 +43,13 @@ module Rubocop
43
43
 
44
44
  expect(encoding.offences).to be_empty
45
45
  end
46
+
47
+ it 'accepts encoding inserted by magic_encoding gem', ruby: 1.9 do
48
+ inspect_source(encoding, ['# -*- encoding : utf-8 -*-',
49
+ 'def foo() end'])
50
+
51
+ expect(encoding.offences.map(&:message)).to be_empty
52
+ end
46
53
  end
47
54
  end
48
55
  end
@@ -6,10 +6,13 @@ module Rubocop
6
6
  module Cop
7
7
  module Style
8
8
  describe ParenthesesAroundCondition do
9
- let(:pac) { ParenthesesAroundCondition.new }
9
+ let(:cop) { ParenthesesAroundCondition.new }
10
+ before do
11
+ ParenthesesAroundCondition.config = { 'AllowSafeAssignment' => true }
12
+ end
10
13
 
11
14
  it 'registers an offence for parentheses around condition' do
12
- inspect_source(pac, ['if (x > 10)',
15
+ inspect_source(cop, ['if (x > 10)',
13
16
  'elsif (x < 3)',
14
17
  'end',
15
18
  'unless (x > 10)',
@@ -23,11 +26,11 @@ module Rubocop
23
26
  'x += 1 while (x < 10)',
24
27
  'x += 1 until (x < 10)',
25
28
  ])
26
- expect(pac.offences.size).to eq(9)
29
+ expect(cop.offences.size).to eq(9)
27
30
  end
28
31
 
29
32
  it 'accepts condition without parentheses' do
30
- inspect_source(pac, ['if x > 10',
33
+ inspect_source(cop, ['if x > 10',
31
34
  'end',
32
35
  'unless x > 10',
33
36
  'end',
@@ -40,18 +43,43 @@ module Rubocop
40
43
  'x += 1 while x < 10',
41
44
  'x += 1 until x < 10',
42
45
  ])
43
- expect(pac.offences).to be_empty
46
+ expect(cop.offences).to be_empty
44
47
  end
45
48
 
46
49
  it 'is not confused by leading brace in subexpression' do
47
- inspect_source(pac, ['(a > b) && other ? one : two'])
48
- expect(pac.offences).to be_empty
50
+ inspect_source(cop, ['(a > b) && other ? one : two'])
51
+ expect(cop.offences).to be_empty
49
52
  end
50
53
 
51
54
  it 'is not confused by unbalanced parentheses' do
52
- inspect_source(pac, ['if (a + b).c()',
55
+ inspect_source(cop, ['if (a + b).c()',
53
56
  'end'])
54
- expect(pac.offences).to be_empty
57
+ expect(cop.offences).to be_empty
58
+ end
59
+
60
+ context 'safe assignment is allowed' do
61
+ it 'accepts = in condition surrounded with braces' do
62
+ inspect_source(cop,
63
+ ['if (test = 10)',
64
+ 'end'
65
+ ])
66
+ expect(cop.offences).to be_empty
67
+ end
68
+
69
+ end
70
+
71
+ context 'safe assignment is not allowed' do
72
+ before do
73
+ ParenthesesAroundCondition.config['AllowSafeAssignment'] = false
74
+ end
75
+
76
+ it 'does not accepts = in condition surrounded with braces' do
77
+ inspect_source(cop,
78
+ ['if (test = 10)',
79
+ 'end'
80
+ ])
81
+ expect(cop.offences.size).to eq(1)
82
+ end
55
83
  end
56
84
  end
57
85
  end