Exemplo n.º 1
0
def test_stop_buzzer():
    serial = MockSerial(ACK)
    attiny = AttinyProtocol(serial)
    result = attiny.stop_buzzer()

    assert result == True
    assert serial.received == STOP_BUZZER
Exemplo n.º 2
0
def test_set_left_motor_timeout():
    serial = MockSerial(b'')

    attiny = AttinyProtocol(serial)
    result = attiny.set_left_motor(0)

    assert result == False
Exemplo n.º 3
0
def test_set_left_motor_nak():
    serial = MockSerial(NAK)

    attiny = AttinyProtocol(serial)
    result = attiny.set_left_motor(0)

    assert result == False
Exemplo n.º 4
0
def test_set_both_motors_timeout():
    serial = MockSerial(b'')

    attiny = AttinyProtocol(serial)
    result = attiny.set_motors(0, 0)

    assert result == False
Exemplo n.º 5
0
def test_set_both_motors_nak():
    serial = MockSerial(NAK)

    attiny = AttinyProtocol(serial)
    result = attiny.set_motors(0, 0)

    assert result == False
Exemplo n.º 6
0
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
Exemplo n.º 7
0
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
Exemplo n.º 8
0
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
Exemplo n.º 9
0
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
Exemplo n.º 10
0
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
Exemplo n.º 11
0
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
Exemplo n.º 12
0
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
Exemplo n.º 13
0
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
Exemplo n.º 14
0
def test_stop_motors():
    serial = MockSerial(ACK)

    attiny = AttinyProtocol(serial)
    result = attiny.stop_motors()

    assert serial.received == STOP_MOTORS

    assert result == True
Exemplo n.º 15
0
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
Exemplo n.º 16
0
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
Exemplo n.º 17
0
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
Exemplo n.º 18
0
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
Exemplo n.º 19
0
def test_stop_motors_timeout():
    serial = MockSerial(b'')

    attiny = AttinyProtocol(serial)
    result = attiny.stop_motors()

    assert serial.received == STOP_MOTORS

    assert result == False
Exemplo n.º 20
0
def test_echo_too_long():
    serial = MockSerial(b'')

    attiny = AttinyProtocol(serial)

    with pytest.raises(InvalidLengthException):
        attiny.echo(b'abcd')

    assert serial.received == b''
Exemplo n.º 21
0
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
Exemplo n.º 22
0
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
Exemplo n.º 23
0
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
Exemplo n.º 24
0
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
Exemplo n.º 25
0
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
Exemplo n.º 26
0
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
Exemplo n.º 27
0
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
Exemplo n.º 28
0
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
Exemplo n.º 29
0
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
Exemplo n.º 30
0
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