/plantuml/tests/test_functional.py

https://bitbucket.org/birkenfeld/sphinx-contrib · Python · 195 lines · 119 code · 22 blank · 54 comment · 9 complexity · 3fee7d2aefcf5034857b15ec850c2c96 MD5 · raw file

  1. import os, tempfile, shutil, glob
  2. from sphinx.application import Sphinx
  3. from nose.tools import *
  4. _fixturedir = os.path.join(os.path.dirname(__file__), 'fixture')
  5. _fakecmd = os.path.join(os.path.dirname(__file__), 'fakecmd.py')
  6. _fakeepstopdf = os.path.join(os.path.dirname(__file__), 'fakeepstopdf.py')
  7. def setup():
  8. global _tempdir, _srcdir, _outdir
  9. _tempdir = tempfile.mkdtemp()
  10. _srcdir = os.path.join(_tempdir, 'src')
  11. _outdir = os.path.join(_tempdir, 'out')
  12. os.mkdir(_srcdir)
  13. def teardown():
  14. shutil.rmtree(_tempdir)
  15. def readfile(fname):
  16. f = open(os.path.join(_outdir, fname), 'rb')
  17. try:
  18. return f.read()
  19. finally:
  20. f.close()
  21. def runsphinx(text, builder, confoverrides):
  22. f = open(os.path.join(_srcdir, 'index.rst'), 'w')
  23. try:
  24. f.write(text.encode('utf-8'))
  25. finally:
  26. f.close()
  27. app = Sphinx(_srcdir, _fixturedir, _outdir, _outdir, builder,
  28. confoverrides)
  29. app.build()
  30. def with_runsphinx(builder, **kwargs):
  31. confoverrides = {'plantuml': _fakecmd}
  32. confoverrides.update(kwargs)
  33. def wrapfunc(func):
  34. def test():
  35. src = '\n'.join(l[4:] for l in func.__doc__.splitlines()[2:])
  36. os.mkdir(_outdir)
  37. try:
  38. runsphinx(src, builder, confoverrides)
  39. func()
  40. finally:
  41. os.unlink(os.path.join(_srcdir, 'index.rst'))
  42. shutil.rmtree(_outdir)
  43. test.func_name = func.func_name
  44. return test
  45. return wrapfunc
  46. @with_runsphinx('html', plantuml_output_format='svg')
  47. def test_buildhtml_simple_with_svg():
  48. """Generate simple HTML
  49. .. uml::
  50. Hello
  51. """
  52. pngfiles = glob.glob(os.path.join(_outdir, '_images', 'plantuml-*.png'))
  53. assert len(pngfiles) == 1
  54. svgfiles = glob.glob(os.path.join(_outdir, '_images', 'plantuml-*.svg'))
  55. assert len(svgfiles) == 1
  56. assert '<img src="_images/plantuml' in readfile('index.html')
  57. assert '<object data="_images/plantuml' in readfile('index.html')
  58. pngcontent = readfile(pngfiles[0]).splitlines()
  59. assert '-pipe' in pngcontent[0]
  60. assert_equals('Hello', pngcontent[1][2:])
  61. svgcontent = readfile(svgfiles[0]).splitlines()
  62. assert '-tsvg' in svgcontent[0]
  63. assert_equals('Hello', svgcontent[1][2:])
  64. @with_runsphinx('html')
  65. def test_buildhtml_samediagram():
  66. """Same diagram should be same file
  67. .. uml::
  68. Hello
  69. .. uml::
  70. Hello
  71. """
  72. files = glob.glob(os.path.join(_outdir, '_images', 'plantuml-*.png'))
  73. assert len(files) == 1
  74. imgtags = [l for l in readfile('index.html').splitlines()
  75. if '<img src="_images/plantuml' in l]
  76. assert len(imgtags) == 2
  77. @with_runsphinx('html')
  78. def test_buildhtml_alt():
  79. """Generate HTML with alt specified
  80. .. uml::
  81. :alt: Foo <Bar>
  82. Hello
  83. """
  84. assert 'alt="Foo &lt;Bar&gt;"' in readfile('index.html')
  85. @with_runsphinx('html')
  86. def test_buildhtml_caption():
  87. """Generate HTML with caption specified
  88. .. uml::
  89. :caption: Caption with **bold** and *italic*
  90. Hello
  91. """
  92. assert 'Caption with <strong>bold</strong> and <em>italic</em>' in readfile('index.html')
  93. @with_runsphinx('html')
  94. def test_buildhtml_nonascii():
  95. u"""Generate simple HTML of non-ascii diagram
  96. .. uml::
  97. \u3042
  98. """
  99. files = glob.glob(os.path.join(_outdir, '_images', 'plantuml-*.png'))
  100. content = readfile(files[0]).splitlines()
  101. assert '-charset utf-8' in content[0]
  102. assert_equals(u'\u3042', content[1][2:].decode('utf-8'))
  103. @with_runsphinx('latex')
  104. def test_buildlatex_simple():
  105. """Generate simple LaTeX
  106. .. uml::
  107. Hello
  108. """
  109. files = glob.glob(os.path.join(_outdir, 'plantuml-*.png'))
  110. assert len(files) == 1
  111. assert r'\includegraphics{plantuml-' in readfile('plantuml_fixture.tex')
  112. content = readfile(files[0]).splitlines()
  113. assert '-pipe' in content[0]
  114. assert_equals('Hello', content[1][2:])
  115. @with_runsphinx('latex', plantuml_latex_output_format='eps')
  116. def test_buildlatex_simple_with_eps():
  117. """Generate simple LaTeX with EPS
  118. .. uml::
  119. Hello
  120. """
  121. files = glob.glob(os.path.join(_outdir, 'plantuml-*.eps'))
  122. assert len(files) == 1
  123. assert r'\includegraphics{plantuml-' in readfile('plantuml_fixture.tex')
  124. content = readfile(files[0]).splitlines()
  125. assert '-teps' in content[0]
  126. assert_equals('Hello', content[1][2:])
  127. @with_runsphinx('latex', plantuml_latex_output_format='pdf')
  128. def test_buildlatex_simple_with_pdf():
  129. """Generate simple LaTeX with PDF
  130. .. uml::
  131. Hello
  132. """
  133. epsfiles = glob.glob(os.path.join(_outdir, 'plantuml-*.eps'))
  134. pdffiles = glob.glob(os.path.join(_outdir, 'plantuml-*.pdf'))
  135. assert len(epsfiles) == 1
  136. assert len(pdffiles) == 1
  137. assert r'\includegraphics{plantuml-' in readfile('plantuml_fixture.tex')
  138. epscontent = readfile(epsfiles[0]).splitlines()
  139. assert '-teps' in epscontent[0]
  140. assert_equals('Hello', epscontent[1][2:])
  141. @with_runsphinx('pdf')
  142. def test_buildpdf_simple():
  143. """Generate simple PDF
  144. .. uml::
  145. Hello
  146. """
  147. epsfiles = glob.glob(os.path.join(_outdir, 'plantuml-*.eps'))
  148. pdffiles = glob.glob(os.path.join(_outdir, 'plantuml-*.pdf'))
  149. assert len(epsfiles) == 1
  150. assert len(pdffiles) == 1
  151. epscontent = readfile(epsfiles[0]).splitlines()
  152. assert '-teps' in epscontent[0]
  153. assert_equals('Hello', epscontent[1][2:])