fastdatanode_test.js 8.6 KB

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