~pwlars/lava-test/test-id-after-run

« back to all changes in this revision

Viewing changes to tests/test_abrekcmd.py

  • Committer: Paul Larson
  • Date: 2010-10-12 17:48:03 UTC
  • mfrom: (44.1.9 help-command-subcommand)
  • Revision ID: paul.larson@canonical.com-20101012174803-tbv1uadywcyukxbc
Add a new class for commands with subcommands to deal with those better,
and fix help so that it works better when dealing with subcommands from
Michael Hudson.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
import unittest
17
17
from optparse import make_option
18
 
from abrek.command import AbrekCmd, get_command, get_all_cmds
 
18
from abrek.command import (
 
19
    AbrekCmd,
 
20
    AbrekCmdWithSubcommands,
 
21
    get_command,
 
22
    get_all_cmds,
 
23
    )
19
24
 
20
25
 
21
26
class testAbrekCmd(unittest.TestCase):
72
77
        self.assertTrue(expected_str in cmd.help())
73
78
 
74
79
    def test_subcmds(self):
75
 
        expected_str = 'Sub-Commands:\n  foo'
76
 
        class subcmd_test(AbrekCmd):
77
 
            pass
 
80
        expected_str = 'Available sub-commands:\n  foo'
78
81
 
79
 
        class cmd_test_subcmds(AbrekCmd):
80
 
            subcmds = {'foo':subcmd_test()}
81
 
            pass
 
82
        class cmd_test_subcmds(AbrekCmdWithSubcommands):
 
83
            """Help for test-subcmds."""
 
84
            class cmd_foo(AbrekCmd):
 
85
                pass
82
86
        cmd = cmd_test_subcmds()
83
 
        self.assertTrue(expected_str in cmd.help())
 
87
        self.assertTrue(
 
88
            expected_str in cmd.help()
 
89
            and 'Help for test-subcmds.' in cmd.help())
84
90
 
85
91
    def test_subcmds_run(self):
86
92
        expected_str = "subcmd test str"
87
 
        class subcmd_test(AbrekCmd):
88
 
            def run(self):
89
 
                return expected_str
90
93
 
91
 
        class cmd_test_subcmds(AbrekCmd):
92
 
            subcmds = {'foo':subcmd_test()}
93
 
            pass
 
94
        class cmd_test_subcmds(AbrekCmdWithSubcommands):
 
95
            class cmd_foo(AbrekCmd):
 
96
                def run(self):
 
97
                    return expected_str
94
98
        cmd = cmd_test_subcmds()
95
99
        argv = ['foo']
96
100
        self.assertEqual(expected_str, cmd.main(argv))
97
101
 
 
102
    def test_subcmds_name(self):
 
103
        expected_str = "subcmd test str"
 
104
 
 
105
        class cmd_test_subcmds(AbrekCmdWithSubcommands):
 
106
            class cmd_foo(AbrekCmd):
 
107
                def run(self):
 
108
                    return expected_str
 
109
        cmd = cmd_test_subcmds().get_subcommand('foo')
 
110
        self.assertEqual('test-subcmds foo', cmd.name())
 
111
 
 
112
    def test_subcmds_help(self):
 
113
        expected_str = "subcmd test str"
 
114
 
 
115
        class cmd_test_subcmds(AbrekCmdWithSubcommands):
 
116
            class cmd_foo(AbrekCmd):
 
117
                """Help for foo."""
 
118
                def run(self):
 
119
                    return expected_str
 
120
        cmd = cmd_test_subcmds().get_subcommand('foo')
 
121
        self.assertTrue(
 
122
            'test-subcmds foo' in cmd.help()
 
123
            and 'Help for foo.' in cmd.help())
 
124
 
98
125
    def test_subcmd_strip_argv(self):
99
126
        """
100
127
        Make sure that the argv list is stripped after calling the subcmd
101
128
        """
102
 
        class subcmd_test(AbrekCmd):
103
 
            def main(self, argv):
104
 
                return len(argv)
105
129
 
106
 
        class cmd_test_subcmds(AbrekCmd):
107
 
            subcmds = {'foo':subcmd_test()}
108
 
            pass
 
130
        class cmd_test_subcmds(AbrekCmdWithSubcommands):
 
131
            class cmd_foo(AbrekCmd):
 
132
                def main(self, argv):
 
133
                    return len(argv)
109
134
        cmd = cmd_test_subcmds()
110
135
        argv = ['foo']
111
136
        self.assertEqual(0, cmd.main(argv))