Use transforms in info_translator tests so we can test title and
[rst2info.git] / texinfo / test_info_translator.py
blob28039cc0e37c40b4287f2a5a94b13b70716f06ae
1 import rst_test_utils
2 from info_translator import InfoTranslator
3 from docutils.transforms import frontmatter
5 class T(rst_test_utils.TestCase):
7 def given_input(self, input, transforms = []):
8 super(T, self).given_input(input)
9 self.visitor = InfoTranslator(self.document)
11 for t in transforms:
12 self.document.transformer.add_transform(t)
13 self.document.transformer.apply_transforms()
15 self.document.walkabout(self.visitor)
17 def setUp(self):
18 self.given_input('')
20 def test_adds_info_header(self):
21 self.assertTrue(self.visitor.astext().startswith('\\input texinfo'))
23 def test_title(self):
24 self.given_input('''
25 Hello, world!
26 ================
27 ''', transforms = [frontmatter.DocTitle])
28 self.assertEqual(['@node Top', '@top Hello, world!'], self.visitor.body)
30 def test_subtitle(self):
31 self.given_input('''
32 ======
33 Title!
34 ======
36 --------
37 Subtitle
38 --------
39 ''', transforms = [frontmatter.DocTitle])
40 self.assertEqual(['@node Top', '@top Title!',
41 '@majorheading Subtitle'],
42 self.visitor.body)
44 def test_subsection(self):
45 self.given_input('''
46 Chapter 1
47 =========
49 Section 1
50 ---------
52 Subsection 1
53 ~~~~~~~~~~~~
54 ''')
55 self.assertEqual(['@chapter Chapter 1',
56 '@section Section 1',
57 '@subsection Subsection 1'],
58 self.visitor.body)
60 def test_section_layering(self):
61 self.given_input("""
62 The Problem
63 ===========
65 Cheese
66 ------
68 Lunar mold
69 ~~~~~~~~~~
71 Cows
72 ----
74 A Modest Solution
75 =================
76 """)
77 self.assertEqual(['@chapter The Problem',
78 '@section Cheese',
79 '@subsection Lunar mold',
80 '@section Cows',
81 '@chapter A Modest Solution'],
82 self.visitor.body)
84 def test_beyond_subsections(self):
85 self.given_input('''
86 Chapter 1
87 =========
89 Section 1
90 ---------
92 Subsection 1
93 ~~~~~~~~~~~~
95 Something else
96 ^^^^^^^^^^^^^^
98 Subsection 2
99 ~~~~~~~~~~~~
101 Section 2
102 ---------
104 ''')
105 self.assertEqual(['@chapter Chapter 1',
106 '@section Section 1',
107 '@subsection Subsection 1',
108 '@subsection Something else',
109 '@subsection Subsection 2',
110 '@section Section 2'],
111 self.visitor.body)
113 def test_comments_are_comments(self):
114 self.given_input("""
115 .. hello-world:
116 """)
117 self.assertEqual(['@c hello-world:'], self.visitor.body)
119 def test_paragraph_text_is_normal_text(self):
120 self.given_input("""
121 Someone left the cake out in the rain.
122 I don't think that I can take it.
123 """)
124 self.assertEqual(["Someone left the cake out in the rain.\nI don't think that I can take it."], self.visitor.body)
126 def test_two_paragraphs(self):
127 self.given_input("""
128 MacArthur's park is melting in the dark.
130 Someone left the cake out in the rain.
131 I don't think that I can take it.
132 """)
133 self.assertEqual(["MacArthur's park is melting in the dark.",
134 "Someone left the cake out in the rain.\nI don't think that I can take it."], self.visitor.body)
137 def test_single_bullet_list(self):
138 self.given_input("""
139 * milk
140 """)
141 self.assertEqual(["@itemize @bullet",
142 "@item",
143 "milk",
145 "@end @itemize"], self.visitor.body)
147 def test_three_bullet_list(self):
148 self.given_input("""
149 * milk
150 * eggs
151 * bread
152 """)
153 self.assertEqual(["@itemize @bullet",
154 "@item",
155 "milk",
157 "@item",
158 "eggs",
160 "@item",
161 "bread",
163 "@end @itemize"], self.visitor.body)
165 def test_quotation(self):
166 self.given_input("""
167 Never precede any action with the words "Watch this!"
168 -- the first commandment of frisbeetarianism
170 """)
171 self.assertEqual(["@quotation",
172 '''Never precede any action with the words "Watch this!"
173 -- the first commandment of frisbeetarianism''',
174 "@end quotation"], self.visitor.body)
176 def test_formatted_code(self):
177 self.given_input("""
178 Frisbeetarianism is the belief that, when you die, your soul goes up
179 onto the roof and gets stuck.
181 Never precede any action with the words "Watch this!"
182 -- the first constant Law of Frisbee
183 """)
184 self.assertEqual([
185 '''Frisbeetarianism is the belief that, when you die, your soul goes up
186 onto the roof and gets stuck.''',
187 "@quotation",
188 '''Never precede any action with the words "Watch this!"
189 -- the first constant Law of Frisbee''',
190 "@end quotation"], self.visitor.body)
192 def test_literal_block(self):
193 self.given_input("""
194 THE LANDING::
196 "Just the place for a Snark!" the Bellman cried,
197 As he landed his crew with care;
198 Supporting each man on the top of the tide
199 By a finger entwined in his hair.
201 """)
202 self.assertEqual(["THE LANDING:",
203 """"Just the place for a Snark!" the Bellman cried,
204 As he landed his crew with care;
205 Supporting each man on the top of the tide
206 By a finger entwined in his hair."""
207 ], self.visitor.body)