Permalink
Browse files

Allow returning a variable so as to perform a regex-like capture.

chai 0.2.1
  • Loading branch information...
1 parent 605c258 commit e8d8528b54a82e7458a2821c0066f0fd8e7b5a24 @awestendorf awestendorf committed Sep 6, 2012
Showing with 35 additions and 7 deletions.
  1. +5 −0 CHANGELOG
  2. +4 −2 README.rst
  3. +3 −1 chai/expectation.py
  4. +2 −1 requirements.txt
  5. +1 −1 setup.py
  6. +20 −2 tests/expectation_test.py
View
@@ -1,3 +1,8 @@
+0.2.1
+=====
+
+* Allow variables in returns(), effectively treating the variable as a regex capture.
+
0.2.0
=====
View
@@ -2,7 +2,7 @@
Chai - Python Mocking Made Easy
=================================
-:Version: 0.2.0
+:Version: 0.2.1
:Download: http://pypi.python.org/pypi/chai
:Source: https://github.com/agoragames/chai
:Keywords: python, mocking, testing, unittest, unittest2
@@ -291,7 +291,9 @@ like(container)
Matches if the argument contains all of the same items as in ``container``. Insists that the argument is the same type as ``container``. Useful when you need to assert a few values in a list or dictionary, but the exact contents are not known or can vary.
var(name)
- A variable match against the first time that the argument is called. In the case of multiple calls, the second one must match the previous value of ``name``. After your tests have run, you can check the value against expected arguments through ``var(name).value``. This is really useful when you're testing a deep stack and it's simpler to assert that "value A was used in method call X".
+ A variable match against the first time that the argument is called. In the case of multiple calls, the second one must match the previous value of ``name``. After your tests have run, you can check the value against expected arguments through ``var(name).value``. This is really useful when you're testing a deep stack and it's simpler to assert that "value A was used in method call X". Variables can also be used to capture an argument and return it. ::
+
+ expect( encode ).args( var('src'), 'gzip' ).returns( var('src') )
**A note of caution**
View
@@ -191,7 +191,9 @@ def return_value(self):
else:
raise self._raises
else:
- return self._returns
+ if isinstance(self._returns, tuple):
+ return tuple([x.value if isinstance(x,Variable) else x for x in self._returns])
+ return self._returns.value if isinstance(self._returns,Variable) else self._returns
def close(self, *args, **kwargs):
'''
View
@@ -1 +1,2 @@
-termcolor
+termcolor
+unittest2
View
@@ -8,7 +8,7 @@
setup(
name='chai',
- version='0.2.0',
+ version='0.2.1',
author='Vitaly Babiy, Aaron Westendorf',
author_email="vbabiy@agoragames.com, aaron@agoragames.com",
packages=['chai'],
@@ -220,6 +220,24 @@ def test_return_value_with_value(self):
self.assertEquals(exp.test(), 123)
+ def test_return_value_with_variable(self):
+ exp = Expectation(object)
+ var = Variable('test')
+ Variable._cache['test'] = 123
+ exp.returns( var )
+
+ self.assertEquals(exp.test(), 123)
+ Variable.clear()
+
+ def test_return_value_with_variable_in_tuple(self):
+ exp = Expectation(object)
+ var = Variable('test')
+ Variable._cache['test'] = 123
+ exp.returns( (var,'foo') )
+
+ self.assertEquals(exp.test(), (123,'foo'))
+ Variable.clear()
+
def test_with_returns_return_value(self):
exp = Expectation(object)
with exp.returns(123) as num:
@@ -234,15 +252,15 @@ def foo():
raise Exception("FAIL!")
self.assertRaises(Exception, foo)
- def test_return_value_with_expection_class(self):
+ def test_return_value_with_exception_class(self):
class CustomException(Exception): pass
exp = Expectation(object)
self.assertEquals( exp, exp.raises(CustomException) )
self.assertRaises(CustomException, exp.test)
- def test_return_value_with_expection_instance(self):
+ def test_return_value_with_exception_instance(self):
class CustomException(Exception): pass
exp = Expectation(object)

0 comments on commit e8d8528

Please sign in to comment.