I'll help you create a set of edit mode and runtime tests for a Match 3 game. I'll break these down into key testing categories.
csharp
using UnityEngine;
using UnityEngine.TestTools;
using NUnit.Framework;
using System.Collections;
// Edit Mode Tests
public class Match3EditModeTests
{
private Grid grid;
private const int GRID_SIZE = 12;
private const int COLOR_COUNT = 6;
[SetUp]
public void Setup()
{
grid = new Grid(GRID_SIZE, COLOR_COUNT);
}
[Test]
public void GridInitialization_CorrectDimensions()
{
Assert.AreEqual(GRID_SIZE, grid.Width);
Assert.AreEqual(GRID_SIZE, grid.Height);
}
[Test]
public void TileColors_WithinValidRange()
{
bool allValid = true;
for (int x = 0; x < GRID_SIZE; x++)
{
for (int y = 0; y < GRID_SIZE; y++)
{
int color = grid.GetTileColor(x, y);
if (color < 0 || color >= COLOR_COUNT)
{
allValid = false;
break;
}
}
}
Assert.IsTrue(allValid);
}
[Test]
public void SwapTiles_ValidSwap()
{
Vector2Int pos1 = new Vector2Int(0, 0);
Vector2Int pos2 = new Vector2Int(0, 1);
int color1 = grid.GetTileColor(pos1.x, pos1.y);
int color2 = grid.GetTileColor(pos2.x, pos2.y);
grid.SwapTiles(pos1, pos2);
Assert.AreEqual(color1, grid.GetTileColor(pos2.x, pos2.y));
Assert.AreEqual(color2, grid.GetTileColor(pos1.x, pos1.y));
}
[Test]
public void CheckMatch_HorizontalMatch3()
{
// Set up a known horizontal match
grid.SetTileColor(0, 0, 0);
grid.SetTileColor(1, 0, 0);
grid.SetTileColor(2, 0, 0);
Assert.IsTrue(grid.HasMatch(new Vector2Int(1, 0)));
}
}
// Runtime Tests
public class Match3PlayModeTests : MonoBehaviour
{
private GameController gameController;
[UnityTest]
public IEnumerator TileFalling_CompletesInReasonableTime()
{
gameController = new GameController();
gameController.ClearMatch(new Vector2Int(0, 0));
float maxWaitTime = 5f;
float elapsedTime = 0f;
bool tilesSettled = false;
while (!tilesSettled && elapsedTime < maxWaitTime)
{
yield return new WaitForSeconds(0.1f);
elapsedTime += 0.1f;
tilesSettled = gameController.AreTilesSettled();
}
Assert.IsTrue(tilesSettled);
}
[UnityTest]
public IEnumerator UserInput_CorrectlyProcessed()
{
gameController = new GameController();
Vector2Int clickPos = new Vector2Int(5, 5);
gameController.ProcessInput(clickPos);
yield return new WaitForSeconds(0.1f);
Assert.IsTrue(gameController.IsSelectedTile(clickPos));
}
[UnityTest]
public IEnumerator MatchAnimation_PlaysCorrectly()
{
gameController = new GameController();
// Set up a match
Vector2Int[] matchPositions = new Vector2Int[] {
new Vector2Int(0, 0),
new Vector2Int(1, 0),
new Vector2Int(2, 0)
};
gameController.TriggerMatch(matchPositions);
yield return new WaitForSeconds(gameController.GetMatchAnimationDuration());
Assert.IsTrue(gameController.IsAnimationComplete());
}
[UnityTest]
public IEnumerator Score_CorrectlyUpdated()
{
gameController = new GameController();
int initialScore = gameController.GetScore();
// Create and clear a match
Vector2Int[] matchPositions = new Vector2Int[] {
new Vector2Int(0, 0),
new Vector2Int(1, 0),
new Vector2Int(2, 0)
};
gameController.TriggerMatch(matchPositions);
yield return new WaitForSeconds(gameController.GetMatchAnimationDuration());
Assert.Greater(gameController.GetScore(), initialScore);
}
}
These tests cover the main functionality of a Match 3 game. Here's what each test verifies:
Edit Mode Tests:
Grid initialization with correct dimensions
Valid color range for all tiles
Proper tile swapping
Match detection for horizontal matches
Runtime Tests:
Tile falling mechanics and timing
User input processing
Match animation playback
Score system functionality
Additional tests you might want to add:
Diagonal match detection (if implemented)
Special tile creation
Chain reaction matches
Game over conditions
Save/load system
Power-up functionality
Edge case handling for grid boundaries
Remember to:
Mock necessary components for edit mode tests
Use appropriate setup and teardown methods
Test edge cases and invalid inputs
Verify game state after each significant action
Test performance with large chain reactions
Would you like me to elaborate on any of these tests or add additional test cases for specific features?