Skip to content

Commit beb7c80

Browse files
committed
added basic structure to acomodate shouldify idea (see pull request #15)
1 parent b23389e commit beb7c80

File tree

9 files changed

+98
-1
lines changed

9 files changed

+98
-1
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
PYTHON=python
22

33
test:
4-
$(PYTHON) setup.py test
4+
$(PYTHON) run_all_examples.py
5+
specloud
56

67
tox:
78
@python -c 'import tox' 2>/dev/null || pip install tox

should_dsl/api.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class ShouldDSLApi(object):
2+
3+
def __init__(self):
4+
self.matchers = {}
5+
6+
def add_matcher(self, matcher_name, matcher):
7+
self.matchers[matcher_name] = matcher
8+
9+
def find_matcher(self, matcher_name):
10+
return self.matchers[matcher_name]

should_dsl/specs/.__init__.py.swp

12 KB
Binary file not shown.

should_dsl/specs/.api_specs.py.swp

12 KB
Binary file not shown.
12 KB
Binary file not shown.

should_dsl/specs/__init__.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Mock(object):
2+
3+
called = False
4+
args = ()
5+
kw = {}
6+
return_value = None
7+
8+
def __init__(self, return_value=None):
9+
self.return_value = return_value
10+
11+
def __call__(self, *args, **kw):
12+
self._args = args
13+
self._kw = kw
14+
self.called = True
15+
return self.return_value
16+
17+
def called_with(self, args, kw):
18+
return self._args == args and self._kw == kw
19+
20+

should_dsl/specs/api_specs.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import unittest
2+
from should_dsl.api import ShouldDSLApi
3+
from should_dsl.specs import Mock
4+
5+
6+
class ShouldDSLApiSpec(unittest.TestCase):
7+
8+
def setUp(self):
9+
self.api = ShouldDSLApi()
10+
11+
def test_matchers_should_start_empty(self):
12+
self.assertEquals({}, self.api.matchers)
13+
14+
def test_should_be_possible_to_add_matchers(self):
15+
my_fake_matcher = Mock()
16+
self.api.add_matcher('my_fake_matcher', my_fake_matcher)
17+
self.assertEquals({'my_fake_matcher': my_fake_matcher}, self.api.matchers)
18+
19+
def test_should_be_possible_to_find_matcher_by_name(self):
20+
my_fake_matcher = Mock()
21+
self.api.add_matcher('my_fake_matcher', my_fake_matcher)
22+
self.assertEquals(my_fake_matcher, self.api.find_matcher('my_fake_matcher'))

should_dsl/specs/subject_specs.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import unittest
2+
from should_dsl.api import ShouldDSLApi
3+
from should_dsl.subject import Subject
4+
from should_dsl.specs import Mock
5+
6+
7+
class SubjectSpec(unittest.TestCase):
8+
9+
def setUp(self):
10+
self.api = ShouldDSLApi()
11+
self.subject = Subject('foo', self.api)
12+
13+
def test_should_have_method_should(self):
14+
self.assertTrue(hasattr(self.subject, 'should'))
15+
self.assertTrue(hasattr(self.subject.should, '__call__'))
16+
17+
def test_should_method_should_call_find_matcher_on_api(self):
18+
matcher_mock = Mock()
19+
matcher_mock.match = Mock()
20+
self.api.find_matcher = Mock(return_value=matcher_mock)
21+
22+
self.subject.should(equal_to='foo')
23+
self.assertTrue(self.api.find_matcher.called)
24+
self.assertTrue(self.api.find_matcher.called_with(('equal_to',), {}))
25+
26+
def test_should_method_should_call_match_method_on_matcher_found(self):
27+
matcher_mock = Mock()
28+
matcher_mock.match = Mock(return_value='result')
29+
self.api.find_matcher = Mock(return_value=matcher_mock)
30+
31+
self.assertEqual('result', self.subject.should(equal_to='foo'))
32+
self.assertTrue(matcher_mock.match.called)
33+
self.assertTrue(matcher_mock.match.called_with(('foo',), {}))

should_dsl/subject.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Subject(object):
2+
3+
def __init__(self, obj, api):
4+
self._api = api
5+
self._obj = obj
6+
7+
def should(self, **kwargs):
8+
matcher_name = kwargs.keys()[0]
9+
matcher_args = kwargs[matcher_name]
10+
matcher = self._api.find_matcher(matcher_name)
11+
return matcher.match(matcher_args)

0 commit comments

Comments
 (0)