| 1 |
import math |
| 2 |
import os |
| 3 |
import sys |
| 4 |
import tempfile |
| 5 |
import shutil |
| 6 |
import platform |
| 7 |
|
| 8 |
import pytest |
| 9 |
import cairo |
| 10 |
|
| 11 |
pytest.importorskip("hypothesis") |
| 12 |
from hypothesis import given, strategies, assume, settings |
| 13 |
from hypothesis.strategies import floats, integers |
| 14 |
|
| 15 |
from .hypothesis_fspaths import fspaths |
| 16 |
|
| 17 |
|
| 18 |
if "CI" in os.environ: |
| 19 |
|
| 20 |
|
| 21 |
settings.register_profile( |
| 22 |
"ci", |
| 23 |
deadline=settings.default.deadline * 10, |
| 24 |
max_examples=settings.default.max_examples * 5) |
| 25 |
settings.load_profile("ci") |
| 26 |
|
| 27 |
|
| 28 |
@pytest.fixture(scope='module') |
| 29 |
def tempdir_path(): |
| 30 |
dir_ = tempfile.mkdtemp() |
| 31 |
try: |
| 32 |
yield dir_ |
| 33 |
finally: |
| 34 |
shutil.rmtree(dir_) |
| 35 |
|
| 36 |
|
| 37 |
def _to_temp_path(tempdir_path, p): |
| 38 |
basename = os.path.basename(p) |
| 39 |
if isinstance(basename, bytes): |
| 40 |
tempdir_path = os.fsencode(tempdir_path) |
| 41 |
res = os.path.join(tempdir_path, basename) |
| 42 |
if not isinstance(p, (str, bytes)): |
| 43 |
res = type(p)(res) |
| 44 |
return res |
| 45 |
|
| 46 |
|
| 47 |
def cairo_ver(): |
| 48 |
return tuple(map(int, cairo.cairo_version_string().split("."))) |
| 49 |
|
| 50 |
|
| 51 |
@pytest.mark.skipif( |
| 52 |
platform.python_implementation() == "PyPy" and sys.pypy_version_info < (7, 3, 0), |
| 53 |
reason="PyPy bugs") |
| 54 |
@given(path=fspaths()) |
| 55 |
def test_fspaths(tempdir_path, path): |
| 56 |
p = _to_temp_path(tempdir_path, path) |
| 57 |
|
| 58 |
assert not os.listdir(tempdir_path) |
| 59 |
|
| 60 |
if os.path.exists(p): |
| 61 |
return |
| 62 |
|
| 63 |
if cairo_ver() >= (1, 15, 10): |
| 64 |
def path_encode(p): |
| 65 |
return p.encode("utf-8") |
| 66 |
else: |
| 67 |
def path_encode(p): |
| 68 |
new = temp.encode("mbcs") |
| 69 |
if new.decode("mbcs") != p: |
| 70 |
raise ValueError |
| 71 |
return new |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
is_valid = True |
| 76 |
if os.name == "nt": |
| 77 |
temp = os.path.join(p) |
| 78 |
if isinstance(temp, bytes): |
| 79 |
temp = os.fsdecode(temp) |
| 80 |
if isinstance(temp, str): |
| 81 |
try: |
| 82 |
path_encode(temp) |
| 83 |
except ValueError: |
| 84 |
is_valid = False |
| 85 |
|
| 86 |
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10) |
| 87 |
try: |
| 88 |
surface.write_to_png(p) |
| 89 |
except (TypeError, ValueError): |
| 90 |
assert not is_valid |
| 91 |
assert not os.path.exists(p) |
| 92 |
except cairo.Error: |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
assert is_valid |
| 97 |
assert not os.path.exists(p) |
| 98 |
else: |
| 99 |
assert is_valid |
| 100 |
assert os.path.exists(p), os.listdir(tempdir_path) |
| 101 |
os.unlink(p) |
| 102 |
|
| 103 |
|
| 104 |
@given(strategies.floats(), strategies.floats()) |
| 105 |
def test_surface_set_device_scale(x_scale, y_scale): |
| 106 |
assume(not any(math.isnan(v) for v in [x_scale, y_scale])) |
| 107 |
|
| 108 |
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10) |
| 109 |
try: |
| 110 |
surface.set_device_scale(x_scale, y_scale) |
| 111 |
except cairo.Error as e: |
| 112 |
assert e.status == cairo.Status.INVALID_MATRIX |
| 113 |
else: |
| 114 |
assert surface.get_device_scale() == (x_scale, y_scale) |
| 115 |
|
| 116 |
|
| 117 |
@given(strategies.floats(), strategies.floats()) |
| 118 |
def test_surface_set_device_offset(x_offset, y_offset): |
| 119 |
assume(not any(math.isnan(v) for v in [x_offset, y_offset])) |
| 120 |
|
| 121 |
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10) |
| 122 |
surface.set_device_offset(x_offset, y_offset) |
| 123 |
|
| 124 |
|
| 125 |
@given(strategies.floats(), strategies.floats(), strategies.floats(), |
| 126 |
strategies.floats()) |
| 127 |
def test_surface_create_for_rectangle(x, y, w, h): |
| 128 |
assume(not any(math.isnan(v) for v in [x, y, w, h])) |
| 129 |
|
| 130 |
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 100, 100) |
| 131 |
try: |
| 132 |
surface.create_for_rectangle(x, y, w, h) |
| 133 |
except cairo.Error as e: |
| 134 |
assert e.status == cairo.Status.INVALID_SIZE |
| 135 |
|
| 136 |
|
| 137 |
@given(integers(), floats(allow_nan=False), floats(allow_nan=False)) |
| 138 |
def test_glyph(index, x, y): |
| 139 |
try: |
| 140 |
g = cairo.Glyph(index, x, y) |
| 141 |
except OverflowError: |
| 142 |
pass |
| 143 |
else: |
| 144 |
assert g.index == index |
| 145 |
assert g.x == x |
| 146 |
assert g.y == y |
| 147 |
|
| 148 |
|
| 149 |
@given(floats(allow_nan=False), floats(allow_nan=False), |
| 150 |
floats(allow_nan=False), floats(allow_nan=False)) |
| 151 |
def test_rectangle(x, y, width, height): |
| 152 |
r = cairo.Rectangle(x, y, width, height) |
| 153 |
assert r.x == x |
| 154 |
assert r.y == y |
| 155 |
assert r.width == width |
| 156 |
assert r.height == height |
| 157 |
|
| 158 |
|
| 159 |
@given(integers(), integers()) |
| 160 |
def test_text_cluster(num_bytes, num_glyphs): |
| 161 |
try: |
| 162 |
tc = cairo.TextCluster(num_bytes, num_glyphs) |
| 163 |
except OverflowError: |
| 164 |
pass |
| 165 |
else: |
| 166 |
assert tc.num_bytes == num_bytes |
| 167 |
assert tc.num_glyphs == num_glyphs |
| 168 |
|
| 169 |
|
| 170 |
@given(floats(allow_nan=False), floats(allow_nan=False), |
| 171 |
floats(allow_nan=False), floats(allow_nan=False), |
| 172 |
floats(allow_nan=False), floats(allow_nan=False)) |
| 173 |
def test_text_extents(x_bearing, y_bearing, width, height, x_advance, |
| 174 |
y_advance): |
| 175 |
te = cairo.TextExtents(x_bearing, y_bearing, width, height, x_advance, |
| 176 |
y_advance) |
| 177 |
assert te.x_bearing == x_bearing |
| 178 |
assert te.y_bearing == y_bearing |
| 179 |
assert te.width == width |
| 180 |
assert te.height == height |
| 181 |
assert te.x_advance == x_advance |
| 182 |
assert te.y_advance == y_advance |
| 183 |
|
| 184 |
|
| 185 |
@given(integers(), integers(), integers(), integers()) |
| 186 |
def test_rect_int(x, y, width, height): |
| 187 |
try: |
| 188 |
r = cairo.RectangleInt(x, y, width, height) |
| 189 |
except OverflowError: |
| 190 |
pass |
| 191 |
else: |
| 192 |
assert r.x == x |
| 193 |
assert r.y == y |
| 194 |
assert r.width == width |
| 195 |
assert r.height == height |
| 196 |
|
| 197 |
|
| 198 |
@given(integers()) |
| 199 |
def test_enums(value): |
| 200 |
try: |
| 201 |
e = cairo.Antialias(value) |
| 202 |
except OverflowError: |
| 203 |
pass |
| 204 |
else: |
| 205 |
assert e == value |
| 206 |
|
| 207 |
|
| 208 |
@given(integers()) |
| 209 |
def test_context_get_set_operator(value): |
| 210 |
try: |
| 211 |
op = cairo.Operator(value) |
| 212 |
except OverflowError: |
| 213 |
return |
| 214 |
|
| 215 |
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10) |
| 216 |
context = cairo.Context(surface) |
| 217 |
try: |
| 218 |
context.set_operator(op) |
| 219 |
except OverflowError: |
| 220 |
return |
| 221 |
assert context.get_operator() == op |