/plantuml/tests/test_functional.py

https://bitbucket.org/birkenfeld/sphinx-contrib/
Python | 230 lines | 137 code | 25 blank | 68 comment | 9 complexity | cf35a708cda7d4c50ff4b6073fbbbff3 MD5 | raw file

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