import { describe, expect, test } from "vitest"; import { AddNode, DecimalNode, DivideNode, EquationNode, ExponentNode, FunctionCallNode, MultiplyNode, RadicalNode, toText, VariableNode, } from "."; test("Convert x to text", () => { expect(toText(VariableNode.from("x"))).toStrictEqual("x"); }); test("Convert 3 to text", () => { expect(toText(DecimalNode.from("3"))).toStrictEqual("3"); }); test("Convert x+2.5 to text", () => { expect( toText(AddNode.from([VariableNode.from("x"), DecimalNode.from("2.5")])), ).toStrictEqual("x plus 2.5"); }); test("Convert a^2 + b^2 = c^2 to text", () => { expect( toText( EquationNode.from([ AddNode.from([ ExponentNode.from(VariableNode.from("a"), DecimalNode.from("2")), ExponentNode.from(VariableNode.from("b"), DecimalNode.from("2")), ]), ExponentNode.from(VariableNode.from("c"), DecimalNode.from("2")), ]), ), ).toStrictEqual("a squared plus b squared equals c squared"); // TODO: Find a better output that pronounces 'a' as "aye" rather than "uh" }); test("Convert sqrt(x+1) to text", () => { expect( toText( RadicalNode.from( AddNode.from([VariableNode.from("x"), DecimalNode.from("1")]), ), ), ).toStrictEqual("square root of x plus 1"); }); test("Convert sqrt(x)+1 to text", () => { expect( toText( AddNode.from([ RadicalNode.from(VariableNode.from("x")), DecimalNode.from("1"), ]), ), ).toStrictEqual("square root of x, plus 1"); }); test("Convert f(x+1)+1 to text", () => { expect( toText( AddNode.from([ FunctionCallNode.from("f", [ AddNode.from([VariableNode.from("x"), DecimalNode.from("1")]), ]), DecimalNode.from("1"), ]), ), ).toStrictEqual("f of x plus 1, plus 1"); }); test("Convert f(a)*b to text", () => { expect( toText( MultiplyNode.from([ FunctionCallNode.from("f", [VariableNode.from("a")]), VariableNode.from("b"), ]), ), ).toStrictEqual("f of a, times b"); }); test("Convert (a+b)*c to text", () => { expect( toText( MultiplyNode.from([ AddNode.from([VariableNode.from("a"), VariableNode.from("b")]), VariableNode.from("c"), ]), ), ).toStrictEqual("a plus b, times c"); }); describe("Convert fractions to text", () => { const fracText = (num: string, den: string) => { return toText( DivideNode.from(DecimalNode.from(num), DecimalNode.from(den)), ); }; test("0/0", () => void expect(fracText("0", "0")).toBe("0 over 0")); test("0/1", () => void expect(fracText("0", "1")).toBe("0 over 1")); test("1/1", () => void expect(fracText("1", "1")).toBe("1 over 1")); test("0/2", () => void expect(fracText("0", "2")).toBe("0 halves")); test("1/2", () => void expect(fracText("1", "2")).toBe("1 half")); test("2/2", () => void expect(fracText("2", "2")).toBe("2 halves")); test("0/3", () => void expect(fracText("0", "3")).toBe("0 thirds")); test("1/3", () => void expect(fracText("1", "3")).toBe("1 third")); test("2/3", () => void expect(fracText("2", "3")).toBe("2 thirds")); test("3/3", () => void expect(fracText("3", "3")).toBe("3 thirds")); test("4/3", () => void expect(fracText("4", "3")).toBe("4 thirds")); test("1/4", () => void expect(fracText("1", "4")).toBe("1 fourth")); test("2/4", () => void expect(fracText("2", "4")).toBe("2 fourths")); test("1/5", () => void expect(fracText("1", "5")).toBe("1 fifth")); test("2/5", () => void expect(fracText("2", "5")).toBe("2 fifths")); test("1/6", () => void expect(fracText("1", "6")).toBe("1 sixth")); test("2/6", () => void expect(fracText("2", "6")).toBe("2 sixths")); test("1/7", () => void expect(fracText("1", "7")).toBe("1 seventh")); test("2/7", () => void expect(fracText("2", "7")).toBe("2 sevenths")); test("1/8", () => void expect(fracText("1", "8")).toBe("1 eighth")); test("2/8", () => void expect(fracText("2", "8")).toBe("2 eighths")); test("1/9", () => void expect(fracText("1", "9")).toBe("1 ninth")); test("2/9", () => void expect(fracText("2", "9")).toBe("2 ninths")); test("1/10", () => void expect(fracText("1", "10")).toBe("1 tenth")); test("2/10", () => void expect(fracText("2", "10")).toBe("2 tenths")); test("1/11", () => void expect(fracText("1", "11")).toBe("1 eleventh")); test("2/11", () => void expect(fracText("2", "11")).toBe("2 elevenths")); test("1/12", () => void expect(fracText("1", "12")).toBe("1 twelfth")); test("2/12", () => void expect(fracText("2", "12")).toBe("2 twelfths")); });