/
parameter_lists_spec.rb
99 lines (84 loc) · 2.65 KB
/
parameter_lists_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Metrics::ParameterLists, :config do
let(:cop_config) { { 'Max' => 4, 'CountKeywordArgs' => true, 'MaxOptionalParameters' => 3 } }
it 'registers an offense for a method def with 5 parameters' do
expect_offense(<<~RUBY)
def meth(a, b, c, d, e)
^^^^^^^^^^^^^^^ Avoid parameter lists longer than 4 parameters. [5/4]
end
RUBY
end
it 'accepts a method def with 4 parameters' do
expect_no_offenses(<<~RUBY)
def meth(a, b, c, d)
end
RUBY
end
it 'accepts a proc with more than 4 parameters' do
expect_no_offenses(<<~RUBY)
proc { |a, b, c, d, e| }
RUBY
end
it 'accepts a lambda with more than 4 parameters' do
expect_no_offenses(<<~RUBY)
->(a, b, c, d, e) { }
RUBY
end
context 'When CountKeywordArgs is true' do
it 'counts keyword arguments as well' do
expect_offense(<<~RUBY)
def meth(a, b, c, d: 1, e: 2)
^^^^^^^^^^^^^^^^^^^^^ Avoid parameter lists longer than 4 parameters. [5/4]
end
RUBY
end
end
context 'When CountKeywordArgs is false' do
before { cop_config['CountKeywordArgs'] = false }
it 'does not count keyword arguments' do
expect_no_offenses(<<~RUBY)
def meth(a, b, c, d: 1, e: 2)
end
RUBY
end
it 'does not count keyword arguments without default values' do
expect_no_offenses(<<~RUBY)
def meth(a, b, c, d:, e:)
end
RUBY
end
end
it 'registers an offense when optargs count exceeds the maximum' do
expect_offense(<<~RUBY)
def foo(a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid parameter lists longer than 4 parameters. [7/4]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Method has too many optional parameters. [7/3]
end
def foo(a = 1, b = 2, c = 3, d = 4)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Method has too many optional parameters. [4/3]
end
RUBY
expect(cop.config_to_allow_offenses[:exclude_limit]).to eq(
'Max' => 7,
'MaxOptionalParameters' => 7
)
end
it 'does not register an offense when method has allowed amount of optargs' do
expect_no_offenses(<<~RUBY)
def foo(a, b = 2, c = 3, d = 4)
end
RUBY
end
it 'does not register an offense when method has allowed amount of args with block arg' do
expect_no_offenses(<<~RUBY)
def foo(a, b, c, d, &block)
end
RUBY
end
it 'does not register an offense when method has no args' do
expect_no_offenses(<<~RUBY)
def foo
end
RUBY
end
end