Remove Initialize calls and add AddStatement tests

This commit is contained in:
Stone_Red
2026-03-04 19:41:19 +01:00
parent 8e0abf155a
commit b2fdd9c841
8 changed files with 250 additions and 16 deletions
@@ -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);
+22 -1
View File
@@ -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;