Skip to content

Commit

Permalink
Fix broken unit tests. Delete invalid ones.
Browse files Browse the repository at this point in the history
  • Loading branch information
nightblade9 committed Dec 17, 2024
1 parent 39be9e0 commit df15580
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void Execute_ReturnsABunchOfCommands(string expectedCommand)

// Assert
var expected = $"[{Colours.Command}]{expectedCommand}[/]".ToUpperInvariant();
var actual = console.LastMessage;
var actual = string.Join('\n', console.Messages);
Assert.That(actual.ToUpperInvariant(), Does.Contain(expected));
}
}
17 changes: 0 additions & 17 deletions source/TextBlade.Core.Tests/Commands/DoNothingCommandTests.cs

This file was deleted.

18 changes: 0 additions & 18 deletions source/TextBlade.Core.Tests/Commands/ManuallySaveCommandTests.cs

This file was deleted.

24 changes: 13 additions & 11 deletions source/TextBlade.Core.Tests/Locations/DungeonTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using NSubstitute;
using NUnit.Framework;
using TextBlade.Core.Commands;
using TextBlade.Core.Inv;
using TextBlade.Core.IO;
using TextBlade.Core.Locations;

namespace TextBlade.Core.Tests.Locations;
Expand All @@ -15,15 +17,15 @@ public class DungeonTests
public void Constructor_Throws_IfNumFloorsIsNotPositive(int numFloors)
{
// Arrange/Act/Assert
var ex = Assert.Throws<ArgumentOutOfRangeException>(() => new Dungeon(null, "Lava Dungeon", "Mmm, ice-cream", numFloors, ["A", "B"], "Bossman 37"));
var ex = Assert.Throws<ArgumentOutOfRangeException>(() => new Dungeon("Lava Dungeon", "Mmm, ice-cream", numFloors, ["A", "B"], "Bossman 37"));
Assert.That(ex.Message, Does.Contain(nameof(numFloors)));
}

[Test]
public void Constructor_Throws_IfMonsterListIsEmpty()
{
// Arrange/Act/Assert
var ex = Assert.Throws<ArgumentException>(() => new Dungeon(null, "Ice Dungeon", "Mmm, stew", 33, [], "Bossman 38"));
var ex = Assert.Throws<ArgumentException>(() => new Dungeon("Ice Dungeon", "Mmm, stew", 33, [], "Bossman 38"));
Assert.That(ex.Message, Does.Contain("monsters"));
}

Expand Down Expand Up @@ -119,7 +121,7 @@ public void OnVictory_DoesNotGrantsLoot_OnNonLootFloors()
public void SetStateBasedOnCustomSaveData_ThrowsIfFloorNumberIsNegative(int floorNumber)
{
// Arrange
var dungeon = new Dungeon(null, "Test Dungeon", "N/A", 7, ["Troll"], "Nobody");
var dungeon = new Dungeon("Test Dungeon", "N/A", 7, ["Troll"], "Nobody");

// Act/Assert
var ex = Assert.Throws<ArgumentOutOfRangeException>(() => dungeon.SetStateBasedOnCustomSaveData(MakeCustomData(floorNumber, true)));
Expand All @@ -130,7 +132,7 @@ public void SetStateBasedOnCustomSaveData_ThrowsIfFloorNumberIsNegative(int floo
public void SetStateBasedOnCustomSaveData_SetsCurrentFloorNumber()
{
// Arrange
var dungeon = new Dungeon(null, "North Seaside Cave", "N/A", 40, ["Vole"], "Nobody");
var dungeon = new Dungeon("North Seaside Cave", "N/A", 40, ["Vole"], "Nobody");

// Act
dungeon.SetStateBasedOnCustomSaveData(MakeCustomData(33, false));
Expand All @@ -144,7 +146,7 @@ public void SetStateBasedOnCustomSaveData_DoesNotClearMonstersOrLoot_IfIsClearIs
{
// Arrange
var floorNumber = 0;
var dungeon = new Dungeon(null, "South Seaside Cave", "N/A", 1, ["Mole"], "Nobody");
var dungeon = new Dungeon("South Seaside Cave", "N/A", 1, ["Mole"], "Nobody");
dungeon.FloorLoot[$"B{floorNumber + 1}"] = ["Troll Hair", "Vole Tail", "Moleskin"];

// Act
Expand All @@ -160,7 +162,7 @@ public void SetStateBasedOnCustomSaveData_ClearsMonstersOrLoot_IfIsClearIsTrue()
{
// Arrange
var floorNumber = 0;
var dungeon = new Dungeon(null, "South Seaside Cave", "N/A", 3, ["Mole"], "Nobody");
var dungeon = new Dungeon("South Seaside Cave", "N/A", 3, ["Mole"], "Nobody");
dungeon.FloorLoot[$"B{floorNumber + 1}"] = ["Troll Hair"];
dungeon.FloorLoot[$"B{floorNumber + 2}"] = ["Vole Tail", "Moleskin"];

Expand Down Expand Up @@ -282,7 +284,7 @@ public void GetCommandFor_ReturnsTakeTurnsBattleCommand(string command)
var dungeon = CreateDungeon();

// Act
var actual = dungeon.GetCommandFor(command);
var actual = dungeon.GetCommandFor(Substitute.For<IConsole>(), command);

// Assert
Assert.That(actual, Is.InstanceOf<TakeTurnsBattleCommand>());
Expand All @@ -299,7 +301,7 @@ public void GetCommandFor_IncrementsFloorNumber_IfYouCanDescend(string command)
dungeon.SetStateBasedOnCustomSaveData(MakeCustomData(0, true));

// Act
dungeon.GetCommandFor(command);
dungeon.GetCommandFor(Substitute.For<IConsole>(), command);

// Assert
Assert.That(dungeon.GetCustomSaveData()["CurrentFloor"], Is.EqualTo(1)); // base 0
Expand All @@ -315,7 +317,7 @@ public void GetCommandFor_ReturnsDoNothingCommandForDescend_IfThereAreMonsters(s
var dungeon = CreateDungeon();

// Act
var actual = dungeon.GetCommandFor(command);
var actual = dungeon.GetCommandFor(Substitute.For<IConsole>(), command);

// Assert
Assert.That(dungeon.GetCustomSaveData()["CurrentFloor"], Is.EqualTo(0));
Expand All @@ -333,7 +335,7 @@ public void GetCommandFor_ReturnsDoNothingCommandForDescend_IfItsTheBottomFloor(
dungeon.SetStateBasedOnCustomSaveData(MakeCustomData(0, true));

// Act
var actual = dungeon.GetCommandFor(command);
var actual = dungeon.GetCommandFor(Substitute.For<IConsole>(), command);

// Assert
Assert.That(dungeon.GetCustomSaveData()["CurrentFloor"], Is.EqualTo(0));
Expand All @@ -358,7 +360,7 @@ private Dictionary<string, object> MakeCustomData(int floorNum, bool isFloorClea
class DungeonStub : Dungeon
{
public DungeonStub(string name, string description, int numFloors, List<string> monsters, string boss, string? locationClass = null)
: base(null, name, description, numFloors, monsters, boss, locationClass)
: base(name, description, numFloors, monsters, boss, locationClass)
{
}

Expand Down
6 changes: 4 additions & 2 deletions source/TextBlade.Core.Tests/Locations/InnTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using NSubstitute;
using NUnit.Framework;
using TextBlade.Core.Commands;
using TextBlade.Core.IO;
using TextBlade.Core.Locations;

namespace TextBlade.Core.Tests.Locations;
Expand All @@ -24,7 +26,7 @@ public void GetCommandFor_ReturnsDoNothingCommand(string input)
var inn = new Inn("The Inn of Fluffy Pillows", "An odd pillow-shaped white building", string.Empty);

// Act
var actual = inn.GetCommandFor(input);
var actual = inn.GetCommandFor(Substitute.For<IConsole>(), input);

// Assert
Assert.That(actual, Is.InstanceOf<DoNothingCommand>());
Expand All @@ -39,7 +41,7 @@ public void GetCommandFor_ReturnsInnCommand_IfInputIsS(string s)
var inn = new Inn("The Inn of Fluffy Minnows", "An odd fish-shaped white building", string.Empty);

// Act
var actual = inn.GetCommandFor(s);
var actual = inn.GetCommandFor(Substitute.For<IConsole>(), s);

// Assert
Assert.That(actual, Is.InstanceOf<SleepAtInnCommand>());
Expand Down
4 changes: 3 additions & 1 deletion source/TextBlade.Core.Tests/Locations/LocationTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using NSubstitute;
using NUnit.Framework;
using TextBlade.Core.Commands;
using TextBlade.Core.IO;
using TextBlade.Core.Locations;

namespace TextBlade.Core.Tests.Locations;
Expand All @@ -14,7 +16,7 @@ public void GetCommandFor_ReturnDoNothingCommand()
var location = new Location("A", "B");

// Act
var actual = location.GetCommandFor("q");
var actual = location.GetCommandFor(Substitute.For<IConsole>(), "q");

// Assert
Assert.That(actual, Is.InstanceOf<DoNothingCommand>());
Expand Down
8 changes: 5 additions & 3 deletions source/TextBlade.Core/Battle/BasicMonsterAi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@ public BasicMonsterAi(IConsole console, List<Character> party)
ArgumentNullException.ThrowIfNull(console);

_console = console;
_party = party.Where(p => p.CurrentHealth > 0).ToList();
_party = party;
}

public void ProcessTurnFor(Monster monster)
{
if (_party.Count == 0)
var validTargets = _party.Where(p => p.CurrentHealth > 0).ToList();
if (validTargets.Count == 0)
{
// Wiped out, nothing to do
return;
}

var target = _party[Random.Shared.Next(0, _party.Count)];
var target = validTargets[Random.Shared.Next(0, validTargets.Count)];

var damage = monster.Attack(target);
var message = $"{monster.Name} attacks {target.Name} for [{Colours.Highlight}]{damage}[/] damage! ";
Expand Down

0 comments on commit df15580

Please sign in to comment.