import random from isotope_typst.ast import Frac, Integral, Neg, Num, Pow, Var from isotope_typst.generate import TreeGen, split_of from isotope_typst.render import to_english, to_typst from isotope_typst.validate import compiles, compiles_many def guide_tree(): return Integral(Pow(Var("e"), Neg(Pow(Var("x"), Num(2)))), "x", Num(0), Var("infinity")) def test_guide_example(): assert to_typst(guide_tree()) == "integral_0^infinity e^(-x^2) dif x" def test_frac_and_parens(): assert to_typst(Frac(Num(1), Var("x"))) == "frac(1, x)" assert to_typst(Pow(Frac(Num(1), Num(2)), Num(2))) == "(frac(1, 2))^2" def test_english_is_random_but_typst_is_not(): rng = random.Random(0) phrasings = {to_english(guide_tree(), rng) for _ in range(50)} assert len(phrasings) > 10 def test_generated_typst_compiles(): gen = TreeGen(random.Random(1)) maths = [to_typst(gen.tree()) for _ in range(300)] assert all(compiles_many(maths)) def test_basic_generator_only_uses_basic_nodes(): from isotope_typst.ast import walk gen = TreeGen(random.Random(2), basic=True) allowed = {"Num", "Var", "Neg", "Pow", "Frac", "Integral"} for _ in range(300): assert {type(n).__name__ for n in walk(gen.tree())} <= allowed def test_compile_check_rejects_broken(): assert compiles("x^2") assert not compiles("frac(1, ") assert compiles_many(["x", "sqrt(", "y"]) == [True, False, True] def test_split_is_by_structure(): assert split_of("x^2") == split_of("x^2")