返回 DeepSeek-Reasonix
latex_test.go
根目录 / internal / cli / latex_test.go
1 package cli
2
3 import (
4 "strings"
5 "testing"
6
7 "github.com/charmbracelet/colorprofile"
8 )
9
10 func TestLatexToUnicode(t *testing.T) {
11 cases := []struct {
12 in, want string
13 }{
14 {`E = mc^2`, "E = mc²"},
15 {`x^{n+1}`, "xⁿ⁺¹"},
16 {`H_2O`, "H₂O"},
17 {`x_i^2`, "xᵢ²"},
18 {`\alpha + \beta = \gamma`, "α + β = γ"},
19 {`\sum_{i=1}^{n} i`, "∑ᵢ₌₁ⁿ i"},
20 {`\int_0^1 x\,dx`, "∫₀¹ x dx"},
21 {`\frac{1}{2}`, "1/2"},
22 {`\frac{x+1}{2}`, "(x+1)/2"},
23 {`\sqrt{2}`, "√2"},
24 {`\sqrt{x+y}`, "√(x+y)"},
25 {`\sqrt[3]{x}`, "∛x"},
26 {`a \leq b \neq c`, "a ≤ b ≠ c"},
27 {`\mathbb{R}^n`, "ℝⁿ"},
28 {`\text{if } x > 0`, "if x > 0"},
29 {`\vec{v}`, "v⃗"},
30 {`a^q`, "a^q"},
31 {`f(x) = x^{2y}`, "f(x) = x²ʸ"},
32 {`\boxed{|p\uparrow\rangle}`, "|p↑⟩"},
33 {`\boxed{x}`, "x"},
34 {`\lvert x \rvert`, "∣ x ∣"},
35 {`\Vert x \Vert`, "‖ x ‖"},
36 {`\left. \frac{df}{dx} \right|_{x=0}`, " (df)/(dx) |ₓ₌₀"},
37 }
38 for _, c := range cases {
39 if got := latexToUnicode(c.in); got != c.want {
40 t.Errorf("latexToUnicode(%q) = %q, want %q", c.in, got, c.want)
41 }
42 }
43 }
44
45 func TestNormalizeMath(t *testing.T) {
46 cases := []struct {
47 in, want string
48 }{
49 {`\(x+1\)`, "$x+1$"},
50 {`\[x+1\]`, "$$x+1$$"},
51 {"$$\nE = mc^2\n$$", "$$ E = mc^2 $$"},
52 {"`\\(literal\\)`", "`\\(literal\\)`"},
53 {"```\n\\[code\\]\n```", "```\n\\[code\\]\n```"},
54 }
55 for _, c := range cases {
56 if got := normalizeMath(c.in); got != c.want {
57 t.Errorf("normalizeMath(%q) = %q, want %q", c.in, got, c.want)
58 }
59 }
60 }
61
62 func TestRenderInlineMath(t *testing.T) {
63 activeColorProfile = colorprofile.NoTTY
64 r := newMarkdownRenderer(80)
65
66 out := r.Render(`The mass-energy relation is $E = mc^2$ exactly.`)
67 if !strings.Contains(out, "E = mc²") {
68 t.Errorf("inline math not rendered: %q", out)
69 }
70
71 out = r.Render(`It costs $5 and then $10 total.`)
72 if !strings.Contains(out, "$5 and then $10") {
73 t.Errorf("currency wrongly parsed as math: %q", out)
74 }
75
76 out = r.Render("$$\n\\int_0^1 x\\,dx = \\frac{1}{2}\n$$")
77 if !strings.Contains(out, "∫₀¹ x dx = 1/2") {
78 t.Errorf("display math not rendered: %q", out)
79 }
80
81 out = r.Render("Code stays literal: `$x^2$` here.")
82 if !strings.Contains(out, "$x^2$") {
83 t.Errorf("math inside code span was converted: %q", out)
84 }
85 }
86
86 lines GO