File: test_api.py

package info (click to toggle)
pastel 0.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 296 kB
  • sloc: python: 532; sh: 7; makefile: 3
file content (61 lines) | stat: -rw-r--r-- 1,319 bytes parent folder | download | duplicates (2)
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
# -*- coding: utf-8 -*-

import sys
import pastel

from contextlib import contextmanager


class PseudoTTY(object):
    def __init__(self, underlying):
        self._underlying = underlying

    def __getattr__(self, name):
        return getattr(self._underlying, name)

    def isatty(self):
        return True


@contextmanager
def mock_stdout():
    original = sys.stdout
    sys.stdout = PseudoTTY(sys.stdout)

    yield

    sys.stdout = original


def test_text():
    with mock_stdout():
        assert "\033[32msome info\033[0m" == pastel.colorize("<info>some info</info>")


def test_colorize():
    with mock_stdout():
        pastel.with_colors(False)
        assert "some info" == pastel.colorize("<info>some info</info>")

        pastel.with_colors(True)
        assert "\033[32msome info\033[0m" == pastel.colorize("<info>some info</info>")


def test_add_remove_style():
    with mock_stdout():
        pastel.add_style("success", "green")

        assert "\033[32msome info\033[0m" == pastel.colorize(
            "<success>some info</success>"
        )

        pastel.remove_style("success")

        assert "<success>some info</success>" == pastel.colorize(
            "<success>some info</success>"
        )


def test_pastel():
    p = pastel.pastel()
    assert isinstance(p, pastel.Pastel)