fix(operators): Fix operators failing tests.

This commit is contained in:
Sam 2022-03-16 18:19:34 +02:00
parent 466e61f7c6
commit c25b6b6ea3
No known key found for this signature in database
GPG Key ID: D004126FCD1A6DF0
4 changed files with 59 additions and 16 deletions

View File

@ -23,7 +23,6 @@ async function evaluateBuildOperators({ context, input, refDef }) {
operators,
});
await operatorsParser.init();
const { output, errors } = operatorsParser.parse({
input,
location: refDef.path,

View File

@ -58,11 +58,10 @@ class WebParser {
const reviver = (_, value) => {
if (!type.isObject(value) || Object.keys(value).length !== 1) return value;
let key = Object.keys(value)[0];
const key = Object.keys(value)[0];
if (!key.startsWith(operatorPrefix)) return value;
key = `_${key.substring(operatorPrefix.length)}`;
const [op, methodName] = key.split('.');
const [op, methodName] = `_${key.substring(operatorPrefix.length)}`.split('.');
if (type.isUndefined(operators[op])) return value;
try {

View File

@ -32,6 +32,8 @@ const operators = {
const location = 'location';
const operatorPrefix = '_';
const parser = new NodeParser({ operators, payload: {}, secrets: {}, user: {} });
beforeAll(async () => {
await parser.init();
@ -154,7 +156,12 @@ describe('_array.copyWithin', () => {
describe('_array.every', () => {
const methodName = 'every';
const callback = _function({ location, params: { __gt: [{ __args: '0' }, 3] }, parser });
const callback = _function({
location,
operatorPrefix,
params: { __gt: [{ __args: '0' }, 3] },
parser,
});
test('valid', () => {
expect(
_array({
@ -275,7 +282,12 @@ describe('_array.fill', () => {
describe('_array.filter', () => {
const methodName = 'filter';
const callback = _function({ location, params: { __gt: [{ __args: '0' }, 3] }, parser });
const callback = _function({
location,
operatorPrefix,
params: { __gt: [{ __args: '0' }, 3] },
parser,
});
test('valid', () => {
expect(
_array({
@ -330,7 +342,12 @@ describe('_array.filter', () => {
describe('_array.find', () => {
const methodName = 'find';
const callback = _function({ location, params: { __gt: [{ __args: '0' }, 3] }, parser });
const callback = _function({
location,
operatorPrefix,
params: { __gt: [{ __args: '0' }, 3] },
parser,
});
test('valid', () => {
expect(
_array({
@ -385,7 +402,12 @@ describe('_array.find', () => {
describe('_array.findIndex', () => {
const methodName = 'findIndex';
const callback = _function({ location, params: { __gt: [{ __args: '0' }, 3] }, parser });
const callback = _function({
location,
operatorPrefix,
params: { __gt: [{ __args: '0' }, 3] },
parser,
});
test('valid', () => {
expect(
_array({
@ -706,7 +728,12 @@ describe('_array.lastIndexOf', () => {
describe('_array.map', () => {
const methodName = 'map';
const callback = _function({ location, params: { __sum: [{ __args: '0' }, 1] }, parser });
const callback = _function({
location,
operatorPrefix,
params: { __sum: [{ __args: '0' }, 1] },
parser,
});
test('valid', () => {
expect(
_array({
@ -763,6 +790,7 @@ describe('_array.reduce', () => {
const methodName = 'reduce';
const callback = _function({
location,
operatorPrefix,
params: { __sum: [{ __args: '0' }, { __args: '1' }] },
parser,
});
@ -833,6 +861,7 @@ describe('_array.reduceRight', () => {
const methodName = 'reduceRight';
const callback = _function({
location,
operatorPrefix,
params: { __sum: [{ __args: '0' }, { __args: '1' }] },
parser,
});
@ -1047,7 +1076,12 @@ describe('_array.splice', () => {
describe('_array.some', () => {
const methodName = 'some';
const callback = _function({ location, params: { __gt: [{ __args: '0' }, 3] }, parser });
const callback = _function({
location,
operatorPrefix,
params: { __gt: [{ __args: '0' }, 3] },
parser,
});
test('valid', () => {
expect(
_array({

View File

@ -57,11 +57,22 @@ const context = {
console.error = () => {};
// TODO: Test cases with different operatorPrefix
test('NodeParser, _function that gets from payload', async () => {
const parser = new NodeParser({ operators, payload, secrets: {}, user: {} });
await parser.init();
const params = { __payload: 'string' };
const fn = _function({ location, params, parser });
const fn = _function({ location, params, parser, operatorPrefix: '_' });
expect(fn).toBeInstanceOf(Function);
expect(fn()).toEqual('Some String');
});
test('NodeParser, nested function call', async () => {
const parser = new NodeParser({ operators, payload, secrets: {}, user: {} });
await parser.init();
const params = { ___payload: 'string' };
const fn = _function({ location, params, parser, operatorPrefix: '__' });
expect(fn).toBeInstanceOf(Function);
expect(fn()).toEqual('Some String');
});
@ -70,7 +81,7 @@ test('NodeParser, _function gives args as an array', async () => {
const parser = new NodeParser({ operators, payload, secrets: {}, user: {} });
await parser.init();
const params = { __args: true };
const fn = _function({ location, params, parser });
const fn = _function({ location, params, parser, operatorPrefix: '_' });
expect(fn('a')).toEqual(['a']);
expect(fn('a', { b: true })).toEqual(['a', { b: true }]);
});
@ -79,7 +90,7 @@ test('NodeParser, _function throws on parser errors', async () => {
const parser = new NodeParser({ operators, payload, secrets: {}, user: {} });
await parser.init();
const params = { __payload: [] };
const fn = _function({ location, params, parser });
const fn = _function({ location, params, parser, operatorPrefix: '_' });
expect(fn).toThrow(
'Error: Operator Error: _payload params must be of type string, integer, boolean or object. Received: [] at location.'
);
@ -89,7 +100,7 @@ test('WebParser, _function that gets from state', async () => {
const parser = new WebParser({ context, operators });
await parser.init();
const params = { __state: 'string' };
const fn = _function({ location, params, parser });
const fn = _function({ location, params, parser, operatorPrefix: '_' });
expect(fn).toBeInstanceOf(Function);
expect(fn()).toEqual('Some String');
expect(fn()).toEqual('Some String');
@ -99,7 +110,7 @@ test('WebParser, _function gives args as an array', async () => {
const parser = new WebParser({ context, operators });
await parser.init();
const params = { __args: true };
const fn = _function({ location, params, parser });
const fn = _function({ location, params, parser, operatorPrefix: '_' });
expect(fn('a')).toEqual(['a']);
expect(fn('a', { b: true })).toEqual(['a', { b: true }]);
});
@ -108,7 +119,7 @@ test('WebParser, _function throws on parser errors', async () => {
const parser = new WebParser({ context, operators });
await parser.init();
const params = { __state: [] };
const fn = _function({ location, params, parser });
const fn = _function({ location, params, parser, operatorPrefix: '_' });
expect(fn).toThrow(
'Error: Operator Error: _state params must be of type string, integer, boolean or object. Received: [] at location.'
);