Skip to content

Commit

Permalink
Merge pull request #396 from thorin8k/master
Browse files Browse the repository at this point in the history
Add RightSideComparisonResult as factResult
  • Loading branch information
chris-pardy authored Nov 18, 2024
2 parents 91c7f38 + 8e8b3f1 commit 626604e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/condition.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ export default class Condition {
if (this.factResult !== undefined) {
props.factResult = this.factResult
}
if (this.valueResult !== undefined) {
props.valueResult = this.valueResult
}
if (this.result !== undefined) {
props.result = this.result
}
Expand Down
1 change: 1 addition & 0 deletions src/rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ class Rule extends EventEmitter {
.then((evaluationResult) => {
const passes = evaluationResult.result
condition.factResult = evaluationResult.leftHandSideValue
condition.valueResult = evaluationResult.rightHandSideValue
condition.result = passes
return passes
})
Expand Down
9 changes: 8 additions & 1 deletion test/acceptance/acceptance.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ describe('Acceptance', () => {
path: '$.values',
value: 2,
factResult: [2],
valueResult: 2,
result: true
},
{
fact: 'low-priority',
operator: 'in',
value: [2],
factResult: 2,
valueResult: [2],
result: true
}
],
Expand Down Expand Up @@ -169,7 +171,8 @@ describe('Acceptance', () => {
},
path: '$.values',
result: true,
value: 2
value: 2,
valueResult: 2
},
{
fact: 'low-priority',
Expand All @@ -178,6 +181,9 @@ describe('Acceptance', () => {
result: true,
value: [
2
],
valueResult: [
2
]
}
],
Expand All @@ -202,6 +208,7 @@ describe('Acceptance', () => {
factResult: [
2
],
valueResult: 2,
operator: 'containsDivisibleValuesOf',
params: {
factParam: 1
Expand Down
6 changes: 3 additions & 3 deletions test/engine-event.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ describe('Engine: event', () => {
await engine.run()
const ruleResult = successSpy.getCall(0).args[2]
const expected =
'{"conditions":{"priority":1,"any":[{"name":"over 21","operator":"greaterThanInclusive","value":21,"fact":"age","factResult":21,"result":true},{"operator":"equal","value":true,"fact":"qualified","factResult":false,"result":false}]},"event":{"type":"setDrinkingFlag","params":{"canOrderDrinks":true}},"priority":100,"result":true}'
'{"conditions":{"priority":1,"any":[{"name":"over 21","operator":"greaterThanInclusive","value":21,"fact":"age","factResult":21,"valueResult":21,"result":true},{"operator":"equal","value":true,"fact":"qualified","factResult":false,"valueResult":true,"result":false}]},"event":{"type":"setDrinkingFlag","params":{"canOrderDrinks":true}},"priority":100,"result":true}'
expect(JSON.stringify(ruleResult)).to.equal(expected)
})
})
Expand All @@ -651,7 +651,7 @@ describe('Engine: event', () => {
beforeEach(() => setupWithConditionReference())
it('serializes properties', async () => {
const { results: [ruleResult] } = await engine.run()
const expected = '{"conditions":{"priority":1,"any":[{"priority":1,"all":[{"name":"over 21","operator":"greaterThanInclusive","value":21,"fact":"age","factResult":21,"result":true}]}]},"event":{"type":"awesome"},"priority":100,"result":true}'
const expected = '{"conditions":{"priority":1,"any":[{"priority":1,"all":[{"name":"over 21","operator":"greaterThanInclusive","value":21,"fact":"age","factResult":21,"valueResult":21,"result":true}]}]},"event":{"type":"awesome"},"priority":100,"result":true}'
expect(JSON.stringify(ruleResult)).to.equal(expected)
})
})
Expand All @@ -662,7 +662,7 @@ describe('Engine: event', () => {
const { results: [ruleResult] } = await engine.run()
const { conditions: { any: [conditionReference] } } = ruleResult
expect(conditionReference.result).to.equal(false)
const expected = '{"conditions":{"priority":1,"any":[{"name":"nameOfTheUndefinedConditionReference","condition":"conditionThatIsNotDefined"},{"name":"over 21","operator":"greaterThanInclusive","value":21,"fact":"age","factResult":21,"result":true}]},"event":{"type":"awesome"},"priority":100,"result":true}'
const expected = '{"conditions":{"priority":1,"any":[{"name":"nameOfTheUndefinedConditionReference","condition":"conditionThatIsNotDefined"},{"name":"over 21","operator":"greaterThanInclusive","value":21,"fact":"age","factResult":21,"valueResult":21,"result":true}]},"event":{"type":"awesome"},"priority":100,"result":true}'
expect(JSON.stringify(ruleResult)).to.equal(expected)
})
})
Expand Down
19 changes: 19 additions & 0 deletions test/engine-fact-comparison.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,23 @@ describe('Engine: fact to fact comparison', () => {
expect(eventSpy.callCount).to.equal(0)
})
})

context('constant facts: checking valueResult and factResult', () => {
const constantCondition = {
all: [{
fact: 'height',
operator: 'lessThanInclusive',
value: {
fact: 'width'
}
}]
}
it('result has the correct valueResult and factResult properties', async () => {
setup(constantCondition)
const result = await engine.run({ height: 1, width: 2 })

expect(result.results[0].conditions.all[0].factResult).to.equal(1)
expect(result.results[0].conditions.all[0].valueResult).to.equal(2)
})
})
})
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ interface BooleanConditionResultProperties {

interface ConditionResultProperties extends BooleanConditionResultProperties {
factResult?: unknown
valueResult?: unknown
}

interface ConditionProperties {
Expand Down

0 comments on commit 626604e

Please sign in to comment.