From c25b6b6ea3a6f1100daba2653b263f8aed64a8c4 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 16 Mar 2022 18:19:34 +0200 Subject: [PATCH] fix(operators): Fix operators failing tests. --- .../build/buildRefs/evaluateBuildOperators.js | 1 - packages/operators/src/webParser.js | 5 +- .../src/operators/shared/array.test.js | 46 ++++++++++++++++--- .../src/operators/shared/function.test.js | 23 +++++++--- 4 files changed, 59 insertions(+), 16 deletions(-) diff --git a/packages/build/src/build/buildRefs/evaluateBuildOperators.js b/packages/build/src/build/buildRefs/evaluateBuildOperators.js index 1caa35662..1760b5cea 100644 --- a/packages/build/src/build/buildRefs/evaluateBuildOperators.js +++ b/packages/build/src/build/buildRefs/evaluateBuildOperators.js @@ -23,7 +23,6 @@ async function evaluateBuildOperators({ context, input, refDef }) { operators, }); - await operatorsParser.init(); const { output, errors } = operatorsParser.parse({ input, location: refDef.path, diff --git a/packages/operators/src/webParser.js b/packages/operators/src/webParser.js index 3595f757c..be06a341f 100644 --- a/packages/operators/src/webParser.js +++ b/packages/operators/src/webParser.js @@ -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 { diff --git a/packages/plugins/operators/operators-js/src/operators/shared/array.test.js b/packages/plugins/operators/operators-js/src/operators/shared/array.test.js index 7100f47ac..1b018e3f0 100644 --- a/packages/plugins/operators/operators-js/src/operators/shared/array.test.js +++ b/packages/plugins/operators/operators-js/src/operators/shared/array.test.js @@ -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({ diff --git a/packages/plugins/operators/operators-js/src/operators/shared/function.test.js b/packages/plugins/operators/operators-js/src/operators/shared/function.test.js index 8957ffe8c..2cbf26435 100644 --- a/packages/plugins/operators/operators-js/src/operators/shared/function.test.js +++ b/packages/plugins/operators/operators-js/src/operators/shared/function.test.js @@ -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.' );