|
| 1 | +import unittest |
| 2 | + |
| 3 | +from contentcuration.utils.assessment.markdown import render_markdown |
| 4 | +from contentcuration.utils.assessment.qti import ElementTreeBase |
| 5 | + |
| 6 | + |
| 7 | +class TexMathTestMixin: |
| 8 | + """Mixin providing test methods for TexMath plugin tests""" |
| 9 | + |
| 10 | + def _assert_conversion(self, markdown_text: str, expected: str): |
| 11 | + """Override in subclasses to define assertion behavior""" |
| 12 | + raise NotImplementedError("Subclasses must implement _assert_conversion") |
| 13 | + |
| 14 | + def test_markdown_with_inline_math(self): |
| 15 | + """Test conversion of markdown with inline math to HTML + MathML""" |
| 16 | + |
| 17 | + markdown_text = ( |
| 18 | + "What is the answer to this *question*? $$x\cdot y=z^2$$" # noqa W605 |
| 19 | + ) |
| 20 | + expected = ( |
| 21 | + "<p>What is the answer to this <em>question</em>? " |
| 22 | + '<math display="inline">' |
| 23 | + "<mrow><mi>x</mi><mi>·</mi><mi>y</mi><mo>=</mo><msup><mi>z</mi><mn>2</mn></msup></mrow>" |
| 24 | + "</math></p>\n" |
| 25 | + ) |
| 26 | + |
| 27 | + self._assert_conversion(markdown_text, expected) |
| 28 | + |
| 29 | + def test_block_math(self): |
| 30 | + """Test conversion of block math""" |
| 31 | + |
| 32 | + markdown_text = ( |
| 33 | + "Here's an equation:\n\n$$E = mc^2$$\n\nThat's Einstein's formula." |
| 34 | + ) |
| 35 | + expected = ( |
| 36 | + "<p>Here's an equation:</p>\n" |
| 37 | + '<math display="block">' |
| 38 | + "<mrow><mi>E</mi><mo>=</mo><mi>m</mi><msup><mi>c</mi><mn>2</mn></msup></mrow>" |
| 39 | + "</math>" |
| 40 | + "<p>That's Einstein's formula.</p>\n" |
| 41 | + ) |
| 42 | + |
| 43 | + self._assert_conversion(markdown_text, expected) |
| 44 | + |
| 45 | + def test_multiple_math_expressions(self): |
| 46 | + """Test multiple math expressions in one document""" |
| 47 | + |
| 48 | + markdown_text = "First: $$a + b$$, then $$c \\times d$$, finally $$e^f$$." |
| 49 | + expected = ( |
| 50 | + "<p>First: " |
| 51 | + '<math display="inline"><mrow><mi>a</mi><mo>+</mo><mi>b</mi></mrow></math>' |
| 52 | + ", then " |
| 53 | + '<math display="inline"><mrow><mi>c</mi><mi>×</mi><mi>d</mi></mrow></math>' |
| 54 | + ", finally " |
| 55 | + '<math display="inline"><mrow><msup><mi>e</mi><mi>f</mi></msup></mrow></math>' |
| 56 | + ".</p>\n" |
| 57 | + ) |
| 58 | + |
| 59 | + self._assert_conversion(markdown_text, expected) |
| 60 | + |
| 61 | + def test_mixed_inline_and_block(self): |
| 62 | + """Test document with both inline and block math""" |
| 63 | + |
| 64 | + markdown_text = ( |
| 65 | + "This is inline math: $$a = b$$\n\n" |
| 66 | + "And this is block math:\n\n" |
| 67 | + "$$\\sum_{i=1}^{n} x_i = y$$\n\n" |
| 68 | + "Back to text with more inline: $$z^2$$" |
| 69 | + ) |
| 70 | + expected = ( |
| 71 | + "<p>This is inline math: " |
| 72 | + '<math display="inline"><mrow><mi>a</mi><mo>=</mo><mi>b</mi></mrow></math>' |
| 73 | + "</p>\n" |
| 74 | + "<p>And this is block math:</p>\n" |
| 75 | + '<math display="block">' |
| 76 | + "<mrow><msubsup><mo>∑</mo><mrow><mi>i</mi><mo>=</mo><mn>1</mn></mrow><mrow><mi>n</mi></mrow></msubsup>" |
| 77 | + "<msub><mi>x</mi><mi>i</mi></msub><mo>=</mo><mi>y</mi></mrow>" |
| 78 | + "</math>" |
| 79 | + "<p>Back to text with more inline: " |
| 80 | + '<math display="inline"><mrow><msup><mi>z</mi><mn>2</mn></msup></mrow></math>' |
| 81 | + "</p>\n" |
| 82 | + ) |
| 83 | + |
| 84 | + self._assert_conversion(markdown_text, expected) |
| 85 | + |
| 86 | + def test_no_math_content(self): |
| 87 | + """Test that regular markdown without math still works""" |
| 88 | + |
| 89 | + markdown_text = "This is just *regular* markdown with **bold** text." |
| 90 | + expected = "<p>This is just <em>regular</em> markdown with <strong>bold</strong> text.</p>\n" |
| 91 | + |
| 92 | + self._assert_conversion(markdown_text, expected) |
| 93 | + |
| 94 | + def test_simple_inline_math(self): |
| 95 | + """Test simple inline math expression""" |
| 96 | + |
| 97 | + markdown_text = "The variable $$x$$ is unknown." |
| 98 | + expected = ( |
| 99 | + "<p>The variable " |
| 100 | + '<math display="inline"><mrow><mi>x</mi></mrow></math>' |
| 101 | + " is unknown.</p>\n" |
| 102 | + ) |
| 103 | + |
| 104 | + self._assert_conversion(markdown_text, expected) |
| 105 | + |
| 106 | + def test_simple_block_math(self): |
| 107 | + """Test simple block math expression""" |
| 108 | + |
| 109 | + markdown_text = "$$y = mx + b$$" |
| 110 | + expected = ( |
| 111 | + '<math display="block">' |
| 112 | + "<mrow><mi>y</mi><mo>=</mo><mi>m</mi><mi>x</mi><mo>+</mo><mi>b</mi></mrow>" |
| 113 | + "</math>" |
| 114 | + ) |
| 115 | + |
| 116 | + self._assert_conversion(markdown_text, expected) |
| 117 | + |
| 118 | + |
| 119 | +class TestTexMathPlugin(TexMathTestMixin, unittest.TestCase): |
| 120 | + """Test direct markdown conversion: markdown → HTML+MathML""" |
| 121 | + |
| 122 | + def _assert_conversion(self, markdown_text: str, expected: str): |
| 123 | + """Test direct markdown to HTML+MathML conversion""" |
| 124 | + result = render_markdown(markdown_text) |
| 125 | + self.assertEqual(result, expected) |
| 126 | + |
| 127 | + |
| 128 | +class TestTexMathPluginRoundtrip(TexMathTestMixin, unittest.TestCase): |
| 129 | + """Test full roundtrip: markdown → HTML+MathML → Pydantic → string""" |
| 130 | + |
| 131 | + maxDiff = None |
| 132 | + |
| 133 | + def _assert_conversion(self, markdown_text: str, expected: str): |
| 134 | + """Test full roundtrip conversion via Pydantic objects""" |
| 135 | + result = render_markdown(markdown_text) |
| 136 | + |
| 137 | + # Parse to Pydantic objects and back to string |
| 138 | + parsed = ElementTreeBase.from_string(result) |
| 139 | + roundtrip_result = ( |
| 140 | + "".join(e.to_xml_string().strip() for e in parsed) |
| 141 | + if isinstance(parsed, list) |
| 142 | + else parsed.to_xml_string().strip() |
| 143 | + ) |
| 144 | + |
| 145 | + self.assertEqual(roundtrip_result, expected.replace("\n", "").strip()) |
0 commit comments