Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / lore / test / test_man2lore.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4
5 """
6 Tests for L{twisted.lore.man2lore}.
7 """
8
9 from StringIO import StringIO
10
11 from twisted.trial.unittest import TestCase
12
13 from twisted.lore.man2lore import ManConverter
14
15
16 _TRANSITIONAL_XHTML_DTD = ("""\
17 <?xml version="1.0"?>
18 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
19     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
20 """)
21
22
23 class ManConverterTestCase(TestCase):
24     """
25     Tests for L{ManConverter}.
26     """
27
28     def setUp(self):
29         """
30         Build instance variables useful for tests.
31
32         @ivar converter: a L{ManConverter} to be used during tests.
33         """
34         self.converter = ManConverter()
35
36
37     def assertConvert(self, inputLines, expectedOutput):
38         """
39         Helper method to check conversion from a man page to a Lore output.
40
41         @param inputLines: lines of the manpages.
42         @type inputLines: C{list}
43
44         @param expectedOutput: expected Lore content.
45         @type expectedOutput: C{str}
46         """
47         inputFile = StringIO()
48         for line in inputLines:
49             inputFile.write(line + '\n')
50         inputFile.seek(0)
51         outputFile = StringIO()
52         self.converter.convert(inputFile, outputFile)
53         self.assertEqual(
54             outputFile.getvalue(), _TRANSITIONAL_XHTML_DTD + expectedOutput)
55
56
57     def test_convert(self):
58         """
59         Test convert on a minimal example.
60         """
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)
65
66
67     def test_TP(self):
68         """
69         Test C{TP} parsing.
70         """
71         inputLines = ['.TH BAR "1" "Oct 2007" "" ""',
72                 ".SH HEADER",
73                 ".TP",
74                 "\\fB-o\\fR, \\fB--option\\fR",
75                 "An option"]
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)
81
82
83     def test_TPMultipleOptions(self):
84         """
85         Try to parse multiple C{TP} fields.
86         """
87         inputLines = ['.TH BAR "1" "Oct 2007" "" ""',
88                 ".SH HEADER",
89                 ".TP",
90                 "\\fB-o\\fR, \\fB--option\\fR",
91                 "An option",
92                 ".TP",
93                 "\\fB-n\\fR, \\fB--another\\fR",
94                 "Another option",
95                 ]
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)
103
104
105     def test_TPMultiLineOptions(self):
106         """
107         Try to parse multiple C{TP} fields, with options text on several lines.
108         """
109         inputLines = ['.TH BAR "1" "Oct 2007" "" ""',
110                 ".SH HEADER",
111                 ".TP",
112                 "\\fB-o\\fR, \\fB--option\\fR",
113                 "An option",
114                 "on two lines",
115                 ".TP",
116                 "\\fB-n\\fR, \\fB--another\\fR",
117                 "Another option",
118                 "on two lines",
119                 ]
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)
128
129
130     def test_ITLegacyManagement(self):
131         """
132         Test management of BL/IT/EL used in some man pages.
133         """
134         inputLines = ['.TH BAR "1" "Oct 2007" "" ""',
135                 ".SH HEADER",
136                 ".BL",
137                 ".IT An option",
138                 "on two lines",
139                 ".IT",
140                 "Another option",
141                 "on two lines",
142                 ".EL"
143                 ]
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)
149
150
151     def test_interactiveCommand(self):
152         """
153         Test management of interactive command tag.
154         """
155         inputLines = ['.TH BAR "1" "Oct 2007" "" ""',
156                 ".SH HEADER",
157                 ".BL",
158                 ".IT IC foo AR bar",
159                 "option 1",
160                 ".IT IC egg AR spam OP AR stuff",
161                 "option 2",
162                 ".EL"
163                 ]
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)