Commit | Line | Data |
---|---|---|
d7b32ed3 PM |
1 | # Copyright (C) 2010 Free Software Foundation, Inc. |
2 | ||
3 | # This program is free software; you can redistribute it and/or modify | |
4 | # it under the terms of the GNU General Public License as published by | |
5 | # the Free Software Foundation; either version 3 of the License, or | |
6 | # (at your option) any later version. | |
7 | # | |
8 | # This program is distributed in the hope that it will be useful, | |
9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | # GNU General Public License for more details. | |
12 | # | |
13 | # You should have received a copy of the GNU General Public License | |
14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
15 | ||
16 | # This file is part of the GDB testsuite. It tests the mechanism | |
17 | # exposing convenience functions to Python. | |
18 | ||
19 | if $tracelevel then { | |
20 | strace $tracelevel | |
21 | } | |
22 | ||
23 | # Usage: gdb_py_test_multiple NAME INPUT RESULT {INPUT RESULT}... | |
24 | # Run a test named NAME, consisting of multiple lines of input. | |
25 | # After each input line INPUT, search for result line RESULT. | |
26 | # Succeed if all results are seen; fail otherwise. | |
27 | proc gdb_py_test_multiple {name args} { | |
28 | global gdb_prompt | |
29 | foreach {input result} $args { | |
30 | if {[gdb_test_multiple $input "$name - $input" { | |
31 | -re "\[\r\n\]*($result)\[\r\n\]+($gdb_prompt | *>)$" { | |
32 | pass "$name - $input" | |
33 | } | |
34 | }]} { | |
35 | return 1 | |
36 | } | |
37 | } | |
38 | return 0 | |
39 | } | |
40 | ||
41 | # Run a command in GDB, and report a failure if a Python exception is thrown. | |
42 | # If report_pass is true, report a pass if no exception is thrown. | |
43 | proc gdb_py_test_silent_cmd {cmd name report_pass} { | |
44 | global gdb_prompt | |
45 | ||
46 | gdb_test_multiple $cmd $name { | |
47 | -re "Traceback.*$gdb_prompt $" { fail $name } | |
48 | -re "$gdb_prompt $" { if $report_pass { pass $name } } | |
49 | } | |
50 | } | |
51 | ||
52 | # Start with a fresh gdb. | |
53 | gdb_exit | |
54 | gdb_start | |
55 | gdb_reinitialize_dir $srcdir/$subdir | |
56 | ||
57 | # Skip all tests if Python scripting is not enabled. | |
58 | if { [skip_python_tests] } { continue } | |
59 | ||
60 | # Test a simple boolean parameter. | |
61 | gdb_py_test_multiple "Simple gdb booleanparameter" \ | |
62 | "python" "" \ | |
63 | "class TestParam (gdb.Parameter):" "" \ | |
64 | " \"\"\"When enabled, test param does something useful. When disabled, does nothing.\"\"\"" "" \ | |
65 | " show_doc = \"Show whether the state of the Test Parameter does something useful\"" ""\ | |
66 | " set_doc = \"Set whether the state of the Test Parameter does something useful\"" "" \ | |
67 | " def __init__ (self, name):" "" \ | |
68 | " super (TestParam, self).__init__ (name, gdb.COMMAND_DATA, gdb.PARAM_BOOLEAN)" "" \ | |
69 | " self.value = True" "" \ | |
70 | "test_param = TestParam ('print test-param')" ""\ | |
71 | "end" | |
72 | ||
73 | gdb_test "python print test_param.value" "True" "Test parameter value" | |
74 | gdb_test "show print test-param" "Whether the state of the Test Parameter does something useful is on.*" "Show parameter on" | |
75 | gdb_py_test_silent_cmd "set print test-param off" "Turn off parameter" 1 | |
76 | gdb_test "show print test-param" "Whether the state of the Test Parameter does something useful is off.*" "Show parameter off" | |
77 | gdb_test "python print test_param.value" "False" "Test parameter value" | |
78 | gdb_test "help show print test-param" "Show whether the state of the Test Parameter does something useful.*" "Test show help" | |
79 | gdb_test "help set print test-param" "Set whether the state of the Test Parameter does something useful.*" "Test set help" | |
80 | gdb_test "help set print" "set print test-param -- Set whether the state of the Test Parameter.*" "Test general help" | |
81 | ||
82 | # Test an enum parameter. | |
83 | gdb_py_test_multiple "enum gdb parameter" \ | |
84 | "python" "" \ | |
85 | "class TestEnumParam (gdb.Parameter):" "" \ | |
86 | " \"\"\"When set, test param does something useful. When disabled, does nothing.\"\"\"" "" \ | |
87 | " show_doc = \"Show the state of the enum\"" ""\ | |
88 | " set_doc = \"Set the state of the enum\"" "" \ | |
89 | " def __init__ (self, name):" "" \ | |
90 | " super (TestEnumParam, self).__init__ (name, gdb.COMMAND_DATA, gdb.PARAM_ENUM, \[\"one\", \"two\"\])" "" \ | |
91 | " self.value = \"one\"" "" \ | |
92 | "test_enum_param = TestEnumParam ('print test-enum-param')" ""\ | |
93 | "end" | |
94 | ||
95 | gdb_test "python print test_enum_param.value" "one" "Test enum parameter value" | |
96 | gdb_test "show print test-enum-param" "The state of the enum is \"one\".*" "Show parameter is initial value" | |
97 | gdb_py_test_silent_cmd "set print test-enum-param two" "Set parameter to enum value" 1 | |
98 | gdb_test "show print test-enum-param" "The state of the enum is \"two\".*" "Show parameter is new value" | |
99 | gdb_test "python print test_enum_param.value" "two" "Test enum parameter value" | |
100 | gdb_test "set print test-enum-param three" "Undefined item: \"three\".*" "Set invalid enum parameter" | |
101 | ||
102 | # Test a file parameter. | |
103 | gdb_py_test_multiple "file gdb parameter" \ | |
104 | "python" "" \ | |
105 | "class TestFileParam (gdb.Parameter):" "" \ | |
106 | " \"\"\"When set, test param does something useful. When disabled, does nothing.\"\"\"" "" \ | |
107 | " show_doc = \"Show the name of the file\"" ""\ | |
108 | " set_doc = \"Set the name of the file\"" "" \ | |
109 | " def __init__ (self, name):" "" \ | |
110 | " super (TestFileParam, self).__init__ (name, gdb.COMMAND_FILES, gdb.PARAM_FILENAME)" "" \ | |
111 | " self.value = \"foo.txt\"" "" \ | |
112 | "test_file_param = TestFileParam ('test-file-param')" ""\ | |
113 | "end" | |
114 | ||
115 | gdb_test "python print test_file_param.value" "foo.txt" "Test file parameter value" | |
116 | gdb_test "show test-file-param" "The name of the file is \"foo.txt\".*" "Show initial file value" | |
117 | gdb_py_test_silent_cmd "set test-file-param bar.txt" "Set new file parameter" 1 | |
118 | gdb_test "show test-file-param" "The name of the file is \"bar.txt\".*" "Show new file value" | |
119 | gdb_test "python print test_file_param.value" "bar.txt" "Test new file parameter value" | |
120 | gdb_test "set test-file-param" "Argument required.*" | |
121 | ||
122 | # Test a file parameter. | |
123 | gdb_py_test_multiple "file gdb parameter" \ | |
124 | "python" "" \ | |
125 | "class TestFileParam (gdb.Parameter):" "" \ | |
126 | " \"\"\"When set, test param does something useful. When disabled, does nothing.\"\"\"" "" \ | |
127 | " show_doc = \"Show the name of the file\"" ""\ | |
128 | " set_doc = \"Set the name of the file\"" "" \ | |
129 | " def __init__ (self, name):" "" \ | |
130 | " super (TestFileParam, self).__init__ (name, gdb.COMMAND_FILES, gdb.PARAM_FILENAME)" "" \ | |
131 | " self.value = \"foo.txt\"" "" \ | |
132 | "test_file_param = TestFileParam ('test-file-param')" ""\ | |
133 | "end" | |
134 | ||
135 | gdb_test "python print test_file_param.value" "foo.txt" "Test parameter value" | |
136 | gdb_test "show test-file-param" "The name of the file is \"foo.txt\".*" "Show parameter on" | |
137 | gdb_py_test_silent_cmd "set test-file-param bar.txt" "Turn off parameter" 1 | |
138 | gdb_test "show test-file-param" "The name of the file is \"bar.txt\".*" "Show parameter on" | |
139 | gdb_test "python print test_file_param.value" "bar.txt" "Test parameter value" | |
140 | gdb_test "set test-file-param" "Argument required.*" |