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