Show More
@@ -0,0 +1,71 b'' | |||||
|
1 | # Test that certain objects conform to well-defined interfaces. | |||
|
2 | ||||
|
3 | from __future__ import absolute_import, print_function | |||
|
4 | ||||
|
5 | from mercurial import ( | |||
|
6 | httppeer, | |||
|
7 | localrepo, | |||
|
8 | sshpeer, | |||
|
9 | ui as uimod, | |||
|
10 | ) | |||
|
11 | ||||
|
12 | def checkobject(o): | |||
|
13 | """Verify a constructed object conforms to interface rules. | |||
|
14 | ||||
|
15 | An object must have __abstractmethods__ defined. | |||
|
16 | ||||
|
17 | All "public" attributes of the object (attributes not prefixed with | |||
|
18 | an underscore) must be in __abstractmethods__ or appear on a base class | |||
|
19 | with __abstractmethods__. | |||
|
20 | """ | |||
|
21 | name = o.__class__.__name__ | |||
|
22 | ||||
|
23 | allowed = set() | |||
|
24 | for cls in o.__class__.__mro__: | |||
|
25 | if not getattr(cls, '__abstractmethods__', set()): | |||
|
26 | continue | |||
|
27 | ||||
|
28 | allowed |= cls.__abstractmethods__ | |||
|
29 | allowed |= {a for a in dir(cls) if not a.startswith('_')} | |||
|
30 | ||||
|
31 | if not allowed: | |||
|
32 | print('%s does not have abstract methods' % name) | |||
|
33 | return | |||
|
34 | ||||
|
35 | public = {a for a in dir(o) if not a.startswith('_')} | |||
|
36 | ||||
|
37 | for attr in sorted(public - allowed): | |||
|
38 | print('public attributes not in abstract interface: %s.%s' % ( | |||
|
39 | name, attr)) | |||
|
40 | ||||
|
41 | # Facilitates testing localpeer. | |||
|
42 | class dummyrepo(object): | |||
|
43 | def __init__(self): | |||
|
44 | self.ui = uimod.ui() | |||
|
45 | def filtered(self, name): | |||
|
46 | pass | |||
|
47 | def _restrictcapabilities(self, caps): | |||
|
48 | pass | |||
|
49 | ||||
|
50 | # Facilitates testing sshpeer without requiring an SSH server. | |||
|
51 | class testingsshpeer(sshpeer.sshpeer): | |||
|
52 | def _validaterepo(self, *args, **kwargs): | |||
|
53 | pass | |||
|
54 | ||||
|
55 | class badpeer(httppeer.httppeer): | |||
|
56 | def __init__(self): | |||
|
57 | super(badpeer, self).__init__(uimod.ui(), 'http://localhost') | |||
|
58 | self.badattribute = True | |||
|
59 | ||||
|
60 | def badmethod(self): | |||
|
61 | pass | |||
|
62 | ||||
|
63 | def main(): | |||
|
64 | ui = uimod.ui() | |||
|
65 | ||||
|
66 | checkobject(badpeer()) | |||
|
67 | checkobject(httppeer.httppeer(ui, 'http://localhost')) | |||
|
68 | checkobject(localrepo.localpeer(dummyrepo())) | |||
|
69 | checkobject(testingsshpeer(ui, 'ssh://localhost/foo')) | |||
|
70 | ||||
|
71 | main() |
General Comments 0
You need to be logged in to leave comments.
Login now