Debian Codegraph @

closure-library/closure/goog/datasource/fastdatanode_test.js

raw
1 // Copyright 2006 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.ds.FastDataNodeTest');
16 goog.setTestOnly('goog.ds.FastDataNodeTest');
17
18 goog.require('goog.array');
19 goog.require('goog.ds.DataManager');
20 goog.require('goog.ds.Expr');
21 goog.require('goog.ds.FastDataNode');
22 goog.require('goog.testing.jsunit');
23
24 var simpleObject;
25 var complexObject;
26 var dataChangeEvents;
27
28 function setUp() {
29 simpleObject = {Name: 'Jon Doe', Email: '[email protected]'};
30 complexObject = {
31 Name: 'Jon Doe',
32 Email: '[email protected]',
33 Emails: [
34 {Address: '[email protected]', Type: 'Home'},
35 {Address: '[email protected]', Type: 'Work'}
36 ],
37 GroupIds: [23, 42]
38 };
39 var dm = goog.ds.DataManager.getInstance();
40 dataChangeEvents = [];
41 dm.fireDataChange = function(dataPath) { dataChangeEvents.push(dataPath); };
42 }
43
44 function tearDown() {
45 goog.ds.DataManager.clearInstance();
46 }
47
48 function testGetChildNodeValue() {
49 var node = new goog.ds.FastDataNode(simpleObject, 'Simple');
50 var value = node.getChildNodeValue('Name');
51 assert(goog.isString(value));
52 assertEquals('Jon Doe', value);
53 }
54
55 function testDataNameAndPath() {
56 var node = new goog.ds.FastDataNode(simpleObject, 'Simple');
57 assertEquals('DataName should be \'Simple\'', 'Simple', node.getDataName());
58 assertEquals('DataPath should be \'Simple\'', 'Simple', node.getDataPath());
59 }
60
61 function testStringChildNode() {
62 var node = goog.ds.FastDataNode.fromJs(simpleObject, 'Simple');
63 var name = node.getChildNode('Name');
64 var email = node.getChildNode('Email');
65 assertEquals('Jon Doe', name.get());
66 assertEquals('[email protected]', email.get());
67
68 assertEquals('Name', name.getDataName());
69 assertEquals('Simple/Name', name.getDataPath());
70
71 assertEquals('Email', email.getDataName());
72 assertEquals('Simple/Email', email.getDataPath());
73 }
74
75 function testGetChildNodes() {
76 var node = goog.ds.FastDataNode.fromJs(simpleObject, 'Simple');
77 var children = node.getChildNodes();
78 assertEquals(2, children.getCount());
79 var childValues = [];
80 for (var i = 0; i < 2; ++i) {
81 childValues.push(children.getByIndex(i).get());
82 }
83 goog.array.sort(childValues);
84 assertEquals('Jon Doe', childValues[0]);
85 assertEquals('[email protected]', childValues[1]);
86 }
87
88
89 function testGetDistinguishesBetweenOverloads() {
90 var node = goog.ds.FastDataNode.fromJs(simpleObject, 'Simple');
91 assertEquals(node, node.get());
92 assertEquals('Jon Doe', node.getChildNodes().get('Name').get());
93 }
94
95
96 function testGetChildNodesForPrimitiveNodes() {
97 var node = goog.ds.FastDataNode.fromJs(simpleObject, 'Simple');
98 var children = node.getChildNode('Name').getChildNodes();
99 assertEquals(0, children.getCount());
100 }
101
102
103 function testFastListNode() {
104 var node = goog.ds.FastDataNode.fromJs(complexObject, 'Object');
105 assertEquals('Jon Doe', node.getChildNodeValue('Name'));
106 var emails = node.getChildNode('Emails');
107 assertEquals(
108 '[email protected]',
109 emails.getChildNode('[0]').getChildNodeValue('Address'));
110 assertEquals(
111 '[email protected]',
112 emails.getChildNode('[1]').getChildNodeValue('Address'));
113
114 assertEquals(
115 'Object/Emails/[0]/Address',
116 emails.getChildNode('[0]').getChildNode('Address').getDataPath());
117
118 var groups = node.getChildNode('GroupIds');
119 assertEquals(23, groups.getChildNode('[0]').get());
120 assertEquals(42, groups.getChildNodeValue('[1]'));
121
122 var childValues = emails.getChildNodes();
123 assertEquals(2, childValues.getCount());
124 assertEquals(
125 '[email protected]',
126 childValues.getByIndex(0).getChildNodeValue('Address'));
127 }
128
129
130 function testChildNodeValueForNonexistantAttribute() {
131 var node = goog.ds.FastDataNode.fromJs(complexObject, 'Object');
132 assertNull(node.getChildNodeValue('DoesNotExist'));
133 assertNull(node.getChildNode('Emails').getChildNodeValue('[666]'));
134 }
135
136
137 function testAllChildrenSelector() {
138 var node = goog.ds.FastDataNode.fromJs(complexObject, 'Object');
139 var allChildren = node.getChildNodes('*');
140 assertEquals(4, allChildren.getCount());
141
142 // not implemented, yet
143 // var nameChild = node.getChildNodes('Name');
144 // assertEquals(1, allChildren.getCount());
145 }
146
147 function testExpression() {
148 var node = goog.ds.FastDataNode.fromJs(complexObject, 'Object');
149 assertEquals('Jon Doe', goog.ds.Expr.create('Name').getValue(node));
150 assertEquals(
151 '[email protected]',
152 goog.ds.Expr.create('Emails/[1]/Address').getNode(node).get());
153 var emails = goog.ds.Expr.create('Emails/*').getNodes(node);
154 assertEquals(2, emails.getCount());
155 assertEquals(
156 '[email protected]',
157 emails.getByIndex(1).getChildNodeValue('Address'));
158 }
159
160 function testModifyNode() {
161 var node = goog.ds.FastDataNode.fromJs(complexObject, 'Object');
162 node.getChildNode('Name').set('Foo Bar');
163 assertEquals('Foo Bar', node.getChildNodeValue('Name'));
164 }
165
166 function testClone() {
167 var node = goog.ds.FastDataNode.fromJs(complexObject, 'Object');
168 var clone = node.clone();
169 node.getChildNode('Name').set('Foo Bar');
170 assertEquals('Jon Doe', clone.getChildNodeValue('Name'));
171 var expr = goog.ds.Expr.create('Emails/[1]/Address');
172 expr.getNode(clone).set('[email protected]');
173 assertEquals('[email protected]', expr.getValue(node));
174 assertEquals('[email protected]', expr.getValue(clone));
175 }
176
177 function testSetChildNodeOnList() {
178 var list = goog.ds.FastDataNode.fromJs([], 'list');
179 var node = goog.ds.FastDataNode.fromJs({Id: '42', Name: 'Foo'}, '42', list);
180 list.setChildNode('42', node);
181
182 assertEquals(node, list.getChildNode('42'));
183 assertEquals(node, list.getChildNodes().getByIndex(0));
184 assertEquals(node, list.getChildNodes().get('42'));
185 }
186
187 function testCreateNewValueWithSetChildNode() {
188 var node = goog.ds.FastDataNode.fromJs({}, 'object');
189 node.setChildNode('Foo', 'Bar');
190 assertEquals('Bar', node.getChildNodeValue('Foo'));
191 }
192
193
194 function testSetChildNotWithNull_object() {
195 var node = new goog.ds.FastDataNode({Foo: 'Bar'}, 'test');
196 node.setChildNode('Foo', null);
197 assertNull('node should not have a Foo child', node.getChildNode('Foo'));
198 assertEquals(
199 'node should not have any children', 0, node.getChildNodes().getCount());
200 }
201
202 function testSetChildNotWithNull_list() {
203 var list = goog.ds.FastDataNode.fromJs([], 'list');
204 list.setChildNode('foo', 'bar');
205 list.setChildNode('gee', 'wizz');
206 assertEquals('bar', list.getChildNodeValue('foo'));
207 assertEquals('wizz', list.getChildNodes().getByIndex(1).get());
208 list.setChildNode('foo', null);
209 assertEquals(1, list.getChildNodes().getCount());
210 assertEquals('wizz', list.getChildNodeValue('gee'));
211 assertEquals('wizz', list.getChildNodes().getByIndex(0).get());
212 }
213
214 function testNodeListIndexesOnId() {
215 var list = goog.ds.FastDataNode.fromJs([{id: '^Freq', value: 'foo'}], 'list');
216 assertEquals('foo', list.getChildNode('^Freq').getChildNodeValue('value'));
217 list.setChildNode('^Temp', {id: '^Temp', value: 'bar'});
218 assertEquals('bar', list.getChildNode('^Temp').getChildNodeValue('value'));
219 }
220
221 function verifyDataChangeEvents(expected) {
222 assertEquals(expected.length, dataChangeEvents.length);
223 for (var i = 0; i < expected.length; ++i) {
224 assertEquals(expected[i], dataChangeEvents[i]);
225 }
226 dataChangeEvents = [];
227 }
228
229 function testFireDataChangeOnSet() {
230 var node = new goog.ds.FastDataNode(simpleObject, 'Simple');
231 node.getChildNode('Name').set('Foo Bar');
232 verifyDataChangeEvents(['Simple/Name']);
233 }
234
235 function testFireDataChangeOnSetChildNode_object() {
236 var node = new goog.ds.FastDataNode(simpleObject, 'Simple');
237 node.setChildNode('Name', 'Foo Bar');
238 node.setChildNode('Email', null);
239 verifyDataChangeEvents(['Simple/Name', 'Simple/Email']);
240 }
241
242 function testFireDataChangeOnSetChildNode_list() {
243 var node = new goog.ds.FastDataNode(complexObject, 'Node');
244 node.getChildNode('GroupIds').setChildNode('[0]', 1001);
245 verifyDataChangeEvents(['Node/GroupIds/[0]']);
246
247 node.getChildNode('GroupIds').getChildNodes().add(1002);
248 verifyDataChangeEvents(
249 ['Node/GroupIds/[2]', 'Node/GroupIds', 'Node/GroupIds/count()']);
250
251 node.getChildNode('GroupIds').setChildNode('foo', 1003);
252 verifyDataChangeEvents(
253 ['Node/GroupIds/foo', 'Node/GroupIds', 'Node/GroupIds/count()']);
254 }