pyreadline: Adding test suite for history buffer

This commit is contained in:
jstenar
2007-10-30 20:02:10 +00:00
parent 8089527e23
commit 4cc7d71026
3 changed files with 120 additions and 5 deletions
+4
View File
@@ -1,3 +1,7 @@
2007-10-30 Jörgen Stenarson <jorgen.stenarson -at- bostream.nu>
* 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 <jorgen.stenarson -at- bostream.nu>
* Patch as provided by pan. Changed ensure_text to decode to
consoles codepage instead of utf8, to ensure proper printing
+12 -5
View File
@@ -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
+104
View File
@@ -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")