Debian Codegraph @

tests/test_enums.py

raw symbols SCIP
1 import pickle
2 import re
3 import platform
4
5 import pytest
6 import cairo
7
8
9 # https://foss.heptapod.net/pypy/pypy/-/issues/2742
10 @pytest.mark.skipif(platform.python_implementation() == "PyPy", reason="PyPy")
11 def test_type():
12 t = cairo.Antialias
13 assert int in t.__mro__
14 assert isinstance(t(42), int)
15 assert isinstance(t(0), int)
16 assert issubclass(t, int)
17
18 with pytest.raises(TypeError):
19 t()
20
21 with pytest.raises(TypeError):
22 t(object())
23
24 with pytest.raises(TypeError):
25 type("foo", (t,), {})
26
27 assert hasattr(t, "DEFAULT")
28 assert t.DEFAULT == 0
29 assert t.__name__ == "Antialias"
30 assert t.__module__ == "cairo"
31
32 assert repr(t.DEFAULT) == "cairo.Antialias.DEFAULT"
33 assert repr(t(123456)) == "123456"
34 assert repr(t(0)) == "cairo.Antialias.DEFAULT"
35 assert str(t(0)) == "0"
36
37 assert isinstance(cairo.ANTIALIAS_DEFAULT, t)
38
39
40 def test_misc():
41 cairo.Status.JBIG2_GLOBAL_MISSING
42
43
44 def test_format_methods():
45 assert cairo.Format.RGB24.stride_for_width(8) == 32
46 assert cairo.Format.stride_for_width(cairo.Format.RGB24, 8) == 32
47
48
49 def test_text_cluster_flags():
50 assert cairo.TextClusterFlags.BACKWARD == 1
51
52
53 def test_surface_observer_mode():
54 assert cairo.SurfaceObserverMode.NORMAL == 0
55
56
57 def test_aliases():
58 types_ = [
59 cairo.Antialias,
60 cairo.Content,
61 cairo.Extend,
62 cairo.FillRule,
63 cairo.Filter,
64 cairo.FontWeight,
65 cairo.FontSlant,
66 cairo.Format,
67 cairo.HintMetrics,
68 cairo.HintStyle,
69 cairo.LineCap,
70 cairo.LineJoin,
71 cairo.Operator,
72 cairo.PathDataType,
73 cairo.RegionOverlap,
74 cairo.SubpixelOrder,
75 ]
76
77 if hasattr(cairo, "ColorMode"):
78 # cairo 1.17.8+
79 types_.append(cairo.ColorMode)
80
81 if hasattr(cairo, "Dither"):
82 # cairo 1.18.0+
83 types_.append(cairo.Dither)
84
85 def get_prefix(t):
86 name = t.__name__
87 # special case..
88 if name == "PathDataType":
89 name = "Path"
90 return "_".join([s.upper() for s in re.findall('[A-Z][^A-Z]*', name)])
91
92 for t in types_:
93 for name in dir(t):
94 if name.upper() != name:
95 continue
96 value = getattr(t, name)
97 assert isinstance(value, t)
98 prefix = get_prefix(t)
99 assert getattr(cairo, f"{prefix}_{name}") == value
100
101 for name in dir(cairo):
102 for t in types_:
103 prefix = get_prefix(t)
104 if name.startswith(f"{prefix}_"):
105 postfix = name[len(prefix) + 1:]
106 value = getattr(cairo, name)
107 assert getattr(t, postfix) == value
108
109 # some enums are different, check manually
110 assert cairo.SVG_VERSION_1_1 == cairo.SVGVersion.VERSION_1_1
111 assert isinstance(cairo.SVG_VERSION_1_1, cairo.SVGVersion)
112 assert isinstance(cairo.SVGVersion.VERSION_1_1, cairo.SVGVersion)
113 assert cairo.SVG_VERSION_1_2 == cairo.SVGVersion.VERSION_1_2
114 assert isinstance(cairo.SVG_VERSION_1_2, cairo.SVGVersion)
115 assert isinstance(cairo.SVGVersion.VERSION_1_2, cairo.SVGVersion)
116
117 assert cairo.PDF_VERSION_1_4 == cairo.PDFVersion.VERSION_1_4
118 assert isinstance(cairo.PDF_VERSION_1_4, cairo.PDFVersion)
119 assert isinstance(cairo.PDFVersion.VERSION_1_4, cairo.PDFVersion)
120 assert cairo.PDF_VERSION_1_5 == cairo.PDFVersion.VERSION_1_5
121 assert isinstance(cairo.PDF_VERSION_1_5, cairo.PDFVersion)
122 assert isinstance(cairo.PDFVersion.VERSION_1_5, cairo.PDFVersion)
123
124 assert cairo.PS_LEVEL_2 == cairo.PSLevel.LEVEL_2
125 assert isinstance(cairo.PS_LEVEL_2, cairo.PSLevel)
126 assert isinstance(cairo.PSLevel.LEVEL_2, cairo.PSLevel)
127 assert cairo.PS_LEVEL_3 == cairo.PSLevel.LEVEL_3
128 assert isinstance(cairo.PS_LEVEL_3, cairo.PSLevel)
129 assert isinstance(cairo.PSLevel.LEVEL_3, cairo.PSLevel)
130
131
132 def test_pickle():
133 # These constants used to be plain int. Try to pickle to int so that
134 # there is no dependency on pycairo when unpickling.
135
136 value = cairo.Antialias(42)
137 new_value = pickle.loads(pickle.dumps(value))
138 assert type(new_value) is int