multi_xml 0.5.5 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CONTRIBUTING.md +5 -3
- data/README.md +15 -17
- data/lib/multi_xml.rb +80 -71
- data/lib/multi_xml/parsers/libxml.rb +5 -2
- data/lib/multi_xml/parsers/libxml2_parser.rb +18 -20
- data/lib/multi_xml/parsers/nokogiri.rb +6 -3
- data/lib/multi_xml/parsers/oga.rb +73 -0
- data/lib/multi_xml/parsers/ox.rb +8 -14
- data/lib/multi_xml/parsers/rexml.rb +13 -13
- data/lib/multi_xml/version.rb +43 -1
- data/multi_xml.gemspec +5 -10
- metadata +15 -55
- data.tar.gz.sig +0 -0
- data/Rakefile +0 -21
- data/spec/helper.rb +0 -17
- data/spec/multi_xml_spec.rb +0 -43
- data/spec/parser_shared_example.rb +0 -694
- data/spec/speed.rb +0 -63
- metadata.gz.sig +0 -0
data/spec/helper.rb
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
require 'simplecov'
|
2
|
-
require 'coveralls'
|
3
|
-
|
4
|
-
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
5
|
-
SimpleCov::Formatter::HTMLFormatter,
|
6
|
-
Coveralls::SimpleCov::Formatter
|
7
|
-
]
|
8
|
-
SimpleCov.start
|
9
|
-
|
10
|
-
require 'multi_xml'
|
11
|
-
require 'rspec'
|
12
|
-
|
13
|
-
RSpec.configure do |config|
|
14
|
-
config.expect_with :rspec do |c|
|
15
|
-
c.syntax = :expect
|
16
|
-
end
|
17
|
-
end
|
data/spec/multi_xml_spec.rb
DELETED
@@ -1,43 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
require 'parser_shared_example'
|
3
|
-
|
4
|
-
class MockDecoder; end
|
5
|
-
|
6
|
-
describe "MultiXml" do
|
7
|
-
context "Parsers" do
|
8
|
-
it "picks a default parser" do
|
9
|
-
expect(MultiXml.parser).to be_kind_of(Module)
|
10
|
-
expect(MultiXml.parser).to respond_to(:parse)
|
11
|
-
end
|
12
|
-
|
13
|
-
it "defaults to the best available gem" do
|
14
|
-
# Clear cache variable already set by previous tests
|
15
|
-
MultiXml.send(:remove_instance_variable, :@parser)
|
16
|
-
expect(MultiXml.parser.name).to eq('MultiXml::Parsers::Ox')
|
17
|
-
end
|
18
|
-
|
19
|
-
it "is settable via a symbol" do
|
20
|
-
MultiXml.parser = :rexml
|
21
|
-
expect(MultiXml.parser.name).to eq('MultiXml::Parsers::Rexml')
|
22
|
-
end
|
23
|
-
|
24
|
-
it "is settable via a class" do
|
25
|
-
MultiXml.parser = MockDecoder
|
26
|
-
expect(MultiXml.parser.name).to eq('MockDecoder')
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
[['LibXML', 'libxml'],
|
31
|
-
['REXML', 'rexml/document'],
|
32
|
-
['Nokogiri', 'nokogiri'],
|
33
|
-
['Ox', 'ox']].each do |parser|
|
34
|
-
begin
|
35
|
-
require parser.last
|
36
|
-
context "#{parser.first} parser" do
|
37
|
-
it_behaves_like "a parser", parser.first
|
38
|
-
end
|
39
|
-
rescue LoadError => e
|
40
|
-
puts "Tests not run for #{parser.first} due to a LoadError"
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
@@ -1,694 +0,0 @@
|
|
1
|
-
shared_examples_for "a parser" do |parser|
|
2
|
-
|
3
|
-
before do
|
4
|
-
begin
|
5
|
-
MultiXml.parser = parser
|
6
|
-
rescue LoadError
|
7
|
-
pending "Parser #{parser} couldn't be loaded"
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
describe ".parse" do
|
12
|
-
context "a blank string" do
|
13
|
-
before do
|
14
|
-
@xml = ''
|
15
|
-
end
|
16
|
-
|
17
|
-
it "returns an empty Hash" do
|
18
|
-
expect(MultiXml.parse(@xml)).to eq({})
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
context "a whitespace string" do
|
23
|
-
before do
|
24
|
-
@xml = ' '
|
25
|
-
end
|
26
|
-
|
27
|
-
it "returns an empty Hash" do
|
28
|
-
expect(MultiXml.parse(@xml)).to eq({})
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
context "an invalid XML document" do
|
33
|
-
before do
|
34
|
-
@xml = '<open></close>'
|
35
|
-
end
|
36
|
-
|
37
|
-
it "raises MultiXml::ParseError" do
|
38
|
-
expect{MultiXml.parse(@xml)}.to raise_error(MultiXml::ParseError)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
context "a valid XML document" do
|
43
|
-
before do
|
44
|
-
@xml = '<user/>'
|
45
|
-
end
|
46
|
-
|
47
|
-
it "parses correctly" do
|
48
|
-
expect(MultiXml.parse(@xml)).to eq({'user' => nil})
|
49
|
-
end
|
50
|
-
|
51
|
-
context "with CDATA" do
|
52
|
-
before do
|
53
|
-
@xml = '<user><![CDATA[Erik Michaels-Ober]]></user>'
|
54
|
-
end
|
55
|
-
|
56
|
-
it "returns the correct CDATA" do
|
57
|
-
expect(MultiXml.parse(@xml)['user']).to eq("Erik Michaels-Ober")
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
context "element with the same inner element and attribute name" do
|
62
|
-
before do
|
63
|
-
@xml = "<user name='John'><name>Smith</name></user>"
|
64
|
-
end
|
65
|
-
|
66
|
-
it "returns nams as Array" do
|
67
|
-
expect(MultiXml.parse(@xml)['user']['name']).to eq ['John', 'Smith']
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
context "with content" do
|
72
|
-
before do
|
73
|
-
@xml = '<user>Erik Michaels-Ober</user>'
|
74
|
-
end
|
75
|
-
|
76
|
-
it "returns the correct content" do
|
77
|
-
expect(MultiXml.parse(@xml)['user']).to eq("Erik Michaels-Ober")
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
context "with an attribute" do
|
82
|
-
before do
|
83
|
-
@xml = '<user name="Erik Michaels-Ober"/>'
|
84
|
-
end
|
85
|
-
|
86
|
-
it "returns the correct attribute" do
|
87
|
-
expect(MultiXml.parse(@xml)['user']['name']).to eq("Erik Michaels-Ober")
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
context "with multiple attributes" do
|
92
|
-
before do
|
93
|
-
@xml = '<user name="Erik Michaels-Ober" screen_name="sferik"/>'
|
94
|
-
end
|
95
|
-
|
96
|
-
it "returns the correct attributes" do
|
97
|
-
expect(MultiXml.parse(@xml)['user']['name']).to eq("Erik Michaels-Ober")
|
98
|
-
expect(MultiXml.parse(@xml)['user']['screen_name']).to eq("sferik")
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
context "typecast management" do
|
103
|
-
before do
|
104
|
-
@xml = %Q{
|
105
|
-
<global-settings>
|
106
|
-
<group>
|
107
|
-
<name>Settings</name>
|
108
|
-
<setting type="string">
|
109
|
-
<description>Test</description>
|
110
|
-
</setting>
|
111
|
-
</group>
|
112
|
-
</global-settings>
|
113
|
-
}
|
114
|
-
end
|
115
|
-
|
116
|
-
context "with :typecast_xml_value => true" do
|
117
|
-
before do
|
118
|
-
@setting = MultiXml.parse(@xml)["global_settings"]["group"]["setting"]
|
119
|
-
end
|
120
|
-
|
121
|
-
it { expect(@setting).to eq "" }
|
122
|
-
end
|
123
|
-
|
124
|
-
context "with :typecast_xml_value => false" do
|
125
|
-
before do
|
126
|
-
@setting = MultiXml.parse(@xml, :typecast_xml_value => false)["global_settings"]["group"]["setting"]
|
127
|
-
end
|
128
|
-
|
129
|
-
it { expect(@setting).to eq({"type"=>"string", "description"=>{"__content__"=>"Test"}}) }
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
context "with :symbolize_keys => true" do
|
134
|
-
before do
|
135
|
-
@xml = '<users><user name="Erik Michaels-Ober"/><user><name>Wynn Netherland</name></user></users>'
|
136
|
-
end
|
137
|
-
|
138
|
-
it "symbolizes keys" do
|
139
|
-
expect(MultiXml.parse(@xml, :symbolize_keys => true)).to eq({:users => {:user => [{:name => "Erik Michaels-Ober"}, {:name => "Wynn Netherland"}]}})
|
140
|
-
end
|
141
|
-
end
|
142
|
-
|
143
|
-
context "with an attribute type=\"boolean\"" do
|
144
|
-
%w(true false).each do |boolean|
|
145
|
-
context "when #{boolean}" do
|
146
|
-
it "returns #{boolean}" do
|
147
|
-
xml = "<tag type=\"boolean\">#{boolean}</tag>"
|
148
|
-
expect(MultiXml.parse(xml)['tag']).to instance_eval("be_#{boolean}")
|
149
|
-
end
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
context "when 1" do
|
154
|
-
before do
|
155
|
-
@xml = '<tag type="boolean">1</tag>'
|
156
|
-
end
|
157
|
-
|
158
|
-
it "returns true" do
|
159
|
-
expect(MultiXml.parse(@xml)['tag']).to be_true
|
160
|
-
end
|
161
|
-
end
|
162
|
-
|
163
|
-
context "when 0" do
|
164
|
-
before do
|
165
|
-
@xml = '<tag type="boolean">0</tag>'
|
166
|
-
end
|
167
|
-
|
168
|
-
it "returns false" do
|
169
|
-
expect(MultiXml.parse(@xml)['tag']).to be_false
|
170
|
-
end
|
171
|
-
end
|
172
|
-
end
|
173
|
-
|
174
|
-
context "with an attribute type=\"integer\"" do
|
175
|
-
context "with a positive integer" do
|
176
|
-
before do
|
177
|
-
@xml = '<tag type="integer">1</tag>'
|
178
|
-
end
|
179
|
-
|
180
|
-
it "returns a Fixnum" do
|
181
|
-
expect(MultiXml.parse(@xml)['tag']).to be_a(Fixnum)
|
182
|
-
end
|
183
|
-
|
184
|
-
it "returns a positive number" do
|
185
|
-
expect(MultiXml.parse(@xml)['tag']).to be > 0
|
186
|
-
end
|
187
|
-
|
188
|
-
it "returns the correct number" do
|
189
|
-
expect(MultiXml.parse(@xml)['tag']).to eq(1)
|
190
|
-
end
|
191
|
-
end
|
192
|
-
|
193
|
-
context "with a negative integer" do
|
194
|
-
before do
|
195
|
-
@xml = '<tag type="integer">-1</tag>'
|
196
|
-
end
|
197
|
-
|
198
|
-
it "returns a Fixnum" do
|
199
|
-
expect(MultiXml.parse(@xml)['tag']).to be_a(Fixnum)
|
200
|
-
end
|
201
|
-
|
202
|
-
it "returns a negative number" do
|
203
|
-
expect(MultiXml.parse(@xml)['tag']).to be < 0
|
204
|
-
end
|
205
|
-
|
206
|
-
it "returns the correct number" do
|
207
|
-
expect(MultiXml.parse(@xml)['tag']).to eq(-1)
|
208
|
-
end
|
209
|
-
end
|
210
|
-
end
|
211
|
-
|
212
|
-
context "with an attribute type=\"string\"" do
|
213
|
-
before do
|
214
|
-
@xml = '<tag type="string"></tag>'
|
215
|
-
end
|
216
|
-
|
217
|
-
it "returns a String" do
|
218
|
-
expect(MultiXml.parse(@xml)['tag']).to be_a(String)
|
219
|
-
end
|
220
|
-
|
221
|
-
it "returns the correct string" do
|
222
|
-
expect(MultiXml.parse(@xml)['tag']).to eq("")
|
223
|
-
end
|
224
|
-
end
|
225
|
-
|
226
|
-
context "with an attribute type=\"date\"" do
|
227
|
-
before do
|
228
|
-
@xml = '<tag type="date">1970-01-01</tag>'
|
229
|
-
end
|
230
|
-
|
231
|
-
it "returns a Date" do
|
232
|
-
expect(MultiXml.parse(@xml)['tag']).to be_a(Date)
|
233
|
-
end
|
234
|
-
|
235
|
-
it "returns the correct date" do
|
236
|
-
expect(MultiXml.parse(@xml)['tag']).to eq(Date.parse('1970-01-01'))
|
237
|
-
end
|
238
|
-
end
|
239
|
-
|
240
|
-
context "with an attribute type=\"datetime\"" do
|
241
|
-
before do
|
242
|
-
@xml = '<tag type="datetime">1970-01-01 00:00</tag>'
|
243
|
-
end
|
244
|
-
|
245
|
-
it "returns a Time" do
|
246
|
-
expect(MultiXml.parse(@xml)['tag']).to be_a(Time)
|
247
|
-
end
|
248
|
-
|
249
|
-
it "returns the correct time" do
|
250
|
-
expect(MultiXml.parse(@xml)['tag']).to eq(Time.parse('1970-01-01 00:00'))
|
251
|
-
end
|
252
|
-
end
|
253
|
-
|
254
|
-
context "with an attribute type=\"dateTime\"" do
|
255
|
-
before do
|
256
|
-
@xml = '<tag type="datetime">1970-01-01 00:00</tag>'
|
257
|
-
end
|
258
|
-
|
259
|
-
it "returns a Time" do
|
260
|
-
expect(MultiXml.parse(@xml)['tag']).to be_a(Time)
|
261
|
-
end
|
262
|
-
|
263
|
-
it "returns the correct time" do
|
264
|
-
expect(MultiXml.parse(@xml)['tag']).to eq(Time.parse('1970-01-01 00:00'))
|
265
|
-
end
|
266
|
-
end
|
267
|
-
|
268
|
-
context "with an attribute type=\"double\"" do
|
269
|
-
before do
|
270
|
-
@xml = '<tag type="double">3.14159265358979</tag>'
|
271
|
-
end
|
272
|
-
|
273
|
-
it "returns a Float" do
|
274
|
-
expect(MultiXml.parse(@xml)['tag']).to be_a(Float)
|
275
|
-
end
|
276
|
-
|
277
|
-
it "returns the correct number" do
|
278
|
-
expect(MultiXml.parse(@xml)['tag']).to eq(3.14159265358979)
|
279
|
-
end
|
280
|
-
end
|
281
|
-
|
282
|
-
context "with an attribute type=\"decimal\"" do
|
283
|
-
before do
|
284
|
-
@xml = '<tag type="decimal">3.14159265358979</tag>'
|
285
|
-
end
|
286
|
-
|
287
|
-
it "returns a BigDecimal" do
|
288
|
-
expect(MultiXml.parse(@xml)['tag']).to be_a(BigDecimal)
|
289
|
-
end
|
290
|
-
|
291
|
-
it "returns the correct number" do
|
292
|
-
expect(MultiXml.parse(@xml)['tag']).to eq(3.14159265358979)
|
293
|
-
end
|
294
|
-
end
|
295
|
-
|
296
|
-
context "with an attribute type=\"base64Binary\"" do
|
297
|
-
before do
|
298
|
-
@xml = '<tag type="base64Binary">aW1hZ2UucG5n</tag>'
|
299
|
-
end
|
300
|
-
|
301
|
-
it "returns a String" do
|
302
|
-
expect(MultiXml.parse(@xml)['tag']).to be_a(String)
|
303
|
-
end
|
304
|
-
|
305
|
-
it "returns the correct string" do
|
306
|
-
expect(MultiXml.parse(@xml)['tag']).to eq("image.png")
|
307
|
-
end
|
308
|
-
end
|
309
|
-
|
310
|
-
context "with an attribute type=\"yaml\"" do
|
311
|
-
before do
|
312
|
-
@xml = "<tag type=\"yaml\">--- \n1: returns an integer\n:message: Have a nice day\narray: \n- has-dashes: true\n has_underscores: true\n</tag>"
|
313
|
-
end
|
314
|
-
|
315
|
-
it "raises MultiXML::DisallowedTypeError by default" do
|
316
|
-
expect{ MultiXml.parse(@xml)['tag'] }.to raise_error(MultiXml::DisallowedTypeError)
|
317
|
-
end
|
318
|
-
|
319
|
-
it "returns the correctly parsed YAML when the type is allowed" do
|
320
|
-
expect(MultiXml.parse(@xml, :disallowed_types => [])['tag']).to eq({:message => "Have a nice day", 1 => "returns an integer", "array" => [{"has-dashes" => true, "has_underscores" => true}]})
|
321
|
-
end
|
322
|
-
end
|
323
|
-
|
324
|
-
context "with an attribute type=\"symbol\"" do
|
325
|
-
before do
|
326
|
-
@xml = "<tag type=\"symbol\">my_symbol</tag>"
|
327
|
-
end
|
328
|
-
|
329
|
-
it "raises MultiXML::DisallowedTypeError" do
|
330
|
-
expect{ MultiXml.parse(@xml)['tag'] }.to raise_error(MultiXml::DisallowedTypeError)
|
331
|
-
end
|
332
|
-
|
333
|
-
it "returns the correctly parsed Symbol when the type is allowed" do
|
334
|
-
expect(MultiXml.parse(@xml, :disallowed_types => [])['tag']).to eq(:my_symbol)
|
335
|
-
end
|
336
|
-
end
|
337
|
-
|
338
|
-
context "with an attribute type=\"file\"" do
|
339
|
-
before do
|
340
|
-
@xml = '<tag type="file" name="data.txt" content_type="text/plain">ZGF0YQ==</tag>'
|
341
|
-
end
|
342
|
-
|
343
|
-
it "returns a StringIO" do
|
344
|
-
expect(MultiXml.parse(@xml)['tag']).to be_a(StringIO)
|
345
|
-
end
|
346
|
-
|
347
|
-
it "is decoded correctly" do
|
348
|
-
expect(MultiXml.parse(@xml)['tag'].string).to eq('data')
|
349
|
-
end
|
350
|
-
|
351
|
-
it "has the correct file name" do
|
352
|
-
expect(MultiXml.parse(@xml)['tag'].original_filename).to eq('data.txt')
|
353
|
-
end
|
354
|
-
|
355
|
-
it "has the correct content type" do
|
356
|
-
expect(MultiXml.parse(@xml)['tag'].content_type).to eq('text/plain')
|
357
|
-
end
|
358
|
-
|
359
|
-
context "with missing name and content type" do
|
360
|
-
before do
|
361
|
-
@xml = '<tag type="file">ZGF0YQ==</tag>'
|
362
|
-
end
|
363
|
-
|
364
|
-
it "returns a StringIO" do
|
365
|
-
expect(MultiXml.parse(@xml)['tag']).to be_a(StringIO)
|
366
|
-
end
|
367
|
-
|
368
|
-
it "is decoded correctly" do
|
369
|
-
expect(MultiXml.parse(@xml)['tag'].string).to eq('data')
|
370
|
-
end
|
371
|
-
|
372
|
-
it "has the default file name" do
|
373
|
-
expect(MultiXml.parse(@xml)['tag'].original_filename).to eq('untitled')
|
374
|
-
end
|
375
|
-
|
376
|
-
it "has the default content type" do
|
377
|
-
expect(MultiXml.parse(@xml)['tag'].content_type).to eq('application/octet-stream')
|
378
|
-
end
|
379
|
-
end
|
380
|
-
end
|
381
|
-
|
382
|
-
context "with an attribute type=\"array\"" do
|
383
|
-
before do
|
384
|
-
@xml = '<users type="array"><user>Erik Michaels-Ober</user><user>Wynn Netherland</user></users>'
|
385
|
-
end
|
386
|
-
|
387
|
-
it "returns an Array" do
|
388
|
-
expect(MultiXml.parse(@xml)['users']).to be_a(Array)
|
389
|
-
end
|
390
|
-
|
391
|
-
it "returns the correct array" do
|
392
|
-
expect(MultiXml.parse(@xml)['users']).to eq(["Erik Michaels-Ober", "Wynn Netherland"])
|
393
|
-
end
|
394
|
-
end
|
395
|
-
|
396
|
-
context "with an attribute type=\"array\" in addition to other attributes" do
|
397
|
-
before do
|
398
|
-
@xml = '<users type="array" foo="bar"><user>Erik Michaels-Ober</user><user>Wynn Netherland</user></users>'
|
399
|
-
end
|
400
|
-
|
401
|
-
it "returns an Array" do
|
402
|
-
expect(MultiXml.parse(@xml)['users']).to be_a(Array)
|
403
|
-
end
|
404
|
-
|
405
|
-
it "returns the correct array" do
|
406
|
-
expect(MultiXml.parse(@xml)['users']).to eq(["Erik Michaels-Ober", "Wynn Netherland"])
|
407
|
-
end
|
408
|
-
end
|
409
|
-
|
410
|
-
context "with an attribute type=\"array\" containing only one item" do
|
411
|
-
before do
|
412
|
-
@xml = '<users type="array"><user>Erik Michaels-Ober</user></users>'
|
413
|
-
end
|
414
|
-
|
415
|
-
it "returns an Array" do
|
416
|
-
expect(MultiXml.parse(@xml)['users']).to be_a(Array)
|
417
|
-
end
|
418
|
-
|
419
|
-
it "returns the correct array" do
|
420
|
-
expect(MultiXml.parse(@xml)['users']).to eq(["Erik Michaels-Ober"])
|
421
|
-
end
|
422
|
-
end
|
423
|
-
|
424
|
-
%w(integer boolean date datetime file).each do |type|
|
425
|
-
context "with an empty attribute type=\"#{type}\"" do
|
426
|
-
before do
|
427
|
-
@xml = "<tag type=\"#{type}\"/>"
|
428
|
-
end
|
429
|
-
|
430
|
-
it "returns nil" do
|
431
|
-
expect(MultiXml.parse(@xml)['tag']).to be_nil
|
432
|
-
end
|
433
|
-
end
|
434
|
-
end
|
435
|
-
|
436
|
-
%w{yaml symbol}.each do |type|
|
437
|
-
context "with an empty attribute type=\"#{type}\"" do
|
438
|
-
before do
|
439
|
-
@xml = "<tag type=\"#{type}\"/>"
|
440
|
-
end
|
441
|
-
|
442
|
-
it "raises MultiXml::DisallowedTypeError by default" do
|
443
|
-
expect{ MultiXml.parse(@xml)['tag']}.to raise_error(MultiXml::DisallowedTypeError)
|
444
|
-
end
|
445
|
-
|
446
|
-
it "returns nil when the type is allowed" do
|
447
|
-
expect(MultiXml.parse(@xml, :disallowed_types => [])['tag']).to be_nil
|
448
|
-
end
|
449
|
-
end
|
450
|
-
end
|
451
|
-
|
452
|
-
context "with an empty attribute type=\"array\"" do
|
453
|
-
before do
|
454
|
-
@xml = '<tag type="array"/>'
|
455
|
-
end
|
456
|
-
|
457
|
-
it "returns an empty Array" do
|
458
|
-
expect(MultiXml.parse(@xml)['tag']).to eq([])
|
459
|
-
end
|
460
|
-
|
461
|
-
context "with whitespace" do
|
462
|
-
before do
|
463
|
-
@xml = '<tag type="array"> </tag>'
|
464
|
-
end
|
465
|
-
|
466
|
-
it "returns an empty Array" do
|
467
|
-
expect(MultiXml.parse(@xml)['tag']).to eq([])
|
468
|
-
end
|
469
|
-
end
|
470
|
-
end
|
471
|
-
|
472
|
-
context "with XML entities" do
|
473
|
-
before do
|
474
|
-
@xml_entities = {
|
475
|
-
"<" => "<",
|
476
|
-
">" => ">",
|
477
|
-
'"' => """,
|
478
|
-
"'" => "'",
|
479
|
-
"&" => "&"
|
480
|
-
}
|
481
|
-
end
|
482
|
-
|
483
|
-
context "in content" do
|
484
|
-
it "returns unescaped XML entities" do
|
485
|
-
@xml_entities.each do |key, value|
|
486
|
-
xml = "<tag>#{value}</tag>"
|
487
|
-
expect(MultiXml.parse(xml)['tag']).to eq(key)
|
488
|
-
end
|
489
|
-
end
|
490
|
-
end
|
491
|
-
|
492
|
-
context "in attribute" do
|
493
|
-
it "returns unescaped XML entities" do
|
494
|
-
@xml_entities.each do |key, value|
|
495
|
-
xml = "<tag attribute=\"#{value}\"/>"
|
496
|
-
expect(MultiXml.parse(xml)['tag']['attribute']).to eq(key)
|
497
|
-
end
|
498
|
-
end
|
499
|
-
end
|
500
|
-
end
|
501
|
-
|
502
|
-
|
503
|
-
context "with dasherized tag" do
|
504
|
-
before do
|
505
|
-
@xml = '<tag-1/>'
|
506
|
-
end
|
507
|
-
|
508
|
-
it "returns undasherize tag" do
|
509
|
-
expect(MultiXml.parse(@xml).keys).to include('tag_1')
|
510
|
-
end
|
511
|
-
end
|
512
|
-
|
513
|
-
context "with dasherized attribute" do
|
514
|
-
before do
|
515
|
-
@xml = '<tag attribute-1="1"></tag>'
|
516
|
-
end
|
517
|
-
|
518
|
-
it "returns undasherize attribute" do
|
519
|
-
expect(MultiXml.parse(@xml)['tag'].keys).to include('attribute_1')
|
520
|
-
end
|
521
|
-
end
|
522
|
-
|
523
|
-
context "with children" do
|
524
|
-
context "with attributes" do
|
525
|
-
before do
|
526
|
-
@xml = '<users><user name="Erik Michaels-Ober"/></users>'
|
527
|
-
end
|
528
|
-
|
529
|
-
it "returns the correct attributes" do
|
530
|
-
expect(MultiXml.parse(@xml)['users']['user']['name']).to eq("Erik Michaels-Ober")
|
531
|
-
end
|
532
|
-
end
|
533
|
-
|
534
|
-
context "with text" do
|
535
|
-
before do
|
536
|
-
@xml = '<user><name>Erik Michaels-Ober</name></user>'
|
537
|
-
end
|
538
|
-
|
539
|
-
it "returns the correct text" do
|
540
|
-
expect(MultiXml.parse(@xml)['user']['name']).to eq("Erik Michaels-Ober")
|
541
|
-
end
|
542
|
-
end
|
543
|
-
|
544
|
-
context "with an unrecognized attribute type" do
|
545
|
-
before do
|
546
|
-
@xml = '<user type="admin"><name>Erik Michaels-Ober</name></user>'
|
547
|
-
end
|
548
|
-
|
549
|
-
it "passes through the type" do
|
550
|
-
expect(MultiXml.parse(@xml)['user']['type']).to eq('admin')
|
551
|
-
end
|
552
|
-
end
|
553
|
-
|
554
|
-
context "with attribute tags on content nodes" do
|
555
|
-
context "non 'type' attributes" do
|
556
|
-
before do
|
557
|
-
@xml = <<-XML
|
558
|
-
<options>
|
559
|
-
<value currency='USD'>123</value>
|
560
|
-
<value number='percent'>0.123</value>
|
561
|
-
</options>
|
562
|
-
XML
|
563
|
-
@parsed_xml = MultiXml.parse(@xml)
|
564
|
-
end
|
565
|
-
|
566
|
-
it "adds the attributes to the value hash" do
|
567
|
-
expect(@parsed_xml['options']['value'][0]['__content__']).to eq('123')
|
568
|
-
expect(@parsed_xml['options']['value'][0]['currency']).to eq('USD')
|
569
|
-
expect(@parsed_xml['options']['value'][1]['__content__']).to eq('0.123')
|
570
|
-
expect(@parsed_xml['options']['value'][1]['number']).to eq('percent')
|
571
|
-
end
|
572
|
-
end
|
573
|
-
|
574
|
-
context "unrecognized type attributes" do
|
575
|
-
before do
|
576
|
-
@xml = <<-XML
|
577
|
-
<options>
|
578
|
-
<value type='USD'>123</value>
|
579
|
-
<value type='percent'>0.123</value>
|
580
|
-
<value currency='USD'>123</value>
|
581
|
-
</options>
|
582
|
-
XML
|
583
|
-
@parsed_xml = MultiXml.parse(@xml)
|
584
|
-
end
|
585
|
-
|
586
|
-
it "adds the attributes to the value hash passing through the type" do
|
587
|
-
expect(@parsed_xml['options']['value'][0]['__content__']).to eq('123')
|
588
|
-
expect(@parsed_xml['options']['value'][0]['type']).to eq('USD')
|
589
|
-
expect(@parsed_xml['options']['value'][1]['__content__']).to eq('0.123')
|
590
|
-
expect(@parsed_xml['options']['value'][1]['type']).to eq('percent')
|
591
|
-
expect(@parsed_xml['options']['value'][2]['__content__']).to eq('123')
|
592
|
-
expect(@parsed_xml['options']['value'][2]['currency']).to eq('USD')
|
593
|
-
end
|
594
|
-
end
|
595
|
-
|
596
|
-
context "mixing attributes and non-attributes content nodes type attributes" do
|
597
|
-
before do
|
598
|
-
@xml = <<-XML
|
599
|
-
<options>
|
600
|
-
<value type='USD'>123</value>
|
601
|
-
<value type='percent'>0.123</value>
|
602
|
-
<value>123</value>
|
603
|
-
</options>
|
604
|
-
XML
|
605
|
-
@parsed_xml = MultiXml.parse(@xml)
|
606
|
-
end
|
607
|
-
|
608
|
-
it "adds the attributes to the value hash passing through the type" do
|
609
|
-
expect(@parsed_xml['options']['value'][0]['__content__']).to eq('123')
|
610
|
-
expect(@parsed_xml['options']['value'][0]['type']).to eq('USD')
|
611
|
-
expect(@parsed_xml['options']['value'][1]['__content__']).to eq('0.123')
|
612
|
-
expect(@parsed_xml['options']['value'][1]['type']).to eq('percent')
|
613
|
-
expect(@parsed_xml['options']['value'][2]).to eq('123')
|
614
|
-
end
|
615
|
-
end
|
616
|
-
|
617
|
-
context "mixing recognized type attribute and non-type attributes on content nodes" do
|
618
|
-
before do
|
619
|
-
@xml = <<-XML
|
620
|
-
<options>
|
621
|
-
<value number='USD' type='integer'>123</value>
|
622
|
-
</options>
|
623
|
-
XML
|
624
|
-
@parsed_xml = MultiXml.parse(@xml)
|
625
|
-
end
|
626
|
-
|
627
|
-
it "adds the the non-type attribute and remove the recognized type attribute and do the typecast" do
|
628
|
-
expect(@parsed_xml['options']['value']['__content__']).to eq(123)
|
629
|
-
expect(@parsed_xml['options']['value']['number']).to eq('USD')
|
630
|
-
end
|
631
|
-
end
|
632
|
-
|
633
|
-
context "mixing unrecognized type attribute and non-type attributes on content nodes" do
|
634
|
-
before do
|
635
|
-
@xml = <<-XML
|
636
|
-
<options>
|
637
|
-
<value number='USD' type='currency'>123</value>
|
638
|
-
</options>
|
639
|
-
XML
|
640
|
-
@parsed_xml = MultiXml.parse(@xml)
|
641
|
-
end
|
642
|
-
|
643
|
-
it "adds the the non-type attributes and type attribute to the value hash" do
|
644
|
-
expect(@parsed_xml['options']['value']['__content__']).to eq('123')
|
645
|
-
expect(@parsed_xml['options']['value']['number']).to eq('USD')
|
646
|
-
expect(@parsed_xml['options']['value']['type']).to eq('currency')
|
647
|
-
end
|
648
|
-
end
|
649
|
-
end
|
650
|
-
|
651
|
-
context "with newlines and whitespace" do
|
652
|
-
before do
|
653
|
-
@xml = <<-XML
|
654
|
-
<user>
|
655
|
-
<name>Erik Michaels-Ober</name>
|
656
|
-
</user>
|
657
|
-
XML
|
658
|
-
end
|
659
|
-
|
660
|
-
it "parses correctly" do
|
661
|
-
expect(MultiXml.parse(@xml)).to eq({"user" => {"name" => "Erik Michaels-Ober"}})
|
662
|
-
end
|
663
|
-
end
|
664
|
-
|
665
|
-
# Babies having babies
|
666
|
-
context "with children" do
|
667
|
-
before do
|
668
|
-
@xml = '<users><user name="Erik Michaels-Ober"><status text="Hello"/></user></users>'
|
669
|
-
end
|
670
|
-
|
671
|
-
it "parses correctly" do
|
672
|
-
expect(MultiXml.parse(@xml)).to eq({"users" => {"user" => {"name" => "Erik Michaels-Ober", "status" => {"text" => "Hello"}}}})
|
673
|
-
end
|
674
|
-
end
|
675
|
-
end
|
676
|
-
|
677
|
-
context "with sibling children" do
|
678
|
-
before do
|
679
|
-
@xml = '<users><user>Erik Michaels-Ober</user><user>Wynn Netherland</user></users>'
|
680
|
-
end
|
681
|
-
|
682
|
-
it "returns an Array" do
|
683
|
-
expect(MultiXml.parse(@xml)['users']['user']).to be_a(Array)
|
684
|
-
end
|
685
|
-
|
686
|
-
it "parses correctly" do
|
687
|
-
expect(MultiXml.parse(@xml)).to eq({"users" => {"user" => ["Erik Michaels-Ober", "Wynn Netherland"]}})
|
688
|
-
end
|
689
|
-
end
|
690
|
-
|
691
|
-
end
|
692
|
-
end
|
693
|
-
|
694
|
-
end
|