blob: b3616483c8a5babed71010d55ce39b5e32067c17 [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 Chenf8727752012-01-23 19:49:28 +000021 def test_settings_append_target_er(self):
22 """Test that 'settings append target.er' completes to 'settings append target.error-path'."""
23 self.complete_from_to('settings append target.er', 'settings append target.error-path')
24
25 def test_settings_insert_after_target_en(self):
26 """Test that 'settings insert-after target.en' completes to 'settings insert-after target.env-vars'."""
27 self.complete_from_to('settings insert-after target.en', 'settings insert-after target.env-vars')
28
29 def test_settings_insert_before_target_en(self):
30 """Test that 'settings insert-before target.en' completes to 'settings insert-before target.env-vars'."""
31 self.complete_from_to('settings insert-before target.en', 'settings insert-before target.env-vars')
32
Johnny Chenc8bba452012-01-21 01:45:18 +000033 def test_settings_replace_target_ru(self):
34 """Test that 'settings replace target.ru' completes to 'settings replace target.run-args'."""
35 self.complete_from_to('settings replace target.ru', 'settings replace target.run-args')
36
Johnny Chenfbcad682012-01-20 23:02:51 +000037 def test_settings_s(self):
38 """Test that 'settings s' completes to ['Available completions:', 'set', 'show']."""
39 self.complete_from_to('settings s', ['Available completions:', 'set', 'show'])
40
41 def test_settings_set_th(self):
42 """Test that 'settings set th' completes to 'settings set thread-format'."""
43 self.complete_from_to('settings set th', 'settings set thread-format')
44
45 def test_settings_s_dash(self):
46 """Test that 'settings set -' completes to ['Available completions:', '-n', '-r']."""
47 self.complete_from_to('settings set -', ['Available completions:', '-n', '-r'])
48
49 def test_settings_set_dash_r_th(self):
50 """Test that 'settings set -r th' completes to 'settings set -r thread-format'."""
51 self.complete_from_to('settings set -r th', 'settings set -r thread-format')
52
53 def test_settings_set_ta(self):
54 """Test that 'settings set ta' completes to 'settings set target.'."""
55 self.complete_from_to('settings set ta', 'settings set target.')
56
57 def test_settings_set_target_pr(self):
58 """Test that 'settings set target.pr' completes to ['Available completions:',
59 'target.prefer-dynamic-value', 'target.process.']."""
60 self.complete_from_to('settings set target.pr',
61 ['Available completions:',
62 'target.prefer-dynamic-value',
63 'target.process.'])
64
65 def test_settings_set_target_process(self):
66 """Test that 'settings set target.process' completes to 'settings set target.process.'."""
67 self.complete_from_to('settings set target.process', 'settings set target.process.')
68
69 def test_settings_set_target_process_dot(self):
70 """Test that 'settings set target.process.' completes to 'settings set target.process.thread.'."""
71 self.complete_from_to('settings set target.process.', 'settings set target.process.thread.')
72
73 def test_settings_set_target_process_thread_dot(self):
74 """Test that 'settings set target.process.thread.' completes to ['Available completions:',
75 'target.process.thread.step-avoid-regexp', 'target.process.thread.trace-thread']."""
76 self.complete_from_to('settings set target.process.thread.',
77 ['Available completions:',
78 'target.process.thread.step-avoid-regexp',
79 'target.process.thread.trace-thread'])
80
Johnny Chen5b6e5652012-01-20 23:34:35 +000081 def complete_from_to(self, str_input, patterns):
82 """Test the completion mechanism completes str_input to pattern, where
Johnny Chenfbcad682012-01-20 23:02:51 +000083 patterns could be a pattern-string or a list of pattern-strings"""
84 prompt = "(lldb) "
85 add_prompt = "Enter your stop hook command(s). Type 'DONE' to end.\r\n> "
86 add_prompt1 = "\r\n> "
87
88 # So that the child gets torn down after the test.
89 self.child = pexpect.spawn('%s %s' % (self.lldbHere, self.lldbOption))
90 child = self.child
91 # Turn on logging for input/output to/from the child.
92 with open('child_send.txt', 'w') as f_send:
93 with open('child_read.txt', 'w') as f_read:
94 child.logfile_send = f_send
95 child.logfile_read = f_read
96
Johnny Chenfbcad682012-01-20 23:02:51 +000097 child.expect_exact(prompt)
98 child.setecho(True)
Johnny Chen5b6e5652012-01-20 23:34:35 +000099 # Sends str_input and a Tab to invoke the completion machinery.
100 child.send("%s\t" % str_input)
Johnny Chenfbcad682012-01-20 23:02:51 +0000101 child.sendline('')
102 child.expect_exact(prompt)
103
Johnny Chen47397c72012-01-21 00:08:37 +0000104 # Set logfile to None to stop logging.
105 child.logfile_send = None
106 child.logfile_read = None
107
Johnny Chenfbcad682012-01-20 23:02:51 +0000108 with open('child_send.txt', 'r') as fs:
109 if self.TraceOn():
110 print "\n\nContents of child_send.txt:"
111 print fs.read()
112 with open('child_read.txt', 'r') as fr:
113 from_child = fr.read()
114 if self.TraceOn():
115 print "\n\nContents of child_read.txt:"
116 print from_child
117
118 self.assertFalse(patterns is None)
119 if type(patterns) is not types.ListType:
120 patterns = [patterns]
121
Johnny Chen5b6e5652012-01-20 23:34:35 +0000122 # Test that str_input completes to our patterns.
Johnny Chenfbcad682012-01-20 23:02:51 +0000123 # If each pattern matches from_child, the completion mechanism works!
124 for p in patterns:
Johnny Chen5b6e5652012-01-20 23:34:35 +0000125 self.expect(from_child, msg=COMPLETIOND_MSG(str_input, p), exe=False,
Johnny Chenfbcad682012-01-20 23:02:51 +0000126 patterns = [p])
127
Johnny Chenfbcad682012-01-20 23:02:51 +0000128
129if __name__ == '__main__':
130 import atexit
131 lldb.SBDebugger.Initialize()
132 atexit.register(lambda: lldb.SBDebugger.Terminate())
133 unittest2.main()