Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / lore / test / test_latex.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 """
5 Tests for L{twisted.lore.latex}.
6 """
7
8 import os.path
9 from xml.dom.minidom import Comment, Element, Text
10
11 from twisted.python.filepath import FilePath
12 from twisted.trial.unittest import TestCase
13 from twisted.lore.latex import LatexSpitter, getLatexText
14
15
16 class LatexHelperTests(TestCase):
17     """
18     Tests for free functions in L{twisted.lore.latex}.
19     """
20     def test_getLatexText(self):
21         """
22         L{getLatexText} calls the writer function with all of the text at or
23         beneath the given node.  Non-ASCII characters are encoded using
24         UTF-8.
25         """
26         node = Element('foo')
27         text = Text()
28         text.data = u"foo \N{SNOWMAN}"
29         node.appendChild(text)
30         result = []
31         getLatexText(node, result.append)
32         self.assertEqual(result, [u"foo \N{SNOWMAN}".encode('utf-8')])
33
34
35
36 class LatexSpitterTests(TestCase):
37     """
38     Tests for L{LatexSpitter}.
39     """
40     def setUp(self):
41         self.filename = self.mktemp()
42         self.output = []
43         self.spitter = LatexSpitter(self.output.append, filename=self.filename)
44
45
46     def test_head(self):
47         """
48         L{LatexSpitter.visitNode} writes out author information for each
49         I{link} element with a I{rel} attribute set to I{author}.
50         """
51         head = Element('head')
52         first = Element('link')
53         first.setAttribute('rel', 'author')
54         first.setAttribute('title', 'alice')
55         second = Element('link')
56         second.setAttribute('rel', 'author')
57         second.setAttribute('href', 'http://example.com/bob')
58         third = Element('link')
59         third.setAttribute('rel', 'author')
60         third.setAttribute('href', 'mailto:carol@example.com')
61         head.appendChild(first)
62         head.appendChild(second)
63         head.appendChild(third)
64
65         self.spitter.visitNode(head)
66
67         self.assertEqual(
68             ''.join(self.output),
69             '\\author{alice \\and $<$http://example.com/bob$>$ \\and $<$carol@example.com$>$}')
70
71
72     def test_skipComments(self):
73         """
74         L{LatexSpitter.visitNode} writes nothing to its output stream for
75         comments.
76         """
77         self.spitter.visitNode(Comment('foo'))
78         self.assertNotIn('foo', ''.join(self.output))
79
80
81     def test_anchorListing(self):
82         """
83         L{LatexSpitter.visitNode} emits a verbatim block when it encounters a
84         code listing (represented by an I{a} element with a I{listing} class).
85         """
86         path = FilePath(self.mktemp())
87         path.setContent('foo\nbar\n')
88         listing = Element('a')
89         listing.setAttribute('class', 'listing')
90         listing.setAttribute('href', path.path)
91         self.spitter.visitNode(listing)
92         self.assertEqual(
93             ''.join(self.output),
94             "\\begin{verbatim}\n"
95             "foo\n"
96             "bar\n"
97             "\\end{verbatim}\\parbox[b]{\\linewidth}{\\begin{center} --- "
98             "\\begin{em}temp\\end{em}\\end{center}}")
99
100
101     def test_anchorListingSkipLines(self):
102         """
103         When passed an I{a} element with a I{listing} class and an I{skipLines}
104         attribute, L{LatexSpitter.visitNode} emits a verbatim block which skips
105         the indicated number of lines from the beginning of the source listing.
106         """
107         path = FilePath(self.mktemp())
108         path.setContent('foo\nbar\n')
109         listing = Element('a')
110         listing.setAttribute('class', 'listing')
111         listing.setAttribute('skipLines', '1')
112         listing.setAttribute('href', path.path)
113         self.spitter.visitNode(listing)
114         self.assertEqual(
115             ''.join(self.output),
116             "\\begin{verbatim}\n"
117             "bar\n"
118             "\\end{verbatim}\\parbox[b]{\\linewidth}{\\begin{center} --- "
119             "\\begin{em}temp\\end{em}\\end{center}}")
120
121
122     def test_anchorRef(self):
123         """
124         L{LatexSpitter.visitNode} emits a footnote when it encounters an I{a}
125         element with an I{href} attribute with a network scheme.
126         """
127         listing = Element('a')
128         listing.setAttribute('href', 'http://example.com/foo')
129         self.spitter.visitNode(listing)
130         self.assertEqual(
131             ''.join(self.output),
132             "\\footnote{http://example.com/foo}")
133
134
135     def test_anchorName(self):
136         """
137         When passed an I{a} element with a I{name} attribute,
138         L{LatexSpitter.visitNode} emits a label.
139         """
140         listing = Element('a')
141         listing.setAttribute('name', 'foo')
142         self.spitter.visitNode(listing)
143         self.assertEqual(
144             ''.join(self.output),
145             "\\label{%sHASHfoo}" % (
146                 os.path.abspath(self.filename).replace('\\', '/'),))