Skip to content

Commit

Permalink
solveequations handles invalid equation (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
dqnykamp authored Oct 29, 2023
1 parent bf37648 commit f7a9273
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
17 changes: 16 additions & 1 deletion packages/doenetml-worker/src/components/SolveEquations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
32 changes: 32 additions & 0 deletions packages/test-cypress/cypress/e2e/tagSpecific/solveequations.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -1026,4 +1026,36 @@ describe("SolveEquations Tag Tests", function () {
);
});
});

it("handle bad equation", () => {
cy.window().then(async (win) => {
win.postMessage(
{
doenetML: `
<solveEquations name="solve">x_(2t)=1</solveEquations>
<p>Number of solutions: <copy source="solve.numSolutions" assignNames="num" /></p>
`,
},
"*",
);
});

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);
});
});
});

0 comments on commit f7a9273

Please sign in to comment.