Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/test_text.py
1566 views
1
# Copyright (c) 2013 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
#
22
import sys
23
24
from awscli.testutils import mock, unittest
25
from awscli.compat import StringIO
26
27
from awscli import text
28
29
30
class TestSection(unittest.TestCase):
31
def format_text(self, data, stream=None):
32
if stream is None:
33
stream = StringIO()
34
text.format_text(data, stream=stream)
35
return stream.getvalue()
36
37
def assert_text_renders_to(self, data, expected_rendering):
38
rendered = self.format_text(data)
39
self.assertEqual(rendered, expected_rendering)
40
41
def test_dict_format(self):
42
self.assert_text_renders_to(dict(a=1, b=2, c=3), "1\t2\t3\n")
43
44
def test_list_format(self):
45
self.assert_text_renders_to([1, 2, 3], "1\t2\t3\n")
46
47
def test_list_of_dicts(self):
48
self.assert_text_renders_to(
49
{'foo': [dict(a=1, b=2, c=3), dict(a=4, b=5, c=6)]},
50
'FOO\t1\t2\t3\n'
51
'FOO\t4\t5\t6\n')
52
53
def test_multiple_list_of_dicts(self):
54
self.assert_text_renders_to(
55
{'foo': [dict(a=1, b=2, c=3), dict(a=4, b=5, c=6)],
56
'zoo': [dict(a=7, b=8, c=9), dict(a=0, b=1, c=2)]},
57
'FOO\t1\t2\t3\n'
58
'FOO\t4\t5\t6\n'
59
'ZOO\t7\t8\t9\n'
60
'ZOO\t0\t1\t2\n'
61
)
62
63
def test_single_scalar_number(self):
64
self.assert_text_renders_to(10, '10\n')
65
66
def test_list_of_single_number(self):
67
self.assert_text_renders_to([10], '10\n')
68
69
def test_list_of_multiple_numbers(self):
70
self.assert_text_renders_to([10, 10, 10], '10\t10\t10\n')
71
72
def test_different_keys_in_sublists(self):
73
self.assert_text_renders_to(
74
# missing "b" adds "d"
75
{'foo': [dict(a=1, b=2, c=3), dict(a=4, c=5), dict(a=6, d=7)]},
76
'FOO\t1\t2\t3\t\n'
77
'FOO\t4\t\t5\t\n'
78
'FOO\t6\t\t\t7\n'
79
)
80
81
def test_different_keys_in_nested_sublists(self):
82
self.assert_text_renders_to({'bar':[
83
{'foo': [dict(a=1, b=2, c=3), dict(a=4, c=5)]},
84
{'foo': [dict(b=6, d=7), dict(b=8, c=9)]},
85
]},
86
'FOO\t1\t2\t3\n'
87
'FOO\t4\t\t5\n'
88
'FOO\t6\t\t7\n'
89
'FOO\t8\t9\t\n'
90
)
91
92
def test_different_keys_in_deeply_nested_sublists(self):
93
self.assert_text_renders_to({'bar':[
94
{'foo': [[[dict(a=1, b=2, c=3), dict(a=4, c=5)]]]},
95
{'foo': [[[dict(b=6, d=7), dict(b=8, c=9)]]]},
96
]},
97
'FOO\t1\t2\t3\n'
98
'FOO\t4\t\t5\n'
99
'FOO\t6\t\t7\n'
100
'FOO\t8\t9\t\n'
101
)
102
103
def test_scalars_and_complex_types(self):
104
self.assert_text_renders_to(
105
{'foo': [dict(a=1, b=dict(y='y', z='z'), c=3),
106
dict(a=4, b=dict(y='y', z='z'), c=6)]},
107
'FOO\t1\t3\n'
108
'B\ty\tz\n'
109
'FOO\t4\t6\n'
110
'B\ty\tz\n')
111
112
def test_nested_list_of_lists(self):
113
self.assert_text_renders_to(
114
[['1', '2', '3'], ['4', '5', '6']],
115
'1\t2\t3\n'
116
'4\t5\t6\n'
117
)
118
119
def test_deeply_nested_lists(self):
120
self.assert_text_renders_to(
121
[
122
[['1', '2', '3'], ['4', '5', '6']],
123
[['7', '8', '9'], ['0', '1', '2']],
124
],
125
'1\t2\t3\n'
126
'4\t5\t6\n'
127
'7\t8\t9\n'
128
'0\t1\t2\n'
129
)
130
131
def test_unicode_text(self):
132
self.assert_text_renders_to([['1', '2', u'\u2713']],
133
u'1\t2\t\u2713\n')
134
135
def test_single_scalar_value(self):
136
self.assert_text_renders_to('foobarbaz', 'foobarbaz\n')
137
138
def test_empty_list(self):
139
self.assert_text_renders_to([], '')
140
141
def test_empty_inner_list(self):
142
self.assert_text_renders_to([[]], '')
143
144
def test_deeploy_nested_empty_list(self):
145
self.assert_text_renders_to([[[[]]]], '')
146
147
def test_deeploy_nested_single_scalar(self):
148
self.assert_text_renders_to([[[['a']]]], 'a\n')
149
150
def test_empty_list_mock_calls(self):
151
# We also need this test as well as test_empty_list
152
# because we want to ensure that write() is never called with
153
# a list object.
154
fake_stream = mock.Mock()
155
self.format_text(data=[], stream=fake_stream)
156
# We should not call .write() at all for an empty list.
157
self.assertFalse(fake_stream.write.called)
158
159
def test_list_of_strings_in_dict(self):
160
self.assert_text_renders_to(
161
{'KeyName': ['a', 'b', 'c']},
162
'KEYNAME\ta\n'
163
'KEYNAME\tb\n'
164
'KEYNAME\tc\n')
165
166
def test_inconsistent_sublists(self):
167
self.assert_text_renders_to(
168
[
169
[['1', '2'], ['3', '4', '5', '6']],
170
[['7', '8', '9'], ['0']]
171
],
172
'1\t2\n'
173
'3\t4\t5\t6\n'
174
'7\t8\t9\n'
175
'0\n'
176
)
177
178
def test_lists_mixed_with_scalars(self):
179
self.assert_text_renders_to(
180
[
181
['a', 'b', ['c', 'd']],
182
['e', 'f', ['g', 'h']]
183
],
184
'a\tb\n'
185
'c\td\n'
186
'e\tf\n'
187
'g\th\n'
188
)
189
190
def test_deeply_nested_with_scalars(self):
191
self.assert_text_renders_to(
192
[
193
['a', 'b', ['c', 'd', ['e', 'f', ['g', 'h']]]],
194
['i', 'j', ['k', 'l', ['m', 'n', ['o', 'p']]]],
195
],
196
'a\tb\n'
197
'c\td\n'
198
'e\tf\n'
199
'g\th\n'
200
'i\tj\n'
201
'k\tl\n'
202
'm\tn\n'
203
'o\tp\n'
204
)
205
206
def test_deeply_nested_with_identifier(self):
207
self.assert_text_renders_to(
208
{'foo': [
209
['a', 'b', ['c', 'd']],
210
['e', 'f', ['g', 'h']]
211
]},
212
'FOO\ta\n'
213
'FOO\tb\n'
214
'FOO\tc\n'
215
'FOO\td\n'
216
'FOO\te\n'
217
'FOO\tf\n'
218
'FOO\tg\n'
219
'FOO\th\n'
220
)
221
222
223
if __name__ == '__main__':
224
unittest.main()
225
226