Skip to content

Commit

Permalink
added basic structure to acomodate shouldify idea (see pull request #15)
Browse files Browse the repository at this point in the history
  • Loading branch information
hltbra committed Jul 10, 2011
1 parent b23389e commit beb7c80
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
PYTHON=python

test:
$(PYTHON) setup.py test
$(PYTHON) run_all_examples.py
specloud

tox:
@python -c 'import tox' 2>/dev/null || pip install tox
Expand Down
10 changes: 10 additions & 0 deletions should_dsl/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class ShouldDSLApi(object):

def __init__(self):
self.matchers = {}

def add_matcher(self, matcher_name, matcher):
self.matchers[matcher_name] = matcher

def find_matcher(self, matcher_name):
return self.matchers[matcher_name]
Binary file added should_dsl/specs/.__init__.py.swp
Binary file not shown.
Binary file added should_dsl/specs/.api_specs.py.swp
Binary file not shown.
Binary file added should_dsl/specs/.subject_specs.py.swp
Binary file not shown.
20 changes: 20 additions & 0 deletions should_dsl/specs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Mock(object):

called = False
args = ()
kw = {}
return_value = None

def __init__(self, return_value=None):
self.return_value = return_value

def __call__(self, *args, **kw):
self._args = args
self._kw = kw
self.called = True
return self.return_value

def called_with(self, args, kw):
return self._args == args and self._kw == kw


22 changes: 22 additions & 0 deletions should_dsl/specs/api_specs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import unittest
from should_dsl.api import ShouldDSLApi
from should_dsl.specs import Mock


class ShouldDSLApiSpec(unittest.TestCase):

def setUp(self):
self.api = ShouldDSLApi()

def test_matchers_should_start_empty(self):
self.assertEquals({}, self.api.matchers)

def test_should_be_possible_to_add_matchers(self):
my_fake_matcher = Mock()
self.api.add_matcher('my_fake_matcher', my_fake_matcher)
self.assertEquals({'my_fake_matcher': my_fake_matcher}, self.api.matchers)

def test_should_be_possible_to_find_matcher_by_name(self):
my_fake_matcher = Mock()
self.api.add_matcher('my_fake_matcher', my_fake_matcher)
self.assertEquals(my_fake_matcher, self.api.find_matcher('my_fake_matcher'))
33 changes: 33 additions & 0 deletions should_dsl/specs/subject_specs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import unittest
from should_dsl.api import ShouldDSLApi
from should_dsl.subject import Subject
from should_dsl.specs import Mock


class SubjectSpec(unittest.TestCase):

def setUp(self):
self.api = ShouldDSLApi()
self.subject = Subject('foo', self.api)

def test_should_have_method_should(self):
self.assertTrue(hasattr(self.subject, 'should'))
self.assertTrue(hasattr(self.subject.should, '__call__'))

def test_should_method_should_call_find_matcher_on_api(self):
matcher_mock = Mock()
matcher_mock.match = Mock()
self.api.find_matcher = Mock(return_value=matcher_mock)

self.subject.should(equal_to='foo')
self.assertTrue(self.api.find_matcher.called)
self.assertTrue(self.api.find_matcher.called_with(('equal_to',), {}))

def test_should_method_should_call_match_method_on_matcher_found(self):
matcher_mock = Mock()
matcher_mock.match = Mock(return_value='result')
self.api.find_matcher = Mock(return_value=matcher_mock)

self.assertEqual('result', self.subject.should(equal_to='foo'))
self.assertTrue(matcher_mock.match.called)
self.assertTrue(matcher_mock.match.called_with(('foo',), {}))
11 changes: 11 additions & 0 deletions should_dsl/subject.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Subject(object):

def __init__(self, obj, api):
self._api = api
self._obj = obj

def should(self, **kwargs):
matcher_name = kwargs.keys()[0]
matcher_args = kwargs[matcher_name]
matcher = self._api.find_matcher(matcher_name)
return matcher.match(matcher_args)

0 comments on commit beb7c80

Please sign in to comment.