0
0
mirror of https://github.com/go-gitea/gitea.git synced 2024-12-22 18:45:21 +01:00
gitea/modules/markup/markdown/markdown_math_test.go

246 lines
4.0 KiB
Go
Raw Normal View History

// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package markdown
import (
"strings"
"testing"
"code.gitea.io/gitea/modules/markup"
"github.com/stretchr/testify/assert"
)
const nl = "\n"
func TestMathRender(t *testing.T) {
testcases := []struct {
testcase string
expected string
}{
{
"$a$",
2024-12-13 07:45:32 +01:00
`<p><code class="language-math">a</code></p>` + nl,
},
{
"$ a $",
2024-12-13 07:45:32 +01:00
`<p><code class="language-math">a</code></p>` + nl,
},
{
"$a$ $b$",
2024-12-13 07:45:32 +01:00
`<p><code class="language-math">a</code> <code class="language-math">b</code></p>` + nl,
},
{
`\(a\) \(b\)`,
2024-12-13 07:45:32 +01:00
`<p><code class="language-math">a</code> <code class="language-math">b</code></p>` + nl,
},
{
`$a$.`,
2024-12-13 07:45:32 +01:00
`<p><code class="language-math">a</code>.</p>` + nl,
},
{
`.$a$`,
`<p>.$a$</p>` + nl,
},
{
`$a a$b b$`,
`<p>$a a$b b$</p>` + nl,
},
{
`a a$b b`,
`<p>a a$b b</p>` + nl,
},
{
`a$b $a a$b b$`,
`<p>a$b $a a$b b$</p>` + nl,
},
{
"a$x$",
`<p>a$x$</p>` + nl,
},
{
"$x$a",
`<p>$x$a</p>` + nl,
},
{
"$a$ ($b$) [$c$] {$d$}",
2024-12-13 07:45:32 +01:00
`<p><code class="language-math">a</code> (<code class="language-math">b</code>) [$c$] {$d$}</p>` + nl,
},
{
"$$a$$",
2024-12-13 07:45:32 +01:00
`<code class="language-math display">a</code>` + nl,
},
{
"$$a$$ test",
2024-12-13 07:45:32 +01:00
`<p><code class="language-math">a</code> test</p>` + nl,
},
{
"test $$a$$",
2024-12-13 07:45:32 +01:00
`<p>test <code class="language-math">a</code></p>` + nl,
},
{
`foo $x=\$$ bar`,
2024-12-13 07:45:32 +01:00
`<p>foo <code class="language-math">x=\$</code> bar</p>` + nl,
},
{
`$\text{$b$}$`,
2024-12-13 07:45:32 +01:00
`<p><code class="language-math">\text{$b$}</code></p>` + nl,
},
{
"a$`b`$c",
`<p>a<code class="language-math">b</code>c</p>` + nl,
},
{
"a $`b`$ c",
`<p>a <code class="language-math">b</code> c</p>` + nl,
},
{
"a$``b``$c x$```y```$z",
`<p>a<code class="language-math">b</code>c x<code class="language-math">y</code>z</p>` + nl,
},
}
for _, test := range testcases {
t.Run(test.testcase, func(t *testing.T) {
res, err := RenderString(markup.NewTestRenderContext(), test.testcase)
assert.NoError(t, err)
assert.Equal(t, test.expected, string(res))
})
}
}
func TestMathRenderBlockIndent(t *testing.T) {
testcases := []struct {
name string
testcase string
expected string
}{
{
"indent-0",
`
\[
\alpha
\]
`,
2024-12-13 07:45:32 +01:00
`<pre class="code-block is-loading"><code class="language-math display">
\alpha
</code></pre>
`,
},
{
"indent-1",
`
\[
\alpha
\]
`,
2024-12-13 07:45:32 +01:00
`<pre class="code-block is-loading"><code class="language-math display">
\alpha
</code></pre>
`,
},
{
"indent-2-mismatch",
`
\[
a
b
c
d
\]
`,
2024-12-13 07:45:32 +01:00
`<pre class="code-block is-loading"><code class="language-math display">
a
b
c
d
</code></pre>
`,
},
{
"indent-2",
`
\[
a
b
c
\]
`,
2024-12-13 07:45:32 +01:00
`<pre class="code-block is-loading"><code class="language-math display">
a
b
c
</code></pre>
`,
},
{
"indent-0-oneline",
`$$ x $$
foo`,
2024-12-13 07:45:32 +01:00
`<code class="language-math display"> x </code>
<p>foo</p>
`,
},
{
"indent-3-oneline",
` $$ x $$<SPACE>
foo`,
2024-12-13 07:45:32 +01:00
`<code class="language-math display"> x </code>
<p>foo</p>
`,
},
{
"quote-block",
`
> \[
> a
> \]
> \[
> b
> \]
`,
`<blockquote>
2024-12-13 07:45:32 +01:00
<pre class="code-block is-loading"><code class="language-math display">
a
</code></pre>
2024-12-13 07:45:32 +01:00
<pre class="code-block is-loading"><code class="language-math display">
b
</code></pre>
</blockquote>
`,
},
{
"list-block",
`
1. a
\[
x
\]
2. b`,
`<ol>
<li>a
2024-12-13 07:45:32 +01:00
<pre class="code-block is-loading"><code class="language-math display">
x
</code></pre>
</li>
<li>b</li>
</ol>
`,
},
{
"inline-non-math",
`\[x]`,
`<p>[x]</p>` + nl,
},
}
for _, test := range testcases {
t.Run(test.name, func(t *testing.T) {
res, err := RenderString(markup.NewTestRenderContext(), strings.ReplaceAll(test.testcase, "<SPACE>", " "))
assert.NoError(t, err)
assert.Equal(t, test.expected, string(res), "unexpected result for test case:\n%s", test.testcase)
})
}
}