Skip to content

Commit

Permalink
Add tests for mixed number in KAS (#1507)
Browse files Browse the repository at this point in the history
## 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: #1507
  • Loading branch information
handeyeco authored Aug 22, 2024
1 parent 85c1a6e commit e19c58e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/gentle-dingos-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/kas": patch
---

Add some tests for mixed numbers in KAS.
49 changes: 45 additions & 4 deletions packages/kas/src/__tests__/evaluating.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ expect.extend({
vars: Variables = {},
functions?: ReadonlyArray<string>,
) {
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,
Expand Down Expand Up @@ -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);
});
});
7 changes: 6 additions & 1 deletion packages/kas/src/__tests__/parsing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ expect.extend({
if (actual !== expected) {
return {
pass: false,
message: () => `${input} parses as ${expected}`,
message: () =>
`input: ${input}\nexpected:${expected}\nactual: ${actual}`,
};
}

Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand Down

0 comments on commit e19c58e

Please sign in to comment.