object-is.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.is should exist and have length 2",
  10. body: function () {
  11. assert.isTrue(Object.hasOwnProperty('is'), "Object should have an is method");
  12. assert.areEqual(2, Object.is.length, "is method takes two arguments");
  13. }
  14. },
  15. {
  16. name: "Object.is(undefined, y) returns true for y = undefined, false otherwise",
  17. body: function () {
  18. assert.isTrue(Object.is(undefined, undefined), "Object.is(undefined, y) returns true for Type(y) = Undefined");
  19. assert.isFalse(Object.is(undefined, null), "Object.is(undefined, y) returns false for Type(y) = Null");
  20. assert.isFalse(Object.is(undefined, false), "Object.is(undefined, y) returns false for Type(y) = Boolean");
  21. assert.isFalse(Object.is(undefined, ""), "Object.is(undefined, y) returns false for Type(y) = String");
  22. assert.isFalse(Object.is(undefined, Symbol()), "Object.is(undefined, y) returns false for Type(y) = Symbol");
  23. assert.isFalse(Object.is(undefined, 0), "Object.is(undefined, y) returns false for Type(y) = Number");
  24. assert.isFalse(Object.is(undefined, { }), "Object.is(undefined, y) returns false for Type(y) = Object");
  25. }
  26. },
  27. {
  28. name: "Object.is(null, y) returns true for y = null, false otherwise",
  29. body: function () {
  30. assert.isFalse(Object.is(null, undefined), "Object.is(null, y) returns false for Type(y) = Undefined");
  31. assert.isTrue(Object.is(null, null), "Object.is(null, y) returns true for Type(y) = Null");
  32. assert.isFalse(Object.is(null, false), "Object.is(null, y) returns false for Type(y) = Boolean");
  33. assert.isFalse(Object.is(null, ""), "Object.is(null, y) returns false for Type(y) = String");
  34. assert.isFalse(Object.is(null, Symbol()), "Object.is(null, y) returns false for Type(y) = Symbol");
  35. assert.isFalse(Object.is(null, 0), "Object.is(null, y) returns false for Type(y) = Number");
  36. assert.isFalse(Object.is(null, { }), "Object.is(null, y) returns false for Type(y) = Object");
  37. }
  38. },
  39. {
  40. name: "Object.is(x, y), where Type(x) is Number, returns true for y = x (bitwise), false otherwise",
  41. body: function () {
  42. assert.isFalse(Object.is(0, undefined), "Object.is(0, y) returns false for Type(y) = Undefined");
  43. assert.isFalse(Object.is(0, null), "Object.is(0, y) returns false for Type(y) = Null");
  44. assert.isFalse(Object.is(0, false), "Object.is(0, y) returns false for Type(y) = Boolean");
  45. assert.isFalse(Object.is(0, ""), "Object.is(0, y) returns false for Type(y) = String");
  46. assert.isFalse(Object.is(0, Symbol()), "Object.is(0, y) returns false for Type(y) = Symbol");
  47. assert.isTrue(Object.is(0, 0), "Object.is(0, y) returns true for Type(y) = Number");
  48. assert.isFalse(Object.is(0, { }), "Object.is(0, y) returns false for Type(y) = Object");
  49. assert.isTrue(Object.is(NaN, NaN), "Object.is(NaN, NaN) returns true");
  50. assert.isFalse(Object.is(+0, -0), "Object.is(+0, -0) returns false");
  51. assert.isFalse(Object.is(-0, +0), "Object.is(+0, -0) returns false");
  52. assert.isTrue(Object.is(10, 10), "Object.is(10, 10) returns true");
  53. assert.isFalse(Object.is(10, -10), "Object.is(10, 10) returns false");
  54. assert.isTrue(Object.is(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY), "Object.is(+Infinity, +Infinity) returns true");
  55. assert.isTrue(Object.is(Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY), "Object.is(-Infinity, -Infinity) returns true");
  56. assert.isFalse(Object.is(Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY), "Object.is(+Infinity, -Infinity) returns false");
  57. }
  58. },
  59. {
  60. name: "Object.is(x, y), where Type(x) is String, returns true when Type(y) is String and x and y have the same sequence of code points, false otherwise",
  61. body: function () {
  62. assert.isFalse(Object.is("", undefined), "Object.is('', y) returns false for Type(y) = Undefined");
  63. assert.isFalse(Object.is("", null), "Object.is('', y) returns false for Type(y) = Null");
  64. assert.isFalse(Object.is("", false), "Object.is('', y) returns false for Type(y) = Boolean");
  65. assert.isTrue(Object.is("", ""), "Object.is('', y) returns true for Type(y) = String where x == y");
  66. assert.isFalse(Object.is("", Symbol()), "Object.is('', y) returns false for Type(y) = Symbol");
  67. assert.isFalse(Object.is("", 0), "Object.is('', y) returns false for Type(y) = Number");
  68. assert.isFalse(Object.is("", { }), "Object.is('', y) returns false for Type(y) = Object");
  69. assert.isTrue(Object.is("abc", "abc"), "Object.is('abc', 'abc') returns true");
  70. assert.isFalse(Object.is("abc", "xyz"), "Object.is('abc', 'xyz') returns false");
  71. }
  72. },
  73. {
  74. name: "Object.is(x, y), where Type(x) is Boolean, returns true when Type(y) is Boolean and x = y, false otherwise",
  75. body: function () {
  76. assert.isFalse(Object.is(false, undefined), "Object.is(false, y) returns false for Type(y) = Undefined");
  77. assert.isFalse(Object.is(false, null), "Object.is(false, y) returns false for Type(y) = Null");
  78. assert.isTrue(Object.is(false, false), "Object.is(false, y) returns true for Type(y) = Boolean where x == y");
  79. assert.isFalse(Object.is(false, ""), "Object.is(false, y) returns false for Type(y) = String");
  80. assert.isFalse(Object.is(false, Symbol()), "Object.is(false, y) returns false for Type(y) = Symbol");
  81. assert.isFalse(Object.is(false, 0), "Object.is(false, y) returns false for Type(y) = Number");
  82. assert.isFalse(Object.is(false, { }), "Object.is(false, y) returns false for Type(y) = Object");
  83. assert.isTrue(Object.is(true, true), "Object.is(true, true) returns true");
  84. assert.isFalse(Object.is(false, true), "Object.is(false, true) returns false");
  85. }
  86. },
  87. {
  88. name: "Object.is(x, y), where Type(x) is Symbol, returns true when Type(y) is Symbol and x and y are the same symbol, false otherwise",
  89. body: function () {
  90. var sym = Symbol();
  91. assert.isFalse(Object.is(sym, undefined), "Object.is(Symbol(), y) returns false for Type(y) = Undefined");
  92. assert.isFalse(Object.is(sym, null), "Object.is(Symbol(), y) returns false for Type(y) = Null");
  93. assert.isFalse(Object.is(sym, false), "Object.is(Symbol(), y) returns false for Type(y) = Boolean");
  94. assert.isFalse(Object.is(sym, ""), "Object.is(Symbol(), y) returns false for Type(y) = String");
  95. assert.isTrue(Object.is(sym, sym), "Object.is(x, y) returns true when x and y are the same symbol");
  96. assert.isFalse(Object.is(sym, 0), "Object.is(Symbol(), y) returns false for Type(y) = Number");
  97. assert.isFalse(Object.is(sym, { }), "Object.is(Symbol(), y) returns false for Type(y) = Object");
  98. assert.isFalse(Object.is(sym, Symbol()), "Object.is(x, y) returns false where x and y are different symbols");
  99. }
  100. },
  101. {
  102. name: "Object.is(x, y), where Type(x) is Symbol, returns true when Type(y) is Symbol and x and y are the same symbol, false otherwise",
  103. body: function () {
  104. var o = { };
  105. assert.isFalse(Object.is(o, undefined), "Object.is(Symbol(), y) returns false for Type(y) = Undefined");
  106. assert.isFalse(Object.is(o, null), "Object.is(Symbol(), y) returns false for Type(y) = Null");
  107. assert.isFalse(Object.is(o, false), "Object.is(Symbol(), y) returns false for Type(y) = Boolean");
  108. assert.isFalse(Object.is(o, ""), "Object.is(Symbol(), y) returns false for Type(y) = String");
  109. assert.isFalse(Object.is(o, Symbol()), "Object.is(x, y) returns false when Type(y) = Symbol");
  110. assert.isFalse(Object.is(o, 0), "Object.is(Symbol(), y) returns false for Type(y) = Number");
  111. assert.isTrue(Object.is(o, o), "Object.is(Symbol(), y) returns true for Type(y) = Object where x and y are the same object");
  112. assert.isFalse(Object.is(o, { }), "Object.is(x, y) returns false when x and y are different objects");
  113. }
  114. },
  115. {
  116. name: "Object.is(x, y), where Type(x) is function, returns true when Type(y) is function and x and y are the same function or same throw functions in sloppy mode, false otherwise",
  117. body: function () {
  118. function f() { }
  119. function g() { }
  120. var obj1 = { f }, obj2 = { f };
  121. var bf = f.bind();
  122. var bft = f.bind({});
  123. assert.isTrue(Object.is(obj1.f, obj2.f), "Object.is should return true when comparing the same function object");
  124. assert.isTrue(Object.is(Object.getOwnPropertyDescriptor(f.__proto__, "arguments").set, Object.getOwnPropertyDescriptor(f.__proto__, "caller").set), "Object.is should return true when comparing different throw type error methods on the same function");
  125. assert.isTrue(Object.is(Object.getOwnPropertyDescriptor(f.__proto__, "arguments").set, Object.getOwnPropertyDescriptor(f.__proto__, "caller").get), "Object.is should return true when comparing different throw type error methods on the same function's different accessors");
  126. assert.isTrue(Object.is(Object.getOwnPropertyDescriptor(f.__proto__, "arguments").set, Object.getOwnPropertyDescriptor(g.__proto__, "arguments").set), "Object.is should return true when comparing same throw type error methods on different functions");
  127. assert.isTrue(Object.is(Object.getOwnPropertyDescriptor(f.__proto__, "arguments").set, Object.getOwnPropertyDescriptor(g.__proto__, "caller").set), "Object.is should return true when comparing different throw type error methods on the different functions");
  128. assert.isTrue(Object.is(Object.getOwnPropertyDescriptor(f.__proto__, "arguments").set, Object.getOwnPropertyDescriptor(g.__proto__, "caller").get), "Object.is should return true when comparing different throw type error methods on the different functions' different accessors");
  129. assert.isTrue(Object.is(Object.getOwnPropertyDescriptor(bf.__proto__, "arguments").set, Object.getOwnPropertyDescriptor(bf.__proto__, "caller").set), "Object.is should return true when comparing different throw type error methods on the same bound function");
  130. assert.isTrue(Object.is(Object.getOwnPropertyDescriptor(bf.__proto__, "arguments").set, Object.getOwnPropertyDescriptor(bf.__proto__, "caller").get), "Object.is should return true when comparing different throw type error methods on the same bound function's different accessors");
  131. assert.isTrue(Object.is(Object.getOwnPropertyDescriptor(bft.__proto__, "arguments").set, Object.getOwnPropertyDescriptor(bf.__proto__, "caller").get), "Object.is should return true when comparing different throw type error methods on a different bound function's different accessors");
  132. }
  133. },
  134. {
  135. name: "Object.is(x, y), where Type(x) is function, returns true when Type(y) is function and x and y are the same function or same throw functions in strict mode, false otherwise",
  136. body: function () {
  137. 'use strict';
  138. function f() { 'use strict'; }
  139. var bf = f.bind();
  140. var bft = f.bind({});
  141. assert.isTrue(Object.is(Object.getOwnPropertyDescriptor(Function.prototype, "arguments").set, Object.getOwnPropertyDescriptor(Function.prototype, "caller").set), "Object.is should return true when comparing different throw type error methods on the same function");
  142. assert.isTrue(Object.is(Object.getOwnPropertyDescriptor(Function.prototype, "arguments").set, Object.getOwnPropertyDescriptor(Function.prototype, "caller").get), "Object.is should return true when comparing different throw type error methods on the same function's different accessors");
  143. assert.isTrue(Object.is(Object.getOwnPropertyDescriptor(bf.__proto__, "arguments").set, Object.getOwnPropertyDescriptor(bf.__proto__, "caller").set), "Object.is should return true when comparing different throw type error methods on the same bound function");
  144. assert.isTrue(Object.is(Object.getOwnPropertyDescriptor(bf.__proto__, "arguments").set, Object.getOwnPropertyDescriptor(bf.__proto__, "caller").get), "Object.is should return true when comparing different throw type error methods on the same bound function's different accessors");
  145. assert.isTrue(Object.is(Object.getOwnPropertyDescriptor(bft.__proto__, "arguments").set, Object.getOwnPropertyDescriptor(bf.__proto__, "caller").get), "Object.is should return true when comparing different throw type error methods on a different bound function's different accessors");
  146. }
  147. },
  148. {
  149. name: "Object.is called with less or more than 2 arguments",
  150. body: function () {
  151. assert.isTrue(Object.is(), "Object.is() is the same as Object.is(undefined, undefined) which should return true");
  152. assert.isTrue(Object.is(undefined), "Object.is(undefined) is the same as Object.is(undefined, undefined) and returns true");
  153. assert.isFalse(Object.is(null), "Object.is(null) is the same as Object.is(null, undefined) and returns false");
  154. assert.isFalse(Object.is(false), "Object.is(false) is the same as Object.is(false, undefined) and returns false");
  155. assert.isFalse(Object.is(""), "Object.is('') is the same as Object.is('', undefined) and returns false");
  156. assert.isFalse(Object.is(Symbol()), "Object.is(Symbol()) is the same as Object.is(Symbol(), undefined) and returns false");
  157. assert.isFalse(Object.is(0), "Object.is(0) is the same as Object.is(0, undefined) and returns false");
  158. assert.isFalse(Object.is({ }), "Object.is({ }) is the same as Object.is({ }, undefined) and returns false");
  159. assert.isTrue(Object.is(0, 0, 1), "Object.is ignores arguments after the first two; ignores the 1");
  160. assert.isFalse(Object.is("", 0, false), "Object.is ignores arguments after the first two; ignores the false");
  161. }
  162. },
  163. ];
  164. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });