blob: 7b3a9ff2f843d95131466e664fb3e1d6207f1bc3 [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
21 def test_settings_s(self):
22 """Test that 'settings s' completes to ['Available completions:', 'set', 'show']."""
23 self.complete_from_to('settings s', ['Available completions:', 'set', 'show'])
24
25 def test_settings_set_th(self):
26 """Test that 'settings set th' completes to 'settings set thread-format'."""
27 self.complete_from_to('settings set th', 'settings set thread-format')
28
29 def test_settings_s_dash(self):
30 """Test that 'settings set -' completes to ['Available completions:', '-n', '-r']."""
31 self.complete_from_to('settings set -', ['Available completions:', '-n', '-r'])
32
33 def test_settings_set_dash_r_th(self):
34 """Test that 'settings set -r th' completes to 'settings set -r thread-format'."""
35 self.complete_from_to('settings set -r th', 'settings set -r thread-format')
36
37 def test_settings_set_ta(self):
38 """Test that 'settings set ta' completes to 'settings set target.'."""
39 self.complete_from_to('settings set ta', 'settings set target.')
40
41 def test_settings_set_target_pr(self):
42 """Test that 'settings set target.pr' completes to ['Available completions:',
43 'target.prefer-dynamic-value', 'target.process.']."""
44 self.complete_from_to('settings set target.pr',
45 ['Available completions:',
46 'target.prefer-dynamic-value',
47 'target.process.'])
48
49 def test_settings_set_target_process(self):
50 """Test that 'settings set target.process' completes to 'settings set target.process.'."""
51 self.complete_from_to('settings set target.process', 'settings set target.process.')
52
53 def test_settings_set_target_process_dot(self):
54 """Test that 'settings set target.process.' completes to 'settings set target.process.thread.'."""
55 self.complete_from_to('settings set target.process.', 'settings set target.process.thread.')
56
57 def test_settings_set_target_process_thread_dot(self):
58 """Test that 'settings set target.process.thread.' completes to ['Available completions:',
59 'target.process.thread.step-avoid-regexp', 'target.process.thread.trace-thread']."""
60 self.complete_from_to('settings set target.process.thread.',
61 ['Available completions:',
62 'target.process.thread.step-avoid-regexp',
63 'target.process.thread.trace-thread'])
64
Johnny Chen5b6e5652012-01-20 23:34:35 +000065 def complete_from_to(self, str_input, patterns):
66 """Test the completion mechanism completes str_input to pattern, where
Johnny Chenfbcad682012-01-20 23:02:51 +000067 patterns could be a pattern-string or a list of pattern-strings"""
68 prompt = "(lldb) "
69 add_prompt = "Enter your stop hook command(s). Type 'DONE' to end.\r\n> "
70 add_prompt1 = "\r\n> "
71
72 # So that the child gets torn down after the test.
73 self.child = pexpect.spawn('%s %s' % (self.lldbHere, self.lldbOption))
74 child = self.child
75 # Turn on logging for input/output to/from the child.
76 with open('child_send.txt', 'w') as f_send:
77 with open('child_read.txt', 'w') as f_read:
78 child.logfile_send = f_send
79 child.logfile_read = f_read
80
Johnny Chenfbcad682012-01-20 23:02:51 +000081 child.expect_exact(prompt)
82 child.setecho(True)
Johnny Chen5b6e5652012-01-20 23:34:35 +000083 # Sends str_input and a Tab to invoke the completion machinery.
84 child.send("%s\t" % str_input)
Johnny Chenfbcad682012-01-20 23:02:51 +000085 child.sendline('')
86 child.expect_exact(prompt)
87
Johnny Chen47397c72012-01-21 00:08:37 +000088 # Set logfile to None to stop logging.
89 child.logfile_send = None
90 child.logfile_read = None
91
Johnny Chenfbcad682012-01-20 23:02:51 +000092 with open('child_send.txt', 'r') as fs:
93 if self.TraceOn():
94 print "\n\nContents of child_send.txt:"
95 print fs.read()
96 with open('child_read.txt', 'r') as fr:
97 from_child = fr.read()
98 if self.TraceOn():
99 print "\n\nContents of child_read.txt:"
100 print from_child
101
102 self.assertFalse(patterns is None)
103 if type(patterns) is not types.ListType:
104 patterns = [patterns]
105
Johnny Chen5b6e5652012-01-20 23:34:35 +0000106 # Test that str_input completes to our patterns.
Johnny Chenfbcad682012-01-20 23:02:51 +0000107 # If each pattern matches from_child, the completion mechanism works!
108 for p in patterns:
Johnny Chen5b6e5652012-01-20 23:34:35 +0000109 self.expect(from_child, msg=COMPLETIOND_MSG(str_input, p), exe=False,
Johnny Chenfbcad682012-01-20 23:02:51 +0000110 patterns = [p])
111
Johnny Chenfbcad682012-01-20 23:02:51 +0000112
113if __name__ == '__main__':
114 import atexit
115 lldb.SBDebugger.Initialize()
116 atexit.register(lambda: lldb.SBDebugger.Terminate())
117 unittest2.main()