def test_get_variable_width_object_from_serial_port(self): pn = MockSerialPortNode() #pn.debug = 5 # Initialize some data to be 'read' from our mock serial port node. for x in chr(0x03) + chr(0xAA) + chr(0x03) + chr(0xFF): pn.addbuffer(ord(x)) #print pn.get_buffer_str() conn = gc.FrameworkSerialPortWrapper(pn) lines = "" \ "# Variable Buffer 1 Class\n" \ "class testvbuffer1 {\n" \ " vbuffer1 vbuffer01;\n" \ "}\n" lines = lines.split('\n') pc = ProtoCompiler() pc.parseLines(lines) c = pc.emitCode() pyfilename = os.path.join(properties.TEMP_DIR, "testvbuffer1.py") f = open(pyfilename, 'w') f.write(c) f.close() # Now try to import it. import testvbuffer1 request_obj = testvbuffer1.testvbuffer1() response_obj = testvbuffer1.testvbuffer1() req_vbuffer_obj = request_obj.findChildByName('vbuffer01') # resp_vbuffer_obj = response_obj.findChildByName('vbuffer01') lh = gdlh.SimpleLineHandler(conn) req_vbuffer_obj.setValue("abc") res = lh.send_request_with_response(request_obj, response_obj, 30) assert res == 1, "send_request_with_response() should have returned 1." assert resp_vbuffer_obj.getValue() == chr(0xAA) + chr(0x03) + chr(0xFF), "Did not get expected value." os.remove(pyfilename)
def test_get_variable_width_object_from_serial_port(self): pn = MockSerialPortNode() #pn.debug = 5 # Initialize some data to be 'read' from our mock serial port node. for x in chr(0x03) + chr(0xAA) + chr(0x03) + chr(0xFF): pn.addbuffer(ord(x)) #print pn.get_buffer_str() conn = gc.FrameworkSerialPortWrapper(pn) lines = "" \ "# Variable Buffer 1 Class\n" \ "class testvbuffer1 {\n" \ " vbuffer1 vbuffer01;\n" \ "}\n" lines = lines.split('\n') pc = ProtoCompiler() pc.parseLines(lines) c = pc.emitCode() pyfilename = os.path.join(properties.TEMP_DIR, "testvbuffer1.py") f = open(pyfilename, 'w') f.write(c) f.close() # Now try to import it. import testvbuffer1 request_obj = testvbuffer1.testvbuffer1() response_obj = testvbuffer1.testvbuffer1() req_vbuffer_obj = request_obj.findChildByName('vbuffer01') # resp_vbuffer_obj = response_obj.findChildByName('vbuffer01') lh = gdlh.SimpleLineHandler(conn) req_vbuffer_obj.setValue("abc") res = lh.send_request_with_response(request_obj, response_obj, 30) assert res == 1, "send_request_with_response() should have returned 1." assert resp_vbuffer_obj.getValue( ) == chr(0xAA) + chr(0x03) + chr(0xFF), "Did not get expected value." os.remove(pyfilename)
def test_get_fixed_width_object_from_serial_port(self): pn = MockSerialPortNode() #pn.debug = 5 # Initialize some data to be 'read' from our mock serial port node. for x in chr(0xAA) + chr(0x03) + chr(0xFF): pn.addbuffer(ord(x)) #print pn.get_buffer_str() conn = gc.FrameworkSerialPortWrapper(pn) lines = "" \ "# Single byte Class\n" \ "class singlebyte {\n" \ " uint8 preamble = 0xAA;\n" \ " uint8 data;\n" \ " uint8 postamble = 0xFF;\n" \ "}\n" lines = lines.split('\n') pc = ProtoCompiler() pc.parseLines(lines) c = pc.emitCode() pyfilename = os.path.join(properties.TEMP_DIR, "singlebyte.py") f = open(pyfilename, 'w') f.write(c) f.close() # Now try to import it. import singlebyte request_obj = singlebyte.singlebyte() response_obj = singlebyte.singlebyte() req_data_obj = request_obj.findChildByName('data') # resp_data_obj = response_obj.findChildByName('data') lh = gdlh.SimpleLineHandler(conn) req_data_obj.setValue(0x01) res = lh.send_request_with_response(request_obj, response_obj, 30) #print res os.remove(pyfilename)
def test_strip_comments(self): pc = ProtoCompiler() test_data = ( ('#', ''), ('# ', ''), ('', ''), (' ', ''), ('#aaaa', ''), ('}#comment', '}'), ('} # comment', '}'), ) for x in test_data: line, exp_res = x res = pc.strip_comments(line) assert res == exp_res, 'Expected result of "%s", got result of "%s" for "%s".' % ( exp_res, res, line)
def test_case_insertline(self): lines = "" \ "# Import a standard framework library.\n" \ "insert from mpx import properties\n" \ "insert #\n" \ "insert # This is a comment that should not be stripped\n" \ "insert temp_dir = properties.TEMP_DIR\n" \ "#\n" \ "# Single byte Class\n" \ "class singlebyte {\n" \ " uint8 preamble = 0xAA;\n" \ " uint8 data;\n" \ " uint8 postamble = 0xFF;\n" \ "}\n" pc = ProtoCompiler() fname = os.path.join(properties.TEMP_DIR, 'single_byte_with_insert.class') f = open(fname, 'w') f.write(lines) f.close() pc.parseFile(fname) c = pc.emitCode() pyfilename = os.path.join(properties.TEMP_DIR, "singlebytewithinsert.py") f = open(pyfilename, 'w') f.write(c) f.close() # Now try to import it. import singlebytewithinsert obj = singlebytewithinsert.singlebyte() # Prove that the import (in the insert line above) worked. assert singlebytewithinsert.temp_dir == properties.TEMP_DIR, "Properties apparently not properly imported"
def test_case_dup_item_names(self): lines = "" \ "# Dup Name Class\n" \ "class dupname {\n" \ " uint8 data01;\n" \ " beuint8 data01;\n" \ "}\n" pc = ProtoCompiler() fname = os.path.join(properties.TEMP_DIR, 'dup_name.class') f = open(fname, 'w') f.write(lines) f.close() # We should catch an exception when trying to parse this file. didcatch = 0 try: pc.parseFile(fname) except: didcatch = 1 if not didcatch: raise "Expected to catch an exception when parsing dupname class, but did not"
def test_case_bevbuffer2(self): lines = "" \ "# Big-Endian Variable Buffer 2 Class\n" \ "class testbevbuffer2 {\n" \ " bevbuffer2 bevbuffer01;\n" \ "}\n" pc = ProtoCompiler() fname = os.path.join(properties.TEMP_DIR, 'bevbuffer2.class') f = open(fname, 'w') f.write(lines) f.close() pc.parseFile(fname) assert pc.classnames == ['testbevbuffer2' ], "Classnames != ['testbevbuffer2']" my_class = pc._getClass('testbevbuffer2') assert my_class.isPackCompatible( ) == 0, "Class should be not pack compatible." assert my_class.isFixedWidth() == 0, "Width should not be fixed." assert my_class.getWidth( ) == 0, "Width should be 0, got %d." % my_class.getWidth() c = pc.emitCode() pyfilename = os.path.join(properties.TEMP_DIR, "testbevbuffer2.py") f = open(pyfilename, 'w') f.write(c) f.close() # Now try to import it. import testbevbuffer2 obj = testbevbuffer2.testbevbuffer2() assert obj.isFixedWidth() == 0, "isFixedWidth() should be 0" assert obj.isPackCompatible() == 0, "isPackCompatible() should be 0" assert obj.getWidth( ) == 0, "Width should be 0, got %d" % obj.getWidth() assert obj.getNumItems( ) == 1, "NumItems should be 1, got %d" % obj.getNumItems() # Object has only one item, run some tests against that item item_obj = obj.items[0] width = item_obj.getWidth() assert width == 2, "width should be 2, got %s" % str(width) os.remove(pyfilename)
def test_case_double1(self): lines = "" \ "# Float Class\n" \ "class double1 {\n" \ " double1 double01;\n" \ "}\n" pc = ProtoCompiler() fname = os.path.join(properties.TEMP_DIR, 'double1.class') f = open(fname, 'w') f.write(lines) f.close() pc.parseFile(fname) assert pc.classnames == ['double1'], "Classnames != ['double1']" my_class = pc._getClass('double1') assert my_class.isPackCompatible( ) == 1, "Class should be pack compatible." exp_spec = 'd' assert my_class.packSpec( ) == exp_spec, "Class spec should be '%s', got '%s'." % ( exp_spec, my_class.packSpec()) assert my_class.isFixedWidth() == 1, "Width should be fixed." assert my_class.getWidth( ) == 8, "Width should be 8, got %d." % my_class.getWidth() c = pc.emitCode() pyfilename = os.path.join(properties.TEMP_DIR, "double1.py") f = open(pyfilename, 'w') f.write(c) f.close() # Now try to import it. import double1 obj = double1.double1() #print 'Dir of new object: %s' % str(dir(obj)) assert obj.isFixedWidth() == 1, "isFixedWidth() should be 1" assert obj.isPackCompatible() == 1, "isPackCompatible() should be 1" assert obj.packSpec() == "d", "packSpec() should be d" assert obj.getWidth( ) == 8, "Width should be 8, got %d" % obj.getWidth() assert obj.getNumItems( ) == 1, "NumItems should be 1, got %d" % obj.getNumItems()
def test_case_singlebyte(self): lines = "" \ "# Single byte Class\n" \ "class singlebyte {\n" \ " uint8 preamble = 0xAA;\n" \ " uint8 data;\n" \ " uint8 postamble = 0xFF;\n" \ "}\n" pc = ProtoCompiler() fname = os.path.join(properties.TEMP_DIR, 'single_byte.class') f = open(fname, 'w') f.write(lines) f.close() pc.parseFile(fname) assert pc.classnames == ['singlebyte'], "Classnames != ['singlebyte']" my_class = pc._getClass('singlebyte') assert my_class.isPackCompatible( ) == 1, "Class should be pack compatible." exp_spec = '<B<B<B' assert my_class.packSpec( ) == exp_spec, "Class spec should be '%s', got '%s'." % ( exp_spec, my_class.packSpec()) assert my_class.isFixedWidth() == 1, "Width should be fixed." assert my_class.getWidth( ) == 3, "Width should be 3, got %d." % my_class.getWidth() c = pc.emitCode() pyfilename = os.path.join(properties.TEMP_DIR, "singlebyte.py") f = open(pyfilename, 'w') f.write(c) f.close() # Now try to import it. import singlebyte obj = singlebyte.singlebyte() assert obj.isFixedWidth() == 1, "isFixedWidth() should be 1" assert obj.isPackCompatible() == 1, "isPackCompatible() should be 1" assert obj.packSpec() == "<B<B<B", "packSpec() should be <B<B<B" assert obj.getWidth( ) == 3, "Width should be 3, got %d" % obj.getWidth() assert obj.getNumItems( ) == 3, "NumItems should be 3, got %d" % obj.getNumItems()
def test_case_float1(self): lines = "" \ "# Float Class\n" \ "class float1 {\n" \ " float1 float01;\n" \ "}\n" pc = ProtoCompiler() fname = os.path.join(properties.TEMP_DIR, 'float1.class') f = open(fname, 'w') f.write(lines) f.close() pc.parseFile(fname) assert pc.classnames == ['float1'], "Classnames != ['float1']" my_class = pc._getClass('float1') assert my_class.isPackCompatible( ) == 1, "Class should be pack compatible." exp_spec = 'f' assert my_class.packSpec( ) == exp_spec, "Class spec should be '%s', got '%s'." % ( exp_spec, my_class.packSpec()) assert my_class.isFixedWidth() == 1, "Width should be fixed." assert my_class.getWidth( ) == 4, "Width should be 4, got %d." % my_class.getWidth() c = pc.emitCode() pyfilename = os.path.join(properties.TEMP_DIR, "float1.py") f = open(pyfilename, 'w') f.write(c) f.close() # Now try to import it. import float1 obj = float1.float1() assert obj.isFixedWidth() == 1, "isFixedWidth() should be 1" assert obj.isPackCompatible() == 1, "isPackCompatible() should be 1" assert obj.packSpec() == "f", "packSpec() should be f" assert obj.getWidth( ) == 4, "Width should be 4, got %d" % obj.getWidth() assert obj.getNumItems( ) == 1, "NumItems should be 1, got %d" % obj.getNumItems()
def test_case_fbuffer(self): lines = "" \ "# Fixed Buffer Class\n" \ "class testfbuffer {\n" \ " width=7 fbuffer fbuffer01;\n" \ "}\n" pc = ProtoCompiler() fname = os.path.join(properties.TEMP_DIR, 'fbuffer.class') f = open(fname, 'w') f.write(lines) f.close() pc.parseFile(fname) assert pc.classnames == ['testfbuffer' ], "Classnames != ['testfbuffer']" my_class = pc._getClass('testfbuffer') assert my_class.isPackCompatible( ) == 0, "Class should be not pack compatible." assert my_class.isFixedWidth() == 1, "Width should be fixed." assert my_class.getWidth( ) == 7, "Width should be 7, got %d." % my_class.getWidth() c = pc.emitCode() pyfilename = os.path.join(properties.TEMP_DIR, "testfbuffer.py") f = open(pyfilename, 'w') f.write(c) f.close() # Now try to import it. import testfbuffer obj = testfbuffer.testfbuffer() assert obj.isFixedWidth() == 1, "isFixedWidth() should be 1" assert obj.isPackCompatible() == 0, "isPackCompatible() should be 0" assert obj.getWidth( ) == 7, "Width should be 7, got %d" % obj.getWidth() assert obj.getNumItems( ) == 1, "NumItems should be 1, got %d" % obj.getNumItems()
def test_case_nested_fbuffer_class(self): lines = "" \ "# Single byte Class\n" \ "class singlebyte {\n" \ " uint8 preamble = 0xAA;\n" \ " uint8 data;\n" \ " uint8 postamble = 0xFF;\n" \ "}\n" \ "# Fixed Buffer Class\n" \ "class testnestedfbuffer {\n" \ " width=3 class=singlebyte fbuffer fbuffer01;\n" \ "}\n" pc = ProtoCompiler() fname = os.path.join(properties.TEMP_DIR, 'nestedfbuffer.class') f = open(fname, 'w') f.write(lines) f.close() pc.parseFile(fname) assert pc.classnames == [ 'singlebyte', 'testnestedfbuffer' ], "Classnames != ['singlebyte', 'testnestedfbuffer']" my_class = pc._getClass('testnestedfbuffer') assert my_class.isPackCompatible( ) == 0, "Class should be not pack compatible." assert my_class.isFixedWidth() == 1, "Width should be fixed." assert my_class.getWidth( ) == 3, "Width should be 3, got %d." % my_class.getWidth() c = pc.emitCode() #print "Got code of:" #print c pyfilename = os.path.join(properties.TEMP_DIR, "testnestedfbuffer.py") f = open(pyfilename, 'w') f.write(c) f.close() # Now try to import it. import testnestedfbuffer obj = testnestedfbuffer.testnestedfbuffer() #print 'Got object of: %s' % str(obj) #print 'Dir of new object: %s' % str(dir(obj)) #print obj.getChildren() #print obj.findChildByName('singlebyte') assert obj.isFixedWidth() == 1, "isFixedWidth() should be 1" assert obj.isPackCompatible() == 0, "isPackCompatible() should be 0" assert obj.getWidth( ) == 3, "Width should be 3, got %d" % obj.getWidth() assert obj.getNumItems( ) == 1, "NumItems should be 1, got %d" % obj.getNumItems()
def _test_case_dbuffer(self): lines = "" \ "# Dynamic Buffer Class\n" \ "class testdbuffer {\n" \ " dbuffer dbuffer01;\n" \ "}\n" pc = ProtoCompiler() x = protocompiler.DynamicBufferItem('dbuffer', 'mydbuffer', None) fname = os.path.join(properties.TEMP_DIR, 'dbuffer.class') f = open(fname, 'w') f.write(lines) f.close() #print lines pc.parseFile(fname) assert pc.classnames == ['testdbuffer' ], "Classnames != ['testdbuffer']" my_class = pc._getClass('testdbuffer') #print 'Got class of %s.' % str(my_class) assert my_class.isPackCompatible( ) == 0, "Class should be not pack compatible." assert my_class.isFixedWidth() == 0, "Width should not be fixed." assert my_class.getWidth( ) == 0, "Width should be 0, got %d." % my_class.getWidth() c = pc.emitCode() #print "Got code of:" #print c pyfilename = os.path.join(properties.TEMP_DIR, "testdbuffer.py") f = open(pyfilename, 'w') f.write(c) f.close() # Now try to import it. import testdbuffer obj = testdbuffer.testdbuffer() #print 'Got object of: %s' % str(obj) #print 'Dir of new object: %s' % str(dir(obj)) assert obj.isFixedWidth() == 0, "isFixedWidth() should be 0" assert obj.isPackCompatible() == 0, "isPackCompatible() should be 0" assert obj.getWidth( ) == 0, "Width should be 0, got %d" % obj.getWidth() assert obj.getNumItems( ) == 1, "NumItems should be 1, got %d" % obj.getNumItems() # Object has only one item, run some tests against that item item_obj = obj.items[0] #print 'str', item_obj #print 'dir', dir (item_obj) #width = item_obj.getWidth() #assert width == 2, "width should be 2, got %s" % str(width) os.remove(pyfilename)
def test_case_vbuffer1(self): lines = "" \ "# Variable Buffer 1 Class\n" \ "class testvbuffer1 {\n" \ " vbuffer1 vbuffer01;\n" \ "}\n" pc = ProtoCompiler() fname = os.path.join(properties.TEMP_DIR, 'vbuffer1.class') f = open(fname, 'w') f.write(lines) f.close() pc.parseFile(fname) assert pc.classnames == ['testvbuffer1' ], "Classnames != ['testvbuffer1']" my_class = pc._getClass('testvbuffer1') #print 'Got class of %s.' % str(my_class) assert my_class.isPackCompatible( ) == 0, "Class should be not pack compatible." assert my_class.isFixedWidth() == 0, "Width should not be fixed." assert my_class.getWidth( ) == 0, "Width should be 0, got %d." % my_class.getWidth() c = pc.emitCode() pyfilename = os.path.join(properties.TEMP_DIR, "testvbuffer1.py") f = open(pyfilename, 'w') f.write(c) f.close() pyfilename = os.path.join('/tmp', "testvbuffer1.py") f = open(pyfilename, 'w') f.write(c) f.close() # Now try to import it. import testvbuffer1 obj = testvbuffer1.testvbuffer1() assert obj.isFixedWidth() == 0, "isFixedWidth() should be 0" assert obj.isPackCompatible() == 0, "isPackCompatible() should be 0" assert obj.getWidth( ) == 0, "Width should be 0, got %d" % obj.getWidth() assert obj.getNumItems( ) == 1, "NumItems should be 1, got %d" % obj.getNumItems() # Object has only one item, run some tests against that item item_obj = obj.items[0] width = item_obj.getWidth() assert width == 1, "width should be 1, got %s" % str(width)
def test_case_allints(self): lines = "" \ "# All Ints Class\n" \ "class allints {\n" \ " uint8 data01;\n" \ " beuint8 data02;\n" \ " int8 data03;\n" \ " uint8 data04;\n" \ "" \ " uint16 data05;\n" \ " beuint16 data06;\n" \ " int16 data07;\n" \ " uint16 data08;\n" \ "" \ " uint32 data09;\n" \ " beuint32 data10;\n" \ " int32 data11;\n" \ " uint32 data12;\n" \ "" \ " uint64 data13;\n" \ " beuint64 data14;\n" \ " int64 data15;\n" \ " uint64 data16;\n" \ "}\n" pc = ProtoCompiler() fname = os.path.join(properties.TEMP_DIR, 'all_ints.class') f = open(fname, 'w') f.write(lines) f.close() pc.parseFile(fname) assert pc.classnames == ['allints'], "Classnames != ['allints']" my_class = pc._getClass('allints') #print 'Got class of %s.' % str(my_class) assert my_class.isPackCompatible( ) == 1, "Class should be pack compatible." exp_spec = '<B>B<b<B<H>H<h<H<L>L<l<L<Q>Q<q<Q' assert my_class.packSpec( ) == exp_spec, "Class spec should be '%s', got '%s'." % ( exp_spec, my_class.packSpec()) assert my_class.isFixedWidth() == 1, "Width should be fixed." assert my_class.getWidth( ) == 60, "Width should be 60, got %d." % my_class.getWidth() c = pc.emitCode() pyfilename = os.path.join(properties.TEMP_DIR, "allints.py") f = open(pyfilename, 'w') f.write(c) f.close() # Now try to import it. import allints obj = allints.allints() #print 'Dir of new object: %s' % str(dir(obj)) assert obj.isFixedWidth() == 1, "isFixedWidth() should be 1" assert obj.isPackCompatible() == 1, "isPackCompatible() should be 1" assert obj.packSpec() == exp_spec, "packSpec() should be %s" % exp_spec assert obj.getWidth( ) == 60, "Width should be 60, got %d" % obj.getWidth() assert obj.getNumItems( ) == 16, "NumItems should be 16, got %d" % obj.getNumItems()