/
Vagabond132
/
TerrariaCourse
Обзор
Документация
Войти
/
Vagabond132
/
TerrariaCourse
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
Tests
GameModelTest/ModelGameTest.cs
351 строка
14 KB
Vagabond132
Комментарии добавлены
12 янв 2025, 23:33
12 янв 2025, 23:33
2cc97f4
Код
Авторство
О чём код?
using System.Numerics; using Model.GameComponents; using Model.Blocks; using Model.Blocks.ImplementedBlocks; using System.Drawing; namespace GameModelTest { /// <summary> /// ����� ������ ������ ���� � ����� /// </summary> [TestClass] public class ModelGameTest { /// <summary> /// �������� ������ ���� /// </summary> private GameModel _gameModel; /// <summary> /// ������������� ������ /// </summary> [TestInitialize] public void Setup() { _gameModel = new GameModel("Player1"); _gameModel.Player.Pos = new Vector2(400, 80); } /// <summary> /// ���������, ��� ����� GoUp() ������� ������ �����. /// </summary> [TestMethod] public void Test_GoUp_MovesPlayerUp() { _gameModel.GoUp(); Assert.AreEqual(79, _gameModel.Player.Pos.Y); } /// <summary> /// ���������, ��� ����� GoLeft() ������� ������ �����. /// </summary> [TestMethod] public void Test_GoLeft_MovesPlayerLeft() { _gameModel.GoLeft(40f); Assert.AreEqual(399, _gameModel.Player.Pos.X); } /// <summary> /// ���������, ��� ����� GoRight() ������� ������ ������. /// </summary> [TestMethod] public void Test_GoRight_MovesPlayerRight() { _gameModel.GoRight(40f); Assert.AreEqual(401, _gameModel.Player.Pos.X); } /// <summary> /// ���������, ��� ����� Jump() ���������� ������. /// </summary> [TestMethod] public void Test_Jump_StartsJump() { _gameModel.Player.IsJumping = false; _gameModel.Player.OnGround = true; _gameModel.Jump(); Assert.IsTrue(_gameModel.Player.IsJumping); Assert.IsFalse(_gameModel.Player.OnGround); } /// <summary> /// ���������, ��� ����� PerformJump() ��������� ������ �� ����� ������. /// </summary> [TestMethod] public void Test_PerformJump_JumpsPlayerUp() { _gameModel.Player.IsJumping = false; _gameModel.Player.OnGround = true; _gameModel.Jump(); _gameModel.PerformJump(5f); Assert.IsTrue((_gameModel.Player.JumpProgress > 0)); } /// <summary> /// ���������, ��� ����� ApplyGravity() ���������� ������ ������ �� 50 �� /// </summary> [TestMethod] public void Test_ApplyGravity_PlayerFallsWhenNotOnGround() { _gameModel.Player.OnGround = false; _gameModel.Player.IsJumping = false; _gameModel.Player.SetPosition(50f, 50f); _gameModel.ApplyGravity(50f); Assert.AreEqual(51f, _gameModel.Player.Pos.Y); } /// <summary> /// ���������, ��� ���������� �� �����������, ���� ����� �� �����. /// </summary> [TestMethod] public void Test_OnGround_PreventsFalling() { _gameModel.Player.OnGround = true; _gameModel.ApplyGravity(40f); Assert.AreEqual(80, (int)_gameModel.Player.Pos.Y); } /// <summary> /// ���������, ��� ����� BreakBlock() ��������� ���� �� ����������� ����� �����. /// </summary> [TestMethod] public void Test_BreakBlock_Left_AddsScore() { _gameModel.Player.Direction = Model.GameComponents.Entities.Direction.LEFT; _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y, (int)_gameModel.Player.Pos.X-1, BlockConstructor.getStoneBlock()); _gameModel.BreakBlock(); Assert.AreEqual(2, _gameModel.Player.Score); } /// <summary> /// ���������, ��� ����� BreakBlock() ��������� ���� �� ����������� ����� ������. /// </summary> [TestMethod] public void Test_BreakBlock_Right_AddsScore() { _gameModel.Player.Direction = Model.GameComponents.Entities.Direction.RIGHT; _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y, (int)_gameModel.Player.Pos.X + _gameModel.Player.PlayerWidth, BlockConstructor.getStoneBlock()); _gameModel.BreakBlock(); Assert.AreEqual(2, _gameModel.Player.Score); } /// <summary> /// ���������, ��� ����� BreakBlocksBelow() ��������� ���� �� ����������� ����� ��� �������. /// </summary> [TestMethod] public void Test_BreakBlocksBelow_AddsScore() { _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y+ _gameModel.Player.PlayerHeight, (int)_gameModel.Player.Pos.X, BlockConstructor.getDiamondBlock()); _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y+ _gameModel.Player.PlayerHeight, (int)_gameModel.Player.Pos.X+1, BlockConstructor.getDiamondBlock()); _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y+ _gameModel.Player.PlayerHeight, (int)_gameModel.Player.Pos.X+2, BlockConstructor.getDiamondBlock()); _gameModel.BreakBlocksBelow(); Assert.AreEqual(24, _gameModel.Player.Score); } /// <summary> /// ���������, ��� ����� BreakBlocksAbove() ��������� ���� �� ����������� ����� ��� �������. /// </summary> [TestMethod] public void Test_BreakBlocksAbove_AddsScore() { _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y - 1, (int)_gameModel.Player.Pos.X, BlockConstructor.getDiamondBlock()); _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y - 1, (int)_gameModel.Player.Pos.X+1, BlockConstructor.getDiamondBlock()); _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y - 1, (int)_gameModel.Player.Pos.X+2, BlockConstructor.getDiamondBlock()); _gameModel.BreakBlocksAbove(); Assert.AreEqual(24, _gameModel.Player.Score); } /// <summary> /// ���������, ��� IsBlockDirectlyAbovePlayer() ���������� true, ���� ���� ���� ��� �������. /// </summary> [TestMethod] public void Test_IsBlockDirectlyAbovePlayer_TrueWhenBlockAbove() { _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y-1, (int)_gameModel.Player.Pos.X,BlockConstructor.getDiamondBlock()); Assert.IsTrue(_gameModel.IsBlockDirectlyAbovePlayer()); } /// <summary> /// ���������, ��� IsBlockDirectlyAbovePlayer() ���������� false, ���� ������ ��� ������� ���. /// </summary> [TestMethod] public void Test_IsBlockDirectlyAbovePlayer_FalseWhenNoBlockAbove() { _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y-1, (int)_gameModel.Player.Pos.X, BlockConstructor.getAirBlock()); _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y-1, (int)_gameModel.Player.Pos.X+1, BlockConstructor.getAirBlock()); _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y-1, (int)_gameModel.Player.Pos.X+2, BlockConstructor.getAirBlock()); _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y-1, (int)_gameModel.Player.Pos.X+3, BlockConstructor.getAirBlock()); Assert.IsFalse(_gameModel.IsBlockDirectlyAbovePlayer()); } /// <summary> /// ���������, ��� AreBlocksToRightOfPlayer() ���������� true, ���� ���� ���� ������ �� ������. /// </summary> [TestMethod] public void Test_AreBlocksToRightOfPlayer_TrueWhenBlockRight() { _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y, (int)_gameModel.Player.Pos.X + _gameModel.Player.PlayerWidth, BlockConstructor.getDiamondBlock()); Assert.IsTrue(_gameModel.AreBlocksToRightOfPlayer()); } /// <summary> /// ���������, ��� AreBlocksToRightOfPlayer() ���������� false, ���� ������ ������ �� ������ ���. /// </summary> [TestMethod] public void Test_AreBlocksToRightOfPlayer_FalseWhenNoBlockRight() { _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y, (int)_gameModel.Player.Pos.X + _gameModel.Player.PlayerWidth, BlockConstructor.getAirBlock()); _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y+1, (int)_gameModel.Player.Pos.X + _gameModel.Player.PlayerWidth, BlockConstructor.getAirBlock()); _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y+2, (int)_gameModel.Player.Pos.X + _gameModel.Player.PlayerWidth, BlockConstructor.getAirBlock()); _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y+3, (int)_gameModel.Player.Pos.X + _gameModel.Player.PlayerWidth, BlockConstructor.getAirBlock()); Assert.IsFalse(_gameModel.AreBlocksToRightOfPlayer()); } /// <summary> /// ���������, ��� AreBlocksToLeftOfPlayer() ���������� true, ���� ���� ���� ����� �� ������. /// </summary> [TestMethod] public void Test_AreBlocksToLeftOfPlayer_TrueWhenBlockLeft() { _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y, (int)_gameModel.Player.Pos.X-1, BlockConstructor.getDiamondBlock()); Assert.IsTrue(_gameModel.AreBlocksToLeftOfPlayer()); } /// <summary> /// ���������, ��� AreBlocksToLeftOfPlayer() ���������� false, ���� ��� ����� ����� �� ������. /// </summary> [TestMethod] public void Test_AreNoBlocksToLeftOfPlayer_TrueWhenBlockLeft() { _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y, (int)_gameModel.Player.Pos.X + _gameModel.Player.PlayerWidth + 1, BlockConstructor.getAirBlock()); _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y + 1, (int)_gameModel.Player.Pos.X + _gameModel.Player.PlayerWidth + 1, BlockConstructor.getAirBlock()); _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y + 2, (int)_gameModel.Player.Pos.X + _gameModel.Player.PlayerWidth + 1, BlockConstructor.getAirBlock()); _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y + 3, (int)_gameModel.Player.Pos.X + _gameModel.Player.PlayerWidth + 1, BlockConstructor.getAirBlock()); Assert.IsTrue(!_gameModel.AreBlocksToLeftOfPlayer()); } /// <summary> /// ���������, ��� AreBlocksToLeftOfPlayer() ���������� false, ���� ������ ����� �� ������ ���. /// </summary> [TestMethod] public void Test_AreBlocksToLeftOfPlayer_FalseWhenNoBlockLeft() { _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y, (int)_gameModel.Player.Pos.X - 1, BlockConstructor.getAirBlock()); _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y+1, (int)_gameModel.Player.Pos.X - 1, BlockConstructor.getAirBlock()); _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y+2, (int)_gameModel.Player.Pos.X - 1, BlockConstructor.getAirBlock()); _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y+3, (int)_gameModel.Player.Pos.X - 1, BlockConstructor.getAirBlock()); Assert.IsFalse(_gameModel.AreBlocksToLeftOfPlayer()); } /// <summary> /// ���������, ��� AreBlocksToLeftOfPlayer() ���������� false, ���� ������ ����� �� ������ ���. /// </summary> [TestMethod] public void Test_AreBlocksUnderPlayer_FalseWhenNoBlockLeft() { _gameModel.Player.Direction = Model.GameComponents.Entities.Direction.RIGHT; _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y, (int)_gameModel.Player.Pos.X-1 + _gameModel.Player.PlayerWidth, BlockConstructor.getAirBlock()); _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y+1, (int)_gameModel.Player.Pos.X-1 + _gameModel.Player.PlayerWidth, BlockConstructor.getAirBlock()); _gameModel.Map.SetBlock((int)_gameModel.Player.Pos.Y+2, (int)_gameModel.Player.Pos.X-1 + _gameModel.Player.PlayerWidth, BlockConstructor.getAirBlock()); _gameModel.BreakBlock(); Assert.AreEqual(0, _gameModel.Player.Score); } /// <summary> /// ���������, ��� ����� UpdatePlayerPosition() ��������� ���������� � ������. /// </summary> [TestMethod] public void Test_UpdatePlayerPosition_AppliesGravity() { _gameModel.Player.OnGround = false; _gameModel.UpdatePlayerPosition(50f); Assert.AreEqual(81, _gameModel.Player.Pos.Y); } /// <summary> /// �������� ���������� ����� �� ��������� ������� ����� /// </summary> [TestMethod] public void MapGeneration_ValidBitmap_CreatesCorrectMap() { // Arrange int width = 5; int height = 5; Bitmap bitmap = new Bitmap(width, height); // ���������� ������� ������� for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (x == y) bitmap.SetPixel(x, y, Color.Gray); else bitmap.SetPixel(x, y, Color.White); } } // Act var mapModel = new MapModel(); var method = typeof(MapModel).GetMethod("ConvertToBlocks", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); var map = (AbstractBlock[,])method.Invoke(mapModel, new object[] { bitmap }); // Assert Assert.AreEqual(width, map.GetLength(1)); Assert.AreEqual(height, map.GetLength(0)); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (x == y) Assert.AreEqual(map[y, x].Name, BlockConstructor.getStoneBlock().Name); // �������� �� �������� ����� else Assert.AreEqual(map[y, x].Name, BlockConstructor.getAirBlock().Name); // �������� �� ��������� ����� } } } /// <summary> /// �������� ����, ��� �������� ����� ���������� �����. /// </summary> [TestMethod] public void MapGeneration_CustomColors_CreatesCorrectBlocks() { // Arrange int width = 3; int height = 3; Bitmap bitmap = new Bitmap(width, height); // ����������� ������ ��� ������������ bitmap.SetPixel(0, 0, Color.Gold); // ������� ���� bitmap.SetPixel(1, 1, Color.DarkGray); // �������� ���� bitmap.SetPixel(2, 2, Color.DarkBlue); // �������� ���� bitmap.SetPixel(0, 1, Color.SaddleBrown); // �������� ���� bitmap.SetPixel(0, 2, Color.Gray); // �������� ���� // Act var mapModel = new MapModel(); var method = typeof(MapModel).GetMethod("ConvertToBlocks", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); var map = (AbstractBlock[,])method.Invoke(mapModel, new object[] { bitmap }); Assert.AreEqual(map[0, 0].Name, BlockConstructor.getGoldBlock().Name); Assert.AreEqual(map[1, 1].Name, BlockConstructor.getIronBlock().Name); Assert.AreEqual(map[2, 2].Name, BlockConstructor.getDiamondBlock().Name); Assert.AreEqual(map[1, 0].Name, BlockConstructor.getMudBlock().Name); Assert.AreEqual(map[2, 0].Name, BlockConstructor.getStoneBlock().Name); } } }