yadtfp 1.0.2

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.
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Yadtfp::OutputtersFactory do
4
+
5
+ describe '.create' do
6
+
7
+ it 'when :pretty' do
8
+ expect(Yadtfp::OutputtersFactory.create(:pretty, [])).to be_kind_of(Yadtfp::Outputters::Pretty)
9
+ end
10
+
11
+
12
+
13
+
14
+ it 'when invalid outputter' do
15
+ expect{ Yadtfp::OutputtersFactory.create('invalid', []) }.to raise_error(ArgumentError)
16
+ end
17
+
18
+ end
19
+
20
+ end
@@ -0,0 +1,966 @@
1
+ require 'spec_helper'
2
+
3
+ describe Yadtfp::Parsers::Ox do
4
+
5
+ let(:parser) { Yadtfp::Parsers::Ox.new }
6
+
7
+
8
+
9
+
10
+ describe '#parse' do
11
+
12
+ it 'when xml string is supplied' do
13
+ xml = %Q{ <xml /> }
14
+ expect(parser.parse(xml)).to be_kind_of(::Ox::Document)
15
+ end
16
+
17
+
18
+
19
+ it 'when file is supplied' do
20
+ xml = %Q{ <xml /> }
21
+
22
+ file = double('dummy')
23
+ allow(file).to receive(:empty?) { false }
24
+ allow(File).to receive(:exists?) { true }
25
+ allow(::Ox).to receive(:load_file) { xml }
26
+
27
+ expect(parser.parse('dummy.xml')).to be_kind_of(::Ox::Document)
28
+ end
29
+
30
+
31
+
32
+ it 'when inexistent file is supplied' do
33
+ allow(File).to receive(:exists?) { true }
34
+ expect { parser.parse('dummy.xml') }.to raise_error(IOError)
35
+ end
36
+
37
+
38
+
39
+
40
+ it 'when nil is supplied' do
41
+ expect(parser.parse(nil)).to be_kind_of(::Ox::Document)
42
+ end
43
+
44
+
45
+
46
+
47
+ it 'when empty string is supplied' do
48
+ expect(parser.parse("")).to be_kind_of(::Ox::Document)
49
+ end
50
+
51
+
52
+
53
+
54
+ it 'when invalid xml is supplied' do
55
+ expect { parser.parse('invalid_xml') }.to raise_error(::Ox::ParseError)
56
+ end
57
+
58
+ end
59
+
60
+
61
+
62
+
63
+
64
+
65
+ describe '#filter' do
66
+
67
+
68
+ let(:config) { double('Yadtfp::Configuration.instance') }
69
+
70
+ before do
71
+ allow(config).to receive('filter') { '*' }
72
+ end
73
+
74
+
75
+
76
+
77
+ it 'when document is nil' do
78
+ expect { parser.filter(nil) }.to raise_error ArgumentError
79
+ end
80
+
81
+
82
+
83
+
84
+
85
+ it 'when filter is default' do
86
+ doc = ox_parse_doc %Q{ <xml /> }
87
+ result = [ ::Ox::Element.new('xml') ]
88
+
89
+ expect(parser.filter(doc)).to eq result
90
+ end
91
+
92
+
93
+
94
+
95
+
96
+ it 'when * for single empty node' do
97
+ doc = ox_parse_doc %Q{ <xml /> }
98
+ result = [ ::Ox::Element.new('xml') ]
99
+
100
+ expect(parser.filter(doc)).to eq result
101
+ end
102
+
103
+
104
+
105
+
106
+
107
+ it 'when * for multiple empty nodes' do
108
+ doc = ox_parse_doc %Q{ <xml /><xml /> }
109
+ result = Array.new(2) { ::Ox::Element.new('xml') }
110
+
111
+ expect(parser.filter(doc)).to eq result
112
+ end
113
+
114
+
115
+
116
+
117
+
118
+
119
+ it 'when * for nodes with attributes' do
120
+ doc = ox_parse_doc %Q{ <xml id='root' /> }
121
+ result = Array.new(1) do
122
+ elem = ::Ox::Element.new('xml')
123
+ elem[:id] = 'root'
124
+ elem
125
+ end
126
+
127
+ expect(parser.filter(doc, "*")).to match_array(result)
128
+ end
129
+
130
+
131
+
132
+
133
+
134
+ it 'when * for nodes with attributes and comments' do
135
+ doc = ox_parse_doc %Q{ <xml id='root'><!-- Root node -->Foo</xml> }
136
+ result = Array.new(1) do
137
+ elem = ::Ox::Element.new('xml')
138
+ elem[:id] = 'root'
139
+ elem << ::Ox::Comment.new('Root node')
140
+ elem << "Foo"
141
+ end
142
+
143
+ expect(parser.filter(doc, "*")).to eq(result)
144
+ end
145
+
146
+
147
+
148
+
149
+
150
+ it 'when * for nodes with attributes, comments and cdata' do
151
+ doc = ox_parse_doc %Q{ <xml id='root'><!-- Root node --><![CDATA[Foo]]>Bar</xml> }
152
+ result = Array.new(1) do
153
+ elem = ::Ox::Element.new('xml')
154
+ elem[:id] = 'root'
155
+ elem << ::Ox::Comment.new('Root node')
156
+ elem << ::Ox::CData.new('Foo')
157
+ elem << "Bar"
158
+ end
159
+
160
+ expect(parser.filter(doc, "*")).to eq(result)
161
+ end
162
+
163
+ end
164
+
165
+
166
+
167
+
168
+
169
+
170
+
171
+
172
+ describe '#diff' do
173
+
174
+
175
+ it 'when left and right are nil' do
176
+ expect(parser.diff(nil, nil)).to eq []
177
+ end
178
+
179
+
180
+
181
+
182
+
183
+ it 'when left and right are empty documents' do
184
+ left = right = ox_parse_doc('')
185
+
186
+ expect(parser.diff(left, right)).to eq []
187
+ end
188
+
189
+
190
+
191
+
192
+
193
+ it 'when left is nil' do
194
+ right = ox_parse_doc('')
195
+
196
+ diff = right.attributes.values
197
+ expect(parser.diff(nil, right)).to eq diff
198
+ end
199
+
200
+
201
+
202
+
203
+
204
+ it 'when right is nil' do
205
+ left = ox_parse_doc('')
206
+
207
+ diff = left.attributes.values
208
+ expect(parser.diff(left, nil)).to eq diff
209
+ end
210
+
211
+
212
+
213
+
214
+
215
+ it 'when left and right have different attributes' do
216
+ left = ox_parse_doc %Q{ <xml id='root' /> }
217
+ right = ox_parse_doc %Q{ <xml name='root_node' /> }
218
+
219
+ diff = [
220
+ { type: 'd', path: '/xml/@id', lvalue: 'root', rvalue: nil },
221
+ { type: 'a', path: '/xml/@name', lvalue: nil, rvalue: 'root_node' }
222
+ ]
223
+ expect(parser.diff(left, right)).to eq diff
224
+ end
225
+
226
+
227
+
228
+
229
+
230
+ it 'when left has children and right does not' do
231
+ left_xml = %{
232
+ <xml>
233
+ <node>Child</node>
234
+ </xml>
235
+ }
236
+ left = ox_parse_doc(left_xml)
237
+ right = ox_parse_doc("<xml />")
238
+
239
+ diff = [
240
+ { type: 'd', path: '/xml/node', lvalue: 'Child', rvalue: nil }
241
+ ]
242
+
243
+ expect(parser.diff(left, right)).to eq diff
244
+ end
245
+
246
+
247
+
248
+
249
+
250
+
251
+ it 'when right has children and left does not' do
252
+ right_xml = %{
253
+ <xml>
254
+ <node>Child</node>
255
+ </xml>
256
+ }
257
+ left = ox_parse_doc('<xml />')
258
+ right = ox_parse_doc(right_xml)
259
+
260
+ diff = [
261
+ { type: 'a', path: '/xml/node', lvalue: nil, rvalue: 'Child' }
262
+ ]
263
+
264
+ expect(parser.diff(left, right)).to eq diff
265
+
266
+ end
267
+
268
+
269
+
270
+
271
+
272
+
273
+ it 'when left has more number of children than right' do
274
+ left = ox_parse_doc %{
275
+ <xml>
276
+ <node>First</node>
277
+ <node>Second</node>
278
+ </xml>
279
+ }
280
+ right = ox_parse_doc %{
281
+ <xml>
282
+ <node>Right first</node>
283
+ </xml>
284
+ }
285
+
286
+
287
+ diff = [
288
+ { type: 'c', path: '/xml/node', lvalue: [ 'First', 'Second' ], rvalue: 'Right first' },
289
+ ]
290
+
291
+ expect(parser.diff(left, right)).to eq diff
292
+ end
293
+
294
+
295
+
296
+
297
+
298
+ it 'when left has same number of children as right' do
299
+ left = ox_parse_doc %{
300
+ <xml>
301
+ <node>First</node>
302
+ <node>Second</node>
303
+ </xml>
304
+ }
305
+ right = ox_parse_doc %{
306
+ <xml>
307
+ <node>Right first</node>
308
+ <node>Right second</node>
309
+ </xml>
310
+ }
311
+
312
+
313
+ diff = [
314
+ { type: 'c', path: '/xml/node', lvalue: [ 'First', 'Second' ], rvalue: [ 'Right first', 'Right second' ] }
315
+ ]
316
+
317
+ expect(parser.diff(left, right)).to eq diff
318
+ end
319
+
320
+
321
+
322
+
323
+
324
+ it 'when left has same number of same text children as right' do
325
+ left = ox_parse_doc %{
326
+ <xml>
327
+ <node>First</node>
328
+ <node>Second</node>
329
+ </xml>
330
+ }
331
+ right = ox_parse_doc %{
332
+ <xml>
333
+ <node>First</node>
334
+ <node>Second</node>
335
+ </xml>
336
+ }
337
+
338
+
339
+ diff = []
340
+
341
+ expect(parser.diff(left, right)).to eq diff
342
+ end
343
+
344
+
345
+
346
+
347
+
348
+ it 'when left has children with different comments and text than right' do
349
+ left = ox_parse_doc %Q{
350
+ <xml>
351
+ <node><![CDATA[Foo]]><!-- Foo -->First child</node>
352
+ </xml>
353
+ }
354
+
355
+ right = ox_parse_doc %Q{
356
+ <xml>
357
+ <node><![CDATA[Foo]]><!-- Bar -->Second child<!-- Baz --></node>
358
+ </xml>
359
+ }
360
+
361
+
362
+ diff = [
363
+ { type: 'c', path: '/xml/node/comment()', lvalue: [ 'Foo' ], rvalue: [ 'Bar', 'Baz' ] },
364
+ { type: 'c', path: '/xml/node', lvalue: 'First child', rvalue: 'Second child' }
365
+ ]
366
+
367
+
368
+ expect(parser.diff(left, right)).to eq diff
369
+ end
370
+
371
+
372
+
373
+
374
+
375
+
376
+ it 'when left has children with same comments and text as right' do
377
+ left = ox_parse_doc %Q{
378
+ <xml>
379
+ <node><![CDATA[Foo]]><!-- Foo -->Child<!-- Bar --></node>
380
+ </xml>
381
+ }
382
+
383
+ right = ox_parse_doc %Q{
384
+ <xml>
385
+ <node><![CDATA[Foo]]><!-- Foo -->Child<!-- Bar --></node>
386
+ </xml>
387
+ }
388
+
389
+
390
+ expect(parser.diff(left, right)).to eq []
391
+ end
392
+
393
+
394
+
395
+
396
+
397
+ it 'when right hash more children than left' do
398
+ left = ox_parse_doc %Q{
399
+ <xml>
400
+ <node>a</node>
401
+ <node>b</node>
402
+ <node>c</node>
403
+ </xml>
404
+ }
405
+
406
+ right = ox_parse_doc %Q{
407
+ <xml>
408
+ <node>a</node>
409
+ <node>b</node>
410
+ <node>c</node>
411
+ <node>d</node>
412
+ </xml>
413
+ }
414
+ diff = [ { type: 'a', path: '/xml/node', lvalue: nil, rvalue: [ 'd' ] } ]
415
+
416
+
417
+ expect(parser.diff(left, right)).to eq(diff)
418
+ end
419
+
420
+
421
+
422
+
423
+
424
+ it 'when left has more number than right of same values' do
425
+ left = ox_parse_doc %Q{
426
+ <xml>
427
+ <node>a</node>
428
+ <node>a</node>
429
+ <node>a</node>
430
+ </xml>
431
+ }
432
+
433
+ right = ox_parse_doc %Q{
434
+ <xml>
435
+ <node>a</node>
436
+ <node>a</node>
437
+ </xml>
438
+ }
439
+ diff = [ { type: 'd', path: '/xml/node', lvalue: [ 'a' ], rvalue: nil } ]
440
+
441
+ expect(parser.diff(left, right)).to eq(diff)
442
+ end
443
+
444
+
445
+
446
+
447
+
448
+ it 'when right has more number than left of same values' do
449
+ left = ox_parse_doc %Q{
450
+ <xml>
451
+ <node>a</node>
452
+ <node>a</node>
453
+ </xml>
454
+ }
455
+
456
+ right = ox_parse_doc %Q{
457
+ <xml>
458
+ <node>a</node>
459
+ <node>a</node>
460
+ <node>a</node>
461
+ </xml>
462
+ }
463
+ diff = [ { type: 'a', path: '/xml/node', lvalue: nil, rvalue: [ 'a' ] } ]
464
+
465
+ expect(parser.diff(left, right)).to eq(diff)
466
+ end
467
+
468
+
469
+
470
+
471
+
472
+ it 'when left and right are empty arrays' do
473
+ expect(parser.diff([], [])).to eq []
474
+ end
475
+
476
+
477
+ end
478
+
479
+
480
+
481
+
482
+
483
+
484
+
485
+ describe '#flat_h' do
486
+
487
+
488
+ describe '::Ox::Document' do
489
+
490
+ it 'when document is nil' do
491
+ expect(parser.flat_h(nil)).to be_nil
492
+ end
493
+
494
+
495
+
496
+
497
+ it 'when document is empty' do
498
+ xml = ox_parse_doc('')
499
+ result = { '/@version' => '1.0', '/@encoding' => 'UTF-8' }
500
+
501
+ expect(parser.flat_h(xml)).to eq(result)
502
+ end
503
+
504
+
505
+
506
+
507
+ it 'when document has a comment' do
508
+ xml = %Q{
509
+ <!-- Comment -->
510
+ }
511
+ xml = ox_parse_doc(xml)
512
+ result = { '/@version' => '1.0', '/@encoding' => 'UTF-8', '/comment()' => [ 'Comment' ] }
513
+
514
+ expect(parser.flat_h(xml)).to eq result
515
+ end
516
+
517
+
518
+
519
+
520
+ it 'when document has an empty root node' do
521
+ xml = ox_parse_doc %Q{ <foo /> }
522
+
523
+ result = { '/@version' => '1.0', '/@encoding' => 'UTF-8', '/foo' => nil }
524
+
525
+ expect(parser.flat_h(xml)).to eq result
526
+ end
527
+
528
+
529
+
530
+
531
+ it 'when document has root node with attributes' do
532
+ xml = ox_parse_doc %Q{ <foo id='root' /> }
533
+
534
+ result = { '/@version' => '1.0', '/@encoding' => 'UTF-8', '/foo/@id' => 'root' }
535
+
536
+ expect(parser.flat_h(xml)).to eq result
537
+ end
538
+
539
+
540
+
541
+
542
+
543
+ it 'when document has root node with multiple attributes' do
544
+ xml = ox_parse_doc %Q{ <foo id='root' name='root_node' /> }
545
+
546
+ result = { '/@version' => '1.0', '/@encoding' => 'UTF-8', '/foo/@id' => 'root', '/foo/@name' => 'root_node' }
547
+
548
+ expect(parser.flat_h(xml)).to eq result
549
+ end
550
+
551
+
552
+
553
+
554
+
555
+ it 'when document has root node with attributes and comment' do
556
+ xml = ox_parse_doc %Q{ <foo id='root'><!-- Foo's comment --></foo> }
557
+
558
+ result = { '/@version' => '1.0',
559
+ '/@encoding' => 'UTF-8',
560
+ '/foo/@id' => 'root',
561
+ '/foo/comment()' => [ "Foo's comment" ] }
562
+
563
+
564
+ expect(parser.flat_h(xml)).to eq result
565
+ end
566
+
567
+
568
+
569
+
570
+ it 'when document has root node with attributes and multiple comments' do
571
+ xml = ox_parse_doc %Q{ <foo id='root'><!-- Foo --><!-- Bar --></foo> }
572
+
573
+ result = { '/@version' => '1.0',
574
+ '/@encoding' => 'UTF-8',
575
+ '/foo/@id' => 'root',
576
+ '/foo/comment()' => [ 'Foo', 'Bar' ] }
577
+
578
+
579
+ expect(parser.flat_h(xml)).to eq result
580
+ end
581
+
582
+
583
+
584
+
585
+ it 'when document has root node with text' do
586
+ xml = ox_parse_doc %Q{ <foo>Foo</foo> }
587
+
588
+ result = { '/@version' => '1.0',
589
+ '/@encoding' => 'UTF-8',
590
+ '/foo' => 'Foo' }
591
+
592
+ expect(parser.flat_h(xml)).to eq result
593
+ end
594
+
595
+
596
+
597
+
598
+ it 'when document has root node with attributes and text' do
599
+ xml = ox_parse_doc %Q{ <foo id='foo' name='root_node'>Foo</foo> }
600
+
601
+ result = { '/@version' => '1.0',
602
+ '/@encoding' => 'UTF-8',
603
+ '/foo' => 'Foo',
604
+ '/foo/@id' => 'foo',
605
+ '/foo/@name' => 'root_node' }
606
+
607
+ expect(parser.flat_h(xml)).to eq result
608
+ end
609
+
610
+
611
+
612
+
613
+ it 'when document has root node with attributes, comment and text' do
614
+ xml = ox_parse_doc %Q{ <foo id='foo'><!-- Foo's comment -->Foo</foo> }
615
+
616
+ result = { '/@version' => '1.0',
617
+ '/@encoding' => 'UTF-8',
618
+ '/foo' => 'Foo',
619
+ '/foo/comment()' => [ "Foo's comment" ],
620
+ '/foo/@id' => 'foo' }
621
+
622
+ expect(parser.flat_h(xml)).to eq result
623
+ end
624
+
625
+
626
+
627
+
628
+ it 'when document has root node with attributes, multiple comments and text' do
629
+ xml = ox_parse_doc %Q{ <foo id='foo'><!-- Foo's comment -->Foo<!-- Second comment --></foo> }
630
+
631
+ result = { '/@version' => '1.0',
632
+ '/@encoding' => 'UTF-8',
633
+ '/foo' => 'Foo',
634
+ '/foo/comment()' => [ "Foo's comment", "Second comment" ],
635
+ '/foo/@id' => 'foo' }
636
+
637
+ expect(parser.flat_h(xml)).to eq result
638
+ end
639
+
640
+
641
+
642
+
643
+ it 'when document has root node with attributes, multiple comments and multiple text' do
644
+ xml = ox_parse_doc %Q{ <foo id='foo'><!-- Foo's comment -->Foo<!-- Second comment -->Bar</foo> }
645
+
646
+ result = { '/@version' => '1.0',
647
+ '/@encoding' => 'UTF-8',
648
+ '/foo' => [ 'Foo', 'Bar' ],
649
+ '/foo/comment()' => [ "Foo's comment", "Second comment" ],
650
+ '/foo/@id' => 'foo' }
651
+
652
+ expect(parser.flat_h(xml)).to eq result
653
+ end
654
+
655
+ end
656
+
657
+
658
+
659
+
660
+
661
+ describe '::Ox::Element' do
662
+
663
+ it 'when element is nil' do
664
+ expect(parser.flat_h(nil)).to eq nil
665
+ end
666
+
667
+
668
+
669
+
670
+ it 'when empty element' do
671
+ xml = ox_parse_elem %Q{ <xml /> }
672
+ result = { '/xml' => nil }
673
+
674
+ expect(parser.flat_h(xml)).to eq result
675
+ end
676
+
677
+
678
+
679
+
680
+
681
+ it 'when element has a comment' do
682
+ xml = ox_parse_elem %Q{ <xml><!-- Foo --></xml> }
683
+ result = { '/xml/comment()' => [ 'Foo' ] }
684
+
685
+ expect(parser.flat_h(xml)).to eq result
686
+ end
687
+
688
+
689
+
690
+
691
+
692
+ it 'when element has attribute' do
693
+ xml = ox_parse_elem %Q{ <xml id='root' /> }
694
+ result = { '/xml/@id' => 'root' }
695
+
696
+ expect(parser.flat_h(xml)).to eq result
697
+ end
698
+
699
+
700
+
701
+
702
+
703
+ it 'when element has multiple attributes' do
704
+ xml = ox_parse_elem %Q{ <xml id='root' name='foo' /> }
705
+ result = { '/xml/@id' => 'root', '/xml/@name' => 'foo' }
706
+
707
+ expect(parser.flat_h(xml)).to eq result
708
+ end
709
+
710
+
711
+
712
+
713
+
714
+ it 'when element has attributes and comment' do
715
+ xml = ox_parse_elem %Q{ <xml id='root' name='Root'><!-- Root node --></xml> }
716
+ result = { '/xml/@id' => 'root',
717
+ '/xml/@name' => 'Root',
718
+ '/xml/comment()' => [ 'Root node' ] }
719
+
720
+ expect(parser.flat_h(xml)).to eq result
721
+ end
722
+
723
+
724
+
725
+
726
+
727
+ it 'when element has text' do
728
+ xml = ox_parse_elem %Q{ <xml>Foo</xml> }
729
+ result = { '/xml' => 'Foo' }
730
+
731
+ expect(parser.flat_h(xml)).to eq result
732
+ end
733
+
734
+
735
+
736
+
737
+
738
+ it 'when element has a text and a comment' do
739
+ xml = ox_parse_elem %Q{ <xml><!-- Comment -->Foo</xml> }
740
+ result = { '/xml/comment()' => [ 'Comment' ], '/xml' => 'Foo' }
741
+
742
+ expect(parser.flat_h(xml)).to eq result
743
+ end
744
+
745
+
746
+
747
+
748
+
749
+ it 'when element has comment around text' do
750
+ xml = ox_parse_elem %Q{ <xml><!-- Comment one -->Foo<!-- Comment two --></xml> }
751
+ result = { '/xml' => 'Foo', '/xml/comment()' => [ 'Comment one', 'Comment two' ] }
752
+
753
+ expect(parser.flat_h(xml)).to eq result
754
+ end
755
+
756
+
757
+
758
+
759
+
760
+ it 'when element has multiple comments and multiple texts' do
761
+ xml = ox_parse_elem %Q{ <xml><!-- Comment one -->Foo<!-- Comment two -->Bar</xml> }
762
+ result = { '/xml' => [ 'Foo', 'Bar' ], '/xml/comment()' => [ 'Comment one', 'Comment two' ] }
763
+
764
+ expect(parser.flat_h(xml)).to eq result
765
+ end
766
+
767
+
768
+
769
+
770
+
771
+ it 'when element has attributes, comments and text' do
772
+ xml = ox_parse_elem %Q{ <xml id='root' name='root_node'><!-- Root node -->Foo</xml> }
773
+ result = { '/xml' => 'Foo',
774
+ '/xml/@id' => 'root',
775
+ '/xml/@name' => 'root_node',
776
+ '/xml/comment()' => [ 'Root node' ] }
777
+
778
+ expect(parser.flat_h(xml)).to eq result
779
+ end
780
+
781
+
782
+
783
+
784
+
785
+ it 'when element has an empty child' do
786
+ xml = ox_parse_elem %Q{ <foo><bar /></foo> }
787
+ result = { '/foo/bar' => nil }
788
+
789
+ expect(parser.flat_h(xml)).to eq result
790
+ end
791
+
792
+
793
+
794
+
795
+
796
+ it 'when element has a child with attributes' do
797
+ xml = ox_parse_elem %Q{ <foo><bar id='child' /></foo> }
798
+ result = { '/foo/bar/@id' => 'child' }
799
+
800
+ expect(parser.flat_h(xml)).to eq result
801
+ end
802
+
803
+
804
+
805
+
806
+
807
+ it 'when element has a child with attributes and text' do
808
+ xml = ox_parse_elem %Q{ <foo><bar id='child'>Child</bar></foo> }
809
+ result = { '/foo/bar/@id' => 'child', '/foo/bar' => 'Child' }
810
+
811
+ expect(parser.flat_h(xml)).to eq result
812
+ end
813
+
814
+
815
+
816
+
817
+
818
+ it 'when element has child with attributes, text and comment' do
819
+ xml = ox_parse_elem %Q{ <foo><bar id='child' name='child_node'><!-- Child -->Child</bar></foo> }
820
+ result = { '/foo/bar/@id' => 'child',
821
+ '/foo/bar/@name' => 'child_node',
822
+ '/foo/bar/comment()' => [ 'Child' ],
823
+ '/foo/bar' => 'Child' }
824
+
825
+ expect(parser.flat_h(xml)).to eq result
826
+ end
827
+
828
+
829
+
830
+
831
+
832
+ it 'when element has children with attributes, text and comment' do
833
+ xml = ox_parse_elem %Q{
834
+ <foo>
835
+ <bar id='first' name='first_child'><!-- First child -->First child</bar>
836
+ <bar id='second' name='second_child'><!-- Second child -->Second child</bar>
837
+ </foo>
838
+ }
839
+ result = {
840
+ '/foo/bar/@id' => [ 'first', 'second' ],
841
+ '/foo/bar/@name' => [ 'first_child', 'second_child' ],
842
+ '/foo/bar/comment()' => [ [ 'First child' ], [ 'Second child' ] ],
843
+ '/foo/bar' => [ 'First child', 'Second child' ]
844
+ }
845
+
846
+ expect(parser.flat_h(xml)).to eq result
847
+ end
848
+
849
+
850
+
851
+
852
+
853
+ it 'when element has attributes, comments and children with attributes text and comment' do
854
+ xml = ox_parse_elem %Q{
855
+ <foo id='root' name='root_node'>
856
+ <!-- Root node -->
857
+ <bar id='first' name='first_child'><!-- First child -->First child</bar>
858
+ <bar id='second' name='second_child'><!-- Second child -->Second child</bar>
859
+ </foo>
860
+ }
861
+ result = {
862
+ '/foo/@id' => 'root',
863
+ '/foo/@name' => 'root_node',
864
+ '/foo/comment()' => [ 'Root node' ],
865
+ '/foo/bar/@id' => [ 'first', 'second' ],
866
+ '/foo/bar/@name' => [ 'first_child', 'second_child' ],
867
+ '/foo/bar/comment()' => [ [ 'First child' ], [ 'Second child' ] ],
868
+ '/foo/bar' => [ 'First child', 'Second child' ]
869
+ }
870
+
871
+ expect(parser.flat_h(xml)).to eq result
872
+ end
873
+
874
+
875
+
876
+
877
+
878
+ it 'when element has cdata' do
879
+ xml = ox_parse_elem %Q{ <foo><![CDATA[Foo]]></foo> }
880
+ result = { '/foo/cdata()' => [ 'Foo' ] }
881
+
882
+ expect(parser.flat_h(xml)).to eq result
883
+ end
884
+
885
+
886
+
887
+
888
+
889
+ it 'when element has text and cdata' do
890
+ xml = ox_parse_elem %Q{ <foo><![CDATA[Foo]]>Foo Text</foo> }
891
+ result = { '/foo/cdata()' => [ 'Foo' ], '/foo' => 'Foo Text' }
892
+
893
+ expect(parser.flat_h(xml)).to eq result
894
+ end
895
+
896
+
897
+
898
+
899
+
900
+ it 'when element has multiple text and multiple cdata' do
901
+ xml = ox_parse_elem %Q{ <foo><![CDATA[Foo]]>First Text<![CDATA[Bar]]>Second Text</foo> }
902
+ result = { '/foo/cdata()' => [ 'Foo', 'Bar' ], '/foo' => [ 'First Text', 'Second Text' ] }
903
+
904
+ expect(parser.flat_h(xml)).to eq result
905
+ end
906
+
907
+
908
+
909
+
910
+
911
+ it 'when element has text, cdata and attributes' do
912
+ xml = ox_parse_elem %Q{ <foo id='foo'><![CDATA[Foo]]>Foo</foo> }
913
+ result = { '/foo/@id' => 'foo', '/foo/cdata()' => [ 'Foo' ], '/foo' => 'Foo' }
914
+
915
+ expect(parser.flat_h(xml)).to eq result
916
+ end
917
+
918
+
919
+
920
+
921
+
922
+ it 'when element has text, cdata, attributes and comments' do
923
+ xml = ox_parse_elem %Q{ <foo id='foo'><![CDATA[Foo]]>Foo<!-- Foo's Comment --></foo> }
924
+ result = { '/foo/@id' => 'foo',
925
+ '/foo/cdata()' => [ 'Foo' ],
926
+ '/foo/comment()' => [ "Foo's Comment" ],
927
+ '/foo' => 'Foo' }
928
+
929
+ expect(parser.flat_h(xml)).to eq result
930
+ end
931
+
932
+
933
+
934
+
935
+
936
+
937
+ it 'when element has attributes, comments, cdata and children with attributes text, comment and cdata' do
938
+ xml = ox_parse_elem %Q{
939
+ <foo id='root' name='root_node'>
940
+ <!-- Root node -->
941
+ <![CDATA[Foo]]>
942
+ <bar id='first' name='first_child'><!-- First child -->First child<![CDATA[Foo]]></bar>
943
+ <bar id='second' name='second_child'><!-- Second child -->Second child<![CDATA[Bar]]></bar>
944
+ </foo>
945
+ }
946
+ result = {
947
+ '/foo/@id' => 'root',
948
+ '/foo/@name' => 'root_node',
949
+ '/foo/comment()' => [ 'Root node' ],
950
+ '/foo/cdata()' => [ 'Foo' ],
951
+ '/foo/bar/@id' => [ 'first', 'second' ],
952
+ '/foo/bar/@name' => [ 'first_child', 'second_child' ],
953
+ '/foo/bar/comment()' => [ [ 'First child' ], [ 'Second child' ] ],
954
+ '/foo/bar/cdata()' => [ [ 'Foo' ], [ 'Bar' ] ],
955
+ '/foo/bar' => [ 'First child', 'Second child' ]
956
+ }
957
+
958
+ expect(parser.flat_h(xml)).to eq result
959
+ end
960
+
961
+
962
+ end
963
+
964
+ end
965
+
966
+ end