1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
5 Tests for L{twisted.lore.latex}.
9 from xml.dom.minidom import Comment, Element, Text
11 from twisted.python.filepath import FilePath
12 from twisted.trial.unittest import TestCase
13 from twisted.lore.latex import LatexSpitter, getLatexText
16 class LatexHelperTests(TestCase):
18 Tests for free functions in L{twisted.lore.latex}.
20 def test_getLatexText(self):
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
28 text.data = u"foo \N{SNOWMAN}"
29 node.appendChild(text)
31 getLatexText(node, result.append)
32 self.assertEqual(result, [u"foo \N{SNOWMAN}".encode('utf-8')])
36 class LatexSpitterTests(TestCase):
38 Tests for L{LatexSpitter}.
41 self.filename = self.mktemp()
43 self.spitter = LatexSpitter(self.output.append, filename=self.filename)
48 L{LatexSpitter.visitNode} writes out author information for each
49 I{link} element with a I{rel} attribute set to I{author}.
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)
65 self.spitter.visitNode(head)
69 '\\author{alice \\and $<$http://example.com/bob$>$ \\and $<$carol@example.com$>$}')
72 def test_skipComments(self):
74 L{LatexSpitter.visitNode} writes nothing to its output stream for
77 self.spitter.visitNode(Comment('foo'))
78 self.assertNotIn('foo', ''.join(self.output))
81 def test_anchorListing(self):
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).
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)
97 "\\end{verbatim}\\parbox[b]{\\linewidth}{\\begin{center} --- "
98 "\\begin{em}temp\\end{em}\\end{center}}")
101 def test_anchorListingSkipLines(self):
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.
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)
115 ''.join(self.output),
116 "\\begin{verbatim}\n"
118 "\\end{verbatim}\\parbox[b]{\\linewidth}{\\begin{center} --- "
119 "\\begin{em}temp\\end{em}\\end{center}}")
122 def test_anchorRef(self):
124 L{LatexSpitter.visitNode} emits a footnote when it encounters an I{a}
125 element with an I{href} attribute with a network scheme.
127 listing = Element('a')
128 listing.setAttribute('href', 'http://example.com/foo')
129 self.spitter.visitNode(listing)
131 ''.join(self.output),
132 "\\footnote{http://example.com/foo}")
135 def test_anchorName(self):
137 When passed an I{a} element with a I{name} attribute,
138 L{LatexSpitter.visitNode} emits a label.
140 listing = Element('a')
141 listing.setAttribute('name', 'foo')
142 self.spitter.visitNode(listing)
144 ''.join(self.output),
145 "\\label{%sHASHfoo}" % (
146 os.path.abspath(self.filename).replace('\\', '/'),))