File: parser_shared_example.rb

package info (click to toggle)
ruby-multi-xml 0.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 232 kB
  • sloc: ruby: 1,203; sh: 4; makefile: 2
file content (724 lines) | stat: -rw-r--r-- 21,920 bytes parent folder | download
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
shared_examples_for "a parser" do |parser|
  before do
    MultiXml.parser = parser

    LibXML::XML::Error.set_handler(&LibXML::XML::Error::QUIET_HANDLER) if parser == "LibXML"
  rescue LoadError
    pending "Parser #{parser} couldn't be loaded"
  end

  describe ".parse" do
    context "a blank string" do
      before do
        @xml = ""
      end

      it "returns an empty Hash" do
        expect(MultiXml.parse(@xml)).to eq({})
      end
    end

    context "a whitespace string" do
      before do
        @xml = " "
      end

      it "returns an empty Hash" do
        expect(MultiXml.parse(@xml)).to eq({})
      end
    end

    context "a frozen string" do
      before do
        @xml = " "
      end

      it "returns an empty Hash" do
        expect(MultiXml.parse(@xml)).to eq({})
      end
    end

    unless parser == "Oga"
      context "an invalid XML document" do
        before do
          @xml = "<open></close>"
        end

        it "raises MultiXml::ParseError" do
          expect { MultiXml.parse(@xml) }.to raise_error(MultiXml::ParseError)
        end
      end
    end

    context "a valid XML document" do
      before do
        @xml = "<user/>"
      end

      it "parses correctly" do
        expect(MultiXml.parse(@xml)).to eq("user" => nil)
      end

      context "with CDATA" do
        before do
          @xml = "<user><![CDATA[Erik Berlin]]></user>"
        end

        it "returns the correct CDATA" do
          expect(MultiXml.parse(@xml)["user"]).to eq("Erik Berlin")
        end
      end

      context "element with the same inner element and attribute name" do
        before do
          @xml = "<user name='John'><name>Smith</name></user>"
        end

        it "returns names as Array" do
          expect(MultiXml.parse(@xml)["user"]["name"]).to eq %w[John Smith]
        end
      end

      context "with content" do
        before do
          @xml = "<user>Erik Berlin</user>"
        end

        it "returns the correct content" do
          expect(MultiXml.parse(@xml)["user"]).to eq("Erik Berlin")
        end
      end

      context "with an attribute" do
        before do
          @xml = '<user name="Erik Berlin"/>'
        end

        it "returns the correct attribute" do
          expect(MultiXml.parse(@xml)["user"]["name"]).to eq("Erik Berlin")
        end
      end

      context "with multiple attributes" do
        before do
          @xml = '<user name="Erik Berlin" screen_name="sferik"/>'
        end

        it "returns the correct attributes" do
          expect(MultiXml.parse(@xml)["user"]["name"]).to eq("Erik Berlin")
          expect(MultiXml.parse(@xml)["user"]["screen_name"]).to eq("sferik")
        end
      end

      context "typecast management" do
        before do
          @xml = %(
            <global-settings>
              <group>
                <name>Settings</name>
                <setting type="string">
                  <description>Test</description>
                </setting>
              </group>
            </global-settings>
          )
        end

        context "with :typecast_xml_value => true" do
          before do
            @setting = MultiXml.parse(@xml)["global_settings"]["group"]["setting"]
          end

          it { expect(@setting).to eq "" }
        end

        context "with :typecast_xml_value => false" do
          before do
            @setting = MultiXml.parse(@xml, typecast_xml_value: false)["global_settings"]["group"]["setting"]
          end

          it { expect(@setting).to eq("type" => "string", "description" => {"__content__" => "Test"}) }
        end
      end

      context "with :symbolize_keys => true" do
        before do
          @xml = '<users><user name="Erik Berlin"/><user><name>Wynn Netherland</name></user></users>'
        end

        it "symbolizes keys" do
          expect(MultiXml.parse(@xml,
            symbolize_keys: true)).to eq(users: {user: [{name: "Erik Berlin"},
                                                        {name: "Wynn Netherland"}]})
        end
      end

      context 'with an attribute type="boolean"' do
        %w[true false].each do |boolean|
          context "when #{boolean}" do
            it "returns #{boolean}" do
              xml = "<tag type=\"boolean\">#{boolean}</tag>"
              expect(MultiXml.parse(xml)["tag"]).to be instance_eval(boolean)
            end
          end
        end

        context "when 1" do
          before do
            @xml = '<tag type="boolean">1</tag>'
          end

          it "returns true" do
            expect(MultiXml.parse(@xml)["tag"]).to be true
          end
        end

        context "when 0" do
          before do
            @xml = '<tag type="boolean">0</tag>'
          end

          it "returns false" do
            expect(MultiXml.parse(@xml)["tag"]).to be false
          end
        end
      end

      context 'with an attribute type="integer"' do
        context "with a positive integer" do
          before do
            @xml = '<tag type="integer">1</tag>'
          end

          it "returns a Integer" do
            expect(MultiXml.parse(@xml)["tag"]).to be_a(Integer)
          end

          it "returns a positive number" do
            expect(MultiXml.parse(@xml)["tag"]).to be > 0
          end

          it "returns the correct number" do
            expect(MultiXml.parse(@xml)["tag"]).to eq(1)
          end
        end

        context "with a negative integer" do
          before do
            @xml = '<tag type="integer">-1</tag>'
          end

          it "returns a Integer" do
            expect(MultiXml.parse(@xml)["tag"]).to be_a(Integer)
          end

          it "returns a negative number" do
            expect(MultiXml.parse(@xml)["tag"]).to be < 0
          end

          it "returns the correct number" do
            expect(MultiXml.parse(@xml)["tag"]).to eq(-1)
          end
        end
      end

      context 'with an attribute type="string"' do
        before do
          @xml = '<tag type="string"></tag>'
        end

        it "returns a String" do
          expect(MultiXml.parse(@xml)["tag"]).to be_a(String)
        end

        it "returns the correct string" do
          expect(MultiXml.parse(@xml)["tag"]).to eq("")
        end
      end

      context 'with an attribute type="date"' do
        before do
          @xml = '<tag type="date">1970-01-01</tag>'
        end

        it "returns a Date" do
          expect(MultiXml.parse(@xml)["tag"]).to be_a(Date)
        end

        it "returns the correct date" do
          expect(MultiXml.parse(@xml)["tag"]).to eq(Date.parse("1970-01-01"))
        end
      end

      context 'with an attribute type="datetime"' do
        before do
          @xml = '<tag type="datetime">1970-01-01 00:00</tag>'
        end

        it "returns a Time" do
          expect(MultiXml.parse(@xml)["tag"]).to be_a(Time)
        end

        it "returns the correct time" do
          expect(MultiXml.parse(@xml)["tag"]).to eq(Time.parse("1970-01-01 00:00"))
        end
      end

      context 'with an attribute type="dateTime"' do
        before do
          @xml = '<tag type="datetime">1970-01-01 00:00</tag>'
        end

        it "returns a Time" do
          expect(MultiXml.parse(@xml)["tag"]).to be_a(Time)
        end

        it "returns the correct time" do
          expect(MultiXml.parse(@xml)["tag"]).to eq(Time.parse("1970-01-01 00:00"))
        end
      end

      context 'with an attribute type="double"' do
        before do
          @xml = '<tag type="double">3.14159265358979</tag>'
        end

        it "returns a Float" do
          expect(MultiXml.parse(@xml)["tag"]).to be_a(Float)
        end

        it "returns the correct number" do
          expect(MultiXml.parse(@xml)["tag"]).to eq(3.14159265358979)
        end
      end

      context 'with an attribute type="decimal"' do
        before do
          @xml = '<tag type="decimal">3.14159265358979</tag>'
        end

        it "returns a BigDecimal" do
          expect(MultiXml.parse(@xml)["tag"]).to be_a(BigDecimal)
        end

        it "returns the correct number" do
          expect(MultiXml.parse(@xml)["tag"]).to eq(3.14159265358979)
        end
      end

      context 'with an attribute type="base64Binary"' do
        before do
          @xml = '<tag type="base64Binary">aW1hZ2UucG5n</tag>'
        end

        it "returns a String" do
          expect(MultiXml.parse(@xml)["tag"]).to be_a(String)
        end

        it "returns the correct string" do
          expect(MultiXml.parse(@xml)["tag"]).to eq("image.png")
        end
      end

      context 'with an attribute type="yaml"' do
        before do
          @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>"
        end

        it "raises MultiXML::DisallowedTypeError by default" do
          expect { MultiXml.parse(@xml)["tag"] }.to raise_error(MultiXml::DisallowedTypeError)
        end

        it "returns the correctly parsed YAML when the type is allowed" do
          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}])
        end
      end

      context 'with an attribute type="symbol"' do
        before do
          @xml = '<tag type="symbol">my_symbol</tag>'
        end

        it "raises MultiXML::DisallowedTypeError" do
          expect { MultiXml.parse(@xml)["tag"] }.to raise_error(MultiXml::DisallowedTypeError)
        end

        it "returns the correctly parsed Symbol when the type is allowed" do
          expect(MultiXml.parse(@xml, disallowed_types: [])["tag"]).to eq(:my_symbol)
        end
      end

      context 'with an attribute type="file"' do
        before do
          @xml = '<tag type="file" name="data.txt" content_type="text/plain">ZGF0YQ==</tag>'
        end

        it "returns a StringIO" do
          expect(MultiXml.parse(@xml)["tag"]).to be_a(StringIO)
        end

        it "is decoded correctly" do
          expect(MultiXml.parse(@xml)["tag"].string).to eq("data")
        end

        it "has the correct file name" do
          expect(MultiXml.parse(@xml)["tag"].original_filename).to eq("data.txt")
        end

        it "has the correct content type" do
          expect(MultiXml.parse(@xml)["tag"].content_type).to eq("text/plain")
        end

        context "with missing name and content type" do
          before do
            @xml = '<tag type="file">ZGF0YQ==</tag>'
          end

          it "returns a StringIO" do
            expect(MultiXml.parse(@xml)["tag"]).to be_a(StringIO)
          end

          it "is decoded correctly" do
            expect(MultiXml.parse(@xml)["tag"].string).to eq("data")
          end

          it "has the default file name" do
            expect(MultiXml.parse(@xml)["tag"].original_filename).to eq("untitled")
          end

          it "has the default content type" do
            expect(MultiXml.parse(@xml)["tag"].content_type).to eq("application/octet-stream")
          end
        end
      end

      context 'with an attribute type="array"' do
        before do
          @xml = '<users type="array"><user>Erik Berlin</user><user>Wynn Netherland</user></users>'
        end

        it "returns an Array" do
          expect(MultiXml.parse(@xml)["users"]).to be_a(Array)
        end

        it "returns the correct array" do
          expect(MultiXml.parse(@xml)["users"]).to eq(["Erik Berlin", "Wynn Netherland"])
        end
      end

      context 'with an attribute type="array" in addition to other attributes' do
        before do
          @xml = '<users type="array" foo="bar"><user>Erik Berlin</user><user>Wynn Netherland</user></users>'
        end

        it "returns an Array" do
          expect(MultiXml.parse(@xml)["users"]).to be_a(Array)
        end

        it "returns the correct array" do
          expect(MultiXml.parse(@xml)["users"]).to eq(["Erik Berlin", "Wynn Netherland"])
        end
      end

      context 'with an attribute type="array" containing only one item' do
        before do
          @xml = '<users type="array"><user>Erik Berlin</user></users>'
        end

        it "returns an Array" do
          expect(MultiXml.parse(@xml)["users"]).to be_a(Array)
        end

        it "returns the correct array" do
          expect(MultiXml.parse(@xml)["users"]).to eq(["Erik Berlin"])
        end
      end

      %w[integer boolean date datetime file].each do |type|
        context "with an empty attribute type=\"#{type}\"" do
          before do
            @xml = "<tag type=\"#{type}\"/>"
          end

          it "returns nil" do
            expect(MultiXml.parse(@xml)["tag"]).to be_nil
          end
        end
      end

      %w[yaml symbol].each do |type|
        context "with an empty attribute type=\"#{type}\"" do
          before do
            @xml = "<tag type=\"#{type}\"/>"
          end

          it "raises MultiXml::DisallowedTypeError by default" do
            expect { MultiXml.parse(@xml)["tag"] }.to raise_error(MultiXml::DisallowedTypeError)
          end

          it "returns nil when the type is allowed" do
            expect(MultiXml.parse(@xml, disallowed_types: [])["tag"]).to be_nil
          end
        end
      end

      context 'with an empty attribute type="array"' do
        before do
          @xml = '<tag type="array"/>'
        end

        it "returns an empty Array" do
          expect(MultiXml.parse(@xml)["tag"]).to eq([])
        end

        context "with whitespace" do
          before do
            @xml = '<tag type="array"> </tag>'
          end

          it "returns an empty Array" do
            expect(MultiXml.parse(@xml)["tag"]).to eq([])
          end
        end
      end

      context "with XML entities" do
        before do
          @xml_entities = {
            "<" => "&lt;",
            ">" => "&gt;",
            '"' => "&quot;",
            "'" => "&apos;",
            "&" => "&amp;"
          }
        end

        context "in content" do
          it "returns unescaped XML entities" do
            @xml_entities.each do |key, value|
              xml = "<tag>#{value}</tag>"
              expect(MultiXml.parse(xml)["tag"]).to eq(key)
            end
          end
        end

        context "in attribute" do
          it "returns unescaped XML entities" do
            @xml_entities.each do |key, value|
              xml = "<tag attribute=\"#{value}\"/>"
              expect(MultiXml.parse(xml)["tag"]["attribute"]).to eq(key)
            end
          end
        end
      end

      context "with dasherized tag" do
        before do
          @xml = "<tag-1/>"
        end

        it "returns undasherize tag" do
          expect(MultiXml.parse(@xml).keys).to include("tag_1")
        end
      end

      context "with dasherized attribute" do
        before do
          @xml = '<tag attribute-1="1"></tag>'
        end

        it "returns undasherize attribute" do
          expect(MultiXml.parse(@xml)["tag"].keys).to include("attribute_1")
        end
      end

      context "with children" do
        context "with attributes" do
          before do
            @xml = '<users><user name="Erik Berlin"/></users>'
          end

          it "returns the correct attributes" do
            expect(MultiXml.parse(@xml)["users"]["user"]["name"]).to eq("Erik Berlin")
          end
        end

        context "with text" do
          before do
            @xml = "<user><name>Erik Berlin</name></user>"
          end

          it "returns the correct text" do
            expect(MultiXml.parse(@xml)["user"]["name"]).to eq("Erik Berlin")
          end
        end

        context "with an unrecognized attribute type" do
          before do
            @xml = '<user type="admin"><name>Erik Berlin</name></user>'
          end

          it "passes through the type" do
            expect(MultiXml.parse(@xml)["user"]["type"]).to eq("admin")
          end
        end

        context "with attribute tags on content nodes" do
          context "non 'type' attributes" do
            before do
              @xml = <<-XML
                <options>
                  <value currency='USD'>123</value>
                  <value number='percent'>0.123</value>
                </options>
              XML
              @parsed_xml = MultiXml.parse(@xml)
            end

            it "adds the attributes to the value hash" do
              expect(@parsed_xml["options"]["value"][0]["__content__"]).to eq("123")
              expect(@parsed_xml["options"]["value"][0]["currency"]).to eq("USD")
              expect(@parsed_xml["options"]["value"][1]["__content__"]).to eq("0.123")
              expect(@parsed_xml["options"]["value"][1]["number"]).to eq("percent")
            end
          end

          context "unrecognized type attributes" do
            before do
              @xml = <<-XML
                <options>
                  <value type='USD'>123</value>
                  <value type='percent'>0.123</value>
                  <value currency='USD'>123</value>
                </options>
              XML
              @parsed_xml = MultiXml.parse(@xml)
            end

            it "adds the attributes to the value hash passing through the type" do
              expect(@parsed_xml["options"]["value"][0]["__content__"]).to eq("123")
              expect(@parsed_xml["options"]["value"][0]["type"]).to eq("USD")
              expect(@parsed_xml["options"]["value"][1]["__content__"]).to eq("0.123")
              expect(@parsed_xml["options"]["value"][1]["type"]).to eq("percent")
              expect(@parsed_xml["options"]["value"][2]["__content__"]).to eq("123")
              expect(@parsed_xml["options"]["value"][2]["currency"]).to eq("USD")
            end
          end

          context "mixing attributes and non-attributes content nodes type attributes" do
            before do
              @xml = <<-XML
                <options>
                  <value type='USD'>123</value>
                  <value type='percent'>0.123</value>
                  <value>123</value>
                </options>
              XML
              @parsed_xml = MultiXml.parse(@xml)
            end

            it "adds the attributes to the value hash passing through the type" do
              expect(@parsed_xml["options"]["value"][0]["__content__"]).to eq("123")
              expect(@parsed_xml["options"]["value"][0]["type"]).to eq("USD")
              expect(@parsed_xml["options"]["value"][1]["__content__"]).to eq("0.123")
              expect(@parsed_xml["options"]["value"][1]["type"]).to eq("percent")
              expect(@parsed_xml["options"]["value"][2]).to eq("123")
            end
          end

          context "mixing recognized type attribute and non-type attributes on content nodes" do
            before do
              @xml = <<-XML
                <options>
                  <value number='USD' type='integer'>123</value>
                </options>
              XML
              @parsed_xml = MultiXml.parse(@xml)
            end

            it "adds the the non-type attribute and remove the recognized type attribute and do the typecast" do
              expect(@parsed_xml["options"]["value"]["__content__"]).to eq(123)
              expect(@parsed_xml["options"]["value"]["number"]).to eq("USD")
            end
          end

          context "mixing unrecognized type attribute and non-type attributes on content nodes" do
            before do
              @xml = <<-XML
                <options>
                  <value number='USD' type='currency'>123</value>
                </options>
              XML
              @parsed_xml = MultiXml.parse(@xml)
            end

            it "adds the the non-type attributes and type attribute to the value hash" do
              expect(@parsed_xml["options"]["value"]["__content__"]).to eq("123")
              expect(@parsed_xml["options"]["value"]["number"]).to eq("USD")
              expect(@parsed_xml["options"]["value"]["type"]).to eq("currency")
            end
          end
        end

        context "with newlines and whitespace" do
          before do
            @xml = <<-XML
              <user>
                <name>Erik Berlin</name>
              </user>
            XML
          end

          it "parses correctly" do
            expect(MultiXml.parse(@xml)).to eq("user" => {"name" => "Erik Berlin"})
          end
        end

        # Babies having babies
        context "with children" do
          before do
            @xml = '<users><user name="Erik Berlin"><status text="Hello"/></user></users>'
          end

          it "parses correctly" do
            expect(MultiXml.parse(@xml)).to eq("users" => {"user" => {"name" => "Erik Berlin",
                                                                      "status" => {"text" => "Hello"}}})
          end
        end
      end

      context "with sibling children" do
        before do
          @xml = "<users><user>Erik Berlin</user><user>Wynn Netherland</user></users>"
        end

        it "returns an Array" do
          expect(MultiXml.parse(@xml)["users"]["user"]).to be_a(Array)
        end

        it "parses correctly" do
          expect(MultiXml.parse(@xml)).to eq("users" => {"user" => ["Erik Berlin", "Wynn Netherland"]})
        end
      end
    end

    context "a duplexed stream" do
      before do
        @xml, wr = IO.pipe

        Thread.new do
          "<user/>".each_char do |chunk|
            wr << chunk
          end
          wr.close
        end
      end

      it "parses correctly" do
        expect(MultiXml.parse(@xml)).to eq("user" => nil)
      end
    end
  end
end