File: 26schema.t

package info (click to toggle)
libxml-libxml-perl 2.0001%2Bdfsg-1%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,452 kB
  • sloc: perl: 5,285; ansic: 3,747; xml: 178; sh: 61; makefile: 8
file content (73 lines) | stat: -rw-r--r-- 1,742 bytes parent folder | download
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
# $Id$

##
# Testcases for the XML Schema interface
#

use strict;
use warnings;

use lib './t/lib';
use TestHelpers;

use Test::More;

    use XML::LibXML;

if ( XML::LibXML::LIBXML_VERSION >= 20510 ) {
    plan tests => 6; 
}
else {
    plan skip_all => 'No Schema Support compiled.';
}

my $xmlparser = XML::LibXML->new();

my $file         = "test/schema/schema.xsd";
my $badfile      = "test/schema/badschema.xsd";
my $validfile    = "test/schema/demo.xml";
my $invalidfile  = "test/schema/invaliddemo.xml";


# 1 parse schema from a file
{
    my $rngschema = XML::LibXML::Schema->new( location => $file );
    # TEST
    ok ( $rngschema, 'Good XML::LibXML::Schema was initialised' );
    
    eval { $rngschema = XML::LibXML::Schema->new( location => $badfile ); };
    # TEST
    ok( $@, 'Bad XML::LibXML::Schema throws an exception.' );
}

# 2 parse schema from a string
{
    my $string = slurp($file);

    my $rngschema = XML::LibXML::Schema->new( string => $string );
    # TEST
    ok ( $rngschema, 'RNG Schema initialized from string.' );

    $string = slurp($badfile);
    eval { $rngschema = XML::LibXML::Schema->new( string => $string ); };
    # TEST
    ok( $@, 'Bad string schema throws an excpetion.' );
}

# 3 validate a document
{
    my $doc       = $xmlparser->parse_file( $validfile );
    my $rngschema = XML::LibXML::Schema->new( location => $file );

    my $valid = 0;
    eval { $valid = $rngschema->validate( $doc ); };
    # TEST
    is( $valid, 0, 'validate() returns 0 to indicate validity of valid file.' );

    $doc       = $xmlparser->parse_file( $invalidfile );
    $valid     = 0;
    eval { $valid = $rngschema->validate( $doc ); };
    # TEST
    ok ( $@, 'Invalid file throws an excpetion.');
}