expr_test.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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.ExprTest');
  15. goog.setTestOnly('goog.ds.ExprTest');
  16. goog.require('goog.ds.DataManager');
  17. goog.require('goog.ds.Expr');
  18. goog.require('goog.ds.JsDataSource');
  19. goog.require('goog.testing.jsunit');
  20. var jsDs;
  21. var jsObj = {
  22. Success: true,
  23. Errors: [],
  24. Body: {
  25. Contacts: [
  26. {Name: 'John Doe', Email: 'john@gmail.com', EmailCount: 300},
  27. {Name: 'Jane Doh', Email: 'jane@gmail.com'},
  28. {Name: 'Steve Smith', Email: 'steve@gmail.com', EmailCount: 305},
  29. {Name: 'John Smith', Email: 'smith@gmail.com'},
  30. {Name: 'Homer Simpson', Email: 'homer@gmail.com'},
  31. {Name: 'Bart Simpson', Email: 'bart@gmail.com'}
  32. ]
  33. }
  34. };
  35. function setUp() {
  36. jsDs = new goog.ds.JsDataSource(jsObj, 'JS', null);
  37. var dm = goog.ds.DataManager.getInstance();
  38. dm.addDataSource(jsDs, true);
  39. }
  40. function testBasicStuff() {
  41. assertNotNull('Get Body', goog.ds.Expr.create('$JS/Body').getNode());
  42. }
  43. function testArrayExpressions() {
  44. assertEquals(
  45. 6, goog.ds.Expr.create('$JS/Body/Contacts/*').getNodes().getCount());
  46. assertEquals(
  47. 'John Doe', goog.ds.Expr.create('$JS/Body/Contacts/[0]/Name').getValue());
  48. assertEquals(
  49. 305, goog.ds.Expr.create('$JS/Body/Contacts/[2]/EmailCount').getValue());
  50. assertEquals(
  51. 6, goog.ds.Expr.create('$JS/Body/Contacts/*/count()').getValue());
  52. assertEquals(0, goog.ds.Expr.create('$JS/Errors/*/count()').getValue());
  53. }
  54. function testCommonExpressions() {
  55. assertTrue(goog.ds.Expr.create('.').isCurrent_);
  56. assertFalse(goog.ds.Expr.create('Bob').isCurrent_);
  57. assertTrue(goog.ds.Expr.create('*|text()').isAllChildNodes_);
  58. assertFalse(goog.ds.Expr.create('Bob').isAllChildNodes_);
  59. assertTrue(goog.ds.Expr.create('@*').isAllAttributes_);
  60. assertFalse(goog.ds.Expr.create('Bob').isAllAttributes_);
  61. assertTrue(goog.ds.Expr.create('*').isAllElements_);
  62. assertFalse(goog.ds.Expr.create('Bob').isAllElements_);
  63. }
  64. function testIndexExpressions() {
  65. assertEquals(goog.ds.Expr.create('node/[5]').getNext().size_, 1);
  66. assertEquals(goog.ds.Expr.create('node/[5]').getNext().parts_[0], '[5]');
  67. }