xref: /netbsd-src/external/gpl3/gdb/dist/gdb/testsuite/gdb.python/py-parameter.exp (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1# Copyright (C) 2010-2017 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.
17# It tests gdb.parameter and gdb.Parameter.
18
19load_lib gdb-python.exp
20
21# Start with a fresh gdb.
22gdb_exit
23gdb_start
24gdb_reinitialize_dir $srcdir/$subdir
25
26# Skip all tests if Python scripting is not enabled.
27if { [skip_python_tests] } { continue }
28
29# We use "." here instead of ":" so that this works on win32 too.
30if { [is_remote host] } {
31    # Don't match $srcdir/$subdir because proc gdb_reinitialize_dir
32    # doesn't set search directories on remote host.
33    set directories ".*\\\$cdir.\\\$cwd"
34} else {
35    set escaped_directory [string_to_regexp "$srcdir/$subdir"]
36    set directories "$escaped_directory.\\\$cdir.\\\$cwd"
37}
38gdb_test "python print (gdb.parameter ('directories'))" $directories
39
40# Test a simple boolean parameter.
41gdb_py_test_multiple "Simple gdb booleanparameter" \
42   "python" "" \
43   "class TestParam (gdb.Parameter):" "" \
44   "   \"\"\"When enabled, test param does something useful. When disabled, does nothing.\"\"\"" "" \
45   "   show_doc = \"Show the state of the boolean test-param\"" ""\
46   "   set_doc = \"Set the state of the boolean test-param\"" "" \
47   "   def get_show_string (self, pvalue):" ""\
48   "      return \"The state of the Test Parameter is \" + pvalue" ""\
49   "   def get_set_string (self):" ""\
50   "      val = \"on\"" ""\
51   "      if (self.value == False):" ""\
52   "         val = \"off\"" ""\
53   "      return \"Test Parameter has been set to \" + val" ""\
54   "   def __init__ (self, name):" "" \
55   "      super (TestParam, self).__init__ (name, gdb.COMMAND_DATA, gdb.PARAM_BOOLEAN)" "" \
56   "      self.value = True" "" \
57   "test_param = TestParam ('print test-param')" ""\
58   "end"
59
60gdb_test "python print (test_param.value)" "True" "test parameter value"
61gdb_test "show print test-param" "The state of the Test Parameter is on.*" "show parameter on"
62gdb_test "set print test-param off" "Test Parameter has been set to off" "turn off parameter"
63gdb_test "show print test-param" "The state of the Test Parameter is off.*" "show parameter off"
64gdb_test "python print (test_param.value)" "False" "test parameter value"
65gdb_test "help show print test-param" "Show the state of the boolean test-param.*" "test show help"
66gdb_test "help set print test-param" "Set the state of the boolean test-param.*" "test set help"
67gdb_test "help set print" "set print test-param -- Set the state of the boolean test-param.*" "test general help"
68
69
70# Test an enum parameter.
71gdb_py_test_multiple "enum gdb parameter" \
72   "python" "" \
73   "class TestEnumParam (gdb.Parameter):" "" \
74   "   \"\"\"When set, test param does something useful. When disabled, does nothing.\"\"\"" "" \
75   "   show_doc = \"Show the state of the enum\"" ""\
76   "   set_doc = \"Set the state of the enum\"" "" \
77   "   def get_show_string (self, pvalue):" ""\
78   "      return \"The state of the enum is \" + pvalue" ""\
79   "   def get_set_string (self):" ""\
80   "      return \"The state of the enum has been set to \" + self.value" ""\
81   "   def __init__ (self, name):" "" \
82   "      super (TestEnumParam, self).__init__ (name, gdb.COMMAND_DATA, gdb.PARAM_ENUM, \[\"one\", \"two\"\])" "" \
83   "      self.value = \"one\"" "" \
84   "test_enum_param = TestEnumParam ('print test-enum-param')" ""\
85   "end"
86
87gdb_test "python print (test_enum_param.value)" "one" "test enum parameter value"
88gdb_test "show print test-enum-param" "The state of the enum is one.*" "show parameter is initial value"
89gdb_test "set print test-enum-param two" "The state of the enum has been set to two" "set enum to two"
90gdb_test "show print test-enum-param" "The state of the enum is two.*" "show parameter is new value"
91gdb_test "python print (test_enum_param.value)" "two" "test enum parameter value"
92gdb_test "set print test-enum-param three" "Undefined item: \"three\".*" "set invalid enum parameter"
93
94# Test a file parameter.
95gdb_py_test_multiple "file gdb parameter" \
96   "python" "" \
97   "class TestFileParam (gdb.Parameter):" "" \
98   "   \"\"\"When set, test param does something useful. When disabled, does nothing.\"\"\"" "" \
99   "   show_doc = \"Show the name of the file\"" ""\
100   "   set_doc = \"Set the name of the file\"" "" \
101   "   def get_show_string (self, pvalue):" ""\
102   "      return \"The name of the file is \" + pvalue" ""\
103   "   def get_set_string (self):" ""\
104   "      return \"The name of the file has been changed to \" + self.value" ""\
105   "   def __init__ (self, name):" "" \
106   "      super (TestFileParam, self).__init__ (name, gdb.COMMAND_FILES, gdb.PARAM_FILENAME)" "" \
107   "      self.value = \"foo.txt\"" "" \
108   "test_file_param = TestFileParam ('test-file-param')" ""\
109   "end"
110
111gdb_test "python print (test_file_param.value)" "foo.txt" "test file parameter value"
112gdb_test "show test-file-param" "The name of the file is foo.txt.*" "show initial file value"
113gdb_test "set test-file-param bar.txt" "The name of the file has been changed to bar.txt" "set new file parameter" 1
114gdb_test "show test-file-param" "The name of the file is bar.txt.*" "show new file value"
115gdb_test "python print (test_file_param.value)" "bar.txt" "test new file parameter value"
116gdb_test "set test-file-param" "Argument required.*"
117
118# Test a parameter that is not documented.
119gdb_py_test_multiple "Simple gdb booleanparameter" \
120   "python" "" \
121   "class TestUndocParam (gdb.Parameter):" "" \
122   "   def get_show_string (self, pvalue):" ""\
123   "      return \"The state of the Test Parameter is \" + pvalue" ""\
124   "   def get_set_string (self):" ""\
125   "      val = \"on\"" ""\
126   "      if (self.value == False):" ""\
127   "         val = \"off\"" ""\
128   "      return \"Test Parameter has been set to \" + val" ""\
129   "   def __init__ (self, name):" "" \
130   "      super (TestUndocParam, self).__init__ (name, gdb.COMMAND_DATA, gdb.PARAM_BOOLEAN)" "" \
131   "      self.value = True" "" \
132   "test_undoc_param = TestUndocParam ('print test-undoc-param')" ""\
133   "end"
134
135gdb_test "show print test-undoc-param" "The state of the Test Parameter is on.*" "show parameter on"
136gdb_test "set print test-undoc-param off" "Test Parameter has been set to off" "turn off parameter"
137gdb_test "show print test-undoc-param" "The state of the Test Parameter is off.*" "show parameter off"
138gdb_test "python print (test_undoc_param.value)" "False" "test parameter value"
139gdb_test "help show print test-undoc-param" "This command is not documented.*" "test show help"
140gdb_test "help set print test-undoc-param" "This command is not documented.*" "test set help"
141gdb_test "help set print" "set print test-undoc-param -- This command is not documented.*" "test general help"
142
143# Test a parameter that is not documented in any way..
144gdb_py_test_multiple "Simple gdb booleanparameter" \
145   "python" "" \
146   "class TestNodocParam (gdb.Parameter):" "" \
147   "   def __init__ (self, name):" "" \
148   "      super (TestNodocParam, self).__init__ (name, gdb.COMMAND_DATA, gdb.PARAM_BOOLEAN)" "" \
149   "      self.value = True" "" \
150   "test_nodoc_param = TestNodocParam ('print test-nodoc-param')" ""\
151   "end"
152
153gdb_test "show print test-nodoc-param" "This command is not documented.*" "show parameter on"
154gdb_test "set print test-nodoc-param off" "This command is not documented.*" "turn off parameter"
155gdb_test "show print test-nodoc-param" "This command is not documented.*.*" "show parameter off"
156gdb_test "python print (test_nodoc_param.value)" "False" "test parameter value"
157gdb_test "help show print test-nodoc-param" "This command is not documented.*" "test show help"
158gdb_test "help set print test-nodoc-param" "This command is not documented.*" "test set help"
159gdb_test "help set print" "set print test-nodoc-param -- This command is not documented.*" "test general help"
160
161# Test deprecated API. Do not use in your own implementations.
162gdb_py_test_multiple "Simple gdb booleanparameter" \
163   "python" "" \
164   "class TestParam (gdb.Parameter):" "" \
165   "   \"\"\"When enabled, test param does something useful. When disabled, does nothing.\"\"\"" "" \
166   "   show_doc = \"State of the Test Parameter\"" ""\
167   "   set_doc = \"Set the state of the Test Parameter\"" "" \
168   "   def __init__ (self, name):" "" \
169   "      super (TestParam, self).__init__ (name, gdb.COMMAND_DATA, gdb.PARAM_BOOLEAN)" "" \
170   "      self.value = True" "" \
171   "test_param = TestParam ('print test-param')" ""\
172   "end"
173
174gdb_test "python print (test_param.value)" "True" "test parameter value"
175gdb_test "show print test-param" "State of the Test Parameter on.*" "show parameter on"
176gdb_test "set print test-param off" "Set the state of the Test Parameter.*" "turn off parameter"
177gdb_test "show print test-param" "State of the Test Parameter off.*" "show parameter off"
178gdb_test "python print (test_param.value)" "False" "test parameter value"
179gdb_test "help show print test-param" "State of the Test Parameter.*" "test show help"
180gdb_test "help set print test-param" "Set the state of the Test Parameter.*" "test set help"
181gdb_test "help set print" "set print test-param -- Set the state of the Test Parameter.*" "test general help"
182