|
| 1 | +/* |
| 2 | + * COPYRIGHT: See COPYING in the top level directory |
| 3 | + * PROJECT: InterpreteTests |
| 4 | + * FILE: IrtParserInputTests.cs |
| 5 | + * PURPOSE: Here we test the Parser part of my Script Engine |
| 6 | + * PROGRAMMER: Peter Geinitz (Wayfarer) |
| 7 | + */ |
| 8 | + |
| 9 | + |
| 10 | +using System.Collections.Generic; |
| 11 | +using Interpreter; |
| 12 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 13 | + |
| 14 | +namespace InterpreteTests |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// IrtParserInput Test Class |
| 18 | + /// </summary> |
| 19 | + [TestClass] |
| 20 | + public class IrtParserInputTests |
| 21 | + { |
| 22 | + /// <summary> |
| 23 | + /// The parser input |
| 24 | + /// </summary> |
| 25 | + private IrtParserInput _parserInput; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// The prompt |
| 29 | + /// </summary> |
| 30 | + private Prompt _prompt; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Setups this instance. |
| 34 | + /// </summary> |
| 35 | + [TestInitialize] |
| 36 | + public void Setup() |
| 37 | + { |
| 38 | + _prompt = new Prompt(); |
| 39 | + _parserInput = new IrtParserInput(_prompt); |
| 40 | + |
| 41 | + var commandDict = new Dictionary<int, InCommand> |
| 42 | + { |
| 43 | + { 0, new InCommand { Command = "com1", ParameterCount = 0 } }, |
| 44 | + { 1, new InCommand { Command = "help", ParameterCount = 0 } } |
| 45 | + }; |
| 46 | + |
| 47 | + IrtParserInput.SwitchUserSpace(new UserSpace |
| 48 | + { |
| 49 | + Commands = commandDict, |
| 50 | + UserSpaceName = "TestSpace" |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Switches the user space changes user space. |
| 56 | + /// </summary> |
| 57 | + [TestMethod] |
| 58 | + public void SwitchUserSpaceChangesUserSpace() |
| 59 | + { |
| 60 | + var newCommands = new Dictionary<int, InCommand> |
| 61 | + { |
| 62 | + { 5, new InCommand { Command = "newcmd", ParameterCount = 0 } } |
| 63 | + }; |
| 64 | + |
| 65 | + var newSpace = new UserSpace |
| 66 | + { |
| 67 | + UserSpaceName = "NewSpace", |
| 68 | + Commands = newCommands |
| 69 | + }; |
| 70 | + |
| 71 | + IrtParserInput.SwitchUserSpace(newSpace); |
| 72 | + |
| 73 | + var result = _parserInput.ProcessInput("newcmd()"); |
| 74 | + Assert.IsNotNull(result); |
| 75 | + Assert.AreEqual(5, result.Command); |
| 76 | + Assert.AreEqual("NewSpace", result.UsedNameSpace); |
| 77 | + } |
| 78 | + |
| 79 | + [TestMethod] |
| 80 | + public void ProcessInputReturnsOutCommandForValidCommandWithParameters() |
| 81 | + { |
| 82 | + // Arrange |
| 83 | + var commandDict = new Dictionary<int, InCommand> |
| 84 | + { |
| 85 | + { 0, new InCommand { Command = "com1", ParameterCount = 2, Description = "Help com1" } } |
| 86 | + }; |
| 87 | + |
| 88 | + IrtParserInput.SwitchUserSpace(new UserSpace |
| 89 | + { |
| 90 | + Commands = commandDict, |
| 91 | + UserSpaceName = "DefaultSpace" |
| 92 | + }); |
| 93 | + |
| 94 | + var input = "com1(1,2)"; |
| 95 | + |
| 96 | + // Act |
| 97 | + var result = _parserInput.ProcessInput(input); |
| 98 | + |
| 99 | + // Assert |
| 100 | + Assert.IsNotNull(result); |
| 101 | + Assert.AreEqual(0, result.Command); |
| 102 | + CollectionAssert.AreEqual(new List<string> { "1", "2" }, result.Parameter); |
| 103 | + Assert.AreEqual("DefaultSpace", result.UsedNameSpace); |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Processes the input returns null for unknown command. |
| 108 | + /// </summary> |
| 109 | + [TestMethod] |
| 110 | + public void ProcessInputReturnsNullForUnknownCommand() |
| 111 | + { |
| 112 | + // Arrange |
| 113 | + var commandDict = new Dictionary<int, InCommand> |
| 114 | + { |
| 115 | + { 0, new InCommand { Command = "com1", ParameterCount = 2 } } |
| 116 | + }; |
| 117 | + |
| 118 | + IrtParserInput.SwitchUserSpace(new UserSpace |
| 119 | + { |
| 120 | + Commands = commandDict, |
| 121 | + UserSpaceName = "DefaultSpace" |
| 122 | + }); |
| 123 | + |
| 124 | + var input = "unknown(1,2)"; |
| 125 | + |
| 126 | + // Act |
| 127 | + var result = _parserInput.ProcessInput(input); |
| 128 | + |
| 129 | + // Assert |
| 130 | + Assert.IsNull(result); |
| 131 | + } |
| 132 | + |
| 133 | + /// <summary> |
| 134 | + /// Processes the input returns null for wrong parameter count. |
| 135 | + /// </summary> |
| 136 | + [TestMethod] |
| 137 | + public void ProcessInputReturnsNullForWrongParameterCount() |
| 138 | + { |
| 139 | + // Arrange |
| 140 | + var commandDict = new Dictionary<int, InCommand> |
| 141 | + { |
| 142 | + { 0, new InCommand { Command = "com1", ParameterCount = 2 } } |
| 143 | + }; |
| 144 | + |
| 145 | + IrtParserInput.SwitchUserSpace(new UserSpace |
| 146 | + { |
| 147 | + Commands = commandDict, |
| 148 | + UserSpaceName = "DefaultSpace" |
| 149 | + }); |
| 150 | + |
| 151 | + var input = "com1(1)"; // only 1 parameter, should be 2 |
| 152 | + |
| 153 | + // Act |
| 154 | + var result = _parserInput.ProcessInput(input); |
| 155 | + |
| 156 | + // Assert |
| 157 | + Assert.IsNull(result); // error is set inside parser |
| 158 | + } |
| 159 | + |
| 160 | + /// <summary> |
| 161 | + /// Processes the input handles single parameter command. |
| 162 | + /// </summary> |
| 163 | + [TestMethod] |
| 164 | + public void ProcessInputHandlesSingleParameterCommand() |
| 165 | + { |
| 166 | + // Arrange |
| 167 | + var commandDict = new Dictionary<int, InCommand> |
| 168 | + { |
| 169 | + { 1, new InCommand { Command = "com2", ParameterCount = 1 } } |
| 170 | + }; |
| 171 | + |
| 172 | + IrtParserInput.SwitchUserSpace(new UserSpace |
| 173 | + { |
| 174 | + Commands = commandDict, |
| 175 | + UserSpaceName = "Space2" |
| 176 | + }); |
| 177 | + |
| 178 | + var input = "com2(123)"; |
| 179 | + |
| 180 | + // Act |
| 181 | + var result = _parserInput.ProcessInput(input); |
| 182 | + |
| 183 | + // Assert |
| 184 | + Assert.IsNotNull(result); |
| 185 | + Assert.AreEqual(1, result.Command); |
| 186 | + CollectionAssert.AreEqual(new List<string> { "123" }, result.Parameter); |
| 187 | + } |
| 188 | + |
| 189 | + /// <summary> |
| 190 | + /// Handles the input help command works. |
| 191 | + /// </summary> |
| 192 | + [TestMethod] |
| 193 | + public void HandleInputHelpCommandWorks() |
| 194 | + { |
| 195 | + _parserInput.HandleInput("help()"); |
| 196 | + Assert.IsTrue(true); // Should process help logic safely |
| 197 | + } |
| 198 | + |
| 199 | + /// <summary> |
| 200 | + /// Handles the input empty line handled gracefully. |
| 201 | + /// </summary> |
| 202 | + [TestMethod] |
| 203 | + public void HandleInputEmptyLineHandledGracefully() |
| 204 | + { |
| 205 | + _parserInput.HandleInput(""); |
| 206 | + Assert.IsTrue(true); // Should not throw |
| 207 | + } |
| 208 | + |
| 209 | + /// <summary> |
| 210 | + /// Disposes the can be called multiple times safely. |
| 211 | + /// </summary> |
| 212 | + [TestMethod] |
| 213 | + public void DisposeCanBeCalledMultipleTimesSafely() |
| 214 | + { |
| 215 | + _parserInput.Dispose(); |
| 216 | + _parserInput.Dispose(); // Should be idempotent |
| 217 | + Assert.IsTrue(true); |
| 218 | + } |
| 219 | + } |
| 220 | +} |
0 commit comments