Skip to content

Commit

Permalink
Merge pull request #107 from benpollarduk/minor-issues
Browse files Browse the repository at this point in the history
Minor issues
  • Loading branch information
benpollarduk authored Nov 28, 2024
2 parents 133a0c8 + 176e033 commit b3917bc
Show file tree
Hide file tree
Showing 24 changed files with 378 additions and 272 deletions.
19 changes: 17 additions & 2 deletions NetAF.Examples/Assets/Regions/Hub/Rooms/Clearing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,16 @@ internal class Clearing : IAssetTemplate<Room>
/// <returns>The asset.</returns>
public Room Instantiate()
{
var room = new Room(Name, Description);
var room = new Room(Name, Description, examination: request =>
{
if (request.Scene.Examiner.Attributes.GetValue("Coin") == 0)
{
request.Scene.Examiner.Attributes.Add("Coin", 1);
return new Examination("Well look at that, you found a coin.");
}

return Room.DefaultRoomExamination(request);
});

var conversation = new Conversation(
[
Expand All @@ -46,7 +55,13 @@ public Room Instantiate()
new("Fine, suit yourself! Squarrk!", new ToName("ModeQuestion"))
]);

room.AddCharacter(new NonPlayableCharacter(new Identifier("Parrot"), new Description("A brightly colored parrot."), conversation: conversation));
room.AddCharacter(new NonPlayableCharacter(new Identifier("Parrot"), new Description("A brightly colored parrot."), conversation: conversation, interaction: item =>
{
if (item.Identifier.Equals("Knife"))
return new Interaction(InteractionResult.TargetExpires, item, "You slash at the parrot, in a flash of red feathers it is no more! The beast is vanquished!");

return new(InteractionResult.NoChange, item);
}));

return room;
}
Expand Down
1 change: 0 additions & 1 deletion NetAF.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
using NetAF.Extensions;
using NetAF.Logic;
using NetAF.Logic.Configuration;
using NetAF.Rendering.Console;

namespace NetAF.Examples
{
Expand Down
10 changes: 10 additions & 0 deletions NetAF.Tests/Assets/Attributes/AttributeManager_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ public void GivenNoAttributes_WhenGetAttributes_ThenReturnEmptyArray()
Assert.AreEqual(0, result.Length);
}

[TestMethod]
public void GivenNonExistentAttribute_WhenGetValue_ThenReturn0()
{
AttributeManager manager = new();

var result = manager.GetValue("");

Assert.AreEqual(0, result);
}

[TestMethod]
public void GivenNoAttributes_WhenAdd_ThenOneAttribute()
{
Expand Down
24 changes: 0 additions & 24 deletions NetAF.Tests/Assets/Attributes/Attribute_Tests.cs

This file was deleted.

22 changes: 22 additions & 0 deletions NetAF.Tests/Assets/Locations/Overworld_Tests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using NetAF.Assets.Locations;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetAF.Assets.Characters;
using NetAF.Assets;

namespace NetAF.Tests.Assets.Locations
{
Expand Down Expand Up @@ -72,5 +74,25 @@ public void GivenRegionIsNotPresent_WhenMoveRegion_ThenReturnFalse()

Assert.IsFalse(result);
}

[TestMethod]
public void GivenNotOverworld_WhenExamine_ThenReturnNonEmptyString()
{
var overworld = new Overworld(string.Empty, "An overworld");

var result = overworld.Examination(new ExaminationRequest(new PlayableCharacter("a", "b"), new ExaminationScene(null, new Room(string.Empty, string.Empty))));

Assert.IsTrue(result.Description.Length > 0);
}

[TestMethod]
public void GivenOverworld_WhenExamine_ThenReturnNonEmptyString()
{
var overworld = new Overworld(string.Empty, "An overworld");

var result = overworld.Examination(new ExaminationRequest(overworld, new ExaminationScene(null, new Room(string.Empty, string.Empty))));

Assert.IsTrue(result.Description.Length > 0);
}
}
}
22 changes: 21 additions & 1 deletion NetAF.Tests/Assets/Locations/Region_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using NetAF.Assets.Locations;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetAF.Commands;
using NetAF.Assets.Characters;

namespace NetAF.Tests.Assets.Locations
{
Expand Down Expand Up @@ -491,11 +492,30 @@ public void GivenCurrentRoomNullAndNoStartRoomAssigned_WhenEnter_ThenCurrentRoom
public void GivenCurrentRoomNullAndNoRooms_WhenEnter_ThenCurrentRoomIsNull()
{
var region = new Region(string.Empty, string.Empty);
var room = new Room(string.Empty, string.Empty);

region.Enter();

Assert.IsNull(region.CurrentRoom);
}

[TestMethod]
public void GivenNotRegion_WhenExamine_ThenReturnNonEmptyString()
{
var region = new Region(string.Empty, "A region");

var result = region.Examination(new ExaminationRequest(new PlayableCharacter("a", "b"), new ExaminationScene(null, new Room(string.Empty, string.Empty))));

Assert.IsTrue(result.Description.Length > 0);
}

[TestMethod]
public void GivenRegion_WhenExamine_ThenReturnNonEmptyString()
{
var region = new Region(string.Empty, "A region");

var result = region.Examination(new ExaminationRequest(region, new ExaminationScene(null, new Room(string.Empty, string.Empty))));

Assert.IsTrue(result.Description.Length > 0);
}
}
}
40 changes: 40 additions & 0 deletions NetAF.Tests/Assets/Locations/Room_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,5 +243,45 @@ public void GivenInvalid_WhenFindInteractionTarget_ThenReturnFalse()

Assert.IsFalse(result);
}

[TestMethod]
public void GivenNotRoom_WhenExamine_ThenReturnNonEmptyString()
{
var room = new Room(string.Empty, "A room");

var result = room.Examination(new ExaminationRequest(new PlayableCharacter("a", "b"), new ExaminationScene(null, room)));

Assert.IsTrue(result.Description.Length > 0);
}

[TestMethod]
public void GivenNoItems_WhenExamine_ThenReturnNonEmptyString()
{
var room = new Room(string.Empty, "A room");

var result = room.Examination(new ExaminationRequest(room, new ExaminationScene(null, room)));

Assert.IsTrue(result.Description.Length > 0);
}

[TestMethod]
public void Given1Item_WhenExamine_ThenReturnNonEmptyString()
{
var room = new Room(string.Empty, "A room", items: [new Item("a", "b")]);

var result = room.Examination(new ExaminationRequest(room, new ExaminationScene(null, room)));

Assert.IsTrue(result.Description.Length > 0);
}

[TestMethod]
public void Given2Items_WhenExamine_ThenReturnNonEmptyString()
{
var room = new Room(string.Empty, "A room", items: [new Item("a", "b"), new Item("c", "d")]);

var result = room.Examination(new ExaminationRequest(room, new ExaminationScene(null, room)));

Assert.IsTrue(result.Description.Length > 0);
}
}
}
4 changes: 2 additions & 2 deletions NetAF.Tests/Persistence/Json/JsonSave_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void GivenSimpleGame_WhenToJson_ThenExpectedStringReturned()
var game = Game.Create(new GameInfo(string.Empty, string.Empty, string.Empty), string.Empty, AssetGenerator.Retained(overworldMaker.Make(), new PlayableCharacter(string.Empty, string.Empty)), GameEndConditions.NoEnd, TestGameConfiguration.Default).Invoke();
game.Overworld.CurrentRegion.Enter();
var save = RestorePoint.Create("Test", game);
var expectedMinusDateTime = """{"Game":{"ActivePlayerIdentifier":"","Players":[{"Items":[],"IsAlive":true,"Identifier":"","IsPlayerVisible":true,"AttributeManager":{"Values":{}},"Commands":[]}],"Overworld":{"Regions":[{"Rooms":[{"HasBeenVisited":true,"Items":[{"Identifier":"","IsPlayerVisible":false,"AttributeManager":{"Values":{}},"Commands":[]}],"Exits":[],"Characters":[],"Identifier":"","IsPlayerVisible":true,"AttributeManager":{"Values":{}},"Commands":[]}],"CurrentRoom":"","IsVisibleWithoutDiscovery":false,"Identifier":"","IsPlayerVisible":true,"AttributeManager":{"Values":{}},"Commands":[]}],"CurrentRegion":"","Identifier":"","IsPlayerVisible":true,"AttributeManager":{"Values":{}},"Commands":[]},"InactivePlayerLocations":[]},"Name":"Test""";
var expectedMinusDateTime = """{"Game":{"ActivePlayerIdentifier":"","Players":[{"Items":[],"IsAlive":true,"Identifier":"","IsPlayerVisible":true,"AttributeManager":{"Values":[]},"Commands":[]}],"Overworld":{"Regions":[{"Rooms":[{"HasBeenVisited":true,"Items":[{"Identifier":"","IsPlayerVisible":false,"AttributeManager":{"Values":[]},"Commands":[]}],"Exits":[],"Characters":[],"Identifier":"","IsPlayerVisible":true,"AttributeManager":{"Values":[]},"Commands":[]}],"CurrentRoom":"","IsVisibleWithoutDiscovery":false,"Identifier":"","IsPlayerVisible":true,"AttributeManager":{"Values":[]},"Commands":[]}],"CurrentRegion":"","Identifier":"","IsPlayerVisible":true,"AttributeManager":{"Values":[]},"Commands":[]},"InactivePlayerLocations":[]},"Name":"Test""";
var json = JsonSave.ToJson(save);

Assert.IsTrue(json.StartsWith(expectedMinusDateTime));
Expand All @@ -33,7 +33,7 @@ public void GivenSimpleGame_WhenToJson_ThenExpectedStringReturned()
[TestMethod]
public void GivenSimpleJson_WhenFromJson_ThenValidSaveReturned()
{
var json = """{"Game":{"ActivePlayerIdentifier":"","Players":[{"Items":[],"IsAlive":true,"Identifier":"","IsPlayerVisible":true,"AttributeManager":{"Values":{}},"Commands":[]}],"Overworld":{"Regions":[{"Rooms":[{"HasBeenVisited":true,"Items":[{"Identifier":"","IsPlayerVisible":false,"AttributeManager":{"Values":{}},"Commands":[]}],"Exits":[],"Characters":[],"Identifier":"","IsPlayerVisible":true,"AttributeManager":{"Values":{}},"Commands":[]}],"CurrentRoom":"","IsVisibleWithoutDiscovery":false,"Identifier":"","IsPlayerVisible":true,"AttributeManager":{"Values":{}},"Commands":[]}],"CurrentRegion":"","Identifier":"","IsPlayerVisible":true,"AttributeManager":{"Values":{}},"Commands":[]},"InactivePlayerLocations":[]},"Name":"Test","CreationTime":"2024-11-16T18:55:20.0148219+00:00"}""";
var json = """{"Game":{"ActivePlayerIdentifier":"","Players":[{"Items":[],"IsAlive":true,"Identifier":"","IsPlayerVisible":true,"AttributeManager":{"Values":[]},"Commands":[]}],"Overworld":{"Regions":[{"Rooms":[{"HasBeenVisited":true,"Items":[{"Identifier":"","IsPlayerVisible":false,"AttributeManager":{"Values":[]},"Commands":[]}],"Exits":[],"Characters":[],"Identifier":"","IsPlayerVisible":true,"AttributeManager":{"Values":[]},"Commands":[]}],"CurrentRoom":"","IsVisibleWithoutDiscovery":false,"Identifier":"","IsPlayerVisible":true,"AttributeManager":{"Values":[]},"Commands":[]}],"CurrentRegion":"","Identifier":"","IsPlayerVisible":true,"AttributeManager":{"Values":[]},"Commands":[]},"InactivePlayerLocations":[]},"Name":"Test","CreationTime":"2024-11-28T16:36:00.6660777+00:00"}""";

var result = JsonSave.FromJson(json);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetAF.Assets.Attributes;
using NetAF.Serialization.Assets;

namespace NetAF.Tests.Serialization.Assets
{
[TestClass]
public class AttributeAndValueSerialization_Tests
{
[TestMethod]
public void GivenAttributeWhenNameIsA_WhenFromAttribute_ThenNameIsA()
{
Attribute attribute = new("A", "B", 5, 10);

AttributeAndValueSerialization result = AttributeAndValueSerialization.FromAttributeAndValue(new(attribute, 0));

Assert.AreEqual("A", result.Name);
}

[TestMethod]
public void GivenAttributeWhenDescriptionIsB_WhenFromAttribute_ThenDescriptionIsB()
{
Attribute attribute = new("A", "B", 5, 10);

AttributeAndValueSerialization result = AttributeAndValueSerialization.FromAttributeAndValue(new(attribute, 0));

Assert.AreEqual("B", result.Description);
}

[TestMethod]
public void GivenAttributeWhenMinimumIs5_WhenFromAttribute_ThenMinimumIs5()
{
Attribute attribute = new("A", "B", 5, 10);

AttributeAndValueSerialization result = AttributeAndValueSerialization.FromAttributeAndValue(new(attribute, 0));

Assert.AreEqual(5, result.Minimum);
}

[TestMethod]
public void GivenAttributeWhenMaximumIs10_WhenFromAttribute_ThenMaximumIs10()
{
Attribute attribute = new("A", "B", 5, 10);

AttributeAndValueSerialization result = AttributeAndValueSerialization.FromAttributeAndValue(new(attribute, 0));

Assert.AreEqual(10, result.Maximum);
}

[TestMethod]
public void GivenAttributeWhenValueIs3_WhenFromAttribute_ThenValueIs3()
{
Attribute attribute = new("A", "B", 5, 10);

AttributeAndValueSerialization result = AttributeAndValueSerialization.FromAttributeAndValue(new(attribute, 3));

Assert.AreEqual(3, result.Value);
}

[TestMethod]
public void GivenRestore_ThenNoExceptionThrown()
{
Assertions.NoExceptionThrown(() =>
{
AttributeAndValueSerialization value = new();
value.Restore(new System.Collections.Generic.KeyValuePair<Attribute, int>());
});
}
}
}
65 changes: 0 additions & 65 deletions NetAF.Tests/Serialization/Assets/AttributeSerialization_Tests.cs

This file was deleted.

Loading

0 comments on commit b3917bc

Please sign in to comment.