Johnny Chen | fbcad68 | 2012-01-20 23:02:51 +0000 | [diff] [blame] | 1 | """ |
| 2 | Test the lldb command line completion mechanism. |
| 3 | """ |
| 4 | |
| 5 | import os |
| 6 | import unittest2 |
| 7 | import lldb |
| 8 | import pexpect |
| 9 | from lldbtest import * |
| 10 | |
| 11 | class 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 Chen | 2b05e29 | 2012-02-14 22:00:40 +0000 | [diff] [blame^] | 21 | def test_watchpoint_set_variable_dash_w(self): |
| 22 | """Test that 'watchpoint set variable -w' completes to 'watchpoint set variable -w '.""" |
| 23 | self.complete_from_to('watchpoint set variable -w', 'watchpoint set variable -w ') |
Johnny Chen | 42b7ff9 | 2012-01-25 01:37:36 +0000 | [diff] [blame] | 24 | |
Johnny Chen | 2b05e29 | 2012-02-14 22:00:40 +0000 | [diff] [blame^] | 25 | def test_watchpoint_set_variable_dash_w_space(self): |
| 26 | """Test that 'watchpoint set variable -w ' completes to ['Available completions:', 'read', 'write', 'read_write'].""" |
| 27 | self.complete_from_to('watchpoint set variable -w ', ['Available completions:', 'read', 'write', 'read_write']) |
Johnny Chen | 42b7ff9 | 2012-01-25 01:37:36 +0000 | [diff] [blame] | 28 | |
Johnny Chen | 42404d2 | 2012-02-08 22:37:48 +0000 | [diff] [blame] | 29 | 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 Chen | 28f3771 | 2012-01-31 01:26:28 +0000 | [diff] [blame] | 32 | |
Johnny Chen | 42404d2 | 2012-02-08 22:37:48 +0000 | [diff] [blame] | 33 | 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 Chen | 28f3771 | 2012-01-31 01:26:28 +0000 | [diff] [blame] | 40 | |
Johnny Chen | 42b7ff9 | 2012-01-25 01:37:36 +0000 | [diff] [blame] | 41 | 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 Chen | 28f3771 | 2012-01-31 01:26:28 +0000 | [diff] [blame] | 45 | 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 Chen | f872775 | 2012-01-23 19:49:28 +0000 | [diff] [blame] | 49 | 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 Chen | c8bba45 | 2012-01-21 01:45:18 +0000 | [diff] [blame] | 61 | 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 Chen | fbcad68 | 2012-01-20 23:02:51 +0000 | [diff] [blame] | 65 | 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 | |
Johnny Chen | e7e2099 | 2012-02-13 23:25:06 +0000 | [diff] [blame] | 85 | def test_settings_set_target_exec(self): |
| 86 | """Test that 'settings set target.exec' completes to 'settings set target.exec-search-paths '.""" |
| 87 | self.complete_from_to('settings set target.exec', 'settings set target.exec-search-paths ') |
| 88 | |
Johnny Chen | fbcad68 | 2012-01-20 23:02:51 +0000 | [diff] [blame] | 89 | def test_settings_set_target_pr(self): |
| 90 | """Test that 'settings set target.pr' completes to ['Available completions:', |
| 91 | 'target.prefer-dynamic-value', 'target.process.'].""" |
| 92 | self.complete_from_to('settings set target.pr', |
| 93 | ['Available completions:', |
| 94 | 'target.prefer-dynamic-value', |
| 95 | 'target.process.']) |
| 96 | |
| 97 | def test_settings_set_target_process(self): |
| 98 | """Test that 'settings set target.process' completes to 'settings set target.process.'.""" |
| 99 | self.complete_from_to('settings set target.process', 'settings set target.process.') |
| 100 | |
| 101 | def test_settings_set_target_process_dot(self): |
| 102 | """Test that 'settings set target.process.' completes to 'settings set target.process.thread.'.""" |
| 103 | self.complete_from_to('settings set target.process.', 'settings set target.process.thread.') |
| 104 | |
| 105 | def test_settings_set_target_process_thread_dot(self): |
| 106 | """Test that 'settings set target.process.thread.' completes to ['Available completions:', |
| 107 | 'target.process.thread.step-avoid-regexp', 'target.process.thread.trace-thread'].""" |
| 108 | self.complete_from_to('settings set target.process.thread.', |
| 109 | ['Available completions:', |
| 110 | 'target.process.thread.step-avoid-regexp', |
| 111 | 'target.process.thread.trace-thread']) |
| 112 | |
Johnny Chen | 4ab0c03 | 2012-02-03 01:14:42 +0000 | [diff] [blame] | 113 | def test_target_space(self): |
| 114 | """Test that 'target ' completes to ['Available completions:', 'create', 'delete', 'list', |
| 115 | 'modules', 'select', 'stop-hook', 'variable'].""" |
| 116 | self.complete_from_to('target ', |
| 117 | ['Available completions:', 'create', 'delete', 'list', |
| 118 | 'modules', 'select', 'stop-hook', 'variable']) |
| 119 | |
| 120 | def test_target_va(self): |
| 121 | """Test that 'target va' completes to 'target variable '.""" |
| 122 | self.complete_from_to('target va', 'target variable ') |
| 123 | |
Johnny Chen | 5b6e565 | 2012-01-20 23:34:35 +0000 | [diff] [blame] | 124 | def complete_from_to(self, str_input, patterns): |
Johnny Chen | 39fb5c2 | 2012-01-25 20:25:38 +0000 | [diff] [blame] | 125 | """Test that the completion mechanism completes str_input to patterns, |
| 126 | where patterns could be a pattern-string or a list of pattern-strings""" |
Johnny Chen | 758442a | 2012-01-25 20:50:21 +0000 | [diff] [blame] | 127 | # Patterns should not be None in order to proceed. |
| 128 | self.assertFalse(patterns is None) |
| 129 | # And should be either a string or list of strings. Check for list type |
| 130 | # below, if not, make a list out of the singleton string. If patterns |
| 131 | # is not a string or not a list of strings, there'll be runtime errors |
| 132 | # later on. |
| 133 | if not isinstance(patterns, list): |
| 134 | patterns = [patterns] |
| 135 | |
Johnny Chen | 39fb5c2 | 2012-01-25 20:25:38 +0000 | [diff] [blame] | 136 | # The default lldb prompt. |
Johnny Chen | fbcad68 | 2012-01-20 23:02:51 +0000 | [diff] [blame] | 137 | prompt = "(lldb) " |
Johnny Chen | fbcad68 | 2012-01-20 23:02:51 +0000 | [diff] [blame] | 138 | |
| 139 | # So that the child gets torn down after the test. |
| 140 | self.child = pexpect.spawn('%s %s' % (self.lldbHere, self.lldbOption)) |
| 141 | child = self.child |
| 142 | # Turn on logging for input/output to/from the child. |
| 143 | with open('child_send.txt', 'w') as f_send: |
| 144 | with open('child_read.txt', 'w') as f_read: |
| 145 | child.logfile_send = f_send |
| 146 | child.logfile_read = f_read |
| 147 | |
Johnny Chen | fbcad68 | 2012-01-20 23:02:51 +0000 | [diff] [blame] | 148 | child.expect_exact(prompt) |
| 149 | child.setecho(True) |
Johnny Chen | 5b6e565 | 2012-01-20 23:34:35 +0000 | [diff] [blame] | 150 | # Sends str_input and a Tab to invoke the completion machinery. |
| 151 | child.send("%s\t" % str_input) |
Johnny Chen | fbcad68 | 2012-01-20 23:02:51 +0000 | [diff] [blame] | 152 | child.sendline('') |
| 153 | child.expect_exact(prompt) |
| 154 | |
Johnny Chen | 61d44fa | 2012-01-25 20:29:26 +0000 | [diff] [blame] | 155 | # Now that the necessary logging is done, restore logfile to None to |
| 156 | # stop further logging. |
Johnny Chen | 47397c7 | 2012-01-21 00:08:37 +0000 | [diff] [blame] | 157 | child.logfile_send = None |
| 158 | child.logfile_read = None |
| 159 | |
Johnny Chen | fbcad68 | 2012-01-20 23:02:51 +0000 | [diff] [blame] | 160 | with open('child_send.txt', 'r') as fs: |
| 161 | if self.TraceOn(): |
| 162 | print "\n\nContents of child_send.txt:" |
| 163 | print fs.read() |
| 164 | with open('child_read.txt', 'r') as fr: |
| 165 | from_child = fr.read() |
| 166 | if self.TraceOn(): |
| 167 | print "\n\nContents of child_read.txt:" |
| 168 | print from_child |
| 169 | |
Johnny Chen | 5b6e565 | 2012-01-20 23:34:35 +0000 | [diff] [blame] | 170 | # Test that str_input completes to our patterns. |
Johnny Chen | fbcad68 | 2012-01-20 23:02:51 +0000 | [diff] [blame] | 171 | # If each pattern matches from_child, the completion mechanism works! |
| 172 | for p in patterns: |
Johnny Chen | 5b6e565 | 2012-01-20 23:34:35 +0000 | [diff] [blame] | 173 | self.expect(from_child, msg=COMPLETIOND_MSG(str_input, p), exe=False, |
Johnny Chen | fbcad68 | 2012-01-20 23:02:51 +0000 | [diff] [blame] | 174 | patterns = [p]) |
| 175 | |
Johnny Chen | fbcad68 | 2012-01-20 23:02:51 +0000 | [diff] [blame] | 176 | |
| 177 | if __name__ == '__main__': |
| 178 | import atexit |
| 179 | lldb.SBDebugger.Initialize() |
| 180 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 181 | unittest2.main() |