File: test_structseq.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (101 lines) | stat: -rw-r--r-- 2,523 bytes parent folder | download | duplicates (3)
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
# those tests only work after transation with the new _structseq changes, since
# _structseq is frozen in
import pytest

import sys
from _structseq import structseqtype, structseqfield

class foo(metaclass=structseqtype):
    f1 = structseqfield(0, "a")
    f2 = structseqfield(1, "b")
    f3 = structseqfield(2, "c")

    f5 = structseqfield(4, "nonconsecutive")
    f6 = structseqfield(5, "nonconsecutive2", default=lambda self: -15)

def test_structseqtype():
    t = foo((1, 2, 3))
    assert isinstance(t, tuple)
    assert t.f1 == 1
    assert t.f2 == 2
    assert t.f3 == 3
    assert t.f5 is None

    t = foo((1, 2, 3, 4))
    assert t.f1 == 1
    assert t.f2 == 2
    assert t.f3 == 3
    assert t.f5 == 4
    assert t.f6 == -15
    assert tuple(t) == (1, 2, 3) # only positional

    t = foo((1, 2, 3), dict(f5=4))
    assert t.f1 == 1
    assert t.f2 == 2
    assert t.f3 == 3
    assert t.f5 == 4
    assert t.f6 == -15
    assert tuple(t) == (1, 2, 3) # only positional

    t = foo((1, 2, 3, 4, 5))
    assert t.f1 == 1
    assert t.f2 == 2
    assert t.f3 == 3
    assert t.f5 == 4
    assert t.f6 == 5
    assert tuple(t) == (1, 2, 3)

    t = foo((1, 2, 3), dict(f5=4, f6=12))
    assert t.f1 == 1
    assert t.f2 == 2
    assert t.f3 == 3
    assert t.f5 == 4
    assert t.f6 == 12
    assert tuple(t) == (1, 2, 3)

    t = foo((1, 2, 3, 4), dict(f6=12))
    assert t.f1 == 1
    assert t.f2 == 2
    assert t.f3 == 3
    assert t.f5 == 4
    assert t.f6 == 12
    assert tuple(t) == (1, 2, 3)

    with pytest.raises(TypeError):
        foo((1, ))
    with pytest.raises(TypeError):
        foo((1, ) * 6)
    with pytest.raises(TypeError):
        foo((1, 2, 3, 4), dict(f5=5))


def test_dict_extra_keys_ignored():
    t = foo((1, 2, 3), dict(a=2))
    assert not hasattr(t, 'a') # follow CPython

def test_dict_mapdict():
    import __pypy__
    t = foo((1, 2, 3, 4), dict(f6=12))
    assert __pypy__.strategy(t.__dict__) == 'MapDictStrategy'

def test_dict_structseqfield_immutable():
    import __pypy__
    assert __pypy__.strategy(foo.f5).count("immutable") == 5

def test_default_only_nonpositional():
    with pytest.raises(AssertionError):
        class foo(metaclass=structseqtype):
            f1 = structseqfield(0, "a", default=lambda self: 0)


def test_trace_get():
    l = []
    def trace(frame, event, *args):
        l.append((frame, event, ) + args)
        return trace

    t = foo((1, 2, 3))
    sys.settrace(trace)
    assert t.f1 == 1
    sys.settrace(None)
    assert l == []