Debian Codegraph @

tests/test_multilinewrap.py

raw
1 # Copyright (c) Meta Platforms, Inc. and affiliates.
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3
4 from _drgn_util.multilinewrap import multiline_fill, multiline_wrap
5 from tests import TestCase
6
7
8 class TestMultilineWrap(TestCase):
9 def assert_wrap(self, text, lines, *, width, indent=""):
10 self.assertEqual(multiline_wrap(text, width, indent=indent), lines)
11
12 def test_one_line(self):
13 self.assert_wrap(
14 "Sphinx of black quartz, judge my vow.",
15 [
16 "Sphinx of black quartz,",
17 "judge my vow.",
18 ],
19 width=25,
20 )
21
22 def test_one_paragraph(self):
23 self.assert_wrap(
24 "Sphinx of black\nquartz, judge\nmy vow.",
25 [
26 "Sphinx of black quartz,",
27 "judge my vow.",
28 ],
29 width=25,
30 )
31
32 def test_dedent(self):
33 self.assert_wrap(
34 """
35 Sphinx of black
36 quartz, judge
37 my vow.
38 """,
39 [
40 "Sphinx of black quartz,",
41 "judge my vow.",
42 ],
43 width=25,
44 )
45
46 def test_one_character_words(self):
47 self.assert_wrap("A\nB\nC", ["A B C"], width=25)
48
49 def test_leading_whitespace(self):
50 self.assert_wrap("A\n B\nC", ["A B C"], width=25)
51
52 def test_multiple_paragraphs(self):
53 self.assert_wrap(
54 """
55 Sphinx of black quartz, judge my vow.
56
57 The quick brown fox jumps over the lazy dog.
58 """,
59 [
60 "Sphinx of black quartz,",
61 "judge my vow.",
62 "",
63 "The quick brown fox jumps",
64 "over the lazy dog.",
65 ],
66 width=25,
67 )
68
69 def test_multiple_paragraphs_extra_whitespace(self):
70 self.assert_wrap(
71 """
72
73 Sphinx of black
74 quartz, judge
75 my vow.
76
77
78 The quick brown
79 fox jumps over
80 the lazy dog.
81
82 """,
83 [
84 "Sphinx of black quartz,",
85 "judge my vow.",
86 "",
87 "The quick brown fox jumps",
88 "over the lazy dog.",
89 ],
90 width=25,
91 )
92
93 def test_line_block(self):
94 self.assert_wrap(
95 """
96 |A B C
97 | D E F
98 |G H I
99 """,
100 [
101 "A B C",
102 " D E F",
103 "G H I",
104 ],
105 width=1,
106 )
107
108 def test_line_block_whitespace(self):
109 self.assert_wrap(
110 # Use "\x20" to avoid trailing whitespace warnings.
111 """
112 |A B C
113 |\x20
114 | D E F
115 |
116 |G H I
117 """,
118 [
119 "A B C",
120 " ",
121 " D E F",
122 "",
123 "G H I",
124 ],
125 width=1,
126 )
127
128 def test_line_block_and_paragraphs(self):
129 self.assert_wrap(
130 """
131 Sphinx of black quartz, judge my vow.
132
133 |A B C
134 | D E F
135 |G H I
136
137 The quick brown fox jumps over the lazy dog.
138 """,
139 [
140 "Sphinx of",
141 "black",
142 "quartz,",
143 "judge my",
144 "vow.",
145 "",
146 "A B C",
147 " D E F",
148 "G H I",
149 "",
150 "The quick",
151 "brown fox",
152 "jumps over",
153 "the lazy",
154 "dog.",
155 ],
156 width=10,
157 )
158
159 def test_line_block_in_paragraph(self):
160 self.assert_wrap(
161 """
162 Sphinx of black quartz, judge my vow.
163 |A B C
164 | D E F
165 |G H I
166 The quick brown fox jumps over the lazy dog.
167 """,
168 [
169 "Sphinx of",
170 "black",
171 "quartz,",
172 "judge my",
173 "vow.",
174 "A B C",
175 " D E F",
176 "G H I",
177 "The quick",
178 "brown fox",
179 "jumps over",
180 "the lazy",
181 "dog.",
182 ],
183 width=10,
184 )
185
186 def test_fill(self):
187 self.assertEqual(
188 multiline_fill("Sphinx of black\nquartz, judge\nmy vow.", 25),
189 "Sphinx of black quartz,\njudge my vow.",
190 )