File: test_enums.py

package info (click to toggle)
pycairo 1.27.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,984 kB
  • sloc: ansic: 8,873; python: 3,688; makefile: 32; sh: 4
file content (138 lines) | stat: -rw-r--r-- 4,137 bytes parent folder | download
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
import pickle
import re
import platform

import pytest
import cairo


# https://foss.heptapod.net/pypy/pypy/-/issues/2742
@pytest.mark.skipif(platform.python_implementation() == "PyPy", reason="PyPy")
def test_type():
    t = cairo.Antialias
    assert int in t.__mro__
    assert isinstance(t(42), int)
    assert isinstance(t(0), int)
    assert issubclass(t, int)

    with pytest.raises(TypeError):
        t()

    with pytest.raises(TypeError):
        t(object())

    with pytest.raises(TypeError):
        type("foo", (t,), {})

    assert hasattr(t, "DEFAULT")
    assert t.DEFAULT == 0
    assert t.__name__ == "Antialias"
    assert t.__module__ == "cairo"

    assert repr(t.DEFAULT) == "cairo.Antialias.DEFAULT"
    assert repr(t(123456)) == "123456"
    assert repr(t(0)) == "cairo.Antialias.DEFAULT"
    assert str(t(0)) == "0"

    assert isinstance(cairo.ANTIALIAS_DEFAULT, t)


def test_misc():
    cairo.Status.JBIG2_GLOBAL_MISSING


def test_format_methods():
    assert cairo.Format.RGB24.stride_for_width(8) == 32
    assert cairo.Format.stride_for_width(cairo.Format.RGB24, 8) == 32


def test_text_cluster_flags():
    assert cairo.TextClusterFlags.BACKWARD == 1


def test_surface_observer_mode():
    assert cairo.SurfaceObserverMode.NORMAL == 0


def test_aliases():
    types_ = [
        cairo.Antialias,
        cairo.Content,
        cairo.Extend,
        cairo.FillRule,
        cairo.Filter,
        cairo.FontWeight,
        cairo.FontSlant,
        cairo.Format,
        cairo.HintMetrics,
        cairo.HintStyle,
        cairo.LineCap,
        cairo.LineJoin,
        cairo.Operator,
        cairo.PathDataType,
        cairo.RegionOverlap,
        cairo.SubpixelOrder,
    ]

    if hasattr(cairo, "ColorMode"):
        # cairo 1.17.8+
        types_.append(cairo.ColorMode)

    if hasattr(cairo, "Dither"):
        # cairo 1.18.0+
        types_.append(cairo.Dither)

    def get_prefix(t):
        name = t.__name__
        # special case..
        if name == "PathDataType":
            name = "Path"
        return "_".join([s.upper() for s in re.findall('[A-Z][^A-Z]*', name)])

    for t in types_:
        for name in dir(t):
            if name.upper() != name:
                continue
            value = getattr(t, name)
            assert isinstance(value, t)
            prefix = get_prefix(t)
            assert getattr(cairo, f"{prefix}_{name}") == value

    for name in dir(cairo):
        for t in types_:
            prefix = get_prefix(t)
            if name.startswith(f"{prefix}_"):
                postfix = name[len(prefix) + 1:]
                value = getattr(cairo, name)
                assert getattr(t, postfix) == value

    # some enums are different, check manually
    assert cairo.SVG_VERSION_1_1 == cairo.SVGVersion.VERSION_1_1
    assert isinstance(cairo.SVG_VERSION_1_1, cairo.SVGVersion)
    assert isinstance(cairo.SVGVersion.VERSION_1_1, cairo.SVGVersion)
    assert cairo.SVG_VERSION_1_2 == cairo.SVGVersion.VERSION_1_2
    assert isinstance(cairo.SVG_VERSION_1_2, cairo.SVGVersion)
    assert isinstance(cairo.SVGVersion.VERSION_1_2, cairo.SVGVersion)

    assert cairo.PDF_VERSION_1_4 == cairo.PDFVersion.VERSION_1_4
    assert isinstance(cairo.PDF_VERSION_1_4, cairo.PDFVersion)
    assert isinstance(cairo.PDFVersion.VERSION_1_4, cairo.PDFVersion)
    assert cairo.PDF_VERSION_1_5 == cairo.PDFVersion.VERSION_1_5
    assert isinstance(cairo.PDF_VERSION_1_5, cairo.PDFVersion)
    assert isinstance(cairo.PDFVersion.VERSION_1_5, cairo.PDFVersion)

    assert cairo.PS_LEVEL_2 == cairo.PSLevel.LEVEL_2
    assert isinstance(cairo.PS_LEVEL_2, cairo.PSLevel)
    assert isinstance(cairo.PSLevel.LEVEL_2, cairo.PSLevel)
    assert cairo.PS_LEVEL_3 == cairo.PSLevel.LEVEL_3
    assert isinstance(cairo.PS_LEVEL_3, cairo.PSLevel)
    assert isinstance(cairo.PSLevel.LEVEL_3, cairo.PSLevel)


def test_pickle():
    # These constants used to be plain int. Try to pickle to int so that
    # there is no dependency on pycairo when unpickling.

    value = cairo.Antialias(42)
    new_value = pickle.loads(pickle.dumps(value))
    assert type(new_value) is int