From 4cc7d71026bdc7a5034f90fa7d0701861564ffdd Mon Sep 17 00:00:00 2001 From: jstenar <> Date: Tue, 30 Oct 2007 20:02:10 +0000 Subject: [PATCH] pyreadline: Adding test suite for history buffer --- doc/ChangeLog | 4 ++ pyreadline/lineeditor/history.py | 17 +++-- pyreadline/test/history_test.py | 104 +++++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+), 5 deletions(-) create mode 100644 pyreadline/test/history_test.py diff --git a/doc/ChangeLog b/doc/ChangeLog index b006e4e..525588a 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,7 @@ +2007-10-30 Jörgen Stenarson + * Add tests for history buffer. Add ignore_leading_spaces option for + history searches for forward_search_history, and reverse_search_history. + 2007-10-29 Jörgen Stenarson * Patch as provided by pan. Changed ensure_text to decode to consoles codepage instead of utf8, to ensure proper printing diff --git a/pyreadline/lineeditor/history.py b/pyreadline/lineeditor/history.py index 62882ff..584dfad 100644 --- a/pyreadline/lineeditor/history.py +++ b/pyreadline/lineeditor/history.py @@ -23,6 +23,8 @@ class EscapeHistory(exceptions.Exception): from pyreadline.logger import log_sock +_ignore_leading_spaces=False + class LineHistory(object): def __init__(self): self.history=[] @@ -121,7 +123,11 @@ class LineHistory(object): def reverse_search_history(self,searchfor,startpos=None): if startpos is None: startpos=self.history_cursor - res=[(idx,line) for idx,line in enumerate(self.history[startpos:0:-1]) if line.startswith(searchfor)] + if _ignore_leading_spaces: + res=[(idx,line.lstrip()) for idx,line in enumerate(self.history[startpos:0:-1]) if line.lstrip().startswith(searchfor.lstrip())] + logger.log_sock(res) + else: + res=[(idx,line) for idx,line in enumerate(self.history[startpos:0:-1]) if line.startswith(searchfor)] if res: self.history_cursor-=res[0][0] return res[0][1].get_line_text() @@ -130,7 +136,10 @@ class LineHistory(object): def forward_search_history(self,searchfor,startpos=None): if startpos is None: startpos=self.history_cursor - res=[(idx,line) for idx,line in enumerate(self.history[startpos:]) if line.startswith(searchfor)] + if _ignore_leading_spaces: + res=[(idx,line.lstrip()) for idx,line in enumerate(self.history[startpos:]) if line.lstrip().startswith(searchfor.lstrip())] + else: + res=[(idx,line) for idx,line in enumerate(self.history[startpos:]) if line.startswith(searchfor)] if res: self.history_cursor+=res[0][0] return res[0][1].get_line_text() @@ -197,7 +206,7 @@ class LineHistory(object): self.history_cursor = hc result=lineobj.ReadLineTextBuffer(h,point=len(h.get_line_text())) return result - elif h.get_line_text().startswith(self.query) and h != partial.get_line_text(): + elif (h.get_line_text().startswith(self.query) and (h != partial.get_line_text())): self.history_cursor = hc result=lineobj.ReadLineTextBuffer(h,point=partial.point) return result @@ -232,8 +241,6 @@ class LineHistory(object): q= self._search(-1,partial) return q - - if __name__=="__main__": q=LineHistory() RL=lineobj.ReadLineTextBuffer diff --git a/pyreadline/test/history_test.py b/pyreadline/test/history_test.py new file mode 100644 index 0000000..cf71c74 --- /dev/null +++ b/pyreadline/test/history_test.py @@ -0,0 +1,104 @@ +# -*- coding: UTF-8 -*- +# Copyright (C) 2007 Jörgen Stenarson. <> + +import sys, unittest +sys.path.append ('../..') +#from pyreadline.modes.vi import * +#from pyreadline import keysyms +from pyreadline.lineeditor import lineobj +from pyreadline.lineeditor.history import LineHistory +import pyreadline.lineeditor.history as history + +import pyreadline.logger +pyreadline.logger.sock_silent=False +from pyreadline.logger import log_sock +#---------------------------------------------------------------------- + + +#---------------------------------------------------------------------- +RL=lineobj.ReadLineTextBuffer + +class Test_linepos (unittest.TestCase): + t="test text" + + def init_test(self): + history._ignore_leading_spaces=False + self.q=q=LineHistory() + for x in ["aaaa","aaba","aaca","akca","bbb","ako"]: + q.add_history(RL(x)) + + def test_previous_history (self): + self.init_test() + hist=self.q + assert hist.history_cursor==6 + l=RL("") + hist.previous_history(l) + assert l.get_line_text()=="ako" + hist.previous_history(l) + assert l.get_line_text()=="bbb" + hist.previous_history(l) + assert l.get_line_text()=="akca" + hist.previous_history(l) + assert l.get_line_text()=="aaca" + hist.previous_history(l) + assert l.get_line_text()=="aaba" + hist.previous_history(l) + assert l.get_line_text()=="aaaa" + hist.previous_history(l) + assert l.get_line_text()=="aaaa" + + def test_next_history (self): + self.init_test() + hist=self.q + hist.beginning_of_history() + assert hist.history_cursor==0 + l=RL("") + hist.next_history(l) + assert l.get_line_text()=="aaba" + hist.next_history(l) + assert l.get_line_text()=="aaca" + hist.next_history(l) + assert l.get_line_text()=="akca" + hist.next_history(l) + assert l.get_line_text()=="bbb" + hist.next_history(l) + assert l.get_line_text()=="ako" + hist.next_history(l) + assert l.get_line_text()=="ako" + + def init_test2(self): + self.q=q=LineHistory() + for x in ["aaaa","aaba","aaca","akca","bbb","ako"]: + q.add_history(RL(x)) + + def test_history_search_backward (self): + history._ignore_leading_spaces=False + q=LineHistory() + for x in ["aaaa","aaba","aaca"," aacax","akca","bbb","ako"]: + q.add_history(RL(x)) + a=RL("aa",point=2) + for x in ["aaca","aaba","aaaa","aaaa"]: + res=q.history_search_backward(a) + assert res.get_line_text()==x + + def test_history_search_forward (self): + history._ignore_leading_spaces=False + q=LineHistory() + for x in ["aaaa","aaba","aaca"," aacax","akca","bbb","ako"]: + q.add_history(RL(x)) + q.beginning_of_history() + a=RL("aa",point=2) + for x in ["aaba","aaca","aaca"]: + res=q.history_search_forward(a) + assert res.get_line_text()==x + + +#---------------------------------------------------------------------- +# utility functions + +#---------------------------------------------------------------------- + +if __name__ == '__main__': + unittest.main() + + l=lineobj.ReadLineTextBuffer("First Second Third") \ No newline at end of file