test_Assert.js (6064B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 /* eslint-disable no-array-constructor, no-object-constructor */ 7 8 const { assert } = ChromeUtils.importESModule( 9 "chrome://remote/content/shared/webdriver/Assert.sys.mjs" 10 ); 11 const { error } = ChromeUtils.importESModule( 12 "chrome://remote/content/shared/webdriver/Errors.sys.mjs" 13 ); 14 15 add_task(function test_session() { 16 assert.session({ id: "foo" }); 17 18 const invalidTypes = [ 19 null, 20 undefined, 21 [], 22 {}, 23 { id: undefined }, 24 { id: null }, 25 { id: true }, 26 { id: 1 }, 27 { id: [] }, 28 { id: {} }, 29 ]; 30 31 for (const invalidType of invalidTypes) { 32 Assert.throws(() => assert.session(invalidType), /InvalidSessionIDError/); 33 } 34 35 Assert.throws(() => assert.session({ id: null }, "custom"), /custom/); 36 }); 37 38 add_task(function test_platforms() { 39 // at least one will fail 40 let raised; 41 for (let fn of [assert.desktop, assert.mobile]) { 42 try { 43 fn(); 44 } catch (e) { 45 raised = e; 46 } 47 } 48 ok(raised instanceof error.UnsupportedOperationError); 49 }); 50 51 add_task(function test_noUserPrompt() { 52 assert.noUserPrompt(null); 53 assert.noUserPrompt(undefined); 54 Assert.throws(() => assert.noUserPrompt({}), /UnexpectedAlertOpenError/); 55 Assert.throws(() => assert.noUserPrompt({}, "custom"), /custom/); 56 }); 57 58 add_task(function test_defined() { 59 assert.defined({}); 60 Assert.throws(() => assert.defined(undefined), /InvalidArgumentError/); 61 Assert.throws(() => assert.noUserPrompt({}, "custom"), /custom/); 62 }); 63 64 add_task(function test_number() { 65 assert.number(1); 66 assert.number(0); 67 assert.number(-1); 68 assert.number(1.2); 69 for (let i of ["foo", "1", {}, [], NaN, Infinity, undefined]) { 70 Assert.throws(() => assert.number(i), /InvalidArgumentError/); 71 } 72 73 Assert.throws(() => assert.number("foo", "custom"), /custom/); 74 }); 75 76 add_task(function test_callable() { 77 assert.callable(function () {}); 78 assert.callable(() => {}); 79 80 for (let typ of [undefined, "", true, {}, []]) { 81 Assert.throws(() => assert.callable(typ), /InvalidArgumentError/); 82 } 83 84 Assert.throws(() => assert.callable("foo", "custom"), /custom/); 85 }); 86 87 add_task(function test_integer() { 88 assert.integer(1); 89 assert.integer(0); 90 assert.integer(-1); 91 Assert.throws(() => assert.integer("foo"), /InvalidArgumentError/); 92 Assert.throws(() => assert.integer(1.2), /InvalidArgumentError/); 93 94 Assert.throws(() => assert.integer("foo", "custom"), /custom/); 95 }); 96 97 add_task(function test_positiveInteger() { 98 assert.positiveInteger(1); 99 assert.positiveInteger(0); 100 Assert.throws(() => assert.positiveInteger(-1), /InvalidArgumentError/); 101 Assert.throws(() => assert.positiveInteger("foo"), /InvalidArgumentError/); 102 Assert.throws(() => assert.positiveInteger("foo", "custom"), /custom/); 103 }); 104 105 add_task(function test_positiveNumber() { 106 assert.positiveNumber(1); 107 assert.positiveNumber(0); 108 assert.positiveNumber(1.1); 109 assert.positiveNumber(Number.MAX_VALUE); 110 // eslint-disable-next-line no-loss-of-precision 111 Assert.throws(() => assert.positiveNumber(1.8e308), /InvalidArgumentError/); 112 Assert.throws(() => assert.positiveNumber(-1), /InvalidArgumentError/); 113 Assert.throws(() => assert.positiveNumber(Infinity), /InvalidArgumentError/); 114 Assert.throws(() => assert.positiveNumber("foo"), /InvalidArgumentError/); 115 Assert.throws(() => assert.positiveNumber("foo", "custom"), /custom/); 116 }); 117 118 add_task(function test_boolean() { 119 assert.boolean(true); 120 assert.boolean(false); 121 Assert.throws(() => assert.boolean("false"), /InvalidArgumentError/); 122 Assert.throws(() => assert.boolean(undefined), /InvalidArgumentError/); 123 Assert.throws(() => assert.boolean(undefined, "custom"), /custom/); 124 }); 125 126 add_task(function test_string() { 127 assert.string("foo"); 128 assert.string(`bar`); 129 Assert.throws(() => assert.string(42), /InvalidArgumentError/); 130 Assert.throws(() => assert.string(42, "custom"), /custom/); 131 }); 132 133 add_task(function test_open() { 134 assert.open({ currentWindowGlobal: {} }); 135 136 for (let typ of [null, undefined, { currentWindowGlobal: null }]) { 137 Assert.throws(() => assert.open(typ), /NoSuchWindowError/); 138 } 139 140 Assert.throws(() => assert.open(null, "custom"), /custom/); 141 }); 142 143 add_task(function test_object() { 144 assert.object({}); 145 assert.object(new Object()); 146 for (let typ of [42, "foo", true, null, undefined]) { 147 Assert.throws(() => assert.object(typ), /InvalidArgumentError/); 148 } 149 150 Assert.throws(() => assert.object(null, "custom"), /custom/); 151 }); 152 153 add_task(function test_isInstance() { 154 class Foo { 155 static isInstance(obj) { 156 return obj instanceof Foo; 157 } 158 } 159 assert.isInstance(new Foo(), Foo); 160 for (let typ of [{}, 42, "foo", true, null, undefined]) { 161 Assert.throws(() => assert.isInstance(typ, Foo), /InvalidArgumentError/); 162 } 163 164 Assert.throws(() => assert.isInstance(null, null, "custom"), /custom/); 165 }); 166 167 add_task(function test_in() { 168 assert.in("foo", { foo: 42 }); 169 for (let typ of [{}, 42, true, null, undefined]) { 170 Assert.throws(() => assert.in("foo", typ), /InvalidArgumentError/); 171 } 172 173 Assert.throws(() => assert.in("foo", { bar: 42 }, "custom"), /custom/); 174 }); 175 176 add_task(function test_array() { 177 assert.array([]); 178 assert.array(new Array()); 179 Assert.throws(() => assert.array(42), /InvalidArgumentError/); 180 Assert.throws(() => assert.array({}), /InvalidArgumentError/); 181 182 Assert.throws(() => assert.array(42, "custom"), /custom/); 183 }); 184 185 add_task(function test_that() { 186 equal(1, assert.that(n => n + 1)(1)); 187 Assert.throws(() => assert.that(() => false)(), /InvalidArgumentError/); 188 Assert.throws(() => assert.that(val => val)(false), /InvalidArgumentError/); 189 Assert.throws( 190 () => assert.that(val => val, "foo", error.SessionNotCreatedError)(false), 191 /SessionNotCreatedError/ 192 ); 193 194 Assert.throws(() => assert.that(() => false, "custom")(), /custom/); 195 }); 196 197 /* eslint-enable no-array-constructor, no-new-object */