File: importer.t

package info (click to toggle)
libcatmandu-xml-perl 0.17-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 216 kB
  • sloc: perl: 387; xml: 28; makefile: 2; sh: 1
file content (111 lines) | stat: -rw-r--r-- 3,065 bytes parent folder | download | duplicates (3)
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
use strict;
use warnings;
use Test::More;

use_ok('Catmandu::Importer::XML');

sub check_import(@) {
    my $options = shift;
    my $file    = shift;
    my $importer = Catmandu::Importer::XML->new(file => $file, %$options);
    
    my $data = $importer->to_array;
    is_deeply $data, @_;
}

check_import { },
    \"<root><element>content</element></root>" => [ { element => 'content' } ],
    'simple';

check_import { type => 'ordered' },
    \"<root x='1'><element>content</element></root>" => [ [ 
        root => { x => 1 }, [
            [ element => { } , [ 'content' ] ]
        ]
    ] ],
    'ordered';

my $xml = <<'XML';
<?xml version="1.0"?>
<doc attr="value">
  <field1>foo</field1>
  <field1>bar</field1>
  <field2>
    <doz>baz</doz>
  </field2>
</doc>
XML

check_import { }, 
    \$xml => [
      {
        attr => 'value',
        field1 => [ 'foo', 'bar' ],
        field2 => { 'doz' => 'baz' },
      }
    ], 'simple';

check_import { type => 'ordered', attributes => 1 },
    \$xml => [ 
        [ doc => { attr => "value" }, [
                [ field1 => { }, ["foo"] ],
                [ field1 => { },  ["bar"] ],
                [ field2 => { }, [ [ doz => { }, ["baz"] ] ] ]
            ]
        ] 
    ], 'ordered with attributes';

check_import { type => 'ordered', attributes => 0 },
    \$xml => [ 
        [ doc => [
                [ field1 => ["foo"] ],
                [ field1 => ["bar"] ],
                [ field2 => [ [ doz => ["baz"] ] ] ]
            ]
        ]
    ], 'ordered without attributes';

check_import { type => 'simple', depth => 2 },
    \$xml => [
      {
        attr => 'value',
        field1 => [ 'foo', 'bar' ],
        field2 => { 
            doz => [ [ doz => { }, ["baz"] ] ]
        }
      }
    ], 'simple with depth=2';

check_import { type => 'simple' },
    "t/input.xml" => [ { id => [1,2,4], xx => 3 } ], 'simple';

check_import { root => 1, attributes => 0  },
    "t/input.xml" => [ { doc => { id => [1,2,4], xx => 3 } } ],
    'simple without attributes, with root';

check_import { type => 'simple', path => '/*/id' },
    "t/input.xml" => [ { id => 1 }, { id => 2 }, { id => 4 } ], 
    'multiple entries (root included by default)';

check_import { type => 'simple', path => '/*/id', root => 0 },
    "t/input.xml" => [ { id => 1 }, { id => 2 }, { id => 4 } ], 
    'multiple entries (root option ignored)';

check_import { type => 'simple', path => '/*/id', root => 'n' },
    "t/input.xml" => [ { n => 1 }, { n => 2 }, { n => 4 } ], 
    'multiple entries (simple)';

check_import { type => 'simple', path => 'doc', xslt => 't/transform3.xsl' },
    "t/collection.xml" => [ 
        { doz => 1 }, { doz => 2 }, { doz => 3 } 
    ], 'import with transformation'; 

my $warn;
$SIG{__WARN__} = sub { $warn = shift };
check_import { type => 'simple', path => 'doc', transform => 't/transform3.xsl' },
    "t/collection.xml" => [ 
        { doz => 1 }, { doz => 2 }, { doz => 3 } 
    ], 'import with transformation (deprecated option)'; 
ok $warn, "deprecated option";

done_testing;