forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 1
/
alignment_corrector_spec.rb
119 lines (110 loc) · 2.67 KB
/
alignment_corrector_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::AlignmentCorrector, :config do
let(:cop_class) { RuboCop::Cop::Test::AlignmentDirective }
describe '#correct' do
context 'simple indentation' do
context 'with a positive column delta' do
it 'indents' do
expect(autocorrect_source(<<~INPUT)).to eq(<<~OUTPUT)
# >> 2
42
INPUT
# >> 2
42
OUTPUT
end
end
context 'with a negative column delta' do
it 'outdents' do
expect(autocorrect_source(<<~INPUT)).to eq(<<~OUTPUT)
# << 3
42
INPUT
# << 3
42
OUTPUT
end
end
end
shared_examples 'heredoc indenter' do |start_heredoc, column_delta|
let(:indentation) { ' ' * column_delta }
let(:end_heredoc) { /\w+/.match(start_heredoc)[0] }
it 'does not change indentation of here doc bodies and end markers' do
expect(autocorrect_source(<<~INPUT)).to eq(<<~OUTPUT)
# >> #{column_delta}
begin
#{start_heredoc}
a
b
#{end_heredoc}
end
INPUT
# >> #{column_delta}
#{indentation}begin
#{indentation} #{start_heredoc}
a
b
#{end_heredoc}
#{indentation}end
OUTPUT
end
end
context 'with large column deltas' do
context 'with plain heredoc (<<)' do
it_behaves_like 'heredoc indenter', '<<DOC', 20
end
context 'with heredoc in backticks (<<``)' do
it_behaves_like 'heredoc indenter', '<<`DOC`', 20
end
end
context 'with single-line here docs' do
it 'does not indent body and end marker' do
indentation = ' '
expect(autocorrect_source(<<~INPUT)).to eq(<<~OUTPUT)
# >> 2
begin
<<DOC
single line
DOC
end
INPUT
# >> 2
#{indentation}begin
#{indentation} <<DOC
single line
DOC
#{indentation}end
OUTPUT
end
end
context 'within string literals' do
it 'does not insert whitespace' do
expect(autocorrect_source(<<~INPUT)).to eq(<<~OUTPUT)
# >> 2
begin
dstr =
'a
b
c'
xstr =
`a
b
c`
end
INPUT
# >> 2
begin
dstr =
'a
b
c'
xstr =
`a
b
c`
end
OUTPUT
end
end
end
end