Skip to content

Commit 5b13a53

Browse files
authored
Merge pull request #332 from hoijnet/issue/317-no-evaluate-in-fluent
fix(woql): Add evaluate() method for fluent/chained query style
2 parents ed94e33 + fb8fe64 commit 5b13a53

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lib/query/woqlQuery.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,19 @@ WOQLQuery.prototype.eval = function (arithExp, resultVarName) {
920920
return this;
921921
};
922922

923+
/**
924+
* Evaluates the passed arithmetic expression and generates or matches the result value.
925+
* Alias for eval() to support both naming conventions in fluent/chained style.
926+
* @param {object|WOQLQuery|string} arithExp - A WOQL query containing a valid arithmetic expression
927+
* @param {string|number|Var} resultVarName - Either a variable to store the result, or a numeric
928+
* literal to test against the evaluated expression
929+
* @returns {WOQLQuery}
930+
*/
931+
932+
WOQLQuery.prototype.evaluate = function (arithExp, resultVarName) {
933+
return this.eval(arithExp, resultVarName);
934+
};
935+
923936
/**
924937
* Adds the numbers together
925938
* @param {...(string|number|Var)} args - a variable or numeric containing the values to add

test/woql.spec.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,34 @@ describe('woql queries', () => {
190190
expect(woqlObject.json()).to.eql(woqlMathJson.evalJson);
191191
});
192192

193+
it('check the evaluate method (functional style)', () => {
194+
const woqlObject = WOQL.evaluate('1+2', 'b');
195+
expect(woqlObject.json()).to.eql(woqlMathJson.evalJson);
196+
});
197+
198+
it('check the evaluate method (fluent style)', () => {
199+
// Test the reported bug: WOQL.limit(100).evaluate(...)
200+
const woqlObject = WOQL.limit(100).evaluate(WOQL.times(2, 3), 'v:result');
201+
const json = woqlObject.json();
202+
203+
// Should have Limit with nested query
204+
expect(json).to.have.property('@type', 'Limit');
205+
expect(json).to.have.property('limit', 100);
206+
expect(json).to.have.property('query');
207+
208+
// Nested query should be Eval
209+
expect(json.query).to.have.property('@type', 'Eval');
210+
expect(json.query).to.have.property('expression');
211+
expect(json.query.expression).to.have.property('@type', 'Times');
212+
});
213+
214+
it('check evaluate and eval produce identical results', () => {
215+
const withEval = WOQL.limit(10).eval(WOQL.plus(5, 3), 'x');
216+
const withEvaluate = WOQL.limit(10).evaluate(WOQL.plus(5, 3), 'x');
217+
218+
expect(withEval.json()).to.deep.eql(withEvaluate.json());
219+
});
220+
193221
it('check the minus method', () => {
194222
const woqlObject = WOQL.minus('2', '1');
195223
expect(woqlObject.json()).to.eql(woqlMathJson.minusJson);
@@ -490,7 +518,6 @@ describe('woql queries', () => {
490518
WOQL.eval(WOQL.times(3,4), v.a),
491519
WOQL.eval(WOQL.times(v.a, 3),v.res)
492520
).json();
493-
console.log(JSON.stringify(wq));
494521
expect(wq).to.deep.eql(
495522
{"@type":"And","and":[{"@type":"Eval","expression":{"@type":"Times","left":{"@type":"ArithmeticValue","data":{"@type":"xsd:decimal","@value":3}},"right":{"@type":"ArithmeticValue","data":{"@type":"xsd:decimal","@value":4}}},"result":{"@type":"ArithmeticValue","variable":"a"}},{"@type":"Eval","expression":{"@type":"Times","left":{"@type":"ArithmeticValue","variable":"a"},"right":{"@type":"ArithmeticValue","data":{"@type":"xsd:decimal","@value":3}}},"result":{"@type":"ArithmeticValue","variable":"res"}}]})
496523
});

0 commit comments

Comments
 (0)