-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor and enable And, Not, Or operators with tests
- Loading branch information
Showing
10 changed files
with
324 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const findOperator = require('./find-operator') | ||
|
||
const isJSONPath = p => (p && typeof p.valueOf() === 'string') && p.length !== 0 && p[0] === '$' | ||
|
||
module.exports = function executeOperator (choice, inputCache, values) { | ||
const operator = findOperator(choice) | ||
|
||
const inputValue = inputCache.get(choice.Variable, values) | ||
|
||
if (operator.isPath && isJSONPath(operator.value)) { | ||
operator.value = inputCache.get(operator.value, values) | ||
} | ||
|
||
return operator.operator( | ||
inputValue, | ||
operator.value, | ||
choice.Next, | ||
inputCache | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const operators = require('./operators') | ||
|
||
module.exports = function findOperator (choice) { | ||
let operator | ||
let value | ||
let isPath = false | ||
|
||
Object.entries(choice).forEach(([k, v]) => { | ||
if (operator === undefined) { | ||
if (k.endsWith('Path')) { | ||
k = k.split('Path')[0] | ||
isPath = true | ||
} | ||
|
||
if (operators[k]) { | ||
operator = operators[k] | ||
value = v | ||
} | ||
} | ||
}) | ||
|
||
return { | ||
operator, | ||
value, | ||
isPath | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
/* eslint-env mocha */ | ||
|
||
'use strict' | ||
|
||
const chai = require('chai') | ||
const expect = chai.expect | ||
|
||
// As inspired-by: http://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-choice-state.html#amazon-states-language-choice-state-rules | ||
|
||
const choiceProcessor = require('./../lib') | ||
|
||
const tests = { | ||
Not: [ | ||
{ | ||
choices: { | ||
Choices: [ | ||
{ | ||
Not: { | ||
Variable: '$.type', | ||
StringEquals: 'Private' | ||
}, | ||
Next: 'NextState' | ||
} | ||
], | ||
Default: 'DefaultState' | ||
}, | ||
input: { type: 'Public' }, | ||
expected: 'NextState' | ||
}, | ||
{ | ||
choices: { | ||
Choices: [ | ||
{ | ||
Not: { | ||
Variable: '$.type', | ||
StringEquals: 'Private' | ||
}, | ||
Next: 'NextState' | ||
} | ||
], | ||
Default: 'DefaultState' | ||
}, | ||
input: { type: 'Private' }, | ||
expected: 'DefaultState' | ||
} | ||
], | ||
And: [ | ||
{ | ||
choices: { | ||
Choices: [ | ||
{ | ||
And: [ | ||
{ | ||
Variable: '$.value', | ||
NumericGreaterThan: 20 | ||
}, | ||
{ | ||
Variable: '$.value', | ||
NumericLessThan: 25 | ||
} | ||
], | ||
Next: 'NextState' | ||
} | ||
], | ||
Default: 'DefaultState' | ||
}, | ||
input: { value: 21 }, | ||
expected: 'NextState' | ||
}, | ||
{ | ||
choices: { | ||
Choices: [ | ||
{ | ||
And: [ | ||
{ | ||
Variable: '$.value', | ||
NumericGreaterThan: 20 | ||
}, | ||
{ | ||
Variable: '$.value', | ||
NumericLessThan: 25 | ||
} | ||
], | ||
Next: 'NextState' | ||
} | ||
], | ||
Default: 'DefaultState' | ||
}, | ||
input: { value: 20 }, | ||
expected: 'DefaultState' | ||
}, | ||
{ | ||
choices: { | ||
Choices: [ | ||
{ | ||
And: [ | ||
{ | ||
Variable: '$.value', | ||
NumericGreaterThan: 20 | ||
}, | ||
{ | ||
Variable: '$.value', | ||
NumericLessThan: 25 | ||
} | ||
], | ||
Next: 'NextState' | ||
} | ||
], | ||
Default: 'DefaultState' | ||
}, | ||
input: { value: 28 }, | ||
expected: 'DefaultState' | ||
} | ||
], | ||
Or: [ | ||
{ | ||
choices: { | ||
Choices: [ | ||
{ | ||
Or: [ | ||
{ | ||
Variable: '$.value', | ||
NumericGreaterThan: 22 | ||
}, | ||
{ | ||
Variable: '$.value', | ||
NumericLessThan: 25 | ||
} | ||
], | ||
Next: 'NextState' | ||
} | ||
], | ||
Default: 'DefaultState' | ||
}, | ||
input: { value: 22 }, | ||
expected: 'NextState' | ||
}, | ||
{ | ||
choices: { | ||
Choices: [ | ||
{ | ||
Or: [ | ||
{ | ||
Variable: '$.value', | ||
NumericGreaterThan: 22 | ||
}, | ||
{ | ||
Variable: '$.value', | ||
NumericLessThan: 25 | ||
} | ||
], | ||
Next: 'NextState' | ||
} | ||
], | ||
Default: 'DefaultState' | ||
}, | ||
input: { value: 25 }, | ||
expected: 'NextState' | ||
} | ||
], | ||
Mixed: [ | ||
{ | ||
choices: { | ||
Choices: [ | ||
{ | ||
Not: { | ||
Variable: '$.type', | ||
StringEquals: 'Private' | ||
}, | ||
Next: 'Public' | ||
}, | ||
{ | ||
And: [ | ||
{ | ||
Variable: '$.value', | ||
NumericGreaterThanEquals: 20 | ||
}, | ||
{ | ||
Variable: '$.value', | ||
NumericLessThan: 30 | ||
} | ||
], | ||
Next: 'ValueInTwenties' | ||
} | ||
], | ||
Default: 'RecordEvent' | ||
}, | ||
input: { type: 'Private', value: 22 }, | ||
expected: 'ValueInTwenties' | ||
} | ||
] | ||
} | ||
|
||
describe('Boolean expression', () => { | ||
for (const [operator, t] of Object.entries(tests)) { | ||
describe(operator, () => { | ||
let i = 0 | ||
for (const { choices, input, expected } of t) { | ||
i++ | ||
it(`${i}`, () => { | ||
const calculateNextState = choiceProcessor(choices) | ||
const result = calculateNextState(input) | ||
expect(result).to.eql(expected) | ||
}) | ||
} | ||
}) | ||
} | ||
}) |
Oops, something went wrong.