Skip to content

Commit

Permalink
Battle results applier tests
Browse files Browse the repository at this point in the history
Battle command *is* a command. Oops. Needed for testing.
  • Loading branch information
nightblade9 committed Dec 18, 2024
1 parent adc114e commit 8d2e9d1
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 1 deletion.
141 changes: 141 additions & 0 deletions source/TextBlade.Core.Tests/Battle/BattleResultsApplierTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
using NSubstitute;
using NUnit.Framework;
using TextBlade.Core.Battle;
using TextBlade.Core.Commands;
using TextBlade.Core.Commands.Display;
using TextBlade.Core.IO;
using TextBlade.Core.Locations;

namespace TextBlade.Core.Tests.Battle;

[TestFixture]
public class BattleResultsApplierTests
{
[Test]
public void ApplyResultsIfBattle_DoesNothing_IfCommandIsManualSaveAndLocationIsDungeon()
{
// Arrange
var command = new ManuallySaveCommand();
var location = new Dungeon("Rabbit's Warren", "Cute and deadly", 1, new List<string>() { "Rabbit" }, "X-Hare");
var data = new SaveData();

// Act
new BattleResultsApplier(Substitute.For<IConsole>()).ApplyResultsIfBattle(command, location, data);

// Assert: no gold = nothing happened
Assert.That(data.Gold, Is.EqualTo(0));
}

[Test]
public void ApplyResultsIfBattle_DoesNothing_IfCommandIsNotBattleCommand()
{
// Arrange
var command = new DoNothingCommand();
var location = new Dungeon("Rabbit's Warren", "Cute and deadly", 1, new List<string>() { "Rabbit" }, "X-Hare");
var data = CreateSaveData();

// Act
new BattleResultsApplier(Substitute.For<IConsole>()).ApplyResultsIfBattle(command, location, data);

// Assert: no gold = nothing happened
Assert.That(data.Gold, Is.EqualTo(0));
}

[Test]
public void ApplyResultsIfBattle_GivesGold()
{
// Arrange
var command = Substitute.For<IBattleCommand>();
command.TotalGold.Returns(100);
var location = new Dungeon("Rabbit's Warren", "Cute and deadly", 1, new List<string>() { "Rabbit" }, "X-Hare");
var data = CreateSaveData(150);

// Act
new BattleResultsApplier(Substitute.For<IConsole>()).ApplyResultsIfBattle(command, location, data);

// Assert
Assert.That(data.Gold, Is.EqualTo(250));
}

[Test]
public void ApplyResultsIfBattle_GivesExperiencePointsToAllLivingPlayers_IfVictory()
{
// Arrange
var command = Substitute.For<IBattleCommand>();
command.IsVictory.Returns(true);
command.TotalExperiencePoints.Returns(9999);

var location = new Location("Mountain Pass", "Omnious");
var data = CreateSaveData();
data.Party.Add(new Core.Characters.Character("Player Two", 100, 100, 100) { CurrentHealth = 0});

// Act
new BattleResultsApplier(Substitute.For<IConsole>()).ApplyResultsIfBattle(command, location, data);

// Assert
var p1 = data.Party[0];
// Alive: gained XP
Assert.That(p1.ExperiencePoints, Is.EqualTo(command.TotalExperiencePoints));
var p2 = data.Party[1];
// Dead: no XP
Assert.That(p2.ExperiencePoints, Is.EqualTo(0));
}

[Test]
public void ApplyResultsIfBattle_RevivesAllCharacters_IfDefeat()
{
// Arrange
var command = Substitute.For<IBattleCommand>();
command.IsVictory.Returns(false);

var location = new Location("Steppe Pass", "Lovely");
var data = CreateSaveData();
data.Party.Add(new Core.Characters.Character("Player Two", 100, 100, 100) { CurrentHealth = 0});

// Act
new BattleResultsApplier(Substitute.For<IConsole>()).ApplyResultsIfBattle(command, location, data);

// Assert
Assert.That(data.Party.All(p => p.CurrentHealth == 1));
}

[Test]
public void ApplyResultsIfBattle_GivesLoot_IfVictoryInDungeonAndFloorHasLoot()
{
// Arrange
var command = Substitute.For<IBattleCommand>();
command.IsVictory.Returns(true);

var location = new Dungeon("Owl's Nest", "Cute and deadly", 1, new List<string>() { "Owl" }, "Owlicious");
location.FloorLoot["B1"] = new List<string>
{
"Iron Sword",
"Potion-A",
"Potion-A",
};

var data = CreateSaveData();
data.Inventory.Add(ItemsData.GetItem("Potion-A"));

// Act
new BattleResultsApplier(Substitute.For<IConsole>()).ApplyResultsIfBattle(command, location, data);

// Assert
Assert.That(data.Inventory.ItemsInOrder.Any(a => a.Name == "Iron Sword"));
Assert.That(data.Inventory.ItemsInOrder.Any(a => a.Name == "Potion-A"));
Assert.That(data.Inventory.ItemQuantities["Potion-A"], Is.EqualTo(3)); // one existing, two new
}

private SaveData CreateSaveData(int gold = 0)
{
return new SaveData
{
Party = new List<Core.Characters.Character>
{
new ("Player One", 100, 10, 5, 3),
},
Gold = gold,
Inventory = new(),
};
}
}
2 changes: 1 addition & 1 deletion source/TextBlade.Core/Commands/IBattleCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace TextBlade.Core.Commands;
/// <summary>
/// Used for things that are self-contained battles. Like, "battle engines" or whatever.
/// </summary>
public interface IBattleCommand
public interface IBattleCommand : ICommand
{
public bool IsVictory { get; }
public int TotalGold { get; }
Expand Down

0 comments on commit 8d2e9d1

Please sign in to comment.