-
Notifications
You must be signed in to change notification settings - Fork 40
/
test_functional.py
401 lines (291 loc) · 10.2 KB
/
test_functional.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import glob
import os
import re
import tempfile
import shutil
import sys
import unittest
from sphinx.application import Sphinx
_fixturedir = os.path.join(os.path.dirname(__file__), 'fixture')
_fakecmd = os.path.join(os.path.dirname(__file__), 'fakecmd.py')
def setup_module():
global _tempdir, _srcdir, _outdir
_tempdir = tempfile.mkdtemp()
_srcdir = os.path.join(_tempdir, 'src')
_outdir = os.path.join(_tempdir, 'out')
os.mkdir(_srcdir)
def teardown_module():
shutil.rmtree(_tempdir)
def readfile(fname):
f = open(os.path.join(_outdir, fname), 'rb')
try:
return f.read()
finally:
f.close()
def runsphinx(text, builder, confoverrides):
f = open(os.path.join(_srcdir, 'index.rst'), 'wb')
try:
f.write(text.encode('utf-8'))
finally:
f.close()
app = Sphinx(_srcdir, _fixturedir, _outdir, _outdir, builder,
confoverrides, status=sys.stdout, warning=sys.stdout)
app.build()
def with_runsphinx(builder, **kwargs):
confoverrides = {'plantuml': [sys.executable, _fakecmd]}
confoverrides.update(kwargs)
def wrapfunc(func):
def test():
if builder == 'pdf':
try:
import rst2pdf
rst2pdf.__file__
except ImportError:
raise unittest.SkipTest
src = '\n'.join(l[4:] for l in func.__doc__.splitlines()[2:])
os.mkdir(_outdir)
try:
runsphinx(src, builder, confoverrides)
func()
finally:
os.unlink(os.path.join(_srcdir, 'index.rst'))
shutil.rmtree(_outdir)
test.__name__ = func.__name__
return test
return wrapfunc
@with_runsphinx('html', plantuml_output_format='svg')
def test_buildhtml_simple_with_svg():
"""Generate simple HTML
.. uml::
Hello
"""
pngfiles = glob.glob(os.path.join(_outdir, '_images', 'plantuml-*.png'))
assert len(pngfiles) == 1
svgfiles = glob.glob(os.path.join(_outdir, '_images', 'plantuml-*.svg'))
assert len(svgfiles) == 1
assert b'<img src="_images/plantuml' in readfile('index.html')
assert b'<object data="_images/plantuml' in readfile('index.html')
pngcontent = readfile(pngfiles[0]).splitlines()
assert b'-pipe' in pngcontent[0]
assert pngcontent[1][2:] == b'Hello'
svgcontent = readfile(svgfiles[0]).splitlines()
assert b'-tsvg' in svgcontent[0]
assert svgcontent[1][2:] == b'Hello'
@with_runsphinx('html', plantuml_output_format='none')
def test_buildhtml_no_output():
"""Generate simple HTML with uml directive disabled
.. uml::
Hello
"""
assert b'<img ' not in readfile('index.html')
@with_runsphinx('html')
def test_buildhtml_samediagram():
"""Same diagram should be same file
.. uml::
Hello
.. uml::
Hello
"""
files = glob.glob(os.path.join(_outdir, '_images', 'plantuml-*.png'))
assert len(files) == 1
imgtags = [l for l in readfile('index.html').splitlines()
if b'<img src="_images/plantuml' in l]
assert len(imgtags) == 2
@with_runsphinx('html')
def test_buildhtml_alt():
"""Generate HTML with alt specified
.. uml::
:alt: Foo <Bar>
Hello
"""
assert b'alt="Foo <Bar>"' in readfile('index.html')
@with_runsphinx('html')
def test_buildhtml_caption():
"""Generate HTML with caption specified
.. uml::
:caption: Caption with **bold** and *italic*
Hello
"""
assert (b'Caption with <strong>bold</strong> and <em>italic</em>'
in readfile('index.html'))
@with_runsphinx('html')
def test_buildhtml_name():
"""Generate HTML with name specified
.. uml::
:caption: Caption
:name: label
Hello
"""
re.search(br'<div class="figure[^"]*" id="label">', readfile('index.html'))
@with_runsphinx('html')
def test_buildhtml_nonascii():
u"""Generate simple HTML of non-ascii diagram
.. uml::
\u3042
"""
files = glob.glob(os.path.join(_outdir, '_images', 'plantuml-*.png'))
content = readfile(files[0]).splitlines()
assert b'-charset utf-8' in content[0]
assert content[1][2:].decode('utf-8') == u'\u3042'
@with_runsphinx('html', plantuml_batch_size=2)
def test_buildhtml_in_batches():
"""Render in batches
.. uml::
Hello
.. uml::
Hello!
.. uml::
@startuml
Hello!!
@enduml
.. uml::
!include seq.ja.uml
"""
puml_files = glob.glob(os.path.join(_outdir, '_plantuml', '*', '*.puml'))
assert len(puml_files) == 3
puml_contents = [readfile(f).splitlines() for f in puml_files]
assert all(len(lines) == 3 for lines in puml_contents)
assert all(lines[0] == b'@startuml' for lines in puml_contents)
assert all(lines[-1] == b'@enduml' for lines in puml_contents)
assert (sorted(lines[1] for lines in puml_contents)
== [b'Hello', b'Hello!', b'Hello!!'])
# batches: [2, 1], excluded: 1
png_files = glob.glob(os.path.join(_outdir, '_plantuml', '*', '*.png'))
assert len(png_files) == 4
png_commands = [readfile(f).splitlines()[0] for f in png_files]
assert len(set(png_commands)) == 3
assert sum(b'-pipe' in cmd for cmd in set(png_commands)) == 1
assert sorted(sum(c.endswith(b'.puml') for c in cmd.split())
for cmd in set(png_commands)) == [0, 1, 2]
@with_runsphinx('latex')
def test_buildlatex_simple():
"""Generate simple LaTeX
.. uml::
Hello
"""
files = glob.glob(os.path.join(_outdir, 'plantuml-*.png'))
assert len(files) == 1
assert re.search(br'\\(sphinx)?includegraphics\{+plantuml-',
readfile('plantuml_fixture.tex'))
content = readfile(files[0]).splitlines()
assert b'-pipe' in content[0]
assert content[1][2:] == b'Hello'
@with_runsphinx('latex', plantuml_latex_output_format='eps')
def test_buildlatex_simple_with_eps():
"""Generate simple LaTeX with EPS
.. uml::
Hello
"""
files = glob.glob(os.path.join(_outdir, 'plantuml-*.eps'))
assert len(files) == 1
assert re.search(br'\\(sphinx)?includegraphics\{+plantuml-',
readfile('plantuml_fixture.tex'))
content = readfile(files[0]).splitlines()
assert b'-teps' in content[0]
assert content[1][2:] == b'Hello'
@with_runsphinx('latex', plantuml_latex_output_format='tikz')
def test_buildlatex_simple_with_tikz():
"""Generate simple LaTeX with TikZ
.. uml::
Hello
"""
files = glob.glob(os.path.join(_outdir, 'plantuml-*.latex'))
assert len(files) == 1
assert re.search(br'\\input\{+plantuml-',
readfile('plantuml_fixture.tex'))
content = readfile(files[0]).splitlines()
assert b'-tlatex:nopreamble' in content[0]
assert content[1][2:] == b'Hello'
@with_runsphinx('latex', plantuml_latex_output_format='tikz')
def test_buildlatex_simple_scale_with_tikz():
"""Generate simple LaTeX with TikZ
.. uml::
:scale: 20%
Hello
"""
assert re.search(br'\\adjustbox\{scale=0.2\}\{\\input\{+plantuml-',
readfile('plantuml_fixture.tex'))
@with_runsphinx('latex', plantuml_latex_output_format='tikz')
def test_buildlatex_simple_width_with_tikz():
"""Generate simple LaTeX with TikZ
.. uml::
:width: 50mm
Hello
"""
assert re.search(br'\\adjustbox\{width=50mm\}\{\\input\{+plantuml-',
readfile('plantuml_fixture.tex'))
@with_runsphinx('latex', plantuml_latex_output_format='tikz')
def test_buildlatex_simple_height_with_tikz():
"""Generate simple LaTeX with TikZ
.. uml::
:height: 50mm
Hello
"""
assert re.search(br'\\adjustbox\{height=50mm\}\{\\input\{+plantuml-',
readfile('plantuml_fixture.tex'))
@with_runsphinx('latex', plantuml_latex_output_format='tikz')
def test_buildlatex_simple_max_width_with_tikz():
"""Generate simple LaTeX with TikZ
.. uml::
:max-width: 50mm
Hello
"""
assert re.search(br'\\adjustbox\{max width=50mm\}\{\\input\{+plantuml-',
readfile('plantuml_fixture.tex'))
@with_runsphinx('latex', plantuml_latex_output_format='pdf')
def test_buildlatex_simple_with_pdf():
"""Generate simple LaTeX with PDF
.. uml::
Hello
"""
epsfiles = glob.glob(os.path.join(_outdir, 'plantuml-*.eps'))
pdffiles = glob.glob(os.path.join(_outdir, 'plantuml-*.pdf'))
assert len(epsfiles) == 1
assert len(pdffiles) == 1
assert re.search(br'\\(sphinx)?includegraphics\{+plantuml-',
readfile('plantuml_fixture.tex'))
epscontent = readfile(epsfiles[0]).splitlines()
assert b'-teps' in epscontent[0]
assert epscontent[1][2:] == b'Hello'
@with_runsphinx('latex', plantuml_latex_output_format='none')
def test_buildlatex_no_output():
"""Generate simple LaTeX with uml directive disabled
.. uml::
Hello
"""
assert not re.search(br'\\(sphinx)?includegraphics\{+plantuml-',
readfile('plantuml_fixture.tex'))
@with_runsphinx('latex')
def test_buildlatex_with_caption():
"""Generate LaTeX with caption
.. uml::
:caption: Hello UML
Hello
"""
out = readfile('plantuml_fixture.tex')
assert re.search(br'\\caption\{\s*Hello UML\s*\}', out)
assert re.search(br'\\begin\{figure\}\[htbp\]', out)
assert not re.search(br'\\begin\{flushNone', out) # issue #136
@with_runsphinx('latex')
def test_buildlatex_with_align():
"""Generate LaTeX with caption
.. uml::
:align: right
Hello
"""
out = readfile('plantuml_fixture.tex')
assert (re.search(br'\\begin\{figure\}\[htbp\]\\begin\{flushright\}', out)
or re.search(br'\\begin\{wrapfigure\}\{r\}', out))
@with_runsphinx('pdf')
def test_buildpdf_simple():
"""Generate simple PDF
.. uml::
Hello
"""
epsfiles = glob.glob(os.path.join(_outdir, 'plantuml-*.eps'))
pdffiles = glob.glob(os.path.join(_outdir, 'plantuml-*.pdf'))
assert len(epsfiles) == 1
assert len(pdffiles) == 1
epscontent = readfile(epsfiles[0]).splitlines()
assert b'-teps' in epscontent[0]
assert epscontent[1][2:] == b'Hello'