Skip to content

Commit 09c45a6

Browse files
Fix another subtile bug in my Interpreter
add some more basic tests and add some more debug helpers. All in preparation to add the option to tryrun my stuff in CoreConsole
1 parent 2de3c56 commit 09c45a6

12 files changed

Lines changed: 269 additions & 13 deletions

CoreConsole/ConResources.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ internal static class ConResources
3434
/// <summary>
3535
/// The resxtract overload
3636
/// </summary>
37-
internal const int ResxtractOverload = 2;
37+
internal const int ResXtractOverload = 2;
3838

3939
/// <summary>
4040
/// The analyzer
@@ -65,7 +65,7 @@ internal static class ConResources
6565
}
6666
},
6767
{
68-
ResxtractOverload, new InCommand
68+
ResXtractOverload, new InCommand
6969
{
7070
Command = "resxtract",
7171
ParameterCount = 1,

CoreConsole/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ private static void HandleCommands(OutCommand outCommand)
188188
result = HandleResxtract(outCommand);
189189
_prompt.Callback(result);
190190
break;
191-
case ConResources.ResxtractOverload:
191+
case ConResources.ResXtractOverload:
192192
result = HandleResxtract(outCommand);
193193
_prompt.Callback(result);
194194
break;

CoreLibrary.sln.DotSettings.user

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
&lt;TestId&gt;MSTest::D2D1B7B6-0C94-45F8-BD4E-1FB9A318AC21::net5.0-windows7.0::CommonLibraryTests.MemoryVaultTests.GetData_ShouldReturnDefaultWhenExpired&lt;/TestId&gt;&#xD;
1313
&lt;TestId&gt;MSTest::D2D1B7B6-0C94-45F8-BD4E-1FB9A318AC21::net5.0-windows7.0::CommonLibraryTests.MemoryVaultTests.AddMetadata_ShouldStoreMetadataCorrectly&lt;/TestId&gt;&#xD;
1414
&lt;TestId&gt;MSTest::B5D5FFD4-0B21-4C0B-9B77-2E6DE5D3FFAF::net5.0::CommonLibrarySqlLiteTests.SqlLiteInternals.TestCreateDatabaseBigInternals&lt;/TestId&gt;&#xD;
15+
&lt;TestId&gt;MSTest::18981E7D-2AB9-48B2-8181-9E9DF3263B23::net5.0-windows7.0::InterpreteTests.InterpreterCompleteTests.ConsoleInterpreterShouldHandleNamespaceSwitchFullTest&lt;/TestId&gt;&#xD;
1516
&lt;/TestAncestor&gt;&#xD;
1617
&lt;Project Location="C:\Users\Wayfarer\Documents\Projects\CoreLibrary\CommonLibraryGuiTests" Presentation="&amp;lt;CommonLibraryGuiTests&amp;gt;" /&gt;&#xD;
1718
&lt;Project Location="C:\Users\Wayfarer\Documents\Projects\CoreLibrary\CommonLibraryTests" Presentation="&amp;lt;CommonLibraryTests&amp;gt;" /&gt;&#xD;

InterpreteTests/CommandBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* COPYRIGHT: See COPYING in the top level directory
33
* PROJECT: InterpreteTests
44
* FILE: CommandBuilder.cs
5-
* PURPOSE: Your file purpose here
5+
* PURPOSE: Some full command tests mosttly for the scripting part and if else
66
* PROGRAMMER: Peter Geinitz (Wayfarer)
77
*/
88

InterpreteTests/FeedbackTests.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,25 +117,37 @@ public void FeedbackExtensionExternal()
117117
{
118118
{ 0, new InCommand { Command = "com1", ParameterCount = 2, Description = "Help com1" } },
119119
{
120-
1,
121-
new InCommand { Command = "com2", ParameterCount = 0, Description = "com2 Command Namespace 1" }
120+
1, new InCommand { Command = "com2", ParameterCount = 0, Description = "com2 Command Namespace 1" }
122121
},
123122
{
124-
2,
125-
new InCommand
123+
2, new InCommand
126124
{
127125
Command = "com3", ParameterCount = 0, Description = "Special case no Parameter"
128126
}
129127
}
130128
};
131129

130+
Dictionary<int, InCommand> extensionCommands = new()
131+
{
132+
{
133+
0, new InCommand
134+
{
135+
Command = "dryrun",
136+
ParameterCount = 0,
137+
FeedbackId = 1,
138+
Description =
139+
"Show results and optional run commands"
140+
}
141+
}
142+
};
143+
132144
// Act
133145
var prompt = new Prompt();
134146
prompt.SendLogs += SendLogs;
135147
prompt.SendCommands += SendCommands;
136-
//prompt.Initiate(dctCommandOne, "UserSpace 1", extension: Feedback);
137-
//prompt.ConsoleInput("coM1(1,2).Help()");
138-
//prompt.ConsoleInput("");
148+
prompt.Initiate(dctCommandOne, "UserSpace 1", extension: extensionCommands, Feedback);
149+
prompt.ConsoleInput("coM1(1,2).Dryrun()");
150+
prompt.ConsoleInput("");
139151

140152

141153
//Assert.IsFalse(_log.Contains("Help com1"), "No help provided.");
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
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+
}

Interpreter/ExtensionCommands.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public sealed class ExtensionCommands
3434
/// Gets or sets the parameter.
3535
/// </summary>
3636
public List<string> ExtensionParameter { get; internal set; }
37+
3738
/// <summary>
3839
/// Gets the feedback identifier.
3940
/// Define the feedback id

Interpreter/InCommand.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
// ReSharper disable MemberCanBeInternal
1010

1111
using System;
12+
using System.Diagnostics;
1213

1314
namespace Interpreter
1415
{
1516
/// <summary>
1617
/// Simple Element of the Register
1718
/// </summary>
19+
[DebuggerDisplay("{ToString()}")]
1820
public sealed class InCommand
1921
{
2022
/// <summary>

Interpreter/IrtFeedback.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
* PROGRAMER: Peter Geinitz (Wayfarer)
77
*/
88

9+
using System.Diagnostics;
10+
911
namespace Interpreter
1012
{
1113
/// <summary>
1214
/// Control Object that checks if User Feedback is needed
1315
/// </summary>
16+
[DebuggerDisplay("{ToString()}")]
1417
internal sealed class IrtFeedback
1518
{
1619
/// <summary>
@@ -78,5 +81,13 @@ internal IrtFeedbackInputEventArgs GenerateFeedbackAnswer(AvailableFeedback answ
7881
Answer = answer
7982
};
8083
}
84+
85+
/// <summary>
86+
/// Returns a string representation of this feedback.
87+
/// </summary>
88+
public override string ToString()
89+
{
90+
return $"[Feedback: Cmd={Command}, Key={Key}, ReqId={RequestId}, Branch={BranchId}, Output={AwaitedOutput}]";
91+
}
8192
}
8293
}

Interpreter/IrtParserInput.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ internal void HandleInput(string inputString)
212212
}
213213
else
214214
{
215+
//TODO result is wrong.
215216
var command = ProcessInput(inputString, extensionResult.Extension);
216217
SetResult(command);
217218
}
@@ -236,6 +237,12 @@ internal void HandleInput(string inputString)
236237
/// <param name="extension">Optional extension commands</param>
237238
internal OutCommand? ProcessInput(string inputString, ExtensionCommands? extension = null)
238239
{
240+
if (extension != null)
241+
{
242+
//remove the extension string, and handle only the base command.
243+
inputString = extension.BaseCommand;
244+
}
245+
239246
var key = IrtKernel.CheckForKeyWord(inputString, IrtConst.InternCommands);
240247
if (key != IrtConst.Error)
241248
{
@@ -330,7 +337,7 @@ private void SetErrorWithLog(string errorMessage, string additionalInfo = "")
330337
/// <param name="command">The command.</param>
331338
private void SetResult(OutCommand command)
332339
{
333-
if (_com[command.Command].FeedbackId == 0)
340+
if (_com[command.Command]?.FeedbackId == 0)
334341
{
335342
_prompt.SendCommand(this, command);
336343
}

0 commit comments

Comments
 (0)