Files
Ajedrez_Purgatorio/Assets/Tests/EditMode/MoveValidatorTests.cs
T
jimmyabv f7d56066c0 feat: Purgatory Final duel vs La Muerte + SFX wiring + runtime options menu
- Duel flow: mating piece vs boss while board collapses (PurgatoryDuelManager/Rules/UI/MuerteQuotes)
- GameState.PurgatoryDuel + CampaignManager rescue/restart hooks + SquareClick routing
- Dice-based rescue of most valuable dead identity; final defeat restarts Chapter 1
- 6 SFX clips wired in Ch1/2/3 via AudioClipAssigner
- OptionsMenuController runtime audio/graphics panel, wired to MainMenu
- Tests migrated to Assets/Tests/EditMode subfolders + PurgatoryDuelRulesTests (200 total)
- Editor tools: DiagAssign, SpeakerDatabaseSetup, PurgatoryCanvasMaintenance
2026-08-24 19:39:52 -03:00

227 lines
6.7 KiB
C#

using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
namespace AjedrezPurgatorio.Tests.Unit.Chess
{
[TestFixture]
[Category("Unit")]
[Category("Chess")]
[Category("MoveValidator")]
[Category("FastTests")]
public class MoveValidatorTests
{
private MoveValidator _validator;
private CheckDetector _checkDetector;
private Piece[,] _board;
private GameObject _root;
[SetUp]
public void Setup()
{
_checkDetector = new CheckDetector();
_validator = new MoveValidator(_checkDetector);
_board = new Piece[8, 8];
_root = new GameObject("TestRoot");
}
[TearDown]
public void Teardown()
{
Object.DestroyImmediate(_root);
}
private T CreatePiece<T>(int x, int y, bool isWhite) where T : Piece
{
var go = new GameObject($"Piece_{typeof(T).Name}_{x}_{y}");
go.transform.SetParent(_root.transform);
var piece = go.AddComponent<T>();
piece.currentPos = new Vector2Int(x, y);
piece.isWhite = isWhite;
piece.hasMoved = true;
_board[x, y] = piece;
return piece;
}
#region Constructor Tests
[Test]
public void Constructor_NullCheckDetector_ThrowsArgumentNullException()
{
Assert.Throws<System.ArgumentNullException>(() => new MoveValidator(null));
}
#endregion
#region IsMoveLegal Tests
[Test]
public void IsMoveLegal_NullBoard_ReturnsFalse()
{
var king = CreatePiece<King>(4, 0, true);
LogAssert.Expect(LogType.Error, "[MoveValidator] Board o piece es null.");
Assert.IsFalse(_validator.IsMoveLegal(null, king, new Vector2Int(4, 1)));
}
[Test]
public void IsMoveLegal_NullPiece_ReturnsFalse()
{
LogAssert.Expect(LogType.Error, "[MoveValidator] Board o piece es null.");
Assert.IsFalse(_validator.IsMoveLegal(_board, null, new Vector2Int(4, 1)));
}
[Test]
public void IsMoveLegal_OutOfBoard_ReturnsFalse()
{
var king = CreatePiece<King>(4, 0, true);
Assert.IsFalse(_validator.IsMoveLegal(_board, king, new Vector2Int(-1, 0)));
Assert.IsFalse(_validator.IsMoveLegal(_board, king, new Vector2Int(8, 0)));
}
[Test]
public void IsMoveLegal_RookToValidSquare_ReturnsTrue()
{
CreatePiece<King>(4, 0, true);
var rook = CreatePiece<Rook>(0, 0, true);
Assert.IsTrue(_validator.IsMoveLegal(_board, rook, new Vector2Int(0, 4)));
}
[Test]
public void IsMoveLegal_RookBlockedByFriend_ReturnsFalse()
{
CreatePiece<King>(4, 0, true);
var rook = CreatePiece<Rook>(0, 0, true);
CreatePiece<Pawn>(0, 3, true);
Assert.IsFalse(_validator.IsMoveLegal(_board, rook, new Vector2Int(0, 5)));
}
[Test]
public void IsMoveLegal_RookCanCaptureEnemy_ReturnsTrue()
{
CreatePiece<King>(4, 0, true);
var rook = CreatePiece<Rook>(0, 0, true);
CreatePiece<Pawn>(0, 5, false);
Assert.IsTrue(_validator.IsMoveLegal(_board, rook, new Vector2Int(0, 5)));
}
[Test]
public void IsMoveLegal_KingMoveIntoCheck_ReturnsFalse()
{
var king = CreatePiece<King>(4, 3, true);
CreatePiece<Rook>(4, 7, false);
Assert.IsFalse(_validator.IsMoveLegal(_board, king, new Vector2Int(4, 4)));
}
[Test]
public void IsMoveLegal_KingMoveAwayFromCheck_ReturnsTrue()
{
var king = CreatePiece<King>(4, 3, true);
CreatePiece<Rook>(4, 7, false);
Assert.IsTrue(_validator.IsMoveLegal(_board, king, new Vector2Int(3, 3)));
}
#endregion
#region GetLegalMovesForPiece Tests
[Test]
public void GetLegalMovesForPiece_NullBoard_ReturnsEmptyList()
{
var king = CreatePiece<King>(4, 0, true);
LogAssert.Expect(LogType.Error, "[MoveValidator] Board o piece es null.");
var moves = _validator.GetLegalMovesForPiece(null, king);
Assert.IsNotNull(moves);
Assert.AreEqual(0, moves.Count);
}
[Test]
public void GetLegalMovesForPiece_NullPiece_ReturnsEmptyList()
{
LogAssert.Expect(LogType.Error, "[MoveValidator] Board o piece es null.");
var moves = _validator.GetLegalMovesForPiece(_board, null);
Assert.IsNotNull(moves);
Assert.AreEqual(0, moves.Count);
}
[Test]
public void GetLegalMovesForPiece_KingInCheck_OnlyLegalMoves()
{
var king = CreatePiece<King>(4, 3, true);
CreatePiece<Rook>(4, 7, false);
var moves = _validator.GetLegalMovesForPiece(_board, king);
Assert.IsTrue(moves.Count > 0, "King should have escape squares");
foreach (var move in moves)
{
Assert.IsFalse(
_checkDetector.WouldLeaveKingInCheck(_board, king, move),
$"Move to {move} should not leave king in check"
);
}
}
[Test]
public void GetLegalMovesForPiece_RookInOpenBoard_AllStraightMoves()
{
CreatePiece<King>(0, 0, true);
var rook = CreatePiece<Rook>(4, 4, true);
var moves = _validator.GetLegalMovesForPiece(_board, rook);
Assert.AreEqual(14, moves.Count);
}
#endregion
#region GetAllLegalMoves Tests
[Test]
public void GetAllLegalMoves_NullBoard_ReturnsEmptyList()
{
LogAssert.Expect(LogType.Error, "[MoveValidator] Board es null.");
var moves = _validator.GetAllLegalMoves(null, true);
Assert.IsNotNull(moves);
Assert.AreEqual(0, moves.Count);
}
[Test]
public void GetAllLegalMoves_KingOnly_CorrectCount()
{
CreatePiece<King>(4, 0, true);
var moves = _validator.GetAllLegalMoves(_board, true);
Assert.AreEqual(5, moves.Count);
}
#endregion
#region CountLegalMoves Tests
[Test]
public void CountLegalMoves_EmptyBoard_ReturnsZero()
{
Assert.AreEqual(0, _validator.CountLegalMoves(_board, true));
}
[Test]
public void CountLegalMoves_KingOnly_ReturnsCorrectCount()
{
CreatePiece<King>(4, 0, true);
Assert.AreEqual(5, _validator.CountLegalMoves(_board, true));
}
#endregion
}
}