rubocop 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rubocop.yml +4 -262
- data/CHANGELOG.md +65 -1
- data/Gemfile +4 -0
- data/README.md +89 -2
- data/Rakefile +2 -2
- data/bin/rubocop +15 -10
- data/lib/rubocop/cli.rb +156 -114
- data/lib/rubocop/config.rb +202 -0
- data/lib/rubocop/config_store.rb +37 -0
- data/lib/rubocop/cop/alias.rb +2 -5
- data/lib/rubocop/cop/align_parameters.rb +1 -1
- data/lib/rubocop/cop/array_literal.rb +43 -4
- data/lib/rubocop/cop/{ascii_identifiers_and_comments.rb → ascii_comments.rb} +3 -3
- data/lib/rubocop/cop/ascii_identifiers.rb +18 -0
- data/lib/rubocop/cop/avoid_for.rb +2 -4
- data/lib/rubocop/cop/avoid_global_vars.rb +49 -0
- data/lib/rubocop/cop/block_comments.rb +17 -0
- data/lib/rubocop/cop/brace_after_percent.rb +9 -5
- data/lib/rubocop/cop/{indentation.rb → case_indentation.rb} +1 -1
- data/lib/rubocop/cop/class_methods.rb +20 -0
- data/lib/rubocop/cop/colon_method_call.rb +44 -0
- data/lib/rubocop/cop/cop.rb +33 -0
- data/lib/rubocop/cop/def_parentheses.rb +1 -1
- data/lib/rubocop/cop/empty_line_between_defs.rb +26 -0
- data/lib/rubocop/cop/empty_lines.rb +10 -13
- data/lib/rubocop/cop/eval.rb +22 -0
- data/lib/rubocop/cop/favor_join.rb +37 -0
- data/lib/rubocop/cop/grammar.rb +2 -2
- data/lib/rubocop/cop/hash_literal.rb +43 -4
- data/lib/rubocop/cop/hash_syntax.rb +2 -2
- data/lib/rubocop/cop/if_then_else.rb +1 -1
- data/lib/rubocop/cop/leading_comment_space.rb +20 -0
- data/lib/rubocop/cop/line_continuation.rb +18 -0
- data/lib/rubocop/cop/line_length.rb +1 -1
- data/lib/rubocop/cop/method_and_variable_snake_case.rb +7 -6
- data/lib/rubocop/cop/method_length.rb +4 -15
- data/lib/rubocop/cop/not.rb +15 -0
- data/lib/rubocop/cop/offence.rb +9 -0
- data/lib/rubocop/cop/op_method.rb +6 -1
- data/lib/rubocop/cop/semicolon.rb +74 -3
- data/lib/rubocop/cop/single_line_methods.rb +60 -0
- data/lib/rubocop/cop/space_after_comma_etc.rb +4 -5
- data/lib/rubocop/cop/space_after_control_keyword.rb +28 -0
- data/lib/rubocop/cop/surrounding_space.rb +48 -9
- data/lib/rubocop/cop/symbol_array.rb +29 -0
- data/lib/rubocop/cop/symbol_snake_case.rb +20 -0
- data/lib/rubocop/cop/trivial_accessors.rb +103 -0
- data/lib/rubocop/cop/unless_else.rb +1 -1
- data/lib/rubocop/cop/variable_interpolation.rb +3 -2
- data/lib/rubocop/cop/word_array.rb +38 -0
- data/lib/rubocop/report/plain_text.rb +1 -1
- data/lib/rubocop/version.rb +1 -1
- data/lib/rubocop.rb +22 -7
- data/rubocop.gemspec +11 -7
- data/spec/project_spec.rb +27 -0
- data/spec/rubocop/cli_spec.rb +556 -388
- data/spec/rubocop/config_spec.rb +399 -0
- data/spec/rubocop/config_store_spec.rb +66 -0
- data/spec/rubocop/cops/alias_spec.rb +7 -0
- data/spec/rubocop/cops/array_literal_spec.rb +18 -1
- data/spec/rubocop/cops/ascii_comments_spec.rb +27 -0
- data/spec/rubocop/cops/ascii_identifiers_spec.rb +27 -0
- data/spec/rubocop/cops/avoid_for_spec.rb +15 -1
- data/spec/rubocop/cops/avoid_global_vars.rb +32 -0
- data/spec/rubocop/cops/block_comments_spec.rb +29 -0
- data/spec/rubocop/cops/brace_after_percent_spec.rb +19 -13
- data/spec/rubocop/cops/{indentation_spec.rb → case_indentation_spec.rb} +2 -2
- data/spec/rubocop/cops/class_methods_spec.rb +49 -0
- data/spec/rubocop/cops/colon_method_call_spec.rb +47 -0
- data/spec/rubocop/cops/empty_line_between_defs_spec.rb +83 -0
- data/spec/rubocop/cops/empty_lines_spec.rb +6 -63
- data/spec/rubocop/cops/eval_spec.rb +36 -0
- data/spec/rubocop/cops/favor_join_spec.rb +39 -0
- data/spec/rubocop/cops/hash_literal_spec.rb +18 -1
- data/spec/rubocop/cops/leading_comment_space_spec.rb +60 -0
- data/spec/rubocop/cops/line_continuation_spec.rb +24 -0
- data/spec/rubocop/cops/line_length_spec.rb +1 -0
- data/spec/rubocop/cops/method_and_variable_snake_case_spec.rb +20 -0
- data/spec/rubocop/cops/method_length_spec.rb +2 -5
- data/spec/rubocop/cops/new_lambda_literal_spec.rb +2 -3
- data/spec/rubocop/cops/not_spec.rb +34 -0
- data/spec/rubocop/cops/offence_spec.rb +7 -0
- data/spec/rubocop/cops/op_method_spec.rb +11 -0
- data/spec/rubocop/cops/semicolon_spec.rb +79 -4
- data/spec/rubocop/cops/single_line_methods_spec.rb +50 -0
- data/spec/rubocop/cops/space_after_control_keyword_spec.rb +28 -0
- data/spec/rubocop/cops/space_after_semicolon_spec.rb +5 -0
- data/spec/rubocop/cops/space_around_equals_in_default_parameter_spec.rb +11 -1
- data/spec/rubocop/cops/space_inside_hash_literal_braces_spec.rb +74 -0
- data/spec/rubocop/cops/symbol_array_spec.rb +25 -0
- data/spec/rubocop/cops/symbol_snake_case_spec.rb +13 -0
- data/spec/rubocop/cops/trivial_accessors_spec.rb +332 -0
- data/spec/rubocop/cops/variable_interpolation_spec.rb +10 -1
- data/spec/rubocop/cops/word_array_spec.rb +39 -0
- data/spec/spec_helper.rb +18 -5
- data/spec/support/file_helper.rb +21 -0
- data/spec/support/isolated_environment.rb +27 -0
- metadata +82 -36
- data/spec/rubocop/cops/ascii_identifiers_and_comments_spec.rb +0 -38
|
@@ -6,14 +6,19 @@ module Rubocop
|
|
|
6
6
|
module Cop
|
|
7
7
|
describe Semicolon do
|
|
8
8
|
let(:s) { Semicolon.new }
|
|
9
|
+
before do
|
|
10
|
+
Semicolon.config = {
|
|
11
|
+
'AllowAfterParameterListInOneLineMethods' => false,
|
|
12
|
+
'AllowBeforeEndInOneLineMethods' => true
|
|
13
|
+
}
|
|
14
|
+
end
|
|
9
15
|
|
|
10
16
|
it 'registers an offence for a single expression' do
|
|
11
17
|
inspect_source(s,
|
|
12
18
|
'file.rb',
|
|
13
19
|
['puts "this is a test";'])
|
|
14
20
|
expect(s.offences.size).to eq(1)
|
|
15
|
-
expect(s.offences.map(&:message))
|
|
16
|
-
.to eq([Semicolon::ERROR_MESSAGE])
|
|
21
|
+
expect(s.offences.map(&:message)).to eq([Semicolon::ERROR_MESSAGE])
|
|
17
22
|
end
|
|
18
23
|
|
|
19
24
|
it 'registers an offence for several expressions' do
|
|
@@ -21,8 +26,78 @@ module Rubocop
|
|
|
21
26
|
'file.rb',
|
|
22
27
|
['puts "this is a test"; puts "So is this"'])
|
|
23
28
|
expect(s.offences.size).to eq(1)
|
|
24
|
-
expect(s.offences.map(&:message))
|
|
25
|
-
|
|
29
|
+
expect(s.offences.map(&:message)).to eq([Semicolon::ERROR_MESSAGE])
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
it 'registers an offence for one line method with two statements' do
|
|
33
|
+
inspect_source(s,
|
|
34
|
+
'file.rb',
|
|
35
|
+
['def foo(a) x(1); y(2); z(3); end'])
|
|
36
|
+
expect(s.offences.size).to eq(2)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it 'registers an offence for semicolon before end if so configured' do
|
|
40
|
+
Semicolon.config['AllowBeforeEndInOneLineMethods'] = false
|
|
41
|
+
inspect_source(s,
|
|
42
|
+
'file.rb',
|
|
43
|
+
['def foo(a) z(3); end'])
|
|
44
|
+
expect(s.offences.size).to eq(1)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it 'accepts semicolon before end if so configured' do
|
|
48
|
+
inspect_source(s,
|
|
49
|
+
'file.rb',
|
|
50
|
+
['def foo(a) z(3); end'])
|
|
51
|
+
expect(s.offences).to be_empty
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
it 'registers an offence for semicolon after params if so configured' do
|
|
55
|
+
inspect_source(s,
|
|
56
|
+
'file.rb',
|
|
57
|
+
['def foo(a); y(2); z(3) end',
|
|
58
|
+
'def bar(a) y(2); z(3) end'])
|
|
59
|
+
expect(s.offences.size).to eq(3)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
it 'accepts semicolon after params if so configured' do
|
|
63
|
+
Semicolon.config['AllowAfterParameterListInOneLineMethods'] = true
|
|
64
|
+
inspect_source(s,
|
|
65
|
+
'file.rb',
|
|
66
|
+
['def foo(a); z(3) end'])
|
|
67
|
+
expect(s.offences).to be_empty
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
it 'accepts one line method definitions' do
|
|
71
|
+
Semicolon.config['AllowAfterParameterListInOneLineMethods'] = true
|
|
72
|
+
inspect_source(s,
|
|
73
|
+
'file.rb',
|
|
74
|
+
['def foo1; x(3) end',
|
|
75
|
+
'def initialize(*_); end',
|
|
76
|
+
'def foo2() x(3); end',
|
|
77
|
+
'def foo3; x(3); end'])
|
|
78
|
+
expect(s.offences).to be_empty
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
it 'accepts one line empty class definitions' do
|
|
82
|
+
inspect_source(s,
|
|
83
|
+
'file.rb',
|
|
84
|
+
[' class Foo < Exception; end',
|
|
85
|
+
' class Bar; end'])
|
|
86
|
+
expect(s.offences).to be_empty
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
it 'accepts one line empty module definitions' do
|
|
90
|
+
inspect_source(s,
|
|
91
|
+
'file.rb',
|
|
92
|
+
['module Foo; end'])
|
|
93
|
+
expect(s.offences).to be_empty
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
it 'registers an offence for semicolon at the end no matter what' do
|
|
97
|
+
inspect_source(s,
|
|
98
|
+
'file.rb',
|
|
99
|
+
['module Foo; end;'])
|
|
100
|
+
expect(s.offences.size).to eq(1)
|
|
26
101
|
end
|
|
27
102
|
end
|
|
28
103
|
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module Rubocop
|
|
6
|
+
module Cop
|
|
7
|
+
describe SingleLineMethods do
|
|
8
|
+
let(:slm) { SingleLineMethods.new }
|
|
9
|
+
before { SingleLineMethods.config = { 'AllowIfMethodIsEmpty' => true } }
|
|
10
|
+
|
|
11
|
+
it 'registers an offence for a single-line method' do
|
|
12
|
+
inspect_source(slm, '',
|
|
13
|
+
['def some_method; body end',
|
|
14
|
+
'def link_to(name, url); {:name => name}; end',
|
|
15
|
+
'def @table.columns; super; end'])
|
|
16
|
+
expect(slm.offences.map(&:message)).to eq(
|
|
17
|
+
[SingleLineMethods::ERROR_MESSAGE] * 3)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it 'registers an offence for an empty method if so configured' do
|
|
21
|
+
SingleLineMethods.config = { 'AllowIfMethodIsEmpty' => false }
|
|
22
|
+
inspect_source(slm, '', ['def no_op; end',
|
|
23
|
+
'def self.resource_class=(klass); end',
|
|
24
|
+
'def @table.columns; end'])
|
|
25
|
+
expect(slm.offences.size).to eq(3)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it 'accepts a single-line empty method if so configured' do
|
|
29
|
+
SingleLineMethods.config = { 'AllowIfMethodIsEmpty' => true }
|
|
30
|
+
inspect_source(slm, '', ['def no_op; end',
|
|
31
|
+
'def self.resource_class=(klass); end',
|
|
32
|
+
'def @table.columns; end'])
|
|
33
|
+
expect(slm.offences).to be_empty
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
it 'accepts a multi-line method' do
|
|
37
|
+
inspect_source(slm, '', ['def some_method',
|
|
38
|
+
' body',
|
|
39
|
+
'end'])
|
|
40
|
+
expect(slm.offences).to be_empty
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it 'does not crash on an method with a capitalized name' do
|
|
44
|
+
inspect_source(slm, '', ['def NoSnakeCase',
|
|
45
|
+
'end'])
|
|
46
|
+
expect(slm.offences).to be_empty
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module Rubocop
|
|
6
|
+
module Cop
|
|
7
|
+
describe SpaceAfterControlKeyword do
|
|
8
|
+
let(:ap) { SpaceAfterControlKeyword.new }
|
|
9
|
+
|
|
10
|
+
it 'registers an offence for normal if' do
|
|
11
|
+
inspect_source(ap, 'file.rb',
|
|
12
|
+
['if(test) then result end'])
|
|
13
|
+
expect(ap.offences.size).to eq(1)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it 'registers an offence for modifier unless' do
|
|
17
|
+
inspect_source(ap, 'file.rb', ['action unless(test)'])
|
|
18
|
+
|
|
19
|
+
expect(ap.offences.size).to eq(1)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it 'does not get confused by keywords' do
|
|
23
|
+
inspect_source(ap, 'file.rb', ['[:if, :unless].action'])
|
|
24
|
+
expect(ap.offences).to be_empty
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -12,6 +12,11 @@ module Rubocop
|
|
|
12
12
|
expect(space.offences.map(&:message)).to eq(
|
|
13
13
|
['Space missing after semicolon.'])
|
|
14
14
|
end
|
|
15
|
+
|
|
16
|
+
it 'does not crash if semicolon is the last character of the file' do
|
|
17
|
+
inspect_source(space, 'file.rb', ['x = 1;'])
|
|
18
|
+
expect(space.offences.map(&:message)).to be_empty
|
|
19
|
+
end
|
|
15
20
|
end
|
|
16
21
|
end
|
|
17
22
|
end
|
|
@@ -13,8 +13,18 @@ module Rubocop
|
|
|
13
13
|
['Surrounding space missing in default value assignment.'] * 2)
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
+
it 'registers an offence for assignment of empty string without space' do
|
|
17
|
+
inspect_source(space, 'file.rb', ['def f(x, y="", z=1)', 'end'])
|
|
18
|
+
expect(space.offences.size).to eq(2)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it 'registers an offence for assignment of empty list without space' do
|
|
22
|
+
inspect_source(space, 'file.rb', ['def f(x, y=[])', 'end'])
|
|
23
|
+
expect(space.offences.size).to eq(1)
|
|
24
|
+
end
|
|
25
|
+
|
|
16
26
|
it 'accepts default value assignment with space' do
|
|
17
|
-
inspect_source(space, 'file.rb', ['def f(x, y = 0, z =
|
|
27
|
+
inspect_source(space, 'file.rb', ['def f(x, y = 0, z = {})', 'end'])
|
|
18
28
|
expect(space.offences.map(&:message)).to be_empty
|
|
19
29
|
end
|
|
20
30
|
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module Rubocop
|
|
6
|
+
module Cop
|
|
7
|
+
describe SpaceInsideHashLiteralBraces do
|
|
8
|
+
let(:sihlb) { SpaceInsideHashLiteralBraces.new }
|
|
9
|
+
before do
|
|
10
|
+
SpaceInsideHashLiteralBraces.config = {
|
|
11
|
+
'EnforcedStyleIsWithSpaces' => true
|
|
12
|
+
}
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it 'registers an offence for hashes with no spaces by default' do
|
|
16
|
+
inspect_source(sihlb, '',
|
|
17
|
+
['h = {a: 1, b: 2}',
|
|
18
|
+
'h = {a => 1 }'])
|
|
19
|
+
expect(sihlb.offences.map(&:message)).to eq(
|
|
20
|
+
['Space inside hash literal braces missing.'] * 3)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it 'registers an offence for hashes with no spaces if so configured' do
|
|
24
|
+
inspect_source(sihlb, '',
|
|
25
|
+
['h = {a: 1, b: 2}',
|
|
26
|
+
'h = {a => 1 }'])
|
|
27
|
+
expect(sihlb.offences.map(&:message)).to eq(
|
|
28
|
+
['Space inside hash literal braces missing.'] * 3)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it 'registers an offence for hashes with spaces if so configured' do
|
|
32
|
+
SpaceInsideHashLiteralBraces.config['EnforcedStyleIsWithSpaces'] =
|
|
33
|
+
false
|
|
34
|
+
inspect_source(sihlb, '',
|
|
35
|
+
['h = { a: 1, b: 2 }'])
|
|
36
|
+
expect(sihlb.offences.map(&:message)).to eq(
|
|
37
|
+
['Space inside hash literal braces detected.'] * 2)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it 'accepts hashes with spaces by default' do
|
|
41
|
+
inspect_source(sihlb, '',
|
|
42
|
+
['h = { a: 1, b: 2 }',
|
|
43
|
+
'h = { a => 1 }'])
|
|
44
|
+
expect(sihlb.offences.map(&:message)).to be_empty
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it 'accepts hashes with no spaces if so configured' do
|
|
48
|
+
SpaceInsideHashLiteralBraces.config['EnforcedStyleIsWithSpaces'] =
|
|
49
|
+
false
|
|
50
|
+
inspect_source(sihlb, '',
|
|
51
|
+
['h = {a: 1, b: 2}',
|
|
52
|
+
'h = {a => 1}'])
|
|
53
|
+
expect(sihlb.offences.map(&:message)).to be_empty
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
it 'accepts empty hashes without spaces by default' do
|
|
57
|
+
inspect_source(sihlb, '', ['h = {}'])
|
|
58
|
+
expect(sihlb.offences).to be_empty
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
it 'accepts empty hashes without spaces if configured false' do
|
|
62
|
+
SpaceInsideHashLiteralBraces.config['EnforcedStyleIsWithSpaces'] =
|
|
63
|
+
false
|
|
64
|
+
inspect_source(sihlb, '', ['h = {}'])
|
|
65
|
+
expect(sihlb.offences).to be_empty
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
it 'accepts empty hashes without spaces even if configured true' do
|
|
69
|
+
inspect_source(sihlb, '', ['h = {}'])
|
|
70
|
+
expect(sihlb.offences).to be_empty
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module Rubocop
|
|
6
|
+
module Cop
|
|
7
|
+
describe SymbolArray do
|
|
8
|
+
let(:sa) { SymbolArray.new }
|
|
9
|
+
|
|
10
|
+
it 'registers an offence for arrays of symbols', { ruby: 2.0 } do
|
|
11
|
+
inspect_source(sa,
|
|
12
|
+
'file.rb',
|
|
13
|
+
['[:one, :two, :three]'])
|
|
14
|
+
expect(sa.offences.size).to eq(1)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it 'does nothing on Ruby 1.9', { ruby: 1.9 } do
|
|
18
|
+
inspect_source(sa,
|
|
19
|
+
'file.rb',
|
|
20
|
+
['[:one, :two, :three]'])
|
|
21
|
+
expect(sa.offences).to be_empty
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -14,12 +14,25 @@ module Rubocop
|
|
|
14
14
|
['Use snake_case for symbols.'])
|
|
15
15
|
end
|
|
16
16
|
|
|
17
|
+
it 'registers an offence for symbol used as hash label' do
|
|
18
|
+
inspect_source(snake_case, 'file.rb',
|
|
19
|
+
['{ ONE: 1, TWO: 2 }'])
|
|
20
|
+
expect(snake_case.offences.map(&:message)).to eq(
|
|
21
|
+
['Use snake_case for symbols.'] * 2)
|
|
22
|
+
end
|
|
23
|
+
|
|
17
24
|
it 'accepts snake case in names' do
|
|
18
25
|
inspect_source(snake_case, 'file.rb',
|
|
19
26
|
['test = :good_idea'])
|
|
20
27
|
expect(snake_case.offences).to be_empty
|
|
21
28
|
end
|
|
22
29
|
|
|
30
|
+
it 'accepts snake case in hash label names' do
|
|
31
|
+
inspect_source(snake_case, 'file.rb',
|
|
32
|
+
['{ one: 1, one_more_3: 2 }'])
|
|
33
|
+
expect(snake_case.offences).to be_empty
|
|
34
|
+
end
|
|
35
|
+
|
|
23
36
|
it 'accepts snake case with a prefix @ in names' do
|
|
24
37
|
inspect_source(snake_case, 'file.rb',
|
|
25
38
|
['test = :@good_idea'])
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module Rubocop
|
|
6
|
+
module Cop
|
|
7
|
+
describe TrivialAccessors do
|
|
8
|
+
let(:trivial_accessors_finder) { TrivialAccessors.new }
|
|
9
|
+
|
|
10
|
+
before :each do
|
|
11
|
+
trivial_accessors_finder.offences.clear
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it 'finds trivial reader' do
|
|
15
|
+
inspect_source(trivial_accessors_finder, '',
|
|
16
|
+
['def foo',
|
|
17
|
+
' @foo',
|
|
18
|
+
'end',
|
|
19
|
+
'',
|
|
20
|
+
'def Foo',
|
|
21
|
+
' @Foo',
|
|
22
|
+
'end'])
|
|
23
|
+
expect(trivial_accessors_finder.offences.size).to eq(2)
|
|
24
|
+
expect(trivial_accessors_finder.offences
|
|
25
|
+
.map(&:line_number).sort).to eq([1, 5])
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it 'finds trivial reader in a class' do
|
|
29
|
+
inspect_source(trivial_accessors_finder, '',
|
|
30
|
+
['class TrivialFoo',
|
|
31
|
+
' def foo',
|
|
32
|
+
' @foo',
|
|
33
|
+
' end',
|
|
34
|
+
' def bar',
|
|
35
|
+
' !foo',
|
|
36
|
+
' end',
|
|
37
|
+
'end'])
|
|
38
|
+
expect(trivial_accessors_finder.offences.size).to eq(1)
|
|
39
|
+
expect(trivial_accessors_finder.offences
|
|
40
|
+
.map(&:line_number).sort).to eq([2])
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it 'finds trivial reader in a nested class' do
|
|
44
|
+
inspect_source(trivial_accessors_finder, '',
|
|
45
|
+
['class TrivialFoo',
|
|
46
|
+
' class Nested',
|
|
47
|
+
' def foo',
|
|
48
|
+
' @foo',
|
|
49
|
+
' end',
|
|
50
|
+
' end',
|
|
51
|
+
'end'])
|
|
52
|
+
expect(trivial_accessors_finder.offences.size).to eq(1)
|
|
53
|
+
expect(trivial_accessors_finder.offences
|
|
54
|
+
.map(&:line_number).sort).to eq([3])
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
it 'finds trivial readers in a little less trivial class' do
|
|
58
|
+
inspect_source(trivial_accessors_finder, '',
|
|
59
|
+
['class TrivialFoo',
|
|
60
|
+
' def foo',
|
|
61
|
+
' @foo',
|
|
62
|
+
' end',
|
|
63
|
+
' def foo_and_bar',
|
|
64
|
+
' @foo_bar = @foo + @bar',
|
|
65
|
+
' end',
|
|
66
|
+
' def foo_bar',
|
|
67
|
+
' @foo_bar',
|
|
68
|
+
' end',
|
|
69
|
+
' def foo?',
|
|
70
|
+
' foo.present?',
|
|
71
|
+
' end',
|
|
72
|
+
' def bar?',
|
|
73
|
+
' !bar',
|
|
74
|
+
' end',
|
|
75
|
+
' def foobar',
|
|
76
|
+
' foo? ? foo.value : "bar"',
|
|
77
|
+
' end',
|
|
78
|
+
' def bar',
|
|
79
|
+
' foo.bar',
|
|
80
|
+
' end',
|
|
81
|
+
' def foo_required?',
|
|
82
|
+
' super && !bar_required?',
|
|
83
|
+
' end',
|
|
84
|
+
' def self.from_omniauth(auth)',
|
|
85
|
+
' foobars.each do |f|',
|
|
86
|
+
' # do stuff',
|
|
87
|
+
' end',
|
|
88
|
+
' end',
|
|
89
|
+
' def regex',
|
|
90
|
+
' %r{\A#{visit node}\Z}',
|
|
91
|
+
' end',
|
|
92
|
+
' def array',
|
|
93
|
+
' [foo, bar].join',
|
|
94
|
+
' end',
|
|
95
|
+
' def string',
|
|
96
|
+
' "string"',
|
|
97
|
+
' end',
|
|
98
|
+
' def class',
|
|
99
|
+
' Foo.class',
|
|
100
|
+
' end',
|
|
101
|
+
' def with_return',
|
|
102
|
+
' return foo',
|
|
103
|
+
' end',
|
|
104
|
+
' def captures',
|
|
105
|
+
' (length - 1).times.map { |i| self[i + 1] }',
|
|
106
|
+
' end',
|
|
107
|
+
' def foo val',
|
|
108
|
+
' super',
|
|
109
|
+
' @val',
|
|
110
|
+
' end',
|
|
111
|
+
'end'])
|
|
112
|
+
expect(trivial_accessors_finder.offences.size).to eq(2)
|
|
113
|
+
expect(trivial_accessors_finder.offences
|
|
114
|
+
.map(&:line_number).sort).to eq([2, 8])
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
it 'finds trivial reader with braces' do
|
|
118
|
+
inspect_source(trivial_accessors_finder, '',
|
|
119
|
+
['class Test',
|
|
120
|
+
' # trivial reader with braces',
|
|
121
|
+
' def name()',
|
|
122
|
+
' @name',
|
|
123
|
+
' end',
|
|
124
|
+
'end'])
|
|
125
|
+
expect(trivial_accessors_finder.offences.size).to eq(1)
|
|
126
|
+
expect(trivial_accessors_finder.offences
|
|
127
|
+
.map(&:line_number).sort).to eq([3])
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
it 'finds trivial writer without braces' do
|
|
131
|
+
inspect_source(trivial_accessors_finder, '',
|
|
132
|
+
['class Test',
|
|
133
|
+
' # trivial writer without braces',
|
|
134
|
+
' def name= name',
|
|
135
|
+
' @name = name',
|
|
136
|
+
' end',
|
|
137
|
+
'end'])
|
|
138
|
+
expect(trivial_accessors_finder.offences.size).to eq(1)
|
|
139
|
+
expect(trivial_accessors_finder.offences
|
|
140
|
+
.map(&:line_number).sort).to eq([3])
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
it 'does not find trivial writer with function calls' do
|
|
144
|
+
inspect_source(trivial_accessors_finder, '',
|
|
145
|
+
['class TrivialTest',
|
|
146
|
+
' def test=(val)',
|
|
147
|
+
' @test = val',
|
|
148
|
+
' some_function_call',
|
|
149
|
+
' or_more_of_them',
|
|
150
|
+
' end',
|
|
151
|
+
'end'])
|
|
152
|
+
expect(trivial_accessors_finder.offences).to be_empty
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
it 'finds trivials with less peculiar methods' do
|
|
156
|
+
inspect_source(trivial_accessors_finder, '',
|
|
157
|
+
['class NilStats',
|
|
158
|
+
'def most_traded_pair',
|
|
159
|
+
'end',
|
|
160
|
+
'def win_ratio',
|
|
161
|
+
'end',
|
|
162
|
+
'def win_ratio_percentage()',
|
|
163
|
+
'end',
|
|
164
|
+
'def pips_won',
|
|
165
|
+
' 0.0',
|
|
166
|
+
'end',
|
|
167
|
+
'def gain_at(date)',
|
|
168
|
+
' 1',
|
|
169
|
+
'end',
|
|
170
|
+
'def gain= value',
|
|
171
|
+
' @value = 0.1',
|
|
172
|
+
'end',
|
|
173
|
+
'def gain_percentage',
|
|
174
|
+
' 0',
|
|
175
|
+
'end',
|
|
176
|
+
'def gain_breakdown(options = {})',
|
|
177
|
+
' []',
|
|
178
|
+
'end',
|
|
179
|
+
'def copy_to_all_ratio',
|
|
180
|
+
' nil',
|
|
181
|
+
'end',
|
|
182
|
+
'def trade_population',
|
|
183
|
+
' {}',
|
|
184
|
+
'end',
|
|
185
|
+
'def average_leverage',
|
|
186
|
+
' 1',
|
|
187
|
+
'end',
|
|
188
|
+
'def with_yield',
|
|
189
|
+
' yield',
|
|
190
|
+
'rescue Error => e',
|
|
191
|
+
' #do stuff',
|
|
192
|
+
'end',
|
|
193
|
+
'end'])
|
|
194
|
+
expect(trivial_accessors_finder.offences).to be_empty
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
it 'finds oneliner trivials' do
|
|
198
|
+
inspect_source(trivial_accessors_finder, '',
|
|
199
|
+
['class Oneliner',
|
|
200
|
+
' def foo; @foo; end',
|
|
201
|
+
' def foo= foo; @foo = foo; end',
|
|
202
|
+
'end'])
|
|
203
|
+
expect(trivial_accessors_finder.offences.size).to eq(2)
|
|
204
|
+
expect(trivial_accessors_finder.offences
|
|
205
|
+
.map(&:line_number).sort).to eq([2, 3])
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
it 'does not find a trivial reader' do
|
|
209
|
+
inspect_source(trivial_accessors_finder, '',
|
|
210
|
+
['def bar',
|
|
211
|
+
' @bar + foo',
|
|
212
|
+
'end'])
|
|
213
|
+
expect(trivial_accessors_finder.offences).to be_empty
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
it 'finds trivial writer' do
|
|
217
|
+
inspect_source(trivial_accessors_finder, '',
|
|
218
|
+
['def foo=(val)',
|
|
219
|
+
' @foo = val',
|
|
220
|
+
'end'])
|
|
221
|
+
expect(trivial_accessors_finder.offences.size).to eq(1)
|
|
222
|
+
expect(trivial_accessors_finder.offences
|
|
223
|
+
.map(&:line_number).sort).to eq([1])
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
it 'finds trivial writer in a class' do
|
|
227
|
+
inspect_source(trivial_accessors_finder, '',
|
|
228
|
+
['class TrivialFoo',
|
|
229
|
+
' def foo=(val)',
|
|
230
|
+
' @foo = val',
|
|
231
|
+
' end',
|
|
232
|
+
' def void(no_value)',
|
|
233
|
+
' end',
|
|
234
|
+
' def inspect(sexp)',
|
|
235
|
+
' each(:def, sexp) do |item|',
|
|
236
|
+
' #do stuff',
|
|
237
|
+
' end',
|
|
238
|
+
' end',
|
|
239
|
+
' def if_method(foo)',
|
|
240
|
+
' if true',
|
|
241
|
+
' unless false',
|
|
242
|
+
' #do stuff',
|
|
243
|
+
' end',
|
|
244
|
+
' end',
|
|
245
|
+
' end',
|
|
246
|
+
'end'])
|
|
247
|
+
expect(trivial_accessors_finder.offences.size).to eq(1)
|
|
248
|
+
expect(trivial_accessors_finder.offences
|
|
249
|
+
.map(&:line_number).sort).to eq([2])
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
it 'finds trivial accessors in a little less trivial class' do
|
|
253
|
+
inspect_source(trivial_accessors_finder, '',
|
|
254
|
+
['class TrivialFoo',
|
|
255
|
+
' def foo',
|
|
256
|
+
' @foo',
|
|
257
|
+
' end',
|
|
258
|
+
' def foo_and_bar',
|
|
259
|
+
' @foo_bar = @foo + @bar',
|
|
260
|
+
' end',
|
|
261
|
+
' def foo_bar',
|
|
262
|
+
' @foo_bar',
|
|
263
|
+
' end',
|
|
264
|
+
' def bar=(bar_value)',
|
|
265
|
+
' @bar = bar_value',
|
|
266
|
+
' end',
|
|
267
|
+
'end'])
|
|
268
|
+
expect(trivial_accessors_finder.offences.size).to eq(3)
|
|
269
|
+
expect(trivial_accessors_finder.offences
|
|
270
|
+
.map(&:line_number).sort).to eq([2, 8, 11])
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
it 'does not find a trivial writer' do
|
|
274
|
+
inspect_source(trivial_accessors_finder, '',
|
|
275
|
+
['def bar=(value)',
|
|
276
|
+
' @bar = value + 42',
|
|
277
|
+
'end'])
|
|
278
|
+
expect(trivial_accessors_finder.offences).to be_empty
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
it 'finds trivial writers in a little less trivial class' do
|
|
282
|
+
inspect_source(trivial_accessors_finder, '',
|
|
283
|
+
['class TrivialFoo',
|
|
284
|
+
' def foo_bar=(foo, bar)',
|
|
285
|
+
' @foo_bar = foo + bar',
|
|
286
|
+
' end',
|
|
287
|
+
' def universal=(answer=42)',
|
|
288
|
+
' @universal = answer',
|
|
289
|
+
' end',
|
|
290
|
+
' def bar=(bar_value)',
|
|
291
|
+
' @bar = bar_value',
|
|
292
|
+
' end',
|
|
293
|
+
'end'])
|
|
294
|
+
expect(trivial_accessors_finder.offences.size).to eq(2)
|
|
295
|
+
expect(trivial_accessors_finder.offences
|
|
296
|
+
.map(&:line_number).sort).to eq([5, 8])
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
it 'does not find trivial accessors with method calls' do
|
|
300
|
+
inspect_source(trivial_accessors_finder, '',
|
|
301
|
+
['class TrivialFoo',
|
|
302
|
+
' def foo_bar(foo)',
|
|
303
|
+
' foo_bar = foo + 42',
|
|
304
|
+
' end',
|
|
305
|
+
' def foo(value)',
|
|
306
|
+
' foo = []',
|
|
307
|
+
' # do stuff',
|
|
308
|
+
' foo',
|
|
309
|
+
' end',
|
|
310
|
+
' def bar',
|
|
311
|
+
' foo_method',
|
|
312
|
+
' end',
|
|
313
|
+
'end'])
|
|
314
|
+
expect(trivial_accessors_finder.offences).to be_empty
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
it 'does not find trivial writer with exceptions' do
|
|
318
|
+
inspect_source(trivial_accessors_finder, '',
|
|
319
|
+
[' def expiration_formatted=(value)',
|
|
320
|
+
' begin',
|
|
321
|
+
' @expiration = foo_stuff',
|
|
322
|
+
' rescue ArgumentError',
|
|
323
|
+
' @expiration = nil',
|
|
324
|
+
' end',
|
|
325
|
+
' self[:expiration] = @expiration',
|
|
326
|
+
' end'])
|
|
327
|
+
expect(trivial_accessors_finder.offences).to be_empty
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
end # describe TrivialAccessors
|
|
331
|
+
end # Cop
|
|
332
|
+
end # Rubocop
|