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