toolkit/devtools/webconsole/test/unit/test_js_property_provider.js

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:4de6670413fd
1 /* -*- js2-basic-offset: 2; indent-tabs-mode: nil; -*- */
2 // Any copyright is dedicated to the Public Domain.
3 // http://creativecommons.org/publicdomain/zero/1.0/
4
5 "use strict";
6 const { devtools } = Components.utils.import("resource://gre/modules/devtools/Loader.jsm", {});
7 let JSPropertyProvider = devtools.require("devtools/toolkit/webconsole/utils").JSPropertyProvider;
8
9 Components.utils.import("resource://gre/modules/jsdebugger.jsm");
10 addDebuggerToGlobal(this);
11
12 function run_test() {
13 const testArray = 'var testArray = [\
14 {propA: "A"},\
15 {\
16 propB: "B", \
17 propC: [\
18 {propD: "D"}\
19 ]\
20 },\
21 [\
22 {propE: "E"}\
23 ]\
24 ];'
25
26 const testObject = 'var testObject = {"propA": [{"propB": "B"}]}';
27
28 let sandbox = Components.utils.Sandbox("http://example.com");
29 let dbg = new Debugger;
30 let dbgObject = dbg.addDebuggee(sandbox);
31 Components.utils.evalInSandbox(testArray, sandbox);
32 Components.utils.evalInSandbox(testObject, sandbox);
33
34 let results = JSPropertyProvider(dbgObject, null, "testArray[0].");
35 do_print("Test that suggestions are given for 'foo[n]' where n is an integer.");
36 test_has_result(results, "propA");
37
38 do_print("Test that suggestions are given for multidimensional arrays.");
39 results = JSPropertyProvider(dbgObject, null, "testArray[2][0].");
40 test_has_result(results, "propE");
41
42 do_print("Test that suggestions are not given for index that's out of bounds.");
43 results = JSPropertyProvider(dbgObject, null, "testArray[10].");
44 do_check_null(results);
45
46 do_print("Test that no suggestions are given if an index is not numerical somewhere in the chain.");
47 results = JSPropertyProvider(dbgObject, null, "testArray[0]['propC'][0].");
48 do_check_null(results);
49
50 results = JSPropertyProvider(dbgObject, null, "testObject['propA'][0].");
51 do_check_null(results);
52
53 results = JSPropertyProvider(dbgObject, null, "testArray[0]['propC'].");
54 do_check_null(results);
55
56 results = JSPropertyProvider(dbgObject, null, "testArray[][1].");
57 do_check_null(results);
58 }
59
60 /**
61 * A helper that ensures (required) results were found.
62 * @param Object aResults
63 * The results returned by JSPropertyProvider.
64 * @param String aRequiredSuggestion
65 * A suggestion that must be found from the results.
66 */
67 function test_has_result(aResults, aRequiredSuggestion) {
68 do_check_neq(aResults, null);
69 do_check_true(aResults.matches.length > 0);
70 do_check_true(aResults.matches.indexOf(aRequiredSuggestion) !== -1);
71 }

mercurial