| 1 |
|
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
import sys |
| 6 |
from _structseq import structseqtype, structseqfield |
| 7 |
|
| 8 |
class foo(metaclass=structseqtype): |
| 9 |
f1 = structseqfield(0, "a") |
| 10 |
f2 = structseqfield(1, "b") |
| 11 |
f3 = structseqfield(2, "c") |
| 12 |
|
| 13 |
f5 = structseqfield(4, "nonconsecutive") |
| 14 |
f6 = structseqfield(5, "nonconsecutive2", default=lambda self: -15) |
| 15 |
|
| 16 |
def test_structseqtype(): |
| 17 |
t = foo((1, 2, 3)) |
| 18 |
assert isinstance(t, tuple) |
| 19 |
assert t.f1 == 1 |
| 20 |
assert t.f2 == 2 |
| 21 |
assert t.f3 == 3 |
| 22 |
assert t.f5 is None |
| 23 |
|
| 24 |
t = foo((1, 2, 3, 4)) |
| 25 |
assert t.f1 == 1 |
| 26 |
assert t.f2 == 2 |
| 27 |
assert t.f3 == 3 |
| 28 |
assert t.f5 == 4 |
| 29 |
assert t.f6 == -15 |
| 30 |
assert tuple(t) == (1, 2, 3) |
| 31 |
|
| 32 |
t = foo((1, 2, 3), dict(f5=4)) |
| 33 |
assert t.f1 == 1 |
| 34 |
assert t.f2 == 2 |
| 35 |
assert t.f3 == 3 |
| 36 |
assert t.f5 == 4 |
| 37 |
assert t.f6 == -15 |
| 38 |
assert tuple(t) == (1, 2, 3) |
| 39 |
|
| 40 |
t = foo((1, 2, 3, 4, 5)) |
| 41 |
assert t.f1 == 1 |
| 42 |
assert t.f2 == 2 |
| 43 |
assert t.f3 == 3 |
| 44 |
assert t.f5 == 4 |
| 45 |
assert t.f6 == 5 |
| 46 |
assert tuple(t) == (1, 2, 3) |
| 47 |
|
| 48 |
t = foo((1, 2, 3), dict(f5=4, f6=12)) |
| 49 |
assert t.f1 == 1 |
| 50 |
assert t.f2 == 2 |
| 51 |
assert t.f3 == 3 |
| 52 |
assert t.f5 == 4 |
| 53 |
assert t.f6 == 12 |
| 54 |
assert tuple(t) == (1, 2, 3) |
| 55 |
|
| 56 |
t = foo((1, 2, 3, 4), dict(f6=12)) |
| 57 |
assert t.f1 == 1 |
| 58 |
assert t.f2 == 2 |
| 59 |
assert t.f3 == 3 |
| 60 |
assert t.f5 == 4 |
| 61 |
assert t.f6 == 12 |
| 62 |
assert tuple(t) == (1, 2, 3) |
| 63 |
|
| 64 |
with pytest.raises(TypeError): |
| 65 |
foo((1, )) |
| 66 |
with pytest.raises(TypeError): |
| 67 |
foo((1, ) * 6) |
| 68 |
with pytest.raises(TypeError): |
| 69 |
foo((1, 2, 3, 4), dict(f5=5)) |
| 70 |
|
| 71 |
|
| 72 |
def test_dict_extra_keys_ignored(): |
| 73 |
t = foo((1, 2, 3), dict(a=2)) |
| 74 |
assert not hasattr(t, 'a') |
| 75 |
|
| 76 |
def test_dict_mapdict(): |
| 77 |
import __pypy__ |
| 78 |
t = foo((1, 2, 3, 4), dict(f6=12)) |
| 79 |
assert __pypy__.strategy(t.__dict__) == 'MapDictStrategy' |
| 80 |
|
| 81 |
def test_dict_structseqfield_immutable(): |
| 82 |
import __pypy__ |
| 83 |
assert __pypy__.strategy(foo.f5).count("immutable") == 5 |
| 84 |
|
| 85 |
def test_default_only_nonpositional(): |
| 86 |
with pytest.raises(AssertionError): |
| 87 |
class foo(metaclass=structseqtype): |
| 88 |
f1 = structseqfield(0, "a", default=lambda self: 0) |
| 89 |
|
| 90 |
|
| 91 |
def test_trace_get(): |
| 92 |
l = [] |
| 93 |
def trace(frame, event, *args): |
| 94 |
l.append((frame, event, ) + args) |
| 95 |
return trace |
| 96 |
|
| 97 |
t = foo((1, 2, 3)) |
| 98 |
sys.settrace(trace) |
| 99 |
assert t.f1 == 1 |
| 100 |
sys.settrace(None) |
| 101 |
assert l == [] |