File: A_XMLParser.t

package info (click to toggle)
libxml-simple-perl 2.25-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 428 kB
  • sloc: perl: 1,137; xml: 47; makefile: 2
file content (119 lines) | stat: -rw-r--r-- 2,515 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
112
113
114
115
116
117
118
119

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


# Initialise filenames and check they're there

my $XMLFile = File::Spec->catfile('t', 'test1.xml');  # t/test1.xml

unless(-e $XMLFile) {
  plan skip_all => 'Test data missing';
}

eval { require XML::Parser; };
unless($INC{'XML/Parser.pm'}) {
  plan skip_all => 'no XML::Parser';
}

plan tests => 14;

use XML::Simple;

my $last_warning = '';
my $opt;


# Use environment variable to set preferred parser

$ENV{XML_SIMPLE_PREFERRED_PARSER} = 'XML::Parser';


# Try using a SAX-only option

{
  local($SIG{__WARN__}) = \&warn_handler;

  $opt = XMLin('<x y="z" />', nsexpand => 1);
}

isnt($last_warning, '', "Parsing caused warning (as expected)");
like($last_warning, qr/'nsexpand' option requires XML::SAX/,
  'Message contained expected text');
is_deeply($opt, {y => 'z'}, "Parsing was successful");


# Check for deprecation warning

{
  local($SIG{__WARN__}) = \&warn_handler;

  $last_warning = '';
  $opt = XMLin('<x y="z" />', ParserOpts => [ ParseParamEnt => 1 ]);
}

isnt($last_warning, '', "Using ParserOpts caused warning (as expected)");
like($last_warning, qr/'ParserOpts' is deprecated/,
  'Message contained expected text');
is_deeply($opt, {y => 'z'}, "Parsing was successful");


# Check it doesn't happen if warnings disabled

{
  no warnings;
  local($SIG{__WARN__}) = \&warn_handler;

  $last_warning = '';
  $opt = XMLin('<x y="z" />', ParserOpts => [ ParseParamEnt => 1 ]);
}

is($last_warning, '', "ParserOpts warning uppressed successfully");
is_deeply($opt, {y => 'z'}, "Parsing was successful");



# Try parsing a string

$opt = eval {
  XMLin(q(<opt name1="value1" name2="value2"></opt>));
};

my $expected = {
                 name1 => 'value1',
                 name2 => 'value2',
               };

is($@, '', "No error when parsing");
is_deeply($opt, $expected, 'matches expectations (attributes)');


# Try parsing a named external file

$opt = eval{ XMLin($XMLFile); };
is($@, '', "XML::Parser didn't choke on named external file");
is_deeply($opt, {
  location => 't/test1.xml'
}, 'and contents parsed as expected');


# Try parsing from an IO::Handle

my $fh = new IO::File;
$XMLFile = File::Spec->catfile('t', '1_XMLin.xml');  # t/1_XMLin.xml
eval {
  $fh->open($XMLFile) || die "$!";
  $opt = XMLin($fh);
};
is($@, '', "XML::Parser didn't choke on an IO::File object");
is($opt->{location}, 't/1_XMLin.xml', 'and it parsed the right file');


exit(0);

sub warn_handler {
  $last_warning = $_[0];
}