-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_undo.py
101 lines (81 loc) · 2.31 KB
/
test_undo.py
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
from src.resource_allocation.ds.undo import Undo
class Test(Undo):
def __init__(self):
super().__init__()
self.i = 0
self.l = []
@Undo.undo_func_decorator
def add(self, value=1):
self.append_undo(lambda origin=self.i: setattr(self, 'i', origin))
self.i += value
@Undo.undo_func_decorator
def nested_decorator(self):
self.add()
def no_decorator(self):
self.append_undo(lambda: print("Shouldn't print this!"))
@Undo.undo_func_decorator
def undo_in_undo(self):
t2 = Test2()
for _ in range(5):
self.append_undo(lambda origin=self.i: setattr(self, 'i', origin))
self.i += 1
t2.increase()
self.append_undo(lambda: t2.undo(), lambda: t2.purge_undo())
return t2
@Undo.undo_func_decorator
def append_remove(self):
i = 1
self.l.append(i)
self.append_undo(lambda: self.l.remove(i))
self.l.remove(i)
self.append_undo(lambda: self.l.append(i))
class Test2(Undo):
def __init__(self):
super().__init__()
self.i = 0
@Undo.undo_func_decorator
def increase(self, value=3):
self.append_undo(lambda origin=self.i: setattr(self, 'i', origin))
self.i += value
def test_decorator():
test = Test()
assert test.i == 0
test.add()
assert test.i == 1
test.undo()
assert test.i == 0
test.add(value=2)
test.add()
assert test.i == 3
test.undo(undo_all=True)
assert test.i == 0
# ==========================
try:
test.nested_decorator()
assert False
except AssertionError:
assert True
test.purge_undo()
test._end_of_func = True
pass
# ==========================
try:
test.no_decorator()
assert False
except AssertionError:
assert True
# ==========================
test2 = test.undo_in_undo()
assert test2.i == 15 and test.i == 5
test.undo()
assert test2.i == 0 and test.i == 0
# ==========================
test2 = test.undo_in_undo()
assert test2.i == 15 and test.i == 5
test.purge_undo()
assert test2.i == 15 and test.i == 5
# ==========================
test.append_remove()
assert test.l == []
test.undo()
assert test.l == []