Skip to content

Commit

Permalink
Inproved coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ben_singer committed Nov 17, 2024
1 parent cdc0fea commit cc748fb
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
47 changes: 47 additions & 0 deletions NetAF.Tests/Commands/Scene/UseOn_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,52 @@ public void GivenItemOnPlayerInteractionCausesItemAndTargetExpire_WhenInvoke_The
Assert.IsFalse(result1);
Assert.IsFalse(result2);
}

[TestMethod]
public void GivenItemOnPlayerInteractionCausesItemExpire_WhenInvoke_ThenPlayerDoesNotHaveItem()
{
var item = new Item(Identifier.Empty, Description.Empty, true);
var room = new Room(Identifier.Empty, Description.Empty);
var region = new Region(string.Empty, string.Empty);
region.AddRoom(room, 0, 0, 0);
var overworld = new Overworld(string.Empty, string.Empty);
overworld.AddRegion(region);
var player = new PlayableCharacter(Identifier.Empty, Description.Empty, items: [item], interaction: (i) =>
{
return new Interaction(InteractionResult.ItemExpired, i);
});
var game = Game.Create(new GameInfo(string.Empty, string.Empty, string.Empty), string.Empty, AssetGenerator.Retained(overworld, player), GameEndConditions.NoEnd, TestGameConfiguration.Default).Invoke();
var command = new UseOn(item, player);
command.Invoke(game);

var result = player.HasItem(item);

Assert.IsFalse(result);
}

[TestMethod]
public void GivenItemOnNonPlayableCharacterInteractionCausesItemExpire_WhenInvoke_ThenNonPlayableCharacterDoesNotHaveItem()
{
var item = new Item(Identifier.Empty, Description.Empty, true);
var npc = new NonPlayableCharacter(Identifier.Empty, Description.Empty, interaction: (i) =>
{
return new Interaction(InteractionResult.ItemExpired, i);
});
npc.AddItem(item);
var room = new Room(Identifier.Empty, Description.Empty);
room.AddCharacter(npc);
var region = new Region(string.Empty, string.Empty);
region.AddRoom(room, 0, 0, 0);
var overworld = new Overworld(string.Empty, string.Empty);
overworld.AddRegion(region);
var player = new PlayableCharacter(Identifier.Empty, Description.Empty);
var game = Game.Create(new GameInfo(string.Empty, string.Empty, string.Empty), string.Empty, AssetGenerator.Retained(overworld, player), GameEndConditions.NoEnd, TestGameConfiguration.Default).Invoke();
var command = new UseOn(item, npc);
command.Invoke(game);

var result = npc.HasItem(item);

Assert.IsFalse(result);
}
}
}
20 changes: 12 additions & 8 deletions NetAF/Commands/Scene/UseOn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,27 @@ public sealed class UseOn(Item item, IInteractWithItem target) : ICommand
/// </summary>
/// <param name="game">The game.</param>
/// <param name="item">The item that expired.</param>
private static void ItemExpired(Game game, Item item)
/// <param name="target">The target that the item was used on.</param>
private static void ItemExpired(Game game, Item item, IInteractWithItem target)
{
List<IItemContainer> containers = [];

containers.Add(game.Player);
containers.Add(game.Overworld.CurrentRegion.CurrentRoom);
containers.AddRange(game.Overworld.CurrentRegion.CurrentRoom.Characters ?? []);

foreach (var container in from IItemContainer container in containers where container.Items.Contains(item) select container)
if (target is IItemContainer targetContainer)
containers.Add(targetContainer);

foreach (var container in from IItemContainer container in containers.Distinct() where container.Items.Contains(item) select container)
container.RemoveItem(item);
}

/// <summary>
/// Handle a target expiring.
/// </summary>
/// <param name="game">The game.</param>
/// <param name="target">The item that expired.</param>
/// <param name="target">The target that expired.</param>
private static void TargetExpired(Game game, IInteractWithItem target)
{
if (target is IExaminable examinable && game.Overworld.CurrentRegion.CurrentRoom.ContainsInteractionTarget(examinable.Identifier.Name))
Expand Down Expand Up @@ -93,27 +97,27 @@ public Reaction Invoke(Logic.Game game)
if (game.Player == null)
return new(ReactionResult.Error, "You must specify the character that is using this item.");

var result = target.Interact(item);
var interaction = target.Interact(item);

switch (result.Result)
switch (interaction.Result)
{
case InteractionResult.NeitherItemOrTargetExpired:
break;
case InteractionResult.ItemExpired:
ItemExpired(game, item);
ItemExpired(game, item, target);
break;
case InteractionResult.TargetExpired:
TargetExpired(game, target);
break;
case InteractionResult.ItemAndTargetExpired:
ItemExpired(game, item);
ItemExpired(game, item, target);
TargetExpired(game, target);
break;
default:
throw new NotImplementedException();
}

return new(ReactionResult.Inform, result.Description);
return new(ReactionResult.Inform, interaction.Description);
}

#endregion
Expand Down

0 comments on commit cc748fb

Please sign in to comment.