Skip to content

Commit 2161acc

Browse files
authored
Merge pull request #402 from emilefokkemanavara/chore/396-v8/main
re-apply changes from #396 onto v8
2 parents e244b18 + dd4f365 commit 2161acc

6 files changed

+34
-3
lines changed

src/condition.mjs

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ export default class Condition {
7070
if (this.factResult !== undefined) {
7171
props.factResult = this.factResult;
7272
}
73+
if (this.valueResult !== undefined) {
74+
props.valueResult = this.valueResult
75+
}
7376
if (this.result !== undefined) {
7477
props.result = this.result;
7578
}

src/rule.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ class Rule extends EventEmitter {
229229
.then((evaluationResult) => {
230230
const passes = evaluationResult.result;
231231
condition.factResult = evaluationResult.leftHandSideValue;
232+
condition.valueResult = evaluationResult.rightHandSideValue;
232233
condition.result = passes;
233234
return passes;
234235
});

test/acceptance/acceptance.test.mjs

+7
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ describe("Acceptance", () => {
2626
path: "$.values",
2727
value: 2,
2828
factResult: [2],
29+
valueResult: 2,
2930
result: true,
3031
},
3132
{
3233
fact: "low-priority",
3334
operator: "in",
3435
value: [2],
3536
factResult: 2,
37+
valueResult: [2],
3638
result: true,
3739
},
3840
],
@@ -172,13 +174,17 @@ describe("Acceptance", () => {
172174
path: "$.values",
173175
result: true,
174176
value: 2,
177+
valueResult: 2
175178
},
176179
{
177180
fact: "low-priority",
178181
factResult: 2,
179182
operator: "in",
180183
result: true,
181184
value: [2],
185+
valueResult: [
186+
2
187+
]
182188
},
183189
],
184190
operator: "all",
@@ -200,6 +206,7 @@ describe("Acceptance", () => {
200206
{
201207
fact: "high-priority",
202208
factResult: [2],
209+
valueResult: 2,
203210
operator: "containsDivisibleValuesOf",
204211
params: {
205212
factParam: 1,

test/engine-event.test.mjs

+3-3
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ describe("Engine: event", () => {
687687
await engine.run();
688688
const ruleResult = successSpy.mock.calls[0][2];
689689
const expected =
690-
'{"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}';
690+
'{"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}';
691691
expect(JSON.stringify(ruleResult)).toBe(expected);
692692
});
693693
});
@@ -696,7 +696,7 @@ describe("Engine: event", () => {
696696
beforeEach(() => setupWithConditionReference())
697697
it('serializes properties', async () => {
698698
const { results: [ruleResult] } = await engine.run()
699-
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}'
699+
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}'
700700
expect(JSON.stringify(ruleResult)).toEqual(expected)
701701
})
702702
})
@@ -707,7 +707,7 @@ describe("Engine: event", () => {
707707
const { results: [ruleResult] } = await engine.run()
708708
const { conditions: { any: [conditionReference] } } = ruleResult
709709
expect(conditionReference.result).toEqual(false)
710-
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}'
710+
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}'
711711
expect(JSON.stringify(ruleResult)).toEqual(expected)
712712
})
713713
})

test/engine-fact-comparison.test.mjs

+19
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,23 @@ describe("Engine: fact to fact comparison", () => {
118118
expect(eventSpy).not.toHaveBeenCalled();
119119
});
120120
});
121+
122+
describe('constant facts: checking valueResult and factResult', () => {
123+
const constantCondition = {
124+
all: [{
125+
fact: 'height',
126+
operator: 'lessThanInclusive',
127+
value: {
128+
fact: 'width'
129+
}
130+
}]
131+
}
132+
it('result has the correct valueResult and factResult properties', async () => {
133+
setup(constantCondition)
134+
const result = await engine.run({ height: 1, width: 2 })
135+
136+
expect(result.results[0].conditions.all[0].factResult).toEqual(1)
137+
expect(result.results[0].conditions.all[0].valueResult).toEqual(2)
138+
})
139+
})
121140
});

types/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ interface BooleanConditionResultProperties {
209209

210210
interface ConditionResultProperties extends BooleanConditionResultProperties {
211211
factResult?: unknown
212+
valueResult?: unknown
212213
}
213214

214215
interface ConditionProperties {

0 commit comments

Comments
 (0)