summaryrefslogtreecommitdiff
path: root/t/8_Namespaces.t
blob: 9c83f3d7f4ff8a60bbf6fcd87c46c3cebd9e8eb6 (plain)
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

use strict;
use warnings;
use Test::More;
use File::Spec;
use IO::File;


eval { require XML::SAX; };
if($@) {
  plan skip_all => 'no XML::SAX';
}

eval { require XML::NamespaceSupport; };
if($@) {
  plan skip_all => "no XML::NamespaceSupport";
}
if($XML::NamespaceSupport::VERSION < 1.04) {
  plan skip_all => "XML::NamespaceSupport is too old (upgrade to 1.04 or better)";
}

plan tests => 8;


##############################################################################
#                   S U P P O R T   R O U T I N E S
##############################################################################

##############################################################################
# Copy a file
#

sub CopyFile {
  my($Src, $Dst) = @_;

  open(IN, $Src) || return(undef);
  local($/) = undef;
  my $Data = <IN>;
  close(IN);

  open(OUT, ">$Dst") || return(undef);
  print OUT $Data;
  close(OUT);

  return(1);
}


##############################################################################
#                      T E S T   R O U T I N E S
##############################################################################

use XML::Simple;

# Force default behaviour of using SAX parser if it is available (which it
# is or we wouldn't be here).

$XML::Simple::PREFERRED_PARSER = '';

# Confirm that by default qnames are not expanded on input

my $xml = q(<config xmlns:perl="http://www.perl.com/">
  <perl:list count="3" perl:type="array">
    <item>one</item>
    <item>two</item>
    <item>three</item>
    <test xmlns:perl="http://www.microsoft.com" perl:tm="trademark" />
  </perl:list>
</config>);

my $expected = {
  'perl:list' => {
    'count' => '3',
    'item' => [
      'one',
      'two',
      'three'
    ],
    'perl:type' => 'array',
    'test' => {
      'xmlns:perl' => 'http://www.microsoft.com',
      'perl:tm' => 'trademark',
    }
  },
  'xmlns:perl' => 'http://www.perl.com/'
};

my $opt = XMLin($xml);
is_deeply($opt, $expected, 'qnames are not expanded by default');


# Try again with nsexpand option set

$expected = {
  '{http://www.perl.com/}list' => {
    'count' => '3',
    'item' => [
      'one',
      'two',
      'three'
    ],
    '{http://www.perl.com/}type' => 'array',
    'test' => {
      '{http://www.microsoft.com}tm' => 'trademark',
      '{http://www.w3.org/2000/xmlns/}perl' => 'http://www.microsoft.com'
    }
  },
  '{http://www.w3.org/2000/xmlns/}perl' => 'http://www.perl.com/'
};

$opt = XMLin($xml, nsexpand => 1);
is_deeply($opt, $expected, 'qnames are expanded on request');


# Confirm that output expansion does not occur by default

$opt = {
  '{http://www.w3.org/2000/xmlns/}perl' => 'http://www.perl.com/',
  '{http://www.perl.com/}attr' => 'value',
  'bare' => 'Beer!',
  '{http://www.perl.com/}element' => [ 'data' ],
};

$xml = XMLout($opt);
like($xml, qr{
  ^\s*<opt
  (\s+{http://www.w3.org/2000/xmlns/}perl="http://www.perl.com/"
  |\s+{http://www.perl.com/}attr="value"
  |\s+bare="Beer!"){3}
  \s*>
  \s*<{http://www.perl.com/}element\s*>data</{http://www.perl.com/}element\s*>
  \s*</opt>
  \s*$
}sx, 'clarkian names not converted to qnames on output by default');


# Confirm nsexpand option works on output

$xml = XMLout($opt, nsexpand => 1);
ok($xml =~ m{
  ^\s*<opt
  (\s+xmlns:perl="http://www.perl.com/"
  |\s+perl:attr="value"
  |\s+bare="Beer!"){3}
  \s*>
  \s*<perl:element\s*>data</perl:element\s*>
  \s*</opt>
  \s*$
}sx, 'clarkian names are converted to qnames on output on request');


# Check that default namespace is correctly read in ...

$xml = q(<opt xmlns="http://www.orgsoc.org/">
  <list>
    <member>Tom</member>
    <member>Dick</member>
    <member>Larry</member>
  </list>
</opt>
);

$expected = {
  'xmlns' => 'http://www.orgsoc.org/',
  '{http://www.orgsoc.org/}list' => {
    '{http://www.orgsoc.org/}member' => [ 'Tom', 'Dick', 'Larry' ],
  }
};

$opt = XMLin($xml, nsexpand => 1);
is_deeply($opt, $expected, 'expansion of default namespace works');


# ... and written out

$xml = XMLout($opt, nsexpand => 1);
like($xml, qr{
  ^\s*<opt
  \s+xmlns="http://www.orgsoc.org/"
  \s*>
  \s*<list>
  \s*<member>Tom</member>
  \s*<member>Dick</member>
  \s*<member>Larry</member>
  \s*</list>
  \s*</opt>
  \s*$
}sx, 'default namespaces are output correctly too');


# Check that the autogeneration of namespaces works as we expect

$opt = {
  'xmlns' => 'http://www.orgsoc.org/',
  '{http://www.orgsoc.org/}list' => {
    '{http://www.orgsoc.org/}member' => [ 'Tom', 'Dick', 'Larry' ],
    '{http://www.phantom.com/}director' => [ 'Bill', 'Ben' ],
  }
};

$xml = XMLout($opt, nsexpand => 1);
my $prefix = '';
if($xml =~ m{<list\s+xmlns:(\w+)="http://www.phantom.com/"\s*>}) {
  $prefix = $1;
}
  # regex match split in two to workaround 5.8.1/utf8/regex match prob
like($xml, qr{
  \s*<opt
  \s+xmlns="http://www.orgsoc.org/"
  \s*>
  .*?
  </list>
  \s*</opt>
}sx, 'namespace prefixes are generated automatically (part 1)');

like($xml, qr{
  (\s*<member>Tom</member>
   \s*<member>Dick</member>
   \s*<member>Larry</member>
  |\s*<${prefix}:director>Bill</${prefix}:director>
   \s*<${prefix}:director>Ben</${prefix}:director>){2}
  #\s*</list>
}sx, 'namespace prefixes are generated automatically (part 2)');


exit(0);