tests for od and d.

This commit is contained in:
percious 2010-03-13 01:52:06 -05:00
parent a5ac03ec98
commit fa27c40561
6 changed files with 55 additions and 22 deletions

View File

@ -16,9 +16,6 @@ class which provides the ordinary TurboGears mechanism.
"""
from urllib import url2pathname
from inspect import ismethod, isclass, getargspec
class Dispatcher(object):
"""
Extend this class to define your own mechanism for dispatch.
@ -37,5 +34,8 @@ class Dispatcher(object):
"""
def _setup_wsgi_script_name(self, url_path, remainder, params):
pass
"""
This is expected to be overridden by any subclass that wants to set
the script name.
"""

View File

@ -10,7 +10,7 @@ class DispatchState(object):
us to attach things like routing args and to keep track of the
path the controller takes along the system.
"""
def __init__(self, request, params=None):
def __init__(self, request, dispatcher, params=None):
self.request = request
self.url_path = request.path_info
@ -23,7 +23,8 @@ class DispatchState(object):
self.routing_args = {}
self.method = None
self.remainder = None
self.dispatcher = None
self.dispatcher = dispatcher
self.add_controller('/', dispatcher)
def add_controller(self, location, controller):
"""Add a controller object to the stack"""

View File

@ -1,19 +1,8 @@
"""
This is the main dispatcher module.
Dispatch works as follows:
Start at the RootController, the root controller must
have a _dispatch function, which defines how we move
from object to object in the system.
Continue following the dispatch mechanism for a given
controller until you reach another controller with a
_dispatch method defined. Use the new _dispatch
method until anther controller with _dispatch defined
or until the url has been traversed to entirety.
This module also contains the standard ObjectDispatch
class which provides the ordinary TurboGears mechanism.
Utilities used by crank.
Copyright (c) Chrispther Perkins
MIT License
"""
class odict(dict):

View File

@ -11,13 +11,14 @@ class TestDispatchState:
def setup(self):
self.request = MockRequest()
self.state = DispatchState(self.request, {'a':1, 'b':2})
self.dispatcher = MockController()
self.state = DispatchState(self.request, self.dispatcher, {'a':1, 'b':2})
def test_create(self):
assert self.state.params == {'a':1, 'b':2}, self.state.params
def test_create_params_in_request(self):
state = DispatchState(self.request)
state = DispatchState(self.request, self.dispatcher)
assert state.params == {'c':3, 'd':4}, state.params
def test_add_controller(self):

15
tests/test_dispatcher.py Normal file
View File

@ -0,0 +1,15 @@
from nose.tools import raises
from crank.dispatcher import *
class TestDispatcher:
def setup(self):
self.dispatcher = Dispatcher()
def test_create(self):
pass
@raises(NotImplementedError)
def test_dispatch(self):
self.dispatcher._dispatch(1,2)

View File

@ -0,0 +1,27 @@
from nose.tools import raises
from crank.objectdispatcher import *
from crank.dispatchstate import DispatchState
class MockRequest(object):
path_info = 'something'
params = {'c':3, 'd':4}
class MockDispatcher(ObjectDispatcher):
def index(self, *args, **kw):
return 'here'
mock_request = MockRequest()
class TestDispatcher:
def setup(self):
self.dispatcher = MockDispatcher()
def test_create(self):
pass
def test_dispatch(self):
state = DispatchState(mock_request, self.dispatcher)
state = self.dispatcher._dispatch(state, [])
assert state.method.__name__ == 'index', state.method