OILS / core / comp_ui_test.py View on Github | oils.pub

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