Debian Codegraph @

closure-library/closure/goog/dom/pattern/matcher_test.js

raw
1 // Copyright 2007 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.dom.pattern.matcherTest');
16 goog.setTestOnly('goog.dom.pattern.matcherTest');
17
18 goog.require('goog.dom');
19 goog.require('goog.dom.TagName');
20 goog.require('goog.dom.pattern.EndTag');
21 goog.require('goog.dom.pattern.FullTag');
22 goog.require('goog.dom.pattern.Matcher');
23 goog.require('goog.dom.pattern.Repeat');
24 goog.require('goog.dom.pattern.Sequence');
25 goog.require('goog.dom.pattern.StartTag');
26 goog.require('goog.dom.pattern.callback.Counter');
27 goog.require('goog.dom.pattern.callback.Test');
28 goog.require('goog.iter.StopIteration');
29 goog.require('goog.testing.jsunit');
30
31 function testMatcherAndStartTag() {
32 var pattern = new goog.dom.pattern.StartTag('P');
33
34 var counter = new goog.dom.pattern.callback.Counter();
35 var matcher = new goog.dom.pattern.Matcher();
36 matcher.addPattern(pattern, counter.getCallback());
37 matcher.match(document.body);
38
39 assertEquals('StartTag(p) should match 5 times in body', 5, counter.count);
40 }
41
42 function testMatcherAndStartTagTwice() {
43 var pattern = new goog.dom.pattern.StartTag('P');
44
45 var counter = new goog.dom.pattern.callback.Counter();
46 var matcher = new goog.dom.pattern.Matcher();
47 matcher.addPattern(pattern, counter.getCallback());
48 matcher.match(document.body);
49
50 assertEquals('StartTag(p) should match 5 times in body', 5, counter.count);
51
52 // Make sure no state got mangled.
53 counter.reset();
54 matcher.match(document.body);
55
56 assertEquals(
57 'StartTag(p) should match 5 times in body again', 5, counter.count);
58 }
59
60 function testMatcherAndStartTagAttributes() {
61 var pattern = new goog.dom.pattern.StartTag('SPAN', {id: /./});
62
63 var counter = new goog.dom.pattern.callback.Counter();
64 var matcher = new goog.dom.pattern.Matcher();
65 matcher.addPattern(pattern, counter.getCallback());
66 matcher.match(document.body);
67
68 assertEquals(
69 'StartTag(span,id) should match 2 times in body', 2, counter.count);
70 }
71
72 function testMatcherWithTwoPatterns() {
73 var pattern1 = new goog.dom.pattern.StartTag('SPAN');
74 var pattern2 = new goog.dom.pattern.StartTag('P');
75
76 var counter = new goog.dom.pattern.callback.Counter();
77
78 var matcher = new goog.dom.pattern.Matcher();
79 matcher.addPattern(pattern1, counter.getCallback());
80 matcher.addPattern(pattern2, counter.getCallback());
81
82 matcher.match(document.body);
83
84 assertEquals(
85 'StartTag(span|p) should match 8 times in body', 8, counter.count);
86 }
87
88 function testMatcherWithQuit() {
89 var pattern1 = new goog.dom.pattern.StartTag('SPAN');
90 var pattern2 = new goog.dom.pattern.StartTag('P');
91
92 var count = 0;
93 var callback = function(node, position) {
94 if (node.nodeName == goog.dom.TagName.SPAN) {
95 throw goog.iter.StopIteration;
96 return true;
97 }
98 count++;
99 };
100
101 var matcher = new goog.dom.pattern.Matcher();
102 matcher.addPattern(pattern1, callback);
103 matcher.addPattern(pattern2, callback);
104
105 matcher.match(document.body);
106
107 assertEquals('Stopped span|p should match 1 time in body', 1, count);
108 }
109
110 function testMatcherWithReplace() {
111 var pattern1 = new goog.dom.pattern.StartTag('B');
112 var pattern2 = new goog.dom.pattern.StartTag('I');
113
114 var count = 0;
115 var callback = function(node, position) {
116 count++;
117 if (node.nodeName == goog.dom.TagName.B) {
118 var i = goog.dom.createDom(goog.dom.TagName.I);
119 node.parentNode.insertBefore(i, node);
120 goog.dom.removeNode(node);
121
122 position.setPosition(i);
123
124 return true;
125 }
126 };
127
128 var matcher = new goog.dom.pattern.Matcher();
129 matcher.addPattern(pattern1, callback);
130 matcher.addPattern(pattern2, callback);
131
132 matcher.match(goog.dom.getElement('div1'));
133
134 assertEquals('i|b->i should match 5 times in div1', 5, count);
135 }
136
137 function testMatcherAndFullTag() {
138 var pattern = new goog.dom.pattern.FullTag('P');
139
140 var test = new goog.dom.pattern.callback.Test();
141
142 var matcher = new goog.dom.pattern.Matcher();
143 matcher.addPattern(pattern, test.getCallback());
144
145 matcher.match(goog.dom.getElement('p1'));
146
147 assert('FullTag(p) should match on p1', test.matched);
148
149 test.reset();
150 matcher.match(goog.dom.getElement('div1'));
151
152 assert('FullTag(p) should not match on div1', !test.matched);
153 }
154
155 function testMatcherAndSequence() {
156 var pattern = new goog.dom.pattern.Sequence(
157 [
158 new goog.dom.pattern.StartTag('P'),
159 new goog.dom.pattern.StartTag('SPAN'),
160 new goog.dom.pattern.EndTag('SPAN'), new goog.dom.pattern.EndTag('P')
161 ],
162 true);
163
164 var counter = new goog.dom.pattern.callback.Counter();
165 var matcher = new goog.dom.pattern.Matcher();
166 matcher.addPattern(pattern, counter.getCallback());
167 matcher.match(document.body);
168
169 assertEquals('Sequence should match 1 times in body', 1, counter.count);
170 }
171
172 function testMatcherAndRepeatFullTag() {
173 var pattern =
174 new goog.dom.pattern.Repeat(new goog.dom.pattern.FullTag('P'), 1);
175
176 var count = 0;
177 var tcount = 0;
178 var matcher = new goog.dom.pattern.Matcher();
179 matcher.addPattern(pattern, function() {
180 count++;
181 tcount += pattern.count;
182 });
183 matcher.match(document.body);
184
185 assertEquals('Repeated p should match 2 times in body', 2, count);
186 assertEquals('Repeated p should match 5 total times in body', 5, tcount);
187 }