1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
6 Tests for L{twisted.lore.man2lore}.
9 from StringIO import StringIO
11 from twisted.trial.unittest import TestCase
13 from twisted.lore.man2lore import ManConverter
16 _TRANSITIONAL_XHTML_DTD = ("""\
18 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
19 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
23 class ManConverterTestCase(TestCase):
25 Tests for L{ManConverter}.
30 Build instance variables useful for tests.
32 @ivar converter: a L{ManConverter} to be used during tests.
34 self.converter = ManConverter()
37 def assertConvert(self, inputLines, expectedOutput):
39 Helper method to check conversion from a man page to a Lore output.
41 @param inputLines: lines of the manpages.
42 @type inputLines: C{list}
44 @param expectedOutput: expected Lore content.
45 @type expectedOutput: C{str}
47 inputFile = StringIO()
48 for line in inputLines:
49 inputFile.write(line + '\n')
51 outputFile = StringIO()
52 self.converter.convert(inputFile, outputFile)
54 outputFile.getvalue(), _TRANSITIONAL_XHTML_DTD + expectedOutput)
57 def test_convert(self):
59 Test convert on a minimal example.
61 inputLines = ['.TH BAR "1" "Oct 2007" "" ""', "Foo\n"]
62 output = ("<html><head>\n<title>BAR.1</title></head>\n<body>\n\n"
63 "<h1>BAR.1</h1>\n\n<p>Foo\n\n</p>\n\n</body>\n</html>\n")
64 self.assertConvert(inputLines, output)
71 inputLines = ['.TH BAR "1" "Oct 2007" "" ""',
74 "\\fB-o\\fR, \\fB--option\\fR",
76 output = ("<html><head>\n<title>BAR.1</title></head>\n<body>\n\n"
77 "<h1>BAR.1</h1>\n\n<h2>HEADER</h2>\n\n<dl><dt>"
78 "<strong>-o</strong>, <strong>--option</strong>\n</dt>"
79 "<dd>An option\n</dd>\n\n</dl>\n\n</body>\n</html>\n")
80 self.assertConvert(inputLines, output)
83 def test_TPMultipleOptions(self):
85 Try to parse multiple C{TP} fields.
87 inputLines = ['.TH BAR "1" "Oct 2007" "" ""',
90 "\\fB-o\\fR, \\fB--option\\fR",
93 "\\fB-n\\fR, \\fB--another\\fR",
96 output = ("<html><head>\n<title>BAR.1</title></head>\n<body>\n\n"
97 "<h1>BAR.1</h1>\n\n<h2>HEADER</h2>\n\n<dl><dt>"
98 "<strong>-o</strong>, <strong>--option</strong>\n</dt>"
99 "<dd>An option\n</dd>\n\n<dt>"
100 "<strong>-n</strong>, <strong>--another</strong>\n</dt>"
101 "<dd>Another option\n</dd>\n\n</dl>\n\n</body>\n</html>\n")
102 self.assertConvert(inputLines, output)
105 def test_TPMultiLineOptions(self):
107 Try to parse multiple C{TP} fields, with options text on several lines.
109 inputLines = ['.TH BAR "1" "Oct 2007" "" ""',
112 "\\fB-o\\fR, \\fB--option\\fR",
116 "\\fB-n\\fR, \\fB--another\\fR",
120 output = ("<html><head>\n<title>BAR.1</title></head>\n<body>\n\n"
121 "<h1>BAR.1</h1>\n\n<h2>HEADER</h2>\n\n<dl><dt>"
122 "<strong>-o</strong>, <strong>--option</strong>\n</dt>"
123 "<dd>An option\non two lines\n</dd>\n\n"
124 "<dt><strong>-n</strong>, <strong>--another</strong>\n</dt>"
125 "<dd>Another option\non two lines\n</dd>\n\n</dl>\n\n"
126 "</body>\n</html>\n")
127 self.assertConvert(inputLines, output)
130 def test_ITLegacyManagement(self):
132 Test management of BL/IT/EL used in some man pages.
134 inputLines = ['.TH BAR "1" "Oct 2007" "" ""',
144 output = ("<html><head>\n<title>BAR.1</title></head>\n<body>\n\n"
145 "<h1>BAR.1</h1>\n\n<h2>HEADER</h2>\n\n<dl>"
146 "<dt>on two lines\n</dt><dd>Another option\non two lines\n"
147 "</dd></dl>\n\n</body>\n</html>\n")
148 self.assertConvert(inputLines, output)
151 def test_interactiveCommand(self):
153 Test management of interactive command tag.
155 inputLines = ['.TH BAR "1" "Oct 2007" "" ""',
160 ".IT IC egg AR spam OP AR stuff",
164 output = ("<html><head>\n<title>BAR.1</title></head>\n<body>\n\n"
165 "<h1>BAR.1</h1>\n\n<h2>HEADER</h2>\n\n<dl>"
166 "<dt>foo <u>bar</u></dt><dd>option 1\n</dd><dt>egg "
167 "<u>spam</u> [<u>stuff</u>]</dt><dd>option 2\n</dd></dl>"
168 "\n\n</body>\n</html>\n")
169 self.assertConvert(inputLines, output)