Debian Codegraph @

src/tests/math.rs

raw
1 use super::*;
2 use ntest::test_case;
3
4 #[test_case("$2+2$", "<p><math>2+2</math></p>\n")]
5 #[test_case(
6 "$\\alpha$$\\beta$",
7 "<p><math>\\alpha</math><math>\\beta</math></p>\n"
8 )]
9 #[test_case("$2+2$$2+2$", "<p><math>2+2</math><math>2+2</math></p>\n")]
10 #[test_case("$22 and $2+2$", "<p>$22 and <math>2+2</math></p>\n")]
11 #[test_case("$a!$", "<p><math>a!</math></p>\n")]
12 #[test_case("$x$", "<p><math>x</math></p>\n")]
13 #[test_case("$1+2\\$$", "<p><math>1+2\\$</math></p>\n")]
14 #[test_case("$1+\\$2$", "<p><math>1+\\$2</math></p>\n")]
15 #[test_case("$1+\\%2$", "<p><math>1+\\%2</math></p>\n")]
16 #[test_case(
17 "$22+1$ and $22 + a^2$",
18 "<p><math>22+1</math> and <math>22 + a^2</math></p>\n"
19 )]
20 #[test_case(
21 "$2+2$ $22 and dollars$22 $2+2$",
22 "<p><math>2+2</math> $22 and dollars$22 <math>2+2</math></p>\n"
23 )]
24 #[test_case(
25 "$1/2$ &lt;b&gt;test&lt;/b&gt;",
26 "<p><math>1/2</math> &lt;b&gt;test&lt;/b&gt;</p>\n"
27 )]
28 fn math_dollars_inline(markdown: &str, html: &str) {
29 let result = html
30 .replace("<math>", "<span data-math-style=\"inline\">")
31 .replace("</math>", "</span>");
32
33 html_opts!([extension.math_dollars], markdown, &result);
34 }
35
36 #[test_case("$$2+2$$", "<p><math>2+2</math></p>\n")]
37 #[test_case("$$ 2+2 $$", "<p><math> 2+2 </math></p>\n")]
38 #[test_case("$22 and $$2+2$$", "<p>$22 and <math>2+2</math></p>\n")]
39 #[test_case("$$a!$$", "<p><math>a!</math></p>\n")]
40 #[test_case("$$x$$", "<p><math>x</math></p>\n")]
41 #[test_case("$$20,000 and $$30,000", "<p><math>20,000 and </math>30,000</p>\n")]
42 #[test_case("test $$\n2+2\n$$", "<p>test <math>\n2+2\n</math></p>\n")]
43 #[test_case(
44 "$$22+1$$ and $$22 + a^2$$",
45 "<p><math>22+1</math> and <math>22 + a^2</math></p>\n"
46 )]
47 #[test_case(
48 "$$2+2$$ $22 and dollars$22 $$2+2$$",
49 "<p><math>2+2</math> $22 and dollars$22 <math>2+2</math></p>\n"
50 )]
51 #[test_case(
52 "dollars$22 and $$a^2 + b^2 = c^2$$",
53 "<p>dollars$22 and <math>a^2 + b^2 = c^2</math></p>\n"
54 )]
55 fn math_dollars_inline_display(markdown: &str, html: &str) {
56 let result = html
57 .replace("<math>", "<span data-math-style=\"display\">")
58 .replace("</math>", "</span>");
59
60 html_opts!([extension.math_dollars], markdown, &result);
61 }
62
63 #[test_case("$$\n2+2\n4+4\n$$", "<p><math>\n2+2\n4+4\n</math></p>\n")]
64 fn math_dollars_block(markdown: &str, html: &str) {
65 let result = html
66 .replace("<math>", "<span data-math-style=\"display\">")
67 .replace("</math>", "</span>");
68
69 html_opts!([extension.math_dollars], markdown, &result);
70 }
71
72 #[test_case("\\(2+2\\)", "<p><math>2+2</math></p>\n")]
73 #[test_case(
74 "Calc \\(x^2\\) and \\(y^2\\)",
75 "<p>Calc <math>x^2</math> and <math>y^2</math></p>\n"
76 )]
77 #[test_case("\\(\\alpha + \\beta\\)", "<p><math>\\alpha + \\beta</math></p>\n")]
78 fn math_latex_inline(markdown: &str, html: &str) {
79 let result = html
80 .replace("<math>", "<span data-math-style=\"inline\">")
81 .replace("</math>", "</span>");
82
83 html_opts!([extension.math_latex], markdown, &result);
84 }
85
86 #[test_case("\\[2+2\\]", "<p><math>2+2</math></p>\n")]
87 #[test_case("\\[\n2+2\n\\]", "<p><math>\n2+2\n</math></p>\n")]
88 fn math_latex_display(markdown: &str, html: &str) {
89 let result = html
90 .replace("<math>", "<span data-math-style=\"display\">")
91 .replace("</math>", "</span>");
92
93 html_opts!([extension.math_latex], markdown, &result);
94 }
95
96 #[test_case("`\\(2+2\\)`", "<p><code>\\(2+2\\)</code></p>\n")]
97 #[test_case(
98 "```text\n\\[2+2\\]\n```",
99 "<pre><code class=\"language-text\">\\[2+2\\]\n</code></pre>\n"
100 )]
101 #[test_case("\\(2+2", "<p>(2+2</p>\n")]
102 #[test_case("\\[2+2", "<p>[2+2</p>\n")]
103 #[test_case("\\(x\\]", "<p>(x]</p>\n")]
104 #[test_case("\\[x\\)", "<p>[x)</p>\n")]
105 #[test_case("\\(\\)", "<p>()</p>\n")]
106 #[test_case("\\[\\]", "<p>[]</p>\n")]
107 fn math_latex_unrecognized_syntax(markdown: &str, html: &str) {
108 html_opts!([extension.math_latex], markdown, html);
109 }
110
111 #[test_case("$`2+2`$", "<p><math>2+2</math></p>\n")]
112 #[test_case("$22 and $`2+2`$", "<p>$22 and <math>2+2</math></p>\n")]
113 #[test_case("$`1+\\$2`$", "<p><math>1+\\$2</math></p>\n")]
114 #[test_case(
115 "$`22+1`$ and $`22 + a^2`$",
116 "<p><math>22+1</math> and <math>22 + a^2</math></p>\n"
117 )]
118 #[test_case(
119 "$`2+2`$ $22 and dollars$22 $`2+2`$",
120 "<p><math>2+2</math> $22 and dollars$22 <math>2+2</math></p>\n"
121 )]
122 fn math_code_inline(markdown: &str, html: &str) {
123 let result = html
124 .replace("<math>", "<code data-math-style=\"inline\">")
125 .replace("</math>", "</code>");
126
127 html_opts!([extension.math_code], markdown, &result);
128 }
129
130 #[test_case("```math\n2+2\n4+4\n```", "<pre><math>2+2\n4+4\n</math></pre>\n")]
131 fn math_code_block(markdown: &str, html: &str) {
132 let result = html
133 .replace(
134 "<math>",
135 "<code class=\"language-math\" data-math-style=\"display\">",
136 )
137 .replace("</math>", "</code>");
138
139 html_opts!([extension.math_dollars], markdown, &result);
140 }
141
142 #[test_case("`2+2`", "<p><code>2+2</code></p>\n")]
143 #[test_case("test `2+2`$ test", "<p>test <code>2+2</code>$ test</p>\n")]
144 #[test_case("$20,000 and $30,000", "<p>$20,000 and $30,000</p>\n")]
145 #[test_case("$20,000 in $USD", "<p>$20,000 in $USD</p>\n")]
146 #[test_case("$ a^2 $", "<p>$ a^2 $</p>\n")]
147 #[test_case("$\n$", "<p>$\n$</p>\n")]
148 #[test_case("$$$", "<p>$$$</p>\n")]
149 #[test_case("`$1+2$`", "<p><code>$1+2$</code></p>\n")]
150 #[test_case("`$$1+2$$`", "<p><code>$$1+2$$</code></p>\n")]
151 #[test_case("`$\\$1+2$$`", "<p><code>$\\$1+2$$</code></p>\n")]
152 #[test_case("test $`2+2` test", "<p>test $<code>2+2</code> test</p>\n")]
153 fn math_unrecognized_syntax(markdown: &str, html: &str) {
154 html_opts!(
155 [extension.math_dollars, extension.math_code],
156 markdown,
157 html
158 );
159 }
160
161 #[test_case("$`$", "<p data-sourcepos=\"1:1-1:3\">$`$</p>\n")]
162 fn math_unrecognized_syntax_non_roundtrip(markdown: &str, html: &str) {
163 html_opts!(
164 [
165 extension.math_dollars,
166 extension.math_code,
167 render.sourcepos
168 ],
169 markdown,
170 html,
171 no_roundtrip
172 );
173 }
174
175 #[test]
176 fn sourcepos() {
177 assert_ast_match!(
178 [extension.math_dollars, extension.math_code],
179 "$x^2$ and $$y^2$$ and $`z^2`$\n"
180 "\n"
181 "$$\n"
182 "a^2\n"
183 "$$\n"
184 "\n"
185 "```math\n"
186 "b^2\n"
187 "```\n",
188 (document (1:1-9:3) [
189 (paragraph (1:1-1:29) [
190 (math (1:1-1:5) "x^2")
191 (text (1:6-1:10) " and ")
192 (math (1:11-1:17) "y^2")
193 (text (1:18-1:22) " and ")
194 (math (1:23-1:29) "z^2")
195 ])
196 (paragraph (3:1-5:2) [
197 (math (3:1-5:2) "\na^2\n")
198 ])
199 (code_block (7:1-9:3) info:"math" "b^2\n")
200 ])
201 );
202 }
203
204 #[test]
205 fn math_latex_sourcepos() {
206 assert_ast_match!(
207 [extension.math_latex],
208 "\\(x^2\\) and \\[y^2\\]\n"
209 "\n"
210 "\\[\n"
211 "a^2\n"
212 "\\]\n",
213 (document (1:1-5:2) [
214 (paragraph (1:1-1:19) [
215 (math (1:1-1:7) "x^2")
216 (text (1:8-1:12) " and ")
217 (math (1:13-1:19) "y^2")
218 ])
219 (paragraph (3:1-5:2) [
220 (math (3:1-5:2) "\na^2\n")
221 ])
222 ])
223 );
224 }