Skip to content

Commit

Permalink
Do not allow to set Infinite as an expression value fix #6981 (#6982)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov committed Sep 20, 2023
1 parent 2df63ea commit 9994a67
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/question_expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ export class QuestionExpressionModel extends Question {
this.setPropertyValue("precision", val);
}
private roundValue(val: any): any {
if(val === Infinity) return undefined;
if(this.precision < 0) return val;
if(!Helpers.isNumber(val)) return val;
return parseFloat(val.toFixed(this.precision));
Expand Down
33 changes: 33 additions & 0 deletions tests/question_expressiontests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,36 @@ QUnit.test("survey.onValueChanging, bug#6548", function (assert) {
assert.equal(q.value, 35, "correct value on survey.onValueChanging event, #2");
assert.deepEqual(survey.data, { weightKg: 100, heightCm: 169, bmi: 35 }, "survey.data is correct, #2");
});
QUnit.test("Handle Infinity", function (assert) {
const survey = new SurveyModel({
elements: [
{
"name": "q1",
"type": "text"
},
{
"name": "q2",
"type": "text",
"inputType": "number"
},
{
"name": "q3",
"type": "expression",
"expression": "({q1}^-1)*(2^{q2})",
}
]
});
let counter = 0;
survey.onValueChanged.add((sender, options) => {
if(options.name === "q3") counter ++;
});
const q3 = survey.getQuestionByName("q3");
survey.setValue("q2", 1);
assert.equal(counter, 0, "is not updated");
survey.setValue("q2", 3);
assert.equal(counter, 0, "is not updated yet");
assert.equal(q3.isEmpty(), true, "expression question is undefined");
survey.setValue("q1", 2);
assert.equal(counter, 1, "updated one time");
assert.equal(q3.value, 4, "calculated correctly");
});

0 comments on commit 9994a67

Please sign in to comment.