polyfills.tests.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* global QUnit */
  2. import '../../../src/polyfills';
  3. import { Vector3 } from '../../../src/math/Vector3';
  4. export default QUnit.module( 'Polyfills', () => {
  5. // PUBLIC STUFF
  6. QUnit.test( "Number.EPSILON", ( assert ) => {
  7. assert.equal( Number.EPSILON, Math.pow( 2, - 52 ), "Number.EPSILON should be equal to 2 to the power of -52." );
  8. } );
  9. //https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger#Examples
  10. QUnit.test( "Number.isInteger", ( assert ) => {
  11. assert.ok( Number.isInteger( 0 ), "0 is an integer." );
  12. assert.ok( Number.isInteger( 1 ), "1 is an integer." );
  13. assert.ok( Number.isInteger( - 100000 ), "-100000 is an integer." );
  14. assert.ok( Number.isInteger( 99999999999999999999999 ), "99999999999999999999999 is an integer." );
  15. assert.notOk( Number.isInteger( 0.1 ), "0.1 is not an integer." );
  16. assert.notOk( Number.isInteger( Math.PI ), "PI is not an integer." );
  17. assert.notOk( Number.isInteger( NaN ), "NaN is not an integer." );
  18. assert.notOk( Number.isInteger( Infinity ), "Infinity is not an integer." );
  19. assert.notOk( Number.isInteger( - Infinity ), "-Infinity is not an integer." );
  20. assert.notOk( Number.isInteger( '10' ), "A string, for example '10', is not an integer." );
  21. assert.notOk( Number.isInteger( true ), "true is not an integer." );
  22. assert.notOk( Number.isInteger( false ), "false is not an integer." );
  23. assert.notOk( Number.isInteger( [ 1 ] ), "Array of a number, for example [1], is not an integer." );
  24. } );
  25. //https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign
  26. //http://people.mozilla.org/~jorendorff/es6-draft.html#sec-math.sign
  27. /*
  28. 20.2.2.29 Math.sign(x)
  29. Returns the sign of the x, indicating whether x is positive, negative or zero.
  30. If x is NaN, the result is NaN.
  31. If x is -0, the result is -0.
  32. If x is +0, the result is +0.
  33. If x is negative and not -0, the result is -1.
  34. If x is positive and not +0, the result is +1.
  35. */
  36. QUnit.test( "Math.sign", ( assert ) => {
  37. assert.ok( isNaN( Math.sign( NaN ) ), "If x is NaN<NaN>, the result is NaN." );
  38. assert.ok( isNaN( Math.sign( new Vector3() ) ), "If x is NaN<object>, the result is NaN." );
  39. assert.ok( isNaN( Math.sign() ), "If x is NaN<undefined>, the result is NaN." );
  40. assert.ok( isNaN( Math.sign( '--3' ) ), "If x is NaN<'--3'>, the result is NaN." );
  41. assert.ok( isNegativeZero( Math.sign( - 0 ) ), "If x is -0, the result is -0." );
  42. assert.ok( Math.sign( + 0 ) === + 0, "If x is +0, the result is +0." );
  43. assert.ok( Math.sign( - Infinity ) === - 1, "If x is negative<-Infinity> and not -0, the result is -1." );
  44. assert.ok( Math.sign( '-3' ) === - 1, "If x is negative<'-3'> and not -0, the result is -1." );
  45. assert.ok( Math.sign( '-1e-10' ) === - 1, "If x is negative<'-1e-10'> and not -0, the result is -1." );
  46. assert.ok( Math.sign( + Infinity ) === + 1, "If x is positive<+Infinity> and not +0, the result is +1." );
  47. assert.ok( Math.sign( '+3' ) === + 1, "If x is positive<'+3'> and not +0, the result is +1." );
  48. // Comparing with -0 is tricky because 0 === -0. But
  49. // luckily 1 / -0 === -Infinity so we can use that.
  50. function isNegativeZero( value ) {
  51. return value === 0 && 1 / value < 0;
  52. }
  53. } );
  54. QUnit.test( "'name' in Function.prototype", ( assert ) => {
  55. function test() {}
  56. assert.equal( Function.prototype.name, '', "Name on the prototype of Function should be declared." );
  57. assert.equal( test.name, 'test', "Name of function 'test' should be 'test'." );
  58. } );
  59. // https://github.com/tc39/test262/tree/master/test/built-ins/Object/assign
  60. QUnit.test( "Object.assign", ( assert ) => {
  61. var target = { a: 1 };
  62. var result = Object.assign( target, { a: 2 }, { a: "c" } );
  63. assert.equal( result.a, "c", "The value should be 'c'." );
  64. target = "a";
  65. result = Object.assign( target );
  66. assert.equal( typeof result, "object" );
  67. assert.equal( result.valueOf(), "a", "The value should be 'a'." );
  68. target = 12;
  69. result = Object.assign( target, "aaa", "bb2b", "1c" );
  70. assert.equal( Object.keys( result ).length, 4, "The length should be 4 in the final object." );
  71. assert.equal( result[ 0 ], "1", "The value should be {\"0\":\"1\"}." );
  72. assert.equal( result[ 1 ], "c", "The value should be {\"1\":\"c\"}." );
  73. assert.equal( result[ 2 ], "2", "The value should be {\"2\":\"2\"}." );
  74. assert.equal( result[ 3 ], "b", "The value should be {\"3\":\"b\"}." );
  75. target = { a: 1 };
  76. result = Object.assign( target, "1a2c3", { a: "c" }, undefined, { b: 6 }, null, 125, { a: 5 } );
  77. assert.equal( Object.keys( result ).length, 7, "The length should be 7 in the final object." );
  78. assert.equal( result.a, 5, "The value should be {a:5}." );
  79. assert.equal( result[ 0 ], "1", "The value should be {\"0\":\"1\"}." );
  80. assert.equal( result[ 1 ], "a", "The value should be {\"1\":\"a\"}." );
  81. assert.equal( result[ 2 ], "2", "The value should be {\"2\":\"2\"}." );
  82. assert.equal( result[ 3 ], "c", "The value should be {\"3\":\"c\"}." );
  83. assert.equal( result[ 4 ], "3", "The value should be {\"4\":\"3\"}." );
  84. assert.equal( result.b, 6, "The value should be {b:6}." );
  85. target = new Object();
  86. result = Object.assign( target, undefined, null );
  87. assert.equal( result, target, "null and undefined should be ignored, result should be original object." );
  88. target = new Object();
  89. result = Object.assign( target, 123, true, Symbol( 'foo' ) );
  90. assert.equal( result, target, "Numbers, booleans, and symbols cannot have wrappers with own enumerable properties." );
  91. target = new Object();
  92. result = Object.assign( target, 123, true, Symbol( 'foo' ) );
  93. assert.equal( result, target, "Numbers, booleans, and symbols cannot have wrappers with own enumerable properties." );
  94. var target = new Object();
  95. var result = Object.assign( target, "123" );
  96. assert.equal( result[ 0 ], "1", "The value should be {\"0\":\"1\"}." );
  97. assert.equal( result[ 1 ], "2", "The value should be {\"1\":\"2\"}." );
  98. assert.equal( result[ 2 ], "3", "The value should be {\"2\":\"3\"}." );
  99. var result = Object.assign( true, { a: 1 } );
  100. assert.equal( typeof result, "object", "Return value should be an object." );
  101. assert.equal( result.valueOf(), true, "Return value should be true." );
  102. assert.throws( function () {
  103. Object.assign( null, { a: 1 } );
  104. }, TypeError );
  105. var result = Object.assign( 1, { a: 1 } );
  106. assert.equal( typeof result, "object", "Return value should be an object." );
  107. assert.equal( result.valueOf(), 1, "Return value should be 1." );
  108. var target = { foo: 1 };
  109. var result = Object.assign( target, { a: 2 } );
  110. assert.equal( result.foo, 1, "The value should be {foo: 1}." );
  111. assert.equal( result.a, 2, "The value should be {a: 2}." );
  112. var target = { foo: 1 };
  113. var result = Object.assign( target, { a: 2 } );
  114. assert.equal( result.foo, 1, "The value should be {foo: 1}." );
  115. assert.equal( result.a, 2, "The value should be {a: 2}." );
  116. assert.throws( function () {
  117. Object.assign( undefined, { a: 1 } );
  118. }, TypeError );
  119. assert.equal( Object.assign.length, 2, "The length property of the assign method should be 2." );
  120. assert.throws( function () {
  121. new Object.assign( {} );
  122. }, TypeError );
  123. assert.equal( Object.assign.name, 'assign', 'The value of `Object.assign.name` is `"assign"`' );
  124. var target = {};
  125. var source = Object.defineProperty( {}, 'attr', {
  126. value: 1,
  127. enumerable: false
  128. } );
  129. var result = Object.assign( target, source );
  130. assert.equal( Object.hasOwnProperty.call( target, 'attr' ), false );
  131. assert.equal( result, target );
  132. var callCount = 0;
  133. var target = {};
  134. var result;
  135. var source = new Proxy( {}, {
  136. ownKeys: function () {
  137. callCount += 1;
  138. return [ 'missing' ];
  139. }
  140. } );
  141. result = Object.assign( target, source );
  142. assert.equal( callCount, 1, 'Proxy trap was invoked exactly once' );
  143. assert.equal( Object.hasOwnProperty.call( target, 'missing' ), false, 'An own property was not created for a property without a property descriptor' );
  144. assert.equal( result, target );
  145. } );
  146. } );