Skip to content

Commit

Permalink
Merge pull request #101 from benpollarduk/deep-help
Browse files Browse the repository at this point in the history
Improved help to allow deeper commands help to be displayed
  • Loading branch information
benpollarduk authored Nov 22, 2024
2 parents 850d88d + 5ab92b7 commit 51475af
Show file tree
Hide file tree
Showing 41 changed files with 609 additions and 261 deletions.
45 changes: 45 additions & 0 deletions NetAF.Tests/Commands/Global/CommandList_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using NetAF.Assets;
using NetAF.Assets.Characters;
using NetAF.Assets.Locations;
using NetAF.Commands.Global;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetAF.Logic;
using NetAF.Logic.Modes;
using NetAF.Commands;

namespace NetAF.Tests.Commands.Global
{
[TestClass]
public class CommandList_Tests
{
[TestMethod]
public void GivenNullGame_WhenInvoke_ThenError()
{
var command = new CommandList();

var result = command.Invoke(null);

Assert.AreEqual(ReactionResult.Error, result.Result);
}

[TestMethod]
public void GivenValidGame_WhenInvoke_ThenGameModeChanged()
{
var room = new Room(Identifier.Empty, Description.Empty);
var character = new PlayableCharacter(Identifier.Empty, Description.Empty);
var item = new Item(new Identifier("A"), Description.Empty, true);
room.AddItem(item);
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 game = Game.Create(new GameInfo(string.Empty, string.Empty, string.Empty), string.Empty, AssetGenerator.Retained(overworld, character), GameEndConditions.NoEnd, TestGameConfiguration.Default).Invoke();
game.ChangeMode(new AboutMode());
var command = new CommandList();

var result = command.Invoke(game);

Assert.AreEqual(ReactionResult.GameModeChanged, result.Result);
}
}
}
4 changes: 2 additions & 2 deletions NetAF.Tests/Commands/Global/Help_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Help_Tests
[TestMethod]
public void GivenNullGame_WhenInvoke_ThenError()
{
var command = new Help();
var command = new Help(End.CommandHelp);

var result = command.Invoke(null);

Expand All @@ -35,7 +35,7 @@ public void GivenValidGame_WhenInvoke_ThenGameModeChanged()
overworld.AddRegion(region);
var game = Game.Create(new GameInfo(string.Empty, string.Empty, string.Empty), string.Empty, AssetGenerator.Retained(overworld, character), GameEndConditions.NoEnd, TestGameConfiguration.Default).Invoke();
game.ChangeMode(new AboutMode());
var command = new Help();
var command = new Help(End.CommandHelp);

var result = command.Invoke(game);

Expand Down
26 changes: 0 additions & 26 deletions NetAF.Tests/Extensions/CommandHelpExtensions_Tests.cs

This file was deleted.

49 changes: 48 additions & 1 deletion NetAF.Tests/Interpretation/GlobalCommandInterpreter_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using NetAF.Interpretation;
using NetAF.Logic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetAF.Logic.Modes;

namespace NetAF.Tests.Interpretation
{
Expand Down Expand Up @@ -67,13 +68,59 @@ public void GivenExit_WhenInterpret_ThenReturnTrue()
}

[TestMethod]
public void GivenHelp_WhenInterpret_ThenReturnTrue()
public void GivenHelpWithCommand_WhenInterpret_ThenReturnTrue()
{
var interpreter = new GlobalCommandInterpreter();
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.ChangeMode(new SceneMode());

var result = interpreter.Interpret($"{NetAF.Commands.Global.Help.CommandHelp.Command} {NetAF.Commands.Scene.Examine.CommandHelp.Command}", game);

Assert.IsTrue(result.WasInterpretedSuccessfully);
}

[TestMethod]
public void GivenHelpWithShortcut_WhenInterpret_ThenReturnTrue()
{
var interpreter = new GlobalCommandInterpreter();
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.ChangeMode(new SceneMode());

var result = interpreter.Interpret($"{NetAF.Commands.Global.Help.CommandHelp.Command} {NetAF.Commands.Scene.Examine.CommandHelp.Shortcut}", game);

Assert.IsTrue(result.WasInterpretedSuccessfully);
}

[TestMethod]
public void GivenHelpWithNoCommand_WhenInterpret_ThenReturnFalse()
{
var interpreter = new GlobalCommandInterpreter();
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(NetAF.Commands.Global.Help.CommandHelp.Command, game);

Assert.IsFalse(result.WasInterpretedSuccessfully);
}

[TestMethod]
public void GivenHelpWithUnknownCommand_WhenInterpret_ThenReturnFalse()
{
var interpreter = new GlobalCommandInterpreter();
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($"{NetAF.Commands.Global.Help.CommandHelp.Command} ABC", game);

Assert.IsFalse(result.WasInterpretedSuccessfully);
}

[TestMethod]
public void GivenCommands_WhenInterpret_ThenReturnTrue()
{
var interpreter = new GlobalCommandInterpreter();
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(NetAF.Commands.Global.CommandList.CommandHelp.Command, game);

Assert.IsTrue(result.WasInterpretedSuccessfully);
}

Expand Down
33 changes: 32 additions & 1 deletion NetAF.Tests/Logic/Game_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,28 @@
using NetAF.Logic.Modes;
using NetAF.Logic.Configuration;
using NetAF.Commands;
using NetAF.Commands.Scene;

namespace NetAF.Tests.Logic
{
[TestClass]
public class Game_Tests
{
[TestMethod]
public void GivenEmptyRoom_WhenGetContextualCommands_ThenNotNullOrEmpty()
{
RegionMaker regionMaker = new(string.Empty, string.Empty);
Room room = new(string.Empty, string.Empty);
regionMaker[0, 0, 0] = room;
OverworldMaker overworldMaker = new(string.Empty, string.Empty, regionMaker);
var game = Game.Create(new(string.Empty, string.Empty, string.Empty), string.Empty, AssetGenerator.Retained(overworldMaker.Make(), new PlayableCharacter(string.Empty, string.Empty)), GameEndConditions.NoEnd, TestGameConfiguration.Default).Invoke();

var result = game.GetContextualCommands();

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

[TestMethod]
public void GivenEmptyRoom_WhenGetAllPlayerVisibleExaminables_ThenNotNull()
{
Expand Down Expand Up @@ -165,6 +181,21 @@ public void GivenSimpleGame_WhenChangeModeToAbout_ThenNoExceptionThrown()
});
}

[TestMethod]
public void GivenSimpleGame_WhenChangeModeToCommandList_ThenNoExceptionThrown()
{
Assertions.NoExceptionThrown(() =>
{
RegionMaker regionMaker = new(string.Empty, string.Empty);
Room room = new(string.Empty, string.Empty);
regionMaker[0, 0, 0] = room;
OverworldMaker overworldMaker = new(string.Empty, string.Empty, regionMaker);
var game = Game.Create(new(string.Empty, string.Empty, string.Empty), string.Empty, AssetGenerator.Retained(overworldMaker.Make(), new PlayableCharacter(string.Empty, string.Empty)), GameEndConditions.NoEnd, TestGameConfiguration.Default).Invoke();

game.ChangeMode(new CommandListMode([]));
});
}

[TestMethod]
public void GivenSimpleGame_WhenChangeModeToHelp_ThenNoExceptionThrown()
{
Expand All @@ -176,7 +207,7 @@ public void GivenSimpleGame_WhenChangeModeToHelp_ThenNoExceptionThrown()
OverworldMaker overworldMaker = new(string.Empty, string.Empty, regionMaker);
var game = Game.Create(new(string.Empty, string.Empty, string.Empty), string.Empty, AssetGenerator.Retained(overworldMaker.Make(), new PlayableCharacter(string.Empty, string.Empty)), GameEndConditions.NoEnd, TestGameConfiguration.Default).Invoke();

game.ChangeMode(new HelpMode([]));
game.ChangeMode(new HelpMode(Take.CommandHelp));
});
}

Expand Down
31 changes: 31 additions & 0 deletions NetAF.Tests/Logic/Modes/CommandListMode_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using NetAF.Logic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetAF.Assets.Characters;
using NetAF.Assets.Locations;
using NetAF.Utilities;
using NetAF.Logic.Modes;
using NetAF.Commands.Global;
using NetAF.Commands.Scene;

namespace NetAF.Tests.Logic.Modes
{
[TestClass]
public class CommandListMode_Tests
{
[TestMethod]
public void GivenNew_WhenRender_ThenNoExceptionThrown()
{
Assertions.NoExceptionThrown(() =>
{
RegionMaker regionMaker = new(string.Empty, string.Empty);
Room room = new(string.Empty, string.Empty);
regionMaker[0, 0, 0] = room;
OverworldMaker overworldMaker = new(string.Empty, string.Empty, regionMaker);
var game = Game.Create(new(string.Empty, string.Empty, string.Empty), string.Empty, AssetGenerator.Retained(overworldMaker.Make(), new PlayableCharacter(string.Empty, string.Empty)), GameEndConditions.NoEnd, TestGameConfiguration.Default).Invoke();
var mode = new CommandListMode([End.CommandHelp, Take.CommandHelp]);

mode.Render(game);
});
}
}
}
3 changes: 2 additions & 1 deletion NetAF.Tests/Logic/Modes/HelpMode_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using NetAF.Assets.Locations;
using NetAF.Utilities;
using NetAF.Logic.Modes;
using NetAF.Commands.Global;

namespace NetAF.Tests.Logic.Modes
{
Expand All @@ -20,7 +21,7 @@ public void GivenNew_WhenRender_ThenNoExceptionThrown()
regionMaker[0, 0, 0] = room;
OverworldMaker overworldMaker = new(string.Empty, string.Empty, regionMaker);
var game = Game.Create(new(string.Empty, string.Empty, string.Empty), string.Empty, AssetGenerator.Retained(overworldMaker.Make(), new PlayableCharacter(string.Empty, string.Empty)), GameEndConditions.NoEnd, TestGameConfiguration.Default).Invoke();
var mode = new HelpMode([]);
var mode = new HelpMode(End.CommandHelp);

mode.Render(game);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetAF.Assets;
using NetAF.Commands.Scene;
using NetAF.Rendering.FrameBuilders;
using NetAF.Rendering.FrameBuilders.Console;

namespace NetAF.Tests.Rendering.FrameBuilders.Console
{
[TestClass]
public class ConsoleCommandListFrameBuilder_Tests
{
[TestMethod]
public void GivenDefaults_WhenBuild_ThenNoException()
{
Assertions.NoExceptionThrown(() =>
{
var gridStringBuilder = new GridStringBuilder();
var builder = new ConsoleCommandListFrameBuilder(gridStringBuilder);

builder.Build(string.Empty, string.Empty, [Take.CommandHelp], new Size(80, 50));
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,26 @@ namespace NetAF.Tests.Rendering.FrameBuilders.Console
public class ConsoleHelpFrameBuilder_Tests
{
[TestMethod]
public void GivenDefaults_WhenBuild_ThenNoException()
public void GivenDefaultsWithNoInstructions_WhenBuild_ThenNoException()
{
Assertions.NoExceptionThrown(() =>
{
var gridStringBuilder = new GridStringBuilder();
var builder = new ConsoleHelpFrameBuilder(gridStringBuilder);

builder.Build(string.Empty, string.Empty, [], new Size(80, 50));
builder.Build(new NetAF.Commands.CommandHelp("Test", "Test 2"), new Size(80, 50));
});
}

[TestMethod]
public void GivenDefaultsWithInstructions_WhenBuild_ThenNoException()
{
Assertions.NoExceptionThrown(() =>
{
var gridStringBuilder = new GridStringBuilder();
var builder = new ConsoleHelpFrameBuilder(gridStringBuilder);

builder.Build(new NetAF.Commands.CommandHelp("Test", "Test 2", "Test 3."), new Size(80, 50));
});
}
}
Expand Down
27 changes: 27 additions & 0 deletions NetAF.Tests/Utilities/StringUtilities_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,32 @@ public void GivenTwoAttributes_WhenConstructAttributesAsString_ThenTestColon1Tab

Assert.AreEqual("Test: 1\tTest2: 1", result);
}

[TestMethod]
public void GivenEmptyString_WhenSplitTextToVerbAndNoun_ThenReturnEmptyVerbAndNoun()
{
StringUtilities.SplitTextToVerbAndNoun(string.Empty, out var verb, out var noun);

Assert.AreEqual(string.Empty, verb);
Assert.AreEqual(string.Empty, noun);
}

[TestMethod]
public void GivenABC_WhenSplitTextToVerbAndNoun_TheNounABCVerbEmpty()
{
StringUtilities.SplitTextToVerbAndNoun("ABC", out var verb, out var noun);

Assert.AreEqual("ABC", verb);
Assert.AreEqual(string.Empty, noun);
}

[TestMethod]
public void GivenABCSpaceXYZ_WhenSplitTextToVerbAndNoun_TheNounABCVerbXYZ()
{
StringUtilities.SplitTextToVerbAndNoun("ABC XYZ", out var verb, out var noun);

Assert.AreEqual("ABC", verb);
Assert.AreEqual("XYZ", noun);
}
}
}
1 change: 0 additions & 1 deletion NetAF/Assets/Locations/Matrix.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Linq;
using System.Security.Cryptography.X509Certificates;

namespace NetAF.Assets.Locations
{
Expand Down
19 changes: 18 additions & 1 deletion NetAF/Commands/CommandHelp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ namespace NetAF.Commands
/// <param name="command">The command.</param>
/// <param name="description">The help.</param>
/// <param name="shortcut">A shortcut for the command.</param>
public sealed class CommandHelp(string command, string description, string shortcut = "") : IEquatable<CommandHelp>, IEquatable<string>
/// <param name="instructions">A instructions on how to use the command.</param>
/// <param name="displayAs">A string overriding how the command should be displayed..</param>
public sealed class CommandHelp(string command, string description, string shortcut = "", string instructions = "", string displayAs = "") : IEquatable<CommandHelp>, IEquatable<string>
{
#region Properties

Expand All @@ -28,6 +30,21 @@ public sealed class CommandHelp(string command, string description, string short
/// </summary>
public string Shortcut { get; } = shortcut;

/// <summary>
/// Get the instructions of the command.
/// </summary>
public string Instructions { get; } = instructions;

/// <summary>
/// Get how this command should be displayed.
/// </summary>
public string DisplayAs { get; } = displayAs;

/// <summary>
/// Get a string representing the command as it should be displayed to the user.
/// </summary>
public string DisplayCommand => !string.IsNullOrEmpty(DisplayAs) ? DisplayAs : Command;

#endregion

#region Implementation of IEquatable<CommandHelp>
Expand Down
Loading

0 comments on commit 51475af

Please sign in to comment.