You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

shimmed.js 1.9KB

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