Debian Codegraph @

tests/test_textruns_token_cutting.py

raw
1 # SPDX-FileCopyrightText: © 2023 Christian BUHTZ <[email protected]>
2 #
3 # SPDX-License-Identifier: GPL-3.0-only
4 #
5 # This file is part of the program "Hyperorg" which is released under
6 # GNU General Public License v3 (GPLv3). See LICENSES directory
7 # or go to <https://spdx.org/licenses/GPL-3.0-only.html>.
8 """Tests about runs"""
9 import unittest
10 from hyperorg import textruns
11
12
13 class CutIntoRuns(unittest.TestCase):
14 """Test conversion from text to runs.
15
16 Here with `textruns.cut_into_runs()` the combination of the three
17 functions `textruns._token_positions()`, `textruns._token_pairs()` and
18 `textruns._runs_from_token_pairs()` is tested.
19 """
20
21 # pylint: disable=missing-function-docstring
22 def test_text_only(self):
23 """One simple text run."""
24 text = 'Another one got caught today'
25 result = textruns.cut_into_runs(text)
26
27 self.assertEqual(len(result), 1)
28 self.assertIsInstance(result[0], str)
29
30 def test_empty_string(self):
31 """Empty strings.
32
33 This is e.g. useful with list elements where empty
34 list entries are allowed.
35 """
36 result = textruns.cut_into_runs('')
37 self.assertEqual(len(result), 1)
38 self.assertEqual(result[0], '')
39
40 def test_verbatim_only(self):
41 sut = textruns.cut_into_runs('=--foo=')
42
43 self.assertEqual(len(sut), 1)
44 self.assertEqual(
45 sut[0],
46 textruns.Markup('--foo', textruns.Markup.Type.VERBATIM)
47 )
48
49 def test_markup_combined(self):
50 """Bigger text with multiple markup in it."""
51 text = (
52 '+Governments+ of the *Industrial World*, you weary =giants= of '
53 'flesh and steel, I come from _Cyberspace_, the new home of Mind. '
54 'On /behalf of the future/, I ask you of the past to leave us '
55 'alone. You are ~not welcome~ among us. You have no sovereignty '
56 'where we gather.'
57 )
58
59 result = textruns.cut_into_runs(text)
60
61 self.assertEqual(len(result), 12)
62
63 expected_parts = {
64 1: ' of the ',
65 3: ', you weary ',
66 5: ' of flesh and steel, I come from ',
67 7: ', the new home of Mind. On ',
68 9: ', I ask you of the past to leave us alone. You are ',
69 11: ' among us. You have no sovereignty where we gather.'
70 }
71 for idx, exp in expected_parts.items():
72 self.assertEqual(str(result[idx]), exp)
73
74 expected_parts = {
75 0: 'Governments',
76 2: 'Industrial World',
77 4: 'giants',
78 6: 'Cyberspace',
79 8: 'behalf of the future',
80 10: 'not welcome',
81 }
82 for idx, exp in expected_parts.items():
83 self.assertEqual(str(result[idx].runs[0]), exp)
84
85 marker_at_index = [
86 (0, textruns.Markup.Type.STRIKETHROUGH),
87 (2, textruns.Markup.Type.IMPORTANT),
88 (4, textruns.Markup.Type.VERBATIM),
89 (6, textruns.Markup.Type.UNDERLINED),
90 (8, textruns.Markup.Type.EMPHASIZED),
91 (10, textruns.Markup.Type.CODE)
92 ]
93
94 for idx, markup_type in marker_at_index:
95 self.assertEqual(result[idx].markup_type, markup_type)
96
97 def test_links_combined_two(self):
98 test = (
99 'This is [[id:1234][World]] and [[roam:Buhtz][Me]] ende'
100 )
101
102 result = textruns.cut_into_runs(test)
103
104 self.assertEqual(len(result), 5)
105
106 self.assertEqual(result[0], 'This is ')
107 self.assertEqual(result[1][0], 'World')
108 self.assertEqual(result[1].target, '1234')
109 self.assertEqual(result[2], ' and ')
110 self.assertEqual(result[3][0], 'Me')
111 self.assertEqual(result[3].target, 'Buhtz')
112 self.assertEqual(result[4], ' ende')
113
114 def test_links_combined_all(self):
115 test = (
116 'This is [[https://world.me][World]] and [[roam:Buhtz][Me]] '
117 'or just [[roam:Buhtz]]. Also try [[id:12345][with label]] or '
118 'that [[https://out.there]] somewhere.'
119 )
120
121 result = textruns.cut_into_runs(test)
122
123 self.assertEqual(len(result), 11)
124
125 self.assertEqual(result[1][0], 'World')
126 self.assertEqual(result[1].target, 'world.me')
127 self.assertEqual(result[3][0], 'Me')
128 self.assertEqual(result[3].target, 'Buhtz')
129 self.assertEqual(len(result[5]), 0)
130 self.assertEqual(result[5].target, 'Buhtz')
131 self.assertEqual(result[7][0], 'with label')
132 self.assertEqual(result[7].target, '12345')
133 self.assertEqual(len(result[9]), 0)
134 self.assertEqual(result[9].target, 'out.there')
135
136 def test_links_and_markup_neighbors(self):
137 test = 'you _weary_ [[https://big.com][giants]]'
138
139 result = textruns.cut_into_runs(test)
140
141 self.assertEqual(len(result), 4)
142
143 self.assertEqual(result[0], 'you ')
144 self.assertEqual(result[1][0], 'weary')
145 self.assertEqual(result[1].markup_type,
146 textruns.Markup.Type.UNDERLINED)
147 self.assertEqual(result[2], ' ')
148 self.assertEqual(result[3][0], 'giants')
149 self.assertEqual(result[3].target, 'big.com')
150 self.assertEqual(result[3].link_type, textruns.Link.Type.HTTPS)
151
152 def test_links_variants(self):
153 test_data = [
154 ( # orgid
155 'This [[id:0123456789][is an OrgId link]] test.',
156 {
157 1: [textruns.Link.Type.ORGID,
158 '0123456789', 'is an OrgId link'],
159 },
160 ),
161 ( # roam with labels
162 'This [[roam:0123456789][is a Roam link]] test.',
163 {
164 1: [textruns.Link.Type.ROAM,
165 '0123456789', 'is a Roam link'],
166 },
167 ),
168 ( # roam without labels
169 'This [[roam:Buhtz]] test.',
170 {
171 1: [textruns.Link.Type.ROAM, 'Buhtz', None],
172 },
173 ),
174 ( # https with label
175 'This [[https://my.world][Buhtz]] test.',
176 {
177 1: [textruns.Link.Type.HTTPS, 'my.world', 'Buhtz'],
178 },
179 ),
180 ( # two orgid
181 'This [[id:01234][Foo]] and [[id:5678][bar]].',
182 {
183 1: [textruns.Link.Type.ORGID, '01234', 'Foo'],
184 3: [textruns.Link.Type.ORGID, '5678', 'bar'],
185 },
186 ),
187 ]
188
189 for text, links in test_data:
190 result = textruns.cut_into_runs(text)
191
192 for idx, lnk in links.items():
193 kind, target, label = lnk
194
195 self.assertEqual(result[idx].link_type, kind)
196 self.assertEqual(result[idx].target, target)
197
198 if label:
199 self.assertEqual(result[idx][0], label)
200 else:
201 self.assertEqual(len(result[idx]), 0)
202
203 def test_links_label_newline(self):
204 text = 'This [[id:123][is an\nOrgId link]] test.'
205 result = textruns.cut_into_runs(text)
206
207 self.assertEqual(result[1][0], 'is an\nOrgId link')
208
209 def test_url_with_slash_at_end(self):
210 """If there is a slash at the end of an URL it was (see #64)
211 interpreted as emphasized inline markup.
212 """
213 text = 'Foo [[https://my.world/]] bar'
214 result = textruns.cut_into_runs(text)
215
216 self.assertEqual(result[0], 'Foo ')
217 self.assertEqual(len(result[1]), 0)
218 self.assertEqual(result[1].target, 'my.world/')
219 self.assertEqual(result[2], ' bar')
220
221 def test_multiple_links_and_markup_all(self):
222 test = (
223 '*Governments* of the [[roam:Industrial World][IW]], you _weary_'
224 ' [[https://big.com][giants]] /of/ [[id:1854][flesh]] and '
225 '=steel=, +I+ come from [[roam:Cyberspace]], the ~new home~ of '
226 '[[https://Mind.org]]. On behalf of the future'
227 )
228
229 result = textruns.cut_into_runs(test)
230
231 self.assertEqual(len(result), 22)
232
233 # the key here is the index in ``result``
234 check_links = {
235 2: ('Industrial World', 'IW', textruns.Link.Type.ROAM),
236 6: ('big.com', 'giants', textruns.Link.Type.HTTPS),
237 10: ('1854', 'flesh', textruns.Link.Type.ORGID),
238 16: ('Cyberspace', None, textruns.Link.Type.ROAM),
239 20: ('Mind.org', None, textruns.Link.Type.HTTPS)
240 }
241
242 for idx_key, links in check_links.items():
243 target, label, kind = links
244 self.assertEqual(result[idx_key].target, target)
245
246 if label:
247 self.assertEqual(result[idx_key][0], label)
248 else:
249 self.assertEqual(len(result[idx_key]), 0)
250
251 self.assertEqual(result[idx_key].link_type, kind)
252
253 check_markup = {
254 0: ('Governments', textruns.Markup.Type.IMPORTANT),
255 4: ('weary', textruns.Markup.Type.UNDERLINED),
256 8: ('of', textruns.Markup.Type.EMPHASIZED),
257 12: ('steel', textruns.Markup.Type.VERBATIM),
258 14: ('I', textruns.Markup.Type.STRIKETHROUGH),
259 18: ('new home', textruns.Markup.Type.CODE)
260 }
261
262 for idx_key, markup in check_markup.items():
263 text, kind = markup
264 self.assertEqual(len(result[idx_key]), 1)
265 self.assertEqual(result[idx_key][0], text)
266 self.assertEqual(result[idx_key].markup_type, kind)
267
268 def test_markup_without_prefix_suffix(self):
269 sut = textruns.cut_into_runs('+Government+')
270
271 self.assertEqual(len(sut), 1)
272
273 self.assertEqual(sut[0][0], 'Government')
274 self.assertEqual(
275 sut[0].markup_type,
276 textruns.Markup.Type.STRIKETHROUGH
277 )
278
279 def test_markup_prefix_suffix(self):
280 result = textruns.cut_into_runs('foo +Government+ bar')
281
282 self.assertEqual(len(result), 3)
283 self.assertEqual(str(result[0]), 'foo ')
284 self.assertEqual(str(result[2]), ' bar')
285
286 self.assertIsInstance(result[1], textruns.Markup)
287 markup = result[1]
288
289 self.assertEqual(str(markup.runs[0]), 'Government')
290 self.assertEqual(
291 markup.markup_type,
292 textruns.Markup.Type.STRIKETHROUGH
293 )
294
295 def test_markup_variant_blanks(self):
296 result = textruns.cut_into_runs(' +Government+ ')
297
298 self.assertEqual(len(result), 3)
299 self.assertEqual(str(result[0]), ' ')
300 self.assertEqual(str(result[2]), ' ')
301
302 self.assertEqual(str(result[1].runs[0]), 'Government')
303 self.assertEqual(result[1].markup_type,
304 textruns.Markup.Type.STRIKETHROUGH)
305
306 def test_markup_two(self):
307 for markup_type in textruns.Markup.Type:
308 text = 'Governments {m}of{m} the {m}Industrial{m} World' \
309 .format(m=markup_type.value)
310
311 result = textruns.cut_into_runs(text)
312
313 self.assertEqual(len(result), 5, markup_type)
314 self.assertEqual(str(result[0]), 'Governments ', markup_type)
315 self.assertEqual(str(result[1].runs[0]), 'of', markup_type)
316 self.assertEqual(str(result[2]), ' the ', markup_type)
317 self.assertEqual(str(result[3].runs[0]), 'Industrial', markup_type)
318 self.assertEqual(str(result[4]), ' World', markup_type)
319
320 def test_markup_two_without_pre_suffix(self):
321 for markup_type in textruns.Markup.Type:
322 text = '{m}Governments{m} of {m}the{m} Industrial {m}World{m}' \
323 .format(m=markup_type.value)
324
325 result = textruns.cut_into_runs(text)
326
327 self.assertEqual(len(result), 5, markup_type)
328 self.assertEqual(str(result[0].runs[0]),
329 'Governments',
330 markup_type)
331 self.assertEqual(str(result[1]), ' of ', markup_type)
332 self.assertEqual(str(result[2].runs[0]), 'the', markup_type)
333 self.assertEqual(str(result[3]), ' Industrial ', markup_type)
334 self.assertEqual(str(result[4].runs[0]), 'World', markup_type)
335
336 def test_markup_entangled(self):
337 """First wins."""
338 text = '/italics ~code/ verbatim~'
339 result = textruns.cut_into_runs(text)
340
341 self.assertEqual(len(result), 2)
342 self.assertEqual(str(result[0].runs[0]), 'italics ~code')
343 self.assertEqual(str(result[1]), ' verbatim~')
344
345 def test_unsecure_http(self):
346 """HTTP links are not secure but will be parsed.
347
348 See #29 about an improvement of that behavior
349 unsafe (because of HTTP-only) hyperlinks not parsed
350 """
351 text = 'This [[http://foo.bar][foobar]] is not safe.'
352 result = textruns.cut_into_runs(text)
353
354 self.assertEqual(len(result), 3)
355 self.assertEqual(result[1][0], 'foobar')
356 self.assertEqual(result[1].target, 'foo.bar')
357 self.assertEqual(result[1].link_type, textruns.Link.Type.HTTP)