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