test_latextools.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # encoding: utf-8
  2. """Tests for IPython.utils.path.py"""
  3. # Copyright (c) IPython Development Team.
  4. # Distributed under the terms of the Modified BSD License.
  5. try:
  6. from unittest.mock import patch
  7. except ImportError:
  8. from mock import patch
  9. import nose.tools as nt
  10. from IPython.lib import latextools
  11. from IPython.testing.decorators import onlyif_cmds_exist, skipif_not_matplotlib
  12. from IPython.utils.process import FindCmdError
  13. def test_latex_to_png_dvipng_fails_when_no_cmd():
  14. """
  15. `latex_to_png_dvipng` should return None when there is no required command
  16. """
  17. for command in ['latex', 'dvipng']:
  18. yield (check_latex_to_png_dvipng_fails_when_no_cmd, command)
  19. def check_latex_to_png_dvipng_fails_when_no_cmd(command):
  20. def mock_find_cmd(arg):
  21. if arg == command:
  22. raise FindCmdError
  23. with patch.object(latextools, "find_cmd", mock_find_cmd):
  24. nt.assert_equal(latextools.latex_to_png_dvipng("whatever", True),
  25. None)
  26. @onlyif_cmds_exist('latex', 'dvipng')
  27. def test_latex_to_png_dvipng_runs():
  28. """
  29. Test that latex_to_png_dvipng just runs without error.
  30. """
  31. def mock_kpsewhich(filename):
  32. nt.assert_equal(filename, "breqn.sty")
  33. return None
  34. for (s, wrap) in [(u"$$x^2$$", False), (u"x^2", True)]:
  35. yield (latextools.latex_to_png_dvipng, s, wrap)
  36. with patch.object(latextools, "kpsewhich", mock_kpsewhich):
  37. yield (latextools.latex_to_png_dvipng, s, wrap)
  38. @skipif_not_matplotlib
  39. def test_latex_to_png_mpl_runs():
  40. """
  41. Test that latex_to_png_mpl just runs without error.
  42. """
  43. def mock_kpsewhich(filename):
  44. nt.assert_equal(filename, "breqn.sty")
  45. return None
  46. for (s, wrap) in [("$x^2$", False), ("x^2", True)]:
  47. yield (latextools.latex_to_png_mpl, s, wrap)
  48. with patch.object(latextools, "kpsewhich", mock_kpsewhich):
  49. yield (latextools.latex_to_png_mpl, s, wrap)
  50. @skipif_not_matplotlib
  51. def test_latex_to_html():
  52. img = latextools.latex_to_html("$x^2$")
  53. nt.assert_in("data:image/png;base64,iVBOR", img)
  54. def test_genelatex_no_wrap():
  55. """
  56. Test genelatex with wrap=False.
  57. """
  58. def mock_kpsewhich(filename):
  59. assert False, ("kpsewhich should not be called "
  60. "(called with {0})".format(filename))
  61. with patch.object(latextools, "kpsewhich", mock_kpsewhich):
  62. nt.assert_equal(
  63. '\n'.join(latextools.genelatex("body text", False)),
  64. r'''\documentclass{article}
  65. \usepackage{amsmath}
  66. \usepackage{amsthm}
  67. \usepackage{amssymb}
  68. \usepackage{bm}
  69. \pagestyle{empty}
  70. \begin{document}
  71. body text
  72. \end{document}''')
  73. def test_genelatex_wrap_with_breqn():
  74. """
  75. Test genelatex with wrap=True for the case breqn.sty is installed.
  76. """
  77. def mock_kpsewhich(filename):
  78. nt.assert_equal(filename, "breqn.sty")
  79. return "path/to/breqn.sty"
  80. with patch.object(latextools, "kpsewhich", mock_kpsewhich):
  81. nt.assert_equal(
  82. '\n'.join(latextools.genelatex("x^2", True)),
  83. r'''\documentclass{article}
  84. \usepackage{amsmath}
  85. \usepackage{amsthm}
  86. \usepackage{amssymb}
  87. \usepackage{bm}
  88. \usepackage{breqn}
  89. \pagestyle{empty}
  90. \begin{document}
  91. \begin{dmath*}
  92. x^2
  93. \end{dmath*}
  94. \end{document}''')
  95. def test_genelatex_wrap_without_breqn():
  96. """
  97. Test genelatex with wrap=True for the case breqn.sty is not installed.
  98. """
  99. def mock_kpsewhich(filename):
  100. nt.assert_equal(filename, "breqn.sty")
  101. return None
  102. with patch.object(latextools, "kpsewhich", mock_kpsewhich):
  103. nt.assert_equal(
  104. '\n'.join(latextools.genelatex("x^2", True)),
  105. r'''\documentclass{article}
  106. \usepackage{amsmath}
  107. \usepackage{amsthm}
  108. \usepackage{amssymb}
  109. \usepackage{bm}
  110. \pagestyle{empty}
  111. \begin{document}
  112. $$x^2$$
  113. \end{document}''')