From f7a927348d2c8f69b38406db5535b2d74f90ed6a Mon Sep 17 00:00:00 2001 From: Duane Nykamp Date: Sat, 28 Oct 2023 22:25:59 -0500 Subject: [PATCH] solveequations handles invalid equation (#53) --- .../src/components/SolveEquations.js | 17 +++++++++- .../e2e/tagSpecific/solveequations.cy.js | 32 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/packages/doenetml-worker/src/components/SolveEquations.js b/packages/doenetml-worker/src/components/SolveEquations.js index 20055188c..de7e3c9dc 100644 --- a/packages/doenetml-worker/src/components/SolveEquations.js +++ b/packages/doenetml-worker/src/components/SolveEquations.js @@ -266,7 +266,22 @@ export default class SolveEquations extends InlineComponent { ["-", expression.tree[2]], ]) .subscripts_to_strings(); - let f_base = formula.f(); + + let f_base; + try { + f_base = formula.f(); + } catch (e) { + let message = + "Cannot solve equation as could not evaluate equation: " + + expression.toString(); + let warnings = [{ message, level: 1 }]; + + return { + setValue: { allSolutions: [] }, + sendWarnings: warnings, + }; + } + let f = (x) => f_base({ [varName]: x }); let f_symbolic = (x) => diff --git a/packages/test-cypress/cypress/e2e/tagSpecific/solveequations.cy.js b/packages/test-cypress/cypress/e2e/tagSpecific/solveequations.cy.js index 2f00a2ebb..78bd0ae49 100644 --- a/packages/test-cypress/cypress/e2e/tagSpecific/solveequations.cy.js +++ b/packages/test-cypress/cypress/e2e/tagSpecific/solveequations.cy.js @@ -1026,4 +1026,36 @@ describe("SolveEquations Tag Tests", function () { ); }); }); + + it("handle bad equation", () => { + cy.window().then(async (win) => { + win.postMessage( + { + doenetML: ` + x_(2t)=1 +

Number of solutions:

+ `, + }, + "*", + ); + }); + + cy.get(cesc("#\\/num")).should("have.text", 0); + + cy.window().then(async (win) => { + let errorWarnings = await win.returnErrorWarnings1(); + + expect(errorWarnings.errors.length).eq(0); + expect(errorWarnings.warnings.length).eq(1); + + expect(errorWarnings.warnings[0].message).contain( + `Cannot solve equation`, + ); + expect(errorWarnings.warnings[0].level).eq(1); + expect(errorWarnings.warnings[0].doenetMLrange.lineBegin).eq(2); + expect(errorWarnings.warnings[0].doenetMLrange.charBegin).eq(3); + expect(errorWarnings.warnings[0].doenetMLrange.lineEnd).eq(2); + expect(errorWarnings.warnings[0].doenetMLrange.charEnd).eq(56); + }); + }); });