| 1 |
"use strict"; |
| 2 |
|
| 3 |
const promisify = require("util.promisify"); |
| 4 |
const gensync = require("../"); |
| 5 |
|
| 6 |
const TEST_ERROR = new Error("TEST_ERROR"); |
| 7 |
|
| 8 |
const DID_ERROR = new Error("DID_ERROR"); |
| 9 |
|
| 10 |
const doSuccess = gensync({ |
| 11 |
sync: () => 42, |
| 12 |
async: () => Promise.resolve(42), |
| 13 |
}); |
| 14 |
|
| 15 |
const doError = gensync({ |
| 16 |
sync: () => { |
| 17 |
throw DID_ERROR; |
| 18 |
}, |
| 19 |
async: () => Promise.reject(DID_ERROR), |
| 20 |
}); |
| 21 |
|
| 22 |
function throwTestError() { |
| 23 |
throw TEST_ERROR; |
| 24 |
} |
| 25 |
|
| 26 |
async function expectResult( |
| 27 |
fn, |
| 28 |
arg, |
| 29 |
{ error, value, expectSync = false, syncErrback = expectSync } |
| 30 |
) { |
| 31 |
if (!expectSync) { |
| 32 |
expect(() => fn.sync(arg)).toThrow(TEST_ERROR); |
| 33 |
} else if (error) { |
| 34 |
expect(() => fn.sync(arg)).toThrow(error); |
| 35 |
} else { |
| 36 |
expect(fn.sync(arg)).toBe(value); |
| 37 |
} |
| 38 |
|
| 39 |
if (error) { |
| 40 |
await expect(fn.async(arg)).rejects.toBe(error); |
| 41 |
} else { |
| 42 |
await expect(fn.async(arg)).resolves.toBe(value); |
| 43 |
} |
| 44 |
|
| 45 |
await new Promise((resolve, reject) => { |
| 46 |
let sync = true; |
| 47 |
fn.errback(arg, (err, val) => { |
| 48 |
try { |
| 49 |
expect(err).toBe(error); |
| 50 |
expect(val).toBe(value); |
| 51 |
expect(sync).toBe(syncErrback); |
| 52 |
|
| 53 |
resolve(); |
| 54 |
} catch (e) { |
| 55 |
reject(e); |
| 56 |
} |
| 57 |
}); |
| 58 |
sync = false; |
| 59 |
}); |
| 60 |
} |
| 61 |
|
| 62 |
describe("gensync({})", () => { |
| 63 |
describe("option validation", () => { |
| 64 |
test("disallow async and errback handler together", () => { |
| 65 |
try { |
| 66 |
gensync({ |
| 67 |
sync: throwTestError, |
| 68 |
async: throwTestError, |
| 69 |
errback: throwTestError, |
| 70 |
}); |
| 71 |
|
| 72 |
throwTestError(); |
| 73 |
} catch (err) { |
| 74 |
expect(err.message).toMatch( |
| 75 |
/Expected one of either opts.async or opts.errback, but got _both_\./ |
| 76 |
); |
| 77 |
expect(err.code).toBe("GENSYNC_OPTIONS_ERROR"); |
| 78 |
} |
| 79 |
}); |
| 80 |
|
| 81 |
test("disallow missing sync handler", () => { |
| 82 |
try { |
| 83 |
gensync({ |
| 84 |
async: throwTestError, |
| 85 |
}); |
| 86 |
|
| 87 |
throwTestError(); |
| 88 |
} catch (err) { |
| 89 |
expect(err.message).toMatch(/Expected opts.sync to be a function./); |
| 90 |
expect(err.code).toBe("GENSYNC_OPTIONS_ERROR"); |
| 91 |
} |
| 92 |
}); |
| 93 |
|
| 94 |
test("errback callback required", () => { |
| 95 |
const fn = gensync({ |
| 96 |
sync: throwTestError, |
| 97 |
async: throwTestError, |
| 98 |
}); |
| 99 |
|
| 100 |
try { |
| 101 |
fn.errback(); |
| 102 |
|
| 103 |
throwTestError(); |
| 104 |
} catch (err) { |
| 105 |
expect(err.message).toMatch(/function called without callback/); |
| 106 |
expect(err.code).toBe("GENSYNC_ERRBACK_NO_CALLBACK"); |
| 107 |
} |
| 108 |
}); |
| 109 |
}); |
| 110 |
|
| 111 |
describe("generator function metadata", () => { |
| 112 |
test("automatic naming", () => { |
| 113 |
expect( |
| 114 |
gensync({ |
| 115 |
sync: function readFileSync() {}, |
| 116 |
async: () => {}, |
| 117 |
}).name |
| 118 |
).toBe("readFile"); |
| 119 |
expect( |
| 120 |
gensync({ |
| 121 |
sync: function readFile() {}, |
| 122 |
async: () => {}, |
| 123 |
}).name |
| 124 |
).toBe("readFile"); |
| 125 |
expect( |
| 126 |
gensync({ |
| 127 |
sync: function readFileAsync() {}, |
| 128 |
async: () => {}, |
| 129 |
}).name |
| 130 |
).toBe("readFileAsync"); |
| 131 |
|
| 132 |
expect( |
| 133 |
gensync({ |
| 134 |
sync: () => {}, |
| 135 |
async: function readFileSync() {}, |
| 136 |
}).name |
| 137 |
).toBe("readFileSync"); |
| 138 |
expect( |
| 139 |
gensync({ |
| 140 |
sync: () => {}, |
| 141 |
async: function readFile() {}, |
| 142 |
}).name |
| 143 |
).toBe("readFile"); |
| 144 |
expect( |
| 145 |
gensync({ |
| 146 |
sync: () => {}, |
| 147 |
async: function readFileAsync() {}, |
| 148 |
}).name |
| 149 |
).toBe("readFile"); |
| 150 |
|
| 151 |
expect( |
| 152 |
gensync({ |
| 153 |
sync: () => {}, |
| 154 |
errback: function readFileSync() {}, |
| 155 |
}).name |
| 156 |
).toBe("readFileSync"); |
| 157 |
expect( |
| 158 |
gensync({ |
| 159 |
sync: () => {}, |
| 160 |
errback: function readFile() {}, |
| 161 |
}).name |
| 162 |
).toBe("readFile"); |
| 163 |
expect( |
| 164 |
gensync({ |
| 165 |
sync: () => {}, |
| 166 |
errback: function readFileAsync() {}, |
| 167 |
}).name |
| 168 |
).toBe("readFileAsync"); |
| 169 |
}); |
| 170 |
|
| 171 |
test("explicit naming", () => { |
| 172 |
expect( |
| 173 |
gensync({ |
| 174 |
name: "readFile", |
| 175 |
sync: () => {}, |
| 176 |
async: () => {}, |
| 177 |
}).name |
| 178 |
).toBe("readFile"); |
| 179 |
}); |
| 180 |
|
| 181 |
test("default arity", () => { |
| 182 |
expect( |
| 183 |
gensync({ |
| 184 |
sync: function(a, b, c, d, e, f, g){ throwTestError(); }, |
| 185 |
async: throwTestError, |
| 186 |
}).length |
| 187 |
).toBe(7); |
| 188 |
}); |
| 189 |
|
| 190 |
test("explicit arity", () => { |
| 191 |
expect( |
| 192 |
gensync({ |
| 193 |
arity: 3, |
| 194 |
sync: throwTestError, |
| 195 |
async: throwTestError, |
| 196 |
}).length |
| 197 |
).toBe(3); |
| 198 |
}); |
| 199 |
}); |
| 200 |
|
| 201 |
describe("'sync' handler", async () => { |
| 202 |
test("success", async () => { |
| 203 |
const fn = gensync({ |
| 204 |
sync: (...args) => JSON.stringify(args), |
| 205 |
}); |
| 206 |
|
| 207 |
await expectResult(fn, 42, { value: "[42]", expectSync: true }); |
| 208 |
}); |
| 209 |
|
| 210 |
test("failure", async () => { |
| 211 |
const fn = gensync({ |
| 212 |
sync: (...args) => { |
| 213 |
throw JSON.stringify(args); |
| 214 |
}, |
| 215 |
}); |
| 216 |
|
| 217 |
await expectResult(fn, 42, { error: "[42]", expectSync: true }); |
| 218 |
}); |
| 219 |
}); |
| 220 |
|
| 221 |
describe("'async' handler", async () => { |
| 222 |
test("success", async () => { |
| 223 |
const fn = gensync({ |
| 224 |
sync: throwTestError, |
| 225 |
async: (...args) => Promise.resolve(JSON.stringify(args)), |
| 226 |
}); |
| 227 |
|
| 228 |
await expectResult(fn, 42, { value: "[42]" }); |
| 229 |
}); |
| 230 |
|
| 231 |
test("failure", async () => { |
| 232 |
const fn = gensync({ |
| 233 |
sync: throwTestError, |
| 234 |
async: (...args) => Promise.reject(JSON.stringify(args)), |
| 235 |
}); |
| 236 |
|
| 237 |
await expectResult(fn, 42, { error: "[42]" }); |
| 238 |
}); |
| 239 |
}); |
| 240 |
|
| 241 |
describe("'errback' sync handler", async () => { |
| 242 |
test("success", async () => { |
| 243 |
const fn = gensync({ |
| 244 |
sync: throwTestError, |
| 245 |
errback: (...args) => args.pop()(null, JSON.stringify(args)), |
| 246 |
}); |
| 247 |
|
| 248 |
await expectResult(fn, 42, { value: "[42]", syncErrback: true }); |
| 249 |
}); |
| 250 |
|
| 251 |
test("failure", async () => { |
| 252 |
const fn = gensync({ |
| 253 |
sync: throwTestError, |
| 254 |
errback: (...args) => args.pop()(JSON.stringify(args)), |
| 255 |
}); |
| 256 |
|
| 257 |
await expectResult(fn, 42, { error: "[42]", syncErrback: true }); |
| 258 |
}); |
| 259 |
}); |
| 260 |
|
| 261 |
describe("'errback' async handler", async () => { |
| 262 |
test("success", async () => { |
| 263 |
const fn = gensync({ |
| 264 |
sync: throwTestError, |
| 265 |
errback: (...args) => |
| 266 |
process.nextTick(() => args.pop()(null, JSON.stringify(args))), |
| 267 |
}); |
| 268 |
|
| 269 |
await expectResult(fn, 42, { value: "[42]" }); |
| 270 |
}); |
| 271 |
|
| 272 |
test("failure", async () => { |
| 273 |
const fn = gensync({ |
| 274 |
sync: throwTestError, |
| 275 |
errback: (...args) => |
| 276 |
process.nextTick(() => args.pop()(JSON.stringify(args))), |
| 277 |
}); |
| 278 |
|
| 279 |
await expectResult(fn, 42, { error: "[42]" }); |
| 280 |
}); |
| 281 |
}); |
| 282 |
}); |
| 283 |
|
| 284 |
describe("gensync(function* () {})", () => { |
| 285 |
test("sync throw before body", async () => { |
| 286 |
const fn = gensync(function*(arg = throwTestError()) {}); |
| 287 |
|
| 288 |
await expectResult(fn, undefined, { |
| 289 |
error: TEST_ERROR, |
| 290 |
syncErrback: true, |
| 291 |
}); |
| 292 |
}); |
| 293 |
|
| 294 |
test("sync throw inside body", async () => { |
| 295 |
const fn = gensync(function*() { |
| 296 |
throwTestError(); |
| 297 |
}); |
| 298 |
|
| 299 |
await expectResult(fn, undefined, { |
| 300 |
error: TEST_ERROR, |
| 301 |
syncErrback: true, |
| 302 |
}); |
| 303 |
}); |
| 304 |
|
| 305 |
test("async throw inside body", async () => { |
| 306 |
const fn = gensync(function*() { |
| 307 |
const val = yield* doSuccess(); |
| 308 |
throwTestError(); |
| 309 |
}); |
| 310 |
|
| 311 |
await expectResult(fn, undefined, { |
| 312 |
error: TEST_ERROR, |
| 313 |
}); |
| 314 |
}); |
| 315 |
|
| 316 |
test("error inside body", async () => { |
| 317 |
const fn = gensync(function*() { |
| 318 |
yield* doError(); |
| 319 |
}); |
| 320 |
|
| 321 |
await expectResult(fn, undefined, { |
| 322 |
error: DID_ERROR, |
| 323 |
expectSync: true, |
| 324 |
syncErrback: false, |
| 325 |
}); |
| 326 |
}); |
| 327 |
|
| 328 |
test("successful return value", async () => { |
| 329 |
const fn = gensync(function*() { |
| 330 |
const value = yield* doSuccess(); |
| 331 |
|
| 332 |
expect(value).toBe(42); |
| 333 |
|
| 334 |
return 84; |
| 335 |
}); |
| 336 |
|
| 337 |
await expectResult(fn, undefined, { |
| 338 |
value: 84, |
| 339 |
expectSync: true, |
| 340 |
syncErrback: false, |
| 341 |
}); |
| 342 |
}); |
| 343 |
|
| 344 |
test("successful final value", async () => { |
| 345 |
const fn = gensync(function*() { |
| 346 |
return 42; |
| 347 |
}); |
| 348 |
|
| 349 |
await expectResult(fn, undefined, { |
| 350 |
value: 42, |
| 351 |
expectSync: true, |
| 352 |
}); |
| 353 |
}); |
| 354 |
|
| 355 |
test("yield unexpected object", async () => { |
| 356 |
const fn = gensync(function*() { |
| 357 |
yield {}; |
| 358 |
}); |
| 359 |
|
| 360 |
try { |
| 361 |
await fn.async(); |
| 362 |
|
| 363 |
throwTestError(); |
| 364 |
} catch (err) { |
| 365 |
expect(err.message).toMatch( |
| 366 |
/Got unexpected yielded value in gensync generator/ |
| 367 |
); |
| 368 |
expect(err.code).toBe("GENSYNC_EXPECTED_START"); |
| 369 |
} |
| 370 |
}); |
| 371 |
|
| 372 |
test("yield suspend yield", async () => { |
| 373 |
const fn = gensync(function*() { |
| 374 |
yield Symbol.for("gensync:v1:start"); |
| 375 |
|
| 376 |
// Should be "yield*" for no error. |
| 377 |
yield {}; |
| 378 |
}); |
| 379 |
|
| 380 |
try { |
| 381 |
await fn.async(); |
| 382 |
|
| 383 |
throwTestError(); |
| 384 |
} catch (err) { |
| 385 |
expect(err.message).toMatch(/Expected GENSYNC_SUSPEND, got {}/); |
| 386 |
expect(err.code).toBe("GENSYNC_EXPECTED_SUSPEND"); |
| 387 |
} |
| 388 |
}); |
| 389 |
|
| 390 |
test("yield suspend return", async () => { |
| 391 |
const fn = gensync(function*() { |
| 392 |
yield Symbol.for("gensync:v1:start"); |
| 393 |
|
| 394 |
// Should be "yield*" for no error. |
| 395 |
return {}; |
| 396 |
}); |
| 397 |
|
| 398 |
try { |
| 399 |
await fn.async(); |
| 400 |
|
| 401 |
throwTestError(); |
| 402 |
} catch (err) { |
| 403 |
expect(err.message).toMatch(/Unexpected generator completion/); |
| 404 |
expect(err.code).toBe("GENSYNC_EXPECTED_SUSPEND"); |
| 405 |
} |
| 406 |
}); |
| 407 |
}); |
| 408 |
|
| 409 |
describe("gensync.all()", () => { |
| 410 |
test("success", async () => { |
| 411 |
const fn = gensync(function*() { |
| 412 |
const result = yield* gensync.all([doSuccess(), doSuccess()]); |
| 413 |
|
| 414 |
expect(result).toEqual([42, 42]); |
| 415 |
}); |
| 416 |
|
| 417 |
await expectResult(fn, undefined, { |
| 418 |
value: undefined, |
| 419 |
expectSync: true, |
| 420 |
syncErrback: false, |
| 421 |
}); |
| 422 |
}); |
| 423 |
|
| 424 |
test("error first", async () => { |
| 425 |
const fn = gensync(function*() { |
| 426 |
yield* gensync.all([doError(), doSuccess()]); |
| 427 |
}); |
| 428 |
|
| 429 |
await expectResult(fn, undefined, { |
| 430 |
error: DID_ERROR, |
| 431 |
expectSync: true, |
| 432 |
syncErrback: false, |
| 433 |
}); |
| 434 |
}); |
| 435 |
|
| 436 |
test("error last", async () => { |
| 437 |
const fn = gensync(function*() { |
| 438 |
yield* gensync.all([doSuccess(), doError()]); |
| 439 |
}); |
| 440 |
|
| 441 |
await expectResult(fn, undefined, { |
| 442 |
error: DID_ERROR, |
| 443 |
expectSync: true, |
| 444 |
syncErrback: false, |
| 445 |
}); |
| 446 |
}); |
| 447 |
}); |
| 448 |
|
| 449 |
describe("gensync.race()", () => { |
| 450 |
test("success", async () => { |
| 451 |
const fn = gensync(function*() { |
| 452 |
const result = yield* gensync.race([doSuccess(), doError()]); |
| 453 |
|
| 454 |
expect(result).toEqual(42); |
| 455 |
}); |
| 456 |
|
| 457 |
await expectResult(fn, undefined, { |
| 458 |
value: undefined, |
| 459 |
expectSync: true, |
| 460 |
syncErrback: false, |
| 461 |
}); |
| 462 |
}); |
| 463 |
|
| 464 |
test("error", async () => { |
| 465 |
const fn = gensync(function*() { |
| 466 |
yield* gensync.race([doError(), doSuccess()]); |
| 467 |
}); |
| 468 |
|
| 469 |
await expectResult(fn, undefined, { |
| 470 |
error: DID_ERROR, |
| 471 |
expectSync: true, |
| 472 |
syncErrback: false, |
| 473 |
}); |
| 474 |
}); |
| 475 |
}); |