test script for the cmd module original by michael schneider 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 parselinefunc func func mycmd parselinefunc arg1 func arg1 func arg1 test for the function onecmd mycmd onecmd mycmd onecmdadd 4 5 9 mycmd onecmd 9 mycmd onecmdtest unknown syntax test test for the function emptyline mycmd emptyline unknown syntax test test for the function default mycmd defaultdefault unknown syntax default test for the function completedefault mycmd completedefault this is the completedefault method mycmd completenamesa add test for the function completenames mycmd completenames12 mycmd completenameshelp help test for the function completehelp mycmd completehelpa add mycmd completehelphe help mycmd completehelp12 sortedmycmd completehelp add exit help life meaning shell test for the function dohelp mycmd dohelptestet no help on testet mycmd dohelpadd help text for add mycmd onecmdhelp add help text for add mycmd onecmdhelp meaning doctest normalizewhitespace try and be nice to people avoid eating fat read a good book every now and then get some walking in and try to live together in peace and harmony with people of all creeds and nations mycmd dohelp blankline documented commands type help topic add help blankline miscellaneous help topics life meaning blankline undocumented commands exit shell blankline test for the function printtopics mycmd printtopicsheader command1 command2 2 10 header command1 command2 blankline test for the function columnize mycmd columnizestri for i in range20 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 mycmd columnizestri for i in range20 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 dohelp functions mycmd userawinput0 mycmd cmdqueueadd add 4 5 help help add exit mycmd cmdloop doctest reportndiff hello from preloop invalid number of arguments 9 9 blankline documented commands type help topic add help blankline miscellaneous help topics life meaning blankline undocumented commands exit shell blankline help text for add hello from postloop gh80731 cmd cmd should print the correct exception in default mycmd cmdprintexceptionclass try raise valueerrortest except valueerror mycmd onecmdnot important class valueerror valueerror test 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 life meaning 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 onecmd help meaning doctest normalize_whitespace try and be nice to people avoid eating fat read a good book every now and then get some walking in and try to live together in peace and harmony with people of all creeds and nations mycmd do_help blankline documented commands type help topic add help blankline miscellaneous help topics life meaning blankline undocumented commands exit shell blankline test for the function print_topics mycmd print_topics header command1 command2 2 10 header command1 command2 blankline 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 doctest report_ndiff hello from preloop invalid number of arguments 9 9 blankline documented commands type help topic add help blankline miscellaneous help topics life meaning blankline undocumented commands exit shell blankline help text for add hello from postloop gh 80731 cmd cmd should print the correct exception in default mycmd cmdprintexceptionclass try raise valueerror test except valueerror mycmd onecmd not important class valueerror valueerror test
import cmd
import sys
import doctest
import unittest
import io
from test import support

class samplecmdclass(cmd.Cmd):
    

    def preloop(self):
        print("Hello from preloop")

    def postloop(self):
        print("Hello from postloop")

    def completedefault(self, *ignored):
        print("This is the completedefault method")

    def complete_command(self):
        print("complete command")

    def do_shell(self, s):
        pass

    def do_add(self, s):
        l = s.split()
        if len(l) != 2:
            print("*** invalid number of arguments")
            return
        try:
            l = [int(i) for i in l]
        except ValueError:
            print("*** arguments should be numbers")
            return
        print(l[0]+l[1])

    def help_add(self):
        print("help text for add")
        return

    def help_meaning(self):
        print("Try and be nice to people, avoid eating fat, read a "
              "good book every now and then, get some walking in, "
              "and try to live together in peace and harmony with "
              "people of all creeds and nations.")
        return

    def help_life(self):
        print("Always look on the bright side of life")
        return

    def do_exit(self, arg):
        return True


class TestAlternateInput(unittest.TestCase):

    class simplecmd(cmd.Cmd):

        def do_print(self, args):
            print(args, file=self.stdout)

        def do_EOF(self, args):
            return True


    class simplecmd2(simplecmd):

        def do_EOF(self, args):
            print('*** Unknown syntax: EOF', file=self.stdout)
            return True


    def test_file_with_missing_final_nl(self):
        input = io.StringIO("print test\nprint test2")
        output = io.StringIO()
        cmd = self.simplecmd(stdin=input, stdout=output)
        cmd.use_rawinput = False
        cmd.cmdloop()
        self.assertMultiLineEqual(output.getvalue(),
            ("(Cmd) test\n"
             "(Cmd) test2\n"
             "(Cmd) "))


    def test_input_reset_at_EOF(self):
        input = io.StringIO("print test\nprint test2")
        output = io.StringIO()
        cmd = self.simplecmd2(stdin=input, stdout=output)
        cmd.use_rawinput = False
        cmd.cmdloop()
        self.assertMultiLineEqual(output.getvalue(),
            ("(Cmd) test\n"
             "(Cmd) test2\n"
             "(Cmd) *** Unknown syntax: EOF\n"))
        input = io.StringIO("print \n\n")
        output = io.StringIO()
        cmd.stdin = input
        cmd.stdout = output
        cmd.cmdloop()
        self.assertMultiLineEqual(output.getvalue(),
            ("(Cmd) \n"
             "(Cmd) \n"
             "(Cmd) *** Unknown syntax: EOF\n"))


class CmdPrintExceptionClass(cmd.Cmd):
    

    def default(self, line):
        print(sys.exc_info()[:2])

def load_tests(loader, tests, pattern):
    tests.addTest(doctest.DocTestSuite())
    return tests


if __name__ == "__main__":
    if "-i" in sys.argv:
        samplecmdclass().cmdloop()
    else:
        unittest.main()