2 <!-- Submitted from TestTWF Paris -->
5 <link rel=help
href=
"http://dvcs.w3.org/hg/IndexedDB/raw-file/tip/Overview.html#key-path-construct">
6 <link rel=assert
title=
"A key path is a DOMString that defines how to extract a key from a value. A valid key path is either the empty string, a JavaScript identifier, or multiple Javascript identifiers separated by periods (ASCII character code 46) [ECMA-262].">
7 <script src=
"../../../resources/testharness.js"></script>
8 <script src=
"../../../resources/testharnessreport.js"></script>
9 <script src=
"support.js"></script>
13 var global_db
= createdb_for_multiple_tests();
15 function keypath(keypath
, objects
, expected_keys
, desc
) {
17 t
= async_test(document
.title
+ " - " + (desc
? desc
: keypath
)),
19 store_name
= "store-"+(Date
.now())+Math
.random();
21 var open_rq
= global_db
.setTest(t
);
22 open_rq
.onupgradeneeded = function(e
) {
24 var objStore
= db
.createObjectStore(store_name
, { keyPath
: keypath
});
26 for (var i
= 0; i
< objects
.length
; i
++)
27 objStore
.add(objects
[i
]);
30 open_rq
.onsuccess = function(e
) {
32 rq
= db
.transaction(store_name
)
33 .objectStore(store_name
)
36 rq
.onsuccess
= t
.step_func(function(e
) {
37 var cursor
= e
.target
.result
;
40 actual_keys
.push(cursor
.key
.valueOf());
44 assert_key_equals(actual_keys
, expected_keys
, "keyorder array");
52 [ { my
: { key
: 10 } } ],
56 [ { my
: { k
øi
: 5 } } ],
60 [ { my
: { key_ya
: 10 } } ],
63 keypath('public.key$ya',
64 [ { public: { key
$ya
: 10 } } ],
68 [ { true: { $: 10 } } ],
72 [ { my
: { _
: 10 } } ],
76 [ { delete: { a7
: 10 } } ],
79 keypath('p.p.p.p.p.p.p.p.p.p.p.p.p.p',
80 [ {p
:{p
:{p
:{p
:{p
:{p
:{p
:{p
:{p
:{p
:{p
:{p
:{p
:{p
:10}}}}}}}}}}}}}} ],
84 [ { str
: "pony" }, { str
: "my" }, { str
: "little" }, { str
: "" } ],
88 [ {arr
: [0, 0, 0, 0]}, {arr
: [{}, 0, "hei", "length", Infinity
, []]}, {arr
: [10, 10]}, { arr
: []} ],
92 [ [10, 10], "123", { length
: 20 } ],
96 [ ["bags"], "bean", 10 ],
97 [ 10, "bean", ["bags"] ],
98 "'' uses value as key");
101 [ ["bags"], "bean", 10 ],
102 [ [10], ["bean"] , [["bags"]] ],
103 "[''] uses value as [key]");
106 [ {x
:10, y
:20}, {y
:1.337, x
:100} ],
107 [ [10, 20], [100, 1.337] ],
110 keypath([['x'], ['y']],
111 [ {x
:10, y
:20}, {y
:1.337, x
:100} ],
112 [ [10, 20], [100, 1.337] ],
113 "[['x'], 'y'] (stringifies)");
115 keypath(['x', {toString:function(){return 'y'}}],
116 [ {x
:10, y
:20}, {y
:1.337, x
:100} ],
117 [ [10, 20], [100, 1.337] ],
118 "['x', {toString->'y'}] (stringifies)");
121 var myblob
= Blob(["Yoda"], {type
:'suprawsum'});
122 keypath(['length', 'type'],
125 "[Blob.length, Blob.type]");
128 // File.name and File.lastModifiedDate is not testable automatically
130 keypath(['name', 'type'],
131 [ { name
: "orange", type
: "fruit" }, { name
: "orange", type
: ["telecom", "french"] } ],
132 [ ["orange", "fruit"], ["orange", ["telecom", "french"]] ]);
134 keypath(['name', 'type.name'],
135 [ { name
: "orange", type
: { name
: "fruit" }}, { name
: "orange", type
: { name
: "telecom" }} ],
136 [ ["orange", "fruit"], ["orange", "telecom" ] ]);
139 loop_array
.push(loop_array
);
142 [ [1], ["a"], [["k"]] ],
143 "array loop -> stringify becomes ['']");