|
3 | 3 |
|
4 | 4 | const common = require('../common'); |
5 | 5 | const assert = require('assert'); |
6 | | -const { fromSync } = require('stream/iter'); |
| 6 | +const { fromSync, textSync } = require('stream/iter'); |
7 | 7 |
|
8 | 8 | function testFromSyncString() { |
9 | 9 | // String input should be UTF-8 encoded |
@@ -186,6 +186,30 @@ function testFromSyncRejectsAsyncIterable() { |
186 | 186 | assert.throws(() => fromSync(gen()), { code: 'ERR_INVALID_ARG_TYPE' }); |
187 | 187 | } |
188 | 188 |
|
| 189 | +function testFromSyncPrefersIteratorForDualIterable() { |
| 190 | + const input = { |
| 191 | + *[Symbol.iterator]() { |
| 192 | + yield new TextEncoder().encode('sync'); |
| 193 | + }, |
| 194 | + async *[Symbol.asyncIterator]() { |
| 195 | + yield new TextEncoder().encode('async'); |
| 196 | + }, |
| 197 | + }; |
| 198 | + |
| 199 | + assert.strictEqual(textSync(fromSync(input)), 'sync'); |
| 200 | +} |
| 201 | + |
| 202 | +function testFromSyncPrefersIteratorForThenableIterable() { |
| 203 | + const input = { |
| 204 | + then() {}, |
| 205 | + *[Symbol.iterator]() { |
| 206 | + yield new TextEncoder().encode('sync'); |
| 207 | + }, |
| 208 | + }; |
| 209 | + |
| 210 | + assert.strictEqual(textSync(fromSync(input)), 'sync'); |
| 211 | +} |
| 212 | + |
189 | 213 | // Promise rejected |
190 | 214 | function testFromSyncRejectsPromise() { |
191 | 215 | assert.throws(() => fromSync(Promise.resolve('hello')), |
@@ -232,6 +256,8 @@ Promise.all([ |
232 | 256 | testFromSyncTopLevelProtocolOverIterator(), |
233 | 257 | testFromSyncIgnoresAsyncStreamable(), |
234 | 258 | testFromSyncRejectsAsyncIterable(), |
| 259 | + testFromSyncPrefersIteratorForDualIterable(), |
| 260 | + testFromSyncPrefersIteratorForThenableIterable(), |
235 | 261 | testFromSyncRejectsPromise(), |
236 | 262 | testFromSyncDataView(), |
237 | 263 | ]).then(common.mustCall()); |
0 commit comments