-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
optional_arguments_spec.rb
99 lines (87 loc) · 2.55 KB
/
optional_arguments_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::Style::OptionalArguments, :config do
it 'registers an offense when an optional argument is followed by a required argument' do
expect_offense(<<~RUBY)
def foo(a = 1, b)
^^^^^ Optional arguments should appear at the end of the argument list.
end
RUBY
end
it 'registers an offense for each optional argument when multiple ' \
'optional arguments are followed by a required argument' do
expect_offense(<<~RUBY)
def foo(a = 1, b = 2, c)
^^^^^ Optional arguments should appear at the end of the argument list.
^^^^^ Optional arguments should appear at the end of the argument list.
end
RUBY
end
it 'allows methods without arguments' do
expect_no_offenses(<<~RUBY)
def foo
end
RUBY
end
it 'allows methods with only one required argument' do
expect_no_offenses(<<~RUBY)
def foo(a)
end
RUBY
end
it 'allows methods with only required arguments' do
expect_no_offenses(<<~RUBY)
def foo(a, b, c)
end
RUBY
end
it 'allows methods with only one optional argument' do
expect_no_offenses(<<~RUBY)
def foo(a = 1)
end
RUBY
end
it 'allows methods with only optional arguments' do
expect_no_offenses(<<~RUBY)
def foo(a = 1, b = 2, c = 3)
end
RUBY
end
it 'allows methods with multiple optional arguments at the end' do
expect_no_offenses(<<~RUBY)
def foo(a, b = 2, c = 3)
end
RUBY
end
context 'named params' do
context 'with default values' do
it 'allows optional arguments before an optional named argument' do
expect_no_offenses(<<~RUBY)
def foo(a = 1, b: 2)
end
RUBY
end
end
context 'required params', :ruby21 do
it 'registers an offense for optional arguments that come before ' \
'required arguments where there are name arguments' do
expect_offense(<<~RUBY)
def foo(a = 1, b, c:, d: 4)
^^^^^ Optional arguments should appear at the end of the argument list.
end
RUBY
end
it 'allows optional arguments before required named arguments' do
expect_no_offenses(<<~RUBY)
def foo(a = 1, b:)
end
RUBY
end
it 'allows optional arguments to come before a mix of required and optional named argument' do
expect_no_offenses(<<~RUBY)
def foo(a = 1, b:, c: 3)
end
RUBY
end
end
end
end