|
| 1 | +import assert from "assert"; |
| 2 | +import {CachedAsyncIterable} from "../src/index"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Return a promise for an array with all the elements of the iterable. |
| 6 | + * |
| 7 | + * It uses for-await to support async iterables which can't be spread with |
| 8 | + * ...iterable. See https://github.com/tc39/proposal-async-iteration/issues/103 |
| 9 | + * |
| 10 | + */ |
| 11 | +async function toArray(iterable) { |
| 12 | + const result = []; |
| 13 | + for await (const elem of iterable) { |
| 14 | + result.push(elem); |
| 15 | + } |
| 16 | + return result; |
| 17 | +} |
| 18 | + |
| 19 | +suite("CachedAsyncIterable", function() { |
| 20 | + suite("constructor errors", function(){ |
| 21 | + test("no argument", function() { |
| 22 | + function run() { |
| 23 | + new CachedAsyncIterable(); |
| 24 | + } |
| 25 | + |
| 26 | + assert.throws(run, TypeError); |
| 27 | + assert.throws(run, /iteration protocol/); |
| 28 | + }); |
| 29 | + |
| 30 | + test("null argument", function() { |
| 31 | + function run() { |
| 32 | + new CachedAsyncIterable(null); |
| 33 | + } |
| 34 | + |
| 35 | + assert.throws(run, TypeError); |
| 36 | + assert.throws(run, /iteration protocol/); |
| 37 | + }); |
| 38 | + |
| 39 | + test("bool argument", function() { |
| 40 | + function run() { |
| 41 | + new CachedAsyncIterable(1); |
| 42 | + } |
| 43 | + |
| 44 | + assert.throws(run, TypeError); |
| 45 | + assert.throws(run, /iteration protocol/); |
| 46 | + }); |
| 47 | + |
| 48 | + test("number argument", function() { |
| 49 | + function run() { |
| 50 | + new CachedAsyncIterable(1); |
| 51 | + } |
| 52 | + |
| 53 | + assert.throws(run, TypeError); |
| 54 | + assert.throws(run, /iteration protocol/); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + suite("async iteration", function(){ |
| 59 | + let o1, o2; |
| 60 | + |
| 61 | + suiteSetup(function() { |
| 62 | + o1 = Object(); |
| 63 | + o2 = Object(); |
| 64 | + }); |
| 65 | + |
| 66 | + test("lazy iterable", async function() { |
| 67 | + async function *generate() { |
| 68 | + yield *[o1, o2]; |
| 69 | + } |
| 70 | + |
| 71 | + const iterable = new CachedAsyncIterable(generate()); |
| 72 | + assert.deepEqual(await toArray(iterable), [o1, o2]); |
| 73 | + }); |
| 74 | + |
| 75 | + test("lazy iterable works more than once", async function() { |
| 76 | + async function *generate() { |
| 77 | + let i = 2; |
| 78 | + |
| 79 | + while (--i) { |
| 80 | + yield Object(); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + const iterable = new CachedAsyncIterable(generate()); |
| 85 | + const first = await toArray(iterable); |
| 86 | + assert.deepEqual(await toArray(iterable), first); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + suite("async touchNext", function(){ |
| 91 | + let o1, o2, generateMessages; |
| 92 | + |
| 93 | + suiteSetup(function() { |
| 94 | + o1 = Object(); |
| 95 | + o2 = Object(); |
| 96 | + |
| 97 | + generateMessages = async function *generateMessages() { |
| 98 | + yield *[o1, o2]; |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | + test("consumes an element into the cache", async function() { |
| 103 | + const iterable = new CachedAsyncIterable(generateMessages()); |
| 104 | + assert.equal(iterable.seen.length, 0); |
| 105 | + await iterable.touchNext(); |
| 106 | + assert.equal(iterable.seen.length, 1); |
| 107 | + }); |
| 108 | + |
| 109 | + test("allows to consume multiple elements into the cache", async function() { |
| 110 | + const iterable = new CachedAsyncIterable(generateMessages()); |
| 111 | + await iterable.touchNext(); |
| 112 | + await iterable.touchNext(); |
| 113 | + assert.equal(iterable.seen.length, 2); |
| 114 | + }); |
| 115 | + |
| 116 | + test("allows to consume multiple elements at once", async function() { |
| 117 | + const iterable = new CachedAsyncIterable(generateMessages()); |
| 118 | + await iterable.touchNext(2); |
| 119 | + assert.equal(iterable.seen.length, 2); |
| 120 | + }); |
| 121 | + |
| 122 | + test("stops at the last element", async function() { |
| 123 | + const iterable = new CachedAsyncIterable(generateMessages()); |
| 124 | + await iterable.touchNext(); |
| 125 | + await iterable.touchNext(); |
| 126 | + await iterable.touchNext(); |
| 127 | + assert.equal(iterable.seen.length, 3); |
| 128 | + |
| 129 | + await iterable.touchNext(); |
| 130 | + assert.equal(iterable.seen.length, 3); |
| 131 | + }); |
| 132 | + |
| 133 | + test("works on an empty iterable", async function() { |
| 134 | + async function *generateEmptyMessages() { |
| 135 | + yield *[]; |
| 136 | + } |
| 137 | + const iterable = new CachedAsyncIterable(generateEmptyMessages()); |
| 138 | + await iterable.touchNext(); |
| 139 | + await iterable.touchNext(); |
| 140 | + await iterable.touchNext(); |
| 141 | + assert.equal(iterable.seen.length, 1); |
| 142 | + }); |
| 143 | + |
| 144 | + test("iteration for such cache works", async function() { |
| 145 | + const iterable = new CachedAsyncIterable(generateMessages()); |
| 146 | + await iterable.touchNext(); |
| 147 | + await iterable.touchNext(); |
| 148 | + await iterable.touchNext(); |
| 149 | + |
| 150 | + // It's a bit quirky compared to the sync counterpart, |
| 151 | + // but there's no good way to fold async iterator into |
| 152 | + // an array. |
| 153 | + let values = []; |
| 154 | + for await (let elem of iterable) { |
| 155 | + values.push(elem); |
| 156 | + } |
| 157 | + assert.deepEqual(values, [o1, o2]); |
| 158 | + }); |
| 159 | + |
| 160 | + test("async version handles sync iterator", async function() { |
| 161 | + const iterable = new CachedAsyncIterable([o1, o2]); |
| 162 | + await iterable.touchNext(); |
| 163 | + await iterable.touchNext(); |
| 164 | + await iterable.touchNext(); |
| 165 | + |
| 166 | + // It's a bit quirky compared to the sync counterpart, |
| 167 | + // but there's no good way to fold async iterator into |
| 168 | + // an array. |
| 169 | + let values = []; |
| 170 | + for await (let elem of iterable) { |
| 171 | + values.push(elem); |
| 172 | + } |
| 173 | + assert.deepEqual(values, [o1, o2]); |
| 174 | + }); |
| 175 | + }); |
| 176 | +}); |
0 commit comments