]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.python/py-cmd.exp
Update years in copyright notice for the GDB files.
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.python / py-cmd.exp
1 # Copyright (C) 2009-2013 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 # for defining new GDB commands in Python.
18
19 load_lib gdb-python.exp
20
21 # Start with a fresh gdb.
22
23 gdb_exit
24 gdb_start
25 gdb_reinitialize_dir $srcdir/$subdir
26
27 # Skip all tests if Python scripting is not enabled.
28 if { [skip_python_tests] } { continue }
29
30 # Test a simple command.
31
32 gdb_py_test_multiple "input simple command" \
33 "python" "" \
34 "class test_cmd (gdb.Command):" "" \
35 " def __init__ (self):" "" \
36 " super (test_cmd, self).__init__ (\"test_cmd\", gdb.COMMAND_OBSCURE)" "" \
37 " def invoke (self, arg, from_tty):" "" \
38 " print (\"test_cmd output, arg = %s\" % arg)" "" \
39 "test_cmd ()" "" \
40 "end" ""
41
42 gdb_test "test_cmd ugh" "test_cmd output, arg = ugh" "call simple command"
43
44 # Test a prefix command, and a subcommand within it.
45
46 gdb_py_test_multiple "input prefix command" \
47 "python" "" \
48 "class prefix_cmd (gdb.Command):" "" \
49 " def __init__ (self):" "" \
50 " super (prefix_cmd, self).__init__ (\"prefix_cmd\", gdb.COMMAND_OBSCURE, gdb.COMPLETE_NONE, True)" "" \
51 " def invoke (self, arg, from_tty):" "" \
52 " print (\"prefix_cmd output, arg = %s\" % arg)" "" \
53 "prefix_cmd ()" "" \
54 "end" ""
55
56 gdb_test "prefix_cmd ugh" "prefix_cmd output, arg = ugh" "call prefix command"
57
58 gdb_py_test_multiple "input subcommand" \
59 "python" "" \
60 "class subcmd (gdb.Command):" "" \
61 " def __init__ (self):" "" \
62 " super (subcmd, self).__init__ (\"prefix_cmd subcmd\", gdb.COMMAND_OBSCURE)" "" \
63 " def invoke (self, arg, from_tty):" "" \
64 " print (\"subcmd output, arg = %s\" % arg)" "" \
65 "subcmd ()" "" \
66 "end" ""
67
68 gdb_test "prefix_cmd subcmd ugh" "subcmd output, arg = ugh" "call subcmd"
69
70 # Test prefix command using keyword arguments.
71
72 gdb_py_test_multiple "input prefix command, keyword arguments" \
73 "python" "" \
74 "class prefix_cmd2 (gdb.Command):" "" \
75 " def __init__ (self):" "" \
76 " super (prefix_cmd2, self).__init__ (\"prefix_cmd2\", gdb.COMMAND_OBSCURE, prefix = True, completer_class = gdb.COMPLETE_FILENAME)" "" \
77 " def invoke (self, arg, from_tty):" "" \
78 " print (\"prefix_cmd2 output, arg = %s\" % arg)" "" \
79 "prefix_cmd2 ()" "" \
80 "end" ""
81
82 gdb_test "prefix_cmd2 argh" "prefix_cmd2 output, arg = argh" "call prefix command, keyword arguments"
83
84 gdb_py_test_multiple "input subcommand under prefix_cmd2" \
85 "python" "" \
86 "class subcmd (gdb.Command):" "" \
87 " def __init__ (self):" "" \
88 " super (subcmd, self).__init__ (\"prefix_cmd2 subcmd\", gdb.COMMAND_OBSCURE)" "" \
89 " def invoke (self, arg, from_tty):" "" \
90 " print (\"subcmd output, arg = %s\" % arg)" "" \
91 "subcmd ()" "" \
92 "end" ""
93
94 gdb_test "prefix_cmd2 subcmd ugh" "subcmd output, arg = ugh" "call subcmd under prefix_cmd2"
95
96 # Test a subcommand in an existing GDB prefix.
97
98 gdb_py_test_multiple "input new subcommand" \
99 "python" "" \
100 "class newsubcmd (gdb.Command):" "" \
101 " def __init__ (self):" "" \
102 " super (newsubcmd, self).__init__ (\"info newsubcmd\", gdb.COMMAND_OBSCURE)" "" \
103 " def invoke (self, arg, from_tty):" "" \
104 " print (\"newsubcmd output, arg = %s\" % arg)" "" \
105 "newsubcmd ()" "" \
106 "end" ""
107
108 gdb_test "info newsubcmd ugh" "newsubcmd output, arg = ugh" "call newsubcmd"
109
110 # Test a command that throws gdb.GdbError.
111
112 gdb_py_test_multiple "input command to throw error" \
113 "python" "" \
114 "class test_error_cmd (gdb.Command):" "" \
115 " def __init__ (self):" "" \
116 " super (test_error_cmd, self).__init__ (\"test_error_cmd\", gdb.COMMAND_OBSCURE)" "" \
117 " def invoke (self, arg, from_tty):" "" \
118 " raise gdb.GdbError ('you lose!')" "" \
119 "test_error_cmd ()" "" \
120 "end" ""
121
122 gdb_test "test_error_cmd ugh" "you lose!" "call error command"
123
124 # Test gdb.string_to_argv.
125
126 gdb_test "python print (gdb.string_to_argv (\"1 2 3\"))" \
127 {\['1', '2', '3'\]} \
128 "string_to_argv (\"1 2 3\")"
129
130 gdb_test "python print (gdb.string_to_argv (\"'1 2' 3\"))" \
131 {\['1 2', '3'\]} \
132 "string_to_argv (\"'1 2' 3\")"
133
134 gdb_test "python print (gdb.string_to_argv ('\"1 2\" 3'))" \
135 {\['1 2', '3'\]} \
136 "string_to_argv ('\"1 2\" 3')"
137
138 gdb_test "python print (gdb.string_to_argv ('1\\ 2 3'))" \
139 {\['1 2', '3'\]} \
140 "string_to_argv ('1\\ 2 3')"
141
142 # Test user-defined python commands.
143 gdb_py_test_multiple "input simple user-defined command" \
144 "python" "" \
145 "class test_help (gdb.Command):" "" \
146 " \"\"\"Docstring\"\"\"" "" \
147 " def __init__ (self):" "" \
148 " super (test_help, self).__init__ (\"test_help\", gdb.COMMAND_USER)" "" \
149 " def invoke (self, arg, from_tty):" "" \
150 " print (\"test_cmd output, arg = %s\" % arg)" "" \
151 "test_help ()" "" \
152 "end" ""
153
154 gdb_test "test_help ugh" "test_cmd output, arg = ugh" "call simple user-defined command"
155
156 # Make sure the command shows up in `help user-defined`.
157 gdb_test "help user-defined" "User-defined commands.\[\r\n\]+The commands in this class are those defined by the user.\[\r\n\]+Use the \"define\" command to define a command.\[\r\n\]+\[\r\n\]+List of commands:\[\r\n\]+\[\r\n\]+test_help -- Docstring\[\r\n\]+\[\r\n\]+Type \"help\" followed by command name for full documentation.\[\r\n\]+Type \"apropos word\" to search for commands related to \"word\".\[\r\n\]+Command name abbreviations are allowed if unambiguous.\[\r\n\]+" "see user-defined command in `help user-defined`"