-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathmarshal-far-function.test.js
74 lines (65 loc) · 1.99 KB
/
marshal-far-function.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import test from '@endo/ses-ava/prepare-endo.js';
import { getInterfaceOf, passStyleOf, Far } from '@endo/pass-style';
const { freeze, setPrototypeOf } = Object;
const harden = /** @type {import('ses').Harden & { isFake?: boolean }} */ (
// eslint-disable-next-line no-undef
global.harden
);
test('Far functions', t => {
t.notThrows(() => Far('arrow', a => a + 1), 'Far function');
const arrow = Far('arrow', a => a + 1);
t.is(passStyleOf(arrow), 'remotable');
t.is(getInterfaceOf(arrow), 'Alleged: arrow');
});
test('Acceptable far functions', t => {
t.is(passStyleOf(Far('asyncArrow', async a => a + 1)), 'remotable');
// Even though concise methods start as methods, they can be
// made into far functions *instead*.
const concise = { doFoo() {} }.doFoo;
t.is(passStyleOf(Far('concise', concise)), 'remotable');
});
test('Unacceptable far functions', t => {
if (!harden.isFake) {
t.throws(
() =>
Far(
'alreadyFrozen',
freeze(a => a + 1),
),
{
message: /is already frozen/,
},
);
}
// eslint-disable-next-line prefer-arrow-callback -- under test
t.throws(() => Far('keywordFunc', function keyword() {}), {
message: /unexpected properties besides \.name and \.length/,
});
});
test('Far functions cannot be methods', t => {
const doFoo = Far('doFoo', a => a + 1);
t.throws(
() =>
Far('badMethod', {
doFoo,
}),
{
message: /Remotables with non-methods/,
},
);
});
test('Data can contain far functions', t => {
const arrow = Far('arrow', a => a + 1);
t.is(passStyleOf(harden({ x: 8, foo: arrow })), 'copyRecord');
const mightBeMethod = a => a + 1;
t.throws(() => passStyleOf(harden({ x: 8, foo: mightBeMethod })), {
message: /Remotables with non-methods like "x" /,
});
});
test('function without prototype', t => {
const arrow = a => a;
setPrototypeOf(arrow, null);
t.throws(() => Far('arrow', arrow), {
message: /must not inherit from null/,
});
});