1<?php
2/**
3 * @group plugin_latexport
4 * @group plugins
5 */
6
7require_once DOKU_PLUGIN . 'latexport/_test/decorator_mock.php';
8require_once DOKU_PLUGIN . 'latexport/implementation/decorator_math.php';
9
10class DecoratorMathTest extends DokuWikiTest {
11
12
13    protected $pluginsEnabled = array('latexport', 'mathjax');
14	private $decoratorMock;
15	private $decoratorMath;
16
17    public static function setUpBeforeClass() {
18        parent::setUpBeforeClass();
19	}
20
21	public function setUp() {
22		$this->decoratorMock = new DecoratorMock();
23		$this->decoratorMath = new DecoratorMath($this->decoratorMock);
24    }
25
26    public function testPlacesADisplayFormulaWithDollarsIntoBeginAndEndEquation() {
27		$formula = "e = m \cdot e^2";
28		$this->decoratorMath->mathjax_content("$");
29		$this->decoratorMath->mathjax_content("$ $formula");
30		$this->decoratorMath->mathjax_content(" $$");
31		$this->decoratorMath->cdata("Hey");
32
33		$this->assertEquals(new CommandMathjaxContent("\\begin{equation}\r\n    $formula\r\n\\end{equation}\r\n"),
34				            $this->decoratorMock->nextCommand());
35		$this->assertEquals(new CommandCData('Hey'),
36		                    $this->decoratorMock->nextCommand());
37
38		$this->assertTrue($this->decoratorMock->noCommands(), "Should not have more commands");
39    }
40
41	public function testPlacesADisplayFormulaWithBracketsIntoBeginAndEndEquation() {
42		$formula = "e = m \cdot e^2";
43		$this->decoratorMath->mathjax_content("\\[");
44		$this->decoratorMath->mathjax_content(" $formula");
45		$this->decoratorMath->mathjax_content(" \\]");
46		$this->decoratorMath->cdata("Hey");
47
48		$this->assertEquals(new CommandMathjaxContent("\\begin{equation}\r\n    $formula\r\n\\end{equation}\r\n"),
49				            $this->decoratorMock->nextCommand());
50		$this->assertEquals(new CommandCData('Hey'),
51		                    $this->decoratorMock->nextCommand());
52
53		$this->assertTrue($this->decoratorMock->noCommands(), "Should not have more commands");
54	}
55
56	public function testPlacesAnInlineFormulaWithDollarBetweenParenthesis() {
57		$formula = "e = m \cdot e^2";
58		$this->decoratorMath->mathjax_content("$");
59		$this->decoratorMath->mathjax_content("$formula");
60		$this->decoratorMath->mathjax_content("$");
61		$this->decoratorMath->cdata(" Hey");
62
63		$this->assertEquals(new CommandMathjaxContent("\\($formula\\)"),
64				            $this->decoratorMock->nextCommand());
65		$this->assertEquals(new CommandCData(' Hey'),
66		                    $this->decoratorMock->nextCommand());
67
68		$this->assertTrue($this->decoratorMock->noCommands(), "Should not have more commands");
69	}
70
71	public function testPlacesAnInlineFormulaWithParenthesisBetweenParenthesis() {
72		$formula = "e = m \cdot e^2";
73		$this->decoratorMath->mathjax_content("\\(");
74		$this->decoratorMath->mathjax_content("$formula\\");
75		$this->decoratorMath->mathjax_content(")");
76		$this->decoratorMath->cdata(" Hey");
77
78		$this->assertEquals(new CommandMathjaxContent("\\($formula\\)"),
79				            $this->decoratorMock->nextCommand());
80		$this->assertEquals(new CommandCData(' Hey'),
81		                    $this->decoratorMock->nextCommand());
82
83		$this->assertTrue($this->decoratorMock->noCommands(), "Should not have more commands");
84	}
85
86    public function testLeavesAmsmathCommandsUntouched() {
87		$formula = "\\begin{align*}
88e^x & = 1 + x + \\frac{x^2}{2} + \\frac{x^3}{6} + \\cdots \\
89    & = \\sum_{n\geq 0} \\frac{x^n}{n!}
90\\end{align*}";
91		$this->decoratorMath->mathjax_content($formula);
92		$this->decoratorMath->cdata("Hey");
93
94		$this->assertEquals(new CommandMathjaxContent($formula),
95		                    $this->decoratorMock->nextCommand());
96		$this->assertEquals(new CommandCData('Hey'),
97		                    $this->decoratorMock->nextCommand());
98
99		$this->assertTrue($this->decoratorMock->noCommands(), "Should not have more commands");
100    }
101
102    public function testRemovesTheExplicitTagCommandFromInlineAndDisplay() {
103		$this->decoratorMath->mathjax_content("\\[ \\label{coser-b}\\tag{removeme} \\]");
104		$this->decoratorMath->cdata("Hey");
105		$this->decoratorMath->mathjax_content("$$ \\label{coser-b}\\tag{removeme} $$");
106		$this->decoratorMath->cdata("Hey");
107
108		$this->assertEquals(new CommandMathjaxContent("\\begin{equation}\r\n    \\label{coser-b}\r\n\\end{equation}\r\n"),
109		                    $this->decoratorMock->nextCommand());
110		$this->assertEquals(new CommandCData('Hey'),
111		                    $this->decoratorMock->nextCommand());
112
113		$this->assertEquals(new CommandMathjaxContent("\\begin{equation}\r\n    \\label{coser-b}\r\n\\end{equation}\r\n"),
114		                    $this->decoratorMock->nextCommand());
115		$this->assertEquals(new CommandCData('Hey'),
116		                    $this->decoratorMock->nextCommand());
117
118		$this->assertTrue($this->decoratorMock->noCommands(), "Should not have more commands");
119    }
120
121	public function testKeepsTheExplicitTagCommandFromAmsmath() {
122		$this->decoratorMath->mathjax_content("\\begin{align} \\label{coser-b}\\tag{removeme} \\end{align}");
123		$this->decoratorMath->cdata("Hey");
124
125		$this->assertEquals(new CommandMathjaxContent("\\begin{align} \\label{coser-b}\\tag{removeme} \\end{align}"),
126		                    $this->decoratorMock->nextCommand());
127		$this->assertEquals(new CommandCData('Hey'),
128		                    $this->decoratorMock->nextCommand());
129
130		$this->assertTrue($this->decoratorMock->noCommands(), "Should not have more commands");
131	}
132}
133?>
134