mirror of
https://github.com/Stone-Red-Code/YesNt-Interpreter.git
synced 2026-09-04 00:56:31 +02:00
Remove Initialize calls and add AddStatement tests
This commit is contained in:
@@ -32,7 +32,6 @@ internal class TextEditor
|
||||
|
||||
public TextEditor()
|
||||
{
|
||||
YesNtInterpreter.Initialize();
|
||||
YesNtInterpreter.OnDebugOutput += YesNtInterpreter_OnDebugOutput;
|
||||
YesNtInterpreter.OnLineExecuted += YesNtInterpreter_OnLineExecuted;
|
||||
syntaxHighlighter = new(YesNtInterpreter.StatementInformation);
|
||||
|
||||
@@ -5,7 +5,6 @@ using YesNt.Interpreter.Runtime;
|
||||
if (args.Length == 1)
|
||||
{
|
||||
YesNtInterpreter interpreter = new YesNtInterpreter();
|
||||
interpreter.Initialize();
|
||||
interpreter.Execute(args[0]);
|
||||
}
|
||||
else
|
||||
|
||||
@@ -0,0 +1,220 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
using YesNt.Interpreter.Attributes;
|
||||
using YesNt.Interpreter.Enums;
|
||||
using YesNt.Interpreter.Runtime;
|
||||
|
||||
namespace YesNt.Interpreter.Tests;
|
||||
|
||||
[TestClass]
|
||||
public class AddStatementTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void AddStatementStartOfLineExecutesHandlerTest()
|
||||
{
|
||||
List<string> lines =
|
||||
[
|
||||
"my_command hello"
|
||||
];
|
||||
|
||||
string? capturedArgs = null;
|
||||
|
||||
YesNtAssert.GetLastLineWithSetup(lines, interpreter =>
|
||||
{
|
||||
interpreter.AddStatement("my_command", SearchMode.StartOfLine, SpaceAround.End, args =>
|
||||
{
|
||||
capturedArgs = args;
|
||||
});
|
||||
});
|
||||
|
||||
Assert.AreEqual("hello", capturedArgs);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AddStatementConvenienceOverloadHandlerIsCalledTest()
|
||||
{
|
||||
List<string> lines =
|
||||
[
|
||||
"custom_cmd world"
|
||||
];
|
||||
|
||||
bool handlerCalled = false;
|
||||
|
||||
YesNtAssert.GetLastLineWithSetup(lines, interpreter =>
|
||||
{
|
||||
interpreter.AddStatement("custom_cmd", SearchMode.StartOfLine, SpaceAround.End, _ =>
|
||||
{
|
||||
handlerCalled = true;
|
||||
});
|
||||
});
|
||||
|
||||
Assert.IsTrue(handlerCalled);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AddStatementAttributeOverloadHandlerIsCalledTest()
|
||||
{
|
||||
List<string> lines =
|
||||
[
|
||||
"attr_cmd test"
|
||||
];
|
||||
|
||||
bool handlerCalled = false;
|
||||
|
||||
YesNtAssert.GetLastLineWithSetup(lines, interpreter =>
|
||||
{
|
||||
var attr = new StatementAttribute("attr_cmd", SearchMode.StartOfLine, SpaceAround.End);
|
||||
interpreter.AddStatement(attr, _ =>
|
||||
{
|
||||
handlerCalled = true;
|
||||
});
|
||||
});
|
||||
|
||||
Assert.IsTrue(handlerCalled);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AddStatementExactSearchModeTest()
|
||||
{
|
||||
List<string> lines =
|
||||
[
|
||||
"exact_cmd"
|
||||
];
|
||||
|
||||
bool handlerCalled = false;
|
||||
|
||||
YesNtAssert.GetLastLineWithSetup(lines, interpreter =>
|
||||
{
|
||||
interpreter.AddStatement("exact_cmd", SearchMode.Exact, SpaceAround.None, _ =>
|
||||
{
|
||||
handlerCalled = true;
|
||||
});
|
||||
});
|
||||
|
||||
Assert.IsTrue(handlerCalled);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AddStatementContainsSearchModeTest()
|
||||
{
|
||||
List<string> lines =
|
||||
[
|
||||
"prefix ~mark~ suffix"
|
||||
];
|
||||
|
||||
bool handlerCalled = false;
|
||||
|
||||
YesNtAssert.GetLastLineWithSetup(lines, interpreter =>
|
||||
{
|
||||
interpreter.AddStatement(" ~mark~ ", SearchMode.Contains, SpaceAround.None, _ =>
|
||||
{
|
||||
handlerCalled = true;
|
||||
});
|
||||
});
|
||||
|
||||
Assert.IsTrue(handlerCalled);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AddStatementEndOfLineSearchModeTest()
|
||||
{
|
||||
List<string> lines =
|
||||
[
|
||||
"some text !end"
|
||||
];
|
||||
|
||||
bool handlerCalled = false;
|
||||
|
||||
YesNtAssert.GetLastLineWithSetup(lines, interpreter =>
|
||||
{
|
||||
interpreter.AddStatement(" !end", SearchMode.EndOfLine, SpaceAround.None, _ =>
|
||||
{
|
||||
handlerCalled = true;
|
||||
});
|
||||
});
|
||||
|
||||
Assert.IsTrue(handlerCalled);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AddStatementReceivesCorrectArgsTest()
|
||||
{
|
||||
List<string> lines =
|
||||
[
|
||||
"capture_cmd the quick brown fox"
|
||||
];
|
||||
|
||||
string? capturedArgs = null;
|
||||
|
||||
YesNtAssert.GetLastLineWithSetup(lines, interpreter =>
|
||||
{
|
||||
interpreter.AddStatement("capture_cmd", SearchMode.StartOfLine, SpaceAround.End, args =>
|
||||
{
|
||||
capturedArgs = args;
|
||||
});
|
||||
});
|
||||
|
||||
Assert.AreEqual("the quick brown fox", capturedArgs);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AddStatementWorksAlongsideBuiltinStatementsTest()
|
||||
{
|
||||
List<string> lines =
|
||||
[
|
||||
"custom_log first",
|
||||
"print_line second"
|
||||
];
|
||||
|
||||
bool customCalled = false;
|
||||
|
||||
YesNtAssert.ContainsDebugOutputWithSetup(lines, "second", interpreter =>
|
||||
{
|
||||
interpreter.AddStatement("custom_log", SearchMode.StartOfLine, SpaceAround.End, _ =>
|
||||
{
|
||||
customCalled = true;
|
||||
});
|
||||
});
|
||||
|
||||
Assert.IsTrue(customCalled);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AddStatementUnknownStatementFailsTest()
|
||||
{
|
||||
List<string> lines =
|
||||
[
|
||||
"unknown_command foo"
|
||||
];
|
||||
|
||||
YesNtAssert.ContainsTerminationMessage(lines, "Invalid statement");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AddStatementWithHighPriorityRunsBeforeNormalTest()
|
||||
{
|
||||
List<string> lines =
|
||||
[
|
||||
"priority_cmd arg"
|
||||
];
|
||||
|
||||
int callOrder = 0;
|
||||
int highPriorityOrder = -1;
|
||||
int normalPriorityOrder = -1;
|
||||
|
||||
YesNtAssert.GetLastLineWithSetup(lines, interpreter =>
|
||||
{
|
||||
interpreter.AddStatement(
|
||||
new StatementAttribute("priority_cmd", SearchMode.StartOfLine, SpaceAround.End) { Priority = Priority.High },
|
||||
_ => highPriorityOrder = callOrder++);
|
||||
|
||||
interpreter.AddStatement(
|
||||
new StatementAttribute("priority_cmd", SearchMode.StartOfLine, SpaceAround.End) { Priority = Priority.Normal },
|
||||
_ => normalPriorityOrder = callOrder++);
|
||||
});
|
||||
|
||||
Assert.IsTrue(highPriorityOrder < normalPriorityOrder, "High priority statement should execute before Normal priority");
|
||||
}
|
||||
}
|
||||
@@ -79,9 +79,8 @@ public class ConsoleStatementsTests
|
||||
public void ReadKeyCanBeInterruptedByStopTest()
|
||||
{
|
||||
YesNtInterpreter interpreter = new YesNtInterpreter();
|
||||
interpreter.Initialize();
|
||||
|
||||
AutoResetEvent onDone = new AutoResetEvent(false);
|
||||
AutoResetEvent onDone= new AutoResetEvent(false);
|
||||
StringBuilder output = new StringBuilder();
|
||||
|
||||
interpreter.OnDebugOutput += (s) => _ = output.Append(s);
|
||||
|
||||
@@ -67,10 +67,31 @@ internal static class YesNtAssert
|
||||
StringAssert.Matches(value, new Regex(pattern));
|
||||
}
|
||||
|
||||
public static void IsLastLineEqualWithSetup(List<string> lines, string expected, Action<YesNtInterpreter> setup, int timeout = 1000)
|
||||
{
|
||||
(DebugEventArgs? debugEventArgs, _) = ExecuteAndCapture(lines, timeout, setup);
|
||||
Assert.AreEqual(expected, debugEventArgs?.CurrentLine);
|
||||
}
|
||||
|
||||
public static void ContainsDebugOutputWithSetup(List<string> lines, string expectedFragment, Action<YesNtInterpreter> setup, int timeout = 1000)
|
||||
{
|
||||
(_, string debugOutput) = ExecuteAndCapture(lines, timeout, setup);
|
||||
StringAssert.Contains(debugOutput, expectedFragment);
|
||||
}
|
||||
|
||||
public static string? GetLastLineWithSetup(List<string> lines, Action<YesNtInterpreter> setup, int timeout = 1000)
|
||||
{
|
||||
(DebugEventArgs? debugEventArgs, _) = ExecuteAndCapture(lines, timeout, setup);
|
||||
return debugEventArgs?.CurrentLine;
|
||||
}
|
||||
|
||||
private static (DebugEventArgs? LastDebugEvent, string DebugOutput) ExecuteAndCapture(List<string> lines, int timeout)
|
||||
=> ExecuteAndCapture(lines, timeout, setup: null);
|
||||
|
||||
private static (DebugEventArgs? LastDebugEvent, string DebugOutput) ExecuteAndCapture(List<string> lines, int timeout, Action<YesNtInterpreter>? setup)
|
||||
{
|
||||
YesNtInterpreter yesNtInterpreter = new YesNtInterpreter();
|
||||
yesNtInterpreter.Initialize();
|
||||
setup?.Invoke(yesNtInterpreter);
|
||||
|
||||
AutoResetEvent onDone = new AutoResetEvent(false);
|
||||
DebugEventArgs? debugEventArgs = null;
|
||||
|
||||
@@ -11,7 +11,6 @@ internal static class Program
|
||||
if (args.Length == 1)
|
||||
{
|
||||
YesNtInterpreter interpreter = new YesNtInterpreter();
|
||||
interpreter.Initialize();
|
||||
interpreter.Execute(args[0]);
|
||||
}
|
||||
else
|
||||
|
||||
@@ -60,21 +60,19 @@ public class YesNtInterpreter
|
||||
AddStatement(new StatementAttribute(name, searchMode, spaceAround, consoleColor), handler);
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
public YesNtInterpreter()
|
||||
{
|
||||
runtimeInfo.Exit("Terminated by external process", true);
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
statements.Clear();
|
||||
staticStatements.Clear();
|
||||
GeneratedStatementRegistry.Register(runtimeInfo, out statements, out staticStatements);
|
||||
|
||||
runtimeInfo.OnDebugOutput += (s) => OnDebugOutput?.Invoke(s);
|
||||
runtimeInfo.OnLineExecuted += (DebugEventArgs e) => OnLineExecuted?.Invoke(e);
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
runtimeInfo.Exit("Terminated by external process", true);
|
||||
}
|
||||
|
||||
public void Execute(string path, bool isDebugMode = false)
|
||||
{
|
||||
runtimeInfo.Reset();
|
||||
|
||||
@@ -50,7 +50,6 @@ internal partial class ProcessingStatements : StatementRuntimeInformation
|
||||
_ = Task.Run(() =>
|
||||
{
|
||||
YesNtInterpreter interpreter = new YesNtInterpreter();
|
||||
interpreter.Initialize();
|
||||
interpreter.Execute(lines, RuntimeInfo.GlobalVariables, lineNumber, RuntimeInfo);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user