Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

chai 0.2.1
  • Loading branch information...
commit e8d8528b54a82e7458a2821c0066f0fd8e7b5a24 1 parent 605c258
@awestendorf awestendorf authored
View
5 CHANGELOG
@@ -1,3 +1,8 @@
+0.2.1
+=====
+
+* Allow variables in returns(), effectively treating the variable as a regex capture.
+
0.2.0
=====
View
6 README.rst
@@ -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
4 chai/expectation.py
@@ -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
3  requirements.txt
@@ -1 +1,2 @@
-termcolor
+termcolor
+unittest2
View
2  setup.py
@@ -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'],
View
22 tests/expectation_test.py
@@ -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,7 +252,7 @@ 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)
@@ -242,7 +260,7 @@ class CustomException(Exception): pass
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)
Please sign in to comment.
Something went wrong with that request. Please try again.