TestCompletion.py revision 42b7ff92105e1b9e96c870ebea2ce6ab601d8809
1"""
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_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
29    def test_help_fi(self):
30        """Test that 'help fi' completes to ['Available completions:', 'file', 'finish']."""
31        self.complete_from_to('help fi', ['Available completions:', 'file', 'finish'])
32
33    def test_settings_append_target_er(self):
34        """Test that 'settings append target.er' completes to 'settings append target.error-path'."""
35        self.complete_from_to('settings append target.er', 'settings append target.error-path')
36
37    def test_settings_insert_after_target_en(self):
38        """Test that 'settings insert-after target.en' completes to 'settings insert-after target.env-vars'."""
39        self.complete_from_to('settings insert-after target.en', 'settings insert-after target.env-vars')
40
41    def test_settings_insert_before_target_en(self):
42        """Test that 'settings insert-before target.en' completes to 'settings insert-before target.env-vars'."""
43        self.complete_from_to('settings insert-before target.en', 'settings insert-before target.env-vars')
44
45    def test_settings_replace_target_ru(self):
46        """Test that 'settings replace target.ru' completes to 'settings replace target.run-args'."""
47        self.complete_from_to('settings replace target.ru', 'settings replace target.run-args')
48
49    def test_settings_s(self):
50        """Test that 'settings s' completes to ['Available completions:', 'set', 'show']."""
51        self.complete_from_to('settings s', ['Available completions:', 'set', 'show'])
52
53    def test_settings_set_th(self):
54        """Test that 'settings set th' completes to 'settings set thread-format'."""
55        self.complete_from_to('settings set th', 'settings set thread-format')
56
57    def test_settings_s_dash(self):
58        """Test that 'settings set -' completes to ['Available completions:', '-n', '-r']."""
59        self.complete_from_to('settings set -', ['Available completions:', '-n', '-r'])
60
61    def test_settings_set_dash_r_th(self):
62        """Test that 'settings set -r th' completes to 'settings set -r thread-format'."""
63        self.complete_from_to('settings set -r th', 'settings set -r thread-format')
64
65    def test_settings_set_ta(self):
66        """Test that 'settings set ta' completes to 'settings set target.'."""
67        self.complete_from_to('settings set ta', 'settings set target.')
68
69    def test_settings_set_target_pr(self):
70        """Test that 'settings set target.pr' completes to ['Available completions:',
71        'target.prefer-dynamic-value', 'target.process.']."""
72        self.complete_from_to('settings set target.pr',
73                              ['Available completions:',
74                               'target.prefer-dynamic-value',
75                               'target.process.'])
76
77    def test_settings_set_target_process(self):
78        """Test that 'settings set target.process' completes to 'settings set target.process.'."""
79        self.complete_from_to('settings set target.process', 'settings set target.process.')
80
81    def test_settings_set_target_process_dot(self):
82        """Test that 'settings set target.process.' completes to 'settings set target.process.thread.'."""
83        self.complete_from_to('settings set target.process.', 'settings set target.process.thread.')
84
85    def test_settings_set_target_process_thread_dot(self):
86        """Test that 'settings set target.process.thread.' completes to ['Available completions:',
87        'target.process.thread.step-avoid-regexp', 'target.process.thread.trace-thread']."""
88        self.complete_from_to('settings set target.process.thread.',
89                              ['Available completions:',
90                               'target.process.thread.step-avoid-regexp',
91                               'target.process.thread.trace-thread'])
92
93    def complete_from_to(self, str_input, patterns):
94        """Test the completion mechanism completes str_input to pattern, where
95        patterns could be a pattern-string or a list of pattern-strings"""
96        prompt = "(lldb) "
97        add_prompt = "Enter your stop hook command(s).  Type 'DONE' to end.\r\n> "
98        add_prompt1 = "\r\n> "
99
100        # So that the child gets torn down after the test.
101        self.child = pexpect.spawn('%s %s' % (self.lldbHere, self.lldbOption))
102        child = self.child
103        # Turn on logging for input/output to/from the child.
104        with open('child_send.txt', 'w') as f_send:
105            with open('child_read.txt', 'w') as f_read:
106                child.logfile_send = f_send
107                child.logfile_read = f_read
108
109                child.expect_exact(prompt)
110                child.setecho(True)
111                # Sends str_input and a Tab to invoke the completion machinery.
112                child.send("%s\t" % str_input)
113                child.sendline('')
114                child.expect_exact(prompt)
115
116        # Set logfile to None to stop logging.
117        child.logfile_send = None
118        child.logfile_read = None
119
120        with open('child_send.txt', 'r') as fs:
121            if self.TraceOn():
122                print "\n\nContents of child_send.txt:"
123                print fs.read()
124        with open('child_read.txt', 'r') as fr:
125            from_child = fr.read()
126            if self.TraceOn():
127                print "\n\nContents of child_read.txt:"
128                print from_child
129
130            self.assertFalse(patterns is None)
131            if type(patterns) is not types.ListType:
132                patterns = [patterns]
133
134            # Test that str_input completes to our patterns.
135            # If each pattern matches from_child, the completion mechanism works!
136            for p in patterns:
137                self.expect(from_child, msg=COMPLETIOND_MSG(str_input, p), exe=False,
138                    patterns = [p])
139
140
141if __name__ == '__main__':
142    import atexit
143    lldb.SBDebugger.Initialize()
144    atexit.register(lambda: lldb.SBDebugger.Terminate())
145    unittest2.main()
146