rubocop 0.5.0 → 0.6.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/.rubocop.yml +59 -1
- data/CHANGELOG.md +38 -0
- data/Gemfile +1 -10
- data/README.md +28 -1
- data/Rakefile +1 -15
- data/lib/rubocop.rb +14 -0
- data/lib/rubocop/cli.rb +70 -7
- data/lib/rubocop/cop/alias.rb +5 -8
- 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_perlisms.rb +19 -28
- data/lib/rubocop/cop/brace_after_percent.rb +28 -0
- data/lib/rubocop/cop/cop.rb +15 -3
- data/lib/rubocop/cop/encoding.rb +1 -1
- data/lib/rubocop/cop/ensure_return.rb +36 -0
- data/lib/rubocop/cop/favor_percent_r.rb +19 -0
- data/lib/rubocop/cop/favor_sprintf.rb +2 -10
- data/lib/rubocop/cop/grammar.rb +6 -3
- data/lib/rubocop/cop/handle_exceptions.rb +21 -0
- data/lib/rubocop/cop/hash_literal.rb +22 -0
- data/lib/rubocop/cop/method_length.rb +66 -0
- data/lib/rubocop/cop/op_method.rb +23 -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/symbol_snake_case.rb +5 -5
- data/lib/rubocop/cop/syntax.rb +13 -2
- data/lib/rubocop/version.rb +3 -1
- data/rubocop.gemspec +36 -169
- data/spec/rubocop/cli_spec.rb +146 -15
- data/spec/rubocop/cops/alias_spec.rb +10 -1
- 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_perlisms_spec.rb +12 -0
- data/spec/rubocop/cops/brace_after_percent_spec.rb +27 -0
- data/spec/rubocop/cops/encoding_spec.rb +2 -2
- data/spec/rubocop/cops/ensure_return_spec.rb +37 -0
- data/spec/rubocop/cops/favor_percent_r.rb +29 -0
- data/spec/rubocop/cops/favor_sprintf_spec.rb +8 -1
- data/spec/rubocop/cops/grammar_spec.rb +54 -40
- data/spec/rubocop/cops/handle_exceptions_spec.rb +36 -0
- data/spec/rubocop/cops/hash_literal_spec.rb +29 -0
- data/spec/rubocop/cops/method_length_spec.rb +150 -0
- data/spec/rubocop/cops/op_method_spec.rb +58 -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/space_around_operators_spec.rb +7 -0
- data/spec/rubocop/cops/symbol_snake_case_spec.rb +19 -7
- data/spec/rubocop/cops/tab_spec.rb +1 -1
- metadata +131 -50
- data/Gemfile.lock +0 -41
- data/VERSION +0 -1
@@ -7,7 +7,7 @@ module Rubocop
|
|
7
7
|
describe Alias do
|
8
8
|
let(:a) { Alias.new }
|
9
9
|
|
10
|
-
it 'registers an offence for alias' do
|
10
|
+
it 'registers an offence for alias with symbol args' do
|
11
11
|
inspect_source(a,
|
12
12
|
'file.rb',
|
13
13
|
['alias :ala :bala'])
|
@@ -16,6 +16,15 @@ module Rubocop
|
|
16
16
|
.to eq([Alias::ERROR_MESSAGE])
|
17
17
|
end
|
18
18
|
|
19
|
+
it 'registers an offence for alias with bareword args' do
|
20
|
+
inspect_source(a,
|
21
|
+
'file.rb',
|
22
|
+
['alias ala bala'])
|
23
|
+
expect(a.offences.size).to eq(1)
|
24
|
+
expect(a.offences.map(&:message))
|
25
|
+
.to eq([Alias::ERROR_MESSAGE])
|
26
|
+
end
|
27
|
+
|
19
28
|
it 'does not register an offence for alias_method' do
|
20
29
|
inspect_source(a,
|
21
30
|
'file.rb',
|
@@ -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
|
@@ -27,6 +27,18 @@ module Rubocop
|
|
27
27
|
expect(ap.offences.map(&:message))
|
28
28
|
.to eq(['Prefer $PROGRAM_NAME over $0.'])
|
29
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
|
30
42
|
end
|
31
43
|
end
|
32
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
|
@@ -11,7 +11,7 @@ module Rubocop
|
|
11
11
|
inspect_source(encoding, 'file.rb', ['def foo() end'])
|
12
12
|
|
13
13
|
expect(encoding.offences.map(&:message)).to eq(
|
14
|
-
['Missing encoding comment.'])
|
14
|
+
['Missing utf-8 encoding comment.'])
|
15
15
|
end
|
16
16
|
|
17
17
|
it 'accepts encoding on first line', ruby: 1.9 do
|
@@ -34,7 +34,7 @@ module Rubocop
|
|
34
34
|
'# encoding: utf-8'])
|
35
35
|
|
36
36
|
expect(encoding.offences.map(&:message)).to eq(
|
37
|
-
['Missing encoding comment.'])
|
37
|
+
['Missing utf-8 encoding comment.'])
|
38
38
|
end
|
39
39
|
|
40
40
|
it 'does not register an offence on Ruby 2.0', ruby: 2.0 do
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Rubocop
|
6
|
+
module Cop
|
7
|
+
describe EnsureReturn do
|
8
|
+
let(:er) { EnsureReturn.new }
|
9
|
+
|
10
|
+
it 'registers an offence for return in ensure' do
|
11
|
+
inspect_source(er,
|
12
|
+
'file.rb',
|
13
|
+
['begin',
|
14
|
+
' something',
|
15
|
+
'ensure',
|
16
|
+
' file.close',
|
17
|
+
' return',
|
18
|
+
'end'])
|
19
|
+
expect(er.offences.size).to eq(1)
|
20
|
+
expect(er.offences.map(&:message))
|
21
|
+
.to eq([EnsureReturn::ERROR_MESSAGE])
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'does not register an offence for return outside ensure' do
|
25
|
+
inspect_source(er,
|
26
|
+
'file.rb',
|
27
|
+
['begin',
|
28
|
+
' something',
|
29
|
+
' return',
|
30
|
+
'ensure',
|
31
|
+
' file.close',
|
32
|
+
'end'])
|
33
|
+
expect(er.offences).to be_empty
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Rubocop
|
6
|
+
module Cop
|
7
|
+
describe FavorPercentR do
|
8
|
+
let(:fpr) { FavorPercentR.new }
|
9
|
+
|
10
|
+
it 'registers an offence for // with two slashes in regexp' do
|
11
|
+
inspect_source(fpr, 'file.rb', ['x =~ /home\/\//',
|
12
|
+
'y =~ /etc\/top\//'])
|
13
|
+
expect(fpr.offences.map(&:message))
|
14
|
+
.to eq([FavorPercentR::ERROR_MESSAGE] * 2)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'accepts // with only one slash in regexp' do
|
18
|
+
inspect_source(fpr, 'file.rb', ['x =~ /\/home/',
|
19
|
+
'y =~ /\//)'])
|
20
|
+
expect(fpr.offences.map(&:message)).to be_empty
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'accepts %r delimiters for regexp with two or more slashes' do
|
24
|
+
inspect_source(fpr, 'file.rb', ['x =~ %r(/home/)'])
|
25
|
+
expect(fpr.offences.map(&:message)).to be_empty
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -44,7 +44,14 @@ module Rubocop
|
|
44
44
|
expect(fs.offences).to be_empty
|
45
45
|
end
|
46
46
|
|
47
|
-
it '
|
47
|
+
it 'works if the first operand contains embedded expressions' do
|
48
|
+
inspect_source(fs,
|
49
|
+
'file.rb',
|
50
|
+
['puts "#{x * 5} %d #{@test}" % 10'])
|
51
|
+
expect(fs.offences.size).to eq(1)
|
52
|
+
expect(fs.offences.map(&:message))
|
53
|
+
.to eq([FavorSprintf::ERROR_MESSAGE])
|
54
|
+
end
|
48
55
|
end
|
49
56
|
end
|
50
57
|
end
|
@@ -5,38 +5,50 @@ require 'spec_helper'
|
|
5
5
|
module Rubocop
|
6
6
|
module Cop
|
7
7
|
describe Grammar do
|
8
|
-
EXAMPLE = '3.times { |i| x = "#{y}#{z}}" }'
|
8
|
+
EXAMPLE = '3.times { |i| x = "#{c ? y : a}#{z}}" }'
|
9
9
|
tokens = Ripper.lex(EXAMPLE).map { |t| Token.new(*t) }
|
10
10
|
let(:grammar) { Grammar.new(tokens) }
|
11
11
|
|
12
12
|
it 'correlates token indices to grammar paths' do
|
13
13
|
method_block = [:program, :method_add_block]
|
14
14
|
brace_block = method_block + [:brace_block]
|
15
|
+
string_embexpr = brace_block + [:assign, :string_literal,
|
16
|
+
:string_content,
|
17
|
+
:string_embexpr]
|
18
|
+
|
15
19
|
test_2_0 = [[[1, 0], :on_int, '3'],
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
20
|
+
[[1, 1], :on_period, '.'],
|
21
|
+
[[1, 2], :on_ident, 'times'],
|
22
|
+
[[1, 7], :on_sp, ' '],
|
23
|
+
[[1, 8], :on_lbrace, '{'],
|
24
|
+
[[1, 9], :on_sp, ' '], # 5
|
25
|
+
[[1, 10], :on_op, '|'],
|
26
|
+
[[1, 11], :on_ident, 'i'],
|
27
|
+
[[1, 12], :on_op, '|'],
|
28
|
+
[[1, 13], :on_sp, ' '],
|
29
|
+
[[1, 14], :on_ident, 'x'],
|
30
|
+
[[1, 15], :on_sp, ' '],
|
31
|
+
[[1, 16], :on_op, '='],
|
32
|
+
[[1, 17], :on_sp, ' '],
|
33
|
+
[[1, 18], :on_tstring_beg, '"'],
|
34
|
+
[[1, 19], :on_embexpr_beg, '#{'],
|
35
|
+
[[1, 21], :on_ident, 'c'],
|
36
|
+
[[1, 22], :on_sp, ' '],
|
37
|
+
[[1, 23], :on_op, '?'],
|
38
|
+
[[1, 24], :on_sp, ' '],
|
39
|
+
[[1, 25], :on_ident, 'y'],
|
40
|
+
[[1, 26], :on_sp, ' '],
|
41
|
+
[[1, 27], :on_op, ':'],
|
42
|
+
[[1, 28], :on_sp, ' '],
|
43
|
+
[[1, 29], :on_ident, 'a'],
|
44
|
+
[[1, 30], :on_embexpr_end, '}'],
|
45
|
+
[[1, 31], :on_embexpr_beg, '#{'],
|
46
|
+
[[1, 33], :on_ident, 'z'],
|
47
|
+
[[1, 34], :on_embexpr_end, '}'],
|
48
|
+
[[1, 35], :on_tstring_content, '}'],
|
49
|
+
[[1, 36], :on_tstring_end, '"'],
|
50
|
+
[[1, 37], :on_sp, ' '],
|
51
|
+
[[1, 38], :on_rbrace, '}']]
|
40
52
|
expect(Ripper.lex(EXAMPLE)).to eq(test_2_0) if RUBY_VERSION >= '2.0'
|
41
53
|
sexp = Ripper.sexp(EXAMPLE)
|
42
54
|
Position.make_position_objects(sexp)
|
@@ -44,21 +56,23 @@ module Rubocop
|
|
44
56
|
varref = (RUBY_VERSION == '1.9.2') ? :var_ref : :vcall
|
45
57
|
|
46
58
|
test = {
|
47
|
-
0 => method_block + [:call, :@int],
|
48
|
-
2 => method_block + [:call, :@ident],
|
49
|
-
4 => brace_block,
|
50
|
-
6 => brace_block + [:block_var],
|
51
|
-
7 => brace_block + [:block_var, :params, :@ident],
|
52
|
-
8 => brace_block + [:block_var],
|
53
|
-
10 => brace_block + [:assign, :var_field, :@ident],
|
54
|
-
12 => brace_block + [:assign],
|
55
|
-
16 =>
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
59
|
+
0 => method_block + [:call, :@int], # 3
|
60
|
+
2 => method_block + [:call, :@ident], # times
|
61
|
+
4 => brace_block, # {
|
62
|
+
6 => brace_block + [:block_var], # |
|
63
|
+
7 => brace_block + [:block_var, :params, :@ident], # i
|
64
|
+
8 => brace_block + [:block_var], # |
|
65
|
+
10 => brace_block + [:assign, :var_field, :@ident], # x
|
66
|
+
12 => brace_block + [:assign], # =
|
67
|
+
16 => string_embexpr + [:ifop, varref, :@ident], # c
|
68
|
+
18 => string_embexpr + [:ifop], # ?
|
69
|
+
20 => string_embexpr + [:ifop, varref, :@ident], # y
|
70
|
+
22 => string_embexpr + [:ifop], # :
|
71
|
+
24 => string_embexpr + [:ifop, varref, :@ident], # a
|
72
|
+
27 => string_embexpr + [:vcall, :@ident], # z
|
73
|
+
29 => brace_block + [:assign, :string_literal,
|
74
|
+
:string_content, :@tstring_content], # }
|
75
|
+
32 => brace_block,
|
62
76
|
}
|
63
77
|
expect(grammar.correlate(sexp)).to eq(test)
|
64
78
|
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Rubocop
|
6
|
+
module Cop
|
7
|
+
describe HandleExceptions do
|
8
|
+
let(:he) { HandleExceptions.new }
|
9
|
+
|
10
|
+
it 'registers an offence for empty rescue block' do
|
11
|
+
inspect_source(he,
|
12
|
+
'file.rb',
|
13
|
+
['begin',
|
14
|
+
' something',
|
15
|
+
'rescue',
|
16
|
+
' #do nothing',
|
17
|
+
'end'])
|
18
|
+
expect(he.offences.size).to eq(1)
|
19
|
+
expect(he.offences.map(&:message))
|
20
|
+
.to eq([HandleExceptions::ERROR_MESSAGE])
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'does not register an offence for rescue with body' do
|
24
|
+
inspect_source(he,
|
25
|
+
'file.rb',
|
26
|
+
['begin',
|
27
|
+
' something',
|
28
|
+
' return',
|
29
|
+
'rescue',
|
30
|
+
' file.close',
|
31
|
+
'end'])
|
32
|
+
expect(he.offences).to be_empty
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Rubocop
|
6
|
+
module Cop
|
7
|
+
describe HashLiteral do
|
8
|
+
let(:a) { HashLiteral.new }
|
9
|
+
|
10
|
+
it 'registers an offence for Hash.new()' do
|
11
|
+
inspect_source(a,
|
12
|
+
'file.rb',
|
13
|
+
['test = Hash.new()'])
|
14
|
+
expect(a.offences.size).to eq(1)
|
15
|
+
expect(a.offences.map(&:message))
|
16
|
+
.to eq([HashLiteral::ERROR_MESSAGE])
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'registers an offence for Hash.new'
|
20
|
+
|
21
|
+
it 'does not register an offence for Hash.new(3)' do
|
22
|
+
inspect_source(a,
|
23
|
+
'file.rb',
|
24
|
+
['test = Hash.new(3)'])
|
25
|
+
expect(a.offences).to be_empty
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,150 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Rubocop
|
6
|
+
module Cop
|
7
|
+
describe MethodLength do
|
8
|
+
let(:method_length) { MethodLength.new }
|
9
|
+
before do
|
10
|
+
MethodLength.stub(:max).and_return(5)
|
11
|
+
MethodLength.stub(:count_comments?).and_return(false)
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'rejects a method with more than 5 lines' do
|
15
|
+
inspect_source(method_length, '', ['def m()',
|
16
|
+
' a = 1',
|
17
|
+
' a = 2',
|
18
|
+
' a = 3',
|
19
|
+
' a = 4',
|
20
|
+
' a = 5',
|
21
|
+
' a = 6',
|
22
|
+
'end'])
|
23
|
+
expect(method_length.offences.size).to eq(1)
|
24
|
+
expect(method_length.offences.map(&:line_number).sort).to eq([1])
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'accepts a method with less than 5 lines' do
|
28
|
+
inspect_source(method_length, '', ['def m()',
|
29
|
+
' a = 1',
|
30
|
+
' a = 2',
|
31
|
+
' a = 3',
|
32
|
+
' a = 4',
|
33
|
+
'end'])
|
34
|
+
expect(method_length.offences).to be_empty
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'does not count blank lines' do
|
38
|
+
inspect_source(method_length, '', ['def m()',
|
39
|
+
' a = 1',
|
40
|
+
' a = 2',
|
41
|
+
' a = 3',
|
42
|
+
' a = 4',
|
43
|
+
'',
|
44
|
+
'',
|
45
|
+
' a = 7',
|
46
|
+
'end'])
|
47
|
+
expect(method_length.offences).to be_empty
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'accepts empty methods' do
|
51
|
+
inspect_source(method_length, '', ['def m()',
|
52
|
+
'end'])
|
53
|
+
expect(method_length.offences).to be_empty
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'is not fooled by one-liner methods, syntax #1' do
|
57
|
+
inspect_source(method_length, '', ['def one_line; 10 end',
|
58
|
+
'def self.m()',
|
59
|
+
' a = 1',
|
60
|
+
' a = 2',
|
61
|
+
' a = 4',
|
62
|
+
' a = 5',
|
63
|
+
' a = 6',
|
64
|
+
'end'])
|
65
|
+
expect(method_length.offences).to be_empty
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'is not fooled by one-liner methods, syntax #2' do
|
69
|
+
inspect_source(method_length, '', ['def one_line(test) 10 end',
|
70
|
+
'def self.m()',
|
71
|
+
' a = 1',
|
72
|
+
' a = 2',
|
73
|
+
' a = 4',
|
74
|
+
' a = 5',
|
75
|
+
' a = 6',
|
76
|
+
'end'])
|
77
|
+
expect(method_length.offences).to be_empty
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'checks class methods, syntax #1' do
|
81
|
+
inspect_source(method_length, '', ['def self.m()',
|
82
|
+
' a = 1',
|
83
|
+
' a = 2',
|
84
|
+
' a = 3',
|
85
|
+
' a = 4',
|
86
|
+
' a = 5',
|
87
|
+
' a = 6',
|
88
|
+
'end'])
|
89
|
+
expect(method_length.offences.size).to eq(1)
|
90
|
+
expect(method_length.offences.map(&:line_number).sort).to eq([1])
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'checks class methods, syntax #2' do
|
94
|
+
inspect_source(method_length, '', ['class K',
|
95
|
+
' class << self',
|
96
|
+
' def m()',
|
97
|
+
' a = 1',
|
98
|
+
' a = 2',
|
99
|
+
' a = 3',
|
100
|
+
' a = 4',
|
101
|
+
' a = 5',
|
102
|
+
' a = 6',
|
103
|
+
' end',
|
104
|
+
' end',
|
105
|
+
'end'])
|
106
|
+
expect(method_length.offences.size).to eq(1)
|
107
|
+
expect(method_length.offences.map(&:line_number).sort).to eq([3])
|
108
|
+
end
|
109
|
+
|
110
|
+
it 'properly counts lines when method ends with block' do
|
111
|
+
inspect_source(method_length, '', ['def m()',
|
112
|
+
' do',
|
113
|
+
' a = 2',
|
114
|
+
' a = 3',
|
115
|
+
' a = 4',
|
116
|
+
' a = 5',
|
117
|
+
' end',
|
118
|
+
'end'])
|
119
|
+
expect(method_length.offences.size).to eq(1)
|
120
|
+
expect(method_length.offences.map(&:line_number).sort).to eq([1])
|
121
|
+
end
|
122
|
+
|
123
|
+
it 'does not count commented lines by default' do
|
124
|
+
inspect_source(method_length, '', ['def m()',
|
125
|
+
' a = 1',
|
126
|
+
' #a = 2',
|
127
|
+
' a = 3',
|
128
|
+
' #a = 4',
|
129
|
+
' a = 5',
|
130
|
+
' a = 6',
|
131
|
+
'end'])
|
132
|
+
expect(method_length.offences).to be_empty
|
133
|
+
end
|
134
|
+
|
135
|
+
it 'has the option of counting commented lines' do
|
136
|
+
MethodLength.stub(:count_comments?).and_return(true)
|
137
|
+
inspect_source(method_length, '', ['def m()',
|
138
|
+
' a = 1',
|
139
|
+
' #a = 2',
|
140
|
+
' a = 3',
|
141
|
+
' #a = 4',
|
142
|
+
' a = 5',
|
143
|
+
' a = 6',
|
144
|
+
'end'])
|
145
|
+
expect(method_length.offences.size).to eq(1)
|
146
|
+
expect(method_length.offences.map(&:line_number).sort).to eq([1])
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|