|
| 1 | +import unittest |
| 2 | +import re |
| 3 | +from pix2tex.dataset.demacro import pydemacro |
| 4 | + |
| 5 | + |
| 6 | +def norm(s): |
| 7 | + s = re.sub(r'\n+', '\n', s) |
| 8 | + s = re.sub(r'\s+', ' ', s) |
| 9 | + return s.strip() |
| 10 | + |
| 11 | + |
| 12 | +def f(s): |
| 13 | + return norm(pydemacro(s)) |
| 14 | + |
| 15 | + |
| 16 | +class TestDemacroCases(unittest.TestCase): |
| 17 | + def test_noargs(self): |
| 18 | + inp = r''' |
| 19 | + \newcommand*{\noargs}{sample text} |
| 20 | + \noargs[a]\noargs{b}\noargs |
| 21 | + ''' |
| 22 | + expected = r'''sample text[a]sample text{b}sample text''' |
| 23 | + self.assertEqual(f(inp), norm(expected)) |
| 24 | + |
| 25 | + def test_optional_arg(self): |
| 26 | + inp = r''' |
| 27 | + \newcommand{\example}[2][YYY]{Mandatory arg: #2; Optional arg: #1.} |
| 28 | + \example{BBB} |
| 29 | + \example[XXX]{AAA} |
| 30 | + ''' |
| 31 | + expected = r''' |
| 32 | + Mandatory arg: BBB; Optional arg: YYY. |
| 33 | + Mandatory arg: AAA; Optional arg: XXX. |
| 34 | + ''' |
| 35 | + self.assertEqual(f(inp), norm(expected)) |
| 36 | + |
| 37 | + def test_optional_arg_and_positional_args(self): |
| 38 | + inp = r''' |
| 39 | + \newcommand{\plusbinomial}[3][2]{(#2 + #3)^{#1}} |
| 40 | + \plusbinomial[4]{y}{x} |
| 41 | + ''' |
| 42 | + expected = r'''(y + x)^{4}''' |
| 43 | + self.assertEqual(f(inp), norm(expected)) |
| 44 | + |
| 45 | + def test_alt_definition1(self): |
| 46 | + inp = r''' |
| 47 | + \newcommand\d{replacement} |
| 48 | + \d |
| 49 | + ''' |
| 50 | + expected = r'''replacement''' |
| 51 | + self.assertEqual(f(inp), norm(expected)) |
| 52 | + |
| 53 | + def test_arg_with_bs_and_cb(self): |
| 54 | + # def 1 argument and with backslash (bs) and cruly brackets (cb) in definition |
| 55 | + inp = r''' |
| 56 | + \newcommand{\eq}[1]{\begin{equation}#1\end{equation}} |
| 57 | + \eq{\sqrt{2}\approx1.4} |
| 58 | + \eq[unexpected argument]{\sqrt{2}\approx1.4} |
| 59 | + ''' |
| 60 | + expected = r''' |
| 61 | + \begin{equation}\sqrt{2}\approx1.4\end{equation} |
| 62 | + \begin{equation}\sqrt{2}\approx1.4\end{equation} |
| 63 | + ''' |
| 64 | + self.assertEqual(f(inp), norm(expected)) |
| 65 | + |
| 66 | + def test_multiline_definition(self): |
| 67 | + inp = r''' |
| 68 | + \newcommand{\multiline}[2]{% |
| 69 | + Arg 1: \bf{#1} |
| 70 | + Arg 2: #2 |
| 71 | + } |
| 72 | + \multiline{1}{two} |
| 73 | + ''' |
| 74 | + expected = r''' |
| 75 | + Arg 1: \bf{1} |
| 76 | + Arg 2: two |
| 77 | + ''' |
| 78 | + self.assertEqual(f(inp), norm(expected)) |
| 79 | + |
| 80 | + def test_multiline_definition_alt1(self): |
| 81 | + inp = r''' |
| 82 | + \newcommand{\identity}[1] |
| 83 | + {#1} |
| 84 | + \identity{x} |
| 85 | + ''' |
| 86 | + expected = 'x' |
| 87 | + self.assertEqual(f(inp), norm(expected)) |
| 88 | + |
| 89 | + def test_multiline_definition_alt2(self): |
| 90 | + inp = r''' |
| 91 | + \newcommand |
| 92 | + {\identity}[1]{#1} |
| 93 | + \identity{x} |
| 94 | + ''' |
| 95 | + expected = 'x' |
| 96 | + self.assertEqual(f(inp), norm(expected)) |
| 97 | + |
| 98 | + def test_multiline_definition_alt3(self): |
| 99 | + inp = r''' |
| 100 | + \newcommand |
| 101 | + {\identity}[1] |
| 102 | + {#1} |
| 103 | + \identity{x} |
| 104 | + ''' |
| 105 | + expected = 'x' |
| 106 | + self.assertEqual(f(inp), norm(expected)) |
| 107 | + |
| 108 | + def test_multiline_definition_alt4(self): |
| 109 | + inp = r''' |
| 110 | + \newcommand |
| 111 | + {\identity} |
| 112 | + [1] |
| 113 | + {#1} |
| 114 | + \identity{x} |
| 115 | + ''' |
| 116 | + expected = 'x' |
| 117 | + self.assertEqual(f(inp), norm(expected)) |
| 118 | + |
| 119 | + def test_nested_definition(self): |
| 120 | + inp = r''' |
| 121 | + \newcommand{\cmd}[1]{command #1} |
| 122 | + \newcommand{\nested}[2]{\cmd{#1} \cmd{#2}} |
| 123 | + \nested{\alpha}{\beta} |
| 124 | + ''' |
| 125 | + expected = r''' |
| 126 | + command \alpha command \beta |
| 127 | + ''' |
| 128 | + self.assertEqual(f(inp), norm(expected)) |
| 129 | + |
| 130 | + def test_def(self): |
| 131 | + # check if \def is handled correctly. |
| 132 | + inp = r''' |
| 133 | + \def\defcheck#1#2{Defcheck arg1: #1 arg2: #2} |
| 134 | + \defcheck{1}{two} |
| 135 | + ''' |
| 136 | + expected = r''' |
| 137 | + Defcheck arg1: 1 arg2: two |
| 138 | + ''' |
| 139 | + self.assertEqual(f(inp), norm(expected)) |
| 140 | + |
| 141 | + def test_multi_def_lines_alt0(self): |
| 142 | + inp = r'''\def\be{\begin{equation}} \def\ee{\end{equation}} %some comment |
| 143 | + \be |
| 144 | + 1+1=2 |
| 145 | + \ee''' |
| 146 | + expected = r''' |
| 147 | + \begin{equation} |
| 148 | + 1+1=2 |
| 149 | + \end{equation} |
| 150 | + ''' |
| 151 | + self.assertEqual(f(inp), norm(expected)) |
| 152 | + |
| 153 | + def test_multi_def_lines_alt1(self): |
| 154 | + inp = r'''\def\be{\begin{equation}}\def\ee{\end{equation}} |
| 155 | + \be |
| 156 | + 1+1=2 |
| 157 | + \ee''' |
| 158 | + expected = r''' |
| 159 | + \begin{equation} |
| 160 | + 1+1=2 |
| 161 | + \end{equation} |
| 162 | + ''' |
| 163 | + self.assertEqual(f(inp), norm(expected)) |
| 164 | + |
| 165 | + def test_multi_def_lines_alt2(self): |
| 166 | + inp = r'''\def |
| 167 | + \be{\begin{equation}} |
| 168 | + \def\ee |
| 169 | + {\end{equation}} |
| 170 | + \be |
| 171 | + 1+1=2 |
| 172 | + \ee''' |
| 173 | + expected = r''' |
| 174 | + \begin{equation} |
| 175 | + 1+1=2 |
| 176 | + \end{equation} |
| 177 | + ''' |
| 178 | + self.assertEqual(f(inp), norm(expected)) |
| 179 | + |
| 180 | + def test_multi_def_lines_alt3(self): |
| 181 | + inp = r''' |
| 182 | + \def\be |
| 183 | + { |
| 184 | + \begin{equation} |
| 185 | + } |
| 186 | + \def |
| 187 | + \ee |
| 188 | + {\end{equation}} |
| 189 | + \be |
| 190 | + 1+1=2 |
| 191 | + \ee''' |
| 192 | + expected = r''' |
| 193 | + \begin{equation} |
| 194 | + 1+1=2 |
| 195 | + \end{equation} |
| 196 | + ''' |
| 197 | + self.assertEqual(f(inp), norm(expected)) |
| 198 | + |
| 199 | + def test_let_alt0(self): |
| 200 | + inp = r'''\let\a\alpha\let\b=\beta |
| 201 | + \a \b''' |
| 202 | + expected = r'''\alpha \beta''' |
| 203 | + self.assertEqual(f(inp), norm(expected)) |
| 204 | + |
| 205 | + def test_let_alt1(self): |
| 206 | + inp = r'''\let\a\alpha \let\b=\beta |
| 207 | + \a \b''' |
| 208 | + expected = r'''\alpha \beta''' |
| 209 | + self.assertEqual(f(inp), norm(expected)) |
| 210 | + |
| 211 | + def test_let_alt2(self): |
| 212 | + inp = r'''\let\a\alpha \let\b=\beta |
| 213 | + \a \b''' |
| 214 | + expected = r'''\alpha \beta''' |
| 215 | + self.assertEqual(f(inp), norm(expected)) |
| 216 | + |
| 217 | + def test_let_alt3(self): |
| 218 | + inp = r''' |
| 219 | + \let |
| 220 | + \a |
| 221 | + \alpha |
| 222 | + \let\b= |
| 223 | + \beta |
| 224 | + \a \b''' |
| 225 | + expected = r'''\alpha \beta''' |
| 226 | + self.assertEqual(f(inp), norm(expected)) |
| 227 | + |
| 228 | + |
| 229 | +if __name__ == '__main__': |
| 230 | + unittest.main() |
0 commit comments