This repository was archived by the owner on Mar 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathtest_lines.py
More file actions
312 lines (238 loc) · 9.71 KB
/
test_lines.py
File metadata and controls
312 lines (238 loc) · 9.71 KB
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
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from gapic.utils import lines
def test_sort_lines():
assert (
lines.sort_lines(
"import foo\nimport bar",
)
== "import bar\nimport foo"
)
def test_sort_lines_keeps_leading_newline():
assert (
lines.sort_lines(
"\nimport foo\nimport bar",
)
== "\nimport bar\nimport foo"
)
def test_sort_lines_keeps_trailing_newline():
assert (
lines.sort_lines(
"import foo\nimport bar\n",
)
== "import bar\nimport foo\n"
)
def test_sort_lines_eliminates_blank_lines():
assert (
lines.sort_lines(
"import foo\n\n\nimport bar",
)
== "import bar\nimport foo"
)
def test_sort_lines_dedupe():
assert (
lines.sort_lines(
"import foo\nimport bar\nimport foo",
)
== "import bar\nimport foo"
)
def test_sort_lines_no_dedupe():
assert (
lines.sort_lines(
"import foo\nimport bar\nimport foo",
dedupe=False,
)
== "import bar\nimport foo\nimport foo"
)
def test_wrap_noop():
assert lines.wrap("foo bar baz", width=80) == "foo bar baz"
def test_wrap_empty_text():
assert lines.wrap("", width=80) == ""
def test_wrap_simple():
assert lines.wrap("foo bar baz", width=5) == "foo\nbar\nbaz"
def test_wrap_strips():
assert lines.wrap("foo bar baz ", width=80) == "foo bar baz"
def test_wrap_subsequent_offset():
assert (
lines.wrap(
"foo bar baz",
width=5,
offset=0,
indent=2,
)
== "foo\n bar\n baz"
)
def test_wrap_initial_offset():
assert (
lines.wrap(
"The hail in Wales falls mainly on the snails.",
width=20,
offset=12,
indent=0,
)
== "The hail\nin Wales falls\nmainly on the\nsnails."
)
def test_wrap_indent_short():
assert lines.wrap("foo bar", width=30, indent=10) == "foo bar"
def test_wrap_short_line_preserved():
assert lines.wrap("foo\nbar\nbaz", width=80) == "foo\nbar\nbaz"
def test_wrap_does_not_break_hyphenated_word():
assert lines.wrap("do-not-break", width=5) == "do-not-break"
def test_wrap_with_short_lines():
input = """The hail in Wales falls mainly on the snails. The hail in Wales falls mainly
on the snails."""
expected = """The hail in Wales falls mainly on the snails. The hail in
Wales falls mainly on the snails."""
assert lines.wrap(input, width=60) == expected
def test_lines_which_have_2_spaces_following_period():
input = """Information related to the a standard versioned package. This includes
package info for APT, Yum, Zypper, and Googet package managers."""
expected = """Information related to the a standard versioned package.
This includes package info for APT, Yum, Zypper, and Googet
package managers."""
assert lines.wrap(input, width=60) == expected
def test_list_each_item_in_list_has_new_line():
input = """Type of weather:
- Hail
- Rain Rain Rain Rain Rain Rain Rain Rain Rain Rain Rain Rain
- Snow"""
expected = """Type of weather:
- Hail
- Rain Rain Rain Rain Rain Rain Rain Rain Rain Rain Rain Rain
- Snow"""
assert lines.wrap(input, width=80) == expected
def test_list_items_are_indented():
input = """Type of weather.
Some types of weather:
- A mix of hail and snow, followed by rain clouds, then finally clear sky
- Rain
- Snow"""
expected = """Type of weather.
Some types of weather:
- A mix of hail and snow, followed by rain clouds, then
finally clear sky
- Rain
- Snow"""
assert lines.wrap(input, width=60) == expected
def test_list_items_short_text_before_list_with_new_line_preserved():
input = """Today's forecast will have different types of weather:
- A mix of hail and snow, followed by rain clouds, then finally clear sky
- Rain
- Snow"""
expected = """Today's forecast will have different types
of weather:
- A mix of hail and snow, followed by rain
clouds, then finally clear sky
- Rain
- Snow"""
assert lines.wrap(input, width=60, indent=16) == expected
def test_list_items_long_text_before_list_with_new_line_preserved():
input = """Weather Weather Weather Weather Weather Weather Weather
Weather Weather Weather Weather Weather Weather Type of weather:
- Hail
- Rain Rain Rain Rain Rain Rain Rain Rain Rain Rain Rain Rain
- Snow"""
expected = """Weather Weather Weather Weather Weather Weather Weather
Weather Weather Weather Weather Weather Weather Type of
weather:
- Hail
- Rain Rain Rain Rain Rain Rain Rain Rain Rain Rain Rain
Rain
- Snow"""
assert lines.wrap(input, width=60) == expected
def test_new_line_added_short_text_before_list():
input = """Today's forecast will have different weather:
- A mix of hail and snow, followed by rain clouds, then finally clear sky
- Rain
- Snow"""
expected = """Today's forecast will have different weather:
- A mix of hail and snow, followed by rain clouds, then
finally clear sky
- Rain
- Snow"""
assert lines.wrap(input, width=60) == expected
def test_new_line_preserved_short_text_before_list_without_colon():
input = """Today's forecast will have different weather.
- A mix of hail and snow, followed by rain clouds, then finally clear sky
- Rain
- Snow"""
expected = """Today's forecast will have different weather.
- A mix of hail and snow, followed by rain clouds, then
finally clear sky
- Rain
- Snow"""
assert lines.wrap(input, width=60) == expected
def test_list_with_multiple_paragraphs():
input = """Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec porta euismod est a viverra. Integer vulputate ipsum id lacus tincidunt, id tincidunt tortor ullamcorper. Vestibulum facilisis at nulla nec lobortis. Nunc consectetur suscipit lacus id aliquam.
Donec et urna aliquam, efficitur mauris et, consectetur enim. Aliquam aliquet turpis eget erat gravida condimentum. Sed vel feugiat risus.
Sed interdum.
Convallis turpis nec congue. Integer vulputate sed urna eu mollis. Mauris in congue nisi, sed pellentesque ex.
- Ut vestibulum
- consequat imperdiet
- Integer rhoncus varius. Ante, ac tempus augue
finibus sit amet. Integer ac fermentum neque, a sodales nibh. Mauris et dictum ipsum. Integer sit amet posuere urna. Nullam cursus molestie posuere. Praesent imperdiet cursus purus, in posuere odio.
- Orci varius natoque penatibus et
Aagnis dis parturient montes, nascetur ridiculus mus. Mauris mattis turpis quis hendrerit gravida. Curabitur nec diam erat. In nec est nisl. Quisque ut orci efficitur, vestibulum ante non, vestibulum erat. Donec mollis ultricies nisl."""
expected = """Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Donec porta euismod est a viverra. Integer vulputate ipsum
id lacus tincidunt, id tincidunt tortor ullamcorper.
Vestibulum facilisis at nulla nec lobortis. Nunc consectetur
suscipit lacus id aliquam. Donec et urna aliquam, efficitur
mauris et, consectetur enim. Aliquam aliquet turpis eget
erat gravida condimentum. Sed vel feugiat risus.
Sed interdum.
Convallis turpis nec congue. Integer vulputate sed urna eu
mollis. Mauris in congue nisi, sed pellentesque ex.
- Ut vestibulum
- consequat imperdiet
- Integer rhoncus varius. Ante, ac tempus augue finibus sit
amet. Integer ac fermentum neque, a sodales nibh. Mauris
et dictum ipsum. Integer sit amet posuere urna. Nullam
cursus molestie posuere. Praesent imperdiet cursus purus,
in posuere odio.
- Orci varius natoque penatibus et
Aagnis dis parturient montes, nascetur ridiculus mus. Mauris
mattis turpis quis hendrerit gravida. Curabitur nec diam
erat. In nec est nisl. Quisque ut orci efficitur, vestibulum
ante non, vestibulum erat. Donec mollis ultricies nisl."""
assert lines.wrap(input, width=60) == expected
def test_list_with_numbered_list():
input = """Config for video classification human labeling task.
Currently two types of video classification are supported:
1. Assign labels on the entire video. Assign labels on the entire video.
22. Split the video into multiple video clips based on camera shot, and
assign labels on each video clip."""
expected = """Config for video classification human labeling task.
Currently two types of video classification are supported:
1. Assign labels on the entire video. Assign labels on the
entire video.
22. Split the video into multiple video clips based on
camera shot, and assign labels on each video clip."""
assert lines.wrap(input, width=60) == expected
def test_list_with_plus_list_item_marker():
input = """User-assigned name of the trigger. Must be unique within the project.
Trigger names must meet the following requirements:
+ They must contain only alphanumeric characters and dashes.
+ They can be 1-64 characters long.
+ They must begin and end with an alphanumeric character."""
expected = """User-assigned name of the trigger. Must
be unique within the project. Trigger
names must meet the following
requirements:
+ They must contain only alphanumeric
characters and dashes.
+ They can be 1-64 characters long.
+ They must begin and end with an
alphanumeric character."""
assert lines.wrap(input, width=40) == expected