| Philip Jägenstedt | fb85c5c | 2014-01-19 17:50:31 | [diff] [blame] | 1 | <!DOCTYPE html> |
| Rebecca Hauck | 4c85775 | 2012-12-08 00:00:26 | [diff] [blame] | 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]."> |
| Rebecca Hauck | 4c85775 | 2012-12-08 00:00:26 | [diff] [blame] | 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 { |
| Joshua Bell | ab1556f | 2015-07-29 00:02:53 | [diff] [blame] | 44 | assert_key_equals(actual_keys, expected_keys, "keyorder array"); |
| Rebecca Hauck | 4c85775 | 2012-12-08 00:00:26 | [diff] [blame] | 45 | t.done(); |
| 46 | } |
| 47 | }); |
| 48 | }; |
| 49 | } |
| 50 | |
| 51 | keypath('my.key', |
| 52 | [ { my: { key: 10 } } ], |
| 53 | [ 10 ]); |
| 54 | |
| 55 | keypath('my.køi', |
| 56 | [ { my: { køi: 5 } } ], |
| 57 | [ 5 ]); |
| 58 | |
| 59 | keypath('my.key_ya', |
| 60 | [ { my: { key_ya: 10 } } ], |
| 61 | [ 10 ]); |
| 62 | |
| 63 | keypath('public.key$ya', |
| 64 | [ { public: { key$ya: 10 } } ], |
| 65 | [ 10 ]); |
| 66 | |
| 67 | keypath('true.$', |
| 68 | [ { true: { $: 10 } } ], |
| 69 | [ 10 ]); |
| 70 | |
| 71 | keypath('my._', |
| 72 | [ { my: { _: 10 } } ], |
| 73 | [ 10 ]); |
| 74 | |
| 75 | keypath('delete.a7', |
| 76 | [ { delete: { a7: 10 } } ], |
| 77 | [ 10 ]); |
| 78 | |
| 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 ]); |
| 82 | |
| 83 | keypath('str.length', |
| 84 | [ { str: "pony" }, { str: "my" }, { str: "little" }, { str: "" } ], |
| 85 | [ 0, 2, 4, 6 ]); |
| 86 | |
| 87 | keypath('arr.length', |
| 88 | [ {arr: [0, 0, 0, 0]}, {arr: [{}, 0, "hei", "length", Infinity, []]}, {arr: [10, 10]}, { arr: []} ], |
| 89 | [ 0, 2, 4, 6 ]); |
| 90 | |
| 91 | keypath('length', |
| 92 | [ [10, 10], "123", { length: 20 } ], |
| 93 | [ 2, 3, 20 ]); |
| 94 | |
| 95 | keypath('', |
| 96 | [ ["bags"], "bean", 10 ], |
| 97 | [ 10, "bean", ["bags"] ], |
| 98 | "'' uses value as key"); |
| 99 | |
| 100 | keypath([''], |
| 101 | [ ["bags"], "bean", 10 ], |
| 102 | [ [10], ["bean"] , [["bags"]] ], |
| 103 | "[''] uses value as [key]"); |
| 104 | |
| 105 | keypath(['x', 'y'], |
| 106 | [ {x:10, y:20}, {y:1.337, x:100} ], |
| 107 | [ [10, 20], [100, 1.337] ], |
| 108 | "['x', 'y']"); |
| 109 | |
| 110 | keypath([['x'], ['y']], |
| 111 | [ {x:10, y:20}, {y:1.337, x:100} ], |
| 112 | [ [10, 20], [100, 1.337] ], |
| 113 | "[['x'], 'y'] (stringifies)"); |
| 114 | |
| 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)"); |
| 119 | |
| 120 | if (false) { |
| 121 | var myblob = Blob(["Yoda"], {type:'suprawsum'}); |
| 122 | keypath(['length', 'type'], |
| 123 | [ myblob ], |
| 124 | [ 4, 'suprawsum' ], |
| 125 | "[Blob.length, Blob.type]"); |
| 126 | } |
| 127 | |
| Andrea Marchesini | f86aa23 | 2018-05-04 12:44:17 | [diff] [blame] | 128 | // File.name and File.lastModified is not testable automatically |
| Rebecca Hauck | 4c85775 | 2012-12-08 00:00:26 | [diff] [blame] | 129 | |
| 130 | keypath(['name', 'type'], |
| 131 | [ { name: "orange", type: "fruit" }, { name: "orange", type: ["telecom", "french"] } ], |
| 132 | [ ["orange", "fruit"], ["orange", ["telecom", "french"]] ]); |
| 133 | |
| 134 | keypath(['name', 'type.name'], |
| 135 | [ { name: "orange", type: { name: "fruit" }}, { name: "orange", type: { name: "telecom" }} ], |
| 136 | [ ["orange", "fruit"], ["orange", "telecom" ] ]); |
| 137 | |
| 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> |
| 145 | |
| 146 | <div id=log></div> |