okami.mom/albedo

git clone https://git.nkp.pet/okami.mom/albedo

nekomimi.pettui: one delimiter table for math, bytes.Cut over hand scans, and the fork's IndentWriter for list itemse4c3bd6

main
1.5 KiB48 linesraw
1// Inline math has to leave the dollar signs of prices and shell variables
2// alone, and display math has to stay inside the list item it belongs to.
3// Replies in e2e are scripted and never look at layout, so only these pin it.
4package tui
5
6import (
7	"regexp"
8	"testing"
9
10	"github.com/charmbracelet/x/ansi"
11)
12
13func TestScanInlineMath(t *testing.T) {
14	for _, tc := range []struct {
15		line   string
16		before rune
17		tex    string
18		length int
19	}{
20		{line: "$E = mc^2$ holds", before: ' ', tex: "E = mc^2", length: 10},
21		{line: `\(\alpha\) and`, before: ' ', tex: `\alpha`, length: 10},
22		{line: "$$x$$ inline", before: ' ', tex: "x", length: 5},
23		{line: `$a \$ b$`, before: ' ', tex: `a \$ b`, length: 8},
24		{line: "$5 or $10", before: ' '},
25		{line: "$HOME/$USER", before: ' '},
26		{line: "$FOO$BAR", before: ' '},
27		{line: "$x$", before: 'a'},
28		{line: "$ x$", before: ' '},
29		{line: "$x $", before: ' '},
30		{line: "$x\n$", before: ' '},
31		{line: "$$", before: ' '},
32	} {
33		tex, length := scanInlineMath([]byte(tc.line), tc.before)
34		if tex != tc.tex || length != tc.length {
35			t.Errorf("%q after %q: got %q, %d; want %q, %d", tc.line, tc.before, tex, length, tc.tex, tc.length)
36		}
37	}
38}
39
40func TestDisplayMathHangsInsideListItems(t *testing.T) {
41	rendered := ansi.Strip(RenderMarkdownAnsi("- item\n\n  $$\\frac{a}{b}$$\n- next", 40))
42	want := "• item\n\n   a\n  ───\n   b\n• next"
43	if got := trailingSpace.ReplaceAllString(rendered, ""); got != want {
44		t.Errorf("rendered\n%s\nwant\n%s", got, want)
45	}
46}
47
48var trailingSpace = regexp.MustCompile(`(?m) +$`)