88Tests the XBee (IEEE 802.15.4) implementation class for XBee API compliance
99"""
1010import unittest , sys , traceback
11- from xbee .tests .Fake import FakeDevice , FakeReadDevice
11+ from xbee .tests .Fake import Serial
1212from xbee .ieee import XBee
1313from xbee .frame import APIFrame
1414from xbee .python2to3 import byteToInt , intToByte , stringToBytes
@@ -443,15 +443,16 @@ def test_send_at_command(self):
443443 API AT command packet to the serial device.
444444 """
445445
446- serial_port = FakeDevice ()
446+ serial_port = Serial ()
447447 xbee = XBee (serial_port )
448448
449449 # Send an AT command
450450 xbee .send ('at' , frame_id = stringToBytes ('A' ), command = stringToBytes ('MY' ))
451451
452452 # Expect a full packet to be written to the device
453453 expected_data = b'\x7E \x00 \x04 \x08 AMY\x10 '
454- self .assertEqual (serial_port .data , expected_data )
454+ result_data = serial_port .get_data_written ()
455+ self .assertEqual (result_data , expected_data )
455456
456457
457458 def test_send_at_command_with_param (self ):
@@ -460,7 +461,7 @@ def test_send_at_command_with_param(self):
460461 API AT command packet to the serial device.
461462 """
462463
463- serial_port = FakeDevice ()
464+ serial_port = Serial ()
464465 xbee = XBee (serial_port )
465466
466467 # Send an AT command
@@ -472,8 +473,9 @@ def test_send_at_command_with_param(self):
472473 )
473474
474475 # Expect a full packet to be written to the device
476+ result_data = serial_port .get_data_written ()
475477 expected_data = b'\x7E \x00 \x06 \x08 AMY\x00 \x00 \x10 '
476- self .assertEqual (serial_port . data , expected_data )
478+ self .assertEqual (result_data , expected_data )
477479
478480class TestSendShorthand (unittest .TestCase ):
479481 """
@@ -485,7 +487,7 @@ def setUp(self):
485487 """
486488 Prepare a fake device to read from
487489 """
488- self .ser = FakeDevice ()
490+ self .ser = Serial ()
489491 self .xbee = XBee (self .ser )
490492
491493 def test_send_at_command (self ):
@@ -496,30 +498,33 @@ def test_send_at_command(self):
496498 self .xbee .at (frame_id = stringToBytes ('A' ), command = stringToBytes ('MY' ))
497499
498500 # Expect a full packet to be written to the device
501+ result_data = self .ser .get_data_written ()
499502 expected_data = b'\x7E \x00 \x04 \x08 AMY\x10 '
500- self .assertEqual (self . ser . data , expected_data )
503+ self .assertEqual (result_data , expected_data )
501504
502505 def test_send_at_command_with_param (self ):
503506 """
504507 calling send should write a full API frame containing the
505508 API AT command packet to the serial device.
506509 """
507-
510+
508511 # Send an AT command
509512 self .xbee .at (frame_id = stringToBytes ('A' ), command = stringToBytes ('MY' ), parameter = b'\x00 \x00 ' )
510513
511514 # Expect a full packet to be written to the device
515+ result_data = self .ser .get_data_written ()
512516 expected_data = b'\x7E \x00 \x06 \x08 AMY\x00 \x00 \x10 '
513- self .assertEqual (self . ser . data , expected_data )
517+ self .assertEqual (result_data , expected_data )
514518
515519 def test_send_tx_with_close_brace (self ):
516520 """
517521 Calling tx where the given data string includes a close brace '}'
518522 must write correctly.
519523 """
520524 self .xbee .tx (dest_addr = b'\x01 \x02 ' ,data = b'{test=1}' )
525+ result_data = self .ser .get_data_written ()
521526 expected_data = b'\x7E \x00 \x0D \x01 \x00 \x01 \x02 \x00 {test=1}\xD5 '
522- self .assertEqual (self . ser . data , expected_data )
527+ self .assertEqual (result_data , expected_data )
523528
524529 def test_shorthand_disabled (self ):
525530 """
@@ -544,7 +549,8 @@ def test_read_at(self):
544549 """
545550 read and parse a parameterless AT command
546551 """
547- device = FakeReadDevice (b'\x7E \x00 \x05 \x88 DMY\x01 \x8c ' )
552+ device = Serial ()
553+ device .set_read_data (b'\x7E \x00 \x05 \x88 DMY\x01 \x8c ' )
548554 xbee = XBee (device )
549555
550556 info = xbee .wait_read_frame ()
@@ -558,9 +564,8 @@ def test_read_at_params(self):
558564 """
559565 read and parse an AT command with a parameter
560566 """
561- device = FakeReadDevice (
562- b'\x7E \x00 \x08 \x88 DMY\x01 \x00 \x00 \x00 \x8c '
563- )
567+ device = Serial ()
568+ device .set_read_data (b'\x7E \x00 \x08 \x88 DMY\x01 \x00 \x00 \x00 \x8c ' )
564569 xbee = XBee (device )
565570
566571 info = xbee .wait_read_frame ()
@@ -585,10 +590,8 @@ def test_is_response_parsed_as_io(self):
585590 sample = b'\x00 \xAA \x00 \xFF '
586591 data = header + sample
587592
588- device = FakeReadDevice (
589- APIFrame (data = b'\x88 DIS\x00 ' + data ).output ()
590- )
591-
593+ device = Serial ()
594+ device .set_read_data (APIFrame (data = b'\x88 DIS\x00 ' + data ).output ());
592595 xbee = XBee (device )
593596
594597 info = xbee .wait_read_frame ()
@@ -616,10 +619,9 @@ def test_is_remote_response_parsed_as_io(self):
616619 # ADC0 value of 255
617620 sample = b'\x00 \xAA \x00 \xFF '
618621 data = header + sample
619-
620- device = FakeReadDevice (
621- APIFrame (data = b'\x97 D\x00 \x13 \xa2 \x00 @oG\xe4 v\x1a IS\x00 ' + data ).output ()
622- )
622+
623+ device = Serial ()
624+ device .set_read_data (APIFrame (data = b'\x97 D\x00 \x13 \xa2 \x00 @oG\xe4 v\x1a IS\x00 ' + data ).output ())
623625
624626 xbee = XBee (device )
625627
@@ -655,9 +657,8 @@ def test_read_io_data(self):
655657 # RX frame data
656658 rx_io_resp = b'\x83 \x00 \x01 \x28 \x00 '
657659
658- device = FakeReadDevice (
659- b'\x7E \x00 \x0C ' + rx_io_resp + data + b'\xfd '
660- )
660+ device = Serial ()
661+ device .set_read_data (b'\x7E \x00 \x0C ' + rx_io_resp + data + b'\xfd ' )
661662 xbee = XBee (device )
662663
663664 info = xbee .wait_read_frame ()
@@ -681,16 +682,17 @@ def test_read_empty_string(self):
681682 an empty string. In this event, we must not crash.
682683 """
683684
684- class BadReadDevice (FakeReadDevice ):
685+ class BadReadDevice (Serial ):
685686 def __init__ (self , bad_read_index , data ):
686687 self .read_id = 0
687688 self .bad_read_index = bad_read_index
688- super (BadReadDevice , self ).__init__ (data )
689+ super (BadReadDevice , self ).__init__ ()
690+ self .set_read_data (data )
689691
690- def inWaiting (self ):
692+ def in_waiting (self ):
691693 return 1
692694
693- def read (self ):
695+ def read (self , length = 1 ):
694696 if self .read_id == self .bad_read_index :
695697 self .read_id += 1
696698 return ''
@@ -711,9 +713,8 @@ def test_read_at_params_in_escaped_mode(self):
711713 """
712714 read and parse an AT command with a parameter in escaped API mode
713715 """
714- device = FakeReadDevice (
715- b'~\x00 \t \x88 DMY\x01 }^}]}1}3m'
716- )
716+ device = Serial ()
717+ device .set_read_data (b'~\x00 \t \x88 DMY\x01 }^}]}1}3m' )
717718 xbee = XBee (device , escaped = True )
718719
719720 info = xbee .wait_read_frame ()
@@ -728,7 +729,8 @@ def test_empty_frame_ignored(self):
728729 """
729730 If an empty frame is received from a device, it must be ignored.
730731 """
731- device = FakeReadDevice (b'\x7E \x00 \x00 \xFF \x7E \x00 \x05 \x88 DMY\x01 \x8c ' )
732+ device = Serial ()
733+ device .set_read_data (b'\x7E \x00 \x00 \xFF \x7E \x00 \x05 \x88 DMY\x01 \x8c ' )
732734 xbee = XBee (device )
733735
734736 #import pdb
@@ -744,7 +746,8 @@ def test_read_rx_with_close_brace(self):
744746 """
745747 An rx data frame including a close brace must be read properly.
746748 """
747- device = FakeReadDevice (APIFrame (b'\x81 \x01 \x02 \x55 \x00 {test=1}' ).output ())
749+ device = Serial ()
750+ device .set_read_data (APIFrame (b'\x81 \x01 \x02 \x55 \x00 {test=1}' ).output ())
748751 xbee = XBee (device )
749752
750753 info = xbee .wait_read_frame ()
@@ -759,9 +762,8 @@ def test_read_rx_with_close_brace_escaped(self):
759762 """
760763 An escaped rx data frame including a close brace must be read properly.
761764 """
762- device = FakeReadDevice (
763- APIFrame (b'\x81 \x01 \x02 \x55 \x00 {test=1}' , escaped = True ).output ()
764- )
765+ device = Serial ()
766+ device .set_read_data (APIFrame (b'\x81 \x01 \x02 \x55 \x00 {test=1}' , escaped = True ).output ())
765767 xbee = XBee (device , escaped = True )
766768
767769 info = xbee .wait_read_frame ()
0 commit comments