def test_stop_buzzer(): serial = MockSerial(ACK) attiny = AttinyProtocol(serial) result = attiny.stop_buzzer() assert result == True assert serial.received == STOP_BUZZER
def test_set_left_motor_timeout(): serial = MockSerial(b'') attiny = AttinyProtocol(serial) result = attiny.set_left_motor(0) assert result == False
def test_set_left_motor_nak(): serial = MockSerial(NAK) attiny = AttinyProtocol(serial) result = attiny.set_left_motor(0) assert result == False
def test_set_both_motors_timeout(): serial = MockSerial(b'') attiny = AttinyProtocol(serial) result = attiny.set_motors(0, 0) assert result == False
def test_set_both_motors_nak(): serial = MockSerial(NAK) attiny = AttinyProtocol(serial) result = attiny.set_motors(0, 0) assert result == False
def test_reset_both_encoders_invalid(): serial = MockSerial(INVALID_RESPONSE) attiny = AttinyProtocol(serial) with pytest.raises(InvalidResponseException): attiny.reset_encoders() assert serial.received == ENCODERS_RESET_BOTH
def test_reset_both_encoders_timeout(): serial = MockSerial(b'') attiny = AttinyProtocol(serial) result = attiny.reset_encoders() assert serial.received == ENCODERS_RESET_BOTH assert result == False
def test_reset_left_encoder_timeout(): serial = MockSerial(b'') attiny = AttinyProtocol(serial) result = attiny.reset_left_encoder() assert serial.received == ENCODERS_RESET_LEFT assert result == False
def test_reset_right_encoder_nak(): serial = MockSerial(NAK) attiny = AttinyProtocol(serial) result = attiny.reset_right_encoder() assert serial.received == ENCODERS_RESET_RIGHT assert result == False
def test_set_pi_parameters_timeout(): serial = MockSerial(b'') attiny = AttinyProtocol(serial) result = attiny.set_pi_parameters(0, 0, 0) assert len(serial.received) == 6 assert result == False
def test_set_both_motors_invalid(): serial = MockSerial(INVALID_RESPONSE) attiny = AttinyProtocol(serial) with pytest.raises(InvalidResponseException): attiny.set_motors(0, 0) assert serial.received[:1] == SET_BOTH_MOTORS
def test_set_right_motor_invalid(): serial = MockSerial(INVALID_RESPONSE) attiny = AttinyProtocol(serial) with pytest.raises(InvalidResponseException): attiny.set_right_motor(0) assert serial.received[:1] == SET_RIGHT_MOTOR
def test_reset_left_encoder(): serial = MockSerial(ACK) attiny = AttinyProtocol(serial) result = attiny.reset_left_encoder() assert serial.received == ENCODERS_RESET_LEFT assert result == True
def test_stop_motors(): serial = MockSerial(ACK) attiny = AttinyProtocol(serial) result = attiny.stop_motors() assert serial.received == STOP_MOTORS assert result == True
def test_alive_ack(): # send out an ACK byte serial = MockSerial(ACK) attiny = AttinyProtocol(serial) result = attiny.alive() assert serial.received == ALIVE assert result == True
def test_alive_nak(): # send out an NAK byte serial = MockSerial(NAK) attiny = AttinyProtocol(serial) result = attiny.alive() assert serial.received == ALIVE assert result == False
def test_echo(): serial = MockSerial(ECHO_TEST) attiny = AttinyProtocol(serial) response = attiny.echo(ECHO_TEST) assert serial.received == ECHO + ECHO_TEST assert response == ECHO_TEST
def test_stop_motors_undefined(): # send out something that is neither an ACK or a NAK byte serial = MockSerial(INVALID_RESPONSE) attiny = AttinyProtocol(serial) with pytest.raises(InvalidResponseException): attiny.stop_motors() assert serial.received == STOP_MOTORS
def test_stop_motors_timeout(): serial = MockSerial(b'') attiny = AttinyProtocol(serial) result = attiny.stop_motors() assert serial.received == STOP_MOTORS assert result == False
def test_echo_too_long(): serial = MockSerial(b'') attiny = AttinyProtocol(serial) with pytest.raises(InvalidLengthException): attiny.echo(b'abcd') assert serial.received == b''
def test_echo_invalid(): serial = MockSerial(ECHO_INVALID) attiny = AttinyProtocol(serial) with pytest.raises(InvalidResponseException): attiny.echo(ECHO_TEST) assert serial.received == ECHO + ECHO_TEST
def test_get_right_encoder(): value = 21845 encoder_bytes = value.to_bytes(2, 'big') serial = MockSerial(encoder_bytes) attiny = AttinyProtocol(serial) result = attiny.get_right_encoder() assert serial.received == ENCODERS_RIGHT assert result == value
def test_get_left_encoder(): value = 43690 encoder_bytes = value.to_bytes(2, 'big') serial = MockSerial(encoder_bytes) attiny = AttinyProtocol(serial) result = attiny.get_left_encoder() assert serial.received == ENCODERS_LEFT assert result == value
def test_set_pi_parameters_timeout(): serial = MockSerial(INVALID_RESPONSE) attiny = AttinyProtocol(serial) with pytest.raises(InvalidResponseException): attiny.set_pi_parameters(0, 0, 0) assert len(serial.received) == 6 assert serial.received[:1] == SET_PI
def test_set_left_motor_zero(): serial = MockSerial(ACK) left = MOTOR_ZERO left_bytes = BYTES_MOTOR_ZERO attiny = AttinyProtocol(serial) result = attiny.set_left_motor(left) assert len(serial.received) == 2 assert serial.received == SET_LEFT_MOTOR + left_bytes assert result == True
def test_set_right_motor_zero(): serial = MockSerial(ACK) right = MOTOR_ZERO right_bytes = BYTES_MOTOR_ZERO attiny = AttinyProtocol(serial) result = attiny.set_right_motor(right) assert len(serial.received) == 2 assert serial.received == SET_RIGHT_MOTOR + right_bytes assert result == True
def test_set_buzzer_max(): serial = MockSerial(ACK) attiny = AttinyProtocol(serial) result = attiny.set_buzzer(FREQUENCY_MAX, DURATION_MAX, VOLUME_MAX) assert result == True assert len(serial.received) == 6 assert serial.received[:1] == SET_BUZZER assert serial.received[1:3] == FREQUENCY_MAX_ENCODED assert serial.received[3:5] == DURATION_MAX_ENCODED assert serial.received[5:] == VOLUME_MAX_ENCODED
def test_set_pi_parameters_max(): serial = MockSerial(ACK) attiny = AttinyProtocol(serial) result = attiny.set_pi_parameters(INT16_MAX, INT16_MAX, UINT8_MAX) assert result == True assert len(serial.received) == 6 assert serial.received[:1] == SET_PI assert serial.received[1:3] == INT16_MAX_ENCODED assert serial.received[3:5] == INT16_MAX_ENCODED assert serial.received[5:] == UINT8_MAX_ENCODED
def test_set_both_motors_exceed_maxmin(): serial = MockSerial(ACK) left = MOTOR_MAX + 1 left_bytes = BYTES_MOTOR_MAX right = MOTOR_MIN - 1 right_bytes = BYTES_MOTOR_MIN attiny = AttinyProtocol(serial) attiny.set_motors(left, right) assert len(serial.received) == 3 assert serial.received == SET_BOTH_MOTORS + left_bytes + right_bytes
def test_set_both_motors_zero(): serial = MockSerial(ACK) left = MOTOR_ZERO left_bytes = BYTES_MOTOR_ZERO right = MOTOR_ZERO right_bytes = BYTES_MOTOR_ZERO attiny = AttinyProtocol(serial) result = attiny.set_motors(left, right) assert len(serial.received) == 3 assert serial.received == SET_BOTH_MOTORS + left_bytes + right_bytes assert result == True