2008-09-16 06:26:17 +00:00
|
|
|
"""
|
|
|
|
|
@file test_template_parser.py
|
|
|
|
|
@date 2008-09-16
|
|
|
|
|
Contributors can be viewed at:
|
|
|
|
|
http://svn.secondlife.com/svn/linden/projects/2008/pyogp/CONTRIBUTORS.txt
|
|
|
|
|
|
|
|
|
|
$LicenseInfo:firstyear=2008&license=apachev2$
|
|
|
|
|
|
|
|
|
|
Copyright 2008, Linden Research, Inc.
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License").
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
or in
|
|
|
|
|
http://svn.secondlife.com/svn/linden/projects/2008/pyogp/LICENSE.txt
|
|
|
|
|
|
|
|
|
|
$/LicenseInfo$
|
|
|
|
|
"""
|
|
|
|
|
|
2008-07-21 18:18:39 +00:00
|
|
|
#standard libraries
|
|
|
|
|
import unittest, doctest
|
|
|
|
|
|
|
|
|
|
#local libraries
|
2008-09-03 22:55:07 +00:00
|
|
|
from pyogp.lib.base.message.data import msg_tmpl
|
|
|
|
|
from pyogp.lib.base.message.template import MessageTemplate, MessageTemplateBlock, MessageTemplateVariable
|
|
|
|
|
from pyogp.lib.base.message.template_dict import TemplateDictionary
|
|
|
|
|
from pyogp.lib.base.message.template_parser import MessageTemplateParser
|
|
|
|
|
from pyogp.lib.base.message.types import MsgFrequency, MsgTrust, MsgEncoding, MsgDeprecation, MsgBlockType, MsgType
|
2008-07-21 18:18:39 +00:00
|
|
|
|
2008-07-21 21:12:23 +00:00
|
|
|
class TestDictionary(unittest.TestCase):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.template_file = msg_tmpl
|
2008-07-22 13:33:35 +00:00
|
|
|
parser = MessageTemplateParser(self.template_file)
|
2008-07-23 19:38:37 +00:00
|
|
|
self.template_list = parser.message_templates
|
2008-07-21 21:12:23 +00:00
|
|
|
|
2008-07-22 13:33:35 +00:00
|
|
|
def tearDown(self):
|
|
|
|
|
pass
|
2009-03-03 01:40:52 +00:00
|
|
|
|
2008-07-21 21:12:23 +00:00
|
|
|
def test_create_dictionary(self):
|
|
|
|
|
try:
|
|
|
|
|
msg_dict = TemplateDictionary(None)
|
|
|
|
|
assert False, "Template dictionary fail case list==None not caught"
|
|
|
|
|
except:
|
|
|
|
|
assert True
|
2009-03-03 01:40:52 +00:00
|
|
|
|
2008-07-21 21:12:23 +00:00
|
|
|
def test_get_packet(self):
|
|
|
|
|
msg_dict = TemplateDictionary(self.template_list)
|
2008-07-22 13:33:35 +00:00
|
|
|
packet = msg_dict.get_template('ConfirmEnableSimulator')
|
2008-07-21 21:12:23 +00:00
|
|
|
assert packet != None, "get_packet failed"
|
2008-07-23 19:38:37 +00:00
|
|
|
assert packet.frequency == MsgFrequency.MEDIUM_FREQUENCY_MESSAGE, "Incorrect frequency for ConfirmEnableSimulator"
|
|
|
|
|
assert packet.msg_num == 8, "Incorrect message number for ConfirmEnableSimulator"
|
2008-07-21 21:12:23 +00:00
|
|
|
|
|
|
|
|
def test_get_packet_pair(self):
|
|
|
|
|
msg_dict = TemplateDictionary(self.template_list)
|
2008-07-22 13:33:35 +00:00
|
|
|
packet = msg_dict.get_template_by_pair('Medium', 8)
|
2008-07-23 19:38:37 +00:00
|
|
|
assert packet.name == 'ConfirmEnableSimulator', "Frequency-Number pair resulting in incorrect packet"
|
2008-07-21 18:18:39 +00:00
|
|
|
|
|
|
|
|
class TestTemplates(unittest.TestCase):
|
2009-03-03 01:40:52 +00:00
|
|
|
|
2008-07-21 18:18:39 +00:00
|
|
|
def tearDown(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.template_file = msg_tmpl
|
2008-07-22 15:18:35 +00:00
|
|
|
self.parser = MessageTemplateParser(self.template_file)
|
2008-07-23 19:38:37 +00:00
|
|
|
self.msg_dict = TemplateDictionary(self.parser.message_templates)
|
2008-07-21 18:18:39 +00:00
|
|
|
|
|
|
|
|
def test_parser(self):
|
|
|
|
|
parser = MessageTemplateParser(self.template_file)
|
2008-07-23 19:38:37 +00:00
|
|
|
assert parser.message_templates != None, "Parsing template file failed"
|
2008-07-21 18:42:08 +00:00
|
|
|
|
|
|
|
|
def test_parser_fail(self):
|
|
|
|
|
try:
|
|
|
|
|
parser = MessageTemplateParser(None)
|
|
|
|
|
assert False, "Fail case TEMPLATE_FILE == NONE not caught"
|
|
|
|
|
except:
|
|
|
|
|
assert True
|
2009-03-03 01:40:52 +00:00
|
|
|
|
2008-07-21 18:18:39 +00:00
|
|
|
def test_parser_version(self):
|
2008-07-23 19:38:37 +00:00
|
|
|
version = self.parser.version
|
2008-07-21 18:18:39 +00:00
|
|
|
assert version == 2.0, "Version not correct, expected 2.0, got " + str(version)
|
|
|
|
|
|
|
|
|
|
def test_template(self):
|
2008-07-22 15:18:35 +00:00
|
|
|
template = self.msg_dict['CompletePingCheck']
|
2008-07-23 19:38:37 +00:00
|
|
|
name = template.name
|
|
|
|
|
freq = template.frequency
|
|
|
|
|
num = template.msg_num
|
|
|
|
|
trust = template.msg_trust
|
|
|
|
|
enc = template.msg_encoding
|
|
|
|
|
dep = template.msg_deprecation
|
2008-07-21 18:18:39 +00:00
|
|
|
assert name == 'CompletePingCheck', "Expected: CompletePingCheck Returned: " + name
|
2008-07-22 15:18:35 +00:00
|
|
|
assert freq == MsgFrequency.HIGH_FREQUENCY_MESSAGE, "Expected: High Returned: " + freq
|
2008-07-21 18:18:39 +00:00
|
|
|
assert num == 2, "Expected: 2 Returned: " + str(num)
|
2008-07-22 15:18:35 +00:00
|
|
|
assert trust == MsgTrust.LL_NOTRUST, "Expected: NotTrusted Returned: " + trust
|
|
|
|
|
assert enc == MsgEncoding.LL_UNENCODED, "Expected: Unencoded Returned: " + enc
|
|
|
|
|
assert dep == MsgDeprecation.LL_NOTDEPRECATED, "Expected: Returned: " + dep
|
2008-07-21 18:18:39 +00:00
|
|
|
|
2008-08-07 18:48:08 +00:00
|
|
|
"""def test_template_low(self):
|
2008-07-22 15:18:35 +00:00
|
|
|
template = self.msg_dict['AddCircuitCode']
|
2008-07-23 19:38:37 +00:00
|
|
|
hx = template.msg_num_hex
|
2008-08-07 18:48:08 +00:00
|
|
|
assert hx == '\xff\xff\x00\x02', "Expected: " + r'\xff\xff\x00\x02' + " Returned: " + repr(hx)"""
|
2008-07-22 15:18:35 +00:00
|
|
|
|
|
|
|
|
def test_deprecated(self):
|
|
|
|
|
template = self.msg_dict['ObjectPosition']
|
2008-07-23 19:38:37 +00:00
|
|
|
dep = template.msg_deprecation
|
2008-07-22 15:18:35 +00:00
|
|
|
assert dep == MsgDeprecation.LL_DEPRECATED, "Expected: Deprecated Returned: " + dep
|
2008-07-21 18:18:39 +00:00
|
|
|
|
2008-08-07 18:48:08 +00:00
|
|
|
"""def test_template_medium(self):
|
2008-07-22 15:18:35 +00:00
|
|
|
template = self.msg_dict['RequestMultipleObjects']
|
2008-07-23 19:38:37 +00:00
|
|
|
hx = template.msg_num_hex
|
2008-08-07 18:48:08 +00:00
|
|
|
assert hx == '\xff\x03', "Expected: " + r'\xff\x03' + " Returned: " + hx"""
|
2008-07-21 18:18:39 +00:00
|
|
|
|
|
|
|
|
def test_template_fixed(self):
|
2008-07-22 15:18:35 +00:00
|
|
|
template = self.msg_dict['PacketAck']
|
2008-07-23 19:38:37 +00:00
|
|
|
num = template.msg_num
|
2008-08-07 18:48:08 +00:00
|
|
|
#hx = template.msg_num_hex
|
2008-07-21 18:18:39 +00:00
|
|
|
assert num == 251, "Expected: 251 Returned: " + str(num)
|
2008-08-07 18:48:08 +00:00
|
|
|
#assert hx == '\xff\xff\xff\xfb', "Expected: " + r'\xff\xff\xff\xfb' + " Returned: " + repr(hx)
|
2008-07-21 18:18:39 +00:00
|
|
|
|
|
|
|
|
def test_block(self):
|
2008-07-22 15:18:35 +00:00
|
|
|
block = self.msg_dict['OpenCircuit'].get_block('CircuitInfo')
|
2008-10-28 04:39:09 +00:00
|
|
|
tp = block.block_type #block.block_type vs block.type issue
|
2008-07-29 15:10:59 +00:00
|
|
|
num = block.number
|
2008-07-22 15:18:35 +00:00
|
|
|
assert tp == MsgBlockType.MBT_SINGLE, "Expected: Single Returned: " + tp
|
2008-07-21 18:18:39 +00:00
|
|
|
assert num == 0, "Expected: 0 Returned: " + str(num)
|
2009-03-03 01:40:52 +00:00
|
|
|
|
2008-07-21 18:18:39 +00:00
|
|
|
def test_block_multiple(self):
|
2008-07-22 15:18:35 +00:00
|
|
|
block = self.msg_dict['NeighborList'].get_block('NeighborBlock')
|
2008-10-28 04:39:09 +00:00
|
|
|
tp = block.block_type #block.block_type vs block.type issue
|
2008-07-29 15:10:59 +00:00
|
|
|
num = block.number
|
2008-07-22 15:18:35 +00:00
|
|
|
assert tp == MsgBlockType.MBT_MULTIPLE, "Expected: Multiple Returned: " + tp
|
|
|
|
|
assert num == 4, "Expected: 4 Returned: " + str(num)
|
2008-07-21 18:18:39 +00:00
|
|
|
|
|
|
|
|
def test_variable(self):
|
2008-07-22 15:18:35 +00:00
|
|
|
variable = self.msg_dict['StartPingCheck'].get_block('PingID').get_variable('PingID')
|
2008-07-23 19:38:37 +00:00
|
|
|
tp = variable.type
|
2008-07-22 15:18:35 +00:00
|
|
|
assert tp == MsgType.MVT_U8, "Expected: U8 Returned: " + str(tp)
|
2008-07-21 18:18:39 +00:00
|
|
|
|
|
|
|
|
def test_add_get_block(self):
|
2008-07-22 15:18:35 +00:00
|
|
|
template = MessageTemplate('CompletePingCheck')
|
|
|
|
|
block = MessageTemplateBlock('CircuitCode')
|
2008-07-21 18:18:39 +00:00
|
|
|
template.add_block(block)
|
|
|
|
|
blocks = template.get_blocks()
|
|
|
|
|
count = len(blocks)
|
2008-07-23 19:38:37 +00:00
|
|
|
assert blocks[0].name == 'CircuitCode', "Add block failed"
|
2008-07-21 18:18:39 +00:00
|
|
|
assert template.get_block('CircuitCode') != None, "Get block failed"
|
2009-03-03 01:40:52 +00:00
|
|
|
|
2008-07-21 18:18:39 +00:00
|
|
|
def test_add_variable(self):
|
2008-07-22 15:18:35 +00:00
|
|
|
block = MessageTemplateBlock('CircuitCode')
|
|
|
|
|
variable = MessageTemplateVariable("PingID", MsgType.MVT_U8, 1)
|
2008-07-21 18:18:39 +00:00
|
|
|
block.add_variable(variable)
|
|
|
|
|
var_list = block.get_variables()
|
2008-07-23 19:38:37 +00:00
|
|
|
assert var_list[0].name == 'PingID', "Add variable failed"
|
2008-07-21 18:18:39 +00:00
|
|
|
assert block.get_variable('PingID') != None, "Get variable failed"
|
|
|
|
|
|
|
|
|
|
def test_suite():
|
|
|
|
|
from unittest import TestSuite, makeSuite
|
|
|
|
|
suite = TestSuite()
|
|
|
|
|
suite.addTest(makeSuite(TestTemplates))
|
2008-07-21 21:12:23 +00:00
|
|
|
suite.addTest(makeSuite(TestDictionary))
|
2008-07-21 18:18:39 +00:00
|
|
|
return suite
|