Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/bcdoc/test_style.py
1567 views
1
# Copyright (c) 2012-2013 Mitch Garnaat http://garnaat.org/
2
# Copyright 2012-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
#
4
# Permission is hereby granted, free of charge, to any person obtaining a
5
# copy of this software and associated documentation files (the
6
# "Software"), to deal in the Software without restriction, including
7
# without limitation the rights to use, copy, modify, merge, publish, dis-
8
# tribute, sublicense, and/or sell copies of the Software, and to permit
9
# persons to whom the Software is furnished to do so, subject to the fol-
10
# lowing conditions:
11
#
12
# The above copyright notice and this permission notice shall be included
13
# in all copies or substantial portions of the Software.
14
#
15
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
17
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
18
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
# IN THE SOFTWARE.
22
#
23
import unittest
24
from awscli.bcdoc.style import ReSTStyle
25
from awscli.bcdoc.restdoc import ReSTDocument
26
27
28
class TestStyle(unittest.TestCase):
29
30
def test_spaces(self):
31
style = ReSTStyle(None, 4)
32
self.assertEqual(style.spaces(), '')
33
style.indent()
34
self.assertEqual(style.spaces(), ' ')
35
style.indent()
36
self.assertEqual(style.spaces(), ' ')
37
style.dedent()
38
self.assertEqual(style.spaces(), ' ')
39
style.dedent()
40
self.assertEqual(style.spaces(), '')
41
style.dedent()
42
self.assertEqual(style.spaces(), '')
43
44
def test_bold(self):
45
style = ReSTStyle(ReSTDocument())
46
style.bold('foobar')
47
self.assertEqual(style.doc.getvalue(), b'**foobar** ')
48
49
def test_empty_bold(self):
50
style = ReSTStyle(ReSTDocument())
51
style.start_b()
52
style.end_b()
53
self.assertEqual(style.doc.getvalue(), b'')
54
55
def test_italics(self):
56
style = ReSTStyle(ReSTDocument())
57
style.italics('foobar')
58
self.assertEqual(style.doc.getvalue(), b'*foobar* ')
59
60
def test_empty_italics(self):
61
style = ReSTStyle(ReSTDocument())
62
style.start_i()
63
style.end_i()
64
self.assertEqual(style.doc.getvalue(), b'')
65
66
def test_p(self):
67
style = ReSTStyle(ReSTDocument())
68
style.start_p()
69
style.doc.write('foo')
70
style.end_p()
71
self.assertEqual(style.doc.getvalue(), b'\n\nfoo\n\n')
72
73
def test_code(self):
74
style = ReSTStyle(ReSTDocument())
75
style.code('foobar')
76
self.assertEqual(style.doc.getvalue(), b'``foobar`` ')
77
78
def test_empty_code(self):
79
style = ReSTStyle(ReSTDocument())
80
style.start_code()
81
style.end_code()
82
self.assertEqual(style.doc.getvalue(), b'')
83
84
def test_h1(self):
85
style = ReSTStyle(ReSTDocument())
86
style.h1('foobar fiebaz')
87
self.assertEqual(
88
style.doc.getvalue(),
89
b'\n\n*************\nfoobar fiebaz\n*************\n\n'
90
)
91
92
def test_h2(self):
93
style = ReSTStyle(ReSTDocument())
94
style.h2('foobar fiebaz')
95
self.assertEqual(
96
style.doc.getvalue(),
97
b'\n\n=============\nfoobar fiebaz\n=============\n\n'
98
)
99
100
def test_h3(self):
101
style = ReSTStyle(ReSTDocument())
102
style.h3('foobar fiebaz')
103
self.assertEqual(
104
style.doc.getvalue(),
105
b'\n\n-------------\nfoobar fiebaz\n-------------\n\n'
106
)
107
108
def test_ref(self):
109
style = ReSTStyle(ReSTDocument())
110
style.ref('foobar', 'http://foo.bar.com')
111
self.assertEqual(style.doc.getvalue(),
112
b':doc:`foobar <http://foo.bar.com>`')
113
114
def test_examples(self):
115
style = ReSTStyle(ReSTDocument())
116
self.assertTrue(style.doc.keep_data)
117
style.start_examples()
118
self.assertFalse(style.doc.keep_data)
119
style.end_examples()
120
self.assertTrue(style.doc.keep_data)
121
122
def test_codeblock(self):
123
style = ReSTStyle(ReSTDocument())
124
style.codeblock('foobar')
125
self.assertEqual(style.doc.getvalue(),
126
b'::\n\n foobar\n\n\n')
127
128
def test_important(self):
129
style = ReSTStyle(ReSTDocument())
130
style.start_important()
131
style.end_important()
132
self.assertEqual(style.doc.getvalue(),
133
b'\n\n.. warning::\n\n \n\n')
134
135
def test_note(self):
136
style = ReSTStyle(ReSTDocument())
137
style.start_note()
138
style.end_note()
139
self.assertEqual(style.doc.getvalue(),
140
b'\n\n.. note::\n\n \n\n')
141
142
def test_danger(self):
143
style = ReSTStyle(ReSTDocument())
144
style.start_danger()
145
style.end_danger()
146
self.assertEqual(style.doc.getvalue(),
147
b'\n\n.. danger::\n\n \n\n')
148
149
def test_toctree_html(self):
150
style = ReSTStyle(ReSTDocument())
151
style.doc.target = 'html'
152
style.toctree()
153
style.tocitem('foo')
154
style.tocitem('bar')
155
self.assertEqual(
156
style.doc.getvalue(),
157
b'\n.. toctree::\n :maxdepth: 1\n :titlesonly:\n\n foo\n bar\n'
158
)
159
160
def test_toctree_man(self):
161
style = ReSTStyle(ReSTDocument())
162
style.doc.target = 'man'
163
style.toctree()
164
style.tocitem('foo')
165
style.tocitem('bar')
166
self.assertEqual(style.doc.getvalue(),
167
b'\n\n\n* foo\n\n\n* bar\n\n')
168
169
def test_hidden_toctree_html(self):
170
style = ReSTStyle(ReSTDocument())
171
style.doc.target = 'html'
172
style.hidden_toctree()
173
style.hidden_tocitem('foo')
174
style.hidden_tocitem('bar')
175
self.assertEqual(
176
style.doc.getvalue(),
177
b'\n.. toctree::\n :maxdepth: 1\n :hidden:\n\n foo\n bar\n'
178
)
179
180
def test_hidden_toctree_non_html(self):
181
style = ReSTStyle(ReSTDocument())
182
style.doc.target = 'man'
183
style.hidden_toctree()
184
style.hidden_tocitem('foo')
185
style.hidden_tocitem('bar')
186
self.assertEqual(
187
style.doc.getvalue(), b''
188
)
189
190
def test_href_link(self):
191
style = ReSTStyle(ReSTDocument())
192
style.start_a(attrs=[('href', 'http://example.org')])
193
style.doc.write('example')
194
style.end_a()
195
self.assertEqual(
196
style.doc.getvalue(),
197
b'`example <http://example.org>`__ '
198
)
199
200
def test_escape_href_link(self):
201
style = ReSTStyle(ReSTDocument())
202
style.start_a(attrs=[('href', 'http://example.org')])
203
style.doc.write('foo: the next bar')
204
style.end_a()
205
self.assertEqual(
206
style.doc.getvalue(),
207
b'`foo\\: the next bar <http://example.org>`__ '
208
)
209
210
def test_handle_no_text_hrefs(self):
211
style = ReSTStyle(ReSTDocument())
212
style.start_a(attrs=[('href', 'http://example.org')])
213
style.end_a()
214
self.assertEqual(
215
style.doc.getvalue(),
216
b'`<http://example.org>`__ '
217
)
218
219
def test_sphinx_reference_label_html(self):
220
style = ReSTStyle(ReSTDocument())
221
style.doc.target = 'html'
222
style.sphinx_reference_label('foo', 'bar')
223
self.assertEqual(style.doc.getvalue(), b':ref:`bar <foo>`')
224
225
def test_sphinx_reference_label_html_no_text(self):
226
style = ReSTStyle(ReSTDocument())
227
style.doc.target = 'html'
228
style.sphinx_reference_label('foo')
229
self.assertEqual(style.doc.getvalue(), b':ref:`foo <foo>`')
230
231
def test_sphinx_reference_label_non_html(self):
232
style = ReSTStyle(ReSTDocument())
233
style.doc.target = 'man'
234
style.sphinx_reference_label('foo', 'bar')
235
self.assertEqual(style.doc.getvalue(), b'bar')
236
237
def test_sphinx_reference_label_non_html_no_text(self):
238
style = ReSTStyle(ReSTDocument())
239
style.doc.target = 'man'
240
style.sphinx_reference_label('foo')
241
self.assertEqual(style.doc.getvalue(), b'foo')
242
243
def test_table_of_contents(self):
244
style = ReSTStyle(ReSTDocument())
245
style.table_of_contents()
246
self.assertEqual(style.doc.getvalue(), b'.. contents:: ')
247
248
def test_table_of_contents_with_title(self):
249
style = ReSTStyle(ReSTDocument())
250
style.table_of_contents(title='Foo')
251
self.assertEqual(style.doc.getvalue(), b'.. contents:: Foo\n')
252
253
def test_table_of_contents_with_title_and_depth(self):
254
style = ReSTStyle(ReSTDocument())
255
style.table_of_contents(title='Foo', depth=2)
256
self.assertEqual(style.doc.getvalue(),
257
b'.. contents:: Foo\n :depth: 2\n')
258
259
def test_sphinx_py_class(self):
260
style = ReSTStyle(ReSTDocument())
261
style.start_sphinx_py_class('FooClass')
262
style.end_sphinx_py_class()
263
self.assertEqual(
264
style.doc.getvalue(),
265
b'\n\n.. py:class:: FooClass\n\n \n\n'
266
)
267
268
def test_sphinx_py_method(self):
269
style = ReSTStyle(ReSTDocument())
270
style.start_sphinx_py_method('method')
271
style.end_sphinx_py_method()
272
self.assertEqual(style.doc.getvalue(),
273
b'\n\n.. py:method:: method\n\n \n\n')
274
275
def test_sphinx_py_method_with_params(self):
276
style = ReSTStyle(ReSTDocument())
277
style.start_sphinx_py_method('method', 'foo=None')
278
style.end_sphinx_py_method()
279
self.assertEqual(
280
style.doc.getvalue(),
281
b'\n\n.. py:method:: method(foo=None)\n\n \n\n')
282
283
def test_sphinx_py_attr(self):
284
style = ReSTStyle(ReSTDocument())
285
style.start_sphinx_py_attr('Foo')
286
style.end_sphinx_py_attr()
287
self.assertEqual(style.doc.getvalue(),
288
b'\n\n.. py:attribute:: Foo\n\n \n\n')
289
290
def test_write_py_doc_string(self):
291
style = ReSTStyle(ReSTDocument())
292
docstring = (
293
'This describes a function\n'
294
':param foo: Describes foo\n'
295
'returns: None'
296
)
297
style.write_py_doc_string(docstring)
298
self.assertEqual(style.doc.getvalue(), docstring.encode() + b'\n')
299
300
def test_new_line(self):
301
style = ReSTStyle(ReSTDocument())
302
style.new_line()
303
self.assertEqual(style.doc.getvalue(), b'\n')
304
305
style.do_p = False
306
style.new_line()
307
self.assertEqual(style.doc.getvalue(), b'\n\n')
308
309
def test_list(self):
310
style = ReSTStyle(ReSTDocument())
311
style.li('foo')
312
self.assertEqual(style.doc.getvalue(), b'\n* foo\n\n')
313
314
def test_non_top_level_lists_are_indented(self):
315
style = ReSTStyle(ReSTDocument())
316
317
# Start the top level list
318
style.start_ul()
319
320
# Write one list element
321
style.start_li()
322
style.doc.handle_data('foo')
323
style.end_li()
324
325
self.assertEqual(style.doc.getvalue(), b"\n\n\n* foo\n")
326
327
# Start the nested list
328
style.start_ul()
329
330
# Write an element to the nested list
331
style.start_li()
332
style.doc.handle_data('bar')
333
style.end_li()
334
335
self.assertEqual(style.doc.getvalue(),
336
b"\n\n\n* foo\n\n\n \n * bar\n ")
337
338
def test_external_link(self):
339
style = ReSTStyle(ReSTDocument())
340
style.doc.target = 'html'
341
style.external_link('MyLink', 'http://example.com/foo')
342
self.assertEqual(style.doc.getvalue(),
343
b'`MyLink <http://example.com/foo>`_')
344
345
def test_external_link_in_man_page(self):
346
style = ReSTStyle(ReSTDocument())
347
style.doc.target = 'man'
348
style.external_link('MyLink', 'http://example.com/foo')
349
self.assertEqual(style.doc.getvalue(), b'MyLink')
350
351
def test_internal_link(self):
352
style = ReSTStyle(ReSTDocument())
353
style.doc.target = 'html'
354
style.internal_link('MyLink', '/index')
355
self.assertEqual(
356
style.doc.getvalue(),
357
b':doc:`MyLink </index>`'
358
)
359
360
def test_internal_link_in_man_page(self):
361
style = ReSTStyle(ReSTDocument())
362
style.doc.target = 'man'
363
style.internal_link('MyLink', '/index')
364
self.assertEqual(style.doc.getvalue(), b'MyLink')
365
366