Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / imported / web-platform-tests / IndexedDB / keypath.htm
blob6d5ec86996c13751e91d11bbf66a6c059dc045ee
1 <!DOCTYPE html>
2 <!-- Submitted from TestTWF Paris -->
3 <meta charset="utf-8">
4 <title>Keypath</title>
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>
11 <script>
13 var global_db = createdb_for_multiple_tests();
15 function keypath(keypath, objects, expected_keys, desc) {
16 var db,
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) {
23 db = e.target.result;
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) {
31 var actual_keys = [],
32 rq = db.transaction(store_name)
33 .objectStore(store_name)
34 .openCursor();
36 rq.onsuccess = t.step_func(function(e) {
37 var cursor = e.target.result;
39 if (cursor) {
40 actual_keys.push(cursor.key.valueOf());
41 cursor.continue();
43 else {
44 assert_key_equals(actual_keys, expected_keys, "keyorder array");
45 t.done();
47 });
51 keypath('my.key',
52 [ { my: { key: 10 } } ],
53 [ 10 ]);
55 keypath('my.køi',
56 [ { my: { køi: 5 } } ],
57 [ 5 ]);
59 keypath('my.key_ya',
60 [ { my: { key_ya: 10 } } ],
61 [ 10 ]);
63 keypath('public.key$ya',
64 [ { public: { key$ya: 10 } } ],
65 [ 10 ]);
67 keypath('true.$',
68 [ { true: { $: 10 } } ],
69 [ 10 ]);
71 keypath('my._',
72 [ { my: { _: 10 } } ],
73 [ 10 ]);
75 keypath('delete.a7',
76 [ { delete: { a7: 10 } } ],
77 [ 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}}}}}}}}}}}}}} ],
81 [ 10 ]);
83 keypath('str.length',
84 [ { str: "pony" }, { str: "my" }, { str: "little" }, { str: "" } ],
85 [ 0, 2, 4, 6 ]);
87 keypath('arr.length',
88 [ {arr: [0, 0, 0, 0]}, {arr: [{}, 0, "hei", "length", Infinity, []]}, {arr: [10, 10]}, { arr: []} ],
89 [ 0, 2, 4, 6 ]);
91 keypath('length',
92 [ [10, 10], "123", { length: 20 } ],
93 [ 2, 3, 20 ]);
95 keypath('',
96 [ ["bags"], "bean", 10 ],
97 [ 10, "bean", ["bags"] ],
98 "'' uses value as key");
100 keypath([''],
101 [ ["bags"], "bean", 10 ],
102 [ [10], ["bean"] , [["bags"]] ],
103 "[''] uses value as [key]");
105 keypath(['x', 'y'],
106 [ {x:10, y:20}, {y:1.337, x:100} ],
107 [ [10, 20], [100, 1.337] ],
108 "['x', 'y']");
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)");
120 if (false) {
121 var myblob = Blob(["Yoda"], {type:'suprawsum'});
122 keypath(['length', 'type'],
123 [ myblob ],
124 [ 4, 'suprawsum' ],
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" ] ]);
138 loop_array = [];
139 loop_array.push(loop_array);
140 keypath(loop_array,
141 [ "a", 1, ["k"] ],
142 [ [1], ["a"], [["k"]] ],
143 "array loop -> stringify becomes ['']");
144 </script>
146 <div id=log></div>