blob: 1af0bb4d1a57a4cff5823b8e9cc2bb7d1617eb86 [file] [log] [blame]
Johnny Chenfbcad682012-01-20 23:02:51 +00001"""
2Test the lldb command line completion mechanism.
3"""
4
5import os
6import unittest2
7import lldb
8import pexpect
9from lldbtest import *
10
11class CommandLineCompletionTestCase(TestBase):
12
13 mydir = os.path.join("functionalities", "completion")
14
15 @classmethod
16 def classCleanup(cls):
17 """Cleanup the test byproducts."""
18 system(["/bin/sh", "-c", "rm -f child_send.txt"])
19 system(["/bin/sh", "-c", "rm -f child_read.txt"])
20
Johnny Chen42b7ff92012-01-25 01:37:36 +000021 def test_frame_variable_dash_w(self):
22 """Test that 'frame variable -w' completes to 'frame variable -w '."""
23 self.complete_from_to('frame variable -w', 'frame variable -w ')
24
25 def test_frame_variable_dash_w_space(self):
26 """Test that 'frame variable -w ' completes to ['Available completions:', 'read', 'write', 'read_write']."""
27 self.complete_from_to('frame variable -w ', ['Available completions:', 'read', 'write', 'read_write'])
28
Johnny Chen42404d22012-02-08 22:37:48 +000029 def test_watchpoint_set_ex(self):
30 """Test that 'watchpoint set ex' completes to 'watchpoint set expression '."""
31 self.complete_from_to('watchpoint set ex', 'watchpoint set expression ')
Johnny Chen28f37712012-01-31 01:26:28 +000032
Johnny Chen42404d22012-02-08 22:37:48 +000033 def test_watchpoint_set_var(self):
34 """Test that 'watchpoint set var' completes to 'watchpoint set variable '."""
35 self.complete_from_to('watchpoint set var', 'watchpoint set variable ')
36
37 def test_watchpoint_set_variable_dash_w_read_underbar(self):
38 """Test that 'watchpoint set variable -w read_' completes to 'watchpoint set variable -w read_write'."""
39 self.complete_from_to('watchpoint set variable -w read_', 'watchpoint set variable -w read_write')
Johnny Chen28f37712012-01-31 01:26:28 +000040
Johnny Chen42b7ff92012-01-25 01:37:36 +000041 def test_help_fi(self):
42 """Test that 'help fi' completes to ['Available completions:', 'file', 'finish']."""
43 self.complete_from_to('help fi', ['Available completions:', 'file', 'finish'])
44
Johnny Chen28f37712012-01-31 01:26:28 +000045 def test_help_watchpoint_s(self):
46 """Test that 'help watchpoint s' completes to 'help watchpoint set '."""
47 self.complete_from_to('help watchpoint s', 'help watchpoint set ')
48
Johnny Chenf8727752012-01-23 19:49:28 +000049 def test_settings_append_target_er(self):
50 """Test that 'settings append target.er' completes to 'settings append target.error-path'."""
51 self.complete_from_to('settings append target.er', 'settings append target.error-path')
52
53 def test_settings_insert_after_target_en(self):
54 """Test that 'settings insert-after target.en' completes to 'settings insert-after target.env-vars'."""
55 self.complete_from_to('settings insert-after target.en', 'settings insert-after target.env-vars')
56
57 def test_settings_insert_before_target_en(self):
58 """Test that 'settings insert-before target.en' completes to 'settings insert-before target.env-vars'."""
59 self.complete_from_to('settings insert-before target.en', 'settings insert-before target.env-vars')
60
Johnny Chenc8bba452012-01-21 01:45:18 +000061 def test_settings_replace_target_ru(self):
62 """Test that 'settings replace target.ru' completes to 'settings replace target.run-args'."""
63 self.complete_from_to('settings replace target.ru', 'settings replace target.run-args')
64
Johnny Chenfbcad682012-01-20 23:02:51 +000065 def test_settings_s(self):
66 """Test that 'settings s' completes to ['Available completions:', 'set', 'show']."""
67 self.complete_from_to('settings s', ['Available completions:', 'set', 'show'])
68
69 def test_settings_set_th(self):
70 """Test that 'settings set th' completes to 'settings set thread-format'."""
71 self.complete_from_to('settings set th', 'settings set thread-format')
72
73 def test_settings_s_dash(self):
74 """Test that 'settings set -' completes to ['Available completions:', '-n', '-r']."""
75 self.complete_from_to('settings set -', ['Available completions:', '-n', '-r'])
76
77 def test_settings_set_dash_r_th(self):
78 """Test that 'settings set -r th' completes to 'settings set -r thread-format'."""
79 self.complete_from_to('settings set -r th', 'settings set -r thread-format')
80
81 def test_settings_set_ta(self):
82 """Test that 'settings set ta' completes to 'settings set target.'."""
83 self.complete_from_to('settings set ta', 'settings set target.')
84
85 def test_settings_set_target_pr(self):
86 """Test that 'settings set target.pr' completes to ['Available completions:',
87 'target.prefer-dynamic-value', 'target.process.']."""
88 self.complete_from_to('settings set target.pr',
89 ['Available completions:',
90 'target.prefer-dynamic-value',
91 'target.process.'])
92
93 def test_settings_set_target_process(self):
94 """Test that 'settings set target.process' completes to 'settings set target.process.'."""
95 self.complete_from_to('settings set target.process', 'settings set target.process.')
96
97 def test_settings_set_target_process_dot(self):
98 """Test that 'settings set target.process.' completes to 'settings set target.process.thread.'."""
99 self.complete_from_to('settings set target.process.', 'settings set target.process.thread.')
100
101 def test_settings_set_target_process_thread_dot(self):
102 """Test that 'settings set target.process.thread.' completes to ['Available completions:',
103 'target.process.thread.step-avoid-regexp', 'target.process.thread.trace-thread']."""
104 self.complete_from_to('settings set target.process.thread.',
105 ['Available completions:',
106 'target.process.thread.step-avoid-regexp',
107 'target.process.thread.trace-thread'])
108
Johnny Chen4ab0c032012-02-03 01:14:42 +0000109 def test_target_space(self):
110 """Test that 'target ' completes to ['Available completions:', 'create', 'delete', 'list',
111 'modules', 'select', 'stop-hook', 'variable']."""
112 self.complete_from_to('target ',
113 ['Available completions:', 'create', 'delete', 'list',
114 'modules', 'select', 'stop-hook', 'variable'])
115
116 def test_target_va(self):
117 """Test that 'target va' completes to 'target variable '."""
118 self.complete_from_to('target va', 'target variable ')
119
Johnny Chen5b6e5652012-01-20 23:34:35 +0000120 def complete_from_to(self, str_input, patterns):
Johnny Chen39fb5c22012-01-25 20:25:38 +0000121 """Test that the completion mechanism completes str_input to patterns,
122 where patterns could be a pattern-string or a list of pattern-strings"""
Johnny Chen758442a2012-01-25 20:50:21 +0000123 # Patterns should not be None in order to proceed.
124 self.assertFalse(patterns is None)
125 # And should be either a string or list of strings. Check for list type
126 # below, if not, make a list out of the singleton string. If patterns
127 # is not a string or not a list of strings, there'll be runtime errors
128 # later on.
129 if not isinstance(patterns, list):
130 patterns = [patterns]
131
Johnny Chen39fb5c22012-01-25 20:25:38 +0000132 # The default lldb prompt.
Johnny Chenfbcad682012-01-20 23:02:51 +0000133 prompt = "(lldb) "
Johnny Chenfbcad682012-01-20 23:02:51 +0000134
135 # So that the child gets torn down after the test.
136 self.child = pexpect.spawn('%s %s' % (self.lldbHere, self.lldbOption))
137 child = self.child
138 # Turn on logging for input/output to/from the child.
139 with open('child_send.txt', 'w') as f_send:
140 with open('child_read.txt', 'w') as f_read:
141 child.logfile_send = f_send
142 child.logfile_read = f_read
143
Johnny Chenfbcad682012-01-20 23:02:51 +0000144 child.expect_exact(prompt)
145 child.setecho(True)
Johnny Chen5b6e5652012-01-20 23:34:35 +0000146 # Sends str_input and a Tab to invoke the completion machinery.
147 child.send("%s\t" % str_input)
Johnny Chenfbcad682012-01-20 23:02:51 +0000148 child.sendline('')
149 child.expect_exact(prompt)
150
Johnny Chen61d44fa2012-01-25 20:29:26 +0000151 # Now that the necessary logging is done, restore logfile to None to
152 # stop further logging.
Johnny Chen47397c72012-01-21 00:08:37 +0000153 child.logfile_send = None
154 child.logfile_read = None
155
Johnny Chenfbcad682012-01-20 23:02:51 +0000156 with open('child_send.txt', 'r') as fs:
157 if self.TraceOn():
158 print "\n\nContents of child_send.txt:"
159 print fs.read()
160 with open('child_read.txt', 'r') as fr:
161 from_child = fr.read()
162 if self.TraceOn():
163 print "\n\nContents of child_read.txt:"
164 print from_child
165
Johnny Chen5b6e5652012-01-20 23:34:35 +0000166 # Test that str_input completes to our patterns.
Johnny Chenfbcad682012-01-20 23:02:51 +0000167 # If each pattern matches from_child, the completion mechanism works!
168 for p in patterns:
Johnny Chen5b6e5652012-01-20 23:34:35 +0000169 self.expect(from_child, msg=COMPLETIOND_MSG(str_input, p), exe=False,
Johnny Chenfbcad682012-01-20 23:02:51 +0000170 patterns = [p])
171
Johnny Chenfbcad682012-01-20 23:02:51 +0000172
173if __name__ == '__main__':
174 import atexit
175 lldb.SBDebugger.Initialize()
176 atexit.register(lambda: lldb.SBDebugger.Terminate())
177 unittest2.main()