/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz
622.1.2 by John Arbash Meinel
Add tests of RevisionView that it can handle broken file-info properties.
1
# Copyright (C) 2007, 2008 Jelmer Vernooij <jelmer@samba.org>
140 by Jelmer Vernooij
add framework for tests.
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
776.2.5 by Curtis Hovey
Clean up test module.
17
__all__ = [
18
    'load_tests',
19
    'MockMethod',
20
    'MockProperty',
21
    ]
22
776.1.36 by Curtis Hovey
Allow me to add tests without updating __init__.py
23
import os
24
632 by Vincent Ladeuil
Upgrade to the new loading tests API.
25
779.1.2 by Curtis Hovey
DRY.
26
def discover_test_names(module_or_name):
27
    if isinstance(module_or_name, basestring):
28
        match = module_or_name
29
    else:
30
        match = ''
779.1.1 by Curtis Hovey
Support selftest, check, and check -m
31
    file_names = os.listdir(os.path.dirname(__file__))
32
    test_names = set()
33
    for file_name in file_names:
34
        name, ext = os.path.splitext(file_name)
779.1.2 by Curtis Hovey
DRY.
35
        if name.startswith('test_') and ext == '.py' and match in name:
779.1.1 by Curtis Hovey
Support selftest, check, and check -m
36
            test_names.add("%s.%s" % (__name__, name))
37
    return test_names
38
39
632 by Vincent Ladeuil
Upgrade to the new loading tests API.
40
def load_tests(basic_tests, module, loader):
779.1.2 by Curtis Hovey
DRY.
41
    test_names = discover_test_names(module)
779.1.1 by Curtis Hovey
Support selftest, check, and check -m
42
    basic_tests.addTest(loader.loadTestsFromModuleNames(test_names))
632 by Vincent Ladeuil
Upgrade to the new loading tests API.
43
    return basic_tests
771.1.2 by Curtis Hovey
Moved MockMethod to a common module.
44
45
776.2.5 by Curtis Hovey
Clean up test module.
46
class MockMethod(object):
771.1.2 by Curtis Hovey
Moved MockMethod to a common module.
47
48
    @classmethod
776.3.11 by Curtis Hovey
Added test to verify that DivergedBranches is handled.
49
    def bind(klass, test_instance, obj, method_name,
50
             return_value=None, raise_error=None, raise_on=1):
771.1.2 by Curtis Hovey
Moved MockMethod to a common module.
51
        original_method = getattr(obj, method_name)
52
        test_instance.addCleanup(setattr, obj, method_name, original_method)
776.3.11 by Curtis Hovey
Added test to verify that DivergedBranches is handled.
53
        setattr(obj, method_name, klass(return_value, raise_error, raise_on))
771.1.2 by Curtis Hovey
Moved MockMethod to a common module.
54
776.3.11 by Curtis Hovey
Added test to verify that DivergedBranches is handled.
55
    def __init__(self, return_value=None, raise_error=None, raise_on=1):
771.1.2 by Curtis Hovey
Moved MockMethod to a common module.
56
        self.called = False
776.1.18 by Curtis Hovey
Added test for call to show_all because dialog.run() requires the that to be called first.
57
        self.call_count = 0
771.1.2 by Curtis Hovey
Moved MockMethod to a common module.
58
        self.args = None
59
        self.kwargs = None
776.1.8 by Curtis Hovey
Allow the test to provide a return value when mocking a method.
60
        self.return_value = return_value
776.3.11 by Curtis Hovey
Added test to verify that DivergedBranches is handled.
61
        self.raise_error = raise_error
62
        self.raise_on = raise_on
771.1.2 by Curtis Hovey
Moved MockMethod to a common module.
63
64
    def __call__(self, *args, **kwargs):
65
        self.called = True
776.1.18 by Curtis Hovey
Added test for call to show_all because dialog.run() requires the that to be called first.
66
        self.call_count += 1
771.1.2 by Curtis Hovey
Moved MockMethod to a common module.
67
        self.args = args
68
        self.kwargs = kwargs
776.3.11 by Curtis Hovey
Added test to verify that DivergedBranches is handled.
69
        if self.raise_error is not None and self.call_count == self.raise_on:
70
            raise self.raise_error
776.1.8 by Curtis Hovey
Allow the test to provide a return value when mocking a method.
71
        return self.return_value
776.1.10 by Curtis Hovey
Added MockProperty to test that a dialog property is called by get_password.
72
73
74
class MockProperty(MockMethod):
75
76
    @classmethod
77
    def bind(klass, test_instance, obj, method_name, return_value=None):
78
        original_method = getattr(obj, method_name)
79
        test_instance.addCleanup(setattr, obj, method_name, original_method)
80
        mock = klass(return_value)
81
        setattr(obj, method_name, property(mock.get_value, mock.set_value))
776.1.11 by Curtis Hovey
Verify the property was called.
82
        return mock
776.1.10 by Curtis Hovey
Added MockProperty to test that a dialog property is called by get_password.
83
84
    def get_value(self, other):
85
        self.called = True
86
        return self.return_value
87
88
    def set_value(self, other, value):
89
        self.called = True
90
        self.return_value = value