Skip to content

Commit

Permalink
Fix restoring at root node
Browse files Browse the repository at this point in the history
  • Loading branch information
numirias committed Dec 19, 2017
1 parent eb3977a commit 1e4b6c5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
16 changes: 11 additions & 5 deletions plasma/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,12 +547,13 @@ def restore(self, node):
Try to add the node in a place where a node with the same payload
has previously been.
"""
restorables = self.root.restorables
try:
parent, idx, sizes, flip = self.root.restorables[node.payload]
parent, idx, sizes, fixed, flip = restorables[node.payload]
except KeyError:
raise NotRestorableError()
if parent not in self.root:
# Don't restore at a parent that's not part of the tree anymore
# Don't try to restore if parent is not part of the tree anymore
raise NotRestorableError()
node.reset_size()
if flip:
Expand All @@ -565,18 +566,23 @@ def restore(self, node):
node.size = sizes[0]
if len(sizes) == 2:
node.siblings[0].size = sizes[1]
del self.root.restorables[node.payload]
if not fixed:
node.reset_size()
del restorables[node.payload]

def _save_restore_state(self):
parent = self.parent
sizes = (self._size,)
sizes = (self.size,)
flip = False
if len(self.siblings) == 1:
# If there is only one node left in the container, we need to save
# its size too because the size will be lost.
sizes += (self.siblings[0]._size,) # pylint: disable=W0212
if not self.parent.is_root:
flip = True
parent = self.siblings[0]
self.root.restorables[self.payload] = (parent, self.index, sizes, flip)
self.root.restorables[self.payload] = (parent, self.index, sizes,
self.fixed, flip)

def move(self, direction):
if self.is_root:
Expand Down
46 changes: 31 additions & 15 deletions tests/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,21 +893,6 @@ def test_resize_max(self, root, tiny_grid):
assert a.width == 110
assert b.width == c.width == 10

def test_resize_with_collapse_and_restore(self, root, small_grid):
a, b, c, d = small_grid
root.height = 30
c.size = 30
d.size += 10
b.remove()
assert c.size == c.height == 10
assert d.size == d.height == 20
root.restore(b)
assert b.height == 15
assert b.width == 60
assert c.height == d.height == 15
assert c.width == 20
assert d.width == 40

class TestRestore:

def test_restore(self, root, grid):
Expand Down Expand Up @@ -1001,6 +986,22 @@ def test_restore_root(self, root):
assert a._size == 20
assert b._size is None

def test_restore_root2(self, root):
a, b, c = Nodes('a b c')
root.add_child(a)
root.add_child(b)
root.add_child(c)
b.size = 20
c.size = 40
a.remove()
assert b.size == 40
assert c.size == 80
root.restore(a)
assert not a.fixed
assert a.size == 60
assert b.size == 20
assert c.size == 40

def test_restore_keep_flexible(self, root, tiny_grid):
a, b, c = tiny_grid
b.remove()
Expand All @@ -1025,3 +1026,18 @@ def test_restore_keep_flexible(self, root, tiny_grid):
root.restore(c)
assert b._size is None
assert c._size == 10

def test_resize_with_collapse_and_restore(self, root, small_grid):
a, b, c, d = small_grid
root.height = 30
c.size = 30
d.size += 10
b.remove()
assert c.size == c.height == 10
assert d.size == d.height == 20
root.restore(b)
assert b.height == 15
assert b.width == 60
assert c.height == d.height == 15
assert c.width == 20
assert d.width == 40

0 comments on commit 1e4b6c5

Please sign in to comment.