PR 68: update unit tests for pubsub

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxPython/Phoenix/trunk@76020 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robin Dunn
2014-02-26 03:53:59 +00:00
parent fe2d0b3a8b
commit 207c1f294c
13 changed files with 458 additions and 444 deletions

View File

@@ -13,8 +13,8 @@ from wx.lib.pubsub.core.topicobj import Topic
from wx.lib.pubsub.core.treeconfig import TreeConfig
from wx.lib.pubsub.core.topicutils import ALL_TOPICS
from wx.lib.pubsub.core.topicargspec import ArgsInfo, ArgSpecGiven
#from wx.lib.pubsub.core.listener import ListenerInadequate
#from wx.lib.pubsub.core.topicexc import ListenerSpecInvalid
from wx.lib.pubsub.core.listener import ListenerMismatchError
from wx.lib.pubsub.core.topicexc import MessageDataSpecError
@@ -30,26 +30,26 @@ class lib_pubsub_Topic(wtc.PubsubTestCase):
#
# Test create and then modify state of a topic object
#
nameTuple = ('root',)
description = 'root description'
msgArgsInfo = None
# when parent is None, only nameTuple=ALL_TOPICS is allowed, thereby
# guaranteeing that only one tree root can be created
self.assertRaises(ValueError, Topic, self.treeConfig, nameTuple, description, msgArgsInfo)
# create the ALL TOPICS topic; it has no message args
nameTuple = (ALL_TOPICS,)
argSpec = ArgSpecGiven(dict() )
msgArgsInfo = ArgsInfo(nameTuple, argSpec, None)
obj = Topic(self.treeConfig, nameTuple, description, msgArgsInfo)
# verify its state is as expected after creation:
assert obj.getListeners() == []
assert obj.getNumListeners() == 0
assert obj.hasListeners() == False
def listener1():
pass
def listener2():
@@ -61,15 +61,15 @@ class lib_pubsub_Topic(wtc.PubsubTestCase):
assert obj.isValid(listener1)
assert not obj.isValid(badListener1)
assert not obj.isValid(badListener2)
self.rootTopic = obj
def test1_SubUnsub(self):
#
# Test subscription and unsubscription of listeners
#
def listener1():
pass
def listener2():
@@ -77,28 +77,28 @@ class lib_pubsub_Topic(wtc.PubsubTestCase):
# need to run this here again to get rootTopic setup for this test
self.test0_CreateRoot()
obj = self.rootTopic
# now modify its state by subscribing listeners
obj.subscribe(listener1)
obj.subscribe(listener2)
obj.hasListener(listener1)
obj.hasListener(listener2)
assert obj.hasListeners() == True
assert set(obj.getListeners()) == set([listener1, listener2])
assert obj.getNumListeners() == 2
# try to subscribe an invalid listener
def badListener(arg1):
pass # extra required arg
self.assertRaises(ListenerInadequate, obj.subscribe, badListener)
self.assertRaises(ListenerMismatchError, obj.subscribe, badListener)
# try unsubscribe
obj.unsubscribe(listener1)
assert obj.hasListeners() == True
assert obj.getListeners() == [listener2]
assert obj.getNumListeners() == 1
# try unsubscribe all, with filtering
obj.subscribe(listener1)
def listener3(): pass
@@ -116,13 +116,13 @@ class lib_pubsub_Topic(wtc.PubsubTestCase):
assert obj.getNumListeners() == 3
obj.unsubscribeAllListeners()
assert obj.getNumListeners() == 0
def test2_CreateChild(self):
#
# Test creation of a child topic, subscription of listeners
#
# need to run this here again to get rootTopic setup for this test
self.test0_CreateRoot()
@@ -135,30 +135,30 @@ class lib_pubsub_Topic(wtc.PubsubTestCase):
parent = Topic(self.treeConfig, nameTuple, description, msgArgsInfo,
parent=self.rootTopic)
assert parent.getParent() is self.rootTopic
# now create a child of child with wrong arguments so we can test exceptions
nameTuple = ('childOfAll', 'grandChild')
description = 'grandchild description'
def tryCreate(ad, r):
argSpec = ArgSpecGiven(argsDocs=ad, reqdArgs = r)
msgArgsInfo = ArgsInfo(nameTuple, argSpec, parent._getListenerSpec())
obj = Topic(self.treeConfig, nameTuple, description, msgArgsInfo,
parent=parent)
# test when all OK
argsDocs = dict(arg1='arg1 desc', arg2='arg2 desc')
reqdArgs = ('arg2',)
tryCreate(argsDocs, reqdArgs)
# test when requiredArg wrong
reqdArgs = ('arg3',)
self.assertRaises(ListenerSpecInvalid, tryCreate, argsDocs, reqdArgs)
self.assertRaises(MessageDataSpecError, tryCreate, argsDocs, reqdArgs)
reqdArgs = ()
self.assertRaises(ListenerSpecInvalid, tryCreate, argsDocs, reqdArgs)
self.assertRaises(MessageDataSpecError, tryCreate, argsDocs, reqdArgs)
# test when missing opt arg
argsDocs = dict(arg1='arg1 desc', arg2='arg2 desc')
reqdArgs = ('arg2',)
#---------------------------------------------------------------------------