/
space_in_lambda_literal_spec.rb
146 lines (120 loc) · 4.29 KB
/
space_in_lambda_literal_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Layout::SpaceInLambdaLiteral, :config do
context 'when configured to enforce spaces' do
let(:cop_config) { { 'EnforcedStyle' => 'require_space' } }
it 'registers an offense and corrects no space between -> and (' do
expect_offense(<<~RUBY)
a = ->(b, c) { b + c }
^^^^^^^^ Use a space between `->` and `(` in lambda literals.
RUBY
expect_correction(<<~RUBY)
a = -> (b, c) { b + c }
RUBY
end
it 'does not register an offense for a space between -> and (' do
expect_no_offenses('a = -> (b, c) { b + c }')
end
it 'does not register an offense for multi-line lambdas' do
expect_no_offenses(<<~RUBY)
l = lambda do |a, b|
tmp = a * 7
tmp * b / 50
end
RUBY
end
it 'does not register an offense for no space between -> and {' do
expect_no_offenses('a = ->{ b + c }')
end
it 'registers an offense and corrects no space in the inner nested lambda' do
expect_offense(<<~RUBY)
a = -> (b = ->(c) {}, d) { b + d }
^^^^^ Use a space between `->` and `(` in lambda literals.
RUBY
expect_correction(<<~RUBY)
a = -> (b = -> (c) {}, d) { b + d }
RUBY
end
it 'registers an offense and corrects no space in the outer nested lambda' do
expect_offense(<<~RUBY)
a = ->(b = -> (c) {}, d) { b + d }
^^^^^^^^^^^^^^^^^^^^ Use a space between `->` and `(` in lambda literals.
RUBY
expect_correction(<<~RUBY)
a = -> (b = -> (c) {}, d) { b + d }
RUBY
end
it 'registers an offense and corrects no space in both lambdas when nested' do
expect_offense(<<~RUBY)
a = ->(b = ->(c) {}, d) { b + d }
^^^^^ Use a space between `->` and `(` in lambda literals.
^^^^^^^^^^^^^^^^^^^ Use a space between `->` and `(` in lambda literals.
RUBY
expect_correction(<<~RUBY)
a = -> (b = -> (c) {}, d) { b + d }
RUBY
end
end
context 'when configured to enforce no space' do
let(:cop_config) { { 'EnforcedStyle' => 'require_no_space' } }
it 'registers an offense and corrects a space between -> and (' do
expect_offense(<<~RUBY)
a = -> (b, c) { b + c }
^ Do not use spaces between `->` and `(` in lambda literals.
RUBY
expect_correction(<<~RUBY)
a = ->(b, c) { b + c }
RUBY
end
it 'does not register an offense for no space between -> and (' do
expect_no_offenses('a = ->(b, c) { b + c }')
end
it 'does not register an offense for multi-line lambdas' do
expect_no_offenses(<<~RUBY)
l = lambda do |a, b|
tmp = a * 7
tmp * b / 50
end
RUBY
end
it 'does not register an offense for a space between -> and {' do
expect_no_offenses('a = -> { b + c }')
end
it 'registers an offense and corrects spaces between -> and (' do
expect_offense(<<~RUBY)
a = -> (b, c) { b + c }
^^^ Do not use spaces between `->` and `(` in lambda literals.
RUBY
expect_correction(<<~RUBY)
a = ->(b, c) { b + c }
RUBY
end
it 'registers an offense and corrects a space in the inner nested lambda' do
expect_offense(<<~RUBY)
a = ->(b = -> (c) {}, d) { b + d }
^ Do not use spaces between `->` and `(` in lambda literals.
RUBY
expect_correction(<<~RUBY)
a = ->(b = ->(c) {}, d) { b + d }
RUBY
end
it 'registers an offense and corrects a space in the outer nested lambda' do
expect_offense(<<~RUBY)
a = -> (b = ->(c) {}, d) { b + d }
^ Do not use spaces between `->` and `(` in lambda literals.
RUBY
expect_correction(<<~RUBY)
a = ->(b = ->(c) {}, d) { b + d }
RUBY
end
it 'register offenses and correct spaces in both lambdas when nested' do
expect_offense(<<~RUBY)
a = -> (b = -> (c) {}, d) { b + d }
^ Do not use spaces between `->` and `(` in lambda literals.
^ Do not use spaces between `->` and `(` in lambda literals.
RUBY
expect_correction(<<~RUBY)
a = ->(b = ->(c) {}, d) { b + d }
RUBY
end
end
end