forked from urnest/urnest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpt.py.test
executable file
·139 lines (120 loc) · 3.74 KB
/
Opt.py.test
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python3
# Copyright (c) 2023 Trevor Taylor
# coding: utf-8
#
# Permission to use, copy, modify, and/or distribute this software for
# any purpose with or without fee is hereby granted, provided that all
# copyright notices and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
import sys
from sys import path
from os.path import dirname
if path[0]==dirname(__file__): path.pop(0)
from xju.cmc import Opt
import contextlib
from typing import Self
from xju.assert_ import Assert
from xju.xn import in_context
_log:list[str]=[]
def log(s: str) -> int:
_log.append(s)
print(s,file=sys.stderr)
return len(_log)
def extract_log() -> list[str]:
result=_log[:]
del _log[:]
return result
class Resource:
name: str
entered_at: int|None = None
exited_at: int|None = None
ee: None|Exception
xe: None|Exception
def __init__(self, name:str, ee:None|Exception, xe:None|Exception):
self.name = name
self.ee = ee
self.xe = xe
pass
def __repr__(self):
return f"Resource({self.name!r})"
def __enter__(self) -> Self:
Assert(self.entered_at)==None
self.entered_at=log(f'+ enter {self.name}')
if self.ee:
log(f'E enter {self.name}')
raise self.ee
log(f'- enter {self.name}')
return self
def __exit__(self, t, e, b) -> None:
try:
Assert(self.entered_at).isNot(None)
Assert(self.exited_at).is_(None)
self.exited_at=log(f'+ exit {self.name}')
if self.xe:
log(f'E exit {self.name}')
raise self.xe
log(f'- exit {self.name}')
except Exception:
raise in_context(f'exit {self.name}') from None
pass
pass
def main():
x = Opt(Resource('c',None,None))
x.set(Resource('b',None,None))
v = x.get()
assert v is not None
Assert(v.name)=='b'
x.clear()
Assert(x.get()).is_(None)
Assert(extract_log())==[]
x = Opt(Opt(Resource('c',None,None)))
x.set(Resource('b',None,None))
assert v is not None
Assert(v.name)=='b'
Assert(repr(x))=="Opt(Resource('b'))"
x.clear()
Assert(repr(x))=="Opt(None)"
Assert(x.get()).is_(None)
Assert(extract_log())==[]
x=Opt()
Assert(extract_log())==[]
with x as v:
assert v is None
x.set(Resource('c',None,None))
x.set(Resource('b',None,None))
Assert(extract_log())==[
'+ enter c', '- enter c',
'+ enter b', '- enter b',
'+ exit c', '- exit c'
]
Assert(extract_log())==['+ exit b', '- exit b']
x=Opt(Resource('c',None,None))
with x as v:
assert v is not None
Assert(v.name)=='c'
Assert(extract_log())==[
'+ enter c', '- enter c'
]
x.set(Resource('b',None,None))
Assert(extract_log())==[
'+ enter b', '- enter b', '+ exit c', '- exit c'
]
Assert(extract_log())==['+ exit b', '- exit b']
x=Opt()
with x:
x.set(Opt(Resource('c',None,None)))
x.set(Opt())
Assert(x.get()).is_(None)
Assert(extract_log())==['+ enter c', '- enter c', '+ exit c', '- exit c']
pass
Assert(extract_log())==[]
pass
main()