Skip to content

Commit

Permalink
Merge pull request #109 from benpollarduk/hiding-restore
Browse files Browse the repository at this point in the history
Hiding restore
  • Loading branch information
benpollarduk authored Dec 4, 2024
2 parents f13e2fb + 43d8fe5 commit 805a782
Show file tree
Hide file tree
Showing 39 changed files with 190 additions and 62 deletions.
25 changes: 24 additions & 1 deletion NetAF.Tests/Interpretation/CustomCommandInterpreter_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public void GivenValidCustomCommandThatIsNotPlayerVisibleButIsStillInterpreted_W
interpreter.Interpret("Test", game);
}


[TestMethod]
public void GivenValidCustomCommandThatIsNotPlayerVisibleAndIsNotStillInterpreted_WhenInterpret_ThenResultWasInterpretedSuccessfullyIsFalse()
{
Expand All @@ -118,5 +117,29 @@ public void GivenValidCustomCommandThatIsNotPlayerVisibleAndIsNotStillInterprete

Assert.IsFalse(result.WasInterpretedSuccessfully);
}

[TestMethod]
public void GivenValidCustomCommandAndSingleArgument_WhenInterpret_ThenResultWasInterpretedSuccessfullyIsTrue()
{
var interpreter = new CustomCommandInterpreter();
CustomCommand[] commands =
[
new CustomCommand(new("Two word", string.Empty), true, true, (_, _) =>
{
return new(ReactionResult.Error, string.Empty);
})
];
var overworld = new Overworld(Identifier.Empty, Description.Empty, commands);
var region = new Region(Identifier.Empty, Description.Empty);
region.AddRoom(new(Identifier.Empty, Description.Empty, [new Exit(Direction.North)]), 0, 0, 0);
region.AddRoom(new(Identifier.Empty, Description.Empty, [new Exit(Direction.South)]), 0, 1, 0);
overworld.AddRegion(region);
var game = Game.Create(new GameInfo(string.Empty, string.Empty, string.Empty), string.Empty, AssetGenerator.Retained(overworld, new PlayableCharacter(string.Empty, string.Empty)), GameEndConditions.NoEnd, TestGameConfiguration.Default).Invoke();
game.Overworld.CurrentRegion.Enter();

var result = interpreter.Interpret("Two word args", game);

Assert.IsTrue(result.WasInterpretedSuccessfully);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace NetAF.Tests.Interpretation
{
[TestClass]
public class GameCommandInterpreter_Tests
public class SceneCommandInterpreter_Tests
{
[TestInitialize]
public void Setup()
Expand Down Expand Up @@ -245,5 +245,37 @@ public void GivenUseOn_WhenInterpret_ThenReturnTrue()

Assert.IsTrue(result.WasInterpretedSuccessfully);
}

[TestMethod]
public void GivenUseOnWhenBothItemsAreTwoWords_WhenInterpret_ThenReturnTrue()
{
var interpreter = new SceneCommandInterpreter();
var game = Game.Create(new GameInfo(string.Empty, string.Empty, string.Empty), string.Empty, AssetGenerator.Retained(overworld, new PlayableCharacter(string.Empty, string.Empty)), GameEndConditions.NoEnd, TestGameConfiguration.Default).Invoke();

var result = interpreter.Interpret($"{UseOn.UseCommandHelp.Command} test tube {UseOn.OnCommandHelp.Command} test egg", game);

Assert.IsTrue(result.WasInterpretedSuccessfully);
}

[TestMethod]
public void GivenInterpreter_WhenGetSupportedCommands_ThenReturnArrayWithSomeItems()
{
var interpreter = new SceneCommandInterpreter();

var result = interpreter.SupportedCommands;

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

[TestMethod]
public void GivenInterpreter_WhenGetContextualCommandHelp_ThenReturnArrayWithSomeItems()
{
var interpreter = new SceneCommandInterpreter();
var game = Game.Create(new GameInfo(string.Empty, string.Empty, string.Empty), string.Empty, AssetGenerator.Retained(overworld, new PlayableCharacter(string.Empty, string.Empty)), GameEndConditions.NoEnd, TestGameConfiguration.Default).Invoke();

var result = interpreter.GetContextualCommandHelp(game);

Assert.IsTrue(result.Length > 0);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetAF.Assets.Attributes;
using NetAF.Serialization;
using NetAF.Serialization.Assets;
using System.Collections.Generic;

namespace NetAF.Tests.Serialization.Assets
{
Expand Down Expand Up @@ -63,7 +65,7 @@ public void GivenRestore_ThenNoExceptionThrown()
Assertions.NoExceptionThrown(() =>
{
AttributeAndValueSerialization value = new();
value.Restore(new System.Collections.Generic.KeyValuePair<Attribute, int>());
((IObjectSerialization<KeyValuePair<Attribute, int>>)value).Restore(new KeyValuePair<Attribute, int>());
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetAF.Assets.Attributes;
using NetAF.Serialization;
using NetAF.Serialization.Assets;
using System.Linq;

Expand Down Expand Up @@ -27,7 +28,7 @@ public void Given0AttributesButARestorationWith1Attribute_WhenRestore_ThenValues
attributeManager2.Add(new Attribute("a", "b", 1, 10), 5);
var serialization = AttributeManagerSerialization.FromAttributeManager(attributeManager2);

serialization.Restore(attributeManager1);
((IObjectSerialization<AttributeManager>)serialization).Restore(attributeManager1);

Assert.AreEqual(1, attributeManager1.Count);
}
Expand All @@ -40,7 +41,7 @@ public void Given0Attributes_WhenRestoreFromARestorationWith1Attribute_When_Rest
attributeManager2.Add(new Attribute("a", "b", 1, 10), 5);
var serialization = AttributeManagerSerialization.FromAttributeManager(attributeManager2);

serialization.Restore(attributeManager1);
((IObjectSerialization<AttributeManager>)serialization).Restore(attributeManager1);
var attributeDictionary = attributeManager2.GetAsDictionary();
var attribute = attributeDictionary.ElementAt(0).Key;
var count = attributeDictionary.ElementAt(0).Value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetAF.Assets;
using NetAF.Assets.Characters;
using NetAF.Serialization;
using NetAF.Serialization.Assets;

namespace NetAF.Tests.Serialization.Assets
Expand Down Expand Up @@ -58,7 +59,7 @@ public void GivenCharacterThatIsAlive_WhenRestoreFromCharacterThatIsNotAlive_The
character2.Kill();
CharacterSerialization serialization = CharacterSerialization.FromCharacter(character2);

serialization.Restore(character);
((IObjectSerialization<Character>)serialization).Restore(character);

Assert.IsFalse(character.IsAlive);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetAF.Conversations;
using NetAF.Serialization;
using NetAF.Serialization.Assets;

namespace NetAF.Tests.Serialization.Assets
Expand Down Expand Up @@ -48,7 +49,7 @@ public void GivenAConversation_WhenRestoreFromWithNoCurrentParagraph_ThenCurrent
Conversation conversation2 = new();
ConversationSerialization serialization = ConversationSerialization.FromConversation(conversation2);

serialization.Restore(conversation);
((IObjectSerialization<Conversation>)serialization).Restore(conversation);

Assert.IsNull(conversation.CurrentParagraph);
}
Expand All @@ -62,7 +63,7 @@ public void GivenAConversation_WhenRestoreFromWithCurrentParagraph1_ThenCurrentP
conversation2.Next(null);
ConversationSerialization serialization = ConversationSerialization.FromConversation(conversation2);

serialization.Restore(conversation);
((IObjectSerialization<Conversation>)serialization).Restore(conversation);

Assert.AreEqual(conversation.Paragraphs[1], conversation.CurrentParagraph);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using NetAF.Assets;
using NetAF.Assets.Attributes;
using NetAF.Commands;
using NetAF.Serialization;
using NetAF.Serialization.Assets;

namespace NetAF.Tests.Serialization.Assets
Expand Down Expand Up @@ -67,7 +68,7 @@ public void GivenAnExaminable_WhenRestoreFrom_ThenIsPlayerVisibleSetCorrectly()
Item item2 = new(string.Empty, string.Empty) { IsPlayerVisible = true };
ExaminableSerialization serialization = ExaminableSerialization.FromIExaminable(item2);

serialization.Restore(item);
((IObjectSerialization<IExaminable>)serialization).Restore(item);

Assert.IsTrue(item.IsPlayerVisible);
}
Expand All @@ -80,7 +81,7 @@ public void GivenAnExaminable_WhenRestoreFrom_ThenAttributesSetCorrectly()
item2.Attributes.Add(new Attribute(string.Empty, string.Empty, 0, 1), 1);
ExaminableSerialization serialization = ExaminableSerialization.FromIExaminable(item2);

serialization.Restore(item);
((IObjectSerialization<IExaminable>)serialization).Restore(item);

Assert.AreEqual(1, item.Attributes.Count);
}
Expand Down
3 changes: 2 additions & 1 deletion NetAF.Tests/Serialization/Assets/ExitSerialization_Tests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetAF.Assets.Locations;
using NetAF.Serialization;
using NetAF.Serialization.Assets;

namespace NetAF.Tests.Serialization.Assets
Expand All @@ -24,7 +25,7 @@ public void GivenExitThatIsUnlocked_WhenRestoreFromExitThatIsLocked_ThenIsLocked
Exit exit2 = new(Direction.North, true);
ExitSerialization serialization = ExitSerialization.FromExit(exit2);

serialization.Restore(exit);
((IObjectSerialization<Exit>)serialization).Restore(exit);

Assert.IsTrue(exit.IsLocked);
}
Expand Down
3 changes: 2 additions & 1 deletion NetAF.Tests/Serialization/Assets/ItemSerialization_Tests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetAF.Assets;
using NetAF.Assets.Attributes;
using NetAF.Serialization;
using NetAF.Serialization.Assets;

namespace NetAF.Tests.Serialization.Assets
Expand All @@ -16,7 +17,7 @@ public void GivenAnExaminable_WhenRestoreFrom_ThenAttributesSetCorrectly()
item2.Attributes.Add(new Attribute(string.Empty, string.Empty, 0, 1), 1);
ItemSerialization serialization = ItemSerialization.FromItem(item2);

serialization.Restore(item);
((IObjectSerialization<Item>)serialization).Restore(item);

Assert.AreEqual(1, item.Attributes.Count);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetAF.Assets.Characters;
using NetAF.Conversations;
using NetAF.Serialization;
using NetAF.Serialization.Assets;

namespace NetAF.Tests.Serialization.Assets
Expand Down Expand Up @@ -36,7 +37,7 @@ public void GivenCharacter_WhenRestoreFromCharacterConversationElement0_ThenCurr
character2.Conversation.Next(null);
NonPlayableCharacterSerialization serialization = NonPlayableCharacterSerialization.FromNonPlayableCharacter(character2);

serialization.Restore(character);
((IObjectSerialization<NonPlayableCharacter>)serialization).Restore(character);

Assert.IsNotNull(character.Conversation.CurrentParagraph);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetAF.Assets.Locations;
using NetAF.Serialization;
using NetAF.Serialization.Assets;

namespace NetAF.Tests.Serialization.Assets
Expand Down Expand Up @@ -51,7 +52,7 @@ public void GivenOverworldWith2Regions_WhenRestoreFromOverworldWhereSecondRegion
overworld2.Move(overworld2.Regions[1]);
OverworldSerialization serialization = OverworldSerialization.FromOverworld(overworld2);

serialization.Restore(overworld);
((IObjectSerialization<Overworld>)serialization).Restore(overworld);

Assert.AreEqual("TARGET", overworld.CurrentRegion.Identifier.Name);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetAF.Logic;
using NetAF.Serialization;
using NetAF.Serialization.Assets;

namespace NetAF.Tests.Serialization.Assets
Expand Down Expand Up @@ -44,7 +45,7 @@ public void GivenEmptyPlayableCharacterLocation_WhenRestoreFrom_ThenPlayableChar
PlayableCharacterLocation location2 = new(string.Empty, string.Empty, string.Empty);
PlayableCharacterLocationSerialization serialization = PlayableCharacterLocationSerialization.FromPlayableCharacterLocation(location);

serialization.Restore(location2);
((IObjectSerialization<PlayableCharacterLocation>)serialization).Restore(location2);

Assert.AreEqual("a", location2.PlayerIdentifier);
Assert.AreEqual("b", location2.RegionIdentifier);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetAF.Assets.Locations;
using NetAF.Serialization;
using NetAF.Serialization.Assets;

namespace NetAF.Tests.Serialization.Assets
Expand Down Expand Up @@ -54,7 +55,7 @@ public void GivenRegionWith2Rooms_WhenRestoreFromRegionWhereSecondRoomIsCurrentR
region2.Move(Direction.North);
RegionSerialization serialization = RegionSerialization.FromRegion(region2);

serialization.Restore(region);
((IObjectSerialization<Region>)serialization).Restore(region);

Assert.AreEqual("TARGET", region.CurrentRoom.Identifier.Name);
}
Expand Down
3 changes: 2 additions & 1 deletion NetAF.Tests/Serialization/Assets/RoomSerialization_Tests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetAF.Assets.Locations;
using NetAF.Serialization;
using NetAF.Serialization.Assets;

namespace NetAF.Tests.Serialization.Assets
Expand Down Expand Up @@ -102,7 +103,7 @@ public void GivenRoomThatHasNotBeenVisited_WhenRestoreFromRoomThatHasBeenVisited
room2.MovedInto(Direction.North);
RoomSerialization serialization = RoomSerialization.FromRoom(room2);

serialization.Restore(room);
((IObjectSerialization<Room>)serialization).Restore(room);

Assert.IsTrue(room.HasBeenVisited);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void GivenACustomCommand_WhenRestoreFrom_ThenIsPlayerVisibleSetCorrectly(

CustomCommandSerialization serialization = CustomCommandSerialization.FromCustomCommand(command1);

serialization.Restore(command2);
((IObjectSerialization<CustomCommand>)serialization).Restore(command2);

Assert.IsTrue(command2.IsPlayerVisible);
}
Expand Down
6 changes: 3 additions & 3 deletions NetAF.Tests/Serialization/GameSerialization_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void GivenAGame_WhenRestoreFromSerializedAndPlayerTookAnItemFromARoom_The

GameSerialization serialization = GameSerialization.FromGame(game2);

serialization.Restore(game);
((IObjectSerialization<Game>)serialization).Restore(game);

Assert.AreEqual(1, game.Player.Items.Length);
Assert.AreEqual(0, room.Items.Length);
Expand Down Expand Up @@ -126,7 +126,7 @@ public void GivenAGame_WhenRestoreFromSerializedAndPlayerDroppedAnItemInARoom_Th

GameSerialization serialization = GameSerialization.FromGame(game2);

serialization.Restore(game);
((IObjectSerialization<Game>)serialization).Restore(game);

Assert.AreEqual(0, game.Player.Items.Length);
Assert.AreEqual(1, room.Items.Length);
Expand Down Expand Up @@ -162,7 +162,7 @@ public void GivenAGame_WhenRestoreFromSerializedAndCharacterMovedFromRoom1ToRoom

GameSerialization serialization = GameSerialization.FromGame(game2);

serialization.Restore(game);
((IObjectSerialization<Game>)serialization).Restore(game);

Assert.AreEqual(0, roomA.Characters.Length);
Assert.AreEqual(1, roomB.Characters.Length);
Expand Down
2 changes: 1 addition & 1 deletion NetAF/Assets/Characters/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void IRestoreFromObjectSerialization<CharacterSerialization>.RestoreFrom(Charact
foreach (var item in Items)
{
var itemSerialization = Array.Find(serialization.Items, x => item.Identifier.Equals(x.Identifier));
itemSerialization?.Restore(item);
((IObjectSerialization<Item>)itemSerialization)?.Restore(item);
}
}

Expand Down
2 changes: 1 addition & 1 deletion NetAF/Assets/Locations/Overworld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ void IRestoreFromObjectSerialization<OverworldSerialization>.RestoreFrom(Overwor
foreach (var region in Regions)
{
var regionSerialization = Array.Find(serialization.Regions, x => region.Identifier.Equals(x.Identifier));
regionSerialization?.Restore(region);
((IObjectSerialization<Region>)regionSerialization)?.Restore(region);
}

CurrentRegion = Array.Find(Regions, x => x.Identifier.Equals(serialization.CurrentRegion));
Expand Down
2 changes: 1 addition & 1 deletion NetAF/Assets/Locations/Region.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ void IRestoreFromObjectSerialization<RegionSerialization>.RestoreFrom(RegionSeri
foreach (var room in rooms)
{
var roomSerialization = Array.Find(serialization.Rooms, x => room.Identifier.Equals(x.Identifier));
roomSerialization?.Restore(room);
((IObjectSerialization<Room>)roomSerialization)?.Restore(room);
}

CurrentRoom = Array.Find(rooms, x => x.Identifier.Equals(serialization.CurrentRoom));
Expand Down
6 changes: 3 additions & 3 deletions NetAF/Assets/Locations/Room.cs
Original file line number Diff line number Diff line change
Expand Up @@ -529,19 +529,19 @@ void IRestoreFromObjectSerialization<RoomSerialization>.RestoreFrom(RoomSerializ
foreach (var exit in Exits)
{
var exitSerialization = Array.Find(serialization.Exits, x => exit.Identifier.Equals(x.Identifier));
exitSerialization?.Restore(exit);
((IObjectSerialization<Exit>)exitSerialization)?.Restore(exit);
}

foreach (var item in Items)
{
var itemSerialization = Array.Find(serialization.Items, x => item.Identifier.Equals(x.Identifier));
itemSerialization?.Restore(item);
((IObjectSerialization<Item>)itemSerialization)?.Restore(item);
}

foreach (var character in Characters)
{
var characterSerialization = Array.Find(serialization.Characters, x => character.Identifier.Equals(x.Identifier));
characterSerialization?.Restore(character);
((IObjectSerialization<NonPlayableCharacter>)characterSerialization)?.Restore(character);
}
}

Expand Down
Loading

0 comments on commit 805a782

Please sign in to comment.