Skip to content

Commit 918316e

Browse files
committed
feat: improve test cases
1 parent b89b0f2 commit 918316e

2 files changed

Lines changed: 25 additions & 13 deletions

File tree

internal/libs/tex/latexpand.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ import (
1010

1111
// commentRegex matches a LaTeX comment: an unescaped % and everything after it
1212
// until end of line. The leading group captures either start-of-line or a
13-
// non-backslash character so that \% (an escaped percent) is preserved. Pairs
14-
// of backslashes (\\) before % are treated as a line-break followed by a real
15-
// comment, matching LaTeX semantics.
13+
// non-backslash character, then consumes pairs of backslashes (\\) before %.
14+
// This generalizes to any run of N backslashes preceding %: if N is even
15+
// (including 0), every backslash pairs up as a literal-backslash escape and
16+
// the % is unescaped, so the comment is stripped; if N is odd, the final
17+
// backslash escapes the % itself, so the % (and the surrounding text) is
18+
// preserved.
1619
var commentRegex = regexp.MustCompile(`(^|[^\\])((?:\\\\)*)%.*$`)
1720

1821
func removeComments(text string) string {

internal/libs/tex/latexpand_test.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,25 @@ Hello, world!
2020
\end{document}`, removeComments(input))
2121
}
2222

23-
func TestRemoveCommentsPreservesEscapedPercent(t *testing.T) {
24-
const input = `accuracy improved by 12\% over baseline % TODO: recheck`
25-
assert.Equal(t, `accuracy improved by 12\% over baseline`, removeComments(input))
26-
}
27-
28-
func TestRemoveCommentsDoubleBackslashBeforePercent(t *testing.T) {
29-
const input = `line one \\% this is a real comment after a line break
30-
next line`
31-
assert.Equal(t, `line one \\
32-
next line`, removeComments(input))
23+
func TestRemoveCommentsBackslashRunsBeforePercent(t *testing.T) {
24+
cases := []struct {
25+
name string
26+
input string
27+
want string
28+
}{
29+
{"1 backslash (odd) preserves %", `a\% keep`, `a\% keep`},
30+
{"2 backslashes (even) strips comment", `a\\% drop`, `a\\`},
31+
{"3 backslashes (odd) preserves %", `a\\\% keep`, `a\\\% keep`},
32+
{"4 backslashes (even) strips comment", `a\\\\% drop`, `a\\\\`},
33+
{"5 backslashes (odd) preserves %", `a\\\\\% keep`, `a\\\\\% keep`},
34+
{"3 backslashes at line start preserves %", `\\\% keep`, `\\\% keep`},
35+
{"4 backslashes at line start strips comment", `\\\\% drop`, `\\\\`},
36+
}
37+
for _, tc := range cases {
38+
t.Run(tc.name, func(t *testing.T) {
39+
assert.Equal(t, tc.want, removeComments(tc.input))
40+
})
41+
}
3342
}
3443

3544
func TestLatexpand(t *testing.T) {

0 commit comments

Comments
 (0)