|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { Future } = require('..'); |
| 4 | +const metatests = require('metatests'); |
| 5 | + |
| 6 | +metatests.test('Future map/fork', async test => { |
| 7 | + Future.of(3) |
| 8 | + .map(x => x ** 2) |
| 9 | + .fork(value => { |
| 10 | + test.strictSame(value, 9); |
| 11 | + test.end(); |
| 12 | + }); |
| 13 | +}); |
| 14 | + |
| 15 | +metatests.test('Future lazy', async test => { |
| 16 | + Future.of(3).map(test.mustNotCall()); |
| 17 | + test.end(); |
| 18 | +}); |
| 19 | + |
| 20 | +metatests.test('Future resolve', async test => { |
| 21 | + new Future(resolve => { |
| 22 | + setTimeout(() => { |
| 23 | + resolve(5); |
| 24 | + }, 0); |
| 25 | + }).fork(value => { |
| 26 | + test.strictSame(value, 5); |
| 27 | + test.end(); |
| 28 | + }, test.mustNotCall()); |
| 29 | +}); |
| 30 | + |
| 31 | +metatests.test('Future reject', async test => { |
| 32 | + new Future((resolve, reject) => { |
| 33 | + reject(new Error('msg')); |
| 34 | + }).fork(test.mustNotCall(), error => { |
| 35 | + test.strictSame(error.message, 'msg'); |
| 36 | + test.end(); |
| 37 | + }); |
| 38 | +}); |
| 39 | + |
| 40 | +metatests.test('Future promise', async test => { |
| 41 | + const value = await Future.of(6) |
| 42 | + .map(x => ++x) |
| 43 | + .map(x => x ** 3) |
| 44 | + .promise(); |
| 45 | + |
| 46 | + test.strictSame(value, 343); |
| 47 | + test.end(); |
| 48 | +}); |
| 49 | + |
| 50 | +metatests.test('Future catch', async test => { |
| 51 | + Future.of(6) |
| 52 | + .map(() => { |
| 53 | + throw new Error('msg'); |
| 54 | + }) |
| 55 | + .fork(test.mustNotCall, error => { |
| 56 | + test.strictSame(error.message, 'msg'); |
| 57 | + test.end(); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +metatests.test('Future stateless', async test => { |
| 62 | + const f1 = Future.of(3); |
| 63 | + const f2 = f1.map(x => ++x); |
| 64 | + const f3 = f2.map(x => x ** 3); |
| 65 | + const f4 = f1.map(x => x * 2); |
| 66 | + |
| 67 | + f1.fork(value => { |
| 68 | + test.strictSame(value, 3); |
| 69 | + }); |
| 70 | + |
| 71 | + f1.fork(value => { |
| 72 | + test.strictSame(value, 3); |
| 73 | + }); |
| 74 | + |
| 75 | + f2.fork(value => { |
| 76 | + test.strictSame(value, 4); |
| 77 | + }); |
| 78 | + |
| 79 | + f2.fork(value => { |
| 80 | + test.strictSame(value, 4); |
| 81 | + }); |
| 82 | + |
| 83 | + f3.fork(value => { |
| 84 | + test.strictSame(value, 64); |
| 85 | + }); |
| 86 | + |
| 87 | + f4.fork(value => { |
| 88 | + test.strictSame(value, 6); |
| 89 | + }); |
| 90 | + |
| 91 | + test.end(); |
| 92 | +}); |
0 commit comments