Merge branch 'master' into def-lists
[rst2info.git] / texinfo / test_info_translator.py
blob8a43e3415a43ec5a81d7947efb8a1a3d13146c9c
2 import rst_test_utils
3 from info_translator import InfoTranslator
4 from docutils.transforms import frontmatter
6 def strip_comments(body):
7 return filter(lambda x: not x.startswith('@c '), body)
9 class T(rst_test_utils.TestCase):
11 def given_input(self, input, transforms=[], settings_overrides={}):
12 super(T, self).given_input(input, settings_overrides)
13 self.visitor = InfoTranslator(self.document)
15 for t in transforms:
16 self.document.transformer.add_transform(t)
17 self.document.transformer.apply_transforms()
19 self.document.walkabout(self.visitor)
21 def test_strip_comments(self):
22 self.visitor.body = ['Hello', '@c blah', 'World!']
23 self.assertEqual(['Hello', '@c blah', 'World!'], self.visitor.body)
24 self.assertEqual(['Hello', 'World!'], strip_comments(self.visitor.body))
26 def setUp(self):
27 self.given_input('')
29 def test_adds_info_header(self):
30 self.assertTrue(self.visitor.astext().startswith('\\input texinfo'))
32 def test_title(self):
33 self.given_input('''
34 Hello, world!
35 ================
36 ''', transforms = [frontmatter.DocTitle])
37 self.assertEqual(['@node Top', '@top Hello, world!'], self.visitor.body)
39 def test_subtitle(self):
40 self.given_input('''
41 ======
42 Title!
43 ======
45 --------
46 Subtitle
47 --------
48 ''', transforms = [frontmatter.DocTitle])
49 self.assertEqual(['@node Top', '@top Title!',
50 '@majorheading Subtitle'],
51 self.visitor.body)
53 def test_subsection(self):
54 self.given_input('''
55 Chapter 1
56 =========
58 Section 1
59 ---------
61 Subsection 1
62 ~~~~~~~~~~~~
63 ''')
64 self.assertEqual(['@chapter Chapter 1',
65 '@section Section 1',
66 '@subsection Subsection 1'],
67 self.visitor.body)
69 def test_section_layering(self):
70 self.given_input("""
71 The Problem
72 ===========
74 Cheese
75 ------
77 Lunar mold
78 ~~~~~~~~~~
80 Cows
81 ----
83 A Modest Solution
84 =================
85 """)
86 self.assertEqual(['@chapter The Problem',
87 '@section Cheese',
88 '@subsection Lunar mold',
89 '@section Cows',
90 '@chapter A Modest Solution'],
91 self.visitor.body)
93 def test_beyond_subsections(self):
94 self.given_input('''
95 Chapter 1
96 =========
98 Section 1
99 ---------
101 Subsection 1
102 ~~~~~~~~~~~~
104 Something else
105 ^^^^^^^^^^^^^^
107 Subsection 2
108 ~~~~~~~~~~~~
110 Section 2
111 ---------
113 ''')
114 self.assertEqual(['@chapter Chapter 1',
115 '@section Section 1',
116 '@subsection Subsection 1',
117 '@subsection Something else',
118 '@subsection Subsection 2',
119 '@section Section 2'],
120 self.visitor.body)
122 def test_comments_are_comments(self):
123 self.given_input("""
124 .. hello-world:
125 """)
126 self.assertEqual(['@c hello-world:'], self.visitor.body)
128 def test_paragraph_text_is_normal_text(self):
129 self.given_input("""
130 Someone left the cake out in the rain.
131 I don't think that I can take it.
132 """)
133 self.assertEqual(["Someone left the cake out in the rain.\nI don't think that I can take it.", ''], self.visitor.body)
135 def test_two_paragraphs(self):
136 self.given_input("""
137 MacArthur's park is melting in the dark.
139 Someone left the cake out in the rain.
140 I don't think that I can take it.
141 """)
142 self.assertEqual(["MacArthur's park is melting in the dark.", '',
143 "Someone left the cake out in the rain.\nI don't think that I can take it.", ''], self.visitor.body)
146 def test_single_bullet_list(self):
147 self.given_input("""
148 * milk
149 """)
150 self.assertEqual(["@itemize @bullet",
151 "@item", "milk", "",
152 "@end @itemize", ""], self.visitor.body)
154 def test_three_bullet_list(self):
155 self.given_input("""
156 * milk
157 * eggs
158 * bread
159 """)
160 self.assertEqual(["@itemize @bullet",
161 "@item", "milk", "",
162 "@item", "eggs", "",
163 "@item", "bread", "",
164 "@end @itemize", ""], self.visitor.body)
166 def test_single_enumerated_list(self):
167 self.given_input("""
168 (1) I was transferred to the moon.
169 """)
170 self.assertEqual(["@enumerate 1",
171 "@item", "I was transferred to the moon.", "",
172 "@end enumerate", ""], self.visitor.body)
174 def test_three_item_enumerated_list(self):
175 self.given_input("""
176 (1) I was transferred to the moon.
177 (2) Worse pay, better hours.
178 (3) Worse pay, better fellow workers.
179 """)
180 self.assertEqual(["@enumerate 1",
181 "@item", "I was transferred to the moon.", "",
182 "@item", "Worse pay, better hours.", "",
183 "@item", "Worse pay, better fellow workers.", "",
184 "@end enumerate", ""], self.visitor.body)
186 def test_single_enumerated_sub_list(self):
187 self.given_input("""
188 a. lower-case letters
190 1. with a sub-list
191 2. with two items
192 """)
193 self.assertEqual(["@enumerate a",
194 "@item", "lower-case letters", "",
195 "@enumerate 1",
196 "@item", "with a sub-list", "",
197 "@item", "with two items", "",
198 "@end enumerate", "",
199 "@end enumerate", ""], self.visitor.body)
201 def test_sub_lists_from_rst_quickstart(self):
202 self.given_input("""
203 1. numbers
205 A. upper-case letters
206 and it goes over many lines
208 with two paragraphs and all!
210 a. lower-case letters
212 3. with a sub-list starting at a different number
213 4. make sure the numbers are in the correct sequence though!
215 I. upper-case roman numerals
217 i. lower-case roman numerals
219 (1) numbers again
221 1) and again
222 """)
223 self.assertEqual(["@enumerate 1",
224 "@item", "numbers", "",
225 "@end enumerate", "",
227 "@enumerate A",
228 "@item", "upper-case letters\nand it goes over many lines", "",
229 "with two paragraphs and all!", "",
230 "@end enumerate", "",
232 "@enumerate a",
233 "@item", "lower-case letters", "",
234 "@enumerate 3",
235 "@item", "with a sub-list starting at a different number", "",
236 "@item", "make sure the numbers are in the correct sequence though!", "",
237 "@end enumerate", "",
238 "@end enumerate", "",
240 "@enumerate 1",
241 "@item", "upper-case roman numerals", "",
242 "@end enumerate", "",
244 "@enumerate 1",
245 "@item", "lower-case roman numerals", "",
246 "@end enumerate", "",
248 "@enumerate 1",
249 "@item", "numbers again", "",
250 "@end enumerate", "",
252 "@enumerate 1",
253 "@item", "and again", "",
254 "@end enumerate", ""], strip_comments(self.visitor.body))
256 def test_system_message(self):
257 self.given_input("""
258 3. Lists generally don't start at 3
259 """, settings_overrides={'report_level':1})
260 self.assertEqual(["@enumerate 3",
261 "@item", "Lists generally don't start at 3", "",
262 "@end enumerate", "",
263 "@c System Message: INFO/1 (rst_test_utils:, line 2)",
264 "@c Enumerated list start value not ordinal-1: \"3\" (ordinal 3)",
265 "@c --end system message--"], self.visitor.body)
267 def test_system_message_below_report_level(self):
268 self.given_input("""
269 3. Lists generally don't start at 3
270 """)
271 self.assertEqual(["@enumerate 3",
272 "@item", "Lists generally don't start at 3", "",
273 "@end enumerate", ""
274 ], self.visitor.body)
276 def test_quotation(self):
277 self.given_input("""
278 Never precede any action with the words "Watch this!"
279 -- the second constant Law of Frisbee
281 """)
282 self.assertEqual(["@quotation",
283 '''Never precede any action with the words "Watch this!"
284 -- the second constant Law of Frisbee''', '',
285 "@end quotation", ""], self.visitor.body)
287 def test_formatted_code(self):
288 self.given_input("""
289 Frisbeetarianism is the belief that, when you die, your soul goes up
290 onto the roof and gets stuck.
292 Never precede any action with the words "Watch this!"
293 -- the second constant Law of Frisbee
294 """)
295 self.assertEqual([
296 '''Frisbeetarianism is the belief that, when you die, your soul goes up
297 onto the roof and gets stuck.''', '',
298 "@quotation",
299 '''Never precede any action with the words "Watch this!"
300 -- the second constant Law of Frisbee''', '',
301 "@end quotation", ""], self.visitor.body)
303 def test_literal_block(self):
304 self.given_input("""
305 THE LANDING::
307 "Just the place for a Snark!" the Bellman cried,
308 As he landed his crew with care;
309 Supporting each man on the top of the tide
310 By a finger entwined in his hair.
312 """)
313 self.assertEqual(["THE LANDING:", '',
314 """"Just the place for a Snark!" the Bellman cried,
315 As he landed his crew with care;
316 Supporting each man on the top of the tide
317 By a finger entwined in his hair."""], self.visitor.body)
319 def test_definition_list(self):
320 self.given_input("""
321 what
322 Definition lists associate a term with a definition.
325 The term is a one-line phrase, and the definition is one or more
326 paragraphs or body elements, indented relative to the term.
327 Blank lines are not allowed between term and definition.
328 """)
329 self.assertEqual(["what",
330 "@quotation",
331 "Definition lists associate a term with a definition.",
333 "@end quotation", "",
334 "how",
335 "@quotation",
336 "The term is a one-line phrase, and the definition is one or more\n"
337 "paragraphs or body elements, indented relative to the term.\n"
338 "Blank lines are not allowed between term and definition.",
340 "@end quotation", ""], self.visitor.body)