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-03-30
Revision:
34:85994e0501fb
Parent:
32:50b777bab5a4
Child:
35:7ee45f6d6484

File content as of revision 34:85994e0501fb:

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

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

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);
}