blob: 271131d483521c0d9f249424ad9384400001f0d8 [file] [log] [blame]
Brian Silvermanf7f267a2017-02-04 16:16:08 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2017. All Rights Reserved. */
3/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
8#include <DriverStation.h>
9#include <RobotState.h>
10
11#include "Commands/ConditionalCommand.h"
12#include "Commands/Scheduler.h"
13#include "command/MockCommand.h"
14#include "command/MockConditionalCommand.h"
15#include "gtest/gtest.h"
16
17using namespace frc;
18
19class ConditionalCommandTest : public testing::Test {
20 public:
21 MockConditionalCommand* m_command;
22 MockCommand* m_onTrue;
23 MockCommand* m_onFalse;
24
25 protected:
26 void SetUp() override {
27 RobotState::SetImplementation(DriverStation::GetInstance());
28 Scheduler::GetInstance()->SetEnabled(true);
29
30 m_onTrue = new MockCommand();
31 m_onFalse = new MockCommand();
32 m_command = new MockConditionalCommand(m_onTrue, m_onFalse);
33 }
34
35 void TearDown() override { delete m_command; }
36
37 /**
38 * Tears Down the Scheduler at the end of each test.
39 *
40 * Must be called at the end of each test inside each test in order to prevent
41 * them being deallocated when they leave the scope of the test (causing a
42 * segfault). This cannot be done within the virtual void Teardown() method
43 * because it is called outside of the scope of the test.
44 */
45 void TeardownScheduler() { Scheduler::GetInstance()->ResetAll(); }
46
47 void AssertCommandState(MockCommand& command, int32_t initialize,
48 int32_t execute, int32_t isFinished, int32_t end,
49 int32_t interrupted) {
50 EXPECT_EQ(initialize, command.GetInitializeCount());
51 EXPECT_EQ(execute, command.GetExecuteCount());
52 EXPECT_EQ(isFinished, command.GetIsFinishedCount());
53 EXPECT_EQ(end, command.GetEndCount());
54 EXPECT_EQ(interrupted, command.GetInterruptedCount());
55 }
56};
57
58TEST_F(ConditionalCommandTest, OnTrueTest) {
59 m_command->SetCondition(true);
60
61 Scheduler::GetInstance()->AddCommand(m_command);
62 AssertCommandState(*m_onTrue, 0, 0, 0, 0, 0);
63 Scheduler::GetInstance()->Run(); // init command and select m_onTrue
64 AssertCommandState(*m_onTrue, 0, 0, 0, 0, 0);
65 Scheduler::GetInstance()->Run(); // init m_onTrue
66 AssertCommandState(*m_onTrue, 0, 0, 0, 0, 0);
67 Scheduler::GetInstance()->Run();
68 AssertCommandState(*m_onTrue, 1, 1, 2, 0, 0);
69 Scheduler::GetInstance()->Run();
70 AssertCommandState(*m_onTrue, 1, 2, 4, 0, 0);
71
72 EXPECT_TRUE(m_onTrue->GetInitializeCount() > 0)
73 << "Did not initialize the true command\n";
74 EXPECT_TRUE(m_onFalse->GetInitializeCount() == 0)
75 << "Initialized the false command\n";
76
77 TeardownScheduler();
78}
79
80TEST_F(ConditionalCommandTest, OnFalseTest) {
81 m_command->SetCondition(false);
82
83 Scheduler::GetInstance()->AddCommand(m_command);
84 AssertCommandState(*m_onFalse, 0, 0, 0, 0, 0);
85 Scheduler::GetInstance()->Run(); // init command and select m_onTrue
86 AssertCommandState(*m_onFalse, 0, 0, 0, 0, 0);
87 Scheduler::GetInstance()->Run(); // init m_onTrue
88 AssertCommandState(*m_onFalse, 0, 0, 0, 0, 0);
89 Scheduler::GetInstance()->Run();
90 AssertCommandState(*m_onFalse, 1, 1, 2, 0, 0);
91 Scheduler::GetInstance()->Run();
92 AssertCommandState(*m_onFalse, 1, 2, 4, 0, 0);
93
94 EXPECT_TRUE(m_onFalse->GetInitializeCount() > 0)
95 << "Did not initialize the false command";
96 EXPECT_TRUE(m_onTrue->GetInitializeCount() == 0)
97 << "Initialized the true command";
98
99 TeardownScheduler();
100}