Add debug foundation
This commit is contained in:
176
tests/SideScrollerGame.Content.Tests/DebugCommandServiceTests.cs
Normal file
176
tests/SideScrollerGame.Content.Tests/DebugCommandServiceTests.cs
Normal file
@@ -0,0 +1,176 @@
|
||||
#nullable enable
|
||||
|
||||
using SideScrollerGame.Content;
|
||||
using SideScrollerGame.Content.Samples;
|
||||
using SideScrollerGame.Debug.Commands;
|
||||
|
||||
namespace SideScrollerGame.Content.Tests;
|
||||
|
||||
public class DebugCommandServiceTests
|
||||
{
|
||||
[Fact]
|
||||
public void PauseResumeAndTogglePause_UpdateState()
|
||||
{
|
||||
DebugCommandService service = CreateService();
|
||||
|
||||
service.Execute(DebugCommandId.Pause);
|
||||
Assert.True(service.State.IsPaused);
|
||||
|
||||
service.Execute(DebugCommandId.Resume);
|
||||
Assert.False(service.State.IsPaused);
|
||||
|
||||
service.Execute(DebugCommandId.TogglePause);
|
||||
Assert.True(service.State.IsPaused);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("0.25", 0.25)]
|
||||
[InlineData("0.5", 0.5)]
|
||||
[InlineData("1", 1.0)]
|
||||
[InlineData("2", 2.0)]
|
||||
[InlineData("4", 4.0)]
|
||||
public void SetTimeScale_AcceptsSupportedValues(string argument, double expected)
|
||||
{
|
||||
DebugCommandService service = CreateService();
|
||||
|
||||
DebugCommandResult result = service.Execute(DebugCommandId.SetTimeScale, argument);
|
||||
|
||||
Assert.True(result.Succeeded);
|
||||
Assert.Equal(expected, service.State.TimeScale);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetTimeScale_RejectsUnsupportedValue()
|
||||
{
|
||||
DebugCommandService service = CreateService();
|
||||
|
||||
DebugCommandResult result = service.Execute(DebugCommandId.SetTimeScale, "3");
|
||||
|
||||
Assert.False(result.Succeeded);
|
||||
Assert.Equal(1.0, service.State.TimeScale);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetDifficulty_ValidatesIds()
|
||||
{
|
||||
DebugCommandService service = CreateService();
|
||||
|
||||
DebugCommandResult validResult = service.Execute(DebugCommandId.SetDifficulty, "difficulty.hard");
|
||||
DebugCommandResult invalidResult = service.Execute(DebugCommandId.SetDifficulty, "difficulty.missing");
|
||||
|
||||
Assert.True(validResult.Succeeded);
|
||||
Assert.Equal("difficulty.hard", service.State.ActiveDifficultyId);
|
||||
Assert.False(invalidResult.Succeeded);
|
||||
Assert.Equal("difficulty.hard", service.State.ActiveDifficultyId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetSeed_UpdatesState()
|
||||
{
|
||||
DebugCommandService service = CreateService();
|
||||
|
||||
DebugCommandResult result = service.Execute(DebugCommandId.SetSeed, "333");
|
||||
|
||||
Assert.True(result.Succeeded);
|
||||
Assert.Equal(333, service.State.Seed);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToggleFlags_UpdateState()
|
||||
{
|
||||
DebugCommandService service = CreateService();
|
||||
|
||||
service.Execute(DebugCommandId.ToggleCollisionShapes);
|
||||
service.Execute(DebugCommandId.ToggleGameplayBounds);
|
||||
service.Execute(DebugCommandId.ToggleInvulnerability);
|
||||
service.Execute(DebugCommandId.ToggleInfiniteSpecialAmmo);
|
||||
service.Execute(DebugCommandId.ToggleNoEnemyFire);
|
||||
|
||||
Assert.True(service.State.ShowCollisionShapes);
|
||||
Assert.True(service.State.ShowGameplayBounds);
|
||||
Assert.True(service.State.Invulnerable);
|
||||
Assert.True(service.State.InfiniteSpecialAmmo);
|
||||
Assert.True(service.State.NoEnemyFire);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SpawnActor_ValidatesActorAndRunsHandler()
|
||||
{
|
||||
DebugCommandService service = CreateService();
|
||||
string spawnedActorId = string.Empty;
|
||||
service.RegisterSpawnHandler(actorId =>
|
||||
{
|
||||
spawnedActorId = actorId;
|
||||
return DebugCommandResult.Success(DebugCommandId.SpawnActor, $"Spawned {actorId}", actorId);
|
||||
});
|
||||
|
||||
DebugCommandResult validResult = service.Execute(DebugCommandId.SpawnActor, "enemy.serial");
|
||||
DebugCommandResult invalidResult = service.Execute(DebugCommandId.SpawnActor, "enemy.missing");
|
||||
|
||||
Assert.True(validResult.Succeeded);
|
||||
Assert.Equal("enemy.serial", spawnedActorId);
|
||||
Assert.Equal("enemy.serial", service.State.LastSpawnedActorId);
|
||||
Assert.Equal(1, service.State.SpawnedActorCount);
|
||||
Assert.False(invalidResult.Succeeded);
|
||||
Assert.Equal(1, service.State.SpawnedActorCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JumpToMarker_ValidatesMarkerAndRunsHandler()
|
||||
{
|
||||
DebugCommandService service = CreateService();
|
||||
string jumpedMarkerId = string.Empty;
|
||||
service.RegisterTimelineJumpHandler(markerId =>
|
||||
{
|
||||
jumpedMarkerId = markerId;
|
||||
return DebugCommandResult.Success(DebugCommandId.JumpToMarker, $"Jumped {markerId}", markerId);
|
||||
});
|
||||
|
||||
DebugCommandResult validResult = service.Execute(DebugCommandId.JumpToMarker, "cluster.opening");
|
||||
DebugCommandResult invalidResult = service.Execute(DebugCommandId.JumpToMarker, "cluster.missing");
|
||||
|
||||
Assert.True(validResult.Succeeded);
|
||||
Assert.Equal("cluster.opening", jumpedMarkerId);
|
||||
Assert.Equal("cluster.opening", service.State.CurrentMarkerId);
|
||||
Assert.False(invalidResult.Succeeded);
|
||||
Assert.Equal("cluster.opening", service.State.CurrentMarkerId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RestartMission_IncrementsRequestCountAndClearsRuntimeTargets()
|
||||
{
|
||||
DebugCommandService service = CreateService();
|
||||
service.Execute(DebugCommandId.SpawnActor, "enemy.serial");
|
||||
service.Execute(DebugCommandId.JumpToMarker, "cluster.opening");
|
||||
|
||||
DebugCommandResult result = service.Execute(DebugCommandId.RestartMission);
|
||||
|
||||
Assert.True(result.Succeeded);
|
||||
Assert.Equal(1, service.State.RestartMissionRequestCount);
|
||||
Assert.Equal(0, service.State.SpawnedActorCount);
|
||||
Assert.Equal(string.Empty, service.State.LastSpawnedActorId);
|
||||
Assert.Equal(string.Empty, service.State.CurrentMarkerId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CommandEvents_FireForExecutedCommands()
|
||||
{
|
||||
DebugCommandService service = CreateService();
|
||||
int stateChangedCount = 0;
|
||||
int commandExecutedCount = 0;
|
||||
service.StateChanged += _ => stateChangedCount++;
|
||||
service.CommandExecuted += _ => commandExecutedCount++;
|
||||
|
||||
service.Execute(DebugCommandId.Pause);
|
||||
service.Execute(DebugCommandId.SetDifficulty, "difficulty.missing");
|
||||
|
||||
Assert.Equal(1, stateChangedCount);
|
||||
Assert.Equal(2, commandExecutedCount);
|
||||
}
|
||||
|
||||
private static DebugCommandService CreateService()
|
||||
{
|
||||
ContentRegistry registry = SampleContent.CreateRegistry();
|
||||
return new DebugCommandService(registry, 123);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user