PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/XML-Simple-2.20/t/8_Namespaces.t

#
Unknown | 227 lines | 179 code | 48 blank | 0 comment | 0 complexity | cde582cccfe398e5e156f071d7167777 MD5 | raw file
Possible License(s): AGPL-1.0
  1. use strict;
  2. use warnings;
  3. use Test::More;
  4. use File::Spec;
  5. use IO::File;
  6. eval { require XML::SAX; };
  7. if($@) {
  8. plan skip_all => 'no XML::SAX';
  9. }
  10. eval { require XML::NamespaceSupport; };
  11. if($@) {
  12. plan skip_all => "no XML::NamespaceSupport";
  13. }
  14. if($XML::NamespaceSupport::VERSION < 1.04) {
  15. plan skip_all => "XML::NamespaceSupport is too old (upgrade to 1.04 or better)";
  16. }
  17. plan tests => 8;
  18. ##############################################################################
  19. # S U P P O R T R O U T I N E S
  20. ##############################################################################
  21. ##############################################################################
  22. # Copy a file
  23. #
  24. sub CopyFile {
  25. my($Src, $Dst) = @_;
  26. open(IN, $Src) || return(undef);
  27. local($/) = undef;
  28. my $Data = <IN>;
  29. close(IN);
  30. open(OUT, ">$Dst") || return(undef);
  31. print OUT $Data;
  32. close(OUT);
  33. return(1);
  34. }
  35. ##############################################################################
  36. # T E S T R O U T I N E S
  37. ##############################################################################
  38. use XML::Simple;
  39. # Force default behaviour of using SAX parser if it is available (which it
  40. # is or we wouldn't be here).
  41. $XML::Simple::PREFERRED_PARSER = '';
  42. # Confirm that by default qnames are not expanded on input
  43. my $xml = q(<config xmlns:perl="http://www.perl.com/">
  44. <perl:list count="3" perl:type="array">
  45. <item>one</item>
  46. <item>two</item>
  47. <item>three</item>
  48. <test xmlns:perl="http://www.microsoft.com" perl:tm="trademark" />
  49. </perl:list>
  50. </config>);
  51. my $expected = {
  52. 'perl:list' => {
  53. 'count' => '3',
  54. 'item' => [
  55. 'one',
  56. 'two',
  57. 'three'
  58. ],
  59. 'perl:type' => 'array',
  60. 'test' => {
  61. 'xmlns:perl' => 'http://www.microsoft.com',
  62. 'perl:tm' => 'trademark',
  63. }
  64. },
  65. 'xmlns:perl' => 'http://www.perl.com/'
  66. };
  67. my $opt = XMLin($xml);
  68. is_deeply($opt, $expected, 'qnames are not expanded by default');
  69. # Try again with nsexpand option set
  70. $expected = {
  71. '{http://www.perl.com/}list' => {
  72. 'count' => '3',
  73. 'item' => [
  74. 'one',
  75. 'two',
  76. 'three'
  77. ],
  78. '{http://www.perl.com/}type' => 'array',
  79. 'test' => {
  80. '{http://www.microsoft.com}tm' => 'trademark',
  81. '{http://www.w3.org/2000/xmlns/}perl' => 'http://www.microsoft.com'
  82. }
  83. },
  84. '{http://www.w3.org/2000/xmlns/}perl' => 'http://www.perl.com/'
  85. };
  86. $opt = XMLin($xml, nsexpand => 1);
  87. is_deeply($opt, $expected, 'qnames are expanded on request');
  88. # Confirm that output expansion does not occur by default
  89. $opt = {
  90. '{http://www.w3.org/2000/xmlns/}perl' => 'http://www.perl.com/',
  91. '{http://www.perl.com/}attr' => 'value',
  92. 'bare' => 'Beer!',
  93. '{http://www.perl.com/}element' => [ 'data' ],
  94. };
  95. $xml = XMLout($opt);
  96. like($xml, qr{
  97. ^\s*<opt
  98. (\s+{http://www.w3.org/2000/xmlns/}perl="http://www.perl.com/"
  99. |\s+{http://www.perl.com/}attr="value"
  100. |\s+bare="Beer!"){3}
  101. \s*>
  102. \s*<{http://www.perl.com/}element\s*>data</{http://www.perl.com/}element\s*>
  103. \s*</opt>
  104. \s*$
  105. }sx, 'clarkian names not converted to qnames on output by default');
  106. # Confirm nsexpand option works on output
  107. $xml = XMLout($opt, nsexpand => 1);
  108. ok($xml =~ m{
  109. ^\s*<opt
  110. (\s+xmlns:perl="http://www.perl.com/"
  111. |\s+perl:attr="value"
  112. |\s+bare="Beer!"){3}
  113. \s*>
  114. \s*<perl:element\s*>data</perl:element\s*>
  115. \s*</opt>
  116. \s*$
  117. }sx, 'clarkian names are converted to qnames on output on request');
  118. # Check that default namespace is correctly read in ...
  119. $xml = q(<opt xmlns="http://www.orgsoc.org/">
  120. <list>
  121. <member>Tom</member>
  122. <member>Dick</member>
  123. <member>Larry</member>
  124. </list>
  125. </opt>
  126. );
  127. $expected = {
  128. 'xmlns' => 'http://www.orgsoc.org/',
  129. '{http://www.orgsoc.org/}list' => {
  130. '{http://www.orgsoc.org/}member' => [ 'Tom', 'Dick', 'Larry' ],
  131. }
  132. };
  133. $opt = XMLin($xml, nsexpand => 1);
  134. is_deeply($opt, $expected, 'expansion of default namespace works');
  135. # ... and written out
  136. $xml = XMLout($opt, nsexpand => 1);
  137. like($xml, qr{
  138. ^\s*<opt
  139. \s+xmlns="http://www.orgsoc.org/"
  140. \s*>
  141. \s*<list>
  142. \s*<member>Tom</member>
  143. \s*<member>Dick</member>
  144. \s*<member>Larry</member>
  145. \s*</list>
  146. \s*</opt>
  147. \s*$
  148. }sx, 'default namespaces are output correctly too');
  149. # Check that the autogeneration of namespaces works as we expect
  150. $opt = {
  151. 'xmlns' => 'http://www.orgsoc.org/',
  152. '{http://www.orgsoc.org/}list' => {
  153. '{http://www.orgsoc.org/}member' => [ 'Tom', 'Dick', 'Larry' ],
  154. '{http://www.phantom.com/}director' => [ 'Bill', 'Ben' ],
  155. }
  156. };
  157. $xml = XMLout($opt, nsexpand => 1);
  158. my $prefix = '';
  159. if($xml =~ m{<list\s+xmlns:(\w+)="http://www.phantom.com/"\s*>}) {
  160. $prefix = $1;
  161. }
  162. # regex match split in two to workaround 5.8.1/utf8/regex match prob
  163. like($xml, qr{
  164. \s*<opt
  165. \s+xmlns="http://www.orgsoc.org/"
  166. \s*>
  167. .*?
  168. </list>
  169. \s*</opt>
  170. }sx, 'namespace prefixes are generated automatically (part 1)');
  171. like($xml, qr{
  172. (\s*<member>Tom</member>
  173. \s*<member>Dick</member>
  174. \s*<member>Larry</member>
  175. |\s*<${prefix}:director>Bill</${prefix}:director>
  176. \s*<${prefix}:director>Ben</${prefix}:director>){2}
  177. #\s*</list>
  178. }sx, 'namespace prefixes are generated automatically (part 2)');
  179. exit(0);