Debian Codegraph @

tests/test_latex_formatter.py

raw
1 """
2 Pygments LaTeX formatter tests
3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5 :copyright: Copyright 2006-present by the Pygments team, see AUTHORS.
6 :license: BSD, see LICENSE for details.
7 """
8
9 import os
10 import tempfile
11 from os import path
12 from io import StringIO
13 from textwrap import dedent
14
15 import pytest
16
17 from pygments.formatters import LatexFormatter
18 from pygments.formatters.latex import LatexEmbeddedLexer
19 from pygments.lexers import PythonLexer, PythonConsoleLexer
20 from pygments.token import Token
21
22 TESTDIR = path.dirname(path.abspath(__file__))
23 TESTFILE = path.join(TESTDIR, 'test_latex_formatter.py')
24
25
26 def test_correct_output():
27 with open(TESTFILE, encoding='utf-8') as fp:
28 tokensource = list(PythonLexer().get_tokens(fp.read()))
29 hfmt = LatexFormatter(nowrap=True)
30 houtfile = StringIO()
31 hfmt.format(tokensource, houtfile)
32
33 assert r'\begin{Verbatim}' not in houtfile.getvalue()
34 assert r'\end{Verbatim}' not in houtfile.getvalue()
35
36
37 def test_valid_output():
38 with open(TESTFILE, encoding='utf-8') as fp:
39 tokensource = list(PythonLexer().get_tokens(fp.read()))
40 fmt = LatexFormatter(full=True, encoding='latin1')
41
42 handle, pathname = tempfile.mkstemp('.tex')
43 # place all output files in /tmp too
44 old_wd = os.getcwd()
45 os.chdir(os.path.dirname(pathname))
46 tfile = os.fdopen(handle, 'wb')
47 fmt.format(tokensource, tfile)
48 tfile.close()
49 try:
50 import subprocess
51 po = subprocess.Popen(['latex', '-interaction=nonstopmode',
52 pathname], stdout=subprocess.PIPE)
53 ret = po.wait()
54 output = po.stdout.read()
55 po.stdout.close()
56 except OSError as e:
57 # latex not available
58 pytest.skip(str(e))
59 else:
60 if ret:
61 print(output)
62 assert not ret, 'latex run reported errors'
63
64 os.unlink(pathname)
65 os.chdir(old_wd)
66
67
68 def test_embedded_lexer():
69 # Latex surrounded by '|' should be Escaped
70 lexer = LatexEmbeddedLexer('|', '|', PythonConsoleLexer())
71
72 # similar to gh-1516
73 src = dedent("""\
74 >>> x = 1
75 >>> y = mul(x, |$z^2$|) # these |pipes| are untouched
76 >>> y
77 |$1 + z^2$|""")
78
79 assert list(lexer.get_tokens(src)) == [
80 (Token.Generic.Prompt, '>>> '),
81 (Token.Name, 'x'),
82 (Token.Text, ' '),
83 (Token.Operator, '='),
84 (Token.Text, ' '),
85 (Token.Literal.Number.Integer, '1'),
86 (Token.Text.Whitespace, '\n'),
87 (Token.Generic.Prompt, '>>> '),
88 (Token.Name, 'y'),
89 (Token.Text, ' '),
90 (Token.Operator, '='),
91 (Token.Text, ' '),
92 (Token.Name, 'mul'),
93 (Token.Punctuation, '('),
94 (Token.Name, 'x'),
95 (Token.Punctuation, ','),
96 (Token.Text, ' '),
97 (Token.Escape, '$z^2$'),
98 (Token.Punctuation, ')'),
99 (Token.Text, ' '),
100 (Token.Comment.Single, '# these |pipes| are untouched'), # note: not Token.Escape
101 (Token.Text.Whitespace, '\n'),
102 (Token.Generic.Prompt, '>>> '),
103 (Token.Name, 'y'),
104 (Token.Text.Whitespace, '\n'),
105 (Token.Escape, '$1 + z^2$'),
106 (Token.Generic.Output, '\n'),
107 ]