index.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. function () { stopIterationIterator(); },
  9. SyntaxError,
  10. 'throws a SyntaxError when StopIteration is not supported'
  11. );
  12. st.end();
  13. });
  14. t.test('StopIteration support', { skip: typeof StopIteration !== 'object' }, function (st) {
  15. var s = new Set([1, 2]);
  16. var i = s.iterator();
  17. st.equal(i.next(), 1, 'first item is 1');
  18. st.equal(i.next(), 2, 'second item is 2');
  19. try {
  20. i.next();
  21. st.fail();
  22. } catch (e) {
  23. st.equal(e, StopIteration, 'StopIteration thrown');
  24. }
  25. var m = new Map([[1, 'a'], [2, 'b']]);
  26. var mi = m.iterator();
  27. st.deepEqual(mi.next(), [1, 'a'], 'first item is 1 and a');
  28. st.deepEqual(mi.next(), [2, 'b'], 'second item is 2 and b');
  29. try {
  30. mi.next();
  31. st.fail();
  32. } catch (e) {
  33. st.equal(e, StopIteration, 'StopIteration thrown');
  34. }
  35. st.end();
  36. });
  37. t.end();
  38. });