[commands] Add RepeatCommand (#4009)

Co-authored-by: Starlight220 <53231611+Starlight220@users.noreply.github.com>
This commit is contained in:
Excalibur FRC | 6738
2022-04-08 08:02:08 +03:00
committed by GitHub
parent 88222daa3d
commit 1b26e2d5da
8 changed files with 359 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include "CommandTestBase.h"
#include "frc2/command/FunctionalCommand.h"
#include "frc2/command/RepeatCommand.h"
using namespace frc2;
class RepeatCommandTest : public CommandTestBase {};
TEST_F(RepeatCommandTest, CallsMethodsCorrectly) {
CommandScheduler scheduler = GetScheduler();
int initCounter = 0;
int exeCounter = 0;
int isFinishedCounter = 0;
int endCounter = 0;
bool isFinishedHook = false;
auto command =
FunctionalCommand([&initCounter] { initCounter++; },
[&exeCounter] { exeCounter++; },
[&endCounter](bool interrupted) { endCounter++; },
[&isFinishedCounter, &isFinishedHook] {
isFinishedCounter++;
return isFinishedHook;
})
.Repeat();
EXPECT_EQ(0, initCounter);
EXPECT_EQ(0, exeCounter);
EXPECT_EQ(0, isFinishedCounter);
EXPECT_EQ(0, endCounter);
scheduler.Schedule(&command);
EXPECT_EQ(1, initCounter);
EXPECT_EQ(0, exeCounter);
EXPECT_EQ(0, isFinishedCounter);
EXPECT_EQ(0, endCounter);
isFinishedHook = false;
scheduler.Run();
EXPECT_EQ(1, initCounter);
EXPECT_EQ(1, exeCounter);
EXPECT_EQ(1, isFinishedCounter);
EXPECT_EQ(0, endCounter);
isFinishedHook = true;
scheduler.Run();
EXPECT_EQ(2, initCounter);
EXPECT_EQ(2, exeCounter);
EXPECT_EQ(2, isFinishedCounter);
EXPECT_EQ(1, endCounter);
isFinishedHook = false;
scheduler.Run();
EXPECT_EQ(2, initCounter);
EXPECT_EQ(3, exeCounter);
EXPECT_EQ(3, isFinishedCounter);
EXPECT_EQ(1, endCounter);
}