proxytest6.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. // JavaScript source code
  6. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  7. var handler = {
  8. getOwnPropertyDescriptor: function (target, name) {
  9. return emulatedProps[name];
  10. },
  11. defineProperty: function (target, name, desc) {
  12. emulatedProps[name] = desc;
  13. // $LOG('success: ' + success);
  14. return success[name];
  15. },
  16. preventExtensions: function (target) {
  17. Object.defineProperties(target, emulatedProps);
  18. Object.preventExtensions(target);
  19. return true;
  20. },
  21. deleteProperty: function (target, name) {
  22. delete emulatedProps[name];
  23. return success[name];
  24. },
  25. get: function (target, name, receiver) {
  26. var desc = emulatedProps[name];
  27. if (desc === undefined) { return emulatedProto[name]; }
  28. if ('value' in desc) return desc.value;
  29. if ('get' in desc) return desc.get.call(target);
  30. },
  31. set: function (target, name, value, receiver) {
  32. return success[name];
  33. },
  34. has: function (target, name) {
  35. return !!emulatedProps[name];
  36. },
  37. hasOwn: function (target, name) {
  38. return !!emulatedProps[name];
  39. },
  40. ownKeys: function (target) {
  41. return Object.getOwnPropertyNames(emulatedProps);
  42. },
  43. enumerate: function (target) {
  44. return Object.getOwnPropertyNames(emulatedProps).filter(function (name) {
  45. return emulatedProps[name].enumerable;
  46. });
  47. }
  48. };
  49. var tests = [
  50. {
  51. name: "non-configurable property can't be reported as non-existent",
  52. body: function () {
  53. var target = {};
  54. emulatedProps = {};
  55. emulatedProps.x = { value: 'test', configurable: false };
  56. Object.defineProperty(target, 'x', emulatedProps.x);
  57. var proxy = new Proxy(target, handler); // Checkout handler code at the bottom
  58. var desc;
  59. delete emulatedProps.x;
  60. assert.throws(function () { desc = Object.getOwnPropertyDescriptor(proxy, 'x'); }, TypeError, "Cannot return non-existent for a non-configurable property");
  61. },
  62. },
  63. {
  64. name: "existing property on non-extensible object cannot be reported as non-existent",
  65. body: function(){
  66. var target = {};
  67. emulatedProps = {};
  68. target.x = 20;
  69. Object.preventExtensions(target);
  70. var desc;
  71. var proxy = new Proxy(target, handler); // Checkout handler code at the bottom
  72. assert.throws(function () { desc = Object.getOwnPropertyDescriptor(proxy, 'x'); }, TypeError);
  73. }
  74. },
  75. {
  76. name: "non existing property on non-extensible object cannot be reported as existent",
  77. body: function () {
  78. var target = {};
  79. emulatedProps = {};
  80. emulatedProps.x = { value: 1, configurable: false };
  81. Object.preventExtensions(target);
  82. var proxy = new Proxy(target, handler);
  83. assert.throws(function () { Object.getOwnPropertyDescriptor(proxy, 'x') }, TypeError);
  84. }
  85. },
  86. {
  87. name:'configurable property cannot be reported as non-configurable',
  88. body: function () {
  89. var target = {};
  90. emulatedProps = {};
  91. emulatedProps.x = { value: 1, configurable: false };
  92. Object.defineProperty(target, 'x', { value: 1, configurable: true });
  93. var proxy = new Proxy(target, handler);
  94. assert.throws(function () { desc = Object.getOwnPropertyDescriptor(proxy, 'x') }, TypeError, "configurable property cannot be reported as non-configurable");
  95. }
  96. },
  97. {
  98. name: "a non-existent property cannot be reported as non-configurable",
  99. body: function() {
  100. emulatedProps = {};
  101. var target = {};
  102. Object.defineProperty(emulatedProps, 'y', { value: 'test', configurable: 'false' });
  103. var proxy = new Proxy(target, handler);
  104. assert.throws(function () { var desc = Object.getOwnPropertyDescriptor(proxy, 'y') }, TypeError, "a non-existent property cannot be reported as non-configurable");
  105. }
  106. },
  107. {
  108. name: "can't add property on non-extensible object.",
  109. body: function () {
  110. var target = {};
  111. Object.preventExtensions(target);
  112. emulatedProps = {};
  113. emulatedProps.x = 5;
  114. var proxy = new Proxy(target, emulatedProps);
  115. success = function () { };
  116. assert.throws(function () { Object.defineProperty(proxy, 'x', { value: 20 }) }, TypeError);
  117. },
  118. },
  119. {
  120. name: "target property must be non-configurable after set as such in proxy #1",
  121. body: function () {
  122. var target = {};
  123. emulatedProps = {};
  124. var proxy = new Proxy(target, handler);
  125. success = function () { };
  126. success.x = 21;
  127. assert.throws(function () { Object.defineProperty(proxy, 'x', { value: 20, configurable: false }) }, TypeError);
  128. },
  129. },
  130. {
  131. name: "target property must be non-configurable after set as such in proxy #2",
  132. body: function () {
  133. var target = {};
  134. emulatedProps = {};
  135. emulatedProps.x = 5;
  136. var proxy = new Proxy(target, handler);
  137. Object.defineProperty(target, 'x', { value: 41, configurable: true });
  138. success = function () { };
  139. success.x = 21;
  140. assert.throws(function () { Object.defineProperty(proxy, 'x', { value: 20, configurable: false }) }, TypeError);
  141. },
  142. },
  143. ];
  144. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });