ยปCore Development>Code coverage>Lib/test/test_cmd.py

Python code coverage for Lib/test/test_cmd.py

#countcontent
1n/a"""
2n/aTest script for the 'cmd' module
3n/aOriginal by Michael Schneider
4n/a"""
5n/a
6n/a
7n/aimport cmd
8n/aimport sys
9n/aimport unittest
10n/aimport io
11n/afrom test import support
12n/a
13n/aclass samplecmdclass(cmd.Cmd):
14n/a """
15n/a Instance the sampleclass:
16n/a >>> mycmd = samplecmdclass()
17n/a
18n/a Test for the function parseline():
19n/a >>> mycmd.parseline("")
20n/a (None, None, '')
21n/a >>> mycmd.parseline("?")
22n/a ('help', '', 'help ')
23n/a >>> mycmd.parseline("?help")
24n/a ('help', 'help', 'help help')
25n/a >>> mycmd.parseline("!")
26n/a ('shell', '', 'shell ')
27n/a >>> mycmd.parseline("!command")
28n/a ('shell', 'command', 'shell command')
29n/a >>> mycmd.parseline("func")
30n/a ('func', '', 'func')
31n/a >>> mycmd.parseline("func arg1")
32n/a ('func', 'arg1', 'func arg1')
33n/a
34n/a
35n/a Test for the function onecmd():
36n/a >>> mycmd.onecmd("")
37n/a >>> mycmd.onecmd("add 4 5")
38n/a 9
39n/a >>> mycmd.onecmd("")
40n/a 9
41n/a >>> mycmd.onecmd("test")
42n/a *** Unknown syntax: test
43n/a
44n/a Test for the function emptyline():
45n/a >>> mycmd.emptyline()
46n/a *** Unknown syntax: test
47n/a
48n/a Test for the function default():
49n/a >>> mycmd.default("default")
50n/a *** Unknown syntax: default
51n/a
52n/a Test for the function completedefault():
53n/a >>> mycmd.completedefault()
54n/a This is the completedefault methode
55n/a >>> mycmd.completenames("a")
56n/a ['add']
57n/a
58n/a Test for the function completenames():
59n/a >>> mycmd.completenames("12")
60n/a []
61n/a >>> mycmd.completenames("help")
62n/a ['help']
63n/a
64n/a Test for the function complete_help():
65n/a >>> mycmd.complete_help("a")
66n/a ['add']
67n/a >>> mycmd.complete_help("he")
68n/a ['help']
69n/a >>> mycmd.complete_help("12")
70n/a []
71n/a >>> sorted(mycmd.complete_help(""))
72n/a ['add', 'exit', 'help', 'shell']
73n/a
74n/a Test for the function do_help():
75n/a >>> mycmd.do_help("testet")
76n/a *** No help on testet
77n/a >>> mycmd.do_help("add")
78n/a help text for add
79n/a >>> mycmd.onecmd("help add")
80n/a help text for add
81n/a >>> mycmd.do_help("")
82n/a <BLANKLINE>
83n/a Documented commands (type help <topic>):
84n/a ========================================
85n/a add help
86n/a <BLANKLINE>
87n/a Undocumented commands:
88n/a ======================
89n/a exit shell
90n/a <BLANKLINE>
91n/a
92n/a Test for the function print_topics():
93n/a >>> mycmd.print_topics("header", ["command1", "command2"], 2 ,10)
94n/a header
95n/a ======
96n/a command1
97n/a command2
98n/a <BLANKLINE>
99n/a
100n/a Test for the function columnize():
101n/a >>> mycmd.columnize([str(i) for i in range(20)])
102n/a 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
103n/a >>> mycmd.columnize([str(i) for i in range(20)], 10)
104n/a 0 7 14
105n/a 1 8 15
106n/a 2 9 16
107n/a 3 10 17
108n/a 4 11 18
109n/a 5 12 19
110n/a 6 13
111n/a
112n/a This is an interactive test, put some commands in the cmdqueue attribute
113n/a and let it execute
114n/a This test includes the preloop(), postloop(), default(), emptyline(),
115n/a parseline(), do_help() functions
116n/a >>> mycmd.use_rawinput=0
117n/a >>> mycmd.cmdqueue=["", "add", "add 4 5", "help", "help add","exit"]
118n/a >>> mycmd.cmdloop()
119n/a Hello from preloop
120n/a help text for add
121n/a *** invalid number of arguments
122n/a 9
123n/a <BLANKLINE>
124n/a Documented commands (type help <topic>):
125n/a ========================================
126n/a add help
127n/a <BLANKLINE>
128n/a Undocumented commands:
129n/a ======================
130n/a exit shell
131n/a <BLANKLINE>
132n/a help text for add
133n/a Hello from postloop
134n/a """
135n/a
136n/a def preloop(self):
137n/a print("Hello from preloop")
138n/a
139n/a def postloop(self):
140n/a print("Hello from postloop")
141n/a
142n/a def completedefault(self, *ignored):
143n/a print("This is the completedefault methode")
144n/a
145n/a def complete_command(self):
146n/a print("complete command")
147n/a
148n/a def do_shell(self, s):
149n/a pass
150n/a
151n/a def do_add(self, s):
152n/a l = s.split()
153n/a if len(l) != 2:
154n/a print("*** invalid number of arguments")
155n/a return
156n/a try:
157n/a l = [int(i) for i in l]
158n/a except ValueError:
159n/a print("*** arguments should be numbers")
160n/a return
161n/a print(l[0]+l[1])
162n/a
163n/a def help_add(self):
164n/a print("help text for add")
165n/a return
166n/a
167n/a def do_exit(self, arg):
168n/a return True
169n/a
170n/a
171n/aclass TestAlternateInput(unittest.TestCase):
172n/a
173n/a class simplecmd(cmd.Cmd):
174n/a
175n/a def do_print(self, args):
176n/a print(args, file=self.stdout)
177n/a
178n/a def do_EOF(self, args):
179n/a return True
180n/a
181n/a
182n/a class simplecmd2(simplecmd):
183n/a
184n/a def do_EOF(self, args):
185n/a print('*** Unknown syntax: EOF', file=self.stdout)
186n/a return True
187n/a
188n/a
189n/a def test_file_with_missing_final_nl(self):
190n/a input = io.StringIO("print test\nprint test2")
191n/a output = io.StringIO()
192n/a cmd = self.simplecmd(stdin=input, stdout=output)
193n/a cmd.use_rawinput = False
194n/a cmd.cmdloop()
195n/a self.assertMultiLineEqual(output.getvalue(),
196n/a ("(Cmd) test\n"
197n/a "(Cmd) test2\n"
198n/a "(Cmd) "))
199n/a
200n/a
201n/a def test_input_reset_at_EOF(self):
202n/a input = io.StringIO("print test\nprint test2")
203n/a output = io.StringIO()
204n/a cmd = self.simplecmd2(stdin=input, stdout=output)
205n/a cmd.use_rawinput = False
206n/a cmd.cmdloop()
207n/a self.assertMultiLineEqual(output.getvalue(),
208n/a ("(Cmd) test\n"
209n/a "(Cmd) test2\n"
210n/a "(Cmd) *** Unknown syntax: EOF\n"))
211n/a input = io.StringIO("print \n\n")
212n/a output = io.StringIO()
213n/a cmd.stdin = input
214n/a cmd.stdout = output
215n/a cmd.cmdloop()
216n/a self.assertMultiLineEqual(output.getvalue(),
217n/a ("(Cmd) \n"
218n/a "(Cmd) \n"
219n/a "(Cmd) *** Unknown syntax: EOF\n"))
220n/a
221n/a
222n/adef test_main(verbose=None):
223n/a from test import test_cmd
224n/a support.run_doctest(test_cmd, verbose)
225n/a support.run_unittest(TestAlternateInput)
226n/a
227n/adef test_coverage(coverdir):
228n/a trace = support.import_module('trace')
229n/a tracer=trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix,],
230n/a trace=0, count=1)
231n/a tracer.run('import importlib; importlib.reload(cmd); test_main()')
232n/a r=tracer.results()
233n/a print("Writing coverage results...")
234n/a r.write_results(show_missing=True, summary=True, coverdir=coverdir)
235n/a
236n/aif __name__ == "__main__":
237n/a if "-c" in sys.argv:
238n/a test_coverage('/tmp/cmd.cover')
239n/a elif "-i" in sys.argv:
240n/a samplecmdclass().cmdloop()
241n/a else:
242n/a test_main()