rubocop 0.3.0 → 0.4.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 +40 -0
- data/.travis.yml +1 -0
- data/Gemfile +7 -10
- data/Gemfile.lock +21 -19
- data/README.md +62 -5
- data/VERSION +1 -1
- data/bin/rubocop +1 -1
- data/lib/rubocop/cli.rb +35 -15
- 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 +1 -1
- data/lib/rubocop/cop/def_parentheses.rb +4 -1
- data/lib/rubocop/cop/encoding.rb +6 -4
- data/lib/rubocop/cop/end_of_line.rb +1 -3
- data/lib/rubocop/cop/favor_modifier.rb +82 -0
- data/lib/rubocop/cop/favor_unless_over_negated_if.rb +47 -0
- data/lib/rubocop/cop/grammar.rb +3 -2
- 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/new_lambda_literal.rb +17 -0
- data/lib/rubocop/cop/parentheses_around_condition.rb +24 -0
- data/lib/rubocop/cop/surrounding_space.rb +48 -47
- data/lib/rubocop/cop/syntax.rb +19 -0
- data/lib/rubocop/cop/tab.rb +1 -3
- data/lib/rubocop/report/plain_text.rb +1 -1
- data/lib/rubocop/report/report.rb +5 -2
- data/lib/rubocop/version.rb +1 -1
- data/lib/rubocop.rb +14 -0
- data/rubocop.gemspec +38 -22
- data/spec/rubocop/cli_spec.rb +120 -40
- data/spec/rubocop/cops/align_parameters_spec.rb +29 -29
- data/spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb +4 -4
- 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/favor_modifier_spec.rb +112 -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 +14 -9
- 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/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 +4 -4
- data/spec/rubocop/cops/one_line_conditional_spec.rb +2 -3
- 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/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 +50 -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 +5 -5
- data/spec/rubocop/cops/syntax_spec.rb +18 -0
- data/spec/rubocop/cops/tab_spec.rb +3 -3
- 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/when_then_spec.rb +5 -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 +57 -28
- data/.rbenv-version +0 -1
- data/.rvmrc +0 -1
- data/features/rubocop.feature +0 -9
- data/features/step_definitions/rubocop_steps.rb +0 -1
- data/features/support/env.rb +0 -15
data/spec/rubocop/cli_spec.rb
CHANGED
|
@@ -10,24 +10,33 @@ module Rubocop
|
|
|
10
10
|
after(:each) { $stdout = STDOUT }
|
|
11
11
|
|
|
12
12
|
it 'exits cleanly when -h is used' do
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
expect { cli.run ['-h'] }.to exit_with_code(0)
|
|
14
|
+
expect { cli.run ['--help'] }.to exit_with_code(0)
|
|
15
15
|
message = ['Usage: rubocop [options] [file1, file2, ...]',
|
|
16
|
-
' -
|
|
16
|
+
' -d, --[no-]debug Display debug info',
|
|
17
17
|
' -e, --emacs Emacs style output',
|
|
18
18
|
' -c, --config FILE Configuration file',
|
|
19
|
-
' -s, --silent Silence summary'
|
|
20
|
-
|
|
19
|
+
' -s, --silent Silence summary',
|
|
20
|
+
' -v, --version Display version']
|
|
21
|
+
expect($stdout.string).to eq((message * 2).join("\n") + "\n")
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it 'exits cleanly when -v is used' do
|
|
25
|
+
expect { cli.run ['-v'] }.to exit_with_code(0)
|
|
26
|
+
expect { cli.run ['--version'] }.to exit_with_code(0)
|
|
27
|
+
expect($stdout.string).to eq((Rubocop::VERSION + "\n") * 2)
|
|
21
28
|
end
|
|
22
29
|
|
|
23
30
|
it 'checks a given correct file and returns 0' do
|
|
24
31
|
File.open('example.rb', 'w') do |f|
|
|
25
32
|
f.puts '# encoding: utf-8'
|
|
26
33
|
f.puts 'x = 0'
|
|
34
|
+
f.puts 'puts x'
|
|
27
35
|
end
|
|
28
36
|
begin
|
|
29
|
-
cli.run(['example.rb']).
|
|
30
|
-
$stdout.string.
|
|
37
|
+
expect(cli.run(['example.rb'])).to eq(0)
|
|
38
|
+
expect($stdout.string.uncolored)
|
|
39
|
+
.to eq("\n1 files inspected, 0 offences detected\n")
|
|
31
40
|
ensure
|
|
32
41
|
File.delete 'example.rb'
|
|
33
42
|
end
|
|
@@ -37,52 +46,95 @@ module Rubocop
|
|
|
37
46
|
File.open('example.rb', 'w') do |f|
|
|
38
47
|
f.puts '# encoding: utf-8'
|
|
39
48
|
f.puts 'x = 0 '
|
|
49
|
+
f.puts 'puts x'
|
|
40
50
|
end
|
|
41
51
|
begin
|
|
42
|
-
cli.run(['example.rb']).
|
|
43
|
-
$stdout.string.
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
52
|
+
expect(cli.run(['example.rb'])).to eq(1)
|
|
53
|
+
expect($stdout.string.uncolored)
|
|
54
|
+
.to eq ['== example.rb ==',
|
|
55
|
+
'C: 2: Trailing whitespace detected.',
|
|
56
|
+
'',
|
|
57
|
+
'1 files inspected, 1 offences detected',
|
|
58
|
+
''].join("\n")
|
|
48
59
|
ensure
|
|
49
60
|
File.delete 'example.rb'
|
|
50
61
|
end
|
|
51
62
|
end
|
|
52
63
|
|
|
53
|
-
it 'can report in emacs style' do
|
|
54
|
-
File.open('example1.rb', 'w') { |f| f.puts 'x
|
|
55
|
-
File.open('example2.rb', 'w') { |f| f.puts "\tx = 0" }
|
|
64
|
+
it 'can report in emacs style', ruby: 1.9 do
|
|
65
|
+
File.open('example1.rb', 'w') { |f| f.puts 'x= 0 ', 'y ', 'puts x' }
|
|
66
|
+
File.open('example2.rb', 'w') { |f| f.puts "\tx = 0", 'puts x' }
|
|
56
67
|
begin
|
|
57
|
-
cli.run(['--emacs', 'example1.rb', 'example2.rb']).
|
|
58
|
-
$stdout.string.
|
|
68
|
+
expect(cli.run(['--emacs', 'example1.rb', 'example2.rb'])).to eq(1)
|
|
69
|
+
expect($stdout.string.uncolored)
|
|
70
|
+
.to eq(
|
|
59
71
|
['example1.rb:1: C: Missing encoding comment.',
|
|
60
72
|
'example1.rb:1: C: Trailing whitespace detected.',
|
|
73
|
+
"example1.rb:1: C: Surrounding space missing for operator '='.",
|
|
74
|
+
'example1.rb:2: C: Trailing whitespace detected.',
|
|
61
75
|
'example2.rb:1: C: Missing encoding comment.',
|
|
62
76
|
'example2.rb:1: C: Tab detected.',
|
|
63
77
|
'',
|
|
78
|
+
'2 files inspected, 6 offences detected',
|
|
79
|
+
''].join("\n"))
|
|
80
|
+
ensure
|
|
81
|
+
File.delete 'example1.rb'
|
|
82
|
+
File.delete 'example2.rb'
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
it 'can report in emacs style', ruby: 2.0 do
|
|
87
|
+
File.open('example1.rb', 'w') { |f| f.puts 'x= 0 ', 'y ', 'puts x' }
|
|
88
|
+
File.open('example2.rb', 'w') { |f| f.puts "\tx = 0", 'puts x' }
|
|
89
|
+
begin
|
|
90
|
+
expect(cli.run(['--emacs', 'example1.rb', 'example2.rb'])).to eq(1)
|
|
91
|
+
expect($stdout.string.uncolored)
|
|
92
|
+
.to eq(
|
|
93
|
+
['example1.rb:1: C: Trailing whitespace detected.',
|
|
94
|
+
"example1.rb:1: C: Surrounding space missing for operator '='.",
|
|
95
|
+
'example1.rb:2: C: Trailing whitespace detected.',
|
|
96
|
+
'example2.rb:1: C: Tab detected.',
|
|
97
|
+
'',
|
|
64
98
|
'2 files inspected, 4 offences detected',
|
|
65
|
-
''].join("\n")
|
|
99
|
+
''].join("\n"))
|
|
66
100
|
ensure
|
|
67
101
|
File.delete 'example1.rb'
|
|
68
102
|
File.delete 'example2.rb'
|
|
69
103
|
end
|
|
70
104
|
end
|
|
71
105
|
|
|
72
|
-
it 'ommits summary when --silent passed' do
|
|
73
|
-
File.open('example1.rb', 'w') { |f| f.puts '
|
|
74
|
-
File.open('example2.rb', 'w') { |f| f.puts "\
|
|
106
|
+
it 'ommits summary when --silent passed', ruby: 1.9 do
|
|
107
|
+
File.open('example1.rb', 'w') { |f| f.puts 'puts 0 ' }
|
|
108
|
+
File.open('example2.rb', 'w') { |f| f.puts "\tputs 0" }
|
|
75
109
|
begin
|
|
76
|
-
cli.run(['--emacs',
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
$stdout.string.
|
|
110
|
+
expect(cli.run(['--emacs',
|
|
111
|
+
'--silent',
|
|
112
|
+
'example1.rb',
|
|
113
|
+
'example2.rb'])).to eq(1)
|
|
114
|
+
expect($stdout.string).to eq(
|
|
81
115
|
['example1.rb:1: C: Missing encoding comment.',
|
|
82
116
|
'example1.rb:1: C: Trailing whitespace detected.',
|
|
83
117
|
'example2.rb:1: C: Missing encoding comment.',
|
|
84
118
|
'example2.rb:1: C: Tab detected.',
|
|
85
|
-
''].join("\n")
|
|
119
|
+
''].join("\n"))
|
|
120
|
+
ensure
|
|
121
|
+
File.delete 'example1.rb'
|
|
122
|
+
File.delete 'example2.rb'
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
it 'ommits summary when --silent passed', ruby: 2.0 do
|
|
127
|
+
File.open('example1.rb', 'w') { |f| f.puts 'puts 0 ' }
|
|
128
|
+
File.open('example2.rb', 'w') { |f| f.puts "\tputs 0" }
|
|
129
|
+
begin
|
|
130
|
+
expect(cli.run(['--emacs',
|
|
131
|
+
'--silent',
|
|
132
|
+
'example1.rb',
|
|
133
|
+
'example2.rb'])).to eq(1)
|
|
134
|
+
expect($stdout.string).to eq(
|
|
135
|
+
['example1.rb:1: C: Trailing whitespace detected.',
|
|
136
|
+
'example2.rb:1: C: Tab detected.',
|
|
137
|
+
''].join("\n"))
|
|
86
138
|
ensure
|
|
87
139
|
File.delete 'example1.rb'
|
|
88
140
|
File.delete 'example2.rb'
|
|
@@ -90,7 +142,7 @@ module Rubocop
|
|
|
90
142
|
end
|
|
91
143
|
|
|
92
144
|
it 'can be configured with option to disable a certain error' do
|
|
93
|
-
File.open('example1.rb', 'w') { |f| f.puts '
|
|
145
|
+
File.open('example1.rb', 'w') { |f| f.puts 'puts 0 ' }
|
|
94
146
|
File.open('rubocop.yml', 'w') do |f|
|
|
95
147
|
f.puts('Encoding:',
|
|
96
148
|
' Enabled: false',
|
|
@@ -99,14 +151,13 @@ module Rubocop
|
|
|
99
151
|
' Enabled: false')
|
|
100
152
|
end
|
|
101
153
|
begin
|
|
102
|
-
|
|
103
|
-
$stdout.string.
|
|
154
|
+
expect(cli.run(['-c', 'rubocop.yml', 'example1.rb'])).to eq(1)
|
|
155
|
+
expect($stdout.string.uncolored).to eq(
|
|
104
156
|
['== example1.rb ==',
|
|
105
157
|
'C: 1: Trailing whitespace detected.',
|
|
106
158
|
'',
|
|
107
159
|
'1 files inspected, 1 offences detected',
|
|
108
|
-
''].join("\n")
|
|
109
|
-
return_code.should == 1
|
|
160
|
+
''].join("\n"))
|
|
110
161
|
ensure
|
|
111
162
|
File.delete 'example1.rb'
|
|
112
163
|
File.delete 'rubocop.yml'
|
|
@@ -115,7 +166,7 @@ module Rubocop
|
|
|
115
166
|
|
|
116
167
|
it 'can be configured with project config to disable a certain error' do
|
|
117
168
|
FileUtils.mkdir 'example_src'
|
|
118
|
-
File.open('example_src/example1.rb', 'w') { |f| f.puts '
|
|
169
|
+
File.open('example_src/example1.rb', 'w') { |f| f.puts 'puts 0 ' }
|
|
119
170
|
File.open('example_src/.rubocop.yml', 'w') do |f|
|
|
120
171
|
f.puts('Encoding:',
|
|
121
172
|
' Enabled: false',
|
|
@@ -124,14 +175,34 @@ module Rubocop
|
|
|
124
175
|
' Enabled: false')
|
|
125
176
|
end
|
|
126
177
|
begin
|
|
127
|
-
|
|
128
|
-
$stdout.string.
|
|
178
|
+
expect(cli.run(['example_src/example1.rb'])).to eq(1)
|
|
179
|
+
expect($stdout.string.uncolored).to eq(
|
|
129
180
|
['== example_src/example1.rb ==',
|
|
130
181
|
'C: 1: Trailing whitespace detected.',
|
|
131
182
|
'',
|
|
132
183
|
'1 files inspected, 1 offences detected',
|
|
133
|
-
''].join("\n")
|
|
134
|
-
|
|
184
|
+
''].join("\n"))
|
|
185
|
+
ensure
|
|
186
|
+
FileUtils.rm_rf 'example_src'
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
it 'can use an alternative max line length from a config file' do
|
|
191
|
+
FileUtils.mkdir 'example_src'
|
|
192
|
+
File.open('example_src/example1.rb', 'w') do |f|
|
|
193
|
+
f.puts '# encoding: utf-8'
|
|
194
|
+
f.puts '#' * 90
|
|
195
|
+
end
|
|
196
|
+
File.open('example_src/.rubocop.yml', 'w') do |f|
|
|
197
|
+
f.puts('LineLength:',
|
|
198
|
+
' Enabled: true',
|
|
199
|
+
' Max: 100')
|
|
200
|
+
end
|
|
201
|
+
begin
|
|
202
|
+
expect(cli.run(['example_src/example1.rb'])).to eq(0)
|
|
203
|
+
expect($stdout.string.uncolored).to eq(
|
|
204
|
+
['', '1 files inspected, 0 offences detected',
|
|
205
|
+
''].join("\n"))
|
|
135
206
|
ensure
|
|
136
207
|
FileUtils.rm_rf 'example_src'
|
|
137
208
|
end
|
|
@@ -139,7 +210,9 @@ module Rubocop
|
|
|
139
210
|
|
|
140
211
|
it 'finds no violations when checking the rubocop source code' do
|
|
141
212
|
cli.run
|
|
142
|
-
$stdout.string.
|
|
213
|
+
expect($stdout.string.uncolored).to match(
|
|
214
|
+
/files inspected, 0 offences detected\n/
|
|
215
|
+
)
|
|
143
216
|
end
|
|
144
217
|
|
|
145
218
|
it 'can process a file with an invalide UTF-8 byte sequence' do
|
|
@@ -148,11 +221,18 @@ module Rubocop
|
|
|
148
221
|
f.puts "# \xf9\x29"
|
|
149
222
|
end
|
|
150
223
|
begin
|
|
151
|
-
cli.run(['--emacs', 'example.rb']).
|
|
224
|
+
expect(cli.run(['--emacs', 'example.rb'])).to eq(0)
|
|
152
225
|
ensure
|
|
153
226
|
File.delete 'example.rb'
|
|
154
227
|
end
|
|
155
228
|
end
|
|
229
|
+
|
|
230
|
+
it 'has configuration for all cops in .rubocop.yml' do
|
|
231
|
+
cop_names = Cop::Cop.all.map do |cop_class|
|
|
232
|
+
cop_class.name.split('::').last
|
|
233
|
+
end
|
|
234
|
+
expect(YAML.load_file('.rubocop.yml').keys.sort).to eq(cop_names.sort)
|
|
235
|
+
end
|
|
156
236
|
end
|
|
157
237
|
end
|
|
158
238
|
end
|
|
@@ -5,22 +5,22 @@ 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
12
|
' if b then c else d end)'])
|
|
13
|
-
align.offences.map(&:message).
|
|
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
20
|
' if b then c else d end)'])
|
|
21
|
-
align.offences.map(&:message).
|
|
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
|
|
@@ -28,19 +28,19 @@ module Rubocop
|
|
|
28
28
|
' 0, 1,',
|
|
29
29
|
' (x + y),',
|
|
30
30
|
' if b then c else d end)'])
|
|
31
|
-
align.offences.map(&:message).
|
|
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,49 @@ 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
59
|
end
|
|
60
60
|
|
|
61
61
|
it 'can handle a correctly aligned string literal as first argument' do
|
|
62
62
|
inspect_source(align, '',
|
|
63
63
|
['add_offence(:convention, x,',
|
|
64
64
|
' a)'])
|
|
65
|
-
align.offences.map(&:message).
|
|
65
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
66
66
|
end
|
|
67
67
|
|
|
68
68
|
it 'can handle a string literal as other argument' do
|
|
69
69
|
inspect_source(align, '',
|
|
70
70
|
['add_offence(:convention,',
|
|
71
71
|
' "", a)'])
|
|
72
|
-
align.offences.map(&:message).
|
|
72
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
73
73
|
end
|
|
74
74
|
|
|
75
75
|
it "doesn't get confused by a line break inside a parameter" do
|
|
76
76
|
inspect_source(align, '',
|
|
77
77
|
['read(path, { headers: true,',
|
|
78
78
|
' converters: :numeric })'])
|
|
79
|
-
align.offences.map(&:message).
|
|
79
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
80
80
|
end
|
|
81
81
|
|
|
82
82
|
it "doesn't get confused by symbols with embedded expressions" do
|
|
83
83
|
inspect_source(align, '',
|
|
84
84
|
['send(:"#{name}_comments_path")'])
|
|
85
|
-
align.offences.map(&:message).
|
|
85
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
86
86
|
end
|
|
87
87
|
|
|
88
88
|
it "doesn't get confused by regexen with embedded expressions" do
|
|
89
89
|
inspect_source(align, '',
|
|
90
90
|
['a(/#{name}/)'])
|
|
91
|
-
align.offences.map(&:message).
|
|
91
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
92
92
|
end
|
|
93
93
|
|
|
94
94
|
it 'accepts braceless hashes' do
|
|
95
95
|
inspect_source(align, '',
|
|
96
96
|
['run(collection, :entry_name => label,',
|
|
97
97
|
' :paginator => paginator)'])
|
|
98
|
-
align.offences.map(&:message).
|
|
98
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
99
99
|
end
|
|
100
100
|
|
|
101
101
|
it 'accepts the first parameter being on a new row' do
|
|
@@ -104,7 +104,7 @@ module Rubocop
|
|
|
104
104
|
' a,',
|
|
105
105
|
' b',
|
|
106
106
|
' )'])
|
|
107
|
-
align.offences.map(&:message).
|
|
107
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
108
108
|
end
|
|
109
109
|
|
|
110
110
|
it 'can handle heredoc strings' do
|
|
@@ -115,7 +115,7 @@ module Rubocop
|
|
|
115
115
|
' end',
|
|
116
116
|
' EOS']
|
|
117
117
|
inspect_source(align, '', src)
|
|
118
|
-
align.offences.map(&:message).
|
|
118
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
119
119
|
end
|
|
120
120
|
|
|
121
121
|
it 'can handle a method call within a method call' do
|
|
@@ -124,13 +124,13 @@ module Rubocop
|
|
|
124
124
|
' b(b1,',
|
|
125
125
|
' b2),',
|
|
126
126
|
' a2)'])
|
|
127
|
-
align.offences.map(&:message).
|
|
127
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
128
128
|
end
|
|
129
129
|
|
|
130
130
|
it 'can handle a call embedded in a string' do
|
|
131
131
|
inspect_source(align, '',
|
|
132
132
|
['model("#{index(name)}", child)'])
|
|
133
|
-
align.offences.map(&:message).
|
|
133
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
134
134
|
end
|
|
135
135
|
|
|
136
136
|
it 'can handle do-end' do
|
|
@@ -138,7 +138,7 @@ module Rubocop
|
|
|
138
138
|
[' run(lambda do |e|',
|
|
139
139
|
" w = e['warden']",
|
|
140
140
|
' end)'])
|
|
141
|
-
align.offences.map(&:message).
|
|
141
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
142
142
|
end
|
|
143
143
|
|
|
144
144
|
it 'can handle a call with a block inside another call' do
|
|
@@ -147,19 +147,19 @@ module Rubocop
|
|
|
147
147
|
" col['name']",
|
|
148
148
|
' })']
|
|
149
149
|
inspect_source(align, '', src)
|
|
150
|
-
align.offences.map(&:message).
|
|
150
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
151
151
|
end
|
|
152
152
|
|
|
153
153
|
it 'can handle a ternary condition with a block reference' do
|
|
154
154
|
src = ['cond ? a : func(&b)']
|
|
155
155
|
inspect_source(align, '', src)
|
|
156
|
-
align.offences.map(&:message).
|
|
156
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
157
157
|
end
|
|
158
158
|
|
|
159
159
|
it 'can handle parentheses used with no parameters' do
|
|
160
160
|
src = ['func()']
|
|
161
161
|
inspect_source(align, '', src)
|
|
162
|
-
align.offences.map(&:message).
|
|
162
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
163
163
|
end
|
|
164
164
|
|
|
165
165
|
it 'can handle a multiline hash as first parameter' do
|
|
@@ -167,7 +167,7 @@ module Rubocop
|
|
|
167
167
|
' :space_before => "",',
|
|
168
168
|
'}, state)']
|
|
169
169
|
inspect_source(align, '', src)
|
|
170
|
-
align.offences.map(&:message).
|
|
170
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
171
171
|
end
|
|
172
172
|
|
|
173
173
|
it 'can handle a multiline hash as second parameter' do
|
|
@@ -175,26 +175,26 @@ module Rubocop
|
|
|
175
175
|
' :value => value',
|
|
176
176
|
'})']
|
|
177
177
|
inspect_source(align, '', src)
|
|
178
|
-
align.offences.map(&:message).
|
|
178
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
179
179
|
end
|
|
180
180
|
|
|
181
181
|
it 'can handle method calls without parentheses' do
|
|
182
182
|
src = ['a(b c, d)']
|
|
183
183
|
inspect_source(align, '', src)
|
|
184
|
-
align.offences.map(&:message).
|
|
184
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
185
185
|
end
|
|
186
186
|
|
|
187
187
|
it 'can handle other method calls without parentheses' do
|
|
188
188
|
src = ['chars(Unicode.apply_mapping @wrapped_string, :uppercase)']
|
|
189
189
|
inspect_source(align, '', src)
|
|
190
|
-
align.offences.map(&:message).
|
|
190
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
191
191
|
end
|
|
192
192
|
|
|
193
193
|
it "doesn't check alignment if tabs are used to indent" do
|
|
194
194
|
src = ['a(b,',
|
|
195
195
|
"\tc)"]
|
|
196
196
|
inspect_source(align, '', src)
|
|
197
|
-
align.offences.map(&:message).
|
|
197
|
+
expect(align.offences.map(&:message)).to be_empty
|
|
198
198
|
end
|
|
199
199
|
end
|
|
200
200
|
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,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
|