|
| 1 | +// Copyright 2024 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package markdown |
| 5 | + |
| 6 | +import ( |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "code.gitea.io/gitea/modules/markup" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | +) |
| 14 | + |
| 15 | +func TestMathRender(t *testing.T) { |
| 16 | + const nl = "\n" |
| 17 | + testcases := []struct { |
| 18 | + testcase string |
| 19 | + expected string |
| 20 | + }{ |
| 21 | + { |
| 22 | + "$a$", |
| 23 | + `<p><code class="language-math is-loading">a</code></p>` + nl, |
| 24 | + }, |
| 25 | + { |
| 26 | + "$ a $", |
| 27 | + `<p><code class="language-math is-loading">a</code></p>` + nl, |
| 28 | + }, |
| 29 | + { |
| 30 | + "$a$ $b$", |
| 31 | + `<p><code class="language-math is-loading">a</code> <code class="language-math is-loading">b</code></p>` + nl, |
| 32 | + }, |
| 33 | + { |
| 34 | + `\(a\) \(b\)`, |
| 35 | + `<p><code class="language-math is-loading">a</code> <code class="language-math is-loading">b</code></p>` + nl, |
| 36 | + }, |
| 37 | + { |
| 38 | + `$a$.`, |
| 39 | + `<p><code class="language-math is-loading">a</code>.</p>` + nl, |
| 40 | + }, |
| 41 | + { |
| 42 | + `.$a$`, |
| 43 | + `<p>.$a$</p>` + nl, |
| 44 | + }, |
| 45 | + { |
| 46 | + `$a a$b b$`, |
| 47 | + `<p>$a a$b b$</p>` + nl, |
| 48 | + }, |
| 49 | + { |
| 50 | + `a a$b b`, |
| 51 | + `<p>a a$b b</p>` + nl, |
| 52 | + }, |
| 53 | + { |
| 54 | + `a$b $a a$b b$`, |
| 55 | + `<p>a$b $a a$b b$</p>` + nl, |
| 56 | + }, |
| 57 | + { |
| 58 | + "a$x$", |
| 59 | + `<p>a$x$</p>` + nl, |
| 60 | + }, |
| 61 | + { |
| 62 | + "$x$a", |
| 63 | + `<p>$x$a</p>` + nl, |
| 64 | + }, |
| 65 | + { |
| 66 | + "$a$ ($b$) [$c$] {$d$}", |
| 67 | + `<p><code class="language-math is-loading">a</code> (<code class="language-math is-loading">b</code>) [$c$] {$d$}</p>` + nl, |
| 68 | + }, |
| 69 | + { |
| 70 | + "$$a$$", |
| 71 | + `<pre class="code-block is-loading"><code class="chroma language-math display">a</code></pre>` + nl, |
| 72 | + }, |
| 73 | + { |
| 74 | + "$$a$$ test", |
| 75 | + `<p><code class="language-math display is-loading">a</code> test</p>` + nl, |
| 76 | + }, |
| 77 | + { |
| 78 | + "test $$a$$", |
| 79 | + `<p>test <code class="language-math display is-loading">a</code></p>` + nl, |
| 80 | + }, |
| 81 | + { |
| 82 | + "foo $x=\\$$ bar", |
| 83 | + `<p>foo <code class="language-math is-loading">x=\$</code> bar</p>` + nl, |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + for _, test := range testcases { |
| 88 | + t.Run(test.testcase, func(t *testing.T) { |
| 89 | + res, err := RenderString(markup.NewTestRenderContext(), test.testcase) |
| 90 | + assert.NoError(t, err) |
| 91 | + assert.Equal(t, test.expected, string(res)) |
| 92 | + }) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestMathRenderBlockIndent(t *testing.T) { |
| 97 | + const nl = "\n" |
| 98 | + testcases := []struct { |
| 99 | + name string |
| 100 | + testcase string |
| 101 | + expected string |
| 102 | + }{ |
| 103 | + { |
| 104 | + "indent-0", |
| 105 | + ` |
| 106 | +\[ |
| 107 | +\alpha |
| 108 | +\] |
| 109 | +`, |
| 110 | + `<pre class="code-block is-loading"><code class="chroma language-math display"> |
| 111 | +\alpha |
| 112 | +</code></pre> |
| 113 | +`, |
| 114 | + }, |
| 115 | + { |
| 116 | + "indent-1", |
| 117 | + ` |
| 118 | + \[ |
| 119 | + \alpha |
| 120 | + \] |
| 121 | +`, |
| 122 | + `<pre class="code-block is-loading"><code class="chroma language-math display"> |
| 123 | +\alpha |
| 124 | +</code></pre> |
| 125 | +`, |
| 126 | + }, |
| 127 | + { |
| 128 | + "indent-2", |
| 129 | + ` |
| 130 | + \[ |
| 131 | + \alpha |
| 132 | + \] |
| 133 | +`, |
| 134 | + `<pre class="code-block is-loading"><code class="chroma language-math display"> |
| 135 | +\alpha |
| 136 | +</code></pre> |
| 137 | +`, |
| 138 | + }, |
| 139 | + { |
| 140 | + "indent-0-oneline", |
| 141 | + `$$ x $$ |
| 142 | +foo`, |
| 143 | + `<pre class="code-block is-loading"><code class="chroma language-math display"> x </code></pre> |
| 144 | +<p>foo</p> |
| 145 | +`, |
| 146 | + }, |
| 147 | + { |
| 148 | + "indent-3-oneline", |
| 149 | + ` $$ x $$<SPACE> |
| 150 | +foo`, |
| 151 | + `<pre class="code-block is-loading"><code class="chroma language-math display"> x </code></pre> |
| 152 | +<p>foo</p> |
| 153 | +`, |
| 154 | + }, |
| 155 | + } |
| 156 | + |
| 157 | + for _, test := range testcases { |
| 158 | + t.Run(test.name, func(t *testing.T) { |
| 159 | + res, err := RenderString(markup.NewTestRenderContext(), strings.ReplaceAll(test.testcase, "<SPACE>", " ")) |
| 160 | + assert.NoError(t, err) |
| 161 | + assert.Equal(t, test.expected, string(res), "unexpected result for test case:\n%s", test.testcase) |
| 162 | + }) |
| 163 | + } |
| 164 | +} |
0 commit comments