Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/bcdoc/test_docstringparser.py
1567 views
1
# Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
#
3
# Permission is hereby granted, free of charge, to any person obtaining a
4
# copy of this software and associated documentation files (the
5
# "Software"), to deal in the Software without restriction, including
6
# without limitation the rights to use, copy, modify, merge, publish, dis-
7
# tribute, sublicense, and/or sell copies of the Software, and to permit
8
# persons to whom the Software is furnished to do so, subject to the fol-
9
# lowing conditions:
10
#
11
# The above copyright notice and this permission notice shall be included
12
# in all copies or substantial portions of the Software.
13
#
14
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
16
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20
# IN THE SOFTWARE.
21
from awscli.testutils import mock, unittest
22
23
import awscli.bcdoc.docstringparser as parser
24
from awscli.bcdoc.restdoc import ReSTDocument
25
26
27
class TestDocStringParser(unittest.TestCase):
28
def parse(self, html):
29
docstring_parser = parser.DocStringParser(ReSTDocument())
30
docstring_parser.feed(html)
31
docstring_parser.close()
32
return docstring_parser.doc.getvalue()
33
34
def assert_contains_exact_lines_in_order(self, actual, expected):
35
# Get each line and filter out empty lines
36
contents = actual.split(b'\n')
37
contents = [line for line in contents if line and not line.isspace()]
38
39
for line in expected:
40
self.assertIn(line, contents)
41
beginning = contents.index(line)
42
contents = contents[beginning:]
43
44
def test_nested_lists(self):
45
html = "<ul><li>Wello</li><ul><li>Horld</li></ul></ul>"
46
result = self.parse(html)
47
self.assert_contains_exact_lines_in_order(result, [
48
b'* Wello',
49
b' * Horld'
50
])
51
52
def test_nested_lists_with_extra_white_space(self):
53
html = "<ul> <li> Wello</li><ul> <li> Horld</li></ul></ul>"
54
result = self.parse(html)
55
self.assert_contains_exact_lines_in_order(result, [
56
b'* Wello',
57
b' * Horld'
58
])
59
60
61
class TestHTMLTree(unittest.TestCase):
62
def setUp(self):
63
self.style = mock.Mock()
64
self.doc = mock.Mock()
65
self.doc.style = self.style
66
self.tree = parser.HTMLTree(self.doc)
67
68
def test_add_tag(self):
69
self.tree.add_tag('foo')
70
self.assertIsInstance(self.tree.current_node, parser.TagNode)
71
self.assertEqual(self.tree.current_node.tag, 'foo')
72
73
def test_add_unsupported_tag(self):
74
del self.style.start_foo
75
del self.style.end_foo
76
self.tree.add_tag('foo')
77
self.assertIn('foo', self.tree.unhandled_tags)
78
79
def test_add_data(self):
80
self.tree.add_data('foo')
81
self.assertNotIsInstance(self.tree.current_node, parser.DataNode)
82
node = self.tree.head.children[0]
83
self.assertIsInstance(node, parser.DataNode)
84
self.assertEqual(node.data, 'foo')
85
86
87
class TestStemNode(unittest.TestCase):
88
def setUp(self):
89
self.style = mock.Mock()
90
self.doc = mock.Mock()
91
self.doc.style = self.style
92
self.node = parser.StemNode()
93
94
def test_add_child(self):
95
child = parser.StemNode()
96
self.node.add_child(child)
97
self.assertIn(child, self.node.children)
98
self.assertEqual(child.parent, self.node)
99
100
def test_write(self):
101
self.node.add_child(mock.Mock())
102
self.node.add_child(mock.Mock())
103
104
self.node.write(mock.Mock())
105
for child in self.node.children:
106
self.assertTrue(child.write.called)
107
108
109
class TestTagNode(unittest.TestCase):
110
def setUp(self):
111
self.style = mock.Mock()
112
self.doc = mock.Mock()
113
self.doc.style = self.style
114
self.tag = 'foo'
115
self.node = parser.TagNode(self.tag)
116
117
def test_write_calls_style(self):
118
self.node.write(self.doc)
119
self.assertTrue(self.style.start_foo.called)
120
self.assertTrue(self.style.end_foo.called)
121
122
def test_write_unsupported_tag(self):
123
del self.style.start_foo
124
del self.style.end_foo
125
126
try:
127
self.node.write(self.doc)
128
except AttributeError as e:
129
self.fail(str(e))
130
131
132
class TestDataNode(unittest.TestCase):
133
def setUp(self):
134
self.style = mock.Mock()
135
self.doc = mock.Mock()
136
self.doc.style = self.style
137
138
def test_string_data(self):
139
node = parser.DataNode('foo')
140
self.assertEqual(node.data, 'foo')
141
142
def test_non_string_data_raises_error(self):
143
with self.assertRaises(ValueError):
144
parser.DataNode(5)
145
146
def test_lstrip(self):
147
node = parser.DataNode(' foo')
148
node.lstrip()
149
self.assertEqual(node.data, 'foo')
150
151
def test_write(self):
152
node = parser.DataNode('foo bar baz')
153
self.doc.translate_words.return_value = ['foo', 'bar', 'baz']
154
node.write(self.doc)
155
self.doc.handle_data.assert_called_once_with('foo bar baz')
156
157
def test_write_space(self):
158
node = parser.DataNode(' ')
159
node.write(self.doc)
160
self.doc.handle_data.assert_called_once_with(' ')
161
self.doc.handle_data.reset_mock()
162
163
node = parser.DataNode(' ')
164
node.write(self.doc)
165
self.doc.handle_data.assert_called_once_with(' ')
166
167
def test_write_empty_string(self):
168
node = parser.DataNode('')
169
node.write(self.doc)
170
self.assertFalse(self.doc.handle_data.called)
171
172
173
class TestLineItemNode(unittest.TestCase):
174
def setUp(self):
175
self.style = mock.Mock()
176
self.doc = mock.Mock()
177
self.doc.style = self.style
178
self.doc.translate_words.return_value = ['foo']
179
self.node = parser.LineItemNode()
180
181
def test_write_strips_white_space(self):
182
self.node.add_child(parser.DataNode(' foo'))
183
self.node.write(self.doc)
184
self.doc.handle_data.assert_called_once_with('foo')
185
186
def test_write_strips_nested_white_space(self):
187
self.node.add_child(parser.DataNode(' '))
188
tag_child = parser.TagNode('foo')
189
tag_child.add_child(parser.DataNode(' '))
190
tag_child_2 = parser.TagNode('foo')
191
tag_child_2.add_child(parser.DataNode(' foo'))
192
tag_child.add_child(tag_child_2)
193
self.node.add_child(tag_child)
194
195
self.node.write(self.doc)
196
self.doc.handle_data.assert_called_once_with('foo')
197
198
def test_write_only_strips_until_text_is_found(self):
199
self.node.add_child(parser.DataNode(' '))
200
tag_child = parser.TagNode('foo')
201
tag_child.add_child(parser.DataNode(' '))
202
tag_child_2 = parser.TagNode('foo')
203
tag_child_2.add_child(parser.DataNode(' foo'))
204
tag_child_2.add_child(parser.DataNode(' '))
205
tag_child.add_child(tag_child_2)
206
self.node.add_child(tag_child)
207
208
self.node.write(self.doc)
209
210
calls = [mock.call('foo'), mock.call(' ')]
211
self.doc.handle_data.assert_has_calls(calls)
212
213