about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lepus.uberspace.de>2016-02-28 18:16:52 +0100
committerhut <hut@lepus.uberspace.de>2016-02-28 18:16:58 +0100
commit55f9030f784a823a6bce7f498fb6a18e2bd9672b (patch)
tree2edbb71ec5ff4dfddd1b3a6ce3c6b1033abb8f2b
parentc216901aef4380d9f0d06d4c5545c2983fc244ef (diff)
downloadranger-55f9030f784a823a6bce7f498fb6a18e2bd9672b.tar.gz
container.history: fixed rebase() unit test
-rw-r--r--ranger/container/history.py25
-rw-r--r--tests/ranger/container/test_container.py7
2 files changed, 26 insertions, 6 deletions
diff --git a/ranger/container/history.py b/ranger/container/history.py
index db2ea8ab..69c3da57 100644
--- a/ranger/container/history.py
+++ b/ranger/container/history.py
@@ -53,12 +53,31 @@ class History(object):
             self.add(item)
 
     def rebase(self, other_history):
+        """
+        Replace the past of this history by that of another.
+
+        This is used when creating a new tab to seamlessly blend in the history
+        of the old tab into the new one.
+
+        Example: if self is [a,b,C], the current item is uppercase, and
+        other_history is [x,Y,z], then self.merge(other_history) will result in
+        [x, y, C].
+        """
         assert isinstance(other_history, History)
-        index_offset = len(self._history) - self._index
-        self._history[:self._index + 1] = list(other_history._history)
+
+        if len(self._history) == 0:
+            self._index = 0
+            future_length = 0
+        else:
+            future_length = len(self._history) - self._index - 1
+
+        self._history[:self._index] = list(
+                other_history._history[:other_history._index + 1])
         if len(self._history) > self.maxlen:
             self._history = self._history[-self.maxlen:]
-        self._index = len(self._history) - index_offset
+
+        self._index = len(self._history) - future_length - 1
+        assert self._index < len(self._history)
 
     def __len__(self):
         return len(self._history)
diff --git a/tests/ranger/container/test_container.py b/tests/ranger/container/test_container.py
index fafb30c5..a24c3f8d 100644
--- a/tests/ranger/container/test_container.py
+++ b/tests/ranger/container/test_container.py
@@ -74,14 +74,15 @@ def testhistorybasic():
     otherh = history.History(maxlen=h)
     assert(list(h) == list(otherh))
 
-    # Rebase allow to merge entries with another history
+    # Rebase replaces the past of the history with that of another
     otherh = history.History(maxlen=h)
+    old_current_item = h.current()
     for entry in OTHER_TEST_ENTRIES:
         otherh.add(entry)
     assert list(otherh)[-3:] == ["42", "43", "44"]
     h.rebase(otherh)
-    assert h.current() == "44"
-    assert list(h)[-3:] == ['42', '43', '44']
+    assert h.current() == old_current_item
+    assert list(h)[-3:] == ['43', '44', old_current_item]
 
     # modify, modifies the top of the stack
     h.modify("23")