aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/Twisted-12.2.0-py2.7-linux-x86_64.egg/twisted/lore/test/test_latex.py
blob: 21d5029c1e209c53f7f21703ab8096e8aaa0574b (plain)
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
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

"""
Tests for L{twisted.lore.latex}.
"""

import os.path
from xml.dom.minidom import Comment, Element, Text

from twisted.python.filepath import FilePath
from twisted.trial.unittest import TestCase
from twisted.lore.latex import LatexSpitter, getLatexText


class LatexHelperTests(TestCase):
    """
    Tests for free functions in L{twisted.lore.latex}.
    """
    def test_getLatexText(self):
        """
        L{getLatexText} calls the writer function with all of the text at or
        beneath the given node.  Non-ASCII characters are encoded using
        UTF-8.
        """
        node = Element('foo')
        text = Text()
        text.data = u"foo \N{SNOWMAN}"
        node.appendChild(text)
        result = []
        getLatexText(node, result.append)
        self.assertEqual(result, [u"foo \N{SNOWMAN}".encode('utf-8')])



class LatexSpitterTests(TestCase):
    """
    Tests for L{LatexSpitter}.
    """
    def setUp(self):
        self.filename = self.mktemp()
        self.output = []
        self.spitter = LatexSpitter(self.output.append, filename=self.filename)


    def test_head(self):
        """
        L{LatexSpitter.visitNode} writes out author information for each
        I{link} element with a I{rel} attribute set to I{author}.
        """
        head = Element('head')
        first = Element('link')
        first.setAttribute('rel', 'author')
        first.setAttribute('title', 'alice')
        second = Element('link')
        second.setAttribute('rel', 'author')
        second.setAttribute('href', 'http://example.com/bob')
        third = Element('link')
        third.setAttribute('rel', 'author')
        third.setAttribute('href', 'mailto:carol@example.com')
        head.appendChild(first)
        head.appendChild(second)
        head.appendChild(third)

        self.spitter.visitNode(head)

        self.assertEqual(
            ''.join(self.output),
            '\\author{alice \\and $<$http://example.com/bob$>$ \\and $<$carol@example.com$>$}')


    def test_skipComments(self):
        """
        L{LatexSpitter.visitNode} writes nothing to its output stream for
        comments.
        """
        self.spitter.visitNode(Comment('foo'))
        self.assertNotIn('foo', ''.join(self.output))


    def test_anchorListing(self):
        """
        L{LatexSpitter.visitNode} emits a verbatim block when it encounters a
        code listing (represented by an I{a} element with a I{listing} class).
        """
        path = FilePath(self.mktemp())
        path.setContent('foo\nbar\n')
        listing = Element('a')
        listing.setAttribute('class', 'listing')
        listing.setAttribute('href', path.path)
        self.spitter.visitNode(listing)
        self.assertEqual(
            ''.join(self.output),
            "\\begin{verbatim}\n"
            "foo\n"
            "bar\n"
            "\\end{verbatim}\\parbox[b]{\\linewidth}{\\begin{center} --- "
            "\\begin{em}temp\\end{em}\\end{center}}")


    def test_anchorListingSkipLines(self):
        """
        When passed an I{a} element with a I{listing} class and an I{skipLines}
        attribute, L{LatexSpitter.visitNode} emits a verbatim block which skips
        the indicated number of lines from the beginning of the source listing.
        """
        path = FilePath(self.mktemp())
        path.setContent('foo\nbar\n')
        listing = Element('a')
        listing.setAttribute('class', 'listing')
        listing.setAttribute('skipLines', '1')
        listing.setAttribute('href', path.path)
        self.spitter.visitNode(listing)
        self.assertEqual(
            ''.join(self.output),
            "\\begin{verbatim}\n"
            "bar\n"
            "\\end{verbatim}\\parbox[b]{\\linewidth}{\\begin{center} --- "
            "\\begin{em}temp\\end{em}\\end{center}}")


    def test_anchorRef(self):
        """
        L{LatexSpitter.visitNode} emits a footnote when it encounters an I{a}
        element with an I{href} attribute with a network scheme.
        """
        listing = Element('a')
        listing.setAttribute('href', 'http://example.com/foo')
        self.spitter.visitNode(listing)
        self.assertEqual(
            ''.join(self.output),
            "\\footnote{http://example.com/foo}")


    def test_anchorName(self):
        """
        When passed an I{a} element with a I{name} attribute,
        L{LatexSpitter.visitNode} emits a label.
        """
        listing = Element('a')
        listing.setAttribute('name', 'foo')
        self.spitter.visitNode(listing)
        self.assertEqual(
            ''.join(self.output),
            "\\label{%sHASHfoo}" % (
                os.path.abspath(self.filename).replace('\\', '/'),))