master
ブランチ master
LittleBeast / src / Std / Test / LibLatex.php
6.8 KB、137行 生 Blame 履歴
1<?php
2/*************************************************************
3# 076 License
4
5Copyright (c) テクニカル諏訪子
6
7Permission is hereby granted to any person obtaining a copy of the software
8Little Beast (the "Software") to use, modify, merge, copy, publish, distribute,
9sublicense, and/or sell copies of the Software, subject to the following conditions:
10
11 1. **Origin Attribution**:
12 - You must not misrepresent the origin of the Software; you must not claim
13 you created the original Software.
14 - If the Software is used in a product, you must either:
15 a. Provide clear attribution in the product's documentation, user interface,
16 or other visible areas, **OR**
17 b. Pay the original developers a fee they specify in writing.
18 2. **Usage Restriction**:
19 - The Software, or any derivative works, dependencies, or libraries
20 incorporating it, must not be used for censorship or to suppress freedom of
21 speech, expression, or creativity. Prohibited uses include, but are not
22 limited to:
23 - Censorship of so-called "hate speech", visuals, non-mainstream opinions,
24 ideas, or objective reality.
25 - Tools or systems designed to restrict access to information or
26 artistic works.
27 3. **Notice Preservation**:
28 - This license and the above copyright notice must remain intact in all copies
29 of the source code.
30
31THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
32INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
33PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
34HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
35CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
36OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 */
38namespace Std\Test;
39
40require_once __DIR__.'/../../../autoload.php';
41
42use Std\Lib\Tester;
43use Std\Lib\Latex;
44
45$test = new Tester([
46 'colorOutput' => true,
47 'verboseOutput' => true
48]);
49
50// <math>の中身だけ
51$inner = function(string $tex, bool $display = false): string {
52 $html = (new Latex())->render($tex, $display);
53 return preg_replace('#^<math[^>]*>|</math>$#', '', $html);
54};
55
56$test->describe('LaTeXからMathMLへの変換', function($test) use ($inner): void {
57 $test->it('<math>要素で囲み、元のLaTeXをalttextに入れるはず', function($test): void {
58 $test->assertEquals('<math alttext="x"><mi>x</mi></math>', (new Latex())->render('x'));
59 $test->assertEquals('<math display="block" alttext="x"><mi>x</mi></math>', (new Latex())->render('x', true));
60 $test->assertStringContains('alttext="a &lt; b"', (new Latex())->render('a < b'));
61 });
62
63 $test->it('識別子・数字・演算子を区別するはず', function($test) use ($inner): void {
64 $test->assertEquals('<mrow><mi>a</mi><mo>+</mo><mn>12.5</mn></mrow>', $inner('a + 12.5'));
65 $test->assertEquals('<mrow><mi>a</mi><mo>&lt;</mo><mi>b</mi></mrow>', $inner('a < b'));
66 });
67
68 $test->it('上付き・下付きを変換するはず', function($test) use ($inner): void {
69 $test->assertEquals('<msup><mi>x</mi><mn>2</mn></msup>', $inner('x^2'));
70 $test->assertEquals('<msubsup><mi>a</mi><mi>i</mi><mn>2</mn></msubsup>', $inner('a_i^2'));
71 });
72
73 $test->it('分数と根号を変換するはず', function($test) use ($inner): void {
74 $test->assertEquals('<mfrac><mrow><mi>a</mi></mrow><mrow><mi>b</mi></mrow></mfrac>', $inner('\frac{a}{b}'));
75 $test->assertEquals('<msqrt><mrow><mi>x</mi></mrow></msqrt>', $inner('\sqrt{x}'));
76 $test->assertEquals('<mroot><mrow><mi>x</mi></mrow><mn>3</mn></mroot>', $inner('\sqrt[3]{x}'));
77 });
78
79 $test->it('ギリシャ文字と記号を変換するはず', function($test) use ($inner): void {
80 $test->assertEquals('<mrow><mi>α</mi><mo>+</mo><mi>β</mi></mrow>', $inner('\alpha + \beta'));
81 $test->assertStringContains('<mo>→</mo>', $inner('x \to 0'));
82 $test->assertStringContains('<mo>≤</mo>', $inner('a \leq b'));
83 });
84
85 $test->it('関数名を立体にし、細い空白を入れるはず', function($test) use ($inner): void {
86 $test->assertEquals('<mrow><mi>sin</mi><mspace width="0.1667em"></mspace><mi>x</mi></mrow>', $inner('\sin x'));
87 $test->assertStringContains('<mi>tr</mi>', $inner('\operatorname{tr} A'));
88 });
89
90 $test->it('大型演算子と極限を変換するはず', function($test) use ($inner): void {
91 $test->assertStringContains('<msubsup><mo largeop="true" movablelimits="true">∑</mo>', $inner('\sum_{i=1}^{n} i'));
92 $test->assertStringContains('<msub><mo movablelimits="true" form="prefix">lim</mo>', $inner('\lim_{x \to 0} f'));
93 });
94
95 $test->it('行列と括弧を変換するはず', function($test) use ($inner): void {
96 $m = $inner('\begin{pmatrix} 1 & 2 \\\\ 3 & 4 \end{pmatrix}');
97 $test->assertStringContains('<mtable><mtr><mtd><mn>1</mn></mtd><mtd><mn>2</mn></mtd></mtr><mtr><mtd><mn>3</mn></mtd><mtd><mn>4</mn></mtd></mtr></mtable>', $m);
98 $test->assertStringContains('form="prefix">(</mo>', $m);
99 $test->assertStringContains('form="postfix">)</mo>', $m);
100 $test->assertStringContains('form="prefix">(</mo><mi>x</mi>', $inner('\left( x \right)'));
101 });
102
103 $test->it('文字の種類とアクセントを変換するはず', function($test) use ($inner): void {
104 $test->assertEquals('<mrow><mi>𝐯</mi></mrow>', $inner('\mathbf{v}'));
105 $test->assertStringContains('<mi mathvariant="normal">d</mi>', $inner('\mathrm{d}x'));
106 $test->assertStringContains('<mover accent="true">', $inner('\vec{v}'));
107 $test->assertStringContains('<mtext>if </mtext>', $inner('\text{if } x'));
108 });
109
110 $test->it('HTMLをエスケープするはず', function($test) use ($inner): void {
111 $out = $inner('\text{<script>alert(1)</script>}');
112 $test->assertFalse(str_contains($out, '<script>'));
113 $test->assertStringContains('&lt;script&gt;', $out);
114 });
115
116 $test->it('未対応のコマンドを報告するはず', function($test): void {
117 $l = new Latex();
118 $test->assertStringContains('<merror><mtext>\foo</mtext></merror>', $l->render('\foo'));
119 $test->assertEquals(['\foo'], $l->getErrors());
120 $l->render('x');
121 $test->assertEquals([], $l->getErrors());
122 $l->render('\left( x');
123 $test->assertCount(1, $l->getErrors());
124 });
125
126 $test->it('不完全な入力でも止まらないはず', function($test): void {
127 $l = new Latex();
128 foreach (['\frac{a', '{{{', '}', '\sqrt[', '^^', 'x^', '\\', '\begin{pmatrix} 1', str_repeat('{', 2000).'x'] as $tex) {
129 $start = microtime(true);
130 $out = $l->render($tex);
131 $test->assertTrue(str_starts_with($out, '<math') && str_ends_with($out, '</math>'), "「{$tex}」");
132 $test->assertTrue(microtime(true) - $start < 1, "「".substr($tex, 0, 20)."」に時間が掛かり過ぎ");
133 }
134 });
135});
136
137$test->printSummary();