49
|
1 package Test::DOM::Navigator;
|
166
|
2 use parent qw(IMPL::Test::Unit);
|
49
|
3
|
|
4 __PACKAGE__->PassThroughArgs;
|
|
5
|
|
6 use IMPL::Test qw(test failed);
|
|
7 use IMPL::DOM::Navigator;
|
|
8 use IMPL::DOM::Navigator::SchemaNavigator;
|
|
9 use IMPL::DOM::Node;
|
|
10 use IMPL::DOM::Schema;
|
|
11 use IMPL::Class::Property;
|
|
12
|
|
13 BEGIN {
|
|
14 public property doc => prop_all;
|
|
15 }
|
|
16
|
|
17 sub CTOR {
|
|
18 my ($this) = @_;
|
|
19
|
|
20 $this->doc(
|
|
21 IMPL::DOM::Node->new(nodeName => 'root')->appendRange(
|
|
22 IMPL::DOM::Node->new(nodeName=> 'age', nodeValue => 21),
|
|
23 IMPL::DOM::Node->new(nodeName=> 'address')->appendRange(
|
|
24 IMPL::DOM::Node->new(nodeName=>'city', nodeValue=>'moscow'),
|
|
25 IMPL::DOM::Node->new(nodeName=>'street', nodeValue=>'main'),
|
|
26 IMPL::DOM::Node->new(nodeName=>'phone',nodeValue=>'123-456'),
|
|
27 ),
|
|
28 IMPL::DOM::Node->new(nodeName=> 'address')->appendRange(
|
|
29 IMPL::DOM::Node->new(nodeName=>'city', nodeValue=>'San Francisco'),
|
|
30 IMPL::DOM::Node->new(nodeName=>'street', nodeValue=>'Libertador'),
|
|
31 ),
|
|
32 IMPL::DOM::Node->new(nodeName=> 'contacts')->appendRange(
|
|
33 IMPL::DOM::Node->new(nodeName=>'phone',nodeValue=>'123-123'),
|
|
34 IMPL::DOM::Node->new(nodeName=>'phone',nodeValue=>'1-233-434-34-54'),
|
|
35 IMPL::DOM::Node->new(nodeName=>'email',nodeValue=>'some@mail.none')
|
|
36 )
|
|
37
|
|
38 )
|
|
39 );
|
|
40 }
|
|
41
|
|
42 test Creation => sub {
|
|
43 my ($this) = @_;
|
|
44
|
|
45 my $doc = new IMPL::DOM::Node(nodeName => 'root');
|
|
46
|
|
47 my $obj = new IMPL::DOM::Navigator($doc) or failed "Failed to create instance" ;
|
|
48 };
|
|
49
|
|
50 test Navigate => sub {
|
|
51 my $doc = new IMPL::DOM::Node(nodeName => 'root');
|
|
52 my $child = $doc->insertNode(
|
|
53 new IMPL::DOM::Node(
|
|
54 nodeName => 'Child'
|
|
55 )
|
|
56 );
|
|
57
|
|
58 my $navi = new IMPL::DOM::Navigator($doc);
|
|
59 my $navresult = $navi->Navigate("Child");
|
|
60
|
|
61 failed ("Navigate retuned unexpected result", "Recieved: $navresult", "Expected: $child") if $child != $navresult;
|
|
62 my $curr = $navi->Current;
|
|
63 failed ("Current node has a wrong value","Current: $curr","Expected: $child") if $child != $curr;
|
|
64 };
|
|
65
|
|
66 test PathToString => sub {
|
|
67 my $doc = new IMPL::DOM::Node(nodeName => 'root');
|
|
68 my $child = $doc->insertNode(
|
|
69 new IMPL::DOM::Node(
|
|
70 nodeName => 'Child'
|
|
71 )
|
|
72 );
|
|
73
|
|
74 my $navi = new IMPL::DOM::Navigator($doc);
|
|
75 $navi->Navigate("Child");
|
|
76
|
|
77 my $expected = "root/Child";
|
|
78 my $res = $navi->PathToString;
|
|
79 failed("Unexpected PathToString result","Recieved: $res","Expected: $expected") if $res ne $expected;
|
|
80 };
|
|
81
|
|
82 test Back => sub {
|
|
83 my $doc = new IMPL::DOM::Node(nodeName => 'root');
|
|
84 my $child = $doc->insertNode(
|
|
85 new IMPL::DOM::Node(
|
|
86 nodeName => 'Child'
|
|
87 )
|
|
88 );
|
|
89
|
|
90 my $navi = new IMPL::DOM::Navigator($doc);
|
|
91 $navi->Navigate("Child");
|
|
92 my $navresult = $navi->Back;
|
|
93
|
|
94 failed ("Back() retuned unexpected result", "Recieved: $navresult", "Expected: $doc") if $doc != $navresult;
|
|
95 my $curr = $navi->Current;
|
|
96 failed ("Current node has a wrong value","Current: $curr","Expected: $doc") if $doc != $curr;
|
|
97 };
|
|
98
|
|
99 test selectNodes1 => sub {
|
|
100 my ($this) = @_;
|
|
101
|
|
102 my $navi = new IMPL::DOM::Navigator($this->doc);
|
|
103 my @result = $navi->selectNodes('contacts','phone');
|
|
104 failed "Expected to get two entries, but got:",map($_->nodeName,@result) unless @result == 2;
|
|
105 };
|
|
106
|
|
107 test selectNodes2 => sub {
|
|
108 my ($this) = @_;
|
|
109
|
|
110 my $navi = new IMPL::DOM::Navigator($this->doc);
|
|
111 my @result = $navi->selectNodes(undef,'phone');
|
|
112 failed "Expected to get three entries, but got:",map($_->nodeName,@result) unless @result == 3;
|
|
113 };
|
|
114
|
|
115 test FetchDoeachState => sub {
|
|
116 my ($this) = @_;
|
|
117
|
|
118 my $navi = new IMPL::DOM::Navigator($this->doc);
|
|
119
|
|
120 $navi->Navigate(undef,'phone');
|
|
121
|
|
122 $navi->saveState();
|
|
123
|
|
124 my @result;
|
|
125 doeach $navi sub {
|
|
126 push @result,$_;
|
|
127 };
|
|
128
|
|
129 failed "Expected to get three nodes, but got: ", map($_->nodeName,@result) unless @result == 3;
|
|
130
|
|
131 $navi->restoreState();
|
|
132 @result = ();
|
|
133
|
|
134 push @result, $_ while fetch $navi;
|
|
135
|
|
136 failed "Expected to get three nodes, but got: ", map($_->nodeName,@result) unless @result == 3;
|
|
137 };
|
|
138
|
|
139 test NavigateSchema => sub {
|
|
140 my $navi = new IMPL::DOM::Navigator::SchemaNavigator(IMPL::DOM::Schema->MetaSchema);
|
|
141
|
|
142 my $root = $navi->NavigateName('schema') or failed "Failed to navigate to the root element";
|
|
143
|
|
144 $navi->saveState;
|
|
145 $navi->NavigateName('Node') or failed "Failed to navigate to a simple node";
|
|
146 $navi->restoreState;
|
|
147
|
|
148 failed "Can't navigate from simple node" if $navi->dosafe(sub { $navi->NavigateName('Property') || die } );
|
|
149
|
|
150 $navi->NavigateName('ComplexType') or failed "Failed to navigate to a complex node";
|
|
151 $navi->NavigateName('NodeSet') or failed "Failed to navigate to NodeSet";
|
|
152 $navi->SchemaBack();
|
|
153 $navi->NavigateName('NodeList') or failed "Failed to navigate to NodeList";
|
|
154 $navi->NavigateName('SimpleNode') or failed "Failed to navigate to SimpleNode";
|
|
155 $navi->NavigateName('Enum') or failed "Failed to navigate to Enum";
|
|
156 $navi->NavigateName('Item') or failed "Failed to navigate to Item";
|
|
157
|
|
158 };
|
|
159
|
|
160
|
|
161 1;
|