-
Notifications
You must be signed in to change notification settings - Fork 45
/
test_commands.py
500 lines (391 loc) · 11.7 KB
/
test_commands.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
import pytest
from pyecore.ecore import *
from pyecore.commands import *
from pyecore.resources import URI, ResourceSet
from pyecore.utils import DynamicEPackage
from pyecore.notification import EObserver, Kind
from os import path
class LastObserver(EObserver):
def __init__(self, notifier=None):
super().__init__(notifier=notifier)
self.last = None
def notifyChanged(self, notification):
self.last = notification
@pytest.fixture(scope='module')
def mm():
Root = EClass('Root')
A = EClass('A')
B = EClass('B')
Root.eStructuralFeatures.append(EReference('a_s', A, upper=-1,
containment=True))
Root.eStructuralFeatures.append(EReference('bs', B, upper=-1,
containment=True))
A.eStructuralFeatures.append(EAttribute('name', EString))
simple_tob = EReference('simple_tob', eType=B, containment=True)
A.eStructuralFeatures.append(simple_tob)
A.eStructuralFeatures.append(EReference('many_tob', eType=B,
containment=True, upper=-1))
B.eStructuralFeatures.append(EReference('toa', A))
B.eStructuralFeatures.append(EReference('inverseA', A,
eOpposite=simple_tob))
package = EPackage(name='package')
package.eClassifiers.extend([Root, A, B])
return DynamicEPackage(package)
def test_orderedset_insert(mm):
a = mm.A()
b = mm.B()
a.many_tob.insert(0, b)
assert b in a.many_tob
assert a.many_tob.index(b) == 0
a.many_tob.insert(0, b)
assert b in a.many_tob
b2 = mm.B()
a.many_tob.insert(0, b2)
assert b2 in a.many_tob
assert a.many_tob.index(b) == 1
assert a.many_tob.index(b2) == 0
def test_orderedset_pop(mm):
a = mm.A()
with pytest.raises(KeyError):
a.many_tob.pop()
with pytest.raises(KeyError):
a.many_tob.pop(0)
# add/remove one element
b = mm.B()
a.many_tob.append(b)
assert b in a.many_tob
a.many_tob.pop()
assert len(a.many_tob) == 0
b2 = mm.B()
a.many_tob.extend([b, b2])
assert b in a.many_tob and b2 in a.many_tob
a.many_tob.pop()
assert b in a.many_tob and b2 not in a.many_tob
assert a.many_tob.index(b) == 0
a.many_tob.append(b2)
a.many_tob.pop(0)
assert b not in a.many_tob and b2 in a.many_tob
assert a.many_tob.index(b2) == 0
a.many_tob.pop(-1)
assert len(a.many_tob) == 0
def test_command_abs():
class A(Command):
def can_execute(self):
super().can_execute
def can_undo(self):
super().can_undo
def execute(self):
super().execute()
def undo(self):
super().undo()
def redo(self):
super().redo()
a = A()
a.can_execute()
a.execute()
a.can_undo()
a.undo()
a.redo()
def test_command_set_name(mm):
a = mm.A()
with pytest.raises(BadValueError):
Set(owner='R', feature='name', value='testValue')
set = Set(owner=a, feature='name', value='testValue')
assert set.__repr__()
assert set.can_execute
set.execute()
assert a.name == 'testValue'
assert set.can_undo
set.undo()
assert a.name is None
set.redo()
assert a.name == 'testValue'
def test_command_set_b(mm):
a = mm.A()
b = mm.B()
set = Set(owner=a, feature='simple_tob', value=b)
assert set.can_execute
set.execute()
assert a.simple_tob is b
assert b.inverseA is a
assert set.can_undo
set.undo()
assert a.simple_tob is None
assert b.inverseA is None
set.redo()
assert a.simple_tob is b
assert b.inverseA is a
def test_command_add_b(mm):
a = mm.A()
b = mm.B()
add = Add(owner=a, feature='many_tob', value=b)
assert add.can_execute
add.execute()
assert b in a.many_tob
assert add.can_undo
add.undo()
assert b not in a.many_tob
add.redo()
assert b in a.many_tob
def test_command_add_b_index(mm):
a = mm.A()
b = mm.B()
a.many_tob.append(b)
b2 = mm.B()
add = Add(owner=a, feature='many_tob', index=0, value=b2)
assert add.can_execute
add.execute()
assert b2 in a.many_tob and b in a.many_tob
assert a.many_tob.index(b2) == 0 and a.many_tob.index(b) == 1
assert add.can_undo
add.undo()
assert b2 not in a.many_tob and b in a.many_tob
assert a.many_tob.index(b) == 0
add.redo()
assert b2 in a.many_tob
assert a.many_tob.index(b2) == 0 and a.many_tob.index(b) == 1
def test_command_remove_b(mm):
a = mm.A()
b = mm.B()
b2 = mm.B()
a.many_tob.extend([b, b2])
remove = Remove(owner=a, feature='many_tob', value=b)
assert remove.can_execute
remove.execute()
assert b2 in a.many_tob and b not in a.many_tob
assert a.many_tob.index(b2) == 0
assert remove.can_undo
remove.undo()
assert b2 in a.many_tob and b in a.many_tob
assert a.many_tob.index(b) == 0 and a.many_tob.index(b2)
remove.redo()
assert b2 in a.many_tob and b not in a.many_tob
assert a.many_tob.index(b2) == 0
def test_command_remove_index(mm):
a = mm.A()
b = mm.B()
b2 = mm.B()
with pytest.raises(ValueError):
Remove(owner=a, feature='many_tob', index=0, value=b)
a.many_tob.extend([b, b2])
remove = Remove(owner=a, feature='many_tob', index=0)
assert remove.can_execute
remove.execute()
assert b2 in a.many_tob and b not in a.many_tob
assert a.many_tob.index(b2) == 0
assert remove.can_undo
remove.undo()
assert b2 in a.many_tob and b in a.many_tob
assert a.many_tob.index(b) == 0 and a.many_tob.index(b2)
remove.redo()
assert b2 in a.many_tob and b not in a.many_tob
assert a.many_tob.index(b2) == 0
def test_command_move_fromindex(mm):
a = mm.A()
b = mm.B()
b2 = mm.B()
a.many_tob.extend([b, b2])
with pytest.raises(ValueError):
Move(owner=a, feature='many_tob', from_index=0, value=b)
assert a.many_tob.index(b) == 0 and a.many_tob.index(b2) == 1
move = Move(owner=a, feature='many_tob', from_index=0, to_index=1)
assert move.can_execute
move.execute()
assert a.many_tob.index(b) == 1 and a.many_tob.index(b2) == 0
assert move.can_undo
move.undo()
assert a.many_tob.index(b) == 0 and a.many_tob.index(b2) == 1
move.redo()
assert a.many_tob.index(b) == 1 and a.many_tob.index(b2) == 0
def test_command_move_b(mm):
a = mm.A()
b = mm.B()
b2 = mm.B()
a.many_tob.extend([b, b2])
assert a.many_tob.index(b) == 0 and a.many_tob.index(b2) == 1
move = Move(owner=a, feature='many_tob', to_index=1, value=b)
assert move.can_execute
move.execute()
assert a.many_tob.index(b) == 1 and a.many_tob.index(b2) == 0
assert move.can_undo
move.undo()
assert a.many_tob.index(b) == 0 and a.many_tob.index(b2) == 1
move.redo()
assert a.many_tob.index(b) == 1 and a.many_tob.index(b2) == 0
def test_command_delete_b(mm):
a = mm.A()
b = mm.B()
b2 = mm.B()
a.many_tob.extend([b, b2])
b.toa = a
delete = Delete(owner=b)
assert delete.can_execute
delete.execute()
assert b not in a.many_tob
assert b.toa is None
assert delete.can_undo
delete.undo()
assert b in a.many_tob
assert b.toa is a
delete.redo()
assert b not in a.many_tob
assert b.toa is None
def test_command_delete_graph(mm):
root = mm.Root()
a = mm.A()
b = mm.B()
b2 = mm.B()
a.many_tob.extend([b, b2])
b.toa = a
root.a_s.append(a)
delete = Delete(owner=a)
assert delete.can_execute
delete.execute()
assert a not in root.a_s
assert b not in a.many_tob
assert b2 not in a.many_tob
assert b.toa is None
assert delete.can_undo
delete.undo()
assert a in root.a_s
assert b in a.many_tob
assert b2 in a.many_tob
assert b.toa is a
delete.redo()
assert a not in root.a_s
assert b not in a.many_tob
assert b2 not in a.many_tob
assert b.toa is None
def test_command_delete_inverse(mm):
root = mm.Root()
a = mm.A()
b = mm.B()
b.toa = a
delete = Delete(owner=a)
assert delete.can_execute
delete.execute()
assert b.toa is None
assert delete.can_undo
delete.undo()
assert b.toa is a
delete.redo()
assert b.toa is None
def test_command_compound_basics(mm):
a = mm.A()
b = mm.B()
b.toa = a
set = Set(owner=a, feature='name', value='testValue')
delete = Delete(owner=a)
compound = Compound(set, delete)
assert len(compound) == 2
assert compound[0] is set
assert compound[1] is delete
assert compound.__repr__()
del compound[0]
assert len(compound) == 1
assert compound[0] is delete
compound.insert(0, set)
for i, c in enumerate(compound):
if i == 0:
assert isinstance(c, Set)
else:
assert isinstance(c, Delete)
del compound[0]
assert len(compound) == 1
compound[0:0] = [set]
assert len(compound) == 2
assert compound[0] is set
def test_command_compound_exec(mm):
a = mm.A()
set = Set(owner=a, feature='name', value='testValue')
compound = Compound(set,
Set(owner=a, feature='name', value='testValue2'))
assert compound.can_execute
compound.execute()
assert a.name == 'testValue2'
assert compound.can_undo
compound.undo()
assert a.name is None
compound.redo()
assert a.name == 'testValue2'
assert compound is compound.unwrap()
del compound[1]
assert set is compound.unwrap()
def test_command_set_name_stack(mm):
root = mm.Root()
a = mm.A()
name_feature = mm.A.findEStructuralFeature('name')
stack = CommandStack()
set = Set(owner=a, feature=name_feature, value='testValue')
assert set.__repr__()
stack.execute(set)
assert a.name == 'testValue'
stack.undo()
assert a.name is None
stack.redo()
assert a.name == 'testValue'
assert set.__repr__()
def test_stack_simple(mm):
stack = CommandStack()
with pytest.raises(IndexError):
stack.undo()
a = mm.A()
with pytest.raises(ValueError):
stack.execute(Set(a, 'names', 'test'))
stack.execute(Set(a, 'name', 'testValue'))
stack.execute(Set(a, 'name', 'testValue2'))
assert a.name == 'testValue2'
stack.undo()
assert a.name == 'testValue'
stack.undo()
assert a.name is None
def test_stack_complex(mm):
stack = CommandStack()
a = mm.A()
b1 = mm.B()
b2 = mm.B()
stack.execute(Set(a, 'name', 'testValue'))
stack.execute(Add(a, 'many_tob', b1))
stack.execute(Add(a, 'many_tob', b2))
stack.execute(Set(b1, 'toa', a))
stack.execute(Delete(b1))
stack.execute(Set(a, 'name', 'final'))
assert a.name == 'final'
assert b2 in a.many_tob and b1 not in a.many_tob
stack.undo()
assert a.name == 'testValue'
stack.undo()
assert b1 in a.many_tob
assert b1.toa is a
stack.undo()
assert b1.toa is None
stack.undo()
stack.undo()
assert len(a.many_tob) == 0
stack.undo()
assert a.name is None
with pytest.raises(IndexError):
stack.undo()
def test_stack_multiple_undo_redo(mm):
stack = CommandStack()
a = mm.A()
b1 = mm.B()
stack.execute(Set(a, 'name', 'testValue'))
stack.execute(Add(a, 'many_tob', b1))
assert a.name == 'testValue'
assert b1 in a.many_tob
stack.undo()
stack.undo()
assert a.name is None
assert b1 not in a.many_tob
stack.redo()
stack.redo()
assert a.name == 'testValue'
assert b1 in a.many_tob
def test_command_resource(mm):
rset = ResourceSet()
resource = rset.create_resource(URI('http://logical'))
a = mm.A()
resource.append(a)
cmd = Set(a, 'name', 'test_value')
assert cmd.resource is resource