proxies-constructor.js 2.12 KB
Newer Older
1 2 3 4
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6 7 8
(function testNewTarget() {
  assertThrows(function(){ Proxy({}, {}); }, TypeError);
  assertDoesNotThrow(function(){ new Proxy({}, {}); });
})();
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

(function testNonObjectTargetTypes() {
  assertThrows(function(){ new Proxy(undefined, {}); }, TypeError);

  assertThrows(function(){ new Proxy(null, {}); }, TypeError);

  assertThrows(function(){ new Proxy('', {}); }, TypeError);

  assertThrows(function(){ new Proxy(0, {}); }, TypeError);

  assertThrows(function(){ new Proxy(0.5, {}); }, TypeError);

  assertThrows(function(){ new Proxy(false, {}); }, TypeError);
})();


(function testNonObjectHandlerTypes() {
  assertThrows(function(){ new Proxy({}, undefined); }, TypeError);

  assertThrows(function(){ new Proxy({}, null); }, TypeError);

  assertThrows(function(){ new Proxy({}, ''); }, TypeError);

  assertThrows(function(){ new Proxy({}, 0); }, TypeError);

  assertThrows(function(){ new Proxy({}, 0.5); }, TypeError);

  assertThrows(function(){ new Proxy({}, false); }, TypeError);
})();


40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
(function testConstructionWithoutArguments() {
  assertThrows(function(){ new Proxy(); }, TypeError);

  assertThrows(function(){ new Proxy(42); }, TypeError);

  assertThrows(function(){ new Proxy({}); }, TypeError);
})();


(function testConstructionFromArray() {
  var p = new Proxy([42], {});
  assertTrue(p instanceof Array);
  assertEquals(p[0], 42);
})();


(function testConstructionFromObject() {
  var p = new Proxy({
    prop: 42
  }, {});
  assertTrue(p instanceof Object);
  assertEquals(p.prop, 42);
})();


(function testConstructionFromCallable() {
  var p = new Proxy(() => { return 42; }, {});
  assertTrue(p instanceof Function);
  assertEquals(p(), 42);
})();


(function testConstructionFromConstructor() {
  class foo {};
  var p = new Proxy(foo, {});
  assertTrue(p instanceof Function);
  assertTrue(new p() instanceof foo);
})();


(function testConstructionFromProxy() {
  var q = new Proxy({}, {});
  var p = new Proxy(q, {});
  assertTrue(p instanceof Object);
})();