1# Copyright (C) 2010 Canonical Ltd
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17"""Matchers for breezy.
18
19Primarily test support, Matchers are used by self.assertThat in the breezy
20test suite. A matcher is a stateful test helper which can be used to determine
21if a passed object 'matches', much like a regex. If the object does not match
22the mismatch can be described in a human readable fashion. assertThat then
23raises if a mismatch occurs, showing the description as the assertion error.
24
25Matchers are designed to be more reusable and composable than layered
26assertions in Test Case objects, so they are recommended for new testing work.
27"""
28
29__all__ = [
30    'ContainsNoVfsCalls',
31    ]
32
33from breezy.bzr.smart.request import request_handlers as smart_request_handlers
34from breezy.bzr.smart import vfs
35
36from testtools.matchers import Mismatch, Matcher
37
38
39class _NoVfsCallsMismatch(Mismatch):
40    """Mismatch describing a list of HPSS calls which includes VFS requests."""
41
42    def __init__(self, vfs_calls):
43        self.vfs_calls = vfs_calls
44
45    def describe(self):
46        return "no VFS calls expected, got: %s" % ",".join([
47            "%s(%s)" % (c.method,
48                        ", ".join([repr(a) for a in c.args])) for c in self.vfs_calls])
49
50
51class ContainsNoVfsCalls(Matcher):
52    """Ensure that none of the specified calls are HPSS calls."""
53
54    def __str__(self):
55        return 'ContainsNoVfsCalls()'
56
57    @classmethod
58    def match(cls, hpss_calls):
59        vfs_calls = []
60        for call in hpss_calls:
61            try:
62                request_method = smart_request_handlers.get(call.call.method)
63            except KeyError:
64                # A method we don't know about doesn't count as a VFS method.
65                continue
66            if issubclass(request_method, vfs.VfsRequest):
67                vfs_calls.append(call.call)
68        if len(vfs_calls) == 0:
69            return None
70        return _NoVfsCallsMismatch(vfs_calls)
71