#! /usr/bin/env python
# -*- coding: iso-8859-1 -*-
#-----------------------------------------------------------------------------
# Modeling Framework: an Object-Relational Bridge for python
#
# Copyright (c) 2001-2004 Sbastien Bigaret <sbigaret@users.sourceforge.net>
# All rights reserved.
#
# This file is part of the Modeling Framework.
#
# This code is distributed under a "3-clause BSD"-style license;
# see the LICENSE file for details.
#-----------------------------------------------------------------------------
"""
Tests for module delegation
CVS Information
$Id: test_delegation.py 937 2004-08-02 20:54:47Z sbigaret $
"""
__version__= '$Revision: 937 $'[11:-2]
import unittest
if __name__ == "__main__":
import utils, sys
utils.fixpath()
from Modeling.delegation import DelegateWrapper
# Classes for tests
class DelegateInterface:
"""
"""
def canTheWorldBeChanged(self):
"Indicates whether the world can be changed"
def shouldBeReset(self):
"-"
def willBeReset(self):
"-"
# def respondsTo(self):
# "-"
class Delegate:
def __init__(self):
self.resetCounter=0
def canTheWorldBeChanged(self, isElvisAlive=1):
"This method does NOT conform to the DelegateInterface"
return isElvisAlive and 'Ouaip' or 'Nope!'
def willBeReset(self):
"Side-effect: Increments self.resetCounter by one"
self.resetCounter+=1
def shouldBeReset(self):
"Always return true"
return 1
## Tests begin here
class TestDelegate(unittest.TestCase):
"Tests for module delegate"
def setUp(self):
"setUp"
self.delegateObject=Delegate()
self.delegate=DelegateWrapper(DelegateInterface, self.delegateObject)
def test_01_respondsTo(self):
"[delegate] respondsTo"
self.failUnless(self.delegate.respondsTo('shouldBeReset'))
self.failUnless(self.delegate.respondsTo('willBeReset'))
self.failIf(self.delegate.respondsTo('canTheWorldBeChanged'))
self.failIf(self.delegate.respondsTo('MinistryOfSillyWalksPostalAddress'))
def test_01_proxyForDelegateInterfaceMethods(self):
"[delegate] checks the DelegateWrapper's proxying mechanism"
self.failUnless(self.delegate.shouldBeReset()==1)
self.failIf(self.delegate.willBeReset()) # return None
self.failUnless(self.delegate.delegateObject().resetCounter==1)
try:
self.delegate.canTheWorldBeChanged(1)
except AttributeError:
pass
else:
self.fail('self.delegate.canTheWorldBeChanged should have raised')
# No automatic access to variable
self.assertRaises(AttributeError, getattr, self.delegate, 'resetCounter')
# Build the test suite
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestDelegate, "test_"))
return suite
if __name__ == "__main__":
errs = utils.run_suite(test_suite())
sys.exit(errs and 1 or 0)
|