| 1 | import cairo |
| 2 | import pytest |
| 3 | |
| 4 | |
| 5 | def test_error_check_status(): |
| 6 | check_status = cairo.Error._check_status |
| 7 | |
| 8 | with pytest.raises(cairo.Error) as e: |
| 9 | check_status(cairo.Status.DEVICE_FINISHED) |
| 10 | assert e.value.status == cairo.Status.DEVICE_FINISHED |
| 11 | assert str(e.value) == "the target device has been finished" |
| 12 | assert repr(e.value) == ("Error('the target device has been finished', " |
| 13 | "cairo.Status.DEVICE_FINISHED)") |
| 14 | |
| 15 | with pytest.raises(cairo.Error) as e: |
| 16 | check_status(cairo.Status.NO_MEMORY) |
| 17 | assert e.value.status == cairo.Status.NO_MEMORY |
| 18 | with pytest.raises(MemoryError) as e: |
| 19 | check_status(cairo.Status.NO_MEMORY) |
| 20 | assert type(e.value).__name__ == "cairo.MemoryError" |
| 21 | |
| 22 | with pytest.raises(cairo.Error) as e: |
| 23 | check_status(cairo.Status.READ_ERROR) |
| 24 | with pytest.raises(IOError) as e: |
| 25 | check_status(cairo.Status.READ_ERROR) |
| 26 | assert e.value.status == cairo.Status.READ_ERROR |
| 27 | with pytest.raises(cairo.Error) as e: |
| 28 | check_status(cairo.Status.WRITE_ERROR) |
| 29 | with pytest.raises(IOError) as e: |
| 30 | check_status(cairo.Status.WRITE_ERROR) |
| 31 | assert e.value.status == cairo.Status.WRITE_ERROR |
| 32 | assert type(e.value).__name__ == "cairo.IOError" |
| 33 | |
| 34 | err = e.value |
| 35 | err.status = cairo.Status.DEVICE_FINISHED |
| 36 | assert err.status == cairo.Status.DEVICE_FINISHED |
| 37 | |
| 38 | with pytest.raises((TypeError, AttributeError)): |
| 39 | del err.status |
| 40 | |
| 41 | str(cairo.Error()) |
| 42 | |
| 43 | |
| 44 | def test_error_context(): |
| 45 | surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 100, 100) |
| 46 | ctx = cairo.Context(surface) |
| 47 | with pytest.raises(cairo.Error) as excinfo: |
| 48 | ctx.restore() |
| 49 | error = excinfo.value |
| 50 | assert error.status == cairo.STATUS_INVALID_RESTORE |
| 51 | assert error.status == cairo.Status.INVALID_RESTORE |
| 52 | assert isinstance(error.status, cairo.Status) |
| 53 | assert str(error) |
| 54 | |
| 55 | |
| 56 | def test_error(): |
| 57 | with pytest.raises(cairo.Error) as excinfo: |
| 58 | raise cairo.Error |
| 59 | assert excinfo.value.status is None |
| 60 | |
| 61 | with pytest.raises(cairo.Error) as excinfo: |
| 62 | raise cairo.Error("foo") |
| 63 | assert excinfo.value.status is None |
| 64 | assert excinfo.value.args[0] == "foo" |
| 65 | |
| 66 | with pytest.raises(cairo.Error) as excinfo: |
| 67 | raise cairo.Error("foo", 42) |
| 68 | assert excinfo.value.status == 42 |
| 69 | |
| 70 | class Foo(cairo.Error): |
| 71 | pass |
| 72 | |
| 73 | Foo("foo", 42) |
| 74 | |
| 75 | |
| 76 | def test_error_alias(): |
| 77 | assert cairo.Error is cairo.CairoError |