tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

DOMException-custom-bindings.any.js (5028B)


      1 // META: global=window,dedicatedworker,shadowrealm
      2 
      3 "use strict";
      4 
      5 test(() => {
      6  assert_throws_js(TypeError, () => DOMException());
      7 }, "Cannot construct without new");
      8 
      9 test(() => {
     10  assert_equals(Object.getPrototypeOf(DOMException.prototype), Error.prototype);
     11 }, "inherits from Error: prototype-side");
     12 
     13 test(() => {
     14  assert_equals(Object.getPrototypeOf(DOMException), Function.prototype);
     15 }, "does not inherit from Error: class-side");
     16 
     17 test(() => {
     18  const e = new DOMException("message", "name");
     19  assert_false(e.hasOwnProperty("message"), "property is not own");
     20 
     21  const propDesc = Object.getOwnPropertyDescriptor(DOMException.prototype, "message");
     22  assert_equals(typeof propDesc.get, "function", "property descriptor is a getter");
     23  assert_equals(propDesc.set, undefined, "property descriptor is not a setter");
     24  assert_true(propDesc.enumerable, "property descriptor enumerable");
     25  assert_true(propDesc.configurable, "property descriptor configurable");
     26 }, "message property descriptor");
     27 
     28 test(() => {
     29  const getter = Object.getOwnPropertyDescriptor(DOMException.prototype, "message").get;
     30 
     31  assert_throws_js(TypeError, () => getter.apply({}));
     32 }, "message getter performs brand checks (i.e. is not [LegacyLenientThis])");
     33 
     34 test(() => {
     35  const e = new DOMException("message", "name");
     36  assert_false(e.hasOwnProperty("name"), "property is not own");
     37 
     38  const propDesc = Object.getOwnPropertyDescriptor(DOMException.prototype, "name");
     39  assert_equals(typeof propDesc.get, "function", "property descriptor is a getter");
     40  assert_equals(propDesc.set, undefined, "property descriptor is not a setter");
     41  assert_true(propDesc.enumerable, "property descriptor enumerable");
     42  assert_true(propDesc.configurable, "property descriptor configurable");
     43 }, "name property descriptor");
     44 
     45 test(() => {
     46  const getter = Object.getOwnPropertyDescriptor(DOMException.prototype, "name").get;
     47 
     48  assert_throws_js(TypeError, () => getter.apply({}));
     49 }, "name getter performs brand checks (i.e. is not [LegacyLenientThis])");
     50 
     51 test(() => {
     52  const e = new DOMException("message", "name");
     53  assert_false(e.hasOwnProperty("code"), "property is not own");
     54 
     55  const propDesc = Object.getOwnPropertyDescriptor(DOMException.prototype, "code");
     56  assert_equals(typeof propDesc.get, "function", "property descriptor is a getter");
     57  assert_equals(propDesc.set, undefined, "property descriptor is not a setter");
     58  assert_true(propDesc.enumerable, "property descriptor enumerable");
     59  assert_true(propDesc.configurable, "property descriptor configurable");
     60 }, "code property descriptor");
     61 
     62 test(() => {
     63  const getter = Object.getOwnPropertyDescriptor(DOMException.prototype, "code").get;
     64 
     65  assert_throws_js(TypeError, () => getter.apply({}));
     66 }, "code getter performs brand checks (i.e. is not [LegacyLenientThis])");
     67 
     68 test(() => {
     69  const e = new DOMException("message", "InvalidCharacterError");
     70  assert_equals(e.code, 5, "Initially the code is set to 5");
     71 
     72  Object.defineProperty(e, "name", {
     73    value: "WrongDocumentError"
     74  });
     75 
     76  assert_equals(e.code, 5, "The code is still set to 5");
     77 }, "code property is not affected by shadowing the name property");
     78 
     79 test(() => {
     80  const e = new DOMException("message", "name");
     81  assert_equals(Object.prototype.toString.call(e), "[object DOMException]");
     82 }, "Object.prototype.toString behavior is like other interfaces");
     83 
     84 test(() => {
     85  const e = new DOMException("message", "name");
     86  assert_false(e.hasOwnProperty("toString"), "toString must not exist on the instance");
     87  assert_false(DOMException.prototype.hasOwnProperty("toString"), "toString must not exist on DOMException.prototype");
     88  assert_equals(typeof e.toString, "function", "toString must still exist (via Error.prototype)");
     89 }, "Inherits its toString() from Error.prototype");
     90 
     91 test(() => {
     92  const e = new DOMException("message", "name");
     93  assert_equals(e.toString(), "name: message",
     94    "The default Error.prototype.toString() behavior must work on supplied name and message");
     95 
     96  Object.defineProperty(e, "name", { value: "new name" });
     97  Object.defineProperty(e, "message", { value: "new message" });
     98  assert_equals(e.toString(), "new name: new message",
     99    "The default Error.prototype.toString() behavior must work on shadowed names and messages");
    100 }, "toString() behavior from Error.prototype applies as expected");
    101 
    102 test(() => {
    103  assert_throws_js(TypeError, () => DOMException.prototype.toString());
    104 }, "DOMException.prototype.toString() applied to DOMException.prototype throws because of name/message brand checks");
    105 
    106 test(() => {
    107  let stackOnNormalErrors;
    108  try {
    109    throw new Error("normal error");
    110  } catch (e) {
    111    stackOnNormalErrors = e.stack;
    112  }
    113 
    114  let stackOnDOMException;
    115  try {
    116    throw new DOMException("message", "name");
    117  } catch (e) {
    118    stackOnDOMException = e.stack;
    119  }
    120 
    121  assert_equals(typeof stackOnDOMException, typeof stackOnNormalErrors, "The typeof values must match");
    122 }, "If the implementation has a stack property on normal errors, it also does on DOMExceptions");