From 75ba404ca181b9ae2300f1d8ac82da7644d9ca14 2008-06-13 21:47:22 From: Barry Wark Date: 2008-06-13 21:47:22 Subject: [PATCH] fixes and tests for history --- diff --git a/IPython/frontend/cocoa/cocoa_frontend.py b/IPython/frontend/cocoa/cocoa_frontend.py index cea94d9..3c0a46e 100644 --- a/IPython/frontend/cocoa/cocoa_frontend.py +++ b/IPython/frontend/cocoa/cocoa_frontend.py @@ -197,7 +197,7 @@ class IPythonCocoaController(NSObject, FrontEndBase): return True elif(selector == 'moveDown:'): - nextBlock = self.get_history_next(self.currentBlock()) + nextBlock = self.get_history_next() if(nextBlock != None): self.replaceCurrentBlockWithString(textView, nextBlock) else: diff --git a/IPython/frontend/frontendbase.py b/IPython/frontend/frontendbase.py index 12e1dc3..195f4ce 100644 --- a/IPython/frontend/frontendbase.py +++ b/IPython/frontend/frontendbase.py @@ -115,10 +115,11 @@ class IFrontEnd(zi.Interface): def get_history_previous(currentBlock): - """Returns the block previous in the history.""" + """Returns the block previous in the history. Saves currentBlock if + the history_cursor is currently at the end of the input history""" pass - def get_history_next(currentBlock): + def get_history_next(): """Returns the next block in the history.""" pass @@ -261,21 +262,21 @@ class FrontEndBase(object): def get_history_previous(self, currentBlock): """ Returns previous history string and decrement history cursor. """ - print self.history command = self.history.get_history_item(self.history_cursor - 1) - print command + if command is not None: - self.history.input_cache[self.history_cursor] = currentBlock + if(self.history_cursor == len(self.history.input_cache)): + self.history.input_cache[self.history_cursor] = currentBlock self.history_cursor -= 1 return command - def get_history_next(self, currentBlock): + def get_history_next(self): """ Returns next history string and increment history cursor. """ - command = self.history.get_history_item(self.history_cursor + 1) + command = self.history.get_history_item(self.history_cursor+1) + if command is not None: - self.history.input_cache[self.history_cursor] = currentBlock self.history_cursor += 1 return command diff --git a/IPython/frontend/tests/test_frontendbase.py b/IPython/frontend/tests/test_frontendbase.py index 0bd1bca..35949c4 100644 --- a/IPython/frontend/tests/test_frontendbase.py +++ b/IPython/frontend/tests/test_frontendbase.py @@ -110,5 +110,30 @@ class TestFrontendBase(unittest.TestCase): def checkRenderError(self, result): assert(self.fb.renderErrorCalled) - # TODO: add tests for history + def test_history_returns_expected_block(self): + """Make sure history browsing doesn't fail""" + + blocks = ["a=1","a=2","a=3"] + for b in blocks: + d = self.fb.execute(b) + + # d is now the deferred for the last executed block + d.addCallback(self.historyTests, blocks) + + + def historyTests(self, result, blocks): + """historyTests""" + + assert(len(blocks) >= 3) + assert(self.fb.get_history_previous("") == blocks[-2]) + assert(self.fb.get_history_previous("") == blocks[-3]) + assert(self.fb.get_history_next() == blocks[-2]) + + + def test_history_returns_none_at_startup(self): + """test_history_returns_none_at_startup""" + + assert(self.fb.get_history_previous("")==None) + assert(self.fb.get_history_next()==None) +