|
| 1 | +// eslint-disable-next-line import/order |
| 2 | +import { test } from './prepare-test-env-ava.js'; |
| 3 | + |
| 4 | +// eslint-disable-next-line import/order |
| 5 | +import { M } from '@endo/patterns'; |
| 6 | +import { defineExoClass, defineExoClassKit } from '../src/exo-makers.js'; |
| 7 | + |
| 8 | +const { apply } = Reflect; |
| 9 | + |
| 10 | +const UpCounterI = M.interface('UpCounter', { |
| 11 | + incr: M.call() |
| 12 | + // TODO M.number() should not be needed to get a better error message |
| 13 | + .optional(M.and(M.number(), M.gte(0))) |
| 14 | + .returns(M.number()), |
| 15 | +}); |
| 16 | + |
| 17 | +const DownCounterI = M.interface('DownCounter', { |
| 18 | + decr: M.call() |
| 19 | + // TODO M.number() should not be needed to get a better error message |
| 20 | + .optional(M.and(M.number(), M.gte(0))) |
| 21 | + .returns(M.number()), |
| 22 | +}); |
| 23 | + |
| 24 | +test('test revoke defineExoClass', t => { |
| 25 | + let revoke; |
| 26 | + const makeUpCounter = defineExoClass( |
| 27 | + 'UpCounter', |
| 28 | + UpCounterI, |
| 29 | + /** @param {number} x */ |
| 30 | + (x = 0) => ({ x }), |
| 31 | + { |
| 32 | + incr(y = 1) { |
| 33 | + const { state } = this; |
| 34 | + state.x += y; |
| 35 | + return state.x; |
| 36 | + }, |
| 37 | + }, |
| 38 | + { |
| 39 | + receiveRevoker(r) { |
| 40 | + revoke = r; |
| 41 | + }, |
| 42 | + }, |
| 43 | + ); |
| 44 | + const upCounter = makeUpCounter(3); |
| 45 | + t.is(upCounter.incr(5), 8); |
| 46 | + t.is(revoke(upCounter), true); |
| 47 | + t.throws(() => upCounter.incr(1), { |
| 48 | + message: |
| 49 | + '"In \\"incr\\" method of (UpCounter)" may only be applied to a valid instance: "[Alleged: UpCounter]"', |
| 50 | + }); |
| 51 | +}); |
| 52 | + |
| 53 | +test('test revoke defineExoClassKit', t => { |
| 54 | + let revoke; |
| 55 | + const makeCounterKit = defineExoClassKit( |
| 56 | + 'Counter', |
| 57 | + { up: UpCounterI, down: DownCounterI }, |
| 58 | + /** @param {number} x */ |
| 59 | + (x = 0) => ({ x }), |
| 60 | + { |
| 61 | + up: { |
| 62 | + incr(y = 1) { |
| 63 | + const { state } = this; |
| 64 | + state.x += y; |
| 65 | + return state.x; |
| 66 | + }, |
| 67 | + }, |
| 68 | + down: { |
| 69 | + decr(y = 1) { |
| 70 | + const { state } = this; |
| 71 | + state.x -= y; |
| 72 | + return state.x; |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + { |
| 77 | + receiveRevoker(r) { |
| 78 | + revoke = r; |
| 79 | + }, |
| 80 | + }, |
| 81 | + ); |
| 82 | + const { up: upCounter, down: downCounter } = makeCounterKit(3); |
| 83 | + t.is(upCounter.incr(5), 8); |
| 84 | + t.is(downCounter.decr(), 7); |
| 85 | + t.is(revoke(upCounter), true); |
| 86 | + t.is(revoke(upCounter), false); |
| 87 | + t.throws(() => upCounter.incr(3), { |
| 88 | + message: |
| 89 | + '"In \\"incr\\" method of (Counter up)" may only be applied to a valid instance: "[Alleged: Counter up]"', |
| 90 | + }); |
| 91 | + t.is(revoke(downCounter), true); |
| 92 | + t.is(revoke(downCounter), false); |
| 93 | + t.throws(() => downCounter.decr(), { |
| 94 | + message: |
| 95 | + '"In \\"decr\\" method of (Counter down)" may only be applied to a valid instance: "[Alleged: Counter down]"', |
| 96 | + }); |
| 97 | +}); |
| 98 | + |
| 99 | +test('test facet cross-talk', t => { |
| 100 | + const makeCounterKit = defineExoClassKit( |
| 101 | + 'Counter', |
| 102 | + { up: UpCounterI, down: DownCounterI }, |
| 103 | + /** @param {number} x */ |
| 104 | + (x = 0) => ({ x }), |
| 105 | + { |
| 106 | + up: { |
| 107 | + incr(y = 1) { |
| 108 | + const { state } = this; |
| 109 | + state.x += y; |
| 110 | + return state.x; |
| 111 | + }, |
| 112 | + }, |
| 113 | + down: { |
| 114 | + decr(y = 1) { |
| 115 | + const { state } = this; |
| 116 | + state.x -= y; |
| 117 | + return state.x; |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + ); |
| 122 | + const { up: upCounter, down: downCounter } = makeCounterKit(3); |
| 123 | + t.throws(() => apply(upCounter.incr, downCounter, [2]), { |
| 124 | + message: |
| 125 | + '"In \\"incr\\" method of (Counter up)" may only be applied to a valid instance: "[Alleged: Counter down]"', |
| 126 | + }); |
| 127 | +}); |
0 commit comments