File: XML.pm

package info (click to toggle)
libtest-xml-perl 0.08-3
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 180 kB
  • ctags: 47
  • sloc: perl: 351; makefile: 12
file content (209 lines) | stat: -rw-r--r-- 5,232 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
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
package Test::XML;
# @(#) $Id$

use strict;
use warnings;

use Carp;
use Test::Builder;
use XML::SemanticDiff;
use XML::Parser;

our $VERSION = '0.08';

#---------------------------------------------------------------------
# Import shenanigans.  Copied from Test::Pod...
#---------------------------------------------------------------------

sub import {
    my $self   = shift;
    my $caller = caller;

    no strict 'refs';
    *{ $caller . '::is_xml' }             = \&is_xml;
    *{ $caller . '::isnt_xml' }           = \&isnt_xml;
    *{ $caller . '::is_well_formed_xml' } = \&is_well_formed_xml;
    *{ $caller . '::is_good_xml' }        = \&is_well_formed_xml;

    my $Test = Test::Builder->new;
    $Test->exported_to( $caller );
    $Test->plan( @_ );
}

#---------------------------------------------------------------------
# Tool.
#---------------------------------------------------------------------

sub is_xml {
    my ($input, $expected, $test_name) = @_;
    croak "usage: is_xml(input,expected,test_name)"
        unless defined $input && defined $expected;

    my $Test = Test::Builder->new;
    my $differ = XML::SemanticDiff->new;
    my @diffs = eval { $differ->compare( $expected, $input ) };
    if ( @diffs ) {
        $Test->ok( 0, $test_name );
        $Test->diag( "Found " . scalar(@diffs) . " differences with expected:" );
        $Test->diag( "  $_->{message}" ) foreach @diffs;
        $Test->diag( "in processed XML:\n  $input" );
	return 0;
    } elsif ( $@ ) {
        $Test->ok( 0, $test_name );
        # Make the output a bit more testable.
        $@ =~ s/ at \/.*//;
        $Test->diag( "During compare:$@" );
        return 0;
    } else {
        $Test->ok( 1, $test_name );
	return 1;
    }
}

sub isnt_xml {
    my ($input, $mustnotbe, $test_name) = @_;
    croak "usage: isnt_xml(input,mustnotbe,test_name)"
        unless defined $input && defined $mustnotbe;

    my $Test = Test::Builder->new;
    my $differ = XML::SemanticDiff->new;
    my @diffs = eval { $differ->compare( $mustnotbe, $input ) };
    if ( $@ ) {
        $Test->ok( 0, $test_name );
        # Make the output a bit more testable.
        $@ =~ s/ at \/.*//;
        $Test->diag( "During compare:$@" );
        return 0;
    } elsif ( @diffs == 0 ) {
        $Test->ok( 0, $test_name );
        $Test->diag( "Found no differences in processed XML:\n  $input" );
	return 0;
    } else {
        $Test->ok( 1, $test_name );
	return 1;
    }
}

sub is_well_formed_xml {
    my ($input, $test_name) = @_;
    croak "usage: is_well_formed_xml(input,test_name)"
        unless defined $input;

    my $Test = Test::Builder->new;
    my $parser = XML::Parser->new;
    eval { $parser->parse($input) };
    if ( $@ ) {
        $Test->ok( 0, $test_name );
        # Make the output a bit more testable.
        $@ =~ s/ at \/.*//;
        $Test->diag( "During parse: $@" );
        return 0;
    } else {
        $Test->ok( 1, $test_name );
        return 1;
    }
}

1;
__END__

=head1 NAME

Test::XML - Compare XML in perl tests

=head1 SYNOPSIS

  use Test::XML tests => 3;
  is_xml( '<foo />', '<foo></foo>' );   # PASS
  is_xml( '<foo />', '<bar />' );       # FAIL
  isnt_xml( '<foo />', '<bar />' );     # PASS
  is_well_formed_xml('<foo/>');               # PASS
  is_well_formed_xml('<foo>');                # FAIL

=head1 DESCRIPTION

This module contains generic XML testing tools.  See below for a list of
other modules with functions relating to specific XML modules.

=head1 FUNCTIONS

=over 4

=item is_xml ( GOT, EXPECTED [, TESTNAME ] )

This function compares GOT and EXPECTED, both of which are strings of
XML.  The comparison works semantically and will ignore differences in
syntax which are meaningless in xml, such as different quote characters
for attributes, order of attributes or empty tag styles.

Returns true or false, depending upon test success.

=item isnt_xml( GOT, MUST_NOT_BE [, TESTNAME ] )

This function is similar to is_xml(), except that it will fail if GOT
and MUST_NOT_BE are identical.

=item is_well_formed_xml( XML [, TESTNAME ] )

This function determines whether or not a given XML string is parseable
as XML.

=item is_good_xml ( XML [, TESTNAME ] )

This is an alias for is_well_formed_xml().

=back

=head1 NOTES

There are several features of XML::SemanticDiff that may suprise you
if you are not aware of them.  In particular:

=over 4

=item *

Leading and trailing whitespace is always stripped, even in elements
with character content.

=item *

Whitespace inside character content is always stripped down to a single
space.

=item *

In mixed content elements (ie: an element with both text and elements
beneath it), all text is treated as a single value.

=item *

The order of elements is ignored.

=back

=head1 SEE ALSO

L<Test::XML::SAX>, L<Test::XML::Twig>.

L<Test::More>, L<XML::SemanticDiff>.

=head1 AUTHOR

Dominic Mitchell, E<lt>cpan2 (at) semantico.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright 2002 by semantico

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself. 

=cut

# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# indent-tabs-mode: nil
# End:
# vim: set ai et sw=4 :