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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
|
# -*- coding: utf-8 -*-
"""
colorful
~~~~~~~~
Terminal string styling done right, in Python.
:copyright: (c) 2017 by Timo Furrer <tuxtimo@gmail.com>
:license: MIT, see LICENSE for more details.
"""
import os
import sys
import pytest
# do not overwrite module
os.environ['COLORFUL_NO_MODULE_OVERWRITE'] = '1'
import colorful.core as core # noqa
import colorful.terminal as terminal # noqa
@pytest.mark.parametrize('style_string,expected', [
# modifiers
('bold', ('\033[1m', '\033[22m')),
('struckthrough', ('\033[9m', '\033[29m')),
# foreground colors
('black', ('\033[30m', '\033[39m')),
('blue', ('\033[34m', '\033[39m')),
('white', ('\033[37m', '\033[39m')),
# background colors
('on_black', ('\033[40m', '\033[49m')),
('on_blue', ('\033[44m', '\033[49m')),
('on_white', ('\033[47m', '\033[49m')),
# modifiers with foreground colors
('bold_black', ('\033[1m\033[30m', '\033[22m\033[39m')),
('italic_blue', ('\033[3m\033[34m', '\033[23m\033[39m')),
('struckthrough_white', ('\033[9m\033[37m', '\033[29m\033[39m')),
# modifiers with background colors
('bold_on_black', ('\033[1m\033[40m', '\033[22m\033[49m')),
('italic_on_blue', ('\033[3m\033[44m', '\033[23m\033[49m')),
('struckthrough_on_white', ('\033[9m\033[47m', '\033[29m\033[49m')),
# modifiers with foreground and background colors
('bold_green_on_black', ('\033[1m\033[32m\033[40m', '\033[22m\033[39m\033[49m')),
('italic_cyan_on_blue', ('\033[3m\033[36m\033[44m', '\033[23m\033[39m\033[49m')),
('struckthrough_yellow_on_white', ('\033[9m\033[33m\033[47m', '\033[29m\033[39m\033[49m')),
# multiple modifiers
('bold_italic', ('\033[1m\033[3m', '\033[22m\033[23m')),
('underlined_struckthrough', ('\033[4m\033[9m', '\033[24m\033[29m')),
# multiple modifiers with foreground colors
('bold_italic_green', ('\033[1m\033[3m\033[32m', '\033[22m\033[23m\033[39m')),
('underlined_struckthrough_cyan', ('\033[4m\033[9m\033[36m', '\033[24m\033[29m\033[39m'))
])
def test_translate_style_8(style_string, expected):
"""
Test translating style strings with 8 colors
"""
assert core.translate_style(style_string, colormode=terminal.ANSI_8_COLORS,
colorpalette=core.COLOR_PALETTE) == expected
@pytest.mark.parametrize('style_string,expected', [
# modifiers
('bold', ('\033[1m', '\033[22m')),
('struckthrough', ('\033[9m', '\033[29m')),
# foreground colors
('black', ('\033[30m', '\033[39m')),
('blue', ('\033[34m', '\033[39m')),
('white', ('\033[37m', '\033[39m')),
# background colors
('on_black', ('\033[40m', '\033[49m')),
('on_blue', ('\033[44m', '\033[49m')),
('on_white', ('\033[47m', '\033[49m')),
# modifiers with foreground colors
('bold_black', ('\033[1m\033[30m', '\033[22m\033[39m')),
('italic_blue', ('\033[3m\033[34m', '\033[23m\033[39m')),
('struckthrough_white', ('\033[9m\033[37m', '\033[29m\033[39m')),
# modifiers with background colors
('bold_on_black', ('\033[1m\033[40m', '\033[22m\033[49m')),
('italic_on_blue', ('\033[3m\033[44m', '\033[23m\033[49m')),
('struckthrough_on_white', ('\033[9m\033[47m', '\033[29m\033[49m')),
# modifiers with foreground and background colors
('bold_green_on_black', ('\033[1m\033[32m\033[40m', '\033[22m\033[39m\033[49m')),
('italic_cyan_on_blue', ('\033[3m\033[36m\033[44m', '\033[23m\033[39m\033[49m')),
('struckthrough_yellow_on_white', ('\033[9m\033[33m\033[47m', '\033[29m\033[39m\033[49m')),
# multiple modifiers
('bold_italic', ('\033[1m\033[3m', '\033[22m\033[23m')),
('underlined_struckthrough', ('\033[4m\033[9m', '\033[24m\033[29m')),
# multiple modifiers with foreground colors
('bold_italic_green', ('\033[1m\033[3m\033[32m', '\033[22m\033[23m\033[39m')),
('underlined_struckthrough_cyan', ('\033[4m\033[9m\033[36m', '\033[24m\033[29m\033[39m'))
])
def test_translate_style_16(style_string, expected):
"""
Test translating style strings with 16 colors
"""
assert core.translate_style(style_string, colormode=terminal.ANSI_16_COLORS,
colorpalette=core.COLOR_PALETTE) == expected
@pytest.mark.parametrize('style_string,expected', [
# modifiers
('bold', ('\033[1m', '\033[22m')),
('struckthrough', ('\033[9m', '\033[29m')),
# foreground colors
('black', ('\033[38;5;16m', '\033[39m')),
('blue', ('\033[38;5;21m', '\033[39m')),
('white', ('\033[38;5;231m', '\033[39m')),
# background colors
('on_black', ('\033[48;5;16m', '\033[49m')),
('on_blue', ('\033[48;5;21m', '\033[49m')),
('on_white', ('\033[48;5;231m', '\033[49m')),
# modifiers with foreground colors
('bold_black', ('\033[1m\033[38;5;16m', '\033[22m\033[39m')),
('italic_blue', ('\033[3m\033[38;5;21m', '\033[23m\033[39m')),
('struckthrough_white', ('\033[9m\033[38;5;231m', '\033[29m\033[39m')),
# modifiers with background colors
('bold_on_black', ('\033[1m\033[48;5;16m', '\033[22m\033[49m')),
('italic_on_blue', ('\033[3m\033[48;5;21m', '\033[23m\033[49m')),
('struckthrough_on_white', ('\033[9m\033[48;5;231m', '\033[29m\033[49m')),
# modifiers with foreground and background colors
('bold_green_on_black', ('\033[1m\033[38;5;46m\033[48;5;16m', '\033[22m\033[39m\033[49m')),
('italic_cyan_on_blue', ('\033[3m\033[38;5;51m\033[48;5;21m', '\033[23m\033[39m\033[49m')),
('struckthrough_yellow_on_white', ('\033[9m\033[38;5;226m\033[48;5;231m', '\033[29m\033[39m\033[49m')), # noqa
# multiple modifiers
('bold_italic', ('\033[1m\033[3m', '\033[22m\033[23m')),
('underlined_struckthrough', ('\033[4m\033[9m', '\033[24m\033[29m')),
# multiple modifiers with foreground colors
('bold_italic_green', ('\033[1m\033[3m\033[38;5;46m', '\033[22m\033[23m\033[39m')),
('underlined_struckthrough_cyan', ('\033[4m\033[9m\033[38;5;51m', '\033[24m\033[29m\033[39m'))
])
def test_translate_style_256(style_string, expected):
"""
Test translating style strings with 256 colors
"""
assert core.translate_style(style_string, colormode=terminal.ANSI_256_COLORS,
colorpalette=core.COLOR_PALETTE) == expected
@pytest.mark.parametrize('style_string,expected', [
# modifiers
('bold', ('\033[1m', '\033[22m')),
('struckthrough', ('\033[9m', '\033[29m')),
# foreground colors
('black', ('\033[38;2;0;0;0m', '\033[39m')),
('blue', ('\033[38;2;0;0;255m', '\033[39m')),
('white', ('\033[38;2;255;255;255m', '\033[39m')),
# background colors
('on_black', ('\033[48;2;0;0;0m', '\033[49m')),
('on_blue', ('\033[48;2;0;0;255m', '\033[49m')),
('on_white', ('\033[48;2;255;255;255m', '\033[49m')),
# modifiers with foreground colors
('bold_black', ('\033[1m\033[38;2;0;0;0m', '\033[22m\033[39m')),
('italic_blue', ('\033[3m\033[38;2;0;0;255m', '\033[23m\033[39m')),
('struckthrough_white', ('\033[9m\033[38;2;255;255;255m', '\033[29m\033[39m')),
# modifiers with background colors
('bold_on_black', ('\033[1m\033[48;2;0;0;0m', '\033[22m\033[49m')),
('italic_on_blue', ('\033[3m\033[48;2;0;0;255m', '\033[23m\033[49m')),
('struckthrough_on_white', ('\033[9m\033[48;2;255;255;255m', '\033[29m\033[49m')),
# modifiers with foreground and background colors
('bold_green_on_black', ('\033[1m\033[38;2;0;255;0m\033[48;2;0;0;0m', '\033[22m\033[39m\033[49m')), # noqa
('italic_cyan_on_blue', ('\033[3m\033[38;2;0;255;255m\033[48;2;0;0;255m', '\033[23m\033[39m\033[49m')), # noqa
('struckthrough_yellow_on_white', ('\033[9m\033[38;2;255;255;0m\033[48;2;255;255;255m', '\033[29m\033[39m\033[49m')), # noqa
# multiple modifiers
('bold_italic', ('\033[1m\033[3m', '\033[22m\033[23m')),
('underlined_struckthrough', ('\033[4m\033[9m', '\033[24m\033[29m')),
# multiple modifiers with foreground colors
('bold_italic_green', ('\033[1m\033[3m\033[38;2;0;255;0m', '\033[22m\033[23m\033[39m')),
('underlined_struckthrough_cyan', ('\033[4m\033[9m\033[38;2;0;255;255m', '\033[24m\033[29m\033[39m')) # noqa
])
def test_translate_style_true_colors(style_string, expected):
"""
Test translating style strings with true colors
"""
assert core.translate_style(style_string, colormode=terminal.TRUE_COLORS,
colorpalette=core.COLOR_PALETTE) == expected
@pytest.mark.parametrize('method_name,expected', [
('bold', '\033[1mNo, I am your father\033[22m'),
('struckthrough', '\033[9mNo, I am your father\033[29m'),
# foreground colors
('black', '\033[30mNo, I am your father\033[39m'),
('blue', '\033[34mNo, I am your father\033[39m'),
('white', '\033[37mNo, I am your father\033[39m'),
# background colors
('on_black', '\033[40mNo, I am your father\033[49m'),
('on_blue', '\033[44mNo, I am your father\033[49m'),
('on_white', '\033[47mNo, I am your father\033[49m'),
# modifiers with foreground colors
('bold_black', '\033[1m\033[30mNo, I am your father\033[22m\033[39m'),
('italic_blue', '\033[3m\033[34mNo, I am your father\033[23m\033[39m'),
('struckthrough_white', '\033[9m\033[37mNo, I am your father\033[29m\033[39m'),
# modifiers with background colors
('bold_on_black', '\033[1m\033[40mNo, I am your father\033[22m\033[49m'),
('italic_on_blue', '\033[3m\033[44mNo, I am your father\033[23m\033[49m'),
('struckthrough_on_white', '\033[9m\033[47mNo, I am your father\033[29m\033[49m'),
# modifiers with foreground and background colors
('bold_green_on_black', '\033[1m\033[32m\033[40mNo, I am your father\033[22m\033[39m\033[49m'),
('italic_cyan_on_blue', '\033[3m\033[36m\033[44mNo, I am your father\033[23m\033[39m\033[49m'),
('struckthrough_yellow_on_white', '\033[9m\033[33m\033[47mNo, I am your father\033[29m\033[39m\033[49m'), # noqa
# multiple modifiers
('bold_italic', '\033[1m\033[3mNo, I am your father\033[22m\033[23m'),
('underlined_struckthrough', '\033[4m\033[9mNo, I am your father\033[24m\033[29m'),
# multiple modifiers with foreground colors
('bold_italic_green', '\033[1m\033[3m\033[32mNo, I am your father\033[22m\033[23m\033[39m'),
('underlined_struckthrough_cyan', '\033[4m\033[9m\033[36mNo, I am your father\033[24m\033[29m\033[39m') # noqa
])
def test_method_call_to_style_conversion(method_name, expected):
"""
Test converting the method call to an actual style
"""
colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS)
method = getattr(colorful, method_name)
assert str(method('No, I am your father')) == expected
@pytest.mark.parametrize('method_name', [
('bold'),
('struckthrough'),
# foreground colors
('black'),
('blue'),
('white'),
# background colors
('on_black'),
('on_blue'),
('on_white'),
# modifiers with foreground colors
('bold_black'),
('italic_blue'),
('struckthrough_white'),
# modifiers with background colors
('bold_on_black'),
('italic_on_blue'),
('struckthrough_on_white'),
# modifiers with foreground and background colors
('bold_green_on_black'),
('italic_cyan_on_blue'),
('struckthrough_yellow_on_white'),
# multiple modifiers
('bold_italic'),
('underlined_struckthrough'),
# multiple modifiers with foreground colors
('bold_italic_green'),
('underlined_struckthrough_cyan'),
])
def test_method_call_to_style_conversion_disabled_colors(method_name):
"""
Test converting the method call to an actual style with disabled colors
"""
colorful = core.Colorful(colormode=terminal.NO_COLORS)
method = getattr(colorful, method_name)
assert str(method('No, I am your father')) == 'No, I am your father'
@pytest.mark.parametrize('method_name,expected', [
('bold', '\033[1m'),
('struckthrough', '\033[9m'),
# foreground colors
('black', '\033[30m'),
('blue', '\033[34m'),
('white', '\033[37m'),
# background colors
('on_black', '\033[40m'),
('on_blue', '\033[44m'),
('on_white', '\033[47m'),
# modifiers with foreground colors
('bold_black', '\033[1m\033[30m'),
('italic_blue', '\033[3m\033[34m'),
('struckthrough_white', '\033[9m\033[37m'),
# modifiers with background colors
('bold_on_black', '\033[1m\033[40m'),
('italic_on_blue', '\033[3m\033[44m'),
('struckthrough_on_white', '\033[9m\033[47m'),
# modifiers with foreground and background colors
('bold_green_on_black', '\033[1m\033[32m\033[40m'),
('italic_cyan_on_blue', '\033[3m\033[36m\033[44m'),
('struckthrough_yellow_on_white', '\033[9m\033[33m\033[47m'),
# multiple modifiers
('bold_italic', '\033[1m\033[3m'),
('underlined_struckthrough', '\033[4m\033[9m'),
# multiple modifiers with foreground colors
('bold_italic_green', '\033[1m\033[3m\033[32m'),
('underlined_struckthrough_cyan', '\033[4m\033[9m\033[36m')
])
def test_method_str_to_style_conversion(method_name, expected):
"""
Test converting the method to an actual style
"""
colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS)
method = getattr(colorful, method_name)
assert str(method) == expected
@pytest.mark.parametrize('format_str, expected', [
(
'{c.bold}No, I am your father{c.reset}',
'\033[1mNo, I am your father\033[0m'
), (
'{c.struckthrough}No, I am your father{c.reset}',
'\033[9mNo, I am your father\033[0m'
),
# foreground colors
(
'{c.black}No, I am your father{c.reset}',
'\033[30mNo, I am your father\033[0m'
), (
'{c.blue}No, I am your father{c.reset}',
'\033[34mNo, I am your father\033[0m'
), (
'{c.white}No, I am your father{c.reset}',
'\033[37mNo, I am your father\033[0m'
),
# background colors
(
'{c.on_black}No, I am your father{c.reset}',
'\033[40mNo, I am your father\033[0m'
), (
'{c.on_blue}No, I am your father{c.reset}',
'\033[44mNo, I am your father\033[0m'
), (
'{c.on_white}No, I am your father{c.reset}',
'\033[47mNo, I am your father\033[0m'
),
# modifiers with foreground colors
(
'{c.bold_black}No, I am your father{c.reset}',
'\033[1m\033[30mNo, I am your father\033[0m'
), (
'{c.italic_blue}No, I am your father{c.reset}',
'\033[3m\033[34mNo, I am your father\033[0m'
), (
'{c.struckthrough_white}No, I am your father{c.reset}',
'\033[9m\033[37mNo, I am your father\033[0m'
),
# modifiers with background colors
(
'{c.bold_on_black}No, I am your father{c.reset}',
'\033[1m\033[40mNo, I am your father\033[0m'
), (
'{c.italic_on_blue}No, I am your father{c.reset}',
'\033[3m\033[44mNo, I am your father\033[0m'
), (
'{c.struckthrough_on_white}No, I am your father{c.reset}',
'\033[9m\033[47mNo, I am your father\033[0m'
),
# modifiers with foreground and background colors
(
'{c.bold_green_on_black}No, I am your father{c.reset}',
'\033[1m\033[32m\033[40mNo, I am your father\033[0m'
), (
'{c.italic_cyan_on_blue}No, I am your father{c.reset}',
'\033[3m\033[36m\033[44mNo, I am your father\033[0m'
), (
'{c.struckthrough_yellow_on_white}No, I am your father{c.reset}',
'\033[9m\033[33m\033[47mNo, I am your father\033[0m'
),
# multiple modifiers
(
'{c.bold_italic}No, I am your father{c.reset}',
'\033[1m\033[3mNo, I am your father\033[0m'
), (
'{c.underlined_struckthrough}No, I am your father{c.reset}',
'\033[4m\033[9mNo, I am your father\033[0m'
),
# multiple modifiers with foreground colors
(
'{c.bold_italic_green}No, I am your father{c.reset}',
'\033[1m\033[3m\033[32mNo, I am your father\033[0m'
), (
'{c.underlined_struckthrough_cyan}No, I am your father{c.reset}',
'\033[4m\033[9m\033[36mNo, I am your father\033[0m'
),
# color closing delimiters
(
'{c.black}No, I am your father{c.close_fg_color}',
'\033[30mNo, I am your father\033[39m'
), (
'{c.on_black}No, I am your father{c.close_bg_color}',
'\033[40mNo, I am your father\033[49m'
),
# modifier closing delimiters
(
'{c.bold}No, I am your father{c.no_bold}',
'\033[1mNo, I am your father\033[22m'
), (
'{c.struckthrough}No, I am your father{c.no_struckthrough}',
'\033[9mNo, I am your father\033[29m'
)
])
def test_method_in_format_to_style_conversion(format_str, expected):
"""
Test converting the method in a format() call to an actual style
"""
colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS)
assert format_str.format(c=colorful) == expected
def test_invalid_color_mode():
"""
Test setting an invalid color mode
"""
colorful = core.Colorful(colormode=42)
with pytest.raises(core.ColorfulError) as exc:
colorful.white('Invalid color mode')
assert str(exc.value) == 'invalid color mode "42"'
def test_invalid_color_name():
"""
Test invalid color name
"""
colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS)
expected = ('the color "invalidColor" is unknown. '
'Use a color in your color palette (by default: X11 rgb.txt)')
with pytest.raises(core.ColorfulError) as exc:
colorful.bold_invalidColor('Invalid color name')
assert str(exc.value) == expected
@pytest.mark.skipif(not sys.stdout.isatty(), reason='fails without a tty')
@pytest.mark.parametrize('env,expected', [
({'COLORFUL_DISABLE': '1'}, terminal.NO_COLORS),
({'COLORTERM': 'truecolor'}, terminal.TRUE_COLORS),
({'TERM_PROGRAM': 'iTerm.app'}, terminal.TRUE_COLORS),
({'TERM': 'xterm-256color'}, terminal.ANSI_256_COLORS),
({'TERM': 'screen'}, terminal.ANSI_16_COLORS),
({}, terminal.ANSI_8_COLORS),
])
def test_colorful_obj_color_auto_detection(env, expected):
"""
Test that the colorful object is able to auto detect the supported colors
"""
os_env_backup = os.environ.copy()
os.environ = env
colorful = core.Colorful(colormode=None) # None to explicitly auto detect the colors
assert colorful.colormode == expected
# restore environ backup
os.environ = os_env_backup
def test_reading_color_palette(tmpdir):
"""
Test reading color palette from file
"""
# create palette file
palette_file = tmpdir.mkdir('palettes').join('my_rgb.txt')
palette_file.write("""
0 0 0 myBlack
255 255 255 myWhite""")
colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS, colorpalette=str(palette_file))
assert str(colorful.myBlack) == '\033[30m'
assert str(colorful.myWhite) == '\033[37m'
def test_colorful_obj_setup():
"""
Test setup of an existing colorful object
"""
colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS)
colorful.setup(
colormode=terminal.TRUE_COLORS,
colorpalette={'testColor': (0, 0, 0)},
extend_colors=False
)
assert colorful.colormode == terminal.TRUE_COLORS
assert str(colorful.testColor) == '\033[38;2;0;0;0m'
with pytest.raises(core.ColorfulError) as exc:
colorful.black('black does not exist anymore')
expected = ('the color "black" is unknown. '
'Use a color in your color palette (by default: X11 rgb.txt)')
assert str(exc.value) == expected
def test_colorful_obj_setup_with_extending_palette():
"""
Test setup of an existing colorful object and extend the color palette
"""
colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS)
colorful.setup(
colormode=terminal.TRUE_COLORS,
colorpalette={'testColor': (0, 0, 0)},
extend_colors=True
)
assert colorful.colormode == terminal.TRUE_COLORS
assert str(colorful.testColor) == '\033[38;2;0;0;0m'
assert str(colorful.black) == '\033[38;2;0;0;0m'
def test_colorful_obj_setup_with_no_colors():
"""
Test setup switching an existing colorful object to NO_COLORS (disabled)
"""
colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS)
colorful.setup(colormode=terminal.NO_COLORS)
assert colorful.colormode == terminal.NO_COLORS
assert str(colorful.red('color is illusion')) == 'color is illusion'
@pytest.mark.parametrize('method_name, colorname, expected', [
('use_8_ansi_colors', 'black', '\033[30m'),
('use_16_ansi_colors', 'black', '\033[30m'),
('use_256_ansi_colors', 'black', '\033[38;5;16m'),
('use_true_colors', 'black', '\033[38;2;0;0;0m')
])
def test_set_color_mode_methods(method_name, colorname, expected):
"""
Test changing the color mode for an existing colorful object
"""
colorful = core.Colorful(colormode=terminal.NO_COLORS)
# change color mode
getattr(colorful, method_name)()
assert str(getattr(colorful, colorname)) == expected
def test_change_color_palette():
"""
Test changing the color palette for an existing colorful object
"""
NEW_COLOR_PALETTE = {
'black': (0, 0, 0)
}
colorful = core.Colorful(
colormode=terminal.ANSI_8_COLORS, colorpalette={'defaultColor': (255, 255, 255)})
# updating existing color palette
colorful.update_palette(NEW_COLOR_PALETTE)
# old and new colors should exist
assert str(colorful.black) == '\033[30m'
assert str(colorful.defaultColor) == '\033[37m'
# set color palette and overwrite the old one
colorful.use_palette(NEW_COLOR_PALETTE)
assert str(colorful.black) == '\033[30m'
with pytest.raises(core.ColorfulError) as exc:
colorful.defaultColor('The defaultColor was overwritten by the new palette')
expected = ('the color "defaultColor" is unknown. '
'Use a color in your color palette (by default: X11 rgb.txt)')
assert str(exc.value) == expected
def test_use_styles():
"""
Test using a predefined style
"""
colorful = core.Colorful(colormode=terminal.TRUE_COLORS)
colorful.use_style('solarized')
assert str(colorful.red) == '\033[38;2;220;50;47m'
with pytest.raises(core.ColorfulError) as exc:
colorful.lightCoral('The color lightCoral only exists in the X11 rgb.txt palette')
assert str(exc.value).startswith('the color "lightCoral" is unknown.')
def test_use_unknown_style():
"""
Test exception for unknown style
"""
colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS)
with pytest.raises(core.ColorfulError) as exc:
colorful.use_style('unknown')
assert str(exc.value) == 'the style "unknown" is undefined'
def test_colorful_format():
"""
Test the colorful.format method
"""
colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS)
expected = '\033[3m\033[31mNo, I am your father\033[23m\033[39m'
assert colorful.format('{c.italic_red}{0}, I am your {who}{c.no_italic}{c.close_fg_color}',
'No', who='father') == expected
def test_colorful_direct_print(capsys):
"""
Test the colorful.print method
"""
colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS)
expected = u'\033[3m\033[31mNo, I am your father\033[23m\033[39m\n'
colorful.print('{c.italic_red}No, I am your father{c.no_italic}{c.close_fg_color}', flush=True)
out, err = capsys.readouterr()
assert repr(out) == repr(expected)
assert err == ''
@pytest.mark.parametrize('method_name, expected', [
('bold', 'No, I am your father'),
('struckthrough', 'No, I am your father'),
# foreground colors
('black', 'No, I am your father'),
('blue', 'No, I am your father'),
('white', 'No, I am your father'),
# background colors
('on_black', 'No, I am your father'),
('on_blue', 'No, I am your father'),
('on_white', 'No, I am your father'),
# modifiers with foreground colors
('bold_black', 'No, I am your father'),
('italic_blue', 'No, I am your father'),
('struckthrough_white', 'No, I am your father'),
# modifiers with background colors
('bold_on_black', 'No, I am your father'),
('italic_on_blue', 'No, I am your father'),
('struckthrough_on_white', 'No, I am your father'),
# modifiers with foreground and background colors
('bold_green_on_black', 'No, I am your father'),
('italic_cyan_on_blue', 'No, I am your father'),
('struckthrough_yellow_on_white', 'No, I am your father'),
# multiple modifiers
('bold_italic', 'No, I am your father'),
('underlined_struckthrough', 'No, I am your father'),
# multiple modifiers with foreground colors
('bold_italic_green', 'No, I am your father'),
('underlined_struckthrough_cyan', 'No, I am your father')
])
def test_length_of_styled_string(method_name, expected):
"""
Test the length of a styled string
"""
colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS)
method = getattr(colorful, method_name)
assert len(method(expected)) == len(expected)
def test_nested_styled_string():
"""
Test nested styled string
"""
colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS)
s = colorful.red('Hello ' + colorful.blue('awesome', nested=True) + ' world')
assert str(s) == '\033[31mHello \033[34mawesome\033[39m\033[31m world\033[39m'
s = colorful.red('Hello') + ' World'
assert str(s) == '\033[31mHello\033[39m World'
s = 'Hello' + colorful.red(' World')
assert str(s) == 'Hello\033[31m World\033[39m'
s = colorful.red('Hello') + colorful.blue(' World')
assert str(s) == '\033[31mHello\033[39m\033[34m World\033[39m'
s = colorful.red('Hello {0} world'.format(colorful.blue('awesome', nested=True)))
assert str(s) == '\033[31mHello \033[34mawesome\033[39m\033[31m world\033[39m'
def test_nested_styled_string_with_format():
"""
Test nested styled string with format()
"""
colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS)
s = colorful.red('Hello {0} world'.format(colorful.blue('awesome')))
assert str(s) == '\033[31mHello \033[34mawesome\033[39m\033[31m world\033[39m'
def test_styling_object_which_implements_str_proto():
"""
Test styling an object which implements the str protocol
"""
class Dummy(object):
def __str__(self):
return 'I am a dummy object'
colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS)
assert str(colorful.black(Dummy())) == '\033[30mI am a dummy object\033[39m'
def test_str_behavior_for_colorfulstring():
"""
Test that the ColorfulString object behaves like a str
"""
colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS)
s = colorful.black('Hello')
# test adding to str
assert str(s + ' World') == '\033[30mHello\033[39m World'
# test being added to str
assert 'World ' + s == 'World \033[30mHello\033[39m'
# test being augment added to str
string = 'World '
string += s
assert string == 'World \033[30mHello\033[39m'
# test augment add a str
s2 = colorful.red('Hello ')
s2 += 'World'
assert str(s2) == '\033[31mHello \033[39mWorld'
# test ColorfulString to ColorfulString
assert str(s + s) == '\033[30mHello\033[39m\033[30mHello\033[39m'
# test ColorfulString augmented added to other ColorfulString
s2 = colorful.red('World ')
s2 += s
assert str(s2) == '\033[31mWorld \033[39m\033[30mHello\033[39m'
# test multipling ColorfulString
assert str(s * 3) == '\033[30mHello\033[39m\033[30mHello\033[39m\033[30mHello\033[39m'
# other str methods operate on the styled string
assert s.replace('Hello', 'Adieu') == '\033[30mAdieu\033[39m'
def test_joining_colorfulstrings():
"""
Test joining ColorfulStrings
"""
colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS)
parts = [colorful.red('Hello'), colorful.black('World')]
# FIXME(TF): is there any other way??
string = ' '.join(str(part) for part in parts)
assert string == '\033[31mHello\033[39m \033[30mWorld\033[39m'
def test_creating_unstyled_colorfulstring():
"""
Test creating an unstyled ColorfulString
"""
colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS)
s = colorful.str('Hello ')
s += colorful.black('World')
assert s.orig_string == 'Hello World'
assert s.styled_string == str(s) == 'Hello \033[30mWorld\033[39m'
def test_unicode_support():
"""
Test unicode support
"""
colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS)
s = u'🐧🎉🐧'
styled_s = colorful.black(s)
# test basic unicode support
assert str(styled_s) == u'\033[30m🐧🎉🐧\033[39m'
def test_combining_styles():
"""
Test combining styles
"""
colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS)
style = colorful.bold & colorful.red & colorful.on_black
assert str(style) == '\033[1m\033[31m\033[40m'
def test_piping_str_into_style():
"""
Test piping a string into a style
"""
colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS)
string = colorful.bold_red | 'No, I am your father'
assert str(string) == '\033[1m\033[31mNo, I am your father\033[22m\033[39m'
def test_piping_styled_str_into_style():
"""
Test piping a string into a style
"""
colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS)
string = colorful.bold_red | colorful.on_black('No, I am your father')
assert str(string) == '\033[1m\033[31m\033[40mNo, I am your father\033[49m\033[22m\033[39m'
def test_piping_constitency():
"""
Test piping a string into a style does the same as the method call
"""
colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS)
string_piped = colorful.bold & colorful.red | 'No, I am your father'
string_called = (colorful.bold & colorful.red)('No, I am your father')
string_called_single = colorful.bold_red('No, I am your father')
assert str(string_piped) == str(string_called) == str(string_called_single)
def test_colorfulstring_format_protocol():
"""
Test format protocol for colorful string
"""
colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS)
s = colorful.black('father')
string = 'No, I am your {0}'.format(s)
assert string == 'No, I am your \033[30mfather\033[39m\033[26m'
string = colorful.red('No, I am your {0}')
formatted = string.format(s)
assert str(formatted) == '\033[31mNo, I am your \033[30mfather\033[39m\033[26m\033[39m'
def test_colorfulstring_format_protocol_no_placeholder_when_disabled():
"""Test that the ColorfulString format doesn't add a placeholder when colorful is disabled"""
# given
colorful = core.Colorful(colormode=terminal.NO_COLORS)
# when
s = "foo: {}".format(colorful.red("bar"))
# then
assert str(s) == "foo: bar"
def test_underling_type_should_be_stored_as_str():
"""
This test ensures that __str__ is correctly implement
if the input is not a str.
Otherwise Python complains:
TypeError: __str__ returned non-string (type int)
"""
# given
colorful = core.Colorful(colormode=terminal.NO_COLORS)
# when
s = "foo: {}".format(str(colorful.red(1)))
# then
assert str(s) == "foo: 1"
@pytest.mark.parametrize("style_a_name, style_b_name, expected_equal", [
pytest.param("red", "red", True, id="red == red"),
pytest.param("red", "blue", False, id="red != blue"),
pytest.param("bold_red", "bold_red", True, id="bold_red == bold_red"),
pytest.param("bold_red", "bold_blue", False, id="bold_red == bold_blue"),
])
def test_colorfulstyles_support_equals_protocol(style_a_name, style_b_name, expected_equal):
"""Test that the ColorfulStyle objects support the equals protocol"""
# given
colorful = core.Colorful(colormode=terminal.ANSI_8_COLORS)
style_a = getattr(colorful, style_a_name)
style_b = getattr(colorful, style_b_name)
# when
actual_equal = style_a == style_b
actual_hash_equal = hash(style_a) == hash(style_b)
# then
assert actual_equal == expected_equal
assert actual_hash_equal == expected_equal
|