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)
def test2_update(self): td1 = ArgSpecGiven(dict()) td2 = ArgSpecGiven() td4 = ArgSpecGiven() td5 = ArgSpecGiven(argsDocs=dict(arg1='doc for arg1', arg2='doc for arg2', arg3='doc for arg3', arg4='doc for arg4'), reqdArgs=('arg4', 'arg2')) ai1 = ArgsInfo(('t1', ), td1, None) # root, complete ai2 = ArgsInfo(('t1', 'st1'), td2, ai1) # sub 1, empty ai4 = ArgsInfo(('t1', 'st1', 'sst2'), td4, ai2) # empty sub of sub 1 ai5 = ArgsInfo(('t1', 'st1', 'sst3'), td5, ai2) # completed sub of sub 1 # check assumptions before we start: assert not ai2.isComplete() assert not ai4.isComplete() assert ai5.isComplete() assert ai2.numArgs() == 0 assert ai4.numArgs() == 0 assert ai5.numArgs() == 4 # pretend we have an update for ai2: all args now available ai2.updateAllArgsFinal( ArgSpecGiven(dict(arg1='doc for arg1', arg2='doc for arg2'), ('arg2', ))) assert ai2.isComplete() assert ai2.numArgs() == 2 assert ai2.hasSameArgs('arg1', 'arg2') assert ai2.getCompleteAI() is ai2 assert not ai4.isComplete() assert ai2.numArgs() == 2 assert ai4.numArgs() == 0 assert ai5.numArgs() == 4 assert ai4.getCompleteAI() is ai2 assert ai2.hasSameArgs('arg1', 'arg2') assert ai5.hasSameArgs('arg1', 'arg2', 'arg3', 'arg4')
def test1_create(self): # root td1 = ArgSpecGiven(dict()) ai1 = ArgsInfo(('t1', ), td1, None) assert ai1.isComplete() assert ai1.numArgs() == 0 assert ai1.getArgs() == () assert ai1.getCompleteAI() is ai1 # sub, complete td2 = ArgSpecGiven(argsDocs=dict(arg1='doc for arg1', arg2='doc for arg2'), reqdArgs=('arg2', )) ai2 = ArgsInfo(('t1', 'st1'), td2, ai1) assert ai2.isComplete() assert ai2.numArgs() == 2 assert ai2.getArgs() == ('arg1', 'arg2') assert ai2.getCompleteAI() is ai2 # sub, missing td2.argsSpecType = ArgSpecGiven.SPEC_GIVEN_NONE ai4 = ArgsInfo(('t1', 'st3'), td2, ai1) assert not ai4.isComplete() assert ai4.numArgs() == 0 assert ai4.getArgs() == () assert ai4.getCompleteAI() is ai1 # sub, of incomplete spec, given ALL args td3 = ArgSpecGiven(argsDocs=dict(arg1='doc for arg1', arg2='doc for arg2'), reqdArgs=('arg2', )) ai5 = ArgsInfo(('t1', 'st3', 'sst1'), td3, ai4) assert ai5.isComplete() assert ai5.numArgs() == 2 assert ai5.hasSameArgs('arg1', 'arg2') assert ai5.getCompleteAI() is ai5
def test1_create(self): # root td1 = ArgSpecGiven(dict()) ai1 = ArgsInfo(('t1',), td1, None) assert ai1.isComplete() assert ai1.numArgs() == 0 assert ai1.getArgs() == () assert ai1.getCompleteAI() is ai1 # sub, complete td2 = ArgSpecGiven( argsDocs = dict(arg1='doc for arg1', arg2='doc for arg2'), reqdArgs = ('arg2',)) ai2 = ArgsInfo(('t1','st1'), td2, ai1) assert ai2.isComplete() assert ai2.numArgs() == 2 assert ai2.getArgs() == ('arg1', 'arg2') assert ai2.getCompleteAI() is ai2 # sub, missing td2.argsSpecType = ArgSpecGiven.SPEC_GIVEN_NONE ai4 = ArgsInfo(('t1','st3'), td2, ai1) assert not ai4.isComplete() assert ai4.numArgs() == 0 assert ai4.getArgs() == () assert ai4.getCompleteAI() is ai1 # sub, of incomplete spec, given ALL args td3 = ArgSpecGiven( argsDocs = dict(arg1='doc for arg1', arg2='doc for arg2'), reqdArgs = ('arg2',)) ai5 = ArgsInfo(('t1','st3','sst1'), td3, ai4) assert ai5.isComplete() assert ai5.numArgs() == 2 assert ai5.hasSameArgs('arg1', 'arg2') assert ai5.getCompleteAI() is ai5
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() nameTuple = ('childOfAll', ) description = 'child description' argsDocs = dict(arg1='arg1 desc', arg2='arg2 desc') reqdArgs = ('arg2', ) argSpec = ArgSpecGiven(argsDocs=argsDocs, reqdArgs=reqdArgs) msgArgsInfo = ArgsInfo(nameTuple, argSpec, self.rootTopic._getListenerSpec()) 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(MessageDataSpecError, tryCreate, argsDocs, reqdArgs) reqdArgs = () self.assertRaises(MessageDataSpecError, tryCreate, argsDocs, reqdArgs) # test when missing opt arg argsDocs = dict(arg1='arg1 desc', arg2='arg2 desc') reqdArgs = ('arg2', )
def test0_CreateRoot(self): # # 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(): pass def badListener1(arg1): pass # extra required arg def badListener2(arg1=None): pass # extra is optional assert obj.isValid(listener1) assert not obj.isValid(badListener1) assert not obj.isValid(badListener2) self.rootTopic = obj
def test3_filter(self): td = ArgSpecGiven(argsDocs=dict(arg1='doc for arg1', arg2='doc for arg2'), reqdArgs=('arg2', )) ai = ArgsInfo(('t1', ), td, None) # check: argsMissingReqd = {} self.assertRaises(SenderMissingReqdArgs, ai.check, argsMissingReqd) argsExtraOpt = dict(arg2=2, arg5=5) self.assertRaises(SenderUnknownOptArgs, ai.check, argsExtraOpt) args = dict(arg1=1, arg2=2) ai.check(args) # filter: msgArgs = dict(arg1=1, arg2=2) argsOK = msgArgs.copy() assert ai.filterArgs(msgArgs) == argsOK msgArgs.update(arg3=3, arg4=4) assert ai.filterArgs(msgArgs) == argsOK