native.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use strict';
  2. var test = require('tape');
  3. var defineProperties = require('define-properties');
  4. var isEnumerable = Object.prototype.propertyIsEnumerable;
  5. var functionsHaveNames = require('functions-have-names')();
  6. var runTests = require('./tests');
  7. test('native', function (t) {
  8. t.equal(Object.assign.length, 2, 'Object.assign has a length of 2');
  9. t.test('Function name', { skip: !functionsHaveNames }, function (st) {
  10. st.equal(Object.assign.name, 'assign', 'Object.assign has name "assign"');
  11. st.end();
  12. });
  13. t.test('enumerability', { skip: !defineProperties.supportsDescriptors }, function (et) {
  14. et.equal(false, isEnumerable.call(Object, 'assign'), 'Object.assign is not enumerable');
  15. et.end();
  16. });
  17. var supportsStrictMode = (function () { return typeof this === 'undefined'; }());
  18. t.test('bad object value', { skip: !supportsStrictMode }, function (st) {
  19. st['throws'](function () { return Object.assign(undefined); }, TypeError, 'undefined is not an object');
  20. st['throws'](function () { return Object.assign(null); }, TypeError, 'null is not an object');
  21. st.end();
  22. });
  23. // v8 in node 0.8 and 0.10 have non-enumerable string properties
  24. var stringCharsAreEnumerable = isEnumerable.call('xy', 0);
  25. t.test('when Object.assign is present and has pending exceptions', { skip: !stringCharsAreEnumerable || !Object.preventExtensions }, function (st) {
  26. /*
  27. * Firefox 37 still has "pending exception" logic in its Object.assign implementation,
  28. * which is 72% slower than our shim, and Firefox 40's native implementation.
  29. */
  30. var thrower = Object.preventExtensions({ 1: '2' });
  31. var error;
  32. try { Object.assign(thrower, 'xy'); } catch (e) { error = e; }
  33. st.equal(error instanceof TypeError, true, 'error is TypeError');
  34. st.equal(thrower[1], '2', 'thrower[1] === "2"');
  35. st.end();
  36. });
  37. runTests(Object.assign, t);
  38. t.end();
  39. });