| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import random |
| 4 |
|
| 5 |
import pytest |
| 6 |
from pytest_lazy_fixtures import lf |
| 7 |
|
| 8 |
from prettytable import HRuleStyle, PrettyTable, TableStyle, VRuleStyle |
| 9 |
from prettytable.prettytable import _str_block_width |
| 10 |
|
| 11 |
|
| 12 |
class TestPositionalJunctions: |
| 13 |
"""Verify different cases for positional-junction characters""" |
| 14 |
|
| 15 |
def test_default(self, city_data: PrettyTable) -> None: |
| 16 |
city_data.set_style(TableStyle.DOUBLE_BORDER) |
| 17 |
|
| 18 |
assert ( |
| 19 |
city_data.get_string().strip() |
| 20 |
== """ |
| 21 |
╔═══════════╦══════╦════════════╦═════════════════╗ |
| 22 |
║ City name ║ Area ║ Population ║ Annual Rainfall ║ |
| 23 |
╠═══════════╬══════╬════════════╬═════════════════╣ |
| 24 |
║ Adelaide ║ 1295 ║ 1158259 ║ 600.5 ║ |
| 25 |
║ Brisbane ║ 5905 ║ 1857594 ║ 1146.4 ║ |
| 26 |
║ Darwin ║ 112 ║ 120900 ║ 1714.7 ║ |
| 27 |
║ Hobart ║ 1357 ║ 205556 ║ 619.5 ║ |
| 28 |
║ Sydney ║ 2058 ║ 4336374 ║ 1214.8 ║ |
| 29 |
║ Melbourne ║ 1566 ║ 3806092 ║ 646.9 ║ |
| 30 |
║ Perth ║ 5386 ║ 1554769 ║ 869.4 ║ |
| 31 |
╚═══════════╩══════╩════════════╩═════════════════╝""".strip() |
| 32 |
) |
| 33 |
|
| 34 |
def test_no_header(self, city_data: PrettyTable) -> None: |
| 35 |
city_data.set_style(TableStyle.DOUBLE_BORDER) |
| 36 |
city_data.header = False |
| 37 |
|
| 38 |
assert ( |
| 39 |
city_data.get_string().strip() |
| 40 |
== """ |
| 41 |
╔═══════════╦══════╦═════════╦════════╗ |
| 42 |
║ Adelaide ║ 1295 ║ 1158259 ║ 600.5 ║ |
| 43 |
║ Brisbane ║ 5905 ║ 1857594 ║ 1146.4 ║ |
| 44 |
║ Darwin ║ 112 ║ 120900 ║ 1714.7 ║ |
| 45 |
║ Hobart ║ 1357 ║ 205556 ║ 619.5 ║ |
| 46 |
║ Sydney ║ 2058 ║ 4336374 ║ 1214.8 ║ |
| 47 |
║ Melbourne ║ 1566 ║ 3806092 ║ 646.9 ║ |
| 48 |
║ Perth ║ 5386 ║ 1554769 ║ 869.4 ║ |
| 49 |
╚═══════════╩══════╩═════════╩════════╝""".strip() |
| 50 |
) |
| 51 |
|
| 52 |
def test_with_title(self, city_data: PrettyTable) -> None: |
| 53 |
city_data.set_style(TableStyle.DOUBLE_BORDER) |
| 54 |
city_data.title = "Title" |
| 55 |
|
| 56 |
assert ( |
| 57 |
city_data.get_string().strip() |
| 58 |
== """ |
| 59 |
╔═════════════════════════════════════════════════╗ |
| 60 |
║ Title ║ |
| 61 |
╠═══════════╦══════╦════════════╦═════════════════╣ |
| 62 |
║ City name ║ Area ║ Population ║ Annual Rainfall ║ |
| 63 |
╠═══════════╬══════╬════════════╬═════════════════╣ |
| 64 |
║ Adelaide ║ 1295 ║ 1158259 ║ 600.5 ║ |
| 65 |
║ Brisbane ║ 5905 ║ 1857594 ║ 1146.4 ║ |
| 66 |
║ Darwin ║ 112 ║ 120900 ║ 1714.7 ║ |
| 67 |
║ Hobart ║ 1357 ║ 205556 ║ 619.5 ║ |
| 68 |
║ Sydney ║ 2058 ║ 4336374 ║ 1214.8 ║ |
| 69 |
║ Melbourne ║ 1566 ║ 3806092 ║ 646.9 ║ |
| 70 |
║ Perth ║ 5386 ║ 1554769 ║ 869.4 ║ |
| 71 |
╚═══════════╩══════╩════════════╩═════════════════╝""".strip() |
| 72 |
) |
| 73 |
|
| 74 |
def test_with_title_no_header(self, city_data: PrettyTable) -> None: |
| 75 |
city_data.set_style(TableStyle.DOUBLE_BORDER) |
| 76 |
city_data.title = "Title" |
| 77 |
city_data.header = False |
| 78 |
assert ( |
| 79 |
city_data.get_string().strip() |
| 80 |
== """ |
| 81 |
╔═════════════════════════════════════╗ |
| 82 |
║ Title ║ |
| 83 |
╠═══════════╦══════╦═════════╦════════╣ |
| 84 |
║ Adelaide ║ 1295 ║ 1158259 ║ 600.5 ║ |
| 85 |
║ Brisbane ║ 5905 ║ 1857594 ║ 1146.4 ║ |
| 86 |
║ Darwin ║ 112 ║ 120900 ║ 1714.7 ║ |
| 87 |
║ Hobart ║ 1357 ║ 205556 ║ 619.5 ║ |
| 88 |
║ Sydney ║ 2058 ║ 4336374 ║ 1214.8 ║ |
| 89 |
║ Melbourne ║ 1566 ║ 3806092 ║ 646.9 ║ |
| 90 |
║ Perth ║ 5386 ║ 1554769 ║ 869.4 ║ |
| 91 |
╚═══════════╩══════╩═════════╩════════╝""".strip() |
| 92 |
) |
| 93 |
|
| 94 |
def test_hrule_all(self, city_data: PrettyTable) -> None: |
| 95 |
city_data.set_style(TableStyle.DOUBLE_BORDER) |
| 96 |
city_data.title = "Title" |
| 97 |
city_data.hrules = HRuleStyle.ALL |
| 98 |
assert ( |
| 99 |
city_data.get_string().strip() |
| 100 |
== """ |
| 101 |
╔═════════════════════════════════════════════════╗ |
| 102 |
║ Title ║ |
| 103 |
╠═══════════╦══════╦════════════╦═════════════════╣ |
| 104 |
║ City name ║ Area ║ Population ║ Annual Rainfall ║ |
| 105 |
╠═══════════╬══════╬════════════╬═════════════════╣ |
| 106 |
║ Adelaide ║ 1295 ║ 1158259 ║ 600.5 ║ |
| 107 |
╠═══════════╬══════╬════════════╬═════════════════╣ |
| 108 |
║ Brisbane ║ 5905 ║ 1857594 ║ 1146.4 ║ |
| 109 |
╠═══════════╬══════╬════════════╬═════════════════╣ |
| 110 |
║ Darwin ║ 112 ║ 120900 ║ 1714.7 ║ |
| 111 |
╠═══════════╬══════╬════════════╬═════════════════╣ |
| 112 |
║ Hobart ║ 1357 ║ 205556 ║ 619.5 ║ |
| 113 |
╠═══════════╬══════╬════════════╬═════════════════╣ |
| 114 |
║ Sydney ║ 2058 ║ 4336374 ║ 1214.8 ║ |
| 115 |
╠═══════════╬══════╬════════════╬═════════════════╣ |
| 116 |
║ Melbourne ║ 1566 ║ 3806092 ║ 646.9 ║ |
| 117 |
╠═══════════╬══════╬════════════╬═════════════════╣ |
| 118 |
║ Perth ║ 5386 ║ 1554769 ║ 869.4 ║ |
| 119 |
╚═══════════╩══════╩════════════╩═════════════════╝""".strip() |
| 120 |
) |
| 121 |
|
| 122 |
def test_vrules_none(self, city_data: PrettyTable) -> None: |
| 123 |
city_data.set_style(TableStyle.DOUBLE_BORDER) |
| 124 |
city_data.vrules = VRuleStyle.NONE |
| 125 |
assert ( |
| 126 |
city_data.get_string().strip() |
| 127 |
== "═══════════════════════════════════════════════════\n" |
| 128 |
" City name Area Population Annual Rainfall \n" |
| 129 |
"═══════════════════════════════════════════════════\n" |
| 130 |
" Adelaide 1295 1158259 600.5 \n" |
| 131 |
" Brisbane 5905 1857594 1146.4 \n" |
| 132 |
" Darwin 112 120900 1714.7 \n" |
| 133 |
" Hobart 1357 205556 619.5 \n" |
| 134 |
" Sydney 2058 4336374 1214.8 \n" |
| 135 |
" Melbourne 1566 3806092 646.9 \n" |
| 136 |
" Perth 5386 1554769 869.4 \n" |
| 137 |
"═══════════════════════════════════════════════════".strip() |
| 138 |
) |
| 139 |
|
| 140 |
def test_vrules_frame_with_title(self, city_data: PrettyTable) -> None: |
| 141 |
city_data.set_style(TableStyle.DOUBLE_BORDER) |
| 142 |
city_data.vrules = VRuleStyle.FRAME |
| 143 |
city_data.title = "Title" |
| 144 |
assert ( |
| 145 |
city_data.get_string().strip() |
| 146 |
== """ |
| 147 |
╔═════════════════════════════════════════════════╗ |
| 148 |
║ Title ║ |
| 149 |
╠═════════════════════════════════════════════════╣ |
| 150 |
║ City name Area Population Annual Rainfall ║ |
| 151 |
╠═════════════════════════════════════════════════╣ |
| 152 |
║ Adelaide 1295 1158259 600.5 ║ |
| 153 |
║ Brisbane 5905 1857594 1146.4 ║ |
| 154 |
║ Darwin 112 120900 1714.7 ║ |
| 155 |
║ Hobart 1357 205556 619.5 ║ |
| 156 |
║ Sydney 2058 4336374 1214.8 ║ |
| 157 |
║ Melbourne 1566 3806092 646.9 ║ |
| 158 |
║ Perth 5386 1554769 869.4 ║ |
| 159 |
╚═════════════════════════════════════════════════╝""".strip() |
| 160 |
) |
| 161 |
|
| 162 |
|
| 163 |
class TestStyle: |
| 164 |
@pytest.mark.parametrize( |
| 165 |
"style, expected", |
| 166 |
[ |
| 167 |
pytest.param( |
| 168 |
TableStyle.DEFAULT, |
| 169 |
""" |
| 170 |
+---+---------+---------+---------+ |
| 171 |
| | Field 1 | Field 2 | Field 3 | |
| 172 |
+---+---------+---------+---------+ |
| 173 |
| 1 | value 1 | value2 | value3 | |
| 174 |
| 4 | value 4 | value5 | value6 | |
| 175 |
| 7 | value 7 | value8 | value9 | |
| 176 |
+---+---------+---------+---------+ |
| 177 |
""", |
| 178 |
id="DEFAULT", |
| 179 |
), |
| 180 |
pytest.param( |
| 181 |
TableStyle.MARKDOWN, # TODO fix |
| 182 |
""" |
| 183 |
| | Field 1 | Field 2 | Field 3 | |
| 184 |
| :-: | :-----: | :-----: | :-----: | |
| 185 |
| 1 | value 1 | value2 | value3 | |
| 186 |
| 4 | value 4 | value5 | value6 | |
| 187 |
| 7 | value 7 | value8 | value9 | |
| 188 |
""", |
| 189 |
id="MARKDOWN", |
| 190 |
), |
| 191 |
pytest.param( |
| 192 |
TableStyle.MSWORD_FRIENDLY, |
| 193 |
""" |
| 194 |
| | Field 1 | Field 2 | Field 3 | |
| 195 |
| 1 | value 1 | value2 | value3 | |
| 196 |
| 4 | value 4 | value5 | value6 | |
| 197 |
| 7 | value 7 | value8 | value9 | |
| 198 |
""", |
| 199 |
id="MSWORD_FRIENDLY", |
| 200 |
), |
| 201 |
pytest.param( |
| 202 |
TableStyle.ORGMODE, |
| 203 |
""" |
| 204 |
|---+---------+---------+---------| |
| 205 |
| | Field 1 | Field 2 | Field 3 | |
| 206 |
|---+---------+---------+---------| |
| 207 |
| 1 | value 1 | value2 | value3 | |
| 208 |
| 4 | value 4 | value5 | value6 | |
| 209 |
| 7 | value 7 | value8 | value9 | |
| 210 |
|---+---------+---------+---------| |
| 211 |
""", |
| 212 |
id="ORGMODE", |
| 213 |
), |
| 214 |
pytest.param( |
| 215 |
TableStyle.PLAIN_COLUMNS, |
| 216 |
""" |
| 217 |
Field 1 Field 2 Field 3 |
| 218 |
1 value 1 value2 value3 |
| 219 |
4 value 4 value5 value6 |
| 220 |
7 value 7 value8 value9 |
| 221 |
""", # noqa: W291 |
| 222 |
id="PLAIN_COLUMNS", |
| 223 |
), |
| 224 |
pytest.param( |
| 225 |
TableStyle.RANDOM, |
| 226 |
""" |
| 227 |
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^' |
| 228 |
% 1 value 1 value2 value3% |
| 229 |
% 4 value 4 value5 value6% |
| 230 |
% 7 value 7 value8 value9% |
| 231 |
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^' |
| 232 |
""", |
| 233 |
id="RANDOM", |
| 234 |
), |
| 235 |
pytest.param( |
| 236 |
TableStyle.DOUBLE_BORDER, |
| 237 |
""" |
| 238 |
╔═══╦═════════╦═════════╦═════════╗ |
| 239 |
║ ║ Field 1 ║ Field 2 ║ Field 3 ║ |
| 240 |
╠═══╬═════════╬═════════╬═════════╣ |
| 241 |
║ 1 ║ value 1 ║ value2 ║ value3 ║ |
| 242 |
║ 4 ║ value 4 ║ value5 ║ value6 ║ |
| 243 |
║ 7 ║ value 7 ║ value8 ║ value9 ║ |
| 244 |
╚═══╩═════════╩═════════╩═════════╝ |
| 245 |
""", |
| 246 |
), |
| 247 |
pytest.param( |
| 248 |
TableStyle.SINGLE_BORDER, |
| 249 |
""" |
| 250 |
┌───┬─────────┬─────────┬─────────┐ |
| 251 |
│ │ Field 1 │ Field 2 │ Field 3 │ |
| 252 |
├───┼─────────┼─────────┼─────────┤ |
| 253 |
│ 1 │ value 1 │ value2 │ value3 │ |
| 254 |
│ 4 │ value 4 │ value5 │ value6 │ |
| 255 |
│ 7 │ value 7 │ value8 │ value9 │ |
| 256 |
└───┴─────────┴─────────┴─────────┘ |
| 257 |
""", |
| 258 |
), |
| 259 |
], |
| 260 |
) |
| 261 |
def test_style( |
| 262 |
self, helper_table: PrettyTable, style: TableStyle, expected: str |
| 263 |
) -> None: |
| 264 |
random.seed(1234) |
| 265 |
helper_table.set_style(style) |
| 266 |
assert helper_table.get_string().strip() == expected.strip() |
| 267 |
|
| 268 |
def test_style_invalid(self, helper_table: PrettyTable) -> None: |
| 269 |
# This is an hrule style, not a table style |
| 270 |
with pytest.raises(ValueError): |
| 271 |
helper_table.set_style(HRuleStyle.ALL) # type: ignore[arg-type] |
| 272 |
|
| 273 |
@pytest.mark.parametrize( |
| 274 |
"original_style,style, expected", |
| 275 |
[ |
| 276 |
pytest.param( |
| 277 |
TableStyle.MARKDOWN, |
| 278 |
TableStyle.DEFAULT, |
| 279 |
""" |
| 280 |
+---+---------+---------+---------+ |
| 281 |
| | Field 1 | Field 2 | Field 3 | |
| 282 |
+---+---------+---------+---------+ |
| 283 |
| 1 | value 1 | value2 | value3 | |
| 284 |
| 4 | value 4 | value5 | value6 | |
| 285 |
| 7 | value 7 | value8 | value9 | |
| 286 |
+---+---------+---------+---------+ |
| 287 |
""", |
| 288 |
id="DEFAULT", |
| 289 |
), |
| 290 |
pytest.param( |
| 291 |
TableStyle.MSWORD_FRIENDLY, |
| 292 |
TableStyle.MARKDOWN, |
| 293 |
""" |
| 294 |
| | Field 1 | Field 2 | Field 3 | |
| 295 |
| :-: | :-----: | :-----: | :-----: | |
| 296 |
| 1 | value 1 | value2 | value3 | |
| 297 |
| 4 | value 4 | value5 | value6 | |
| 298 |
| 7 | value 7 | value8 | value9 | |
| 299 |
""", |
| 300 |
id="MARKDOWN", |
| 301 |
), |
| 302 |
pytest.param( |
| 303 |
TableStyle.MARKDOWN, |
| 304 |
TableStyle.MSWORD_FRIENDLY, |
| 305 |
""" |
| 306 |
| | Field 1 | Field 2 | Field 3 | |
| 307 |
| 1 | value 1 | value2 | value3 | |
| 308 |
| 4 | value 4 | value5 | value6 | |
| 309 |
| 7 | value 7 | value8 | value9 | |
| 310 |
""", |
| 311 |
id="MSWORD_FRIENDLY", |
| 312 |
), |
| 313 |
pytest.param( |
| 314 |
TableStyle.MARKDOWN, |
| 315 |
TableStyle.ORGMODE, |
| 316 |
""" |
| 317 |
|---+---------+---------+---------| |
| 318 |
| | Field 1 | Field 2 | Field 3 | |
| 319 |
|---+---------+---------+---------| |
| 320 |
| 1 | value 1 | value2 | value3 | |
| 321 |
| 4 | value 4 | value5 | value6 | |
| 322 |
| 7 | value 7 | value8 | value9 | |
| 323 |
|---+---------+---------+---------| |
| 324 |
""", |
| 325 |
id="ORGMODE", |
| 326 |
), |
| 327 |
pytest.param( |
| 328 |
TableStyle.MARKDOWN, |
| 329 |
TableStyle.PLAIN_COLUMNS, |
| 330 |
""" |
| 331 |
Field 1 Field 2 Field 3 |
| 332 |
1 value 1 value2 value3 |
| 333 |
4 value 4 value5 value6 |
| 334 |
7 value 7 value8 value9 |
| 335 |
""", # noqa: W291 |
| 336 |
id="PLAIN_COLUMNS", |
| 337 |
), |
| 338 |
pytest.param( |
| 339 |
TableStyle.MARKDOWN, |
| 340 |
TableStyle.RANDOM, |
| 341 |
""" |
| 342 |
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^' |
| 343 |
% 1 value 1 value2 value3% |
| 344 |
% 4 value 4 value5 value6% |
| 345 |
% 7 value 7 value8 value9% |
| 346 |
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^' |
| 347 |
""", |
| 348 |
id="RANDOM", |
| 349 |
), |
| 350 |
pytest.param( |
| 351 |
TableStyle.MARKDOWN, |
| 352 |
TableStyle.DOUBLE_BORDER, |
| 353 |
""" |
| 354 |
╔═══╦═════════╦═════════╦═════════╗ |
| 355 |
║ ║ Field 1 ║ Field 2 ║ Field 3 ║ |
| 356 |
╠═══╬═════════╬═════════╬═════════╣ |
| 357 |
║ 1 ║ value 1 ║ value2 ║ value3 ║ |
| 358 |
║ 4 ║ value 4 ║ value5 ║ value6 ║ |
| 359 |
║ 7 ║ value 7 ║ value8 ║ value9 ║ |
| 360 |
╚═══╩═════════╩═════════╩═════════╝ |
| 361 |
""", |
| 362 |
id="DOUBLE_BORDER", |
| 363 |
), |
| 364 |
pytest.param( |
| 365 |
TableStyle.MARKDOWN, |
| 366 |
TableStyle.SINGLE_BORDER, |
| 367 |
""" |
| 368 |
┌───┬─────────┬─────────┬─────────┐ |
| 369 |
│ │ Field 1 │ Field 2 │ Field 3 │ |
| 370 |
├───┼─────────┼─────────┼─────────┤ |
| 371 |
│ 1 │ value 1 │ value2 │ value3 │ |
| 372 |
│ 4 │ value 4 │ value5 │ value6 │ |
| 373 |
│ 7 │ value 7 │ value8 │ value9 │ |
| 374 |
└───┴─────────┴─────────┴─────────┘ |
| 375 |
""", |
| 376 |
id="SINGLE_BORDER", |
| 377 |
), |
| 378 |
], |
| 379 |
) |
| 380 |
def test_style_reset( |
| 381 |
self, |
| 382 |
helper_table: PrettyTable, |
| 383 |
original_style: TableStyle, |
| 384 |
style: TableStyle, |
| 385 |
expected: str, |
| 386 |
) -> None: |
| 387 |
""" |
| 388 |
Testing to ensure that default styling is reset between changes |
| 389 |
of styles on a PrettyTable |
| 390 |
|
| 391 |
Args: |
| 392 |
style (str): Style to be used (Default, markdown, etc) |
| 393 |
expected (str): The expected format of style as a string representation |
| 394 |
""" |
| 395 |
random.seed(1234) |
| 396 |
helper_table.set_style(original_style) |
| 397 |
helper_table.set_style(style) |
| 398 |
assert helper_table.get_string().strip() == expected.strip() |
| 399 |
|
| 400 |
@pytest.mark.parametrize( |
| 401 |
"style, expected", |
| 402 |
[ |
| 403 |
pytest.param( |
| 404 |
TableStyle.MARKDOWN, |
| 405 |
""" |
| 406 |
| l | c | r | Align left | Align centre | Align right | |
| 407 |
| :-| :-: |-: | :----------| :----------: |-----------: | |
| 408 |
| 1 | 2 | 3 | value 1 | value2 | value3 | |
| 409 |
| 4 | 5 | 6 | value 4 | value5 | value6 | |
| 410 |
| 7 | 8 | 9 | value 7 | value8 | value9 | |
| 411 |
""", |
| 412 |
id="MARKDOWN", |
| 413 |
), |
| 414 |
], |
| 415 |
) |
| 416 |
def test_style_align(self, style: TableStyle, expected: str) -> None: |
| 417 |
table = PrettyTable( |
| 418 |
["l", "c", "r", "Align left", "Align centre", "Align right"] |
| 419 |
) |
| 420 |
v = 1 |
| 421 |
for row in range(3): |
| 422 |
# Some have spaces, some not, to help test padding columns of |
| 423 |
# different widths |
| 424 |
table.add_row( |
| 425 |
[v, v + 1, v + 2, f"value {v}", f"value{v + 1}", f"value{v + 2}"] |
| 426 |
) |
| 427 |
v += 3 |
| 428 |
|
| 429 |
table.set_style(style) |
| 430 |
table.align["l"] = table.align["Align left"] = "l" |
| 431 |
table.align["c"] = table.align["Align centre"] = "c" |
| 432 |
table.align["r"] = table.align["Align right"] = "r" |
| 433 |
assert table.get_string().strip() == expected.strip() |
| 434 |
|
| 435 |
|
| 436 |
@pytest.fixture |
| 437 |
def japanese_pretty_table() -> PrettyTable: |
| 438 |
table = PrettyTable(["Kanji", "Hiragana", "English"]) |
| 439 |
table.add_row(["神戸", "こうべ", "Kobe"]) |
| 440 |
table.add_row(["京都", "きょうと", "Kyoto"]) |
| 441 |
table.add_row(["長崎", "ながさき", "Nagasaki"]) |
| 442 |
table.add_row(["名古屋", "なごや", "Nagoya"]) |
| 443 |
table.add_row(["大阪", "おおさか", "Osaka"]) |
| 444 |
table.add_row(["札幌", "さっぽろ", "Sapporo"]) |
| 445 |
table.add_row(["東京", "とうきょう", "Tokyo"]) |
| 446 |
table.add_row(["横浜", "よこはま", "Yokohama"]) |
| 447 |
return table |
| 448 |
|
| 449 |
|
| 450 |
@pytest.fixture |
| 451 |
def emoji_pretty_table() -> PrettyTable: |
| 452 |
thunder1 = [ |
| 453 |
'\033[38;5;226m _`/""\033[38;5;250m.-. \033[0m', |
| 454 |
"\033[38;5;226m ,\\_\033[38;5;250m( ). \033[0m", |
| 455 |
"\033[38;5;226m /\033[38;5;250m(___(__) \033[0m", |
| 456 |
"\033[38;5;228;5m ⚡\033[38;5;111;25mʻ ʻ\033[38;5;228;5m" |
| 457 |
"⚡\033[38;5;111;25mʻ ʻ \033[0m", |
| 458 |
"\033[38;5;111m ʻ ʻ ʻ ʻ \033[0m", |
| 459 |
] |
| 460 |
thunder2 = [ |
| 461 |
"\033[38;5;240;1m .-. \033[0m", |
| 462 |
"\033[38;5;240;1m ( ). \033[0m", |
| 463 |
"\033[38;5;240;1m (___(__) \033[0m", |
| 464 |
"\033[38;5;21;1m ‚ʻ\033[38;5;228;5m⚡\033[38;5;21;25mʻ‚\033[38;5;228;5m" |
| 465 |
"⚡\033[38;5;21;25m‚ʻ \033[0m", |
| 466 |
"\033[38;5;21;1m ‚ʻ‚ʻ\033[38;5;228;5m⚡\033[38;5;21;25mʻ‚ʻ \033[0m", |
| 467 |
] |
| 468 |
table = PrettyTable(["Thunderbolt", "Lightning"]) |
| 469 |
for i, t1 in enumerate(thunder1): |
| 470 |
table.add_row([t1, thunder2[i]]) |
| 471 |
return table |
| 472 |
|
| 473 |
|
| 474 |
class TestMultiPattern: |
| 475 |
@pytest.mark.parametrize( |
| 476 |
["pt", "expected_output", "test_type"], |
| 477 |
[ |
| 478 |
( |
| 479 |
lf("city_data"), |
| 480 |
""" |
| 481 |
+-----------+------+------------+-----------------+ |
| 482 |
| City name | Area | Population | Annual Rainfall | |
| 483 |
+-----------+------+------------+-----------------+ |
| 484 |
| Adelaide | 1295 | 1158259 | 600.5 | |
| 485 |
| Brisbane | 5905 | 1857594 | 1146.4 | |
| 486 |
| Darwin | 112 | 120900 | 1714.7 | |
| 487 |
| Hobart | 1357 | 205556 | 619.5 | |
| 488 |
| Sydney | 2058 | 4336374 | 1214.8 | |
| 489 |
| Melbourne | 1566 | 3806092 | 646.9 | |
| 490 |
| Perth | 5386 | 1554769 | 869.4 | |
| 491 |
+-----------+------+------------+-----------------+ |
| 492 |
""", |
| 493 |
"English Table", |
| 494 |
), |
| 495 |
( |
| 496 |
lf("japanese_pretty_table"), |
| 497 |
""" |
| 498 |
+--------+------------+----------+ |
| 499 |
| Kanji | Hiragana | English | |
| 500 |
+--------+------------+----------+ |
| 501 |
| 神戸 | こうべ | Kobe | |
| 502 |
| 京都 | きょうと | Kyoto | |
| 503 |
| 長崎 | ながさき | Nagasaki | |
| 504 |
| 名古屋 | なごや | Nagoya | |
| 505 |
| 大阪 | おおさか | Osaka | |
| 506 |
| 札幌 | さっぽろ | Sapporo | |
| 507 |
| 東京 | とうきょう | Tokyo | |
| 508 |
| 横浜 | よこはま | Yokohama | |
| 509 |
+--------+------------+----------+ |
| 510 |
|
| 511 |
""", |
| 512 |
"Japanese table", |
| 513 |
), |
| 514 |
( |
| 515 |
lf("emoji_pretty_table"), |
| 516 |
""" |
| 517 |
+-----------------+-----------------+ |
| 518 |
| Thunderbolt | Lightning | |
| 519 |
+-----------------+-----------------+ |
| 520 |
| \x1b[38;5;226m _`/""\x1b[38;5;250m.-. \x1b[0m | \x1b[38;5;240;1m .-. \x1b[0m | |
| 521 |
| \x1b[38;5;226m ,\\_\x1b[38;5;250m( ). \x1b[0m | \x1b[38;5;240;1m ( ). \x1b[0m | |
| 522 |
| \x1b[38;5;226m /\x1b[38;5;250m(___(__) \x1b[0m | \x1b[38;5;240;1m (___(__) \x1b[0m | |
| 523 |
| \x1b[38;5;228;5m ⚡\x1b[38;5;111;25mʻ ʻ\x1b[38;5;228;5m⚡\x1b[38;5;111;25mʻ ʻ \x1b[0m | \x1b[38;5;21;1m ‚ʻ\x1b[38;5;228;5m⚡\x1b[38;5;21;25mʻ‚\x1b[38;5;228;5m⚡\x1b[38;5;21;25m‚ʻ \x1b[0m | |
| 524 |
| \x1b[38;5;111m ʻ ʻ ʻ ʻ \x1b[0m | \x1b[38;5;21;1m ‚ʻ‚ʻ\x1b[38;5;228;5m⚡\x1b[38;5;21;25mʻ‚ʻ \x1b[0m | |
| 525 |
+-----------------+-----------------+ |
| 526 |
""", # noqa: E501 |
| 527 |
"Emoji table", |
| 528 |
), |
| 529 |
], |
| 530 |
) |
| 531 |
def test_multi_pattern_outputs( |
| 532 |
self, pt: PrettyTable, expected_output: str, test_type: str |
| 533 |
) -> None: |
| 534 |
assert ( |
| 535 |
pt.get_string().strip() == expected_output.strip() |
| 536 |
), f"Error output for test output of type {test_type}" |
| 537 |
|
| 538 |
|
| 539 |
def test_colored_table() -> None: |
| 540 |
table = PrettyTable(field_names=["Namespace", "Count"]) |
| 541 |
table.title = "\x1b[34mHere be Table caption\x1b[39m" |
| 542 |
assert ( |
| 543 |
table.get_string() |
| 544 |
== """+-----------------------+ |
| 545 |
| \x1b[34mHere be Table caption\x1b[39m | |
| 546 |
+-------------+---------+ |
| 547 |
| Namespace | Count | |
| 548 |
+-------------+---------+ |
| 549 |
+-------------+---------+""" |
| 550 |
) |
| 551 |
|
| 552 |
|
| 553 |
def test_link_and_color() -> None: |
| 554 |
table = PrettyTable(["Link", "Count"]) |
| 555 |
# Add link |
| 556 |
text = "Click here" |
| 557 |
table.add_row([f"\033]8;;https://example.com\033\\{text}\033]8;;\033\\", "1"]) |
| 558 |
table.add_row(["No link", "2"]) |
| 559 |
# Add link with colour |
| 560 |
text = "Click \x1b[34mhere\x1b[39m" |
| 561 |
table.add_row([f"\033]8;;https://example.com\033\\{text}\033]8;;\033\\", "3"]) |
| 562 |
|
| 563 |
assert ( |
| 564 |
table.get_string() |
| 565 |
== """\ |
| 566 |
+------------+-------+ |
| 567 |
| Link | Count | |
| 568 |
+------------+-------+ |
| 569 |
| \033]8;;https://example.com\033\\Click here\033]8;;\033\\ | 1 | |
| 570 |
| No link | 2 | |
| 571 |
| \033]8;;https://example.com\033\\Click \x1b[34mhere\x1b[39m\033]8;;\033\\ | 3 | |
| 572 |
+------------+-------+""" |
| 573 |
) |
| 574 |
|
| 575 |
|
| 576 |
@pytest.mark.parametrize( |
| 577 |
["test_input", "expected"], |
| 578 |
[ |
| 579 |
("a", 1), |
| 580 |
("abc", 3), |
| 581 |
("abc def", 7), |
| 582 |
("\x1b[34mblue\x1b[39m", 4), |
| 583 |
("\033]8;;https://example.com\033\\link\033]8;;\033\\", 4), |
| 584 |
# colour inside link |
| 585 |
("\033]8;;https://example.com\033\\\x1b[34mblue link\x1b[39m\033]8;;\033\\", 9), |
| 586 |
# link inside colour |
| 587 |
("\x1b[34m\033]8;;https://example.com\033\\blue link\033]8;;\033\\\x1b[39m", 9), |
| 588 |
], |
| 589 |
) |
| 590 |
def test__str_block_width(test_input: str, expected: int) -> None: |
| 591 |
assert _str_block_width(test_input) == expected |