Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(at): throw error when using at to access undefined property #523

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions src/askvm/resources/core/__tests__/at.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { runUntyped } from '../../..';
import { parse } from '../../../../askcode';
import * as core from '..';
import { at } from '../at';

const values = {};

function ask(code: string) {
return runUntyped(
{
resources: { ...core, at },
values,
},
parse(code)
);
}

describe(`at`, function () {
it(`Should retrieve a defined property from an object.`, async function () {
await expect(
ask(
`ask(const('cocoObject',object('title','Coco')),call(get('at'),get('cocoObject'),'title'))`
)
).resolves.toStrictEqual(`Coco`);
});

it(`Should throw when trying to access an undefined property of an object.`, async function () {
await expect(
ask(
`ask(const('myobject',object('title','Coco')),call(get('at'),get('myobject'),'description'))`
)
).rejects.toThrow(
`Requested property key was not defined so it is not possible to get its value.`
);
});

it(`Should retrieve an element from a list.`, async function () {
await expect(
ask(
`ask(const('yummyList',list('Coco','Papaya','Mango')),call(get('at'),get('yummyList'),0))`
)
).resolves.toStrictEqual(`Coco`);
});

it(`Should throw when accessing a nonnegative index which is out of lists bounds.`, async function () {
await expect(
ask(
`ask(const('yummyList',list('coco','papaya','mango')),call(get('at'),get('yummyList'),3))`
)
).rejects.toThrow(
`Sorry, but index 3 is out of bounds for a list with 3 elements.`
);
});

it(`Should throw when accessing a nonnegative index which is out of lists bounds.`, async function () {
await expect(
ask(
`ask(const('yummyList',list('coco','papaya','mango')),call(get('at'),get('yummyList'),-1))`
)
).rejects.toThrow(
`Sorry, but index -1 is out of bounds for a list with 3 elements.`
);
});
});
17 changes: 17 additions & 0 deletions src/askvm/resources/core/at.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ import { any, resource, typed } from '../../lib';
export const at = resource({
type: any,
async resolver(listOrObject: any, key: any): Promise<any> {
if (
Array.isArray(listOrObject) &&
Number.isInteger(key) &&
(listOrObject.length <= key || key < 0)
)
throw Error(
`Sorry, but index ${key} is out of bounds for a list with ${listOrObject.length} elements.`
);

if (
listOrObject !== null &&
typeof listOrObject === 'object' &&
!(key in listOrObject)
)
throw Error(
'Requested property key was not defined so it is not possible to get its value.'
);
return typed(listOrObject[key]);
},
});