File: LoadPackages.py

package info (click to toggle)
plastex 3.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,132 kB
  • sloc: python: 23,341; xml: 18,076; javascript: 7,755; ansic: 46; makefile: 40; sh: 26
file content (101 lines) | stat: -rw-r--r-- 2,426 bytes parent folder | download | duplicates (2)
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
import sys
from pathlib import Path
from plasTeX.TeX import TeX, TeXDocument
from plasTeX.Context import Context

def test_builtin_packages():
    doc = TeXDocument()
    tex = TeX(doc)
    tex.input(r"""
        \documentclass{article}
        \usepackage{float}
        \begin{document}
          \floatstyle{ruled}
        \end{document}
        """)
    tex.parse()
    assert doc.getElementsByTagName('floatstyle')

def test_packages_dirs(tmpdir):
    tmpdir = Path(str(tmpdir))

    (tmpdir / "mypkg.py").write_text(
    r"""
from plasTeX import Command

class mycmd(Command):
    my_var = 'ok'
""")
    doc = TeXDocument()
    doc.config['general'].data['packages-dirs'].value = [str(tmpdir)]


    tex = TeX(doc)
    tex.input(r"""
        \documentclass{article}
        \usepackage{mypkg}
        \begin{document}
          \mycmd
        \end{document}
        """)
    tex.parse()
    assert doc.getElementsByTagName('mycmd')
    node = doc.getElementsByTagName('mycmd')[0]
    assert node.my_var == 'ok'


def test_packages_dirs_name_clash(tmpdir):
    tmpdir = Path(str(tmpdir))

    (tmpdir / "plasTeX.py").write_text(
    r"""
from plasTeX import Command

class mycmd(Command):
    my_var = 'ok'
""")
    doc = TeXDocument()
    doc.config['general'].data['packages-dirs'].value = [str(tmpdir)]


    tex = TeX(doc)
    assert not doc.context.loadPackage(tex, 'plasTeX')

def test_plugin_packages(tmpdir):
    tmpdir = Path(str(tmpdir))
    (tmpdir/'my_plugin'/'Packages').mkdir(parents=True)
    (tmpdir/'my_plugin'/'__init__.py').touch()
    (tmpdir/'my_plugin'/'Packages'/'mypkg.py').write_text(
    r"""
from plasTeX import Command

class mycmd(Command):
    my_var = 'ok'
""")
    sys.path.append(str(tmpdir))
    doc = TeXDocument()
    doc.config['general'].data['plugins'].value = ['my_plugin']

    tex = TeX(doc)
    tex.input(r"""
        \documentclass{article}
        \usepackage{mypkg}
        \begin{document}
          \mycmd
        \end{document}
        """)
    tex.parse()
    sys.path.pop()
    assert doc.getElementsByTagName('mycmd')
    node = doc.getElementsByTagName('mycmd')[0]
    assert node.my_var == 'ok'


def test_crazy_package_name():
    """
    This test tries to load a package whose name clashes with a name of
    the python standard library and does not exists in plasTeX.
    """
    ctx = Context()
    tex = TeX()
    assert not ctx.loadPackage(tex, 'rlcompleter')