def test_decode_padding_field(self): """ Test for Field.padding.decode """ self.assertEquals('', Field.padding(1).decode('\x00')) try: Field.padding(1).decode('\x00\x00') self.assertTrue(False) except ValueError: pass
def test_create_input(self): """ Test for MasterCommandSpec.create_input """ basic_action = MasterCommandSpec("BA", [Field.byte("actionType"), Field.byte("actionNumber"), Field.padding(11)], []) ba_input = basic_action.create_input(1, {"actionType": 2, "actionNumber": 4}) self.assertEquals(21, len(ba_input)) self.assertEquals("STRBA\x01\x02\x04" + ("\x00" * 11) + "\r\n", ba_input)
def test_output_wiht_crc(self): """ Test crc and is_crc functions. """ field = Field.crc() self.assertEquals('crc', field.name) self.assertTrue(Field.is_crc(field)) field = Field.padding(1) self.assertFalse(Field.is_crc(field))
def test_consume_output(self): """ Test for MasterCommandSpec.consume_output """ basic_action = MasterCommandSpec( "BA", [], [Field.str("response", 2), Field.padding(11), Field.lit("\r\n")]) # Simple case, full string without offset at once (bytes_consumed, result, done) = \ basic_action.consume_output("OK" + ('\x00' * 11) + '\r\n', None) self.assertEquals((15, True), (bytes_consumed, done)) self.assertEquals("OK", result["response"]) # Full string with extra padding in the back (bytes_consumed, result, done) = \ basic_action.consume_output("OK" + ('\x00' * 11) + '\r\nSome\x04Junk', None) self.assertEquals((15, True), (bytes_consumed, done)) self.assertEquals("OK", result["response"]) # String in 2 pieces (bytes_consumed, result, done) = \ basic_action.consume_output("OK" + ('\x00' * 5), None) self.assertEquals((7, False), (bytes_consumed, done)) self.assertEquals('\x00' * 5, result.pending_bytes) (bytes_consumed, result, done) = \ basic_action.consume_output(('\x00' * 6) + '\r\n', result) self.assertEquals((8, True), (bytes_consumed, done)) self.assertEquals("OK", result["response"]) # String in 2 pieces with extra padding in back (bytes_consumed, result, done) = \ basic_action.consume_output("OK" + ('\x00' * 5), None) self.assertEquals((7, False), (bytes_consumed, done)) self.assertEquals('\x00' * 5, result.pending_bytes) (bytes_consumed, result, done) = \ basic_action.consume_output(('\x00' * 6) + '\r\nWorld', result) self.assertEquals((8, True), (bytes_consumed, done)) self.assertEquals("OK", result["response"]) # String in 3 pieces (bytes_consumed, result, done) = \ basic_action.consume_output("OK" + ('\x00' * 5), None) self.assertEquals((7, False), (bytes_consumed, done)) self.assertEquals('\x00' * 5, result.pending_bytes) (bytes_consumed, result, done) = \ basic_action.consume_output(('\x00' * 3), result) self.assertEquals((3, False), (bytes_consumed, done)) self.assertEquals('\x00' * 8, result.pending_bytes) (bytes_consumed, result, done) = \ basic_action.consume_output(('\x00' * 3), result) self.assertEquals((3, False), (bytes_consumed, done)) self.assertEquals('', result.pending_bytes) (bytes_consumed, result, done) = \ basic_action.consume_output('\r\n', result) self.assertEquals((2, True), (bytes_consumed, done)) self.assertEquals("OK", result["response"])
def test_encode_padding_field(self): """ Test for Field.padding.encode """ self.assertEquals('', Field.padding(0).encode(None)) self.assertEquals('\x00\x00', Field.padding(2).encode(None))