Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. // 73: Generator - `return` inside a generator is special
  2. // To do: make all tests pass, leave the assert lines unchanged!
  3. // Follow the hints of the failure messages!
  4.  
  5. describe('`return` in a generator function is special', function() {
  6. describe('the returned value is an IteratorResult (just like any value returned via `yield`)', function() {
  7. it('returns an IteratorResult (an object with the properties `value` and `done`)', function() {
  8. function* generatorFunction() { return 1; }
  9. const returned = generatorFunction().next();
  10. const propertyNames = ['value', 'done'];
  11. assert.deepEqual(Object.keys(returned), propertyNames);
  12. });
  13. it('the property `value` is the returned value', function() {
  14. function* generatorFunction() { return 23; }
  15. const {value} = generatorFunction().next();
  16. assert.equal(value, 23);
  17. });
  18. it('the property `done` is true', function() {
  19. function* generatorFunction() { return 42; }
  20. const {done} = generatorFunction().next();
  21. assert.equal(done, true);
  22. });
  23. it('NOTE: `yield` does not return `done=true` but `done=false`!', function() {
  24. function* generatorFunction() { yield 1; }
  25. const returned = generatorFunction().next();
  26. assert.deepEqual(returned, {value: 1, done: false});
  27. });
  28. it('a missing `return` returns `{value: undefined, done: true}`', function() {
  29. function* generatorFunction() { return; }
  30. const returned = generatorFunction().next();
  31. assert.deepEqual(returned, {value: void 0, done: true});
  32. });
  33. });
  34.  
  35. describe('mixing `return` and `yield`', function() {
  36. function* generatorFunctionWithYieldAndReturn() {
  37. yield 1;
  38. return 2;
  39. }
  40. it('is possible', function() {
  41. const iterator = generatorFunctionWithYieldAndReturn();
  42. const values = [
  43. iterator.next(),
  44. iterator.next()
  45. ];
  46. assert.deepEqual(values, [{value: 1, done: false}, {value: 2, done: true}]);
  47. });
  48. it('the mix behaves different to two `yield`s', function() {
  49. const iterator = generatorFunctionWithYieldAndReturn();
  50. const values = [1];
  51. assert.deepEqual(Array.from(iterator), values);
  52. });
  53. it('two `yield`s returning values', function() {
  54. function* generatorFunctionWithTwoYields() {
  55. yield 1;
  56. yield 2;
  57.  
  58. }
  59. assert.deepEqual(Array.from(generatorFunctionWithTwoYields()), [1, 2]);
  60. });
  61. it('return a yielded value by "chaining" `return` and `yield`', function() {
  62. function* generatorFunction() {
  63. yield 1;
  64. yield 2;
  65. }
  66. const iterator = generatorFunction();
  67. const values = [
  68. iterator.next().value,
  69. iterator.next(2).value
  70. ];
  71. assert.deepEqual(values, [1, 2]);
  72. });
  73. });
  74. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement