Skip to content

Commit

Permalink
fix(eventual-send): test static method rejections
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Dec 15, 2020
1 parent a2825ac commit f6bd055
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 13 deletions.
24 changes: 13 additions & 11 deletions packages/eventual-send/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,33 +354,35 @@ export function makeHandledPromise() {
};
}

const ntypeof = specimen => (specimen === null ? 'null' : typeof specimen);

// eslint-disable-next-line prefer-const
forwardingHandler = {
get: makeForwarder('get', (o, key) => o[key]),
applyMethod: makeForwarder('applyMethod', (t, method, args) => {
if (method === undefined || method === null) {
if (!(t instanceof Function)) {
const ftype = typeof t;
const ftype = ntypeof(t);
throw TypeError(
`Cannot invoke target as a function, the type is ${ftype}`,
`Cannot invoke target as a function; typeof target is ${q(ftype)}`,
);
}
return t(...args);
}
if (!t) {
const ftype = typeof t;
if (t === undefined || t === null) {
const ftype = ntypeof(t);
throw TypeError(
`Cannot deliver ${q(method)} to target; typeof target is "${ftype}"`,
`Cannot deliver ${q(method)} to target; typeof target is ${q(ftype)}`,
);
}
if (!(method in t)) {
const names = Object.getOwnPropertyNames(t).sort();
throw TypeError(`target has no method ${q(method)}, has [${names}]`);
}
if (!(t[method] instanceof Function)) {
const ftype = typeof t[method];
const ftype = ntypeof(t[method]);
if (ftype === 'undefined') {
const names = Object.getOwnPropertyNames(t).sort();
throw TypeError(`target has no method ${q(method)}, has [${names}]`);
}
throw TypeError(
`invoked method ${q(method)} is not a function, it is a ${ftype}`,
`invoked method ${q(method)} is not a function; it is a ${q(ftype)}`,
);
}
return t[method](...args);
Expand Down
4 changes: 2 additions & 2 deletions packages/eventual-send/test/test-e.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ test('E call undefined method', async t => {
},
};
await t.throwsAsync(() => E(x)(6), {
message: 'Cannot invoke target as a function, the type is object',
message: 'Cannot invoke target as a function; typeof target is "object"',
});
});

test('E invoke a non-method', async t => {
const x = { double: 24 };
await t.throwsAsync(() => E(x).double(6), {
message: 'invoked method "double" is not a function, it is a number',
message: 'invoked method "double" is not a function; it is a "number"',
});
});

Expand Down
124 changes: 124 additions & 0 deletions packages/eventual-send/test/test-eventual-send.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,127 @@ test('handled promises are promises', t => {
t.is(hp.constructor, Promise, 'The constructor is Promise');
t.is(HandledPromise.prototype, Promise.prototype, 'shared prototype');
});

test('eventual send expected errors', async t => {
t.is(
await HandledPromise.get(true, 'toString'),
true.toString,
'true.toString',
);
t.is(
await HandledPromise.get(false, 'toString'),
false.toString,
'false.toString',
);
await t.throwsAsync(
HandledPromise.get(null, 'toString'),
{ instanceOf: TypeError },
'get null.toString',
);
await t.throwsAsync(
HandledPromise.applyFunction(true, []),
{
instanceOf: TypeError,
message: 'Cannot invoke target as a function; typeof target is "boolean"',
},
'applyFunction true',
);
await t.throwsAsync(
HandledPromise.applyFunction(false, []),
{
instanceOf: TypeError,
message: 'Cannot invoke target as a function; typeof target is "boolean"',
},
'applyFunction false',
);
await t.throwsAsync(
HandledPromise.applyFunction(undefined, []),
{
instanceOf: TypeError,
message:
'Cannot invoke target as a function; typeof target is "undefined"',
},
'applyFunction undefined',
);
await t.throwsAsync(
HandledPromise.applyFunction(null, []),
{
instanceOf: TypeError,
message: 'Cannot invoke target as a function; typeof target is "null"',
},
'applyFunction null',
);
t.is(
await HandledPromise.applyMethod(true, 'toString', []),
'true',
'applyMethod true.toString()',
);
t.is(
await HandledPromise.applyMethod(false, 'toString', []),
'false',
'applyMethod false.toString()',
);
await t.throwsAsync(
HandledPromise.applyMethod(undefined, 'toString', []),
{
instanceOf: TypeError,
message:
'Cannot deliver "toString" to target; typeof target is "undefined"',
},
'applyMethod undefined.toString()',
);
await t.throwsAsync(
HandledPromise.applyMethod(null, 'toString', []),
{
instanceOf: TypeError,
message: 'Cannot deliver "toString" to target; typeof target is "null"',
},
'applyMethod null.toString()',
);
t.is(
await HandledPromise.applyMethod({}, 'toString', []),
'[object Object]',
'applyMethod ({}).toString()',
);
await t.throwsAsync(
HandledPromise.applyMethod(true, 'notfound', []),
{
instanceOf: TypeError,
message: 'target has no method "notfound", has []',
},
'applyMethod true.notfound()',
);
await t.throwsAsync(
HandledPromise.applyMethod(false, 'notfound', []),
{
instanceOf: TypeError,
message: 'target has no method "notfound", has []',
},
'applyMethod false.notfound()',
);
await t.throwsAsync(
HandledPromise.applyMethod(undefined, 'notfound', []),
{
instanceOf: TypeError,
message:
'Cannot deliver "notfound" to target; typeof target is "undefined"',
},
'applyMethod undefined.notfound()',
);
await t.throwsAsync(
HandledPromise.applyMethod(null, 'notfound', []),
{
instanceOf: TypeError,
message: 'Cannot deliver "notfound" to target; typeof target is "null"',
},
'applyMethod null.notfound()',
);
await t.throwsAsync(
HandledPromise.applyMethod({ present() {}, other() {} }, 'notfound', []),
{
instanceOf: TypeError,
message: 'target has no method "notfound", has [other,present]',
},
'applyMethod ({}).notfound()',
);
});

0 comments on commit f6bd055

Please sign in to comment.