author | Sverre Rabbelier <srabbelier@gmail.com> |
Sat, 21 Feb 2009 16:40:22 +0000 | |
changeset 1435 | e9a2b1e87b1a |
parent 1000 | 9af147fc1f1c |
permissions | -rwxr-xr-x |
1000
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1 |
#!/usr/bin/python2.4 |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
2 |
# |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
3 |
# Copyright 2008 Google Inc. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
4 |
# |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
6 |
# you may not use this file except in compliance with the License. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
7 |
# You may obtain a copy of the License at |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
8 |
# |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
10 |
# |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
11 |
# Unless required by applicable law or agreed to in writing, software |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
14 |
# See the License for the specific language governing permissions and |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
15 |
# limitations under the License. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
16 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
17 |
"""Mox, an object-mocking framework for Python. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
18 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
19 |
Mox works in the record-replay-verify paradigm. When you first create |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
20 |
a mock object, it is in record mode. You then programmatically set |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
21 |
the expected behavior of the mock object (what methods are to be |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
22 |
called on it, with what parameters, what they should return, and in |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
23 |
what order). |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
24 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
25 |
Once you have set up the expected mock behavior, you put it in replay |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
26 |
mode. Now the mock responds to method calls just as you told it to. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
27 |
If an unexpected method (or an expected method with unexpected |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
28 |
parameters) is called, then an exception will be raised. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
29 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
30 |
Once you are done interacting with the mock, you need to verify that |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
31 |
all the expected interactions occured. (Maybe your code exited |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
32 |
prematurely without calling some cleanup method!) The verify phase |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
33 |
ensures that every expected method was called; otherwise, an exception |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
34 |
will be raised. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
35 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
36 |
Suggested usage / workflow: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
37 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
38 |
# Create Mox factory |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
39 |
my_mox = Mox() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
40 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
41 |
# Create a mock data access object |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
42 |
mock_dao = my_mox.CreateMock(DAOClass) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
43 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
44 |
# Set up expected behavior |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
45 |
mock_dao.RetrievePersonWithIdentifier('1').AndReturn(person) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
46 |
mock_dao.DeletePerson(person) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
47 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
48 |
# Put mocks in replay mode |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
49 |
my_mox.ReplayAll() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
50 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
51 |
# Inject mock object and run test |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
52 |
controller.SetDao(mock_dao) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
53 |
controller.DeletePersonById('1') |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
54 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
55 |
# Verify all methods were called as expected |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
56 |
my_mox.VerifyAll() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
57 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
58 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
59 |
from collections import deque |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
60 |
import inspect |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
61 |
import re |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
62 |
import types |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
63 |
import unittest |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
64 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
65 |
import stubout |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
66 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
67 |
class Error(AssertionError): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
68 |
"""Base exception for this module.""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
69 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
70 |
pass |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
71 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
72 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
73 |
class ExpectedMethodCallsError(Error): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
74 |
"""Raised when Verify() is called before all expected methods have been called |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
75 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
76 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
77 |
def __init__(self, expected_methods): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
78 |
"""Init exception. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
79 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
80 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
81 |
# expected_methods: A sequence of MockMethod objects that should have been |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
82 |
# called. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
83 |
expected_methods: [MockMethod] |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
84 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
85 |
Raises: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
86 |
ValueError: if expected_methods contains no methods. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
87 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
88 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
89 |
if not expected_methods: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
90 |
raise ValueError("There must be at least one expected method") |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
91 |
Error.__init__(self) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
92 |
self._expected_methods = expected_methods |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
93 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
94 |
def __str__(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
95 |
calls = "\n".join(["%3d. %s" % (i, m) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
96 |
for i, m in enumerate(self._expected_methods)]) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
97 |
return "Verify: Expected methods never called:\n%s" % (calls,) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
98 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
99 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
100 |
class UnexpectedMethodCallError(Error): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
101 |
"""Raised when an unexpected method is called. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
102 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
103 |
This can occur if a method is called with incorrect parameters, or out of the |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
104 |
specified order. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
105 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
106 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
107 |
def __init__(self, unexpected_method, expected): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
108 |
"""Init exception. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
109 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
110 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
111 |
# unexpected_method: MockMethod that was called but was not at the head of |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
112 |
# the expected_method queue. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
113 |
# expected: MockMethod or UnorderedGroup the method should have |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
114 |
# been in. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
115 |
unexpected_method: MockMethod |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
116 |
expected: MockMethod or UnorderedGroup |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
117 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
118 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
119 |
Error.__init__(self) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
120 |
self._unexpected_method = unexpected_method |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
121 |
self._expected = expected |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
122 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
123 |
def __str__(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
124 |
return "Unexpected method call: %s. Expecting: %s" % \ |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
125 |
(self._unexpected_method, self._expected) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
126 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
127 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
128 |
class UnknownMethodCallError(Error): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
129 |
"""Raised if an unknown method is requested of the mock object.""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
130 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
131 |
def __init__(self, unknown_method_name): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
132 |
"""Init exception. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
133 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
134 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
135 |
# unknown_method_name: Method call that is not part of the mocked class's |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
136 |
# public interface. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
137 |
unknown_method_name: str |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
138 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
139 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
140 |
Error.__init__(self) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
141 |
self._unknown_method_name = unknown_method_name |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
142 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
143 |
def __str__(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
144 |
return "Method called is not a member of the object: %s" % \ |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
145 |
self._unknown_method_name |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
146 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
147 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
148 |
class Mox(object): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
149 |
"""Mox: a factory for creating mock objects.""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
150 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
151 |
# A list of types that should be stubbed out with MockObjects (as |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
152 |
# opposed to MockAnythings). |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
153 |
_USE_MOCK_OBJECT = [types.ClassType, types.InstanceType, types.ModuleType, |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
154 |
types.ObjectType, types.TypeType] |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
155 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
156 |
def __init__(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
157 |
"""Initialize a new Mox.""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
158 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
159 |
self._mock_objects = [] |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
160 |
self.stubs = stubout.StubOutForTesting() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
161 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
162 |
def CreateMock(self, class_to_mock): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
163 |
"""Create a new mock object. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
164 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
165 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
166 |
# class_to_mock: the class to be mocked |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
167 |
class_to_mock: class |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
168 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
169 |
Returns: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
170 |
MockObject that can be used as the class_to_mock would be. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
171 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
172 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
173 |
new_mock = MockObject(class_to_mock) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
174 |
self._mock_objects.append(new_mock) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
175 |
return new_mock |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
176 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
177 |
def CreateMockAnything(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
178 |
"""Create a mock that will accept any method calls. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
179 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
180 |
This does not enforce an interface. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
181 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
182 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
183 |
new_mock = MockAnything() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
184 |
self._mock_objects.append(new_mock) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
185 |
return new_mock |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
186 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
187 |
def ReplayAll(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
188 |
"""Set all mock objects to replay mode.""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
189 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
190 |
for mock_obj in self._mock_objects: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
191 |
mock_obj._Replay() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
192 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
193 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
194 |
def VerifyAll(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
195 |
"""Call verify on all mock objects created.""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
196 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
197 |
for mock_obj in self._mock_objects: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
198 |
mock_obj._Verify() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
199 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
200 |
def ResetAll(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
201 |
"""Call reset on all mock objects. This does not unset stubs.""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
202 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
203 |
for mock_obj in self._mock_objects: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
204 |
mock_obj._Reset() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
205 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
206 |
def StubOutWithMock(self, obj, attr_name, use_mock_anything=False): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
207 |
"""Replace a method, attribute, etc. with a Mock. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
208 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
209 |
This will replace a class or module with a MockObject, and everything else |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
210 |
(method, function, etc) with a MockAnything. This can be overridden to |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
211 |
always use a MockAnything by setting use_mock_anything to True. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
212 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
213 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
214 |
obj: A Python object (class, module, instance, callable). |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
215 |
attr_name: str. The name of the attribute to replace with a mock. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
216 |
use_mock_anything: bool. True if a MockAnything should be used regardless |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
217 |
of the type of attribute. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
218 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
219 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
220 |
attr_to_replace = getattr(obj, attr_name) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
221 |
if type(attr_to_replace) in self._USE_MOCK_OBJECT and not use_mock_anything: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
222 |
stub = self.CreateMock(attr_to_replace) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
223 |
else: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
224 |
stub = self.CreateMockAnything() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
225 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
226 |
self.stubs.Set(obj, attr_name, stub) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
227 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
228 |
def UnsetStubs(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
229 |
"""Restore stubs to their original state.""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
230 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
231 |
self.stubs.UnsetAll() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
232 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
233 |
def Replay(*args): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
234 |
"""Put mocks into Replay mode. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
235 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
236 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
237 |
# args is any number of mocks to put into replay mode. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
238 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
239 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
240 |
for mock in args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
241 |
mock._Replay() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
242 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
243 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
244 |
def Verify(*args): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
245 |
"""Verify mocks. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
246 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
247 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
248 |
# args is any number of mocks to be verified. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
249 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
250 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
251 |
for mock in args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
252 |
mock._Verify() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
253 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
254 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
255 |
def Reset(*args): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
256 |
"""Reset mocks. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
257 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
258 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
259 |
# args is any number of mocks to be reset. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
260 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
261 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
262 |
for mock in args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
263 |
mock._Reset() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
264 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
265 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
266 |
class MockAnything: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
267 |
"""A mock that can be used to mock anything. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
268 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
269 |
This is helpful for mocking classes that do not provide a public interface. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
270 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
271 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
272 |
def __init__(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
273 |
""" """ |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
274 |
self._Reset() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
275 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
276 |
def __getattr__(self, method_name): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
277 |
"""Intercept method calls on this object. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
278 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
279 |
A new MockMethod is returned that is aware of the MockAnything's |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
280 |
state (record or replay). The call will be recorded or replayed |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
281 |
by the MockMethod's __call__. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
282 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
283 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
284 |
# method name: the name of the method being called. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
285 |
method_name: str |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
286 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
287 |
Returns: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
288 |
A new MockMethod aware of MockAnything's state (record or replay). |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
289 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
290 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
291 |
return self._CreateMockMethod(method_name) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
292 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
293 |
def _CreateMockMethod(self, method_name, method_to_mock=None): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
294 |
"""Create a new mock method call and return it. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
295 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
296 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
297 |
# method_name: the name of the method being called. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
298 |
# method_to_mock: The actual method being mocked, used for introspection. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
299 |
method_name: str |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
300 |
method_to_mock: a method object |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
301 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
302 |
Returns: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
303 |
A new MockMethod aware of MockAnything's state (record or replay). |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
304 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
305 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
306 |
return MockMethod(method_name, self._expected_calls_queue, |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
307 |
self._replay_mode, method_to_mock=method_to_mock) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
308 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
309 |
def __nonzero__(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
310 |
"""Return 1 for nonzero so the mock can be used as a conditional.""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
311 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
312 |
return 1 |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
313 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
314 |
def __eq__(self, rhs): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
315 |
"""Provide custom logic to compare objects.""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
316 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
317 |
return (isinstance(rhs, MockAnything) and |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
318 |
self._replay_mode == rhs._replay_mode and |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
319 |
self._expected_calls_queue == rhs._expected_calls_queue) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
320 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
321 |
def __ne__(self, rhs): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
322 |
"""Provide custom logic to compare objects.""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
323 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
324 |
return not self == rhs |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
325 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
326 |
def _Replay(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
327 |
"""Start replaying expected method calls.""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
328 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
329 |
self._replay_mode = True |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
330 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
331 |
def _Verify(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
332 |
"""Verify that all of the expected calls have been made. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
333 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
334 |
Raises: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
335 |
ExpectedMethodCallsError: if there are still more method calls in the |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
336 |
expected queue. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
337 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
338 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
339 |
# If the list of expected calls is not empty, raise an exception |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
340 |
if self._expected_calls_queue: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
341 |
# The last MultipleTimesGroup is not popped from the queue. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
342 |
if (len(self._expected_calls_queue) == 1 and |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
343 |
isinstance(self._expected_calls_queue[0], MultipleTimesGroup) and |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
344 |
self._expected_calls_queue[0].IsSatisfied()): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
345 |
pass |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
346 |
else: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
347 |
raise ExpectedMethodCallsError(self._expected_calls_queue) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
348 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
349 |
def _Reset(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
350 |
"""Reset the state of this mock to record mode with an empty queue.""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
351 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
352 |
# Maintain a list of method calls we are expecting |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
353 |
self._expected_calls_queue = deque() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
354 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
355 |
# Make sure we are in setup mode, not replay mode |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
356 |
self._replay_mode = False |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
357 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
358 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
359 |
class MockObject(MockAnything, object): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
360 |
"""A mock object that simulates the public/protected interface of a class.""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
361 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
362 |
def __init__(self, class_to_mock): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
363 |
"""Initialize a mock object. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
364 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
365 |
This determines the methods and properties of the class and stores them. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
366 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
367 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
368 |
# class_to_mock: class to be mocked |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
369 |
class_to_mock: class |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
370 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
371 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
372 |
# This is used to hack around the mixin/inheritance of MockAnything, which |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
373 |
# is not a proper object (it can be anything. :-) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
374 |
MockAnything.__dict__['__init__'](self) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
375 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
376 |
# Get a list of all the public and special methods we should mock. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
377 |
self._known_methods = set() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
378 |
self._known_vars = set() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
379 |
self._class_to_mock = class_to_mock |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
380 |
for method in dir(class_to_mock): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
381 |
if callable(getattr(class_to_mock, method)): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
382 |
self._known_methods.add(method) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
383 |
else: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
384 |
self._known_vars.add(method) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
385 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
386 |
def __getattr__(self, name): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
387 |
"""Intercept attribute request on this object. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
388 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
389 |
If the attribute is a public class variable, it will be returned and not |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
390 |
recorded as a call. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
391 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
392 |
If the attribute is not a variable, it is handled like a method |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
393 |
call. The method name is checked against the set of mockable |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
394 |
methods, and a new MockMethod is returned that is aware of the |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
395 |
MockObject's state (record or replay). The call will be recorded |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
396 |
or replayed by the MockMethod's __call__. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
397 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
398 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
399 |
# name: the name of the attribute being requested. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
400 |
name: str |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
401 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
402 |
Returns: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
403 |
Either a class variable or a new MockMethod that is aware of the state |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
404 |
of the mock (record or replay). |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
405 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
406 |
Raises: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
407 |
UnknownMethodCallError if the MockObject does not mock the requested |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
408 |
method. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
409 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
410 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
411 |
if name in self._known_vars: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
412 |
return getattr(self._class_to_mock, name) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
413 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
414 |
if name in self._known_methods: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
415 |
return self._CreateMockMethod( |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
416 |
name, |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
417 |
method_to_mock=getattr(self._class_to_mock, name)) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
418 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
419 |
raise UnknownMethodCallError(name) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
420 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
421 |
def __eq__(self, rhs): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
422 |
"""Provide custom logic to compare objects.""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
423 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
424 |
return (isinstance(rhs, MockObject) and |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
425 |
self._class_to_mock == rhs._class_to_mock and |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
426 |
self._replay_mode == rhs._replay_mode and |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
427 |
self._expected_calls_queue == rhs._expected_calls_queue) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
428 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
429 |
def __setitem__(self, key, value): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
430 |
"""Provide custom logic for mocking classes that support item assignment. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
431 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
432 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
433 |
key: Key to set the value for. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
434 |
value: Value to set. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
435 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
436 |
Returns: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
437 |
Expected return value in replay mode. A MockMethod object for the |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
438 |
__setitem__ method that has already been called if not in replay mode. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
439 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
440 |
Raises: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
441 |
TypeError if the underlying class does not support item assignment. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
442 |
UnexpectedMethodCallError if the object does not expect the call to |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
443 |
__setitem__. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
444 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
445 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
446 |
setitem = self._class_to_mock.__dict__.get('__setitem__', None) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
447 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
448 |
# Verify the class supports item assignment. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
449 |
if setitem is None: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
450 |
raise TypeError('object does not support item assignment') |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
451 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
452 |
# If we are in replay mode then simply call the mock __setitem__ method. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
453 |
if self._replay_mode: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
454 |
return MockMethod('__setitem__', self._expected_calls_queue, |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
455 |
self._replay_mode)(key, value) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
456 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
457 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
458 |
# Otherwise, create a mock method __setitem__. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
459 |
return self._CreateMockMethod('__setitem__')(key, value) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
460 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
461 |
def __getitem__(self, key): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
462 |
"""Provide custom logic for mocking classes that are subscriptable. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
463 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
464 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
465 |
key: Key to return the value for. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
466 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
467 |
Returns: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
468 |
Expected return value in replay mode. A MockMethod object for the |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
469 |
__getitem__ method that has already been called if not in replay mode. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
470 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
471 |
Raises: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
472 |
TypeError if the underlying class is not subscriptable. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
473 |
UnexpectedMethodCallError if the object does not expect the call to |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
474 |
__setitem__. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
475 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
476 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
477 |
getitem = self._class_to_mock.__dict__.get('__getitem__', None) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
478 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
479 |
# Verify the class supports item assignment. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
480 |
if getitem is None: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
481 |
raise TypeError('unsubscriptable object') |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
482 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
483 |
# If we are in replay mode then simply call the mock __getitem__ method. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
484 |
if self._replay_mode: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
485 |
return MockMethod('__getitem__', self._expected_calls_queue, |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
486 |
self._replay_mode)(key) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
487 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
488 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
489 |
# Otherwise, create a mock method __getitem__. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
490 |
return self._CreateMockMethod('__getitem__')(key) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
491 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
492 |
def __contains__(self, key): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
493 |
"""Provide custom logic for mocking classes that contain items. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
494 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
495 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
496 |
key: Key to look in container for. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
497 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
498 |
Returns: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
499 |
Expected return value in replay mode. A MockMethod object for the |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
500 |
__contains__ method that has already been called if not in replay mode. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
501 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
502 |
Raises: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
503 |
TypeError if the underlying class does not implement __contains__ |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
504 |
UnexpectedMethodCaller if the object does not expect the call to |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
505 |
__contains__. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
506 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
507 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
508 |
contains = self._class_to_mock.__dict__.get('__contains__', None) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
509 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
510 |
if contains is None: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
511 |
raise TypeError('unsubscriptable object') |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
512 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
513 |
if self._replay_mode: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
514 |
return MockMethod('__contains__', self._expected_calls_queue, |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
515 |
self._replay_mode)(key) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
516 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
517 |
return self._CreateMockMethod('__contains__')(key) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
518 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
519 |
def __call__(self, *params, **named_params): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
520 |
"""Provide custom logic for mocking classes that are callable.""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
521 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
522 |
# Verify the class we are mocking is callable |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
523 |
callable = self._class_to_mock.__dict__.get('__call__', None) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
524 |
if callable is None: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
525 |
raise TypeError('Not callable') |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
526 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
527 |
# Because the call is happening directly on this object instead of a method, |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
528 |
# the call on the mock method is made right here |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
529 |
mock_method = self._CreateMockMethod('__call__') |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
530 |
return mock_method(*params, **named_params) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
531 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
532 |
@property |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
533 |
def __class__(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
534 |
"""Return the class that is being mocked.""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
535 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
536 |
return self._class_to_mock |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
537 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
538 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
539 |
class MethodCallChecker(object): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
540 |
"""Ensures that methods are called correctly.""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
541 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
542 |
_NEEDED, _DEFAULT, _GIVEN = range(3) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
543 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
544 |
def __init__(self, method): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
545 |
"""Creates a checker. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
546 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
547 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
548 |
# method: A method to check. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
549 |
method: function |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
550 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
551 |
Raises: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
552 |
ValueError: method could not be inspected, so checks aren't possible. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
553 |
Some methods and functions like built-ins can't be inspected. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
554 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
555 |
try: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
556 |
self._args, varargs, varkw, defaults = inspect.getargspec(method) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
557 |
except TypeError: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
558 |
raise ValueError('Could not get argument specification for %r' |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
559 |
% (method,)) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
560 |
if inspect.ismethod(method): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
561 |
self._args = self._args[1:] # Skip 'self'. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
562 |
self._method = method |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
563 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
564 |
self._has_varargs = varargs is not None |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
565 |
self._has_varkw = varkw is not None |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
566 |
if defaults is None: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
567 |
self._required_args = self._args |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
568 |
self._default_args = [] |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
569 |
else: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
570 |
self._required_args = self._args[:-len(defaults)] |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
571 |
self._default_args = self._args[-len(defaults):] |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
572 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
573 |
def _RecordArgumentGiven(self, arg_name, arg_status): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
574 |
"""Mark an argument as being given. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
575 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
576 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
577 |
# arg_name: The name of the argument to mark in arg_status. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
578 |
# arg_status: Maps argument names to one of _NEEDED, _DEFAULT, _GIVEN. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
579 |
arg_name: string |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
580 |
arg_status: dict |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
581 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
582 |
Raises: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
583 |
AttributeError: arg_name is already marked as _GIVEN. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
584 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
585 |
if arg_status.get(arg_name, None) == MethodCallChecker._GIVEN: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
586 |
raise AttributeError('%s provided more than once' % (arg_name,)) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
587 |
arg_status[arg_name] = MethodCallChecker._GIVEN |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
588 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
589 |
def Check(self, params, named_params): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
590 |
"""Ensures that the parameters used while recording a call are valid. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
591 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
592 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
593 |
# params: A list of positional parameters. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
594 |
# named_params: A dict of named parameters. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
595 |
params: list |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
596 |
named_params: dict |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
597 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
598 |
Raises: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
599 |
AttributeError: the given parameters don't work with the given method. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
600 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
601 |
arg_status = dict((a, MethodCallChecker._NEEDED) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
602 |
for a in self._required_args) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
603 |
for arg in self._default_args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
604 |
arg_status[arg] = MethodCallChecker._DEFAULT |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
605 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
606 |
# Check that each positional param is valid. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
607 |
for i in range(len(params)): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
608 |
try: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
609 |
arg_name = self._args[i] |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
610 |
except IndexError: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
611 |
if not self._has_varargs: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
612 |
raise AttributeError('%s does not take %d or more positional ' |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
613 |
'arguments' % (self._method.__name__, i)) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
614 |
else: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
615 |
self._RecordArgumentGiven(arg_name, arg_status) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
616 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
617 |
# Check each keyword argument. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
618 |
for arg_name in named_params: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
619 |
if arg_name not in arg_status and not self._has_varkw: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
620 |
raise AttributeError('%s is not expecting keyword argument %s' |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
621 |
% (self._method.__name__, arg_name)) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
622 |
self._RecordArgumentGiven(arg_name, arg_status) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
623 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
624 |
# Ensure all the required arguments have been given. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
625 |
still_needed = [k for k, v in arg_status.iteritems() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
626 |
if v == MethodCallChecker._NEEDED] |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
627 |
if still_needed: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
628 |
raise AttributeError('No values given for arguments %s' |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
629 |
% (' '.join(sorted(still_needed)))) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
630 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
631 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
632 |
class MockMethod(object): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
633 |
"""Callable mock method. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
634 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
635 |
A MockMethod should act exactly like the method it mocks, accepting parameters |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
636 |
and returning a value, or throwing an exception (as specified). When this |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
637 |
method is called, it can optionally verify whether the called method (name and |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
638 |
signature) matches the expected method. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
639 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
640 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
641 |
def __init__(self, method_name, call_queue, replay_mode, method_to_mock=None): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
642 |
"""Construct a new mock method. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
643 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
644 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
645 |
# method_name: the name of the method |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
646 |
# call_queue: deque of calls, verify this call against the head, or add |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
647 |
# this call to the queue. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
648 |
# replay_mode: False if we are recording, True if we are verifying calls |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
649 |
# against the call queue. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
650 |
# method_to_mock: The actual method being mocked, used for introspection. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
651 |
method_name: str |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
652 |
call_queue: list or deque |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
653 |
replay_mode: bool |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
654 |
method_to_mock: a method object |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
655 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
656 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
657 |
self._name = method_name |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
658 |
self._call_queue = call_queue |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
659 |
if not isinstance(call_queue, deque): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
660 |
self._call_queue = deque(self._call_queue) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
661 |
self._replay_mode = replay_mode |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
662 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
663 |
self._params = None |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
664 |
self._named_params = None |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
665 |
self._return_value = None |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
666 |
self._exception = None |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
667 |
self._side_effects = None |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
668 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
669 |
try: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
670 |
self._checker = MethodCallChecker(method_to_mock) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
671 |
except ValueError: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
672 |
self._checker = None |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
673 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
674 |
def __call__(self, *params, **named_params): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
675 |
"""Log parameters and return the specified return value. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
676 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
677 |
If the Mock(Anything/Object) associated with this call is in record mode, |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
678 |
this MockMethod will be pushed onto the expected call queue. If the mock |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
679 |
is in replay mode, this will pop a MockMethod off the top of the queue and |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
680 |
verify this call is equal to the expected call. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
681 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
682 |
Raises: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
683 |
UnexpectedMethodCall if this call is supposed to match an expected method |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
684 |
call and it does not. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
685 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
686 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
687 |
self._params = params |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
688 |
self._named_params = named_params |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
689 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
690 |
if not self._replay_mode: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
691 |
if self._checker is not None: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
692 |
self._checker.Check(params, named_params) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
693 |
self._call_queue.append(self) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
694 |
return self |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
695 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
696 |
expected_method = self._VerifyMethodCall() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
697 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
698 |
if expected_method._side_effects: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
699 |
expected_method._side_effects(*params, **named_params) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
700 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
701 |
if expected_method._exception: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
702 |
raise expected_method._exception |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
703 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
704 |
return expected_method._return_value |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
705 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
706 |
def __getattr__(self, name): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
707 |
"""Raise an AttributeError with a helpful message.""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
708 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
709 |
raise AttributeError('MockMethod has no attribute "%s". ' |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
710 |
'Did you remember to put your mocks in replay mode?' % name) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
711 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
712 |
def _PopNextMethod(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
713 |
"""Pop the next method from our call queue.""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
714 |
try: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
715 |
return self._call_queue.popleft() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
716 |
except IndexError: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
717 |
raise UnexpectedMethodCallError(self, None) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
718 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
719 |
def _VerifyMethodCall(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
720 |
"""Verify the called method is expected. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
721 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
722 |
This can be an ordered method, or part of an unordered set. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
723 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
724 |
Returns: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
725 |
The expected mock method. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
726 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
727 |
Raises: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
728 |
UnexpectedMethodCall if the method called was not expected. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
729 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
730 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
731 |
expected = self._PopNextMethod() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
732 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
733 |
# Loop here, because we might have a MethodGroup followed by another |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
734 |
# group. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
735 |
while isinstance(expected, MethodGroup): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
736 |
expected, method = expected.MethodCalled(self) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
737 |
if method is not None: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
738 |
return method |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
739 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
740 |
# This is a mock method, so just check equality. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
741 |
if expected != self: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
742 |
raise UnexpectedMethodCallError(self, expected) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
743 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
744 |
return expected |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
745 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
746 |
def __str__(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
747 |
params = ', '.join( |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
748 |
[repr(p) for p in self._params or []] + |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
749 |
['%s=%r' % x for x in sorted((self._named_params or {}).items())]) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
750 |
desc = "%s(%s) -> %r" % (self._name, params, self._return_value) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
751 |
return desc |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
752 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
753 |
def __eq__(self, rhs): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
754 |
"""Test whether this MockMethod is equivalent to another MockMethod. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
755 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
756 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
757 |
# rhs: the right hand side of the test |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
758 |
rhs: MockMethod |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
759 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
760 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
761 |
return (isinstance(rhs, MockMethod) and |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
762 |
self._name == rhs._name and |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
763 |
self._params == rhs._params and |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
764 |
self._named_params == rhs._named_params) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
765 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
766 |
def __ne__(self, rhs): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
767 |
"""Test whether this MockMethod is not equivalent to another MockMethod. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
768 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
769 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
770 |
# rhs: the right hand side of the test |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
771 |
rhs: MockMethod |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
772 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
773 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
774 |
return not self == rhs |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
775 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
776 |
def GetPossibleGroup(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
777 |
"""Returns a possible group from the end of the call queue or None if no |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
778 |
other methods are on the stack. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
779 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
780 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
781 |
# Remove this method from the tail of the queue so we can add it to a group. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
782 |
this_method = self._call_queue.pop() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
783 |
assert this_method == self |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
784 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
785 |
# Determine if the tail of the queue is a group, or just a regular ordered |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
786 |
# mock method. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
787 |
group = None |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
788 |
try: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
789 |
group = self._call_queue[-1] |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
790 |
except IndexError: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
791 |
pass |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
792 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
793 |
return group |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
794 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
795 |
def _CheckAndCreateNewGroup(self, group_name, group_class): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
796 |
"""Checks if the last method (a possible group) is an instance of our |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
797 |
group_class. Adds the current method to this group or creates a new one. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
798 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
799 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
800 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
801 |
group_name: the name of the group. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
802 |
group_class: the class used to create instance of this new group |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
803 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
804 |
group = self.GetPossibleGroup() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
805 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
806 |
# If this is a group, and it is the correct group, add the method. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
807 |
if isinstance(group, group_class) and group.group_name() == group_name: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
808 |
group.AddMethod(self) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
809 |
return self |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
810 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
811 |
# Create a new group and add the method. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
812 |
new_group = group_class(group_name) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
813 |
new_group.AddMethod(self) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
814 |
self._call_queue.append(new_group) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
815 |
return self |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
816 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
817 |
def InAnyOrder(self, group_name="default"): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
818 |
"""Move this method into a group of unordered calls. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
819 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
820 |
A group of unordered calls must be defined together, and must be executed |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
821 |
in full before the next expected method can be called. There can be |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
822 |
multiple groups that are expected serially, if they are given |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
823 |
different group names. The same group name can be reused if there is a |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
824 |
standard method call, or a group with a different name, spliced between |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
825 |
usages. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
826 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
827 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
828 |
group_name: the name of the unordered group. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
829 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
830 |
Returns: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
831 |
self |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
832 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
833 |
return self._CheckAndCreateNewGroup(group_name, UnorderedGroup) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
834 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
835 |
def MultipleTimes(self, group_name="default"): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
836 |
"""Move this method into group of calls which may be called multiple times. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
837 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
838 |
A group of repeating calls must be defined together, and must be executed in |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
839 |
full before the next expected mehtod can be called. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
840 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
841 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
842 |
group_name: the name of the unordered group. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
843 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
844 |
Returns: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
845 |
self |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
846 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
847 |
return self._CheckAndCreateNewGroup(group_name, MultipleTimesGroup) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
848 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
849 |
def AndReturn(self, return_value): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
850 |
"""Set the value to return when this method is called. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
851 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
852 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
853 |
# return_value can be anything. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
854 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
855 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
856 |
self._return_value = return_value |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
857 |
return return_value |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
858 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
859 |
def AndRaise(self, exception): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
860 |
"""Set the exception to raise when this method is called. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
861 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
862 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
863 |
# exception: the exception to raise when this method is called. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
864 |
exception: Exception |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
865 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
866 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
867 |
self._exception = exception |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
868 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
869 |
def WithSideEffects(self, side_effects): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
870 |
"""Set the side effects that are simulated when this method is called. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
871 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
872 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
873 |
side_effects: A callable which modifies the parameters or other relevant |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
874 |
state which a given test case depends on. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
875 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
876 |
Returns: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
877 |
Self for chaining with AndReturn and AndRaise. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
878 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
879 |
self._side_effects = side_effects |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
880 |
return self |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
881 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
882 |
class Comparator: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
883 |
"""Base class for all Mox comparators. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
884 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
885 |
A Comparator can be used as a parameter to a mocked method when the exact |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
886 |
value is not known. For example, the code you are testing might build up a |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
887 |
long SQL string that is passed to your mock DAO. You're only interested that |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
888 |
the IN clause contains the proper primary keys, so you can set your mock |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
889 |
up as follows: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
890 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
891 |
mock_dao.RunQuery(StrContains('IN (1, 2, 4, 5)')).AndReturn(mock_result) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
892 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
893 |
Now whatever query is passed in must contain the string 'IN (1, 2, 4, 5)'. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
894 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
895 |
A Comparator may replace one or more parameters, for example: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
896 |
# return at most 10 rows |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
897 |
mock_dao.RunQuery(StrContains('SELECT'), 10) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
898 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
899 |
or |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
900 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
901 |
# Return some non-deterministic number of rows |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
902 |
mock_dao.RunQuery(StrContains('SELECT'), IsA(int)) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
903 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
904 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
905 |
def equals(self, rhs): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
906 |
"""Special equals method that all comparators must implement. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
907 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
908 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
909 |
rhs: any python object |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
910 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
911 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
912 |
raise NotImplementedError, 'method must be implemented by a subclass.' |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
913 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
914 |
def __eq__(self, rhs): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
915 |
return self.equals(rhs) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
916 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
917 |
def __ne__(self, rhs): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
918 |
return not self.equals(rhs) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
919 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
920 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
921 |
class IsA(Comparator): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
922 |
"""This class wraps a basic Python type or class. It is used to verify |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
923 |
that a parameter is of the given type or class. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
924 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
925 |
Example: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
926 |
mock_dao.Connect(IsA(DbConnectInfo)) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
927 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
928 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
929 |
def __init__(self, class_name): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
930 |
"""Initialize IsA |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
931 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
932 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
933 |
class_name: basic python type or a class |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
934 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
935 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
936 |
self._class_name = class_name |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
937 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
938 |
def equals(self, rhs): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
939 |
"""Check to see if the RHS is an instance of class_name. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
940 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
941 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
942 |
# rhs: the right hand side of the test |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
943 |
rhs: object |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
944 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
945 |
Returns: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
946 |
bool |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
947 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
948 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
949 |
try: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
950 |
return isinstance(rhs, self._class_name) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
951 |
except TypeError: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
952 |
# Check raw types if there was a type error. This is helpful for |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
953 |
# things like cStringIO.StringIO. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
954 |
return type(rhs) == type(self._class_name) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
955 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
956 |
def __repr__(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
957 |
return str(self._class_name) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
958 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
959 |
class IsAlmost(Comparator): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
960 |
"""Comparison class used to check whether a parameter is nearly equal |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
961 |
to a given value. Generally useful for floating point numbers. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
962 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
963 |
Example mock_dao.SetTimeout((IsAlmost(3.9))) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
964 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
965 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
966 |
def __init__(self, float_value, places=7): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
967 |
"""Initialize IsAlmost. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
968 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
969 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
970 |
float_value: The value for making the comparison. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
971 |
places: The number of decimal places to round to. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
972 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
973 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
974 |
self._float_value = float_value |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
975 |
self._places = places |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
976 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
977 |
def equals(self, rhs): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
978 |
"""Check to see if RHS is almost equal to float_value |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
979 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
980 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
981 |
rhs: the value to compare to float_value |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
982 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
983 |
Returns: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
984 |
bool |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
985 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
986 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
987 |
try: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
988 |
return round(rhs-self._float_value, self._places) == 0 |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
989 |
except TypeError: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
990 |
# This is probably because either float_value or rhs is not a number. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
991 |
return False |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
992 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
993 |
def __repr__(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
994 |
return str(self._float_value) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
995 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
996 |
class StrContains(Comparator): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
997 |
"""Comparison class used to check whether a substring exists in a |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
998 |
string parameter. This can be useful in mocking a database with SQL |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
999 |
passed in as a string parameter, for example. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1000 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1001 |
Example: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1002 |
mock_dao.RunQuery(StrContains('IN (1, 2, 4, 5)')).AndReturn(mock_result) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1003 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1004 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1005 |
def __init__(self, search_string): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1006 |
"""Initialize. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1007 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1008 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1009 |
# search_string: the string you are searching for |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1010 |
search_string: str |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1011 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1012 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1013 |
self._search_string = search_string |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1014 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1015 |
def equals(self, rhs): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1016 |
"""Check to see if the search_string is contained in the rhs string. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1017 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1018 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1019 |
# rhs: the right hand side of the test |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1020 |
rhs: object |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1021 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1022 |
Returns: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1023 |
bool |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1024 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1025 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1026 |
try: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1027 |
return rhs.find(self._search_string) > -1 |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1028 |
except Exception: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1029 |
return False |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1030 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1031 |
def __repr__(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1032 |
return '<str containing \'%s\'>' % self._search_string |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1033 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1034 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1035 |
class Regex(Comparator): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1036 |
"""Checks if a string matches a regular expression. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1037 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1038 |
This uses a given regular expression to determine equality. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1039 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1040 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1041 |
def __init__(self, pattern, flags=0): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1042 |
"""Initialize. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1043 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1044 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1045 |
# pattern is the regular expression to search for |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1046 |
pattern: str |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1047 |
# flags passed to re.compile function as the second argument |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1048 |
flags: int |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1049 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1050 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1051 |
self.regex = re.compile(pattern, flags=flags) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1052 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1053 |
def equals(self, rhs): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1054 |
"""Check to see if rhs matches regular expression pattern. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1055 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1056 |
Returns: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1057 |
bool |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1058 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1059 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1060 |
return self.regex.search(rhs) is not None |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1061 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1062 |
def __repr__(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1063 |
s = '<regular expression \'%s\'' % self.regex.pattern |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1064 |
if self.regex.flags: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1065 |
s += ', flags=%d' % self.regex.flags |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1066 |
s += '>' |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1067 |
return s |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1068 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1069 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1070 |
class In(Comparator): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1071 |
"""Checks whether an item (or key) is in a list (or dict) parameter. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1072 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1073 |
Example: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1074 |
mock_dao.GetUsersInfo(In('expectedUserName')).AndReturn(mock_result) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1075 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1076 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1077 |
def __init__(self, key): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1078 |
"""Initialize. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1079 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1080 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1081 |
# key is any thing that could be in a list or a key in a dict |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1082 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1083 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1084 |
self._key = key |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1085 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1086 |
def equals(self, rhs): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1087 |
"""Check to see whether key is in rhs. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1088 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1089 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1090 |
rhs: dict |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1091 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1092 |
Returns: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1093 |
bool |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1094 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1095 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1096 |
return self._key in rhs |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1097 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1098 |
def __repr__(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1099 |
return '<sequence or map containing \'%s\'>' % self._key |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1100 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1101 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1102 |
class Not(Comparator): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1103 |
"""Checks whether a predicates is False. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1104 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1105 |
Example: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1106 |
mock_dao.UpdateUsers(Not(ContainsKeyValue('stevepm', stevepm_user_info))) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1107 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1108 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1109 |
def __init__(self, predicate): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1110 |
"""Initialize. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1111 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1112 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1113 |
# predicate: a Comparator instance. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1114 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1115 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1116 |
assert isinstance(predicate, Comparator), ("predicate %r must be a" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1117 |
" Comparator." % predicate) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1118 |
self._predicate = predicate |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1119 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1120 |
def equals(self, rhs): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1121 |
"""Check to see whether the predicate is False. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1122 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1123 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1124 |
rhs: A value that will be given in argument of the predicate. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1125 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1126 |
Returns: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1127 |
bool |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1128 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1129 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1130 |
return not self._predicate.equals(rhs) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1131 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1132 |
def __repr__(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1133 |
return '<not \'%s\'>' % self._predicate |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1134 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1135 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1136 |
class ContainsKeyValue(Comparator): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1137 |
"""Checks whether a key/value pair is in a dict parameter. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1138 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1139 |
Example: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1140 |
mock_dao.UpdateUsers(ContainsKeyValue('stevepm', stevepm_user_info)) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1141 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1142 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1143 |
def __init__(self, key, value): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1144 |
"""Initialize. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1145 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1146 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1147 |
# key: a key in a dict |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1148 |
# value: the corresponding value |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1149 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1150 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1151 |
self._key = key |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1152 |
self._value = value |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1153 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1154 |
def equals(self, rhs): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1155 |
"""Check whether the given key/value pair is in the rhs dict. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1156 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1157 |
Returns: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1158 |
bool |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1159 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1160 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1161 |
try: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1162 |
return rhs[self._key] == self._value |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1163 |
except Exception: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1164 |
return False |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1165 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1166 |
def __repr__(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1167 |
return '<map containing the entry \'%s: %s\'>' % (self._key, self._value) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1168 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1169 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1170 |
class SameElementsAs(Comparator): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1171 |
"""Checks whether iterables contain the same elements (ignoring order). |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1172 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1173 |
Example: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1174 |
mock_dao.ProcessUsers(SameElementsAs('stevepm', 'salomaki')) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1175 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1176 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1177 |
def __init__(self, expected_seq): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1178 |
"""Initialize. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1179 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1180 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1181 |
expected_seq: a sequence |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1182 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1183 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1184 |
self._expected_seq = expected_seq |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1185 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1186 |
def equals(self, actual_seq): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1187 |
"""Check to see whether actual_seq has same elements as expected_seq. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1188 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1189 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1190 |
actual_seq: sequence |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1191 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1192 |
Returns: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1193 |
bool |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1194 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1195 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1196 |
try: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1197 |
expected = dict([(element, None) for element in self._expected_seq]) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1198 |
actual = dict([(element, None) for element in actual_seq]) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1199 |
except TypeError: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1200 |
# Fall back to slower list-compare if any of the objects are unhashable. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1201 |
expected = list(self._expected_seq) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1202 |
actual = list(actual_seq) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1203 |
expected.sort() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1204 |
actual.sort() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1205 |
return expected == actual |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1206 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1207 |
def __repr__(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1208 |
return '<sequence with same elements as \'%s\'>' % self._expected_seq |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1209 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1210 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1211 |
class And(Comparator): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1212 |
"""Evaluates one or more Comparators on RHS and returns an AND of the results. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1213 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1214 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1215 |
def __init__(self, *args): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1216 |
"""Initialize. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1217 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1218 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1219 |
*args: One or more Comparator |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1220 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1221 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1222 |
self._comparators = args |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1223 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1224 |
def equals(self, rhs): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1225 |
"""Checks whether all Comparators are equal to rhs. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1226 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1227 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1228 |
# rhs: can be anything |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1229 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1230 |
Returns: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1231 |
bool |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1232 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1233 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1234 |
for comparator in self._comparators: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1235 |
if not comparator.equals(rhs): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1236 |
return False |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1237 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1238 |
return True |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1239 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1240 |
def __repr__(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1241 |
return '<AND %s>' % str(self._comparators) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1242 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1243 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1244 |
class Or(Comparator): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1245 |
"""Evaluates one or more Comparators on RHS and returns an OR of the results. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1246 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1247 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1248 |
def __init__(self, *args): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1249 |
"""Initialize. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1250 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1251 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1252 |
*args: One or more Mox comparators |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1253 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1254 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1255 |
self._comparators = args |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1256 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1257 |
def equals(self, rhs): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1258 |
"""Checks whether any Comparator is equal to rhs. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1259 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1260 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1261 |
# rhs: can be anything |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1262 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1263 |
Returns: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1264 |
bool |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1265 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1266 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1267 |
for comparator in self._comparators: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1268 |
if comparator.equals(rhs): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1269 |
return True |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1270 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1271 |
return False |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1272 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1273 |
def __repr__(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1274 |
return '<OR %s>' % str(self._comparators) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1275 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1276 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1277 |
class Func(Comparator): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1278 |
"""Call a function that should verify the parameter passed in is correct. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1279 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1280 |
You may need the ability to perform more advanced operations on the parameter |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1281 |
in order to validate it. You can use this to have a callable validate any |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1282 |
parameter. The callable should return either True or False. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1283 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1284 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1285 |
Example: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1286 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1287 |
def myParamValidator(param): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1288 |
# Advanced logic here |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1289 |
return True |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1290 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1291 |
mock_dao.DoSomething(Func(myParamValidator), true) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1292 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1293 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1294 |
def __init__(self, func): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1295 |
"""Initialize. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1296 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1297 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1298 |
func: callable that takes one parameter and returns a bool |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1299 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1300 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1301 |
self._func = func |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1302 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1303 |
def equals(self, rhs): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1304 |
"""Test whether rhs passes the function test. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1305 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1306 |
rhs is passed into func. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1307 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1308 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1309 |
rhs: any python object |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1310 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1311 |
Returns: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1312 |
the result of func(rhs) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1313 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1314 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1315 |
return self._func(rhs) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1316 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1317 |
def __repr__(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1318 |
return str(self._func) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1319 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1320 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1321 |
class IgnoreArg(Comparator): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1322 |
"""Ignore an argument. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1323 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1324 |
This can be used when we don't care about an argument of a method call. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1325 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1326 |
Example: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1327 |
# Check if CastMagic is called with 3 as first arg and 'disappear' as third. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1328 |
mymock.CastMagic(3, IgnoreArg(), 'disappear') |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1329 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1330 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1331 |
def equals(self, unused_rhs): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1332 |
"""Ignores arguments and returns True. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1333 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1334 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1335 |
unused_rhs: any python object |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1336 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1337 |
Returns: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1338 |
always returns True |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1339 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1340 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1341 |
return True |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1342 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1343 |
def __repr__(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1344 |
return '<IgnoreArg>' |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1345 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1346 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1347 |
class MethodGroup(object): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1348 |
"""Base class containing common behaviour for MethodGroups.""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1349 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1350 |
def __init__(self, group_name): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1351 |
self._group_name = group_name |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1352 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1353 |
def group_name(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1354 |
return self._group_name |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1355 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1356 |
def __str__(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1357 |
return '<%s "%s">' % (self.__class__.__name__, self._group_name) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1358 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1359 |
def AddMethod(self, mock_method): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1360 |
raise NotImplementedError |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1361 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1362 |
def MethodCalled(self, mock_method): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1363 |
raise NotImplementedError |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1364 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1365 |
def IsSatisfied(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1366 |
raise NotImplementedError |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1367 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1368 |
class UnorderedGroup(MethodGroup): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1369 |
"""UnorderedGroup holds a set of method calls that may occur in any order. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1370 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1371 |
This construct is helpful for non-deterministic events, such as iterating |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1372 |
over the keys of a dict. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1373 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1374 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1375 |
def __init__(self, group_name): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1376 |
super(UnorderedGroup, self).__init__(group_name) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1377 |
self._methods = [] |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1378 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1379 |
def AddMethod(self, mock_method): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1380 |
"""Add a method to this group. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1381 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1382 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1383 |
mock_method: A mock method to be added to this group. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1384 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1385 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1386 |
self._methods.append(mock_method) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1387 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1388 |
def MethodCalled(self, mock_method): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1389 |
"""Remove a method call from the group. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1390 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1391 |
If the method is not in the set, an UnexpectedMethodCallError will be |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1392 |
raised. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1393 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1394 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1395 |
mock_method: a mock method that should be equal to a method in the group. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1396 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1397 |
Returns: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1398 |
The mock method from the group |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1399 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1400 |
Raises: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1401 |
UnexpectedMethodCallError if the mock_method was not in the group. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1402 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1403 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1404 |
# Check to see if this method exists, and if so, remove it from the set |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1405 |
# and return it. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1406 |
for method in self._methods: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1407 |
if method == mock_method: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1408 |
# Remove the called mock_method instead of the method in the group. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1409 |
# The called method will match any comparators when equality is checked |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1410 |
# during removal. The method in the group could pass a comparator to |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1411 |
# another comparator during the equality check. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1412 |
self._methods.remove(mock_method) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1413 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1414 |
# If this group is not empty, put it back at the head of the queue. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1415 |
if not self.IsSatisfied(): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1416 |
mock_method._call_queue.appendleft(self) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1417 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1418 |
return self, method |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1419 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1420 |
raise UnexpectedMethodCallError(mock_method, self) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1421 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1422 |
def IsSatisfied(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1423 |
"""Return True if there are not any methods in this group.""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1424 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1425 |
return len(self._methods) == 0 |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1426 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1427 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1428 |
class MultipleTimesGroup(MethodGroup): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1429 |
"""MultipleTimesGroup holds methods that may be called any number of times. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1430 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1431 |
Note: Each method must be called at least once. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1432 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1433 |
This is helpful, if you don't know or care how many times a method is called. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1434 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1435 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1436 |
def __init__(self, group_name): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1437 |
super(MultipleTimesGroup, self).__init__(group_name) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1438 |
self._methods = set() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1439 |
self._methods_called = set() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1440 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1441 |
def AddMethod(self, mock_method): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1442 |
"""Add a method to this group. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1443 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1444 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1445 |
mock_method: A mock method to be added to this group. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1446 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1447 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1448 |
self._methods.add(mock_method) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1449 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1450 |
def MethodCalled(self, mock_method): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1451 |
"""Remove a method call from the group. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1452 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1453 |
If the method is not in the set, an UnexpectedMethodCallError will be |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1454 |
raised. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1455 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1456 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1457 |
mock_method: a mock method that should be equal to a method in the group. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1458 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1459 |
Returns: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1460 |
The mock method from the group |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1461 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1462 |
Raises: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1463 |
UnexpectedMethodCallError if the mock_method was not in the group. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1464 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1465 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1466 |
# Check to see if this method exists, and if so add it to the set of |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1467 |
# called methods. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1468 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1469 |
for method in self._methods: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1470 |
if method == mock_method: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1471 |
self._methods_called.add(mock_method) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1472 |
# Always put this group back on top of the queue, because we don't know |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1473 |
# when we are done. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1474 |
mock_method._call_queue.appendleft(self) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1475 |
return self, method |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1476 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1477 |
if self.IsSatisfied(): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1478 |
next_method = mock_method._PopNextMethod(); |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1479 |
return next_method, None |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1480 |
else: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1481 |
raise UnexpectedMethodCallError(mock_method, self) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1482 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1483 |
def IsSatisfied(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1484 |
"""Return True if all methods in this group are called at least once.""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1485 |
# NOTE(psycho): We can't use the simple set difference here because we want |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1486 |
# to match different parameters which are considered the same e.g. IsA(str) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1487 |
# and some string. This solution is O(n^2) but n should be small. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1488 |
tmp = self._methods.copy() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1489 |
for called in self._methods_called: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1490 |
for expected in tmp: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1491 |
if called == expected: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1492 |
tmp.remove(expected) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1493 |
if not tmp: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1494 |
return True |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1495 |
break |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1496 |
return False |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1497 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1498 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1499 |
class MoxMetaTestBase(type): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1500 |
"""Metaclass to add mox cleanup and verification to every test. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1501 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1502 |
As the mox unit testing class is being constructed (MoxTestBase or a |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1503 |
subclass), this metaclass will modify all test functions to call the |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1504 |
CleanUpMox method of the test class after they finish. This means that |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1505 |
unstubbing and verifying will happen for every test with no additional code, |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1506 |
and any failures will result in test failures as opposed to errors. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1507 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1508 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1509 |
def __init__(cls, name, bases, d): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1510 |
type.__init__(cls, name, bases, d) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1511 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1512 |
# also get all the attributes from the base classes to account |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1513 |
# for a case when test class is not the immediate child of MoxTestBase |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1514 |
for base in bases: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1515 |
for attr_name in dir(base): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1516 |
d[attr_name] = getattr(base, attr_name) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1517 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1518 |
for func_name, func in d.items(): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1519 |
if func_name.startswith('test') and callable(func): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1520 |
setattr(cls, func_name, MoxMetaTestBase.CleanUpTest(cls, func)) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1521 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1522 |
@staticmethod |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1523 |
def CleanUpTest(cls, func): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1524 |
"""Adds Mox cleanup code to any MoxTestBase method. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1525 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1526 |
Always unsets stubs after a test. Will verify all mocks for tests that |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1527 |
otherwise pass. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1528 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1529 |
Args: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1530 |
cls: MoxTestBase or subclass; the class whose test method we are altering. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1531 |
func: method; the method of the MoxTestBase test class we wish to alter. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1532 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1533 |
Returns: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1534 |
The modified method. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1535 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1536 |
def new_method(self, *args, **kwargs): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1537 |
mox_obj = getattr(self, 'mox', None) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1538 |
cleanup_mox = False |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1539 |
if mox_obj and isinstance(mox_obj, Mox): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1540 |
cleanup_mox = True |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1541 |
try: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1542 |
func(self, *args, **kwargs) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1543 |
finally: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1544 |
if cleanup_mox: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1545 |
mox_obj.UnsetStubs() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1546 |
if cleanup_mox: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1547 |
mox_obj.VerifyAll() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1548 |
new_method.__name__ = func.__name__ |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1549 |
new_method.__doc__ = func.__doc__ |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1550 |
new_method.__module__ = func.__module__ |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1551 |
return new_method |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1552 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1553 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1554 |
class MoxTestBase(unittest.TestCase): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1555 |
"""Convenience test class to make stubbing easier. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1556 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1557 |
Sets up a "mox" attribute which is an instance of Mox - any mox tests will |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1558 |
want this. Also automatically unsets any stubs and verifies that all mock |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1559 |
methods have been called at the end of each test, eliminating boilerplate |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1560 |
code. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1561 |
""" |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1562 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1563 |
__metaclass__ = MoxMetaTestBase |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1564 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1565 |
def setUp(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1566 |
super(MoxTestBase, self).setUp() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1567 |
self.mox = Mox() |