| 1 |
# SPDX-FileCopyrightText: © 2023 Christian BUHTZ <[email protected]> |
| 2 |
# SPDX-FileCopyrightText: © 2025 Michael Olie <[email protected]> |
| 3 |
# |
| 4 |
# SPDX-License-Identifier: GPL-3.0-only |
| 5 |
# |
| 6 |
# This file is part of the program "Hyperorg" which is released under |
| 7 |
# GNU General Public License v3 (GPLv3). See LICENSES directory |
| 8 |
# or go to <https://spdx.org/licenses/GPL-3.0-only.html>. |
| 9 |
# pylint: disable=too-many-lines |
| 10 |
"""Tests about tokens and creating runs based on tokens.""" |
| 11 |
import unittest |
| 12 |
from hyperorg import textruns |
| 13 |
|
| 14 |
|
| 15 |
class TokenPositions(unittest.TestCase): |
| 16 |
"""Test `textruns._token_position()`""" |
| 17 |
|
| 18 |
# pylint: disable=missing-function-docstring,protected-access |
| 19 |
def test_token_close(self): |
| 20 |
self.assertEqual( |
| 21 |
textruns._token_positions('Foo* bar'), |
| 22 |
[(3, textruns.Token.IMPORTANT, textruns.TokenState.CLOSE)] |
| 23 |
) |
| 24 |
|
| 25 |
def test_token_open(self): |
| 26 |
self.assertEqual( |
| 27 |
textruns._token_positions('Foo *bar'), |
| 28 |
[(4, textruns.Token.IMPORTANT, textruns.TokenState.OPEN)] |
| 29 |
) |
| 30 |
|
| 31 |
def test_all_tokens(self): |
| 32 |
"""All kinds of tokens.""" |
| 33 |
for token in textruns.Token: |
| 34 |
if token == textruns.Token.LINK: |
| 35 |
text = 'Foo [[marku]] bar' |
| 36 |
expect = [ |
| 37 |
(4, token, textruns.TokenState.OPEN), |
| 38 |
(11, token, textruns.TokenState.CLOSE) |
| 39 |
] |
| 40 |
|
| 41 |
# elif token == textruns.Token.MATH_DOLLAR: |
| 42 |
# text = 'Foo $marku$ bar' |
| 43 |
# expect = [ |
| 44 |
# (4, token, textruns.TokenState.OPEN), |
| 45 |
# (10, token, textruns.TokenState.CLOSE) |
| 46 |
# ] |
| 47 |
elif token == textruns.Token.MATH_PARENS: |
| 48 |
text = r'Foo \(marku\) bar' |
| 49 |
expect = [ |
| 50 |
(4, token, textruns.TokenState.OPEN), |
| 51 |
(11, token, textruns.TokenState.CLOSE) |
| 52 |
] |
| 53 |
# elif token == textruns.Token.MATH_DBL_DOLLAR: |
| 54 |
# text = r'Foo $$marku$$ bar' |
| 55 |
# expect = [ |
| 56 |
# (4, token, textruns.TokenState.OPEN), |
| 57 |
# (12, token, textruns.TokenState.CLOSE) |
| 58 |
# ] |
| 59 |
# elif token == textruns.Token.MATH_BRACKETS: |
| 60 |
# text = r'Foo \[marku\] bar' |
| 61 |
# expect = [ |
| 62 |
# (4, token, textruns.TokenState.OPEN), |
| 63 |
# (11, token, textruns.TokenState.CLOSE) |
| 64 |
# ] |
| 65 |
|
| 66 |
else: |
| 67 |
text = f'Foo {token.value}marku{token.value} bar' |
| 68 |
expect = [ |
| 69 |
(4, token, textruns.TokenState.OPEN), |
| 70 |
(10, token, textruns.TokenState.CLOSE) |
| 71 |
] |
| 72 |
|
| 73 |
result = textruns._token_positions(text) |
| 74 |
self.assertEqual(result, expect) |
| 75 |
|
| 76 |
def test_side_by_side(self): |
| 77 |
"""One markup after the other.""" |
| 78 |
text = 'Foo ~code~ and /italic/ bar' |
| 79 |
self.assertEqual( |
| 80 |
textruns._token_positions(text), |
| 81 |
[ |
| 82 |
(4, textruns.Token.CODE, textruns.TokenState.OPEN), |
| 83 |
(9, textruns.Token.CODE, textruns.TokenState.CLOSE), |
| 84 |
(15, textruns.Token.EMPHASIZED, textruns.TokenState.OPEN), |
| 85 |
(22, textruns.Token.EMPHASIZED, textruns.TokenState.CLOSE), |
| 86 |
] |
| 87 |
) |
| 88 |
|
| 89 |
def test_entangled(self): |
| 90 |
"""Entangled markup tokens. |
| 91 |
|
| 92 |
It doesn't matter at this point if this are correct pairs or not. |
| 93 |
""" |
| 94 |
|
| 95 |
text = '/Worf *Picard/ Diana*' |
| 96 |
self.assertEqual( |
| 97 |
textruns._token_positions(text), |
| 98 |
[ |
| 99 |
(0, textruns.Token.EMPHASIZED, textruns.TokenState.OPEN), |
| 100 |
(6, textruns.Token.IMPORTANT, textruns.TokenState.OPEN), |
| 101 |
(13, textruns.Token.EMPHASIZED, textruns.TokenState.CLOSE), |
| 102 |
(20, textruns.Token.IMPORTANT, textruns.TokenState.CLOSE), |
| 103 |
] |
| 104 |
) |
| 105 |
|
| 106 |
def test_enclosed(self): |
| 107 |
"""Nested markup tokens.""" |
| 108 |
text = 'Frank /Burns *is* a/ fret.' |
| 109 |
self.assertEqual( |
| 110 |
textruns._token_positions(text), |
| 111 |
[ |
| 112 |
(6, textruns.Token.EMPHASIZED, textruns.TokenState.OPEN), |
| 113 |
(13, textruns.Token.IMPORTANT, textruns.TokenState.OPEN), |
| 114 |
(16, textruns.Token.IMPORTANT, textruns.TokenState.CLOSE), |
| 115 |
(19, textruns.Token.EMPHASIZED, textruns.TokenState.CLOSE), |
| 116 |
] |
| 117 |
) |
| 118 |
|
| 119 |
def test_link(self): |
| 120 |
"""Link tokens.""" |
| 121 |
text = 'Giants [[id:1234][Foo]] Cyberspace' |
| 122 |
result = textruns._token_positions(text) |
| 123 |
|
| 124 |
self.assertEqual( |
| 125 |
result, |
| 126 |
[ |
| 127 |
(7, textruns.Token.LINK, textruns.TokenState.OPEN), |
| 128 |
(21, textruns.Token.LINK, textruns.TokenState.CLOSE) |
| 129 |
] |
| 130 |
) |
| 131 |
|
| 132 |
def test_none_token(self): |
| 133 |
""" |
| 134 |
Blanks after OPEN or before CLOSE is not allowed. It is not a token. |
| 135 |
""" |
| 136 |
self.assertEqual( |
| 137 |
textruns._token_positions(" / foobar /"), |
| 138 |
[] |
| 139 |
) |
| 140 |
|
| 141 |
|
| 142 |
class TokenPairs(unittest.TestCase): |
| 143 |
"""Test `textruns._token_pairs()`""" |
| 144 |
|
| 145 |
# pylint: disable=missing-function-docstring,protected-access |
| 146 |
def test_all_tokens(self): |
| 147 |
for token in textruns.Token: |
| 148 |
positions = [ |
| 149 |
(6, token, textruns.TokenState.OPEN), |
| 150 |
(20, token, textruns.TokenState.CLOSE)] |
| 151 |
expect = [(token, 6, 20)] |
| 152 |
|
| 153 |
self.assertEqual(textruns._token_pairs(positions), expect) |
| 154 |
|
| 155 |
def test_side_by_side(self): |
| 156 |
positions = [ |
| 157 |
(2, textruns.Token.EMPHASIZED, textruns.TokenState.OPEN), |
| 158 |
(6, textruns.Token.EMPHASIZED, textruns.TokenState.CLOSE), |
| 159 |
(9, textruns.Token.IMPORTANT, textruns.TokenState.OPEN), |
| 160 |
(14, textruns.Token.IMPORTANT, textruns.TokenState.CLOSE) |
| 161 |
] |
| 162 |
|
| 163 |
self.assertEqual( |
| 164 |
textruns._token_pairs(positions), |
| 165 |
[ |
| 166 |
(textruns.Token.EMPHASIZED, 2, 6), |
| 167 |
(textruns.Token.IMPORTANT, 9, 14), |
| 168 |
] |
| 169 |
) |
| 170 |
|
| 171 |
def test_enclosed(self): |
| 172 |
"""The encloused markup is deleted. |
| 173 |
|
| 174 |
The intention is to parse the inner content of the markup in another |
| 175 |
iteration. |
| 176 |
""" |
| 177 |
|
| 178 |
# The two inner tokens are ignored |
| 179 |
positions = [ |
| 180 |
(2, textruns.Token.EMPHASIZED, textruns.TokenState.OPEN), |
| 181 |
(9, textruns.Token.IMPORTANT, textruns.TokenState.OPEN), |
| 182 |
(14, textruns.Token.IMPORTANT, textruns.TokenState.CLOSE), |
| 183 |
(20, textruns.Token.EMPHASIZED, textruns.TokenState.CLOSE), |
| 184 |
] |
| 185 |
|
| 186 |
self.assertEqual( |
| 187 |
textruns._token_pairs(positions), |
| 188 |
[ |
| 189 |
(textruns.Token.EMPHASIZED, 2, 20), |
| 190 |
] |
| 191 |
) |
| 192 |
|
| 193 |
def test_entangled(self): |
| 194 |
"""First wins. |
| 195 |
|
| 196 |
The pairs are entangled like 'a /b *c/ d* e'. The first wins means |
| 197 |
that the emphasized is closed first and valid in that case. |
| 198 |
""" |
| 199 |
positions = [ |
| 200 |
(0, textruns.Token.EMPHASIZED, textruns.TokenState.OPEN), |
| 201 |
(6, textruns.Token.IMPORTANT, textruns.TokenState.OPEN), |
| 202 |
(13, textruns.Token.EMPHASIZED, textruns.TokenState.CLOSE), |
| 203 |
(20, textruns.Token.IMPORTANT, textruns.TokenState.CLOSE) |
| 204 |
] |
| 205 |
|
| 206 |
self.assertEqual( |
| 207 |
textruns._token_pairs(positions), |
| 208 |
[(textruns.Token.EMPHASIZED, 0, 13)] |
| 209 |
) |
| 210 |
|
| 211 |
def test_link(self): |
| 212 |
positions = [ |
| 213 |
(7, textruns.Token.LINK, textruns.TokenState.OPEN), |
| 214 |
(21, textruns.Token.LINK, textruns.TokenState.CLOSE) |
| 215 |
] |
| 216 |
|
| 217 |
self.assertEqual( |
| 218 |
textruns._token_pairs(positions), |
| 219 |
[(textruns.Token.LINK, 7, 21)] |
| 220 |
) |
| 221 |
|
| 222 |
|
| 223 |
class RunsFromTokenPairs(unittest.TestCase): |
| 224 |
"""Test `textruns._runs_from_token_pairs()`""" |
| 225 |
|
| 226 |
# pylint: disable=missing-function-docstring,protected-access |
| 227 |
def test_markup_tokens(self): |
| 228 |
""" |
| 229 |
Testing all markup token types (excluding links and math). |
| 230 |
This test checks that all markup token types are handled |
| 231 |
correctly by `_runs_from_token_pairs`. |
| 232 |
The test uses a generic marker ('X') in the string, as the marker |
| 233 |
itself does not affect the parsing—only its position does. |
| 234 |
""" |
| 235 |
|
| 236 |
for token in textruns.Token: |
| 237 |
# Skip non-markup tokens |
| 238 |
non_markup_tokens = ( |
| 239 |
textruns.Token.LINK, |
| 240 |
textruns.Token.MATH_DOLLAR, |
| 241 |
textruns.Token.MATH_PARENS, |
| 242 |
# textruns.Token.MATH_DBL_DOLLAR, |
| 243 |
# textruns.Token.MATH_BRACKETS |
| 244 |
) |
| 245 |
if token in non_markup_tokens: |
| 246 |
continue |
| 247 |
|
| 248 |
text = 'Riker XwithX Data' |
| 249 |
pairs = [(token, 6, 11)] |
| 250 |
result = textruns._runs_from_token_pairs(text, pairs) |
| 251 |
|
| 252 |
self.assertEqual(str(result[0]), 'Riker ') |
| 253 |
self.assertEqual(str(result[1].runs[0]), 'with') |
| 254 |
self.assertEqual(result[1].markup_type.value, token.value) |
| 255 |
self.assertEqual(result[2], ' Data') |
| 256 |
|
| 257 |
def test_link(self): |
| 258 |
text = 'Riker [[id:with]] Data' |
| 259 |
pairs = [(textruns.Token.LINK, 6, 15)] |
| 260 |
|
| 261 |
result = textruns._runs_from_token_pairs(text, pairs) |
| 262 |
|
| 263 |
self.assertEqual(result[0], 'Riker ') |
| 264 |
self.assertEqual(len(result[1]), 0) |
| 265 |
self.assertEqual(result[1].target, 'with') |
| 266 |
self.assertEqual(result[1].link_type, textruns.Link.Type.ORGID) |
| 267 |
self.assertEqual(result[2], ' Data') |
| 268 |
|
| 269 |
def test_inlinemath_dollars(self): |
| 270 |
"""Test MATH_DOLLAR token handling.""" |
| 271 |
text = 'Riker $with$ Data' |
| 272 |
pairs = [(textruns.Token.MATH_DOLLAR, 6, 11)] |
| 273 |
result = textruns._runs_from_token_pairs(text, pairs) |
| 274 |
|
| 275 |
self.assertEqual(str(result[0]), 'Riker ') |
| 276 |
self.assertIsInstance(result[1], textruns.InlineMath) |
| 277 |
self.assertEqual(str(result[1].runs[0]), 'with') |
| 278 |
self.assertEqual(result[2], ' Data') |
| 279 |
|
| 280 |
def test_inlinemath_parens(self): |
| 281 |
"""Test MATH_PARENS token handling.""" |
| 282 |
text = r'Riker \(with\) Data' |
| 283 |
pairs = [(textruns.Token.MATH_PARENS, 6, 12)] |
| 284 |
result = textruns._runs_from_token_pairs(text, pairs) |
| 285 |
|
| 286 |
self.assertEqual(str(result[0]), 'Riker ') |
| 287 |
self.assertIsInstance(result[1], textruns.InlineMath) |
| 288 |
self.assertEqual(str(result[1].runs[0]), 'with') |
| 289 |
self.assertEqual(result[2], ' Data') |
| 290 |
|
| 291 |
def test_inlinemath_dollar_with_complex_content(self): |
| 292 |
"""Test MATH_DOLLAR token with complex content.""" |
| 293 |
text = 'The equation $x^2 + y^2 = z^2$ is famous' |
| 294 |
pairs = [(textruns.Token.MATH_DOLLAR, 13, 29)] |
| 295 |
result = textruns._runs_from_token_pairs(text, pairs) |
| 296 |
|
| 297 |
self.assertEqual(result[0], 'The equation ') |
| 298 |
|
| 299 |
self.assertIsInstance(result[1], textruns.InlineMath) |
| 300 |
self.assertEqual(str(result[1][0]), 'x^2 + y^2 = z^2') |
| 301 |
|
| 302 |
self.assertEqual(result[2], ' is famous') |
| 303 |
|
| 304 |
def test_inlinemath_and_markup_tokens(self): |
| 305 |
"""Test mixing math tokens with markup tokens""" |
| 306 |
text = ( |
| 307 |
r'The *important* equation $E=mc^2$ and \(F=ma\) are fundamental' |
| 308 |
) |
| 309 |
pairs = [ |
| 310 |
(textruns.Token.IMPORTANT, 4, 14), |
| 311 |
(textruns.Token.MATH_DOLLAR, 25, 32), |
| 312 |
(textruns.Token.MATH_PARENS, 38, 44), |
| 313 |
] |
| 314 |
result = textruns._runs_from_token_pairs(text, pairs) |
| 315 |
|
| 316 |
self.assertEqual(str(result[0]), 'The ') |
| 317 |
|
| 318 |
# Important markup |
| 319 |
self.assertIsInstance(result[1], textruns.Markup) |
| 320 |
self.assertEqual(result[1][0], 'important') |
| 321 |
self.assertEqual(result[1].markup_type, textruns.Markup.Type.IMPORTANT) |
| 322 |
|
| 323 |
self.assertEqual(result[2], ' equation ') |
| 324 |
|
| 325 |
# Inline (Dollar) math |
| 326 |
self.assertIsInstance(result[3], textruns.InlineMath) |
| 327 |
self.assertEqual(str(result[3][0]), 'E=mc^2') |
| 328 |
|
| 329 |
self.assertEqual(result[4], ' and ') |
| 330 |
|
| 331 |
# Inline (Parens) math |
| 332 |
self.assertIsInstance(result[5], textruns.InlineMath) |
| 333 |
self.assertEqual(str(result[5][0]), 'F=ma') |
| 334 |
|
| 335 |
self.assertEqual(result[6], ' are fundamental') |
| 336 |
|
| 337 |
def test_side_by_side(self): |
| 338 |
text = 'Foo ~code~ and /italic/ bar' |
| 339 |
pairs = [ |
| 340 |
(textruns.Token.CODE, 4, 9), |
| 341 |
(textruns.Token.EMPHASIZED, 15, 22), |
| 342 |
] |
| 343 |
result = textruns._runs_from_token_pairs(text, pairs) |
| 344 |
|
| 345 |
# text runs |
| 346 |
self.assertEqual(str(result[0]), 'Foo ') |
| 347 |
self.assertEqual(str(result[2]), ' and ') |
| 348 |
self.assertEqual(str(result[4]), ' bar') |
| 349 |
|
| 350 |
# markups |
| 351 |
self.assertEqual(str(result[1].runs[0]), 'code') |
| 352 |
self.assertEqual(str(result[3].runs[0]), 'italic') |
| 353 |
self.assertEqual( |
| 354 |
result[1].markup_type, textruns.Markup.Type.CODE) |
| 355 |
self.assertEqual( |
| 356 |
result[3].markup_type, textruns.Markup.Type.EMPHASIZED) |