File: 05text.t

package info (click to toggle)
libxml-libxml-perl 1.70.ds-1%2Bdeb6u1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 1,992 kB
  • ctags: 1,726
  • sloc: perl: 4,521; ansic: 3,583; xml: 177; sh: 62; makefile: 7
file content (133 lines) | stat: -rw-r--r-- 3,665 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
# $Id: 05text.t 618 2006-11-10 18:18:24Z pajas $

##
# this test checks the DOM Characterdata interface of XML::LibXML

use Test;

BEGIN { plan tests => 32 };
use XML::LibXML;

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

{
    print "# 1. creation\n";
    my $foo = "foobar";
    my $textnode = $doc->createTextNode($foo);
    ok( $textnode );
    ok( $textnode->nodeName(), '#text' );
    ok( $textnode->nodeValue(), $foo );

    print "# 2. substring\n";
    my $tnstr = $textnode->substringData( 1,2 );
    ok( $tnstr , "oo" );
    ok( $textnode->nodeValue(), $foo );

    print "# 3. Expansion\n";
    $textnode->appendData( $foo );
    ok( $textnode->nodeValue(), $foo . $foo );

    $textnode->insertData( 6, "FOO" );
    ok( $textnode->nodeValue(), $foo."FOO".$foo );

    $textnode->setData( $foo );
    $textnode->insertData( 6, "FOO" );
    ok( $textnode->nodeValue(), $foo."FOO" );
    $textnode->setData( $foo );
    $textnode->insertData( 3, "" );
    ok( $textnode->nodeValue(), $foo );

    print "# 4. Removement\n";
    $textnode->deleteData( 1,2 );
    ok( $textnode->nodeValue(), "fbar" );
    $textnode->setData( $foo );
    $textnode->deleteData( 1,10 );
    ok( $textnode->nodeValue(), "f" );
    $textnode->setData( $foo );
    $textnode->deleteData( 10,1 );
    ok( $textnode->nodeValue(), $foo );
    $textnode->deleteData( 1,0 );
    ok( $textnode->nodeValue(), $foo );
    $textnode->deleteData( 0,0 );
    ok( $textnode->nodeValue(), $foo );
    $textnode->deleteData( 0,2 );
    ok( $textnode->nodeValue(), "obar" );

    print "# 5. Replacement\n";
    $textnode->setData( "test" );
    $textnode->replaceData( 1,2, "phish" );
    ok( $textnode->nodeValue(), "tphisht" );
    $textnode->setData( "test" );
    $textnode->replaceData( 1,4, "phish" );
    ok( $textnode->nodeValue(), "tphish" );
    $textnode->setData( "test" );
    $textnode->replaceData( 1,0, "phish" );
    ok( $textnode->nodeValue(), "tphishest" );


    print "# 6. XML::LibXML features\n";
    $textnode->setData( "test" );

    $textnode->replaceDataString( "es", "new" );   
    ok( $textnode->nodeValue(), "tnewt" );

    $textnode->replaceDataRegEx( 'n(.)w', '$1s' );
    ok( $textnode->nodeValue(), "test" );

    $textnode->setData( "blue phish, white phish, no phish" );
    $textnode->replaceDataRegEx( 'phish', 'test' );
    ok( $textnode->nodeValue(), "blue test, white phish, no phish" );

    # replace them all!
    $textnode->replaceDataRegEx( 'phish', 'test', 'g' );
    ok( $textnode->nodeValue(), "blue test, white test, no test" );

    # check if special chars are encoded properly 
    $textnode->setData( "te?st" );
    $textnode->replaceDataString( "e?s", 'ne\w' );   
    ok( $textnode->nodeValue(), 'tne\wt' );

    # check if entities don't get translated
    $textnode->setData(q(foo&bar));
    ok ( $textnode->getData eq q(foo&bar) );
}

{
    print "# standalone test\n";
    my $node = XML::LibXML::Text->new("foo");
    ok($node);
    ok($node->nodeValue, "foo" );
}

{
    print "# CDATA node name test\n";

    my $node = XML::LibXML::CDATASection->new("test");

    ok( $node->string_value(), "test" );
    ok( $node->nodeName(), "#cdata-section" );
}

{
    print "# Comment node name test\n";

    my $node = XML::LibXML::Comment->new("test");

    ok( $node->string_value(), "test" );
    ok( $node->nodeName(), "#comment" );
}

{
    print "# Document node name test\n";

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

    ok( $node->nodeName(), "#document" );
}
{
    print "# Document fragment node name test\n";

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

    ok( $node->nodeName(), "#document-fragment" );
}