index.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. 'use strict';
  2. var test = require('tape');
  3. var stopIterationIterator = require('../');
  4. test('stopIterationIterator', function (t) {
  5. t.equal(typeof stopIterationIterator, 'function', 'stopIterationIterator is a function');
  6. t.test('no StopIteration support', { skip: typeof StopIteration === 'object' }, function (st) {
  7. st['throws'](
  8. // @ts-expect-error
  9. function () { stopIterationIterator(); },
  10. SyntaxError,
  11. 'throws a SyntaxError when StopIteration is not supported'
  12. );
  13. st.end();
  14. });
  15. t.test('StopIteration support', { skip: typeof StopIteration !== 'object' }, function (st) {
  16. // eslint-disable-next-line no-extra-parens
  17. var s = /** @type {Set<number> & { iterator(): SetIterator<number>}} */ (new Set([1, 2]));
  18. var i = s.iterator();
  19. st.equal(i.next(), 1, 'first item is 1');
  20. st.equal(i.next(), 2, 'second item is 2');
  21. try {
  22. i.next();
  23. st.fail();
  24. } catch (e) {
  25. st.equal(e, StopIteration, 'StopIteration thrown');
  26. }
  27. // eslint-disable-next-line no-extra-parens
  28. var m = /** @type {Map<number, string> & { iterator(): MapIterator<[string, number]>}} */ (new Map([[1, 'a'], [2, 'b']]));
  29. var mi = m.iterator();
  30. st.deepEqual(mi.next(), [1, 'a'], 'first item is 1 and a');
  31. st.deepEqual(mi.next(), [2, 'b'], 'second item is 2 and b');
  32. try {
  33. mi.next();
  34. st.fail();
  35. } catch (e) {
  36. st.equal(e, StopIteration, 'StopIteration thrown');
  37. }
  38. st.end();
  39. });
  40. t.end();
  41. });