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 | |
| 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 | |
| 65 | def complete_from_to(self, str_before, patterns): |
| 66 | """Test the completion mechanism completes str_before to pattern, where |
| 67 | 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 | |
| 81 | # Test that str_before completes to str_after. |
| 82 | child.expect_exact(prompt) |
| 83 | child.setecho(True) |
| 84 | child.send("%s\t" % str_before) |
| 85 | #child.expect_exact("%s\t" % str_after) |
| 86 | child.sendline('') |
| 87 | child.expect_exact(prompt) |
| 88 | |
| 89 | with open('child_send.txt', 'r') as fs: |
| 90 | if self.TraceOn(): |
| 91 | print "\n\nContents of child_send.txt:" |
| 92 | print fs.read() |
| 93 | with open('child_read.txt', 'r') as fr: |
| 94 | from_child = fr.read() |
| 95 | if self.TraceOn(): |
| 96 | print "\n\nContents of child_read.txt:" |
| 97 | print from_child |
| 98 | |
| 99 | self.assertFalse(patterns is None) |
| 100 | if type(patterns) is not types.ListType: |
| 101 | patterns = [patterns] |
| 102 | |
| 103 | # If each pattern matches from_child, the completion mechanism works! |
| 104 | for p in patterns: |
| 105 | self.expect(from_child, msg=COMPLETIOND_MSG(str_before, p), exe=False, |
| 106 | patterns = [p]) |
| 107 | |
| 108 | child.logfile_send = None |
| 109 | child.logfile_read = None |
| 110 | |
| 111 | |
| 112 | if __name__ == '__main__': |
| 113 | import atexit |
| 114 | lldb.SBDebugger.Initialize() |
| 115 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 116 | unittest2.main() |