1 | dojo.provide("dojox.jsonPath.tests.jsonPath"); |
---|
2 | dojo.require("dojox.jsonPath"); |
---|
3 | |
---|
4 | |
---|
5 | dojox.jsonPath.tests.error = function(t, d, errData){ |
---|
6 | // summary: |
---|
7 | // The error callback function to be used for all of the tests. |
---|
8 | d.errback(errData); |
---|
9 | }; |
---|
10 | |
---|
11 | dojox.jsonPath.tests.testData= { |
---|
12 | store: { |
---|
13 | "book": [ |
---|
14 | { |
---|
15 | "category":"reference", |
---|
16 | "author":"Nigel Rees", |
---|
17 | "title":"Sayings of the Century", |
---|
18 | "price":8.95 |
---|
19 | }, |
---|
20 | { |
---|
21 | "category":"fiction", |
---|
22 | "author":"Evelyn Waugh", |
---|
23 | "title":"Sword of Honour", |
---|
24 | "price":12.99 |
---|
25 | }, |
---|
26 | { |
---|
27 | "category":"fiction", |
---|
28 | "author":"Herman Melville", |
---|
29 | "title":"Moby Dick", |
---|
30 | "isbn":"0-553-21311-3", |
---|
31 | "price":8.99 |
---|
32 | }, |
---|
33 | { |
---|
34 | "category":"fiction", |
---|
35 | "author":"J. R. R. Tolkien", |
---|
36 | "title":"The Lord of the Rings", |
---|
37 | "isbn":"0-395-19395-8", |
---|
38 | "price":22.99 |
---|
39 | } |
---|
40 | ], |
---|
41 | "bicycle": { |
---|
42 | "color":"red", |
---|
43 | "price":19.95 |
---|
44 | } |
---|
45 | }, |
---|
46 | "symbols":{"@.$;":5} |
---|
47 | } |
---|
48 | |
---|
49 | doh.register("dojox.jsonPath.tests.jsonPath", |
---|
50 | [ |
---|
51 | { |
---|
52 | name: "$.store.book[*].author", |
---|
53 | runTest: function(t) { |
---|
54 | var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, this.name)); |
---|
55 | var success = '["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]'; |
---|
56 | doh.assertEqual(success,result); |
---|
57 | } |
---|
58 | }, |
---|
59 | { |
---|
60 | name: "$..author", |
---|
61 | runTest: function(t) { |
---|
62 | var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, this.name)); |
---|
63 | var success = '["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]'; |
---|
64 | doh.assertEqual(success,result); |
---|
65 | |
---|
66 | } |
---|
67 | }, |
---|
68 | { |
---|
69 | name: "$.store.*", |
---|
70 | runTest: function(t) { |
---|
71 | var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, this.name)); |
---|
72 | var success = '[[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}],{"color":"red","price":19.95}]'; |
---|
73 | doh.assertEqual(success,result); |
---|
74 | } |
---|
75 | }, |
---|
76 | { |
---|
77 | name: "$.store..price", |
---|
78 | runTest: function(t) { |
---|
79 | var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, this.name)); |
---|
80 | var success = '[8.95,12.99,8.99,22.99,19.95]'; |
---|
81 | doh.assertEqual(success,result); |
---|
82 | } |
---|
83 | }, |
---|
84 | { |
---|
85 | name: "$..book[(@.length-1)]", |
---|
86 | runTest: function(t) { |
---|
87 | var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, this.name)); |
---|
88 | var success = '[{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}]'; |
---|
89 | doh.assertEqual(success,result); |
---|
90 | } |
---|
91 | }, |
---|
92 | { |
---|
93 | name: "$..book[-1:]", |
---|
94 | runTest: function(t) { |
---|
95 | var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, this.name)); |
---|
96 | var success = '[{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}]'; |
---|
97 | doh.assertEqual(success,result); |
---|
98 | } |
---|
99 | }, |
---|
100 | { |
---|
101 | name: "$..book[0,1]", |
---|
102 | runTest: function(t) { |
---|
103 | var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, this.name)); |
---|
104 | var success = '[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99}]'; |
---|
105 | doh.assertEqual(success,result); |
---|
106 | } |
---|
107 | }, |
---|
108 | { |
---|
109 | name: "$..book[:2]", |
---|
110 | runTest: function(t) { |
---|
111 | var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, this.name)); |
---|
112 | var success = '[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99}]'; |
---|
113 | doh.assertEqual(success,result); |
---|
114 | } |
---|
115 | }, |
---|
116 | { |
---|
117 | name: "$..book[?(@.isbn)]", |
---|
118 | runTest: function(t) { |
---|
119 | var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, this.name)); |
---|
120 | var success = '[{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}]'; |
---|
121 | doh.assertEqual(success,result); |
---|
122 | } |
---|
123 | }, |
---|
124 | { |
---|
125 | name: "$..book[?(@.price<10)]", |
---|
126 | runTest: function(t) { |
---|
127 | var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, this.name)); |
---|
128 | var success = '[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99}]'; |
---|
129 | doh.assertEqual(success,result); |
---|
130 | } |
---|
131 | }, |
---|
132 | { |
---|
133 | name: "$.symbols[*]", |
---|
134 | runTest: function(t) { |
---|
135 | var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, this.name)); |
---|
136 | var success = '[5]'; |
---|
137 | doh.assertEqual(success,result); |
---|
138 | } |
---|
139 | }, |
---|
140 | { |
---|
141 | name: "$.symbols['@.$;']", |
---|
142 | runTest: function(t) { |
---|
143 | var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, this.name)); |
---|
144 | var success = '[5]'; |
---|
145 | doh.assertEqual(success,result); |
---|
146 | } |
---|
147 | }, |
---|
148 | { |
---|
149 | name: "$.symbols[(@[('@.$;')]?'@.$;':'@.$;')]", |
---|
150 | runTest: function(t) { |
---|
151 | var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, this.name)); |
---|
152 | var success = '[5]'; |
---|
153 | doh.assertEqual(success,result); |
---|
154 | } |
---|
155 | }, |
---|
156 | { |
---|
157 | name: "resultType: 'BOTH' test", |
---|
158 | runTest: function(t) { |
---|
159 | var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, "$..book[*]",{resultType:"BOTH"})); |
---|
160 | var success = dojo.toJson([{"path": "$['store']['book'][0]", "value": {"category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95}}, {"path": "$['store']['book'][1]", "value": {"category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99}}, {"path": "$['store']['book'][2]", "value": {"category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99}}, {"path": "$['store']['book'][3]", "value": {"category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99}}]); |
---|
161 | doh.assertEqual(success,result); |
---|
162 | } |
---|
163 | }, |
---|
164 | { |
---|
165 | name: "evalType: 'RESULT' test $.store.book[?(@.price<15)][1:3]", |
---|
166 | runTest: function(t) { |
---|
167 | var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, "$.store.book[?(@.price<15)][1:3]",{evalType:"RESULT"})); |
---|
168 | var success = '[{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99}]'; |
---|
169 | doh.assertEqual(success,result); |
---|
170 | } |
---|
171 | }, |
---|
172 | { |
---|
173 | name: "evalType: 'RESULT' test $.store.book[1].category", |
---|
174 | runTest: function(t) { |
---|
175 | var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, "$.store.book[1].category",{evalType:"RESULT"})); |
---|
176 | var success = '"fiction"'; |
---|
177 | doh.assertEqual(success,result); |
---|
178 | } |
---|
179 | }, |
---|
180 | { |
---|
181 | name: "evalType: 'RESULT' test $.store.bicycle", |
---|
182 | runTest: function(t) { |
---|
183 | var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, "$.store.bicycle",{evalType:"RESULT"})); |
---|
184 | var success = '{"color":"red","price":19.95}'; |
---|
185 | doh.assertEqual(success,result); |
---|
186 | } |
---|
187 | }, |
---|
188 | { |
---|
189 | name: "evalType: 'RESULT' test $.store.book[*]", |
---|
190 | runTest: function(t) { |
---|
191 | var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, "$.store.book[*]",{evalType:"RESULT"})); |
---|
192 | var success = '["reference","Nigel Rees","Sayings of the Century",8.95,"fiction","Evelyn Waugh","Sword of Honour",12.99,"fiction","Herman Melville","Moby Dick","0-553-21311-3",8.99,"fiction","J. R. R. Tolkien","The Lord of the Rings","0-395-19395-8",22.99]'; |
---|
193 | doh.assertEqual(success,result); |
---|
194 | } |
---|
195 | }, |
---|
196 | { |
---|
197 | name: "evalType: 'RESULT' test $.store.book.category", |
---|
198 | runTest: function(t) { |
---|
199 | var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, "$.store.book.category",{evalType:"RESULT"})); |
---|
200 | var success = '["reference","fiction","fiction","fiction"]'; |
---|
201 | doh.assertEqual(success,result); |
---|
202 | } |
---|
203 | } |
---|
204 | ] |
---|
205 | ); |
---|
206 | |
---|