a zei@sdZddlZddlZddlZddlZddlmZGdddejZGdddej Z ddd Z d d Z e d krd ejvre dndejvrene dS)z@ Test script for the 'cmd' module Original by Michael Schneider N)supportc@sPeZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ dS)samplecmdclassa- Instance the sampleclass: >>> mycmd = samplecmdclass() Test for the function parseline(): >>> mycmd.parseline("") (None, None, '') >>> mycmd.parseline("?") ('help', '', 'help ') >>> mycmd.parseline("?help") ('help', 'help', 'help help') >>> mycmd.parseline("!") ('shell', '', 'shell ') >>> mycmd.parseline("!command") ('shell', 'command', 'shell command') >>> mycmd.parseline("func") ('func', '', 'func') >>> mycmd.parseline("func arg1") ('func', 'arg1', 'func arg1') Test for the function onecmd(): >>> mycmd.onecmd("") >>> mycmd.onecmd("add 4 5") 9 >>> mycmd.onecmd("") 9 >>> mycmd.onecmd("test") *** Unknown syntax: test Test for the function emptyline(): >>> mycmd.emptyline() *** Unknown syntax: test Test for the function default(): >>> mycmd.default("default") *** Unknown syntax: default Test for the function completedefault(): >>> mycmd.completedefault() This is the completedefault method >>> mycmd.completenames("a") ['add'] Test for the function completenames(): >>> mycmd.completenames("12") [] >>> mycmd.completenames("help") ['help'] Test for the function complete_help(): >>> mycmd.complete_help("a") ['add'] >>> mycmd.complete_help("he") ['help'] >>> mycmd.complete_help("12") [] >>> sorted(mycmd.complete_help("")) ['add', 'exit', 'help', 'shell'] Test for the function do_help(): >>> mycmd.do_help("testet") *** No help on testet >>> mycmd.do_help("add") help text for add >>> mycmd.onecmd("help add") help text for add >>> mycmd.do_help("") Documented commands (type help ): ======================================== add help Undocumented commands: ====================== exit shell Test for the function print_topics(): >>> mycmd.print_topics("header", ["command1", "command2"], 2 ,10) header ====== command1 command2 Test for the function columnize(): >>> mycmd.columnize([str(i) for i in range(20)]) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 >>> mycmd.columnize([str(i) for i in range(20)], 10) 0 7 14 1 8 15 2 9 16 3 10 17 4 11 18 5 12 19 6 13 This is an interactive test, put some commands in the cmdqueue attribute and let it execute This test includes the preloop(), postloop(), default(), emptyline(), parseline(), do_help() functions >>> mycmd.use_rawinput=0 >>> mycmd.cmdqueue=["", "add", "add 4 5", "help", "help add","exit"] >>> mycmd.cmdloop() Hello from preloop help text for add *** invalid number of arguments 9 Documented commands (type help ): ======================================== add help Undocumented commands: ====================== exit shell help text for add Hello from postloop cCs tddS)NzHello from preloopprintselfrA/opt/bitninja-python-dojo/embedded/lib/python3.9/test/test_cmd.pypreloopszsamplecmdclass.preloopcCs tddS)NzHello from postlooprrrrr postloopszsamplecmdclass.postloopcGs tddS)Nz"This is the completedefault methodr)rZignoredrrr completedefaultszsamplecmdclass.completedefaultcCs tddS)Nzcomplete commandrrrrr complete_commandszsamplecmdclass.complete_commandcCsdS)Nr)rsrrr do_shellszsamplecmdclass.do_shellcCsh|}t|dkr tddSzdd|D}WntyNtdYdS0t|d|ddS)Nz*** invalid number of argumentscSsg|] }t|qSr)int).0irrr z)samplecmdclass.do_add..z*** arguments should be numbersr)splitlenr ValueError)rrlrrr do_adds  zsamplecmdclass.do_addcCs tddS)Nzhelp text for addrrrrr help_addszsamplecmdclass.help_addcCsdSNTr)rargrrr do_exitszsamplecmdclass.do_exitN) __name__ __module__ __qualname____doc__r r r r rrrrrrrr r sz rc@s>eZdZGdddejZGdddeZddZddZd S) TestAlternateInputc@seZdZddZddZdS)zTestAlternateInput.simplecmdcCst||jddS)Nfilerstdoutrargsrrr do_printsz%TestAlternateInput.simplecmd.do_printcCsdSrrr)rrr do_EOFsz#TestAlternateInput.simplecmd.do_EOFN)r r!r"r+r,rrrr simplecmdsr-c@seZdZddZdS)zTestAlternateInput.simplecmd2cCstd|jddS)Nz*** Unknown syntax: EOFr%Tr'r)rrr r,sz$TestAlternateInput.simplecmd2.do_EOFN)r r!r"r,rrrr simplecmd2sr.cCsBtd}t}|j||d}d|_|||ddS)Nprint test print test2stdinr(Fz(Cmd) test (Cmd) test2 (Cmd) )ioStringIOr- use_rawinputcmdloopassertMultiLineEqualgetvaluerinputoutputcmdrrr test_file_with_missing_final_nls  z2TestAlternateInput.test_file_with_missing_final_nlcCsxtd}t}|j||d}d|_|||dtd}t}||_||_|||ddS)Nr/r0Fz5(Cmd) test (Cmd) test2 (Cmd) *** Unknown syntax: EOF zprint z,(Cmd) (Cmd) (Cmd) *** Unknown syntax: EOF ) r2r3r.r4r5r6r7r1r(r8rrr test_input_reset_at_EOFs     z*TestAlternateInput.test_input_reset_at_EOFN) r r!r"r;Cmdr-r.r<r=rrrr r$s  r$cCs&ddlm}t||ttdS)Nr)test_cmd)testr?rZ run_doctestZ run_unittestr$)verboser?rrr test_mains  rBcCsPtd}|jtjtjgddd}|d|}td|j dd|ddS) Ntracerr)Z ignoredirsrCcountz4import importlib; importlib.reload(cmd); test_main()zWriting coverage results...T)Z show_missingZsummarycoverdir) r import_moduleZTracesys base_prefixbase_exec_prefixrunresultsrZ write_results)rErCZtracerrrrr test_coverages  rM__main__z-cz/tmp/cmd.coverz-i)N)r#r;rGZunittestr2r@rr>rZTestCaser$rBrMr argvr5rrrr s  3