eva_bead_1/tests/Model/SnakeLevelTest.cs
2024-10-23 22:11:14 +02:00

149 lines
5.1 KiB
C#

using Snake.Model;
using FluentAssertions;
using Microsoft.Extensions.Time.Testing;
namespace tests.Model
{
public class SnakeLevelTest
{
#region Level constants
private const int Size = 10;
private const int SnakeStartLength = 5;
private const int NewEggRound = 3;
private const int EggLimit = 2;
Point[] obstacles = [(0, 4), (3, 3)];
#endregion
#region Test objects
FakeTimeProvider timeProvider = new FakeTimeProvider();
SnakeLevel level;
#endregion
#region Constructor and initializazion
public SnakeLevelTest()
{
level = new SnakeLevel(
size: Size, obstacles: obstacles,
snake_start_length: SnakeStartLength, new_egg_round: NewEggRound,
egg_limit: EggLimit, time_provider: timeProvider
);
}
#endregion
#region Tests
[Fact]
public void SnakeInitialValuesTests()
{
level.SnakeHeadDirection.Should().Be(SnakeLevel.SnakeDirection.Down);
level.SnakeLength.Should().Be(SnakeStartLength);
level.EggLimit.Should().Be(EggLimit);
level.NewEggRound.Should().Be(NewEggRound);
level.Size.Should().Be(Size);
level.State.Should().Be(SnakeLevel.GameState.Paused);
foreach(Point p in obstacles)
{
level[p].Should().Be(SnakeLevel.LevelBlock.Obstacle);
}
}
[Fact]
public void SnakeOutOfBoundsTest()
{
level._disable_egg_generation = true;
bool game_state_changed_raised = false;
level.GameStateUpdate += (s, e) =>
{
game_state_changed_raised = true;
};
level.Start();
game_state_changed_raised.Should().Be(true);
game_state_changed_raised = false;
for(int i=0; i<Size - SnakeStartLength; i++)
{
timeProvider.Advance(SnakeLevel.TickTimeSpan);
}
game_state_changed_raised.Should().Be(false);
timeProvider.Advance(SnakeLevel.TickTimeSpan);
game_state_changed_raised.Should().Be(true);
level.State.Should().Be(SnakeLevel.GameState.Dead);
}
[Fact]
public void SnakeObstacleTest()
{
// Check the test constants
obstacles.Should().Contain((0, SnakeStartLength - 1), "There must be an obstacle on the left-most block next to the snake head for the obstacle test.");
for (int i = 1; i < Size / 2; i++)
{
obstacles.Should().NotContain((i, SnakeStartLength - 1), "There must not be an obstacle on the left of the snake head except for the one on the left-most position for the obstacle test.");
}
level._disable_egg_generation = true;
bool game_state_changed_raised = false;
level.GameStateUpdate += (s, e) =>
{
game_state_changed_raised = true;
};
level.Start();
game_state_changed_raised.Should().Be(true);
game_state_changed_raised = false;
level.SnakeHeadDirection = SnakeLevel.SnakeDirection.Left;
for(int i=0; i < Size/2 - 1; i++)
{
timeProvider.Advance(SnakeLevel.TickTimeSpan);
}
game_state_changed_raised.Should().Be(false);
timeProvider.Advance(SnakeLevel.TickTimeSpan);
game_state_changed_raised.Should().Be(true);
level.State.Should().Be(SnakeLevel.GameState.Dead);
}
[Fact]
public void EggPickupTest()
{
// check test constants
obstacles.Should().NotContain((Size / 2, SnakeStartLength), "There must be no eggs upto two blocks below the snake for egg pickup test");
obstacles.Should().NotContain((Size / 2, SnakeStartLength + 1), "There must be no eggs upto two blocks below the snake for egg pickup test");
level._disable_egg_generation = true;
Point test_egg = (Size / 2, SnakeStartLength + 1);
level.AddEgg(test_egg);
level.Start();
timeProvider.Advance(SnakeLevel.TickTimeSpan);
level.Eggs.Should().Contain(test_egg);
level.SnakeLength.Should().Be(SnakeStartLength);
timeProvider.Advance(SnakeLevel.TickTimeSpan);
level.Eggs.Should().NotContain(test_egg);
level.Score.Should().Be(1);
level.SnakeLength.Should().Be(SnakeStartLength + 1);
}
[Fact]
public void EggGenerationTest()
{
level._disable_snake_movement = true;
level.Start();
for(int i=0; i<EggLimit+1; i++)
{
level.Eggs.Should().HaveCount(i);
for(int j=0; j<NewEggRound; j++)
{
timeProvider.Advance(SnakeLevel.TickTimeSpan);
}
}
timeProvider.Advance(SnakeLevel.TickTimeSpan);
level.Eggs.Should().HaveCount(EggLimit);
}
#endregion
}
}