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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
module ResolverSpecs
def self.included(base)
base.module_eval do
let(:resolver) { nil }
let(:result) { @result }
before :each do
# See the comment in the first before :each block in safe_yaml_spec.rb.
require "safe_yaml"
end
def parse(yaml)
tree = YAML.parse(yaml.unindent)
@result = resolver.resolve_node(tree)
end
# Isn't this how I should've been doing it all along?
def parse_and_test(yaml)
safe_result = parse(yaml)
exception_thrown = nil
unsafe_result = begin
YAML.unsafe_load(yaml)
rescue Exception => e
exception_thrown = e
end
if exception_thrown
# If the underlying YAML parser (e.g. Psych) threw an exception, I'm
# honestly not sure what the right thing to do is. For now I'll just
# print a warning. Should SafeYAML fail when Psych fails?
Kernel.warn "\n"
Kernel.warn "Discrepancy between SafeYAML and #{SafeYAML::YAML_ENGINE} on input:\n"
Kernel.warn "#{yaml.unindent}\n"
Kernel.warn "SafeYAML result:"
Kernel.warn "#{safe_result.inspect}\n"
Kernel.warn "#{SafeYAML::YAML_ENGINE} result:"
Kernel.warn "#{exception_thrown.inspect}\n"
else
expect(safe_result).to eq(unsafe_result)
end
end
context "by default" do
it "translates maps to hashes" do
parse <<-YAML
potayto: potahto
tomayto: tomahto
YAML
expect(result).to eq({
"potayto" => "potahto",
"tomayto" => "tomahto"
})
end
it "translates sequences to arrays" do
parse <<-YAML
- foo
- bar
- baz
YAML
expect(result).to eq(["foo", "bar", "baz"])
end
it "translates most values to strings" do
parse "string: value"
expect(result).to eq({ "string" => "value" })
end
it "does not deserialize symbols" do
parse ":symbol: value"
expect(result).to eq({ ":symbol" => "value" })
end
it "translates valid integral numbers to integers" do
parse "integer: 1"
expect(result).to eq({ "integer" => 1 })
end
it "translates valid decimal numbers to floats" do
parse "float: 3.14"
expect(result).to eq({ "float" => 3.14 })
end
it "translates valid dates" do
parse "date: 2013-01-24"
expect(result).to eq({ "date" => Date.parse("2013-01-24") })
end
it "translates valid true/false values to booleans" do
parse <<-YAML
- yes
- true
- no
- false
YAML
expect(result).to eq([true, true, false, false])
end
it "translates valid nulls to nil" do
parse <<-YAML
-
- ~
- null
YAML
expect(result).to eq([nil] * 3)
end
it "matches the behavior of the underlying YAML engine w/ respect to capitalization of boolean values" do
parse_and_test <<-YAML
- true
- True
- TRUE
- tRue
- TRue
- False
- FALSE
- fAlse
- FALse
YAML
# using Syck: [true, true, true, "tRue", "TRue", false, false, "fAlse", "FALse"]
# using Psych: all booleans
end
it "matches the behavior of the underlying YAML engine w/ respect to capitalization of nil values" do
parse_and_test <<-YAML
- Null
- NULL
- nUll
- NUll
YAML
# using Syck: [nil, nil, "nUll", "NUll"]
# using Psych: all nils
end
it "translates quoted empty strings to strings (not nil)" do
parse "foo: ''"
expect(result).to eq({ "foo" => "" })
end
it "correctly reverse-translates strings encoded via #to_yaml" do
parse "5.10".to_yaml
expect(result).to eq("5.10")
end
it "does not specially parse any double-quoted strings" do
parse <<-YAML
- "1"
- "3.14"
- "true"
- "false"
- "2013-02-03"
- "2013-02-03 16:27:00 -0600"
YAML
expect(result).to eq(["1", "3.14", "true", "false", "2013-02-03", "2013-02-03 16:27:00 -0600"])
end
it "does not specially parse any single-quoted strings" do
parse <<-YAML
- '1'
- '3.14'
- 'true'
- 'false'
- '2013-02-03'
- '2013-02-03 16:27:00 -0600'
YAML
expect(result).to eq(["1", "3.14", "true", "false", "2013-02-03", "2013-02-03 16:27:00 -0600"])
end
it "deals just fine with nested maps" do
parse <<-YAML
foo:
bar:
marco: polo
YAML
expect(result).to eq({ "foo" => { "bar" => { "marco" => "polo" } } })
end
it "deals just fine with nested sequences" do
parse <<-YAML
- foo
-
- bar1
- bar2
-
- baz1
- baz2
YAML
expect(result).to eq(["foo", ["bar1", "bar2", ["baz1", "baz2"]]])
end
it "applies the same transformations to keys as to values" do
parse <<-YAML
foo: string
:bar: symbol
1: integer
3.14: float
2013-01-24: date
YAML
expect(result).to eq({
"foo" => "string",
":bar" => "symbol",
1 => "integer",
3.14 => "float",
Date.parse("2013-01-24") => "date",
})
end
it "applies the same transformations to elements in sequences as to all values" do
parse <<-YAML
- foo
- :bar
- 1
- 3.14
- 2013-01-24
YAML
expect(result).to eq(["foo", ":bar", 1, 3.14, Date.parse("2013-01-24")])
end
end
context "for Ruby version #{RUBY_VERSION}" do
it "translates valid time values" do
parse "time: 2013-01-29 05:58:00 -0800"
expect(result).to eq({ "time" => Time.utc(2013, 1, 29, 13, 58, 0) })
end
it "applies the same transformation to elements in sequences" do
parse "- 2013-01-29 05:58:00 -0800"
expect(result).to eq([Time.utc(2013, 1, 29, 13, 58, 0)])
end
it "applies the same transformation to keys" do
parse "2013-01-29 05:58:00 -0800: time"
expect(result).to eq({ Time.utc(2013, 1, 29, 13, 58, 0) => "time" })
end
end
context "with symbol deserialization enabled" do
before :each do
SafeYAML::OPTIONS[:deserialize_symbols] = true
end
after :each do
SafeYAML.restore_defaults!
end
it "translates values starting with ':' to symbols" do
parse "symbol: :value"
expect(result).to eq({ "symbol" => :value })
end
it "applies the same transformation to keys" do
parse ":bar: symbol"
expect(result).to eq({ :bar => "symbol" })
end
it "applies the same transformation to elements in sequences" do
parse "- :bar"
expect(result).to eq([:bar])
end
end
end
end
end
|