test_js_helpers.js (3958B)
1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ 2 /* vim: set sw=2 ts=2 sts=2 et : */ 3 /** 4 * Any copyright is dedicated to the Public Domain. 5 * http://creativecommons.org/publicdomain/zero/1.0/ 6 */ 7 8 /** 9 * This file tests that the JS language helpers in various ways. 10 */ 11 12 // Test Functions 13 14 function test_params_enumerate() { 15 let stmt = createStatement("SELECT * FROM test WHERE id IN (:a, :b, :c)"); 16 17 // Make sure they are right. 18 let expected = [0, 1, 2, "a", "b", "c", "length"]; 19 let index = 0; 20 for (let name in stmt.params) { 21 if (name == "QueryInterface") { 22 continue; 23 } 24 Assert.equal(name, expected[index++]); 25 } 26 Assert.equal(index, 7); 27 } 28 29 function test_params_prototype() { 30 let stmt = createStatement("SELECT * FROM sqlite_master"); 31 32 // Set a property on the prototype and make sure it exist (will not be a 33 // bindable parameter, however). 34 Object.getPrototypeOf(stmt.params).test = 2; 35 Assert.equal(stmt.params.test, 2); 36 37 delete Object.getPrototypeOf(stmt.params).test; 38 stmt.finalize(); 39 } 40 41 function test_row_prototype() { 42 let stmt = createStatement("SELECT * FROM sqlite_master"); 43 44 Assert.ok(stmt.executeStep()); 45 46 // Set a property on the prototype and make sure it exists (will not be in the 47 // results, however). 48 Object.getPrototypeOf(stmt.row).test = 2; 49 Assert.equal(stmt.row.test, 2); 50 51 // Clean up after ourselves. 52 delete Object.getPrototypeOf(stmt.row).test; 53 stmt.finalize(); 54 } 55 56 function test_row_enumerate() { 57 let stmt = createStatement("SELECT * FROM test"); 58 59 Assert.ok(stmt.executeStep()); 60 61 let expected = ["id", "string"]; 62 let expected_values = [123, "foo"]; 63 let index = 0; 64 for (let name in stmt.row) { 65 Assert.equal(name, expected[index]); 66 Assert.equal(stmt.row[name], expected_values[index]); 67 index++; 68 } 69 Assert.equal(index, 2); 70 71 // Save off the row helper, then forget the statement and trigger a GC. We 72 // want to ensure that if the row helper is retained but the statement is 73 // destroyed, that no crash occurs and that the late access attempt simply 74 // throws an error. 75 let savedOffRow = stmt.row; 76 stmt = null; 77 Cu.forceGC(); 78 Assert.throws( 79 () => { 80 return savedOffRow.string; 81 }, 82 /NS_ERROR_NOT_INITIALIZED/, 83 "GC'ed statement should throw" 84 ); 85 } 86 87 function test_params_gets_sync() { 88 // Added for bug 562866. 89 /* 90 let stmt = createStatement( 91 "SELECT * FROM test WHERE id IN (:a, :b, :c)" 92 ); 93 94 // Make sure we do not assert in getting the value. 95 let originalCount = Object.getOwnPropertyNames(stmt.params).length; 96 let expected = ["a", "b", "c"]; 97 for (let name of expected) { 98 stmt.params[name]; 99 } 100 101 // Now make sure we didn't magically get any additional properties. 102 let finalCount = Object.getOwnPropertyNames(stmt.params).length; 103 do_check_eq(originalCount + expected.length, finalCount); 104 */ 105 } 106 107 function test_params_gets_async() { 108 // Added for bug 562866. 109 /* 110 let stmt = createAsyncStatement( 111 "SELECT * FROM test WHERE id IN (:a, :b, :c)" 112 ); 113 114 // Make sure we do not assert in getting the value. 115 let originalCount = Object.getOwnPropertyNames(stmt.params).length; 116 let expected = ["a", "b", "c"]; 117 for (let name of expected) { 118 stmt.params[name]; 119 } 120 121 // Now make sure we didn't magically get any additional properties. 122 let finalCount = Object.getOwnPropertyNames(stmt.params).length; 123 do_check_eq(originalCount + expected.length, finalCount); 124 */ 125 } 126 127 // Test Runner 128 129 var tests = [ 130 test_params_enumerate, 131 test_params_prototype, 132 test_row_enumerate, 133 test_row_prototype, 134 test_params_gets_sync, 135 test_params_gets_async, 136 ]; 137 function run_test() { 138 cleanup(); 139 140 // Create our database. 141 getOpenedDatabase().executeSimpleSQL( 142 "CREATE TABLE test (id INTEGER PRIMARY KEY, string TEXT)" 143 ); 144 getOpenedDatabase().executeSimpleSQL( 145 "INSERT INTO test (id, string) VALUES (123, 'foo')" 146 ); 147 148 // Run the tests. 149 tests.forEach(test => test()); 150 }