object-assign.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. // ES6 Object.is(x,y) API extension tests -- verifies the API shape and basic functionality
  6. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  7. var tests = [
  8. {
  9. name: "Object.assign should exist and have length 2",
  10. body: function () {
  11. assert.isTrue(Object.hasOwnProperty('assign'), "Object should have an assign method");
  12. assert.areEqual(2, Object.assign.length, "assign method takes two arguments");
  13. }
  14. },
  15. {
  16. name: "Object.assign(o1, ...) should call ToObject on its sources",
  17. body: function () {
  18. assert.areEqual({}, Object.assign(1), "Call ToObject on target");
  19. assert.areEqual({}, Object.assign(1, 2, 3), "Call ToObject on target and sources");
  20. assert.areEqual({}, Object.assign({}, 2, 3), "Call ToObject on sources");
  21. }
  22. },
  23. {
  24. name: "Object.assign(object, null) and Object.assign(object, undefined) are no-op",
  25. body: function () {
  26. assert.areEqual({ a: 1 }, Object.assign({ a: 1 }, undefined), "Passing undefined as nextSource in assign method is a no-op");
  27. assert.areEqual({ a: 1 }, Object.assign({ a: 1 }, null), "Passing null as nextSource in assign method is a no-op");
  28. }
  29. },
  30. {
  31. name: "Object.assign(target) returns target",
  32. body: function () {
  33. var o = {x:10, y:20, z:30}
  34. assert.isTrue(o === Object.assign(o), "Object.assign(target) should return the same object");
  35. assert.isTrue(o === Object.assign(o, { a: 1 }), "Object.assign(target, source) should return the same object");
  36. assert.isTrue(o === Object.assign(o, { b: 2 }, { c: 3 }), "Object.assign(target, source1, source2) should return the same object");
  37. }
  38. },
  39. {
  40. name: "Object.assign({}, object) clone should work",
  41. body: function () {
  42. assert.areEqual({ a: 1 }, Object.assign({}, { a: 1 }), "Create a clone of a object");
  43. assert.areEqual([1], Object.assign({}, [1]), "Create a clone of a array");
  44. assert.areEqual(new String("hello"), Object.assign({}, new String("hello")), "Create a clone of a string");
  45. }
  46. },
  47. {
  48. name: "Object.assign(o1, o2, o3) Merging objects should work",
  49. body: function () {
  50. assert.areEqual({ a: 1, b: 2}, Object.assign({ a: 1 }, { b: 2 }), "Merging 2 plain objects should work");
  51. assert.areEqual({ a: 1, b: 2, c: 3 }, Object.assign({ a: 1 }, { b: 2 }, { c: 3 }), "Merging 3 plain objects should work");
  52. assert.areEqual({ a: 1, b: 2, c: 3, d: 4 }, Object.assign({ a: 1 }, { b: 2 }, { c: 3 }, { d: 4 }), "Merging 4 plain objects should work");
  53. }
  54. },
  55. {
  56. name: "Object.assign(o1, o2, o3) overlapping property names should preference last set",
  57. body: function () {
  58. assert.areEqual({ a: 2 }, Object.assign({ a: 1 }, { a: 2 }), "Merging 2 plain objects with the overlapping property name should work");
  59. assert.areEqual({ a: 1, b: 2, c: 3, z: 3 }, Object.assign({ a: 1, z: 1 }, { b: 2, z: 2 }, { c: 3, z: 3 }), "Merging 3 plain objects with overlapping property name should work");
  60. }
  61. },
  62. {
  63. name: "Object.assign(o1, o2) throw exception if GetOwnProperty for any enumerable property in o2 if enumeration fails",
  64. body: function () {
  65. try
  66. {
  67. Object.assign({}, { get x() { throw "xyz"; } });
  68. assert.fail("Exception is not thrown when GetOwnProperty fails");
  69. }
  70. catch (e)
  71. {
  72. }
  73. }
  74. },
  75. {
  76. name: "Object.assign(o1, o2) exception if SetProperty for any enumerable property in o2 if enumeration fails",
  77. body: function () {
  78. try {
  79. Object.assign({ set x(a) { throw "xyz"; } }, { x: 10 });
  80. assert.fail("Exception is not thrown when SetProperty fails");
  81. }
  82. catch (e) {
  83. }
  84. }
  85. },
  86. {
  87. name: "Object.assign(o1, o2, o2) should work on undefined values",
  88. body: function () {
  89. assert.areEqual({x: undefined}, Object.assign({}, {x : 40}, { x: undefined }), "Undefined values for the property keys are propagated");
  90. }
  91. },
  92. {
  93. name: "Object.assign(o1, o2, o2) ignore non-enumerable values",
  94. body: function () {
  95. var o2 = { y: 40 };
  96. Object.defineProperty(o2, "x", {value: 50, enumerable:false})
  97. assert.areEqual({y: 40}, Object.assign({}, o2), "Non enumerable values are not ignored");
  98. }
  99. },
  100. {
  101. name: "Object.assign(Object.create(Array.prototype), o2) create pattern",
  102. body: function () {
  103. assert.areEqual({ x: 10, y: 40 }, Object.assign(Object.create(Array.prototype), { x: 10, y: 40 }), "Object.create pattern should work ");
  104. }
  105. },
  106. {
  107. name: "Object.assign(o1, o2) works for symbols",
  108. body: function () {
  109. var o2 = {};
  110. var y = Symbol("y");
  111. o2[y] = 10;
  112. assert.isTrue((Object.assign({}, o2))[y] === 10, "Symbols are assigned to target object ");
  113. }
  114. },
  115. {
  116. name: "Object.assign(o1, o2) works for index properties",
  117. body: function () {
  118. assert.areEqual([1, 2, 3], Object.assign([], [1, 2, 3]), "Indexed properties are assigned to the target object");
  119. }
  120. },
  121. {
  122. name: "Object.assign(o1, o2) exception if SetProperty fails due to preventExtentions on o1",
  123. body: function () {
  124. assert.throws((function() { 'use strict'; var o = Object.preventExtensions([,0]);Object.assign(o,'xo');}), TypeError, "Invalid operand to 'Object.assign': Object expected");
  125. }
  126. },
  127. {
  128. name: "OS Bug 3080673: Object.assign(o1, o2) exception if SetProperty fails due to non-writable on o1 when o1's value is a String",
  129. body: function () {
  130. assert.throws((function() {
  131. var o1 = "aaa";
  132. var o2 = "babbb";
  133. Object.assign(o1, o2);
  134. }), TypeError, "Exception is not thrown when SetProperty fails", "Cannot modify non-writable property '0'");
  135. }
  136. },
  137. ];
  138. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });