comparison _test/Test/DOM/Node.pm @ 18:818c74b038ae

DOM Schema + tests
author Sergey
date Thu, 10 Sep 2009 17:42:47 +0400
parents 65a7bb156fb7
children 7f00786f8210
comparison
equal deleted inserted replaced
17:7f88e01b58f8 18:818c74b038ae
1 package Test::DOM::Node; 1 package Test::DOM::Node;
2 use strict; 2 use strict;
3 use warnings; 3 use warnings;
4 4
5 use base qw(IMPL::Test::Unit); 5 use base qw(IMPL::Test::Unit);
6 use IMPL::Test qw(test shared failed); 6 use IMPL::Test qw(test shared failed cmparray);
7 use IMPL::Class::Property; 7 use IMPL::Class::Property;
8 8
9 require IMPL::DOM::Node; 9 require IMPL::DOM::Node;
10 10
11 __PACKAGE__->PassThroughArgs; 11 __PACKAGE__->PassThroughArgs;
18 my ($this) = @_; 18 my ($this) = @_;
19 19
20 $this->Root(new IMPL::DOM::Node(nodeName => 'Root')) or failed "Failed to create a node"; 20 $this->Root(new IMPL::DOM::Node(nodeName => 'Root')) or failed "Failed to create a node";
21 }; 21 };
22 22
23 test InsertNode => sub {
24 my ($this) = @_;
25 my $child = $this->Root->insertNode(new IMPL::DOM::Node(nodeName => 'Child')) or failed "Failed to insert a child node";
26 failed "fiestChild returned incorrect results" unless ($this->Root->firstChild || 0) == $child;
27 };
28
23 test AppendNode => sub { 29 test AppendNode => sub {
24 my ($this) = @_; 30 my ($this) = @_;
25 31
26 my $child = $this->Root->appendNode(new IMPL::DOM::Node(nodeName => 'Child')) or failed "Failed to append a child node"; 32 my $child = $this->Root->appendNode(new IMPL::DOM::Node(nodeName => 'Child')) or failed "Failed to append a child node";
27 33
28 my $firstChild = $this->Root->firstChild; 34 my $lastChild = $this->Root->removeLast;
29 35
30 failed "firstChild returned incorrect results" unless $firstChild == $child; 36 failed "removeLast returned incorrect results" unless $lastChild == $child;
37 };
38
39 test GetRootNode => sub {
40 my ($this) = @_;
41
42 my $child = $this->Root->firstChild->appendNode(new IMPL::DOM::Node(nodeName => 'GrandChild')) or failed "Failed to append a child node";
43
44 failed "rootNode is undef" unless $child->rootNode;
45 failed "rootNode returned incorrect value" unless $child->rootNode == $this->Root;
46 };
47
48 test MoveNode => sub {
49 my ($this) = @_;
50
51 my $grandChild = $this->Root->firstChild->firstChild;
52 $this->Root->appendNode($grandChild);
53
54 failed "incorrect new parentNode value" unless ($grandChild->parentNode || 0) == $this->Root;
55 failed "incorrect new rootNode value" unless ($grandChild->rootNode || 0) == $this->Root;
56 };
57
58 test AppendRange => sub {
59 my ($this) = @_;
60
61 my $count = $this->Root->childNodes->Count;
62
63 $this->Root->appendRange(
64 map IMPL::DOM::Node->new(nodeName => "Item", nodeValue => $_),1..10
65 );
66
67 failed
68 "Wrong number of a child nodes",
69 "Expected: ".($count+10),
70 "Actual: ".$this->Root->childNodes->Count
71 unless $count + 10 == $this->Root->childNodes->Count;
72 };
73
74 test SelectNodes => sub {
75 my ($this) = @_;
76
77 my @result = $this->Root->selectNodes("Item");
78
79 failed
80 "Wrong number of a selected nodes",
81 "Expected: 10",
82 "Actual: ".scalar(@result)
83 unless @result == 10;
84 };
85
86 test SelectNodesByQuery => sub {
87 my ($this) = @_;
88
89 my @result = $this->Root->selectNodes(sub { $_[0]->nodeName =~ /child/i } );
90 failed
91 "Wrong number of a selected nodes",
92 "Expected: 2",
93 "Actual: ".scalar(@result)
94 unless @result == 2;
95 };
96
97 test CheckNodesValues => sub {
98 my ($this) = @_;
99
100 my @expected = (1..10);
101
102 my @result = map $_->nodeValue, grep $_->nodeValue, $this->Root->selectNodes("Item");
103
104 failed
105 "Some nodes returned wrong node values or in a wrong order",
106 "Expected: ".join(', ',@expected),
107 "Recieved: ".join(', ',@result)
108 unless cmparray(\@expected,\@result);
109
110 failed
111 "a text property of a root node returned a wrong value",
112 "Expected: @expected",
113 "Recieved: ". $this->Root->text
114 unless $this->Root->text eq join '',@expected;
115 };
116
117 test isComplex => sub {
118 my ($this) = @_;
119
120 failed "property isComplex returned false for the root node" unless $this->Root->isComplex;
121 failed "property isComplex returned true for a simple node", $this->Root->firstChild->nodeName if $this->Root->firstChild->isComplex;
31 }; 122 };
32 123
33 1; 124 1;