diff --git a/tests/test_omml_fraction_fix.py b/tests/test_omml_fraction_fix.py new file mode 100644 index 00000000..54a32f41 --- /dev/null +++ b/tests/test_omml_fraction_fix.py @@ -0,0 +1,148 @@ +""" +Test for OMML fraction processing with missing fPr elements +""" + +import lxml.etree as ET +from docling.backend.docx.latex.omml import oMath2Latex, OMML_NS + + +def test_omml_fraction_missing_fpr(): + """Test that fractions with missing fPr elements are handled gracefully""" + + # Create an OMML fraction without fPr element (this would cause KeyError before fix) + omml_xml = f''' + + + + + a + + + + + b + + + + + ''' + + # Parse and process the OMML - this should not raise KeyError + root = ET.fromstring(omml_xml) + result = oMath2Latex(root) + latex_result = str(result) + + # Should produce default fraction formatting + assert "\\frac{a}{b}" in latex_result + + +def test_omml_fraction_with_fpr(): + """Test that fractions with fPr elements still work correctly""" + + # Create an OMML fraction with fPr element + omml_xml = f''' + + + + + + + + x + + + + + y + + + + + ''' + + # Parse and process the OMML + root = ET.fromstring(omml_xml) + result = oMath2Latex(root) + latex_result = str(result) + + # Should produce fraction formatting with fPr properties + assert "\\frac{x}{y}" in latex_result + + +def test_omml_fraction_missing_components(): + """Test that fractions with missing numerator or denominator use placeholders""" + + # Create an OMML fraction with missing numerator + omml_xml_missing_num = f''' + + + + + b + + + + + ''' + + # Parse and process the OMML + root = ET.fromstring(omml_xml_missing_num) + result = oMath2Latex(root) + latex_result = str(result) + + # Should use placeholder for missing numerator + assert "formula_skipped" in latex_result + assert "b" in latex_result + + +def test_omml_complex_equation_with_missing_fpr(): + """Test that complex equations with missing fPr continue processing""" + + # Create a complex equation with a fraction missing fPr + omml_xml = f''' + + + x = + + + + + a + b + + + + + c + + + + + + y + + + ''' + + # Parse and process the OMML + root = ET.fromstring(omml_xml) + result = oMath2Latex(root) + latex_result = str(result) + + # Should process the entire equation correctly + assert "x =" in latex_result + assert "\\frac{a + b}{c}" in latex_result + assert "+ y" in latex_result + + +if __name__ == "__main__": + import logging + + # Set up logging to capture warnings + logging.basicConfig(level=logging.WARNING) + + # Run all tests + test_omml_fraction_missing_fpr() + test_omml_fraction_with_fpr() + test_omml_fraction_missing_components() + test_omml_complex_equation_with_missing_fpr() + + print("All OMML fraction tests passed!") \ No newline at end of file