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