def test_consume_output_varlength(self): """ Test for MasterCommandSpec.consume_output with a variable length output field. """ def dim(byte_value): """ Convert a dimmer byte value to the api value. """ return int(byte_value * 10.0 / 6.0) basic_action = MasterCommandSpec( "OL", [], [Field("outputs", OutputFieldType()), Field.lit("\r\n\r\n")]) # Empty outputs (bytes_consumed, result, done) = \ basic_action.consume_output('\x00\r\n\r\n', None) self.assertEquals((5, True), (bytes_consumed, done)) self.assertEquals([], result["outputs"]) # One output (bytes_consumed, result, done) = \ basic_action.consume_output('\x01\x05\x10\r\n\r\n', None) self.assertEquals((7, True), (bytes_consumed, done)) self.assertEquals([(5, dim(16))], result["outputs"]) # Split up in multiple parts (bytes_consumed, result, done) = \ basic_action.consume_output('\x03', None) self.assertEquals((1, False), (bytes_consumed, done)) (bytes_consumed, result, done) = \ basic_action.consume_output('\x05\x10', result) self.assertEquals((2, False), (bytes_consumed, done)) (bytes_consumed, result, done) = \ basic_action.consume_output('\x01\x02\x03\x04\r\n', result) self.assertEquals((6, False), (bytes_consumed, done)) (bytes_consumed, result, done) = \ basic_action.consume_output('\r\n', result) self.assertEquals((2, True), (bytes_consumed, done)) self.assertEquals([(5, dim(16)), (1, dim(2)), (3, dim(4))], result["outputs"])
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"])