test_ansi_code_processor.py
171 lines
| 6.8 KiB
| text/x-python
|
PythonLexer
epatters
|
r2716 | # Standard library imports | ||
import unittest | ||||
# Local imports | ||||
Fernando Perez
|
r11024 | from IPython.qt.console.ansi_code_processor import AnsiCodeProcessor | ||
epatters
|
r2716 | |||
class TestAnsiCodeProcessor(unittest.TestCase): | ||||
def setUp(self): | ||||
self.processor = AnsiCodeProcessor() | ||||
epatters
|
r3367 | def test_clear(self): | ||
""" Do control sequences for clearing the console work? | ||||
""" | ||||
epatters
|
r2783 | string = '\x1b[2J\x1b[K' | ||
i = -1 | ||||
for i, substring in enumerate(self.processor.split_string(string)): | ||||
if i == 0: | ||||
Bradley M. Froehle
|
r7874 | self.assertEqual(len(self.processor.actions), 1) | ||
epatters
|
r2783 | action = self.processor.actions[0] | ||
Bradley M. Froehle
|
r7874 | self.assertEqual(action.action, 'erase') | ||
self.assertEqual(action.area, 'screen') | ||||
self.assertEqual(action.erase_to, 'all') | ||||
epatters
|
r2783 | elif i == 1: | ||
Bradley M. Froehle
|
r7874 | self.assertEqual(len(self.processor.actions), 1) | ||
epatters
|
r2783 | action = self.processor.actions[0] | ||
Bradley M. Froehle
|
r7874 | self.assertEqual(action.action, 'erase') | ||
self.assertEqual(action.area, 'line') | ||||
self.assertEqual(action.erase_to, 'end') | ||||
epatters
|
r2783 | else: | ||
self.fail('Too many substrings.') | ||||
Bradley M. Froehle
|
r7874 | self.assertEqual(i, 1, 'Too few substrings.') | ||
epatters
|
r2783 | |||
epatters
|
r3367 | def test_colors(self): | ||
""" Do basic controls sequences for colors work? | ||||
""" | ||||
string = 'first\x1b[34mblue\x1b[0mlast' | ||||
epatters
|
r2716 | i = -1 | ||
for i, substring in enumerate(self.processor.split_string(string)): | ||||
if i == 0: | ||||
Bradley M. Froehle
|
r7874 | self.assertEqual(substring, 'first') | ||
self.assertEqual(self.processor.foreground_color, None) | ||||
epatters
|
r2716 | elif i == 1: | ||
Bradley M. Froehle
|
r7874 | self.assertEqual(substring, 'blue') | ||
self.assertEqual(self.processor.foreground_color, 4) | ||||
epatters
|
r2716 | elif i == 2: | ||
Bradley M. Froehle
|
r7874 | self.assertEqual(substring, 'last') | ||
self.assertEqual(self.processor.foreground_color, None) | ||||
epatters
|
r2716 | else: | ||
epatters
|
r2783 | self.fail('Too many substrings.') | ||
Bradley M. Froehle
|
r7874 | self.assertEqual(i, 2, 'Too few substrings.') | ||
epatters
|
r2716 | |||
epatters
|
r3367 | def test_colors_xterm(self): | ||
""" Do xterm-specific control sequences for colors work? | ||||
""" | ||||
string = '\x1b]4;20;rgb:ff/ff/ff\x1b' \ | ||||
'\x1b]4;25;rgbi:1.0/1.0/1.0\x1b' | ||||
substrings = list(self.processor.split_string(string)) | ||||
desired = { 20 : (255, 255, 255), | ||||
25 : (255, 255, 255) } | ||||
Bradley M. Froehle
|
r7874 | self.assertEqual(self.processor.color_map, desired) | ||
epatters
|
r3367 | |||
string = '\x1b[38;5;20m\x1b[48;5;25m' | ||||
substrings = list(self.processor.split_string(string)) | ||||
Bradley M. Froehle
|
r7874 | self.assertEqual(self.processor.foreground_color, 20) | ||
self.assertEqual(self.processor.background_color, 25) | ||||
epatters
|
r3367 | |||
def test_scroll(self): | ||||
""" Do control sequences for scrolling the buffer work? | ||||
""" | ||||
epatters
|
r2998 | string = '\x1b[5S\x1b[T' | ||
i = -1 | ||||
for i, substring in enumerate(self.processor.split_string(string)): | ||||
if i == 0: | ||||
Bradley M. Froehle
|
r7874 | self.assertEqual(len(self.processor.actions), 1) | ||
epatters
|
r2998 | action = self.processor.actions[0] | ||
Bradley M. Froehle
|
r7874 | self.assertEqual(action.action, 'scroll') | ||
self.assertEqual(action.dir, 'up') | ||||
self.assertEqual(action.unit, 'line') | ||||
self.assertEqual(action.count, 5) | ||||
epatters
|
r2998 | elif i == 1: | ||
Bradley M. Froehle
|
r7874 | self.assertEqual(len(self.processor.actions), 1) | ||
epatters
|
r2998 | action = self.processor.actions[0] | ||
Bradley M. Froehle
|
r7874 | self.assertEqual(action.action, 'scroll') | ||
self.assertEqual(action.dir, 'down') | ||||
self.assertEqual(action.unit, 'line') | ||||
self.assertEqual(action.count, 1) | ||||
epatters
|
r2998 | else: | ||
self.fail('Too many substrings.') | ||||
Bradley M. Froehle
|
r7874 | self.assertEqual(i, 1, 'Too few substrings.') | ||
epatters
|
r2998 | |||
Michael Droettboom
|
r5567 | def test_formfeed(self): | ||
""" Are formfeed characters processed correctly? | ||||
epatters
|
r3367 | """ | ||
epatters
|
r2998 | string = '\f' # form feed | ||
Bradley M. Froehle
|
r7874 | self.assertEqual(list(self.processor.split_string(string)), ['']) | ||
self.assertEqual(len(self.processor.actions), 1) | ||||
epatters
|
r2998 | action = self.processor.actions[0] | ||
Bradley M. Froehle
|
r7874 | self.assertEqual(action.action, 'scroll') | ||
self.assertEqual(action.dir, 'down') | ||||
self.assertEqual(action.unit, 'page') | ||||
self.assertEqual(action.count, 1) | ||||
epatters
|
r2998 | |||
Michael Droettboom
|
r5567 | def test_carriage_return(self): | ||
""" Are carriage return characters processed correctly? | ||||
""" | ||||
MinRK
|
r5661 | string = 'foo\rbar' # carriage return | ||
Puneeth Chaganti
|
r6436 | splits = [] | ||
actions = [] | ||||
for split in self.processor.split_string(string): | ||||
splits.append(split) | ||||
actions.append([action.action for action in self.processor.actions]) | ||||
Bradley M. Froehle
|
r7874 | self.assertEqual(splits, ['foo', None, 'bar']) | ||
self.assertEqual(actions, [[], ['carriage-return'], []]) | ||||
Michael Droettboom
|
r5567 | |||
MinRK
|
r5661 | def test_carriage_return_newline(self): | ||
"""transform CRLF to LF""" | ||||
Puneeth Chaganti
|
r6436 | string = 'foo\rbar\r\ncat\r\n\n' # carriage return and newline | ||
MinRK
|
r5661 | # only one CR action should occur, and '\r\n' should transform to '\n' | ||
Puneeth Chaganti
|
r6436 | splits = [] | ||
actions = [] | ||||
for split in self.processor.split_string(string): | ||||
splits.append(split) | ||||
actions.append([action.action for action in self.processor.actions]) | ||||
Bradley M. Froehle
|
r7874 | self.assertEqual(splits, ['foo', None, 'bar', '\r\n', 'cat', '\r\n', '\n']) | ||
self.assertEqual(actions, [[], ['carriage-return'], [], ['newline'], [], ['newline'], ['newline']]) | ||||
MinRK
|
r5661 | |||
Michael Droettboom
|
r5567 | def test_beep(self): | ||
""" Are beep characters processed correctly? | ||||
""" | ||||
Puneeth Chaganti
|
r6436 | string = 'foo\abar' # bell | ||
splits = [] | ||||
actions = [] | ||||
for split in self.processor.split_string(string): | ||||
splits.append(split) | ||||
actions.append([action.action for action in self.processor.actions]) | ||||
Bradley M. Froehle
|
r7874 | self.assertEqual(splits, ['foo', None, 'bar']) | ||
self.assertEqual(actions, [[], ['beep'], []]) | ||||
Puneeth Chaganti
|
r6436 | |||
def test_backspace(self): | ||||
""" Are backspace characters processed correctly? | ||||
""" | ||||
string = 'foo\bbar' # backspace | ||||
splits = [] | ||||
actions = [] | ||||
for split in self.processor.split_string(string): | ||||
splits.append(split) | ||||
actions.append([action.action for action in self.processor.actions]) | ||||
Bradley M. Froehle
|
r7874 | self.assertEqual(splits, ['foo', None, 'bar']) | ||
self.assertEqual(actions, [[], ['backspace'], []]) | ||||
Puneeth Chaganti
|
r6436 | |||
def test_combined(self): | ||||
""" Are CR and BS characters processed correctly in combination? | ||||
BS is treated as a change in print position, rather than a | ||||
backwards character deletion. Therefore a BS at EOL is | ||||
effectively ignored. | ||||
""" | ||||
string = 'abc\rdef\b' # CR and backspace | ||||
splits = [] | ||||
actions = [] | ||||
for split in self.processor.split_string(string): | ||||
splits.append(split) | ||||
actions.append([action.action for action in self.processor.actions]) | ||||
Bradley M. Froehle
|
r7874 | self.assertEqual(splits, ['abc', None, 'def', None]) | ||
self.assertEqual(actions, [[], ['carriage-return'], [], ['backspace']]) | ||||
Michael Droettboom
|
r5567 | |||
epatters
|
r2716 | |||
if __name__ == '__main__': | ||||
unittest.main() | ||||