Debian Codegraph @

extensions/Math/tests/phpunit/unit/WikiTexVC/MMLmappings/BaseParsingTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Math\Tests\WikiTexVC\MMLmappings;
4
5 use MediaWiki\Extension\Math\WikiTexVC\MMLmappings\BaseParsing;
6 use MediaWiki\Extension\Math\WikiTexVC\Nodes\DQ;
7 use MediaWiki\Extension\Math\WikiTexVC\Nodes\Fun1;
8 use MediaWiki\Extension\Math\WikiTexVC\Nodes\Literal;
9 use MediaWiki\Extension\Math\WikiTexVC\Nodes\Matrix;
10 use MediaWiki\Extension\Math\WikiTexVC\Nodes\TexArray;
11 use MediaWiki\Extension\Math\WikiTexVC\TexVC;
12 use PHPUnit\Framework\TestCase;
13
14 /**
15 * @covers \MediaWiki\Extension\Math\WikiTexVC\MMLmappings\BaseParsing
16 */
17 class BaseParsingTest extends TestCase {
18
19 public function testAccent() {
20 $node = new Fun1(
21 '\\widetilde',
22 ( new Literal( 'a' ) )
23 );
24 $result = BaseParsing::accent( $node, [], null, 'widetilde', '007E' );
25 $this->assertStringContainsString( '~', $result );
26 $this->assertStringContainsString( 'mover', $result );
27 }
28
29 public function testAccentArgPassing() {
30 $node = new Fun1(
31 '\\widetilde',
32 ( new Literal( 'a' ) )
33 );
34 $result = BaseParsing::accent( $node, [ 'k' => 'v' ], null, 'widetilde', '007E' );
35 $this->assertStringContainsString( '<mi k="v"', $result );
36 }
37
38 public function testArray() {
39 $node = new Matrix( 'matrix',
40 new TexArray( new TexArray( new Literal( 'a' ) ) ) );
41
42 $result = BaseParsing::array( $node, [], null, 'array', '007E' );
43 $this->assertStringContainsString( '<mi>a</mi>', $result );
44 }
45
46 public function testBoldGreek() {
47 $node = new Fun1(
48 '\\boldsymbol',
49 ( new Literal( '\\alpha' ) )
50 );
51 $result = BaseParsing::boldsymbol( $node, [], null, 'boldsymbol' );
52 $this->assertStringContainsString( 'mathvariant="bold-italic"', $result );
53 }
54
55 public function testBoldSymbol() {
56 $node = new Fun1(
57 '\\boldsymbol',
58 ( new Literal( 'a' ) )
59 );
60 $result = BaseParsing::boldsymbol( $node, [], null, 'boldsymbol' );
61 $this->assertStringContainsString( 'mathvariant="bold-italic"', $result );
62 }
63
64 public function testCancel() {
65 $node = new Fun1(
66 '\\cancel',
67 ( new Literal( 'a' ) )
68 );
69 $result = BaseParsing::cancel( $node, [], null, 'cancel', 'something' );
70 $this->assertStringContainsString( '<mi>a</mi><mrow class="menclose-something"/>',
71 $result );
72 $this->assertStringContainsString( '<menclose notation="something" class="menclose">',
73 $result );
74 }
75
76 public function testUnderOver() {
77 $node = new Fun1(
78 '\\overline',
79 ( new Literal( 'a' ) )
80 );
81 $result = BaseParsing::underover( $node, [], null, '', '00AF' );
82 $this->assertStringStartsWith( '<mrow', $result );
83 $this->assertStringContainsString( 'mover', $result );
84 }
85
86 public function testUnderOverUnder() {
87 $node = new Fun1(
88 '\\underline',
89 ( new Literal( 'a' ) )
90 );
91 $result = BaseParsing::underover( $node, [], null, '', '00AF' );
92 $this->assertStringContainsString( 'munder', $result );
93 }
94
95 public function testUnderOverDqUnder() {
96 $node = new Fun1(
97 '\\underline', new DQ(
98 ( new Literal( 'a' ) ),
99 ( new Literal( 'b' ) )
100 ) );
101 $result = BaseParsing::underover( $node, [], null, '', '00AF' );
102 $this->assertStringContainsString( 'munder', $result );
103 $this->assertStringContainsString( 'mrow', $result );
104 }
105
106 public function testUnderOverInvalid() {
107 $node = new Fun1(
108 '\\someline',
109 ( new Literal( 'a' ) )
110 );
111 $result = BaseParsing::underover( $node, [], null, '', '00AF' );
112 $this->assertStringContainsString( 'merror', $result );
113 }
114
115 public function testUnderArgPassing() {
116 $node = new Fun1(
117 '\\overline',
118 ( new Literal( 'a' ) )
119 );
120 $result = BaseParsing::underover( $node, [ 'k' => 'v' ], null, '', '00AF' );
121 $this->assertStringContainsString( '<mi k="v"', $result );
122 }
123
124 public function testUnderBadArgPassing() {
125 $node = new Fun1(
126 '\\overline',
127 ( new Literal( 'a' ) )
128 );
129 $result = BaseParsing::underover( $node,
130 [ 'k' => '"<script>alert("problem")</script>"' ], null, '', '00AF' );
131 $this->assertStringContainsString( 'k="&quot;&lt;script&gt;alert(&quot;problem&quot;)', $result );
132 }
133
134 public function testAlignAt() {
135 $matrix = new Matrix( 'alignat',
136 new TexArray( new TexArray( new Literal( '\\sin' ) ) ) );
137 $result = BaseParsing::alignAt( $matrix, [], null, 'alignat', '002A' );
138 $this->assertStringContainsString( 'mtable', $result );
139 }
140
141 public function testHLineTop() {
142 $matrix = new Matrix( 'matrix',
143 new TexArray( new TexArray( new TexArray( new Literal( '\\hline ' ), new Literal( 'a'
144 ) ) ) ) );
145 $result = BaseParsing::matrix( $matrix, [], null, 'matrix', '002A' );
146 $this->assertStringContainsString( 'top', $result );
147 }
148
149 public function testHLineBottom() {
150 $matrix = new Matrix( 'matrix',
151 new TexArray( new TexArray( new Literal( 'a' ) ),
152 new TexArray( new TexArray( new Literal( '\\hline ' ) ) ) ) );
153 $result = BaseParsing::matrix( $matrix, [], null, 'matrix', '002A' );
154 $this->assertStringContainsString( 'bottom', $result );
155 $this->assertStringContainsString( '<mi>a</mi>', $result );
156 }
157
158 public function testHLineLastLine() {
159 $matrix = new Matrix( 'matrix',
160 new TexArray( new TexArray( new Literal( 'a' ) ),
161 new TexArray( new TexArray( new Literal( '\\hline ' ), new Literal( 'a'
162 ) ) ) ) );
163 $result = BaseParsing::matrix( $matrix, [], null, 'matrix', '002A' );
164 $this->assertStringContainsString( 'class="mwe-math-matrix-top"', $result );
165 $this->assertStringContainsString( '<mi>a</mi>', $result );
166 }
167
168 public function testComplicatedHline() {
169 $matrix = ( new TexVC() )->parse( '\\begin{array}{c}
170 \\hline a\\\\
171 \\hline 1\\\\
172 2\\\\
173 \\hline
174 \\end{array}' )[0];
175 $result = BaseParsing::matrix( $matrix, [], null, 'matrix', '002A' );
176 $this->assertStringContainsString( 'class="mwe-math-matrix-top"', $result );
177 $this->assertStringContainsString( 'class="mwe-math-matrix-top mwe-math-matrix-bottom"', $result );
178 }
179
180 public function testHandleOperatorName() {
181 $node = new Fun1(
182 '\\operatorname',
183 ( new Literal( 'sn' ) )
184 );
185 $result = BaseParsing::handleOperatorName( $node, [], [
186 "foundNamedFct" => [ true, true ]
187 ], 'operatorname' );
188 $this->assertStringContainsString( 'sn</mi>', $result );
189 $this->assertStringContainsString( '<mo>&#x2061;</mo>', $result );
190 }
191
192 public function testHandleOperatorLast() {
193 $node = new Fun1(
194 '\\operatorname',
195 ( new Literal( 'sn' ) )
196 );
197 $result = BaseParsing::handleOperatorName( $node, [], [
198 "foundNamedFct" => [ true, false ]
199 ], 'operatorname' );
200 $this->assertStringContainsString( 'sn</mi>', $result );
201 $this->assertStringNotContainsString( '<mo>&#x2061;</mo>', $result );
202 }
203
204 public function testColumnSpecs() {
205 $matrix = ( new TexVC() )->parse( '\\begin{array}{lcr}
206 z & = & a \\\\
207 f(x,y,z) & = & x + y + z
208 \\end{array}' )[0];
209 $result = BaseParsing::matrix( $matrix, [], null, 'matrix', '002A' );
210 $this->assertStringContainsString( 'left center right ', $result );
211 }
212
213 public function testNamedOperator() {
214 $node = new Literal( '\\gcd' );
215 $result = BaseParsing::namedOp( $node, [], [], '\\gcd' );
216 $this->assertStringContainsString( '>gcd</mi>', $result );
217 }
218
219 public function testSpace() {
220 $node = new Literal( '\\ ' );
221 $result = BaseParsing::macro( $node, [], [], '\\ ', '\\text{ }' );
222 $this->assertStringContainsString( '<mtext>&#160;</mtext>', $result );
223 }
224
225 public function testIgnoreMisplacedLimit() {
226 $node = new Literal( '\\limits ' );
227 $result = BaseParsing::limits( $node, [], [], '\\limits' );
228 $this->assertSame( '', $result, 'Misplaced limits should be ignored' );
229 }
230 }