NerfUS mobile node that manages a target for the Nerf gun firing range

Dependencies:   LedController mbed-rtos mbed NerfUSXbee Servomotor TargetManager

Fork of NerfUS by NerfUS

TESTS/TargetManagerTest.cpp

Committer:
Maxime Dupuis
Date:
2017-04-08
Revision:
37:8fc7845ae95b
Parent:
35:7ee45f6d6484
Child:
38:70a1c8bd2877

File content as of revision 37:8fc7845ae95b:

#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include "mocks/MockNerfusTicker.hpp"
#include "mocks/MockTarget.hpp"
#include "TargetManager.hpp"

#include "stdint.h"

class TargetManagerTest : public ::testing::Test
{
    public:
        TargetManagerTest()
        {
            targets.push_back(&mock_target_a);
            targets.push_back(&mock_target_b);
        }

        MockTarget mock_target_a;
        MockTarget mock_target_b;
        std::vector<TargetInterface*> targets;

        MockNerfusTicker mock_nerfus_ticker;
};

TEST_F(TargetManagerTest, LibraryBuilds)
{
    TargetManager target_manager(targets, mock_nerfus_ticker);
}

TEST_F(TargetManagerTest, WhenNoTargetIsActiveThenCallsToTargetHitAndTargetMissedDoNothing)
{
    TargetManager target_manager(targets, mock_nerfus_ticker);

    target_manager.target_missed(0);
    target_manager.target_hit(0);
}

TEST_F(TargetManagerTest, WhenExecutingAllyTargetThenUseTheCorrectTargetTypeAndTimeout)
{
    EXPECT_CALL(mock_target_a, ally_command());
    EXPECT_CALL(mock_nerfus_ticker, start(1000));

    TargetManager target_manager(targets, mock_nerfus_ticker);
    target_manager.execute(make_TargetInfo(0, TARGET_TYPE_ALLY, 1000));
}

TEST_F(TargetManagerTest, WhenExecutingEnemyTargetThenUseTheCorrectTargetTypeAndTimeout)
{
    EXPECT_CALL(mock_target_b, enemy_command());
    EXPECT_CALL(mock_nerfus_ticker, start(2000));

    TargetManager target_manager(targets, mock_nerfus_ticker);
    target_manager.execute(make_TargetInfo(1, TARGET_TYPE_ENEMY, 2000));
}

TEST_F(TargetManagerTest, WhenActiveTargetIsHitThenSendTheEvent)
{
    EXPECT_CALL(mock_target_a, ally_command());
    EXPECT_CALL(mock_nerfus_ticker, start(1000));
    EXPECT_CALL(mock_nerfus_ticker, get_time_ms())
        .WillOnce(::testing::Return(42));
    EXPECT_CALL(mock_nerfus_ticker, stop());
    EXPECT_CALL(mock_target_a, hit(42));

    TargetManager target_manager(targets, mock_nerfus_ticker);
    target_manager.execute(make_TargetInfo(0, TARGET_TYPE_ALLY, 1000));

    target_manager.target_hit(0);
}

TEST_F(TargetManagerTest, WhenActiveTargetIsMissedThenSendTheEvent)
{
    EXPECT_CALL(mock_target_a, ally_command());
    EXPECT_CALL(mock_nerfus_ticker, start(1000));
    EXPECT_CALL(mock_nerfus_ticker, get_time_ms())
        .WillOnce(::testing::Return(42));
    EXPECT_CALL(mock_nerfus_ticker, stop());
    EXPECT_CALL(mock_target_a, timeout(42));

    TargetManager target_manager(targets, mock_nerfus_ticker);
    target_manager.execute(make_TargetInfo(0, TARGET_TYPE_ALLY, 1000));

    target_manager.target_missed(0);
}

TEST_F(TargetManagerTest, WhenReceivingCommandInBytesThenExecuteIt)
{
    TargetManager target_manager(targets, mock_nerfus_ticker);

    EXPECT_CALL(mock_target_b, ally_command());
    EXPECT_CALL(mock_nerfus_ticker, start(0x1234));

    std::vector<uint8_t> target_info_bytes;
    target_info_bytes.push_back(0x01); //Second target
    target_info_bytes.push_back(0x00); //Mode ally
    target_info_bytes.push_back(0x12); //Timeout MSB
    target_info_bytes.push_back(0x34); //Timeout MSB

    target_manager.execute(target_info_bytes);
}