Skip to content
This repository has been archived by the owner on Mar 27, 2022. It is now read-only.

Commit

Permalink
cleaned up
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Cuthbertson committed May 16, 2009
1 parent d0aeaca commit 97c3bcc
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 57 deletions.
17 changes: 3 additions & 14 deletions mocktest/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
__all__ = [
'TestCase',
'expect',
'ignore',
'mock_on',
'mock',
'stub',
'pending',
'raw_mock',
]

from core import *
from core import _setup, _teardown # not imported by all
from mocktest import *
from mockmatcher import *
from mockerror import MockError
from stub import stub
from mockerror import *
from stub import *
from matchers import *

__version__ = '0.3.0'
8 changes: 7 additions & 1 deletion mocktest/mockmatcher.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from matchers import Matcher
from callrecord import CallRecord
__unittest = True

Expand Down Expand Up @@ -182,14 +183,19 @@ def _multiplicities_match(self, num_calls):
def _args_equal_func(self, args, kwargs):
"""
returns a function that returns whether its arguments match the
args (tuple), and its kewyord arguments match the kwargs (dict)
args (tuple), and its keyword arguments match the kwargs (dict)
"""
def check(*a, **k):
# print "Comapring args:"
# print "%r %r" % (a, k)
# print "%r %r" % (args, kwargs)
return a == args and k == kwargs
return check

def _equals_or_matches(self, expected, actual):
if isinstance(expected, Matcher):
return expected.matches(actual)
return expected == actual

# multiplicity-checking operators
def _eq(self, a, b):
Expand Down
6 changes: 0 additions & 6 deletions mocktest/mocktest.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,7 @@ def __setup(self):
def __teardown(self):
core._teardown()

def __assert_not_callable(self, expr):
if callable(expr):
raise TypeError, "Assertion called on a callable object - this usually means you forgot to call it"

def assert_(self, expr, desc = None):
self.__assert_not_callable(expr)
if desc is None:
desc = expr
super(TestCase, self).assert_(expr, desc)
Expand All @@ -126,7 +121,6 @@ def assert_(self, expr, desc = None):
failUnless = assert_

def assertFalse(self, expr, desc = None):
self.__assert_not_callable(expr)
if desc is None:
desc = "Expected (%r) to be False" % (expr,)
super(TestCase, self).assertFalse(expr, desc)
Expand Down
5 changes: 4 additions & 1 deletion mocktest/mockwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,13 @@ def __expect_call_on(self, obj):
matcher = MockMatcher(obj)
self.__class__._all_expectations.append(matcher)
return matcher


def __expect_none(self):
return self.is_expected.exactly(0).times
def __expect_call_matcher(self):
return self.__expect_call_on(self)
is_expected = property(__expect_call_matcher)
is_not_expected = property(__expect_none)

def expects(self, methodname):
return self.__expect_call_on(self.__wrapped_child(methodname))
Expand Down
2 changes: 2 additions & 0 deletions mocktest/stub.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
__all__ = ['stub']

from mockwrapper import MockWrapper
def stub(name=None):
"""create a new stub - a frozen mock with no methods or children"""
Expand Down
6 changes: 3 additions & 3 deletions test/mock_anchor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys

import helper
from mocktest import TestCase, pending
from mocktest import TestCase, pending, core
from mocktest import mock_on, raw_mock, mock
import mocktest
mock_class = mocktest.silentmock.SilentMock
Expand All @@ -25,8 +25,8 @@ def __delattr__(self, attr):

class MockAnchorTest(TestCase):
def downup(self):
mocktest._teardown()
mocktest._setup()
core._teardown()
core._setup()

def test_should_attach_new_mocks_to_parent(self):
mock_on(real_object).a
Expand Down
15 changes: 15 additions & 0 deletions test/mockmatcher_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,21 @@ def test_should_allow_argument_checking_callbacks(self):
self.assertTrue(wrapper_.child('foo').called.twice().where_args(lambda *args: all([x < 3 for x in args])))
self.assertTrue(wrapper_.child('foo').called.exactly(4).times)

def test_should_allow_matchers_for_argument_specs(self):
wrapper_ = mock()
mock_ = wrapper_.raw

mock_.raw(foo='hjdsfhsdfds')

class WithAttribute_foo(object):
def foo(self):
pass

mock_.raw(WithAttribute_foo(), 1)

self.assertTrue(mock_.called.once().with_(any_string))
self.assertTrue(mock_.called.once().with_(object_with('foo')))

def test_should_return_arguments_for_a_subset_of_calls_given_conditions(self):
wrapper_ = mock()
mock_ = wrapper_.raw
Expand Down
59 changes: 27 additions & 32 deletions test/mocktest_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@ def __str__(self):
class TestAutoSpecVerification(unittest.TestCase):

def setUp(self):
mocktest._setup()
core._setup()
self.stderr = mock().named('stderr')
sys.stderr = self.stderr.raw
self.output = mock(sys.stderr.write)
print "all expectations is %r" % (core.MockWrapper._all_expectations,)

def tearDown(self):
print "all expectations is %r" % (core.MockWrapper._all_expectations,)
mocktest._teardown()
core._teardown()

def run_suite(self, class_):
mocktest._teardown()
core._teardown()
suite = unittest.makeSuite(class_)
result = unittest.TestResult()
suite.run(result)
ret = result
mocktest._setup()
core._setup()
return result

def run_method(self, test_func):
Expand All @@ -56,22 +56,24 @@ def test_should_hijack_setup_and_teardown(self):
lines = []
def capture(line):
lines.append(line)
backup_setup = mocktest._setup
backup_teardown = mocktest._teardown
backup_setup = core._setup
backup_teardown = core._teardown

core._setup = mock().with_action(lambda: capture("_setup")).raw
core._teardown = mock().with_action(lambda: capture("_teardown")).raw

class Foo(mocktest.TestCase):
def setUp(self):
print "SETUP"
capture("setup")
def tearDown(self):
capture("teardown")
def test_main_is_called(self):
capture("main")

result = self.run_suite(Foo)
suite = unittest.makeSuite(Foo)
result = unittest.TestResult()
suite.run(result)

self.assertTrue(result.wasSuccessful())
self.assertEqual(lines, ['_setup', 'setup', 'main', '_teardown', 'teardown'])

Expand Down Expand Up @@ -141,7 +143,7 @@ def test_failed_pending(self):
print "cant find SkipTest, so this test case won't work"

def test_invalid_usage_after_teardown(self):
mocktest._teardown()
core._teardown()

e = None
try:
Expand All @@ -152,7 +154,7 @@ def test_invalid_usage_after_teardown(self):
self.assertFalse(e is None, "no exception was raised")
self.assertEqual(e.__class__, RuntimeError)
self.assertEqual(e.message, "MockWrapper._setup has not been called. Make sure you are inheriting from mocktest.TestCase, not unittest.TestCase")
mocktest._setup()
core._setup()

def test_expectation_errors_should_be_failures(self):
class myTest(mocktest.TestCase):
Expand Down Expand Up @@ -272,24 +274,6 @@ def test_raise_match(s):
result = self.run_method(test_raise_match)
self.assertTrue(result.wasSuccessful())

def test_assert_true_fails_on_callables(self):
def assert_truth(s):
s.assertTrue(lambda x: True)
result = self.run_method(assert_truth)
self.assertFalse(result.wasSuccessful())

def test_assert_false_fails_on_callables(self):
def assert_falseth(s):
s.assertFalse(lambda x: False)
result = self.run_method(assert_falseth)
self.assertFalse(result.wasSuccessful())

def test_assert__fails_on_callables(self):
def assert_assert_(s):
s.assert_(lambda x: True)
result = self.run_method(assert_assert_)
self.assertFalse(result.wasSuccessful())

def test_assert_raises_verifies_type(self):
def test_raise_mismatch_type(s):
s.assertRaises(TypeError, self.make_error)
Expand Down Expand Up @@ -342,7 +326,18 @@ def test_expect_should_work_on_a_mock_or_wrapper(self):
mock_.a()
mock_.b()
mock_.c()


def test_is_not_expected(self):
wrapper = mock()
mock_ = wrapper.raw

expect(mock_.a).once()
wrapper.child('b').is_not_expected
mock_.a()

core._teardown()
core._setup()


def test_reality_formatting(self):
m = mock().named('ze_mock').raw
Expand Down Expand Up @@ -393,9 +388,9 @@ def failing_mock_expectation(slf):
mocktest.mock().is_expected
# emulate a refresh
try:
mocktest._teardown()
core._teardown()
finally:
mocktest._setup()
core._setup()
ensure_no_mocktest_files_appear_in_failure(failing_mock_expectation)


Expand All @@ -406,7 +401,7 @@ def test_should_do_expectations(self):
f.expects('foo').once()
f.raw.foo('a')
f.raw.foo()
self.assertRaises(AssertionError, mocktest._teardown, matching=re.compile('Mock "foo" .*expected exactly 1 calls.* received 2 calls.*', re.DOTALL))
self.assertRaises(AssertionError, core._teardown, matching=re.compile('Mock "foo" .*expected exactly 1 calls.* received 2 calls.*', re.DOTALL))

# pretend we're in a new test (wipe the expected calls register)
core.MockWrapper._all_expectations = []
Expand Down

0 comments on commit 97c3bcc

Please sign in to comment.