|
7 | 7 | import weakref |
8 | 8 |
|
9 | 9 | from immutables.map import Map as PyMap |
10 | | - |
11 | | - |
12 | | -class HashKey: |
13 | | - _crasher = None |
14 | | - |
15 | | - def __init__(self, hash, name, *, error_on_eq_to=None): |
16 | | - assert hash != -1 |
17 | | - self.name = name |
18 | | - self.hash = hash |
19 | | - self.error_on_eq_to = error_on_eq_to |
20 | | - |
21 | | - def __repr__(self): |
22 | | - if self._crasher is not None and self._crasher.error_on_repr: |
23 | | - raise ReprError |
24 | | - return '<Key name:{} hash:{}>'.format(self.name, self.hash) |
25 | | - |
26 | | - def __hash__(self): |
27 | | - if self._crasher is not None and self._crasher.error_on_hash: |
28 | | - raise HashingError |
29 | | - |
30 | | - return self.hash |
31 | | - |
32 | | - def __eq__(self, other): |
33 | | - if not isinstance(other, HashKey): |
34 | | - return NotImplemented |
35 | | - |
36 | | - if self._crasher is not None and self._crasher.error_on_eq: |
37 | | - raise EqError |
38 | | - |
39 | | - if self.error_on_eq_to is not None and self.error_on_eq_to is other: |
40 | | - raise ValueError('cannot compare {!r} to {!r}'.format(self, other)) |
41 | | - if other.error_on_eq_to is not None and other.error_on_eq_to is self: |
42 | | - raise ValueError('cannot compare {!r} to {!r}'.format(other, self)) |
43 | | - |
44 | | - return (self.name, self.hash) == (other.name, other.hash) |
45 | | - |
46 | | - |
47 | | -class KeyStr(str): |
48 | | - |
49 | | - def __hash__(self): |
50 | | - if HashKey._crasher is not None and HashKey._crasher.error_on_hash: |
51 | | - raise HashingError |
52 | | - return super().__hash__() |
53 | | - |
54 | | - def __eq__(self, other): |
55 | | - if HashKey._crasher is not None and HashKey._crasher.error_on_eq: |
56 | | - raise EqError |
57 | | - return super().__eq__(other) |
58 | | - |
59 | | - def __repr__(self, other): |
60 | | - if HashKey._crasher is not None and HashKey._crasher.error_on_repr: |
61 | | - raise ReprError |
62 | | - return super().__eq__(other) |
63 | | - |
64 | | - |
65 | | -class HashKeyCrasher: |
66 | | - |
67 | | - def __init__(self, *, error_on_hash=False, error_on_eq=False, |
68 | | - error_on_repr=False): |
69 | | - self.error_on_hash = error_on_hash |
70 | | - self.error_on_eq = error_on_eq |
71 | | - self.error_on_repr = error_on_repr |
72 | | - |
73 | | - def __enter__(self): |
74 | | - if HashKey._crasher is not None: |
75 | | - raise RuntimeError('cannot nest crashers') |
76 | | - HashKey._crasher = self |
77 | | - |
78 | | - def __exit__(self, *exc): |
79 | | - HashKey._crasher = None |
80 | | - |
81 | | - |
82 | | -class HashingError(Exception): |
83 | | - pass |
84 | | - |
85 | | - |
86 | | -class EqError(Exception): |
87 | | - pass |
88 | | - |
89 | | - |
90 | | -class ReprError(Exception): |
91 | | - pass |
| 10 | +from immutables._testutils import * # NoQA |
92 | 11 |
|
93 | 12 |
|
94 | 13 | class BaseMapTest: |
|
0 commit comments