-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added basic structure to acomodate shouldify idea (see pull request #15)
- Loading branch information
Showing
9 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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',), {})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |