-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvariable_expander_spec.rb
199 lines (159 loc) · 4.12 KB
/
variable_expander_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
require 'spec_helper'
require 'etcenv/variable_expander'
describe Etcenv::VariableExpander do
let(:string) { '' }
let(:variables) { { 'VAR' => 'var', 'STR' => string } }
subject(:result) { described_class.expand(variables) }
describe "single expansion" do
subject { result['STR'] }
context "no variables" do
it { is_expected.to eq '' }
end
context "with variable" do
let(:string) { "$VAR" }
it { is_expected.to eq 'var' }
end
context "with {variable}" do
let(:string) { "${VAR}" }
it { is_expected.to eq 'var' }
end
context "with unexist variable" do
let(:string) { "$NOVAR" }
it { is_expected.to eq '' }
end
context "with string including variable" do
let(:string) { "foo $VAR baz" }
it { is_expected.to eq "foo var baz" }
end
context "with string including variable" do
let(:string) { "foo ${VAR} baz" }
it { is_expected.to eq "foo var baz" }
end
context "with multiple variable expansions" do
let(:string) { "${VAR}${VAR}" }
it { is_expected.to eq "varvar" }
end
context "with multiple variables expansions" do
let(:variables) { { 'VAR' => 'var', 'VAR2' => 'var2', 'STR' => string } }
let(:string) { "${VAR}${VAR2}" }
it { is_expected.to eq "varvar2" }
end
context "with multiple variables expansions" do
let(:variables) { { 'VAR' => 'var', 'VAR2' => 'var2', 'STR' => string } }
let(:string) { "${VAR}${VAR2}" }
it { is_expected.to eq "varvar2" }
end
context "with escaped variable" do
let(:string) { "\\$VAR" }
it { is_expected.to eq "$VAR" }
end
context "with escaped {variable}" do
let(:string) { "\\${VAR}" }
it { is_expected.to eq "${VAR}" }
end
context "with escaped variable and variable" do
let(:string) { "$VAR \\$VAR $VAR" }
it { is_expected.to eq "var $VAR var" }
end
end
describe "multiple expansions" do
let(:variables) do
{
'VAR2' => '${VAR}',
'VAR' => 'var'
}
end
it "resolves correctly" do
expect(subject['VAR']).to eq 'var'
expect(subject['VAR2']).to eq 'var'
end
end
describe "nested multiple expansions" do
# VALUE
# VAR
# FOO, BAZ
# BAR
# VAR2
let(:variables) do
{
'VAR2' => '${VAR}',
'BAR' => '${FOO} bar ${BAZ}',
'VAR' => '${VALUE}',
'FOO' => 'foo ${VAR}',
'BAZ' => 'baz ${VAR}',
'VALUE' => 'var'
}
end
it "resolves correctly" do
expect(result).to eq(
'VALUE' => 'var',
'VAR' => 'var',
'FOO' => 'foo var',
'BAZ' => 'baz var',
'BAR' => 'foo var bar baz var',
'VAR2' => 'var',
)
end
end
context "looped multiple expansions with root" do
# VAR
# VAR2
let(:variables) do
{
'ROOT' => '${VAR}',
'VAR2' => '${VAR}',
'VAR' => '${VAR2}',
}
end
it "raises error" do
expect { subject }.to raise_error(Etcenv::VariableExpander::LoopError)
end
end
context "looped multiple expansions" do
# VAR
# VAR2
let(:variables) do
{
'VAR2' => '${VAR}',
'VAR' => '${VAR2}',
}
end
it "raises error" do
expect { subject }.to raise_error(Etcenv::VariableExpander::LoopError)
end
end
context "nested looped multiple expansions" do
# VAR
# VAR2
# VAR3
let(:variables) do
{
'VAR' => '${VAR2}',
'VAR2' => '${VAR3}',
'VAR3' => '${VAR}',
}
end
it "raises error" do
expect { subject }.to raise_error(Etcenv::VariableExpander::LoopError)
end
end
context "exceed max_depth" do
# VAR
# VAR2
# VAR3
let(:variables) do
Hash[(1..50).map do |i|
["VAR#{i}", (1...i).map { |_| "${VAR#{_}}" }.join]
end]
end
it "raises error" do
expect { subject }.to raise_error(Etcenv::VariableExpander::DepthLimitError)
end
end
context "empty" do
let(:variables) do
{}
end
it { is_expected.to eq({}) }
end
end