Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.01 KB | None | 0 0
  1. // 49: Generator - creation
  2. // To do: make all tests pass, leave the assert lines unchanged!
  3. // Follow the hints of the failure messages!
  4.  
  5. describe('Generators can be created in multiple ways', function() {
  6. it('the most common way is by adding `*` after `function`', function() {
  7. function* g() {}
  8. assertIsGenerator(g());
  9. });
  10. it('as a function expression, by adding a `*` after `function`', function() {
  11. let g = function*() {};
  12. assertIsGenerator(g());
  13. });
  14. it('inside an object by prefixing the function name with `*`', function() {
  15. let obj = {
  16. *g() {}
  17. };
  18. assertIsGenerator(obj.g());
  19. });
  20. it('computed generator names, are just prefixed with a `*`', function() {
  21. const generatorName = 'g';
  22. let obj = {
  23. *[generatorName]() {}
  24. };
  25. assertIsGenerator(obj.g());
  26. });
  27. it('inside a class the same way', function() {
  28. const generatorName = 'g';
  29. class Klazz {
  30. *[generatorName]() {}
  31. }
  32. assertIsGenerator(new Klazz().g());
  33. });
  34.  
  35. function assertIsGenerator(gen) {
  36. const toStringed = '' + gen;
  37. assert.equal(toStringed, '[object Generator]');
  38. }
  39. });
  40.  
  41. // 51: Generator - Yield Expressions
  42. // To do: make all tests pass, leave the assert lines unchanged!
  43. // Follow the hints of the failure messages!
  44.  
  45. describe('Generator - `yield` is used to pause and resume a generator function', () => {
  46. function* generatorFunction() {
  47. yield 'hello';
  48. yield 'world';
  49. }
  50. let generator;
  51.  
  52. beforeEach(function() {
  53. generator = generatorFunction();
  54. });
  55. it('converting a generator to an array (using `Array.from`) resumes the generator until all values are received', () => {
  56. let values = Array.from(generatorFunction());
  57. assert.deepEqual(values, ['hello', 'world']);
  58. });
  59. describe('after the first `generator.next()` call', function() {
  60. it('the value is "hello"', function() {
  61. const {value} = generator.next();
  62. assert.equal(value, 'hello');
  63. });
  64. it('and `done` is false', function() {
  65. const {done} = generator.next();
  66.  
  67. assert.equal(done, false);
  68. });
  69. });
  70. describe('after the second `next()` call', function() {
  71. let secondItem;
  72. beforeEach(function() {
  73. generator.next()
  74. secondItem = generator.next();
  75. });
  76. it('`value` is "world"', function() {
  77. let {value} = secondItem;
  78. assert.equal(value, 'world');
  79. });
  80. it('and `done` is still false', function() {
  81. const done = secondItem.done;
  82. assert.equal(done, false);
  83. });
  84. });
  85. describe('after stepping past the last element, calling `next()` that often', function() {
  86. it('`done` property equals true, since there is nothing more to iterator over', function() {
  87. generator.next();
  88. generator.next();
  89. let {done} = generator.next();
  90. assert.equal(done, true);
  91. });
  92. });
  93. });
  94.  
  95.  
  96. // 52: Generator - Send value to a generator
  97. // To do: make all tests pass, leave the assert lines unchanged!
  98. // Follow the hints of the failure messages!
  99.  
  100. describe('Pass a value to a generator', () => {
  101. it('basics: get the values from a generator in two ways', function() {
  102. function* generatorFunction() {
  103. yield 1;
  104. yield 2;
  105. }
  106. // way #1
  107. var convertedToAnArray = Array.from(generatorFunction());
  108. // way #2
  109. var iterator = generatorFunction();
  110. var iteratedOver = [iterator.next('test').value, iterator.next().value];
  111. assert.deepEqual(convertedToAnArray, iteratedOver);
  112. });
  113. it('pass a value to the iterator', function() {
  114. function* generatorFunction() {
  115. yield 1;
  116. yield 2;
  117. }
  118. var iterator = generatorFunction();
  119. var iteratedOver = [iterator.next().value, iterator.next(2).value];
  120. assert.deepEqual([1, 2], iteratedOver);
  121. });
  122. it('a value passed to the 1st `next()` call is ignored', function() {
  123. function* generatorFunction() {
  124. yield 1;
  125. yield 2
  126. }
  127. let iterator = generatorFunction();
  128. const values = [iterator.next('test').value, iterator.next().value];
  129. assert.deepEqual(values, [1, 2]);
  130. });
  131. });
  132.  
  133.  
  134. // 56: Generator - Send function to a generator
  135. // To do: make all tests pass, leave the assert lines unchanged!
  136. // Follow the hints of the failure messages!
  137.  
  138. describe('Pass a function to a generator', () => {
  139. it('the generator can receive a function as a value', function() {
  140. let fn = function() {};
  141. function* generatorFunction() {
  142. yield fn
  143. assert.equal(yield null, fn); // remember, don't touch this line
  144. }
  145. let iterator = generatorFunction();
  146. iterator.next();
  147. iterator.next();
  148. });
  149. it('pass a function to the iterator, which calls it', function() {
  150. function* generatorFunction() {
  151. yield (yield 1)(yield 2);
  152. }
  153. var iterator = generatorFunction();
  154. var iteratedOver = [iterator.next().value, iterator.next().value];
  155. assert.deepEqual([1, 2], iteratedOver);
  156. });
  157. it('nesting yielded function calls', function() {
  158. function* generatorFunction() {
  159. yield (yield (yield 1)());
  160. }
  161.  
  162. var iteratedOver = [1, 2, 3];
  163. assert.deepEqual([1, 2, 3], iteratedOver);
  164. });
  165. });
  166.  
  167. // 56: Generator - Send function to a generator
  168. // To do: make all tests pass, leave the assert lines unchanged!
  169. // Follow the hints of the failure messages!
  170.  
  171. describe('Pass a function to a generator', () => {
  172. it('the generator can receive a function as a value', function() {
  173. let fn = function() {};
  174. function* generatorFunction() {
  175. yield fn
  176. assert.equal(yield null, fn); // remember, don't touch this line
  177. }
  178. let iterator = generatorFunction();
  179. iterator.next();
  180. iterator.next();
  181. });
  182. it('pass a function to the iterator, which calls it', function() {
  183. function* generatorFunction() {
  184. yield (yield 1)(yield 2);
  185. }
  186. var iterator = generatorFunction();
  187. var iteratedOver = [iterator.next().value, iterator.next().value];
  188. assert.deepEqual([1, 2], iteratedOver);
  189. });
  190. it('nesting yielded function calls', function() {
  191. function* generatorFunction() {
  192. yield (yield (yield 1)());
  193. }
  194.  
  195. var iteratedOver = [1, 2, 3];
  196. assert.deepEqual([1, 2, 3], iteratedOver);
  197. });
  198. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement