OLD | NEW |
(Empty) | |
| 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 iden
tifier, 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> |
| 10 |
| 11 <script> |
| 12 |
| 13 var global_db = createdb_for_multiple_tests(); |
| 14 |
| 15 function keypath(keypath, objects, expected_keys, desc) { |
| 16 var db, |
| 17 t = async_test(document.title + " - " + (desc ? desc : keypath)), |
| 18 |
| 19 store_name = "store-"+(Date.now())+Math.random(); |
| 20 |
| 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 }
); |
| 25 |
| 26 for (var i = 0; i < objects.length; i++) |
| 27 objStore.add(objects[i]); |
| 28 }; |
| 29 |
| 30 open_rq.onsuccess = function(e) { |
| 31 var actual_keys = [], |
| 32 rq = db.transaction(store_name) |
| 33 .objectStore(store_name) |
| 34 .openCursor(); |
| 35 |
| 36 rq.onsuccess = t.step_func(function(e) { |
| 37 var cursor = e.target.result; |
| 38 |
| 39 if (cursor) { |
| 40 actual_keys.push(cursor.key.valueOf()); |
| 41 cursor.continue(); |
| 42 } |
| 43 else { |
| 44 assert_equals(actual_keys.length, expected_keys.length, "arr
ay length"); |
| 45 assert_object_equals(actual_keys, expected_keys, "keyorder a
rray"); |
| 46 |
| 47 t.done(); |
| 48 } |
| 49 }); |
| 50 }; |
| 51 } |
| 52 |
| 53 keypath('my.key', |
| 54 [ { my: { key: 10 } } ], |
| 55 [ 10 ]); |
| 56 |
| 57 keypath('my.køi', |
| 58 [ { my: { køi: 5 } } ], |
| 59 [ 5 ]); |
| 60 |
| 61 keypath('my.key_ya', |
| 62 [ { my: { key_ya: 10 } } ], |
| 63 [ 10 ]); |
| 64 |
| 65 keypath('public.key$ya', |
| 66 [ { public: { key$ya: 10 } } ], |
| 67 [ 10 ]); |
| 68 |
| 69 keypath('true.$', |
| 70 [ { true: { $: 10 } } ], |
| 71 [ 10 ]); |
| 72 |
| 73 keypath('my._', |
| 74 [ { my: { _: 10 } } ], |
| 75 [ 10 ]); |
| 76 |
| 77 keypath('delete.a7', |
| 78 [ { delete: { a7: 10 } } ], |
| 79 [ 10 ]); |
| 80 |
| 81 keypath('p.p.p.p.p.p.p.p.p.p.p.p.p.p', |
| 82 [ {p:{p:{p:{p:{p:{p:{p:{p:{p:{p:{p:{p:{p:{p:10}}}}}}}}}}}}}} ], |
| 83 [ 10 ]); |
| 84 |
| 85 keypath('str.length', |
| 86 [ { str: "pony" }, { str: "my" }, { str: "little" }, { str: "" } ], |
| 87 [ 0, 2, 4, 6 ]); |
| 88 |
| 89 keypath('arr.length', |
| 90 [ {arr: [0, 0, 0, 0]}, {arr: [{}, 0, "hei", "length", Infinity, []]}, {a
rr: [10, 10]}, { arr: []} ], |
| 91 [ 0, 2, 4, 6 ]); |
| 92 |
| 93 keypath('length', |
| 94 [ [10, 10], "123", { length: 20 } ], |
| 95 [ 2, 3, 20 ]); |
| 96 |
| 97 keypath('', |
| 98 [ ["bags"], "bean", 10 ], |
| 99 [ 10, "bean", ["bags"] ], |
| 100 "'' uses value as key"); |
| 101 |
| 102 keypath([''], |
| 103 [ ["bags"], "bean", 10 ], |
| 104 [ [10], ["bean"] , [["bags"]] ], |
| 105 "[''] uses value as [key]"); |
| 106 |
| 107 keypath(['x', 'y'], |
| 108 [ {x:10, y:20}, {y:1.337, x:100} ], |
| 109 [ [10, 20], [100, 1.337] ], |
| 110 "['x', 'y']"); |
| 111 |
| 112 keypath([['x'], ['y']], |
| 113 [ {x:10, y:20}, {y:1.337, x:100} ], |
| 114 [ [10, 20], [100, 1.337] ], |
| 115 "[['x'], 'y'] (stringifies)"); |
| 116 |
| 117 keypath(['x', {toString:function(){return 'y'}}], |
| 118 [ {x:10, y:20}, {y:1.337, x:100} ], |
| 119 [ [10, 20], [100, 1.337] ], |
| 120 "['x', {toString->'y'}] (stringifies)"); |
| 121 |
| 122 if (false) { |
| 123 var myblob = Blob(["Yoda"], {type:'suprawsum'}); |
| 124 keypath(['length', 'type'], |
| 125 [ myblob ], |
| 126 [ 4, 'suprawsum' ], |
| 127 "[Blob.length, Blob.type]"); |
| 128 } |
| 129 |
| 130 // File.name and File.lastModifiedDate is not testable automatically |
| 131 |
| 132 keypath(['name', 'type'], |
| 133 [ { name: "orange", type: "fruit" }, { name: "orange", type: ["telecom",
"french"] } ], |
| 134 [ ["orange", "fruit"], ["orange", ["telecom", "french"]] ]); |
| 135 |
| 136 keypath(['name', 'type.name'], |
| 137 [ { name: "orange", type: { name: "fruit" }}, { name: "orange", type: {
name: "telecom" }} ], |
| 138 [ ["orange", "fruit"], ["orange", "telecom" ] ]); |
| 139 |
| 140 loop_array = []; |
| 141 loop_array.push(loop_array); |
| 142 keypath(loop_array, |
| 143 [ "a", 1, ["k"] ], |
| 144 [ [1], ["a"], [["k"]] ], |
| 145 "array loop -> stringify becomes ['']"); |
| 146 </script> |
| 147 |
| 148 <div id=log></div> |
OLD | NEW |