rubocop 0.3.0 → 0.6.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.
- data/.rubocop.yml +176 -0
- data/.travis.yml +1 -0
- data/CHANGELOG.md +60 -0
- data/CONTRIBUTING.md +13 -6
- data/Gemfile +1 -13
- data/README.md +131 -6
- data/Rakefile +1 -15
- data/bin/rubocop +2 -1
- data/lib/rubocop/cli.rb +133 -21
- data/lib/rubocop/cop/alias.rb +18 -0
- data/lib/rubocop/cop/align_parameters.rb +5 -4
- data/lib/rubocop/cop/array_literal.rb +22 -0
- data/lib/rubocop/cop/ascii_identifiers_and_comments.rb +18 -0
- data/lib/rubocop/cop/avoid_class_vars.rb +20 -0
- data/lib/rubocop/cop/avoid_for.rb +17 -0
- data/lib/rubocop/cop/avoid_perl_backrefs.rb +20 -0
- data/lib/rubocop/cop/avoid_perlisms.rb +46 -0
- data/lib/rubocop/cop/blocks.rb +1 -1
- data/lib/rubocop/cop/brace_after_percent.rb +28 -0
- data/lib/rubocop/cop/class_and_module_camel_case.rb +20 -0
- data/lib/rubocop/cop/collection_methods.rb +29 -0
- data/lib/rubocop/cop/cop.rb +16 -4
- data/lib/rubocop/cop/def_parentheses.rb +4 -1
- data/lib/rubocop/cop/encoding.rb +7 -5
- data/lib/rubocop/cop/end_of_line.rb +1 -3
- data/lib/rubocop/cop/ensure_return.rb +36 -0
- data/lib/rubocop/cop/favor_modifier.rb +82 -0
- data/lib/rubocop/cop/favor_percent_r.rb +19 -0
- data/lib/rubocop/cop/favor_sprintf.rb +37 -0
- data/lib/rubocop/cop/favor_unless_over_negated_if.rb +47 -0
- data/lib/rubocop/cop/grammar.rb +12 -8
- data/lib/rubocop/cop/handle_exceptions.rb +21 -0
- data/lib/rubocop/cop/hash_literal.rb +22 -0
- data/lib/rubocop/cop/indentation.rb +1 -1
- data/lib/rubocop/cop/line_length.rb +7 -3
- data/lib/rubocop/cop/method_and_variable_snake_case.rb +31 -0
- data/lib/rubocop/cop/method_length.rb +66 -0
- data/lib/rubocop/cop/new_lambda_literal.rb +17 -0
- data/lib/rubocop/cop/offence.rb +3 -1
- data/lib/rubocop/cop/op_method.rb +23 -0
- data/lib/rubocop/cop/parentheses_around_condition.rb +24 -0
- data/lib/rubocop/cop/percent_literals.rb +25 -0
- data/lib/rubocop/cop/percent_r.rb +19 -0
- data/lib/rubocop/cop/reduce_arguments.rb +67 -0
- data/lib/rubocop/cop/rescue_exception.rb +39 -0
- data/lib/rubocop/cop/rescue_modifier.rb +20 -0
- data/lib/rubocop/cop/semicolon.rb +18 -0
- data/lib/rubocop/cop/space_after_comma_etc.rb +2 -1
- data/lib/rubocop/cop/string_literals.rb +12 -0
- data/lib/rubocop/cop/surrounding_space.rb +48 -47
- data/lib/rubocop/cop/symbol_snake_case.rb +27 -0
- data/lib/rubocop/cop/syntax.rb +42 -0
- data/lib/rubocop/cop/tab.rb +1 -3
- data/lib/rubocop/cop/variable_interpolation.rb +20 -0
- data/lib/rubocop/cop/when_then.rb +3 -0
- data/lib/rubocop/report/emacs_style.rb +2 -2
- data/lib/rubocop/report/plain_text.rb +1 -1
- data/lib/rubocop/report/report.rb +5 -2
- data/lib/rubocop/version.rb +3 -1
- data/lib/rubocop.rb +37 -0
- data/rubocop.gemspec +36 -134
- data/spec/rubocop/cli_spec.rb +326 -48
- data/spec/rubocop/cops/alias_spec.rb +36 -0
- data/spec/rubocop/cops/align_parameters_spec.rb +43 -34
- data/spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb +4 -4
- data/spec/rubocop/cops/array_literal_spec.rb +29 -0
- data/spec/rubocop/cops/ascii_identifiers_and_comments_spec.rb +38 -0
- data/spec/rubocop/cops/avoid_class_vars_spec.rb +20 -0
- data/spec/rubocop/cops/avoid_for_spec.rb +22 -0
- data/spec/rubocop/cops/avoid_perl_backrefs_spec.rb +18 -0
- data/spec/rubocop/cops/avoid_perlisms_spec.rb +44 -0
- data/spec/rubocop/cops/brace_after_percent_spec.rb +27 -0
- data/spec/rubocop/cops/class_and_module_camel_case_spec.rb +34 -0
- data/spec/rubocop/cops/collection_methods_spec.rb +39 -0
- data/spec/rubocop/cops/cop_spec.rb +5 -5
- data/spec/rubocop/cops/def_with_parentheses_spec.rb +5 -5
- data/spec/rubocop/cops/def_without_parentheses_spec.rb +4 -4
- data/spec/rubocop/cops/empty_lines_spec.rb +8 -8
- data/spec/rubocop/cops/encoding_spec.rb +17 -11
- data/spec/rubocop/cops/end_of_line_spec.rb +5 -5
- data/spec/rubocop/cops/ensure_return_spec.rb +37 -0
- data/spec/rubocop/cops/favor_modifier_spec.rb +112 -0
- data/spec/rubocop/cops/favor_percent_r.rb +29 -0
- data/spec/rubocop/cops/favor_sprintf_spec.rb +57 -0
- data/spec/rubocop/cops/favor_unless_over_negated_if_spec.rb +62 -0
- data/spec/rubocop/cops/favor_until_over_negated_while_spec.rb +45 -0
- data/spec/rubocop/cops/grammar_spec.rb +59 -44
- data/spec/rubocop/cops/handle_exceptions_spec.rb +36 -0
- data/spec/rubocop/cops/hash_literal_spec.rb +29 -0
- data/spec/rubocop/cops/hash_syntax_spec.rb +10 -10
- data/spec/rubocop/cops/if_with_semicolon_spec.rb +3 -3
- data/spec/rubocop/cops/indentation_spec.rb +7 -7
- data/spec/rubocop/cops/line_length_spec.rb +4 -4
- data/spec/rubocop/cops/method_and_variable_snake_case_spec.rb +47 -0
- data/spec/rubocop/cops/method_length_spec.rb +150 -0
- data/spec/rubocop/cops/multiline_blocks_spec.rb +4 -4
- data/spec/rubocop/cops/multiline_if_then_spec.rb +7 -7
- data/spec/rubocop/cops/new_lambda_literal_spec.rb +23 -0
- data/spec/rubocop/cops/numeric_literals_spec.rb +10 -10
- data/spec/rubocop/cops/offence_spec.rb +10 -4
- data/spec/rubocop/cops/one_line_conditional_spec.rb +2 -3
- data/spec/rubocop/cops/op_method_spec.rb +58 -0
- data/spec/rubocop/cops/parameter_lists_spec.rb +4 -4
- data/spec/rubocop/cops/parentheses_around_condition_spec.rb +64 -0
- data/spec/rubocop/cops/percent_literals_spec.rb +47 -0
- data/spec/rubocop/cops/percent_r_spec.rb +29 -0
- data/spec/rubocop/cops/reduce_arguments_spec.rb +57 -0
- data/spec/rubocop/cops/rescue_exception_spec.rb +73 -0
- data/spec/rubocop/cops/rescue_modifier.rb +40 -0
- data/spec/rubocop/cops/semicolon_spec.rb +29 -0
- data/spec/rubocop/cops/single_line_blocks_spec.rb +4 -4
- data/spec/rubocop/cops/space_after_colon_spec.rb +5 -5
- data/spec/rubocop/cops/space_after_comma_spec.rb +3 -3
- data/spec/rubocop/cops/space_after_semicolon_spec.rb +3 -3
- data/spec/rubocop/cops/space_around_braces_spec.rb +6 -6
- data/spec/rubocop/cops/space_around_equals_in_default_parameter_spec.rb +22 -0
- data/spec/rubocop/cops/space_around_operators_spec.rb +57 -25
- data/spec/rubocop/cops/space_inside_brackets_spec.rb +8 -8
- data/spec/rubocop/cops/space_inside_parens_spec.rb +4 -4
- data/spec/rubocop/cops/string_literals_spec.rb +9 -6
- data/spec/rubocop/cops/symbol_snake_case_spec.rb +74 -0
- data/spec/rubocop/cops/syntax_spec.rb +34 -0
- data/spec/rubocop/cops/tab_spec.rb +4 -4
- data/spec/rubocop/cops/ternary_operator_spec.rb +8 -8
- data/spec/rubocop/cops/trailing_whitespace_spec.rb +4 -4
- data/spec/rubocop/cops/unless_else_spec.rb +4 -4
- data/spec/rubocop/cops/variable_interpolation_spec.rb +45 -0
- data/spec/rubocop/cops/when_then_spec.rb +12 -5
- data/spec/rubocop/reports/emacs_style_spec.rb +3 -3
- data/spec/rubocop/reports/report_spec.rb +5 -5
- data/spec/spec_helper.rb +6 -0
- metadata +165 -36
- data/.rbenv-version +0 -1
- data/.rvmrc +0 -1
- data/Gemfile.lock +0 -39
- data/VERSION +0 -1
- data/features/rubocop.feature +0 -9
- data/features/step_definitions/rubocop_steps.rb +0 -1
- data/features/support/env.rb +0 -15
|
@@ -5,42 +5,42 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe AlignParameters do
|
|
8
|
-
let
|
|
8
|
+
let(:align) { AlignParameters.new }
|
|
9
9
|
|
|
10
10
|
it 'registers an offence for parameters with single indent' do
|
|
11
11
|
inspect_source(align, 'file.rb', ['function(a,',
|
|
12
|
-
|
|
13
|
-
align.offences.map(&:message).
|
|
12
|
+
' if b then c else d end)'])
|
|
13
|
+
expect(align.offences.map(&:message)).to eq(
|
|
14
14
|
['Align the parameters of a method call if they span more than ' +
|
|
15
|
-
'one line.']
|
|
15
|
+
'one line.'])
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
it 'registers an offence for parameters with double indent' do
|
|
19
19
|
inspect_source(align, 'file.rb', ['function(a,',
|
|
20
|
-
|
|
21
|
-
align.offences.map(&:message).
|
|
20
|
+
' if b then c else d end)'])
|
|
21
|
+
expect(align.offences.map(&:message)).to eq(
|
|
22
22
|
['Align the parameters of a method call if they span more than ' +
|
|
23
|
-
'one line.']
|
|
23
|
+
'one line.'])
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
it 'accepts correctly aligned parameters' do
|
|
27
27
|
inspect_source(align, 'file.rb', ['function(a,',
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
align.offences.map(&:message).
|
|
28
|
+
' 0, 1,',
|
|
29
|
+
' (x + y),',
|
|
30
|
+
' if b then c else d end)'])
|
|
31
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
32
32
|
end
|
|
33
33
|
|
|
34
34
|
it 'accepts calls that only span one line' do
|
|
35
35
|
inspect_source(align, 'file.rb', ['find(path, s, @special[sexp[0]])'])
|
|
36
|
-
align.offences.map(&:message).
|
|
36
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
37
37
|
end
|
|
38
38
|
|
|
39
39
|
it "doesn't get confused by a symbol argument" do
|
|
40
40
|
inspect_source(align, '',
|
|
41
41
|
['add_offence(:convention, index,',
|
|
42
42
|
' ERROR_MESSAGE % kind)'])
|
|
43
|
-
align.offences.map(&:message).
|
|
43
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
44
44
|
end
|
|
45
45
|
|
|
46
46
|
it "doesn't get confused by splat operator" do
|
|
@@ -53,49 +53,58 @@ module Rubocop
|
|
|
53
53
|
' c)',
|
|
54
54
|
'func3(*a)',
|
|
55
55
|
])
|
|
56
|
-
align.offences.map(&:to_s).
|
|
56
|
+
expect(align.offences.map(&:to_s)).to eq(
|
|
57
57
|
['C: 5: Align the parameters of a method call if they span ' +
|
|
58
|
-
'more than one line.']
|
|
58
|
+
'more than one line.'])
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
it "doesn't get confused by extra comma at the end" do
|
|
62
|
+
inspect_source(align, '',
|
|
63
|
+
['func1(a,',
|
|
64
|
+
' b,)'])
|
|
65
|
+
expect(align.offences.map(&:to_s)).to eq(
|
|
66
|
+
['C: 2: Align the parameters of a method call if they span ' +
|
|
67
|
+
'more than one line.'])
|
|
59
68
|
end
|
|
60
69
|
|
|
61
70
|
it 'can handle a correctly aligned string literal as first argument' do
|
|
62
71
|
inspect_source(align, '',
|
|
63
72
|
['add_offence(:convention, x,',
|
|
64
73
|
' a)'])
|
|
65
|
-
align.offences.map(&:message).
|
|
74
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
66
75
|
end
|
|
67
76
|
|
|
68
77
|
it 'can handle a string literal as other argument' do
|
|
69
78
|
inspect_source(align, '',
|
|
70
79
|
['add_offence(:convention,',
|
|
71
80
|
' "", a)'])
|
|
72
|
-
align.offences.map(&:message).
|
|
81
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
73
82
|
end
|
|
74
83
|
|
|
75
84
|
it "doesn't get confused by a line break inside a parameter" do
|
|
76
85
|
inspect_source(align, '',
|
|
77
86
|
['read(path, { headers: true,',
|
|
78
87
|
' converters: :numeric })'])
|
|
79
|
-
align.offences.map(&:message).
|
|
88
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
80
89
|
end
|
|
81
90
|
|
|
82
91
|
it "doesn't get confused by symbols with embedded expressions" do
|
|
83
92
|
inspect_source(align, '',
|
|
84
93
|
['send(:"#{name}_comments_path")'])
|
|
85
|
-
align.offences.map(&:message).
|
|
94
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
86
95
|
end
|
|
87
96
|
|
|
88
97
|
it "doesn't get confused by regexen with embedded expressions" do
|
|
89
98
|
inspect_source(align, '',
|
|
90
99
|
['a(/#{name}/)'])
|
|
91
|
-
align.offences.map(&:message).
|
|
100
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
92
101
|
end
|
|
93
102
|
|
|
94
103
|
it 'accepts braceless hashes' do
|
|
95
104
|
inspect_source(align, '',
|
|
96
105
|
['run(collection, :entry_name => label,',
|
|
97
106
|
' :paginator => paginator)'])
|
|
98
|
-
align.offences.map(&:message).
|
|
107
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
99
108
|
end
|
|
100
109
|
|
|
101
110
|
it 'accepts the first parameter being on a new row' do
|
|
@@ -104,7 +113,7 @@ module Rubocop
|
|
|
104
113
|
' a,',
|
|
105
114
|
' b',
|
|
106
115
|
' )'])
|
|
107
|
-
align.offences.map(&:message).
|
|
116
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
108
117
|
end
|
|
109
118
|
|
|
110
119
|
it 'can handle heredoc strings' do
|
|
@@ -115,7 +124,7 @@ module Rubocop
|
|
|
115
124
|
' end',
|
|
116
125
|
' EOS']
|
|
117
126
|
inspect_source(align, '', src)
|
|
118
|
-
align.offences.map(&:message).
|
|
127
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
119
128
|
end
|
|
120
129
|
|
|
121
130
|
it 'can handle a method call within a method call' do
|
|
@@ -124,13 +133,13 @@ module Rubocop
|
|
|
124
133
|
' b(b1,',
|
|
125
134
|
' b2),',
|
|
126
135
|
' a2)'])
|
|
127
|
-
align.offences.map(&:message).
|
|
136
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
128
137
|
end
|
|
129
138
|
|
|
130
139
|
it 'can handle a call embedded in a string' do
|
|
131
140
|
inspect_source(align, '',
|
|
132
141
|
['model("#{index(name)}", child)'])
|
|
133
|
-
align.offences.map(&:message).
|
|
142
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
134
143
|
end
|
|
135
144
|
|
|
136
145
|
it 'can handle do-end' do
|
|
@@ -138,7 +147,7 @@ module Rubocop
|
|
|
138
147
|
[' run(lambda do |e|',
|
|
139
148
|
" w = e['warden']",
|
|
140
149
|
' end)'])
|
|
141
|
-
align.offences.map(&:message).
|
|
150
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
142
151
|
end
|
|
143
152
|
|
|
144
153
|
it 'can handle a call with a block inside another call' do
|
|
@@ -147,19 +156,19 @@ module Rubocop
|
|
|
147
156
|
" col['name']",
|
|
148
157
|
' })']
|
|
149
158
|
inspect_source(align, '', src)
|
|
150
|
-
align.offences.map(&:message).
|
|
159
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
151
160
|
end
|
|
152
161
|
|
|
153
162
|
it 'can handle a ternary condition with a block reference' do
|
|
154
163
|
src = ['cond ? a : func(&b)']
|
|
155
164
|
inspect_source(align, '', src)
|
|
156
|
-
align.offences.map(&:message).
|
|
165
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
157
166
|
end
|
|
158
167
|
|
|
159
168
|
it 'can handle parentheses used with no parameters' do
|
|
160
169
|
src = ['func()']
|
|
161
170
|
inspect_source(align, '', src)
|
|
162
|
-
align.offences.map(&:message).
|
|
171
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
163
172
|
end
|
|
164
173
|
|
|
165
174
|
it 'can handle a multiline hash as first parameter' do
|
|
@@ -167,7 +176,7 @@ module Rubocop
|
|
|
167
176
|
' :space_before => "",',
|
|
168
177
|
'}, state)']
|
|
169
178
|
inspect_source(align, '', src)
|
|
170
|
-
align.offences.map(&:message).
|
|
179
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
171
180
|
end
|
|
172
181
|
|
|
173
182
|
it 'can handle a multiline hash as second parameter' do
|
|
@@ -175,26 +184,26 @@ module Rubocop
|
|
|
175
184
|
' :value => value',
|
|
176
185
|
'})']
|
|
177
186
|
inspect_source(align, '', src)
|
|
178
|
-
align.offences.map(&:message).
|
|
187
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
179
188
|
end
|
|
180
189
|
|
|
181
190
|
it 'can handle method calls without parentheses' do
|
|
182
191
|
src = ['a(b c, d)']
|
|
183
192
|
inspect_source(align, '', src)
|
|
184
|
-
align.offences.map(&:message).
|
|
193
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
185
194
|
end
|
|
186
195
|
|
|
187
196
|
it 'can handle other method calls without parentheses' do
|
|
188
197
|
src = ['chars(Unicode.apply_mapping @wrapped_string, :uppercase)']
|
|
189
198
|
inspect_source(align, '', src)
|
|
190
|
-
align.offences.map(&:message).
|
|
199
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
191
200
|
end
|
|
192
201
|
|
|
193
202
|
it "doesn't check alignment if tabs are used to indent" do
|
|
194
203
|
src = ['a(b,',
|
|
195
204
|
"\tc)"]
|
|
196
205
|
inspect_source(align, '', src)
|
|
197
|
-
align.offences.map(&:message).
|
|
206
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
198
207
|
end
|
|
199
208
|
end
|
|
200
209
|
end
|
|
@@ -5,7 +5,7 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe AmpersandsPipesVsAndOr do
|
|
8
|
-
let
|
|
8
|
+
let(:amp) { AmpersandsPipesVsAndOr.new }
|
|
9
9
|
|
|
10
10
|
it 'registers an offence for AND used in condition of if statement' do
|
|
11
11
|
check('if', 'and', '&&')
|
|
@@ -48,9 +48,9 @@ module Rubocop
|
|
|
48
48
|
'end'])
|
|
49
49
|
# Just one offence should be registered. The good_operator
|
|
50
50
|
# should be accepted.
|
|
51
|
-
amp.offences.map(&:message).
|
|
52
|
-
['Use &&/|| for boolean expressions, and/or for control flow.']
|
|
53
|
-
amp.offences[0].line_number.
|
|
51
|
+
expect(amp.offences.map(&:message)).to eq(
|
|
52
|
+
['Use &&/|| for boolean expressions, and/or for control flow.'])
|
|
53
|
+
expect(amp.offences[0].line_number).to eq(1)
|
|
54
54
|
end
|
|
55
55
|
end
|
|
56
56
|
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module Rubocop
|
|
6
|
+
module Cop
|
|
7
|
+
describe ArrayLiteral do
|
|
8
|
+
let(:a) { ArrayLiteral.new }
|
|
9
|
+
|
|
10
|
+
it 'registers an offence for Array.new()' do
|
|
11
|
+
inspect_source(a,
|
|
12
|
+
'file.rb',
|
|
13
|
+
['test = Array.new()'])
|
|
14
|
+
expect(a.offences.size).to eq(1)
|
|
15
|
+
expect(a.offences.map(&:message))
|
|
16
|
+
.to eq([ArrayLiteral::ERROR_MESSAGE])
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it 'registers an offence for Array.new'
|
|
20
|
+
|
|
21
|
+
it 'does not register an offence for Array.new(3)' do
|
|
22
|
+
inspect_source(a,
|
|
23
|
+
'file.rb',
|
|
24
|
+
['test = Array.new(3)'])
|
|
25
|
+
expect(a.offences).to be_empty
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module Rubocop
|
|
6
|
+
module Cop
|
|
7
|
+
describe AsciiIdentifiersAndComments do
|
|
8
|
+
let(:ascii) { AsciiIdentifiersAndComments.new }
|
|
9
|
+
|
|
10
|
+
it 'registers an offence for a variable name with non-ascii chars' do
|
|
11
|
+
inspect_source(ascii,
|
|
12
|
+
'file.rb',
|
|
13
|
+
['älg = 1'])
|
|
14
|
+
expect(ascii.offences.size).to eq(1)
|
|
15
|
+
expect(ascii.offences.map(&:message))
|
|
16
|
+
.to eq([AsciiIdentifiersAndComments::ERROR_MESSAGE])
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it 'registers an offence for a comment with non-ascii chars' do
|
|
20
|
+
inspect_source(ascii,
|
|
21
|
+
'file.rb',
|
|
22
|
+
['# 这是什么?'])
|
|
23
|
+
expect(ascii.offences.size).to eq(1)
|
|
24
|
+
expect(ascii.offences.map(&:message))
|
|
25
|
+
.to eq([AsciiIdentifiersAndComments::ERROR_MESSAGE])
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it 'accepts comments and identifiers with only ascii chars' do
|
|
29
|
+
inspect_source(ascii,
|
|
30
|
+
'file.rb',
|
|
31
|
+
['# AZaz1@$%~,;*_`|',
|
|
32
|
+
'x.empty?'])
|
|
33
|
+
expect(ascii.offences.size).to eq(0)
|
|
34
|
+
expect(ascii.offences.map(&:message)).to be_empty
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module Rubocop
|
|
6
|
+
module Cop
|
|
7
|
+
describe AvoidClassVars do
|
|
8
|
+
let(:acv) { AvoidClassVars.new }
|
|
9
|
+
|
|
10
|
+
it 'registers an offence for class variables' do
|
|
11
|
+
inspect_source(acv,
|
|
12
|
+
'file.rb',
|
|
13
|
+
['class TestClass; @@test = 10; end'])
|
|
14
|
+
expect(acv.offences.size).to eq(1)
|
|
15
|
+
expect(acv.offences.map(&:message))
|
|
16
|
+
.to eq(['Replace class var @@test with a class instance var.'])
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module Rubocop
|
|
6
|
+
module Cop
|
|
7
|
+
describe AvoidFor do
|
|
8
|
+
let(:af) { AvoidFor.new }
|
|
9
|
+
|
|
10
|
+
it 'registers an offence for for' do
|
|
11
|
+
inspect_source(af,
|
|
12
|
+
'file.rb',
|
|
13
|
+
['for n in [1, 2, 3] do',
|
|
14
|
+
'puts n',
|
|
15
|
+
'end'])
|
|
16
|
+
expect(af.offences.size).to eq(1)
|
|
17
|
+
expect(af.offences.map(&:message))
|
|
18
|
+
.to eq([AvoidFor::ERROR_MESSAGE])
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module Rubocop
|
|
6
|
+
module Cop
|
|
7
|
+
describe AvoidPerlBackrefs do
|
|
8
|
+
let(:ap) { AvoidPerlBackrefs.new }
|
|
9
|
+
|
|
10
|
+
it 'registers an offence for $1' do
|
|
11
|
+
inspect_source(ap, 'file.rb', ['puts $1'])
|
|
12
|
+
expect(ap.offences.size).to eq(1)
|
|
13
|
+
expect(ap.offences.map(&:message))
|
|
14
|
+
.to eq(['Prefer the use of MatchData over $1.'])
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module Rubocop
|
|
6
|
+
module Cop
|
|
7
|
+
describe AvoidPerlisms do
|
|
8
|
+
let(:ap) { AvoidPerlisms.new }
|
|
9
|
+
|
|
10
|
+
it 'registers an offence for $:' do
|
|
11
|
+
inspect_source(ap, 'file.rb', ['puts $:'])
|
|
12
|
+
expect(ap.offences.size).to eq(1)
|
|
13
|
+
expect(ap.offences.map(&:message))
|
|
14
|
+
.to eq(['Prefer $LOAD_PATH over $:.'])
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it 'registers an offence for $"' do
|
|
18
|
+
inspect_source(ap, 'file.rb', ['puts $"'])
|
|
19
|
+
expect(ap.offences.size).to eq(1)
|
|
20
|
+
expect(ap.offences.map(&:message))
|
|
21
|
+
.to eq(['Prefer $LOADED_FEATURES over $".'])
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it 'registers an offence for $0' do
|
|
25
|
+
inspect_source(ap, 'file.rb', ['puts $0'])
|
|
26
|
+
expect(ap.offences.size).to eq(1)
|
|
27
|
+
expect(ap.offences.map(&:message))
|
|
28
|
+
.to eq(['Prefer $PROGRAM_NAME over $0.'])
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it 'registers an offence for $$' do
|
|
32
|
+
inspect_source(ap, 'file.rb', ['puts $$'])
|
|
33
|
+
expect(ap.offences.size).to eq(1)
|
|
34
|
+
expect(ap.offences.map(&:message))
|
|
35
|
+
.to eq(['Prefer $PID or $PROCESS_ID from English library over $$.'])
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it 'does not register an offence for backrefs like $1' do
|
|
39
|
+
inspect_source(ap, 'file.rb', ['puts $1'])
|
|
40
|
+
expect(ap.offences).to be_empty
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module Rubocop
|
|
6
|
+
module Cop
|
|
7
|
+
describe BraceAfterPercent do
|
|
8
|
+
let(:bap) { BraceAfterPercent.new }
|
|
9
|
+
|
|
10
|
+
it 'registers an offence for %w[' do
|
|
11
|
+
inspect_source(bap,
|
|
12
|
+
'file.rb',
|
|
13
|
+
['puts %w[test top]'])
|
|
14
|
+
expect(bap.offences.size).to eq(1)
|
|
15
|
+
expect(bap.offences.map(&:message))
|
|
16
|
+
.to eq([BraceAfterPercent::ERROR_MESSAGE])
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it 'registers an offence for %w(' do
|
|
20
|
+
inspect_source(bap,
|
|
21
|
+
'file.rb',
|
|
22
|
+
['puts %w(test top)'])
|
|
23
|
+
expect(bap.offences).to be_empty
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module Rubocop
|
|
6
|
+
module Cop
|
|
7
|
+
describe ClassAndModuleCamelCase do
|
|
8
|
+
let(:camel_case) { ClassAndModuleCamelCase.new }
|
|
9
|
+
|
|
10
|
+
it 'registers an offence for underscore in class and module name' do
|
|
11
|
+
inspect_source(camel_case, 'file.rb',
|
|
12
|
+
['class My_Class',
|
|
13
|
+
'end',
|
|
14
|
+
'',
|
|
15
|
+
'module My_Module',
|
|
16
|
+
'end',
|
|
17
|
+
])
|
|
18
|
+
expect(camel_case.offences.map(&:message)).to eq(
|
|
19
|
+
['Use CamelCase for classes and modules.'] * 2)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it 'accepts CamelCase names' do
|
|
23
|
+
inspect_source(camel_case, 'file.rb',
|
|
24
|
+
['class MyClass',
|
|
25
|
+
'end',
|
|
26
|
+
'',
|
|
27
|
+
'module Mine',
|
|
28
|
+
'end',
|
|
29
|
+
])
|
|
30
|
+
expect(camel_case.offences.map(&:message)).to be_empty
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module Rubocop
|
|
6
|
+
module Cop
|
|
7
|
+
describe CollectionMethods do
|
|
8
|
+
let(:cm) { CollectionMethods.new }
|
|
9
|
+
|
|
10
|
+
it 'registers an offence for collect' do
|
|
11
|
+
inspect_source(cm, 'file.rb', ['[1, 2, 3].collect { |e| e + 1 }'])
|
|
12
|
+
expect(cm.offences.size).to eq(1)
|
|
13
|
+
expect(cm.offences.map(&:message))
|
|
14
|
+
.to eq(['Prefer map over collect.'])
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it 'registers an offence for inject' do
|
|
18
|
+
inspect_source(cm, 'file.rb', ['[1, 2, 3].inject { |e| e + 1 }'])
|
|
19
|
+
expect(cm.offences.size).to eq(1)
|
|
20
|
+
expect(cm.offences.map(&:message))
|
|
21
|
+
.to eq(['Prefer reduce over inject.'])
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it 'registers an offence for detect' do
|
|
25
|
+
inspect_source(cm, 'file.rb', ['[1, 2, 3].detect { |e| e + 1 }'])
|
|
26
|
+
expect(cm.offences.size).to eq(1)
|
|
27
|
+
expect(cm.offences.map(&:message))
|
|
28
|
+
.to eq(['Prefer find over detect.'])
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it 'registers an offence for find_all' do
|
|
32
|
+
inspect_source(cm, 'file.rb', ['[1, 2, 3].find_all { |e| e + 1 }'])
|
|
33
|
+
expect(cm.offences.size).to eq(1)
|
|
34
|
+
expect(cm.offences.map(&:message))
|
|
35
|
+
.to eq(['Prefer select over find_all.'])
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -5,26 +5,26 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe Cop do
|
|
8
|
-
let
|
|
8
|
+
let(:cop) { Cop.new }
|
|
9
9
|
|
|
10
10
|
it 'initially has 0 offences' do
|
|
11
|
-
cop.offences.
|
|
11
|
+
expect(cop.offences).to be_empty
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
it 'initially has nothing to report' do
|
|
15
|
-
cop.has_report
|
|
15
|
+
expect(cop.has_report?).to be_false
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
it 'keeps track of offences' do
|
|
19
19
|
cop.add_offence(:convention, 1, 'message')
|
|
20
20
|
|
|
21
|
-
cop.offences.size.
|
|
21
|
+
expect(cop.offences.size).to eq(1)
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
it 'will report registered offences' do
|
|
25
25
|
cop.add_offence(:convention, 1, 'message')
|
|
26
26
|
|
|
27
|
-
cop.has_report
|
|
27
|
+
expect(cop.has_report?).to be_true
|
|
28
28
|
end
|
|
29
29
|
end
|
|
30
30
|
end
|
|
@@ -5,28 +5,28 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe DefWithParentheses do
|
|
8
|
-
let
|
|
8
|
+
let(:def_par) { DefWithParentheses.new }
|
|
9
9
|
|
|
10
10
|
it 'reports an offence for def with empty parens' do
|
|
11
11
|
src = ['def func()',
|
|
12
12
|
'end']
|
|
13
13
|
inspect_source(def_par, '', src)
|
|
14
|
-
def_par.offences.map(&:message).
|
|
14
|
+
expect(def_par.offences.map(&:message)).to eq(
|
|
15
15
|
["Omit the parentheses in defs when the method doesn't accept any " +
|
|
16
|
-
'arguments.']
|
|
16
|
+
'arguments.'])
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
it 'accepts def with arg and parens' do
|
|
20
20
|
src = ['def func(a)',
|
|
21
21
|
'end']
|
|
22
22
|
inspect_source(def_par, '', src)
|
|
23
|
-
def_par.offences.map(&:message).
|
|
23
|
+
expect(def_par.offences.map(&:message)).to be_empty
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
it 'accepts empty parentheses in one liners' do
|
|
27
27
|
src = ["def to_s() join '/' end"]
|
|
28
28
|
inspect_source(def_par, '', src)
|
|
29
|
-
def_par.offences.map(&:message).
|
|
29
|
+
expect(def_par.offences.map(&:message)).to be_empty
|
|
30
30
|
end
|
|
31
31
|
end
|
|
32
32
|
end
|
|
@@ -5,21 +5,21 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe DefWithoutParentheses do
|
|
8
|
-
let
|
|
8
|
+
let(:def_par) { DefWithoutParentheses.new }
|
|
9
9
|
|
|
10
10
|
it 'reports an offence for def with parameters but no parens' do
|
|
11
11
|
src = ['def func a, b',
|
|
12
12
|
'end']
|
|
13
13
|
inspect_source(def_par, '', src)
|
|
14
|
-
def_par.offences.map(&:message).
|
|
15
|
-
['Use def with parentheses when there are arguments.']
|
|
14
|
+
expect(def_par.offences.map(&:message)).to eq(
|
|
15
|
+
['Use def with parentheses when there are arguments.'])
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
it 'accepts def with no args and no parens' do
|
|
19
19
|
src = ['def func',
|
|
20
20
|
'end']
|
|
21
21
|
inspect_source(def_par, '', src)
|
|
22
|
-
def_par.offences.map(&:message).
|
|
22
|
+
expect(def_par.offences.map(&:message)).to be_empty
|
|
23
23
|
end
|
|
24
24
|
end
|
|
25
25
|
end
|