File: test_command.py

package info (click to toggle)
thuban 1.2.2-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 7,872 kB
  • ctags: 5,853
  • sloc: python: 30,410; ansic: 6,181; xml: 4,234; cpp: 1,595; makefile: 141
file content (132 lines) | stat: -rw-r--r-- 3,812 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Copyright (c) 2002, 2003 by Intevation GmbH
# Authors:
# Bernhard Herzog <bh@intevation.de>
#
# This program is free software under the GPL (>=v2)
# Read the file COPYING coming with Thuban for details.

"""
Test the Command class
"""

__version__ = "$Revision: 537 $"
# $Source$
# $Id: test_command.py 537 2003-03-14 20:43:50Z bh $


import unittest

import support
support.initthuban()

from Thuban.UI.command import Command, ToolCommand

class MockContext:

    pass


class BaseCommandTest(unittest.TestCase):

    def setUp(self):
        self.command_args = None

    def command(self, *args):
        """Method to use as the command function.

        Bind all args except self to self.command_args as a tuple.
        """
        self.command_args = args

    def context(self):
        """Return a context object"""
        return MockContext()

class TestCommand(BaseCommandTest):

    def test_static_command(self):
        """Test Command object with no callbacks"""
        cmd = Command("do_something", "Do Something", self.command)
        self.assertEquals(cmd.Name(), "do_something")
        self.assertEquals(cmd.Title(), "Do Something")
        self.assertEquals(cmd.HelpText(), "")
        self.assertEquals(cmd.Icon(), "")
        self.failIf(cmd.IsDynamic())
        self.failIf(cmd.IsTool())

        context = self.context()
        self.assert_(cmd.Sensitive(context))
        self.failIf(cmd.Checked(context))

        # Execute the command with just the context
        cmd.Execute(context)
        self.assertEquals(self.command_args, (context,))

        # Execute the command with additional parameters
        cmd.Execute(context, "abc")
        self.assertEquals(self.command_args, (context, "abc"))

class TestDynamicCommand(BaseCommandTest):

    def setUp(self):
        BaseCommandTest.setUp(self)
        self.is_sensitive = 0
        self.is_checked = 0
        self.dynamic_text = ""

    def sensitive(self, context):
        return self.is_sensitive

    def checked(self, context):
        return self.is_checked

    def dyntext(self, context):
        return self.dynamic_text

    def context(self):
        """Return a context object"""
        return MockContext()

    def test_dynamic_sensitivity(self):
        """Test Command object with dynamic sensitivity"""
        cmd = Command("do_something", "Do Something", self.command,
                      sensitive = self.sensitive)
        self.assert_(cmd.IsDynamic())

        context = self.context()
        self.failIf(cmd.Sensitive(context))
        self.is_sensitive = 1
        self.assert_(cmd.Sensitive(context))

    def test_dynamic_checked(self):
        """Test Command object with dynamic checked flag"""
        cmd = Command("do_something", "Do Something", self.command,
                      checked = self.checked)
        self.assert_(cmd.IsDynamic())

        context = self.context()
        self.failIf(cmd.Checked(context))
        self.is_checked = 1
        self.assert_(cmd.Checked(context))

    def test_dynamic_title(self):
        """Test Command object with dynamic title"""
        cmd = Command("do_something", "Do Something", self.command,
                      dyntext = self.dyntext)
        self.assert_(cmd.IsDynamic())
        self.assert_(cmd.HasDynText())

        context = self.context()
        self.assertEquals(cmd.DynText(context), "")
        self.dynamic_text = "A Dynamic Title"
        self.assertEquals(cmd.DynText(context), "A Dynamic Title")

    def test_tool_command(self):
        """Test ToolCommand object"""
        cmd = ToolCommand("do_something", "Do Something", self.command,
                          checked = self.checked)
        self.assert_(cmd.IsDynamic())
        self.assert_(cmd.IsTool())

if __name__ == "__main__":
    unittest.main()