From e19c58eb9f0ef84c32dfdb40a4382cfa4c82392d Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 22 Aug 2024 09:51:23 -0500 Subject: [PATCH] Add tests for mixed number in KAS (#1507) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary: This is a failed attempt at [LEMS-2198](https://khanacademy.atlassian.net/browse/LEMS-2198), but I want to keep the tests to help the next person who tries. [LEMS-2198]: https://khanacademy.atlassian.net/browse/LEMS-2198?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ Author: handeyeco Reviewers: SonicScrewdriver, mark-fitzgerald Required Reviewers: Approved By: SonicScrewdriver, mark-fitzgerald Checks: ✅ codecov/project, ✅ codecov/patch, ✅ Upload Coverage (ubuntu-latest, 20.x), ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Jest Coverage (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ gerald, ✅ Upload Coverage (ubuntu-latest, 20.x), ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ Jest Coverage (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Publish Storybook to Chromatic (ubuntu-latest, 20.x), ✅ gerald Pull Request URL: https://github.com/Khan/perseus/pull/1507 --- .changeset/gentle-dingos-explain.md | 5 ++ packages/kas/src/__tests__/evaluating.test.ts | 49 +++++++++++++++++-- packages/kas/src/__tests__/parsing.test.ts | 7 ++- 3 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 .changeset/gentle-dingos-explain.md diff --git a/.changeset/gentle-dingos-explain.md b/.changeset/gentle-dingos-explain.md new file mode 100644 index 0000000000..0f58873351 --- /dev/null +++ b/.changeset/gentle-dingos-explain.md @@ -0,0 +1,5 @@ +--- +"@khanacademy/kas": patch +--- + +Add some tests for mixed numbers in KAS. diff --git a/packages/kas/src/__tests__/evaluating.test.ts b/packages/kas/src/__tests__/evaluating.test.ts index 099ee575ce..207ada0d60 100644 --- a/packages/kas/src/__tests__/evaluating.test.ts +++ b/packages/kas/src/__tests__/evaluating.test.ts @@ -13,11 +13,16 @@ expect.extend({ vars: Variables = {}, functions?: ReadonlyArray, ) { - const actual = KAS.parse(input, {functions: functions}).expr.eval( - vars, - {functions: functions}, - ); + const parsed = KAS.parse(input, {functions: functions}); + if (parsed.false || parsed.error) { + return { + pass: false, + message: () => + `unable to parse: ${input} (error: ${parsed.error})`, + }; + } + const actual = parsed.expr.eval(vars, {functions: functions}); if (actual !== expected) { return { pass: false, @@ -96,4 +101,40 @@ describe("evaluating", () => { expect("f(x-1)-f(x)").toEvaluateAs(-7, {f: "x^3", x: 2}, ["f"]); expect("g(1)").toEvaluateAs(-1, {f: "x", g: "-f(x)"}, ["f", "g"]); }); + + // TODO (LEMS-2198): these are tests from a failed attempt + // to support mixed numbers correctly. Keeping so we have a record + // of what's wrong and what's expected. + test("fraction expressions", () => { + // wrong + expect("2\\frac{1}{2} + 1").toEvaluateAs(2); + // correct + // expect("2\\frac{1}{2} + 1").toEvaluateAs(3.5); + + // wrong + expect("(2\\frac{1}{2}) + 1").toEvaluateAs(2); + // correct + // expect("(2\\frac{1}{2}) + 1").toEvaluateAs(3.5); + + // wrong + expect("-2\\frac{1}{2} + 1").toEvaluateAs(0); + // correct + // expect("-2\\frac{1}{2} + 1").toEvaluateAs(-1.5); + + // wrong + expect("(-2\\frac{1}{2}) + 1").toEvaluateAs(0); + // correct + // expect("(-2\\frac{1}{2}) + 1").toEvaluateAs(-1.5); + + // should continue to pass after LEMS-2198 is done + expect("2-\\frac{1}{2} + 1").toEvaluateAs(2.5); + expect("(2-\\frac{1}{2}) + 1").toEvaluateAs(2.5); + expect("(2)\\frac{1}{2} + 1").toEvaluateAs(2); + expect("2(\\frac{1}{2}) + 1").toEvaluateAs(2); + expect("\\frac{1}{2}2 + 1").toEvaluateAs(2); + expect("2 + \\frac{1}{2} + 1").toEvaluateAs(3.5); + expect("2 * \\frac{1}{2}").toEvaluateAs(1); + expect("2 2").toEvaluateAs(4); + expect("2\\pi").toEvaluateAs(6.283185307179586); + }); }); diff --git a/packages/kas/src/__tests__/parsing.test.ts b/packages/kas/src/__tests__/parsing.test.ts index b59e1f4c36..6753d6e885 100644 --- a/packages/kas/src/__tests__/parsing.test.ts +++ b/packages/kas/src/__tests__/parsing.test.ts @@ -13,7 +13,8 @@ expect.extend({ if (actual !== expected) { return { pass: false, - message: () => `${input} parses as ${expected}`, + message: () => + `input: ${input}\nexpected:${expected}\nactual: ${actual}`, }; } @@ -113,6 +114,8 @@ describe("parsing", () => { expect("\\frac{42}{1}").toParseAs("42/1"); expect("\\frac{0}{42}").toParseAs("0/42"); + // TODO (LEMS-2198): this should actually be: + // expect("2\\frac{1}{2}").toParseAs("2+1/2"); expect("2\\frac{1}{2}").toParseAs("2*1/2"); expect("\\frac{1}{2}\\frac{1}{2}").toParseAs("1/2*1/2"); expect("-\\frac{1}{2}").toParseAs("-1/2"); @@ -128,6 +131,8 @@ describe("parsing", () => { expect("\\dfrac{42}{1}").toParseAs("42/1"); expect("\\dfrac{0}{42}").toParseAs("0/42"); + // TODO (LEMS-2198): this should actually be: + // expect("2\\dfrac{1}{2}").toParseAs("2+1/2"); expect("2\\dfrac{1}{2}").toParseAs("2*1/2"); expect("\\dfrac{1}{2}\\dfrac{1}{2}").toParseAs("1/2*1/2"); expect("-\\dfrac{1}{2}").toParseAs("-1/2");