Iterator.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. 'use strict';
  2. var defineProperties = require('define-properties');
  3. var test = require('tape');
  4. var functionsHaveNames = require('functions-have-names')();
  5. var index = require('../Iterator');
  6. var impl = require('../Iterator/implementation');
  7. var isEnumerable = Object.prototype.propertyIsEnumerable;
  8. module.exports = {
  9. tests: function (Iter, name, t) {
  10. t.equal(typeof Iter, 'function', name + ' is a function');
  11. t['throws'](
  12. function () { Iter(); }, // eslint-disable-line new-cap
  13. TypeError,
  14. name + ' throws when Call-ed'
  15. );
  16. t['throws'](
  17. function () { return new Iter(); },
  18. TypeError,
  19. name + ' throws when Construct-ed'
  20. );
  21. var SubIter;
  22. var SubSubIter;
  23. try {
  24. /* eslint no-new-func: 0 */
  25. SubIter = Function('Iter', 'return class SubIter extends Iter {};')(Iter);
  26. SubSubIter = Function('SubIter', 'return class SubSubIter extends SubIter {};')(SubIter);
  27. } catch (e) { /**/ }
  28. t.test('class inheritance', { skip: !SubIter }, function (st) {
  29. st.doesNotThrow(
  30. function () { return new SubIter(); },
  31. 'Extending ' + name + ' does not throw when Construct-ed'
  32. );
  33. st.doesNotThrow(
  34. function () { return new SubSubIter(); },
  35. 'Extending ' + name + ' twice does not throw when Construct-ed'
  36. );
  37. st.end();
  38. });
  39. },
  40. index: function () {
  41. test('Iterator: index', function (t) {
  42. module.exports.tests(index, 'Iterator', t);
  43. t.end();
  44. });
  45. },
  46. implementation: function () {
  47. test('Iterator: implementation', function (t) {
  48. module.exports.tests(impl, 'Iterator', t);
  49. t.end();
  50. });
  51. },
  52. shimmed: function () {
  53. test('Iterator: shimmed', function (t) {
  54. t.test('Function name', { skip: !functionsHaveNames }, function (st) {
  55. st.equal(Iterator.name, 'Iterator', 'Iterator has name "Iterator"');
  56. st.end();
  57. });
  58. t.test('enumerability', { skip: !defineProperties.supportsDescriptors }, function (et) {
  59. et.equal(false, isEnumerable.call(global, Iterator), 'Iterator is not enumerable');
  60. et.end();
  61. });
  62. t.test('prototype descriptor', { skip: !defineProperties.supportsDescriptors }, function (pt) {
  63. var desc = Object.getOwnPropertyDescriptor(Iterator, 'prototype');
  64. pt.deepEqual(
  65. desc,
  66. {
  67. configurable: false,
  68. enumerable: false,
  69. value: Iterator.prototype,
  70. writable: false
  71. }
  72. );
  73. pt.end();
  74. });
  75. module.exports.tests(Iterator, 'Iterator', t);
  76. t.end();
  77. });
  78. }
  79. };