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.
- data/.travis.yml +3 -1
- data/CHANGELOG.md +38 -0
- data/README.md +34 -0
- data/Rakefile +3 -0
- data/config/default.yml +14 -1
- data/config/enabled.yml +30 -7
- data/lib/rubocop.rb +15 -0
- data/lib/rubocop/cli.rb +48 -154
- data/lib/rubocop/config.rb +19 -22
- data/lib/rubocop/config_store.rb +2 -4
- data/lib/rubocop/cop/commissioner.rb +90 -0
- data/lib/rubocop/cop/cop.rb +38 -31
- data/lib/rubocop/cop/corrector.rb +84 -0
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +0 -3
- data/lib/rubocop/cop/lint/block_alignment.rb +151 -0
- data/lib/rubocop/cop/lint/empty_ensure.rb +18 -0
- data/lib/rubocop/cop/lint/end_alignment.rb +0 -124
- data/lib/rubocop/cop/lint/end_in_method.rb +0 -2
- data/lib/rubocop/cop/lint/ensure_return.rb +3 -3
- data/lib/rubocop/cop/lint/eval.rb +0 -2
- data/lib/rubocop/cop/lint/handle_exceptions.rb +0 -2
- data/lib/rubocop/cop/lint/literal_in_condition.rb +0 -10
- data/lib/rubocop/cop/lint/loop.rb +0 -2
- data/lib/rubocop/cop/lint/rescue_exception.rb +0 -2
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +2 -2
- data/lib/rubocop/cop/lint/unreachable_code.rb +0 -2
- data/lib/rubocop/cop/lint/unused_local_variable.rb +2 -2
- data/lib/rubocop/cop/lint/void.rb +0 -2
- data/lib/rubocop/cop/offence.rb +9 -0
- data/lib/rubocop/cop/rails/validation.rb +2 -1
- data/lib/rubocop/cop/style/access_control.rb +4 -3
- data/lib/rubocop/cop/style/alias.rb +2 -4
- data/lib/rubocop/cop/style/align_parameters.rb +0 -2
- data/lib/rubocop/cop/style/and_or.rb +4 -6
- data/lib/rubocop/cop/style/ascii_comments.rb +2 -2
- data/lib/rubocop/cop/style/ascii_identifiers.rb +2 -2
- data/lib/rubocop/cop/style/attr.rb +0 -2
- data/lib/rubocop/cop/style/avoid_class_vars.rb +0 -1
- data/lib/rubocop/cop/style/avoid_for.rb +0 -2
- data/lib/rubocop/cop/style/avoid_global_vars.rb +3 -7
- data/lib/rubocop/cop/style/avoid_perl_backrefs.rb +0 -2
- data/lib/rubocop/cop/style/avoid_perlisms.rb +2 -4
- data/lib/rubocop/cop/style/begin_block.rb +0 -2
- data/lib/rubocop/cop/style/block_comments.rb +2 -2
- data/lib/rubocop/cop/style/block_nesting.rb +3 -3
- data/lib/rubocop/cop/style/blocks.rb +0 -2
- data/lib/rubocop/cop/style/case_equality.rb +0 -2
- data/lib/rubocop/cop/style/case_indentation.rb +0 -2
- data/lib/rubocop/cop/style/character_literal.rb +10 -6
- data/lib/rubocop/cop/style/class_and_module_camel_case.rb +0 -4
- data/lib/rubocop/cop/style/class_methods.rb +1 -1
- data/lib/rubocop/cop/style/collection_methods.rb +3 -5
- data/lib/rubocop/cop/style/colon_method_call.rb +3 -3
- data/lib/rubocop/cop/style/comment_annotation.rb +44 -0
- data/lib/rubocop/cop/style/constant_name.rb +0 -2
- data/lib/rubocop/cop/style/def_parentheses.rb +0 -8
- data/lib/rubocop/cop/style/documentation.rb +6 -2
- data/lib/rubocop/cop/style/dot_position.rb +0 -2
- data/lib/rubocop/cop/style/empty_line_between_defs.rb +0 -2
- data/lib/rubocop/cop/style/empty_lines.rb +10 -8
- data/lib/rubocop/cop/style/empty_literal.rb +3 -1
- data/lib/rubocop/cop/style/encoding.rb +7 -6
- data/lib/rubocop/cop/style/end_block.rb +0 -2
- data/lib/rubocop/cop/style/end_of_line.rb +4 -3
- data/lib/rubocop/cop/style/favor_join.rb +0 -2
- data/lib/rubocop/cop/style/favor_modifier.rb +9 -9
- data/lib/rubocop/cop/style/favor_sprintf.rb +0 -2
- data/lib/rubocop/cop/style/favor_unless_over_negated_if.rb +0 -2
- data/lib/rubocop/cop/style/hash_syntax.rb +0 -2
- data/lib/rubocop/cop/style/if_then_else.rb +0 -2
- data/lib/rubocop/cop/style/lambda.rb +0 -2
- data/lib/rubocop/cop/style/leading_comment_space.rb +2 -2
- data/lib/rubocop/cop/style/line_continuation.rb +4 -3
- data/lib/rubocop/cop/style/line_length.rb +4 -3
- data/lib/rubocop/cop/style/method_and_variable_snake_case.rb +4 -3
- data/lib/rubocop/cop/style/method_call_parentheses.rb +0 -2
- data/lib/rubocop/cop/style/method_length.rb +0 -4
- data/lib/rubocop/cop/style/not.rb +0 -2
- data/lib/rubocop/cop/style/op_method.rb +0 -2
- data/lib/rubocop/cop/style/parameter_lists.rb +0 -2
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +12 -6
- data/lib/rubocop/cop/style/proc.rb +0 -2
- data/lib/rubocop/cop/style/reduce_arguments.rb +0 -2
- data/lib/rubocop/cop/style/redundant_begin.rb +45 -0
- data/lib/rubocop/cop/style/redundant_return.rb +59 -0
- data/lib/rubocop/cop/style/redundant_self.rb +83 -0
- data/lib/rubocop/cop/style/regexp_literal.rb +0 -2
- data/lib/rubocop/cop/style/rescue_modifier.rb +13 -21
- data/lib/rubocop/cop/style/semicolon.rb +15 -9
- data/lib/rubocop/cop/style/single_line_methods.rb +0 -4
- data/lib/rubocop/cop/style/space_after_comma_etc.rb +2 -2
- data/lib/rubocop/cop/style/space_after_control_keyword.rb +0 -1
- data/lib/rubocop/cop/style/string_literals.rb +5 -2
- data/lib/rubocop/cop/style/surrounding_space.rb +106 -91
- data/lib/rubocop/cop/style/tab.rb +4 -3
- data/lib/rubocop/cop/style/ternary_operator.rb +0 -4
- data/lib/rubocop/cop/style/trailing_whitespace.rb +4 -3
- data/lib/rubocop/cop/style/trivial_accessors.rb +51 -6
- data/lib/rubocop/cop/style/unless_else.rb +0 -2
- data/lib/rubocop/cop/style/variable_interpolation.rb +0 -2
- data/lib/rubocop/cop/style/when_then.rb +3 -3
- data/lib/rubocop/cop/style/while_until_do.rb +3 -5
- data/lib/rubocop/cop/style/word_array.rb +0 -2
- data/lib/rubocop/cop/util.rb +0 -4
- data/lib/rubocop/formatter/file_list_formatter.rb +18 -0
- data/lib/rubocop/formatter/formatter_set.rb +2 -1
- data/lib/rubocop/processed_source.rb +27 -0
- data/lib/rubocop/rake_task.rb +50 -0
- data/lib/rubocop/source_parser.rb +105 -0
- data/lib/rubocop/target_finder.rb +67 -0
- data/lib/rubocop/token.rb +22 -0
- data/lib/rubocop/version.rb +1 -1
- data/rubocop.gemspec +5 -3
- data/spec/project_spec.rb +0 -11
- data/spec/rubocop/cli_spec.rb +112 -6
- data/spec/rubocop/config_spec.rb +13 -17
- data/spec/rubocop/config_store_spec.rb +8 -23
- data/spec/rubocop/cops/commissioner_spec.rb +72 -0
- data/spec/rubocop/cops/corrector_spec.rb +63 -0
- data/spec/rubocop/cops/lint/assignment_in_condition_spec.rb +2 -2
- data/spec/rubocop/cops/lint/block_alignment_spec.rb +357 -0
- data/spec/rubocop/cops/lint/empty_ensure_spec.rb +33 -0
- data/spec/rubocop/cops/lint/end_alignment_spec.rb +0 -263
- data/spec/rubocop/cops/lint/ensure_return_spec.rb +6 -9
- data/spec/rubocop/cops/offence_spec.rb +28 -0
- data/spec/rubocop/cops/style/and_or_spec.rb +21 -11
- data/spec/rubocop/cops/style/ascii_identifiers_spec.rb +14 -0
- data/spec/rubocop/cops/style/avoid_global_vars_spec.rb +10 -14
- data/spec/rubocop/cops/style/character_literal_spec.rb +17 -2
- data/spec/rubocop/cops/style/colon_method_call_spec.rb +20 -15
- data/spec/rubocop/cops/style/comment_annotation_spec.rb +62 -0
- data/spec/rubocop/cops/style/encoding_spec.rb +7 -0
- data/spec/rubocop/cops/style/parentheses_around_condition_spec.rb +37 -9
- data/spec/rubocop/cops/style/redundant_begin_spec.rb +63 -0
- data/spec/rubocop/cops/style/redundant_return_spec.rb +64 -0
- data/spec/rubocop/cops/style/redundant_self_spec.rb +76 -0
- data/spec/rubocop/cops/style/string_literals_spec.rb +18 -13
- data/spec/rubocop/cops/style/trivial_accessors_spec.rb +110 -52
- data/spec/rubocop/cops/style/when_then_spec.rb +14 -7
- data/spec/rubocop/cops/style/while_until_do_spec.rb +12 -0
- data/spec/rubocop/cops/variable_inspector_spec.rb +3 -5
- data/spec/rubocop/formatter/file_list_formatter_spec.rb +33 -0
- data/spec/rubocop/processed_source_spec.rb +67 -0
- data/spec/rubocop/source_parser_spec.rb +141 -0
- data/spec/rubocop/target_finder_spec.rb +180 -0
- data/spec/rubocop/token_spec.rb +27 -0
- data/spec/spec_helper.rb +24 -4
- metadata +108 -18
- 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(:
|
9
|
+
let(:cop) { EnsureReturn.new }
|
10
10
|
|
11
11
|
it 'registers an offence for return in ensure' do
|
12
|
-
inspect_source(
|
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(
|
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(
|
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(
|
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(
|
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(:
|
9
|
+
let(:cop) { AndOr.new }
|
10
10
|
|
11
11
|
it 'registers an offence for OR' do
|
12
|
-
inspect_source(
|
12
|
+
inspect_source(cop,
|
13
13
|
['test if a or b'])
|
14
|
-
expect(
|
15
|
-
expect(
|
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(
|
19
|
+
inspect_source(cop,
|
20
20
|
['test if a and b'])
|
21
|
-
expect(
|
22
|
-
expect(
|
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(
|
26
|
+
inspect_source(cop,
|
27
27
|
['test if a || b'])
|
28
|
-
expect(
|
28
|
+
expect(cop.offences).to be_empty
|
29
29
|
end
|
30
30
|
|
31
31
|
it 'accepts &&' do
|
32
|
-
inspect_source(
|
32
|
+
inspect_source(cop,
|
33
33
|
['test if a && b'])
|
34
|
-
expect(
|
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(:
|
9
|
+
let(:cop) { AvoidGlobalVars.new }
|
10
10
|
|
11
11
|
it 'registers an offence for $custom' do
|
12
|
-
inspect_source(
|
13
|
-
expect(
|
12
|
+
inspect_source(cop, ['puts $custom'])
|
13
|
+
expect(cop.offences).to have(1).item
|
14
14
|
end
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
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(
|
29
|
-
expect(
|
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
|
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
|
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(:
|
9
|
+
let(:cop) { ColonMethodCall.new }
|
10
10
|
|
11
11
|
it 'registers an offence for instance method call' do
|
12
|
-
inspect_source(
|
12
|
+
inspect_source(cop,
|
13
13
|
['test::method_name'])
|
14
|
-
expect(
|
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(
|
18
|
+
inspect_source(cop,
|
19
19
|
['test::method_name(arg)'])
|
20
|
-
expect(
|
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(
|
24
|
+
inspect_source(cop,
|
25
25
|
['Class::method_name'])
|
26
|
-
expect(
|
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(
|
30
|
+
inspect_source(cop,
|
31
31
|
['Class::method_name(arg, arg2)'])
|
32
|
-
expect(
|
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(
|
36
|
+
inspect_source(cop,
|
37
37
|
['Tip::Top::SOME_CONST'])
|
38
|
-
expect(
|
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(
|
42
|
+
inspect_source(cop,
|
43
43
|
['Tip::Top.some_method'])
|
44
|
-
expect(
|
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(
|
48
|
+
inspect_source(cop,
|
49
49
|
['Tip::Top.some_method[3]'])
|
50
|
-
expect(
|
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(:
|
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(
|
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(
|
29
|
+
expect(cop.offences.size).to eq(9)
|
27
30
|
end
|
28
31
|
|
29
32
|
it 'accepts condition without parentheses' do
|
30
|
-
inspect_source(
|
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(
|
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(
|
48
|
-
expect(
|
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(
|
55
|
+
inspect_source(cop, ['if (a + b).c()',
|
53
56
|
'end'])
|
54
|
-
expect(
|
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
|