/
comp_ui_test.py
executable file
·177 lines (132 loc) · 5.75 KB
/
comp_ui_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python2
"""display_test.py: Tests for display.py."""
from __future__ import print_function
import cStringIO
import sys
import unittest
from core import comp_ui # module under test
from core import pyos
from core import util
import line_input
class VisualTest(unittest.TestCase):
def testPrintPacked(self):
matches = ['foo', 'bar', 'spam', 'eggs', 'python', 'perl']
longest_match_len = max(len(m) for m in matches)
max_lines = 10
for width in (1, 10, 20, 30, 40, 50):
f = cStringIO.StringIO()
n = comp_ui._PrintPacked(matches, longest_match_len, width,
max_lines, f)
out = f.getvalue()
lines = out.splitlines()
max_len = max(len(line) for line in lines)
print('WIDTH = %d' % width)
print('reported lines = %d' % n)
print('measured lines = %d' % len(lines))
print('max_len = %d' % max_len)
# Make sure it fits in the width!
# NOTE: This is too conservative because of non-printable characters
# like \n and ANSI escapes
if width != 1:
self.assertLess(max_len, width)
print('')
def testLongStringAndSkinnyTerminal(self):
matches = ['foo' * 10, 'bar' * 10, 'spams' * 10, 'zzz']
longest_match_len = max(len(m) for m in matches)
for width in (1, 10, 20, 30, 40, 50):
f = cStringIO.StringIO()
n = comp_ui._PrintPacked(matches, longest_match_len, width, 10, f)
out = f.getvalue()
lines = out.splitlines()
max_len = max(len(line) for line in lines)
print('WIDTH = %d' % width)
print('reported lines = %d' % n)
print('measured lines = %d' % len(lines))
print('max_len = %d' % max_len)
print('')
def testTooMany(self):
matches = ['--flag%d' % i for i in xrange(100)]
longest_match_len = max(len(m) for m in matches)
for width in (1, 10, 20, 30, 40, 50, 60):
f = cStringIO.StringIO()
n = comp_ui._PrintPacked(matches, longest_match_len, width, 10, f)
out = f.getvalue()
lines = out.splitlines()
max_len = max(len(line) for line in lines)
print('WIDTH = %d' % width)
print('reported lines = %d' % n)
# Count newlines since last one doesn't have a newline
print('measured lines = %d' % out.count('\n'))
print('max_len = %d' % max_len)
print('')
#print(out)
def testPrintLong(self):
matches = ['--all', '--almost-all', '--verbose']
longest_match_len = max(len(m) for m in matches)
descriptions = {
'--all': 'show all ' * 10, # truncate
'--almost-all': 'foo',
'--verbose': 'bar'
}
max_lines = 10
for width in (1, 10, 20, 30, 40, 50, 60):
f = cStringIO.StringIO()
n = comp_ui._PrintLong(matches, longest_match_len, width,
max_lines, descriptions, f)
out = f.getvalue()
lines = out.splitlines()
max_len = max(len(line) for line in lines)
print('WIDTH = %d' % width)
print('reported lines = %d' % n)
# Count newlines since last one doesn't have a newline
print('measured lines = %d' % out.count('\n'))
print('max_len = %d' % max_len)
print('')
#print(out)
class UiTest(unittest.TestCase):
def testDisplays(self):
comp_ui_state = comp_ui.State()
prompt_state = comp_ui.PromptState()
debug_f = util.DebugFile(sys.stdout)
signal_safe = pyos.InitSignalSafe()
# terminal width
d1 = comp_ui.NiceDisplay(80, comp_ui_state, prompt_state, debug_f,
line_input, signal_safe)
d2 = comp_ui.MinimalDisplay(comp_ui_state, prompt_state, debug_f)
prompt_state.SetLastPrompt('$ ')
for disp in [d1, d2]:
# This one is important
disp.EraseLines()
disp.Reset()
# These are related but we can just set them separately.
comp_ui_state.line_until_tab = 'echo ' # for returning to the prompt
comp_ui_state.display_pos = 5 # Strip this off every candidate
disp.PrintRequired('hello')
disp.PrintOptional('hello')
matches = ['echo one', 'echo two']
disp.PrintCandidates(None, matches, None)
# This needs to be aware of the terminal width.
# It's a bit odd since it's called as a side effect of the PromptEvaluator.
# That class knows about styles and so forth.
disp.ShowPromptOnRight('RIGHT')
class PromptTest(unittest.TestCase):
def testNoEscapes(self):
for prompt in ["> ", "osh>", "[[]][[]][][]]][["]:
self.assertEqual(comp_ui._PromptLen(prompt), len(prompt))
def testValidEscapes(self):
self.assertEqual(
comp_ui._PromptLen("\x01\033[01;34m\x02user\x01\033[00m\x02 >"),
len("user >"))
self.assertEqual(comp_ui._PromptLen("\x01\x02\x01\x02\x01\x02"), 0)
self.assertEqual(
comp_ui._PromptLen("\x01\x02 hi \x01hi\x02 \x01\x02 hello"),
len(" hi hello"))
def testNewline(self):
self.assertEqual(comp_ui._PromptLen("\n"), 0)
self.assertEqual(comp_ui._PromptLen("abc\ndef"), 3)
self.assertEqual(comp_ui._PromptLen(""), 0)
def testControlCharacters(self):
self.assertEqual(comp_ui._PromptLen("\xef"), 1)
self.assertEqual(comp_ui._PromptLen("\x03\x05"), 2)
if __name__ == '__main__':
unittest.main()