Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/bcdoc/test_document.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.restdoc import ReSTDocument, DocumentStructure
25
26
27
class TestReSTDocument(unittest.TestCase):
28
29
def _write_array(self, doc, arr):
30
for elt in arr:
31
doc.write(elt)
32
33
def test_write(self):
34
doc = ReSTDocument()
35
doc.write('foo')
36
self.assertEqual(doc.getvalue(), b'foo')
37
38
def test_writeln(self):
39
doc = ReSTDocument()
40
doc.writeln('foo')
41
self.assertEqual(doc.getvalue(), b'foo\n')
42
43
def test_find_last_write(self):
44
doc = ReSTDocument()
45
self._write_array(doc, ['a', 'b', 'c', 'd', 'e'])
46
expected_index = 0
47
self.assertEqual(doc.find_last_write('a'), expected_index)
48
49
def test_find_last_write_duplicates(self):
50
doc = ReSTDocument()
51
self._write_array(doc, ['a', 'b', 'c', 'a', 'e'])
52
expected_index = 3
53
self.assertEqual(doc.find_last_write('a'), expected_index)
54
55
def test_find_last_write_not_found(self):
56
doc = ReSTDocument()
57
self._write_array(doc, ['a', 'b', 'c', 'd', 'e'])
58
self.assertIsNone(doc.find_last_write('f'))
59
60
def test_insert_write(self):
61
doc = ReSTDocument()
62
self._write_array(doc, ['foo', 'bar'])
63
doc.insert_write(1, 'baz')
64
self.assertEqual(doc.getvalue(), b'foobazbar')
65
66
def test_include_doc_string(self):
67
doc = ReSTDocument()
68
doc.include_doc_string('<p>this is a <code>test</code></p>')
69
self.assertEqual(doc.getvalue(), b'\n\nthis is a ``test`` \n\n')
70
71
def test_remove_doc_string(self):
72
doc = ReSTDocument()
73
doc.writeln('foo')
74
doc.include_doc_string('<p>this is a <code>test</code></p>')
75
doc.remove_last_doc_string()
76
self.assertEqual(doc.getvalue(), b'foo\n')
77
78
def test_add_links(self):
79
doc = ReSTDocument()
80
doc.hrefs['foo'] = 'https://example.com/'
81
self.assertEqual(
82
doc.getvalue(), b'\n\n.. _foo: https://example.com/\n')
83
84
85
class TestDocumentStructure(unittest.TestCase):
86
def setUp(self):
87
self.name = 'mydoc'
88
self.doc_structure = DocumentStructure(self.name)
89
90
def test_name(self):
91
self.assertEqual(self.doc_structure.name, self.name)
92
93
def test_path(self):
94
self.assertEqual(self.doc_structure.path, [self.name])
95
self.doc_structure.path = ['foo']
96
self.assertEqual(self.doc_structure.path, ['foo'])
97
98
def test_add_new_section(self):
99
section = self.doc_structure.add_new_section('mysection')
100
101
# Ensure the name of the section is correct
102
self.assertEqual(section.name, 'mysection')
103
104
# Ensure we can get the section.
105
self.assertEqual(
106
self.doc_structure.get_section('mysection'), section)
107
108
# Ensure the path is correct
109
self.assertEqual(section.path, ['mydoc', 'mysection'])
110
111
# Ensure some of the necessary attributes are passed to the
112
# the section.
113
self.assertEqual(section.style.indentation,
114
self.doc_structure.style.indentation)
115
self.assertEqual(section.translation_map,
116
self.doc_structure.translation_map)
117
self.assertEqual(section.hrefs,
118
self.doc_structure.hrefs)
119
120
def test_delete_section(self):
121
section = self.doc_structure.add_new_section('mysection')
122
self.assertEqual(
123
self.doc_structure.get_section('mysection'), section)
124
self.doc_structure.delete_section('mysection')
125
with self.assertRaises(KeyError):
126
section.get_section('mysection')
127
128
def test_create_sections_at_instantiation(self):
129
sections = ['intro', 'middle', 'end']
130
self.doc_structure = DocumentStructure(
131
self.name, section_names=sections)
132
# Ensure the sections are attached to the new document structure.
133
for section_name in sections:
134
section = self.doc_structure.get_section(section_name)
135
self.assertEqual(section.name, section_name)
136
137
def test_flush_structure(self):
138
section = self.doc_structure.add_new_section('mysection')
139
subsection = section.add_new_section('mysubsection')
140
self.doc_structure.writeln('1')
141
section.writeln('2')
142
subsection.writeln('3')
143
second_section = self.doc_structure.add_new_section('mysection2')
144
second_section.writeln('4')
145
contents = self.doc_structure.flush_structure()
146
147
# Ensure the contents were flushed out correctly
148
self.assertEqual(contents, b'1\n2\n3\n4\n')
149
150
def test_flush_structure_hrefs(self):
151
section = self.doc_structure.add_new_section('mysection')
152
section.writeln('section contents')
153
self.doc_structure.hrefs['foo'] = 'www.foo.com'
154
section.hrefs['bar'] = 'www.bar.com'
155
contents = self.doc_structure.flush_structure()
156
self.assertIn(b'.. _foo: www.foo.com', contents)
157
self.assertIn(b'.. _bar: www.bar.com', contents)
158
159
def test_available_sections(self):
160
self.doc_structure.add_new_section('mysection')
161
self.doc_structure.add_new_section('mysection2')
162
self.assertEqual(
163
self.doc_structure.available_sections,
164
['mysection', 'mysection2']
165
)
166
167
def test_context(self):
168
context = {'Foo': 'Bar'}
169
section = self.doc_structure.add_new_section(
170
'mysection', context=context)
171
self.assertEqual(section.context, context)
172
173
# Make sure if context is not specified it is empty.
174
section = self.doc_structure.add_new_section('mysection2')
175
self.assertEqual(section.context, {})
176
177
def test_remove_all_sections(self):
178
self.doc_structure.add_new_section('mysection2')
179
self.doc_structure.remove_all_sections()
180
self.assertEqual(self.doc_structure.available_sections, [])
181
182
def test_clear_text(self):
183
self.doc_structure.write('Foo')
184
self.doc_structure.clear_text()
185
self.assertEqual(self.doc_structure.flush_structure(), b'')
186
187