Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.pyc

/dist/
/build/

/*.egg-info
129 changes: 105 additions & 24 deletions tests/terminal_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def testLineWrap(self):

def testIssue1(self):
self.assertEqual(10, len(terminal.StripAnsiText('boembabies' '\033[0m')))
terminal.TerminalSize = lambda: (10, 10)
terminal.shutil.get_terminal_size = lambda: (10, 10)
text1 = terminal.LineWrap('\033[32m' + 'boembabies, ' * 10 + 'boembabies' +
'\033[0m', omit_sgr=True)
text2 = ('\033[32m' +
Expand All @@ -110,12 +110,20 @@ def __init__(self):

# pylint: disable=C6409
def write(self, text):
self.output += text
# Ignore initial clear screen output.
if text != '\033[2J\033[H':
self.output += text

# pylint: disable=C6409
def CountLines(self):
return len(self.output.splitlines())

def Show(self):
return self.output

def Clear(self):
self.output = ''

def flush(self):
pass

Expand All @@ -124,39 +132,112 @@ class PagerTest(unittest.TestCase):

def setUp(self):
super(PagerTest, self).setUp()
sys.stdout = FakeTerminal()
self.get_ch_orig = terminal.Pager._GetCh
terminal.Pager._GetCh = lambda self: 'q'
self._output = FakeTerminal()
sys.stdout = self._output
self._getchar_orig = terminal._GetChar
# Quit the pager immediately after the first page.
terminal._GetChar = lambda: 'q'

self.p = terminal.Pager()
self._sample_text = ''
for i in range(10):
self._sample_text += str(i) + '\n'

self.p = terminal.Pager(self._sample_text)
# Both the Prompt, and the ClearPrompt need to be accounted for.
self._prompt_lines = 2

def tearDown(self):
super(PagerTest, self).tearDown()
terminal.Pager._GetCh = self.get_ch_orig
terminal._GetChar = self._getchar_orig
sys.stdout = sys.__stdout__

def testPager(self):

self.p.Clear()
self.assertEqual('', self.p._text)
self.assertEqual(0, self.p._displayed)
self.assertEqual(1, self.p._lastscroll)
def testDisplay(self):
# Display a couple of rows (20%).
self.assertEqual(self.p._Display(0, 2), (2, 20.0, 10))
self.assertEqual(self._output.Show(), '0\n1\n')
self._output.Clear()
self.assertEqual(self.p._Display(3, 2), (5, 50.0, 10))
self.assertEqual(self._output.Show(), '3\n4\n')
self._output.Clear()
# Display past the end of the text.
self.assertEqual(self.p._Display(8, 3), (10, 100.0, 10))
self.assertEqual(self._output.Show(), '8\n9\n')
self._output.Clear()
# Display before the start. Displays form the start.
self.assertEqual(self.p._Display(-1, 2), (2, 20.0, 10))
self.assertEqual(self._output.Show(), '0\n1\n')
self._output.Clear()
# Display the rest of the text.
self.assertEqual(self.p._Display(7), (10, 100.0, 10))
self.assertEqual(self._output.Show(), '7\n8\n9\n')

def testPageAddsText(self):
extra_text = '10\n11\n'
self.p.Page(extra_text)
self.assertEqual(self.p._text, self._sample_text + extra_text)

def testPage(self):
txt = ''
for i in range(100):
txt += '%d a random line of text here\n' % i
self.p._text = txt
self.p.SetLines(3)
self.p.Page()
self.assertEqual(
self._output.Show().splitlines()[:-self._prompt_lines], ['0', '1', '2'])

def testPrompt(self):
self.p.SetLines(2)
# After paging once the progress will be 20%.
self.p.Page()
self._output.Clear()
self.assertEqual(self.p._Prompt(), terminal.AnsiText(
'n: next line, Space: next page, b: prev page, q: quit.',
['green']))
# truncate width to 10 cols, prompt should be likewise truncated.
self.p._cols = 10
self.assertEqual(self.p._Prompt(),
terminal.AnsiText('n: next li', ['green']))

def testPagerClear(self):
self.p.SetLines(2)
self.p.Page()
self.p.Reset()
# Clear output we aren't testing.
self._output.Clear()
# Paging after Reset resumes from the start.
self.p.Page()
self.assertEqual(self.p._cli_lines+2, sys.stdout.CountLines())
self.assertEqual(self._output.Show().splitlines()[:-self._prompt_lines],
['0', '1'])

sys.stdout.output = ''
self.p = terminal.Pager()
self.p._text = ''
for _ in range(10):
self.p._text += 'a' * 100 + '\n'
def testPageAddPercent(self):
self.p.SetLines(2)
self.p.Page()
self.assertEqual(terminal.StripAnsiText(
self._output.Show().splitlines()[-self._prompt_lines]),
terminal.PROMPT_QUESTION + ' (20%)')
self.p.Page()
self.assertEqual(terminal.StripAnsiText(
self._output.Show().splitlines()[-self._prompt_lines]),
terminal.PROMPT_QUESTION + ' (40%)')
self.p.Page('10\n11\n')
# 50%, rather than 60%, as the total size increased from 10 to 12.
# But we don't show percent, as the source is streamed.
self.assertEqual(terminal.StripAnsiText(
self._output.Show().splitlines()[-self._prompt_lines]),
terminal.PROMPT_QUESTION)
self.p.Page('12\n13\n14\n15')
self.assertEqual(terminal.StripAnsiText(
self._output.Show().splitlines()[-self._prompt_lines]),
terminal.PROMPT_QUESTION)
self.p.Page()
self.assertEqual(terminal.StripAnsiText(
self._output.Show().splitlines()[-self._prompt_lines]),
terminal.PROMPT_QUESTION + ' (%d%%)' % (10 / 16 * 100))

def testBlankLines(self):
buffer = 'First line.\n\nThird line.\n'
self.p = terminal.Pager(buffer)
self.p.SetLines(4)
self.p.Page()
self.assertEqual(20, sys.stdout.CountLines())
self.assertEqual(self._output.Show().splitlines()[:-self._prompt_lines],
buffer.splitlines())


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion textfsm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
"""
from textfsm.parser import *

__version__ = '2.0.0'
__version__ = '2.1.0'
Loading