Add new tests for console, function, system, variable, and code flow statements

This commit is contained in:
Stone_Red
2026-03-04 14:49:54 +01:00
parent 36d8735fbb
commit a5f521912e
9 changed files with 899 additions and 56 deletions
@@ -0,0 +1,110 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using YesNt.Interpreter.Runtime;
namespace YesNt.Interpreter.Tests;
[TestClass]
public class ConsoleStatementsTests
{
private static readonly object ConsoleLock = new object();
[TestMethod]
public void PrintLineWritesOutputTest()
{
List<string> lines =
[
"print_line hello"
];
YesNtAssert.ContainsDebugOutput(lines, "hello");
}
[TestMethod]
public void PrintWritesOutputTest()
{
List<string> lines =
[
"print hello"
];
YesNtAssert.ContainsDebugOutput(lines, "hello");
}
[TestMethod]
public void ClearThrowsInNonInteractiveConsoleTest()
{
List<string> lines =
[
"clear"
];
_ = Assert.ThrowsException<IOException>(() => YesNtAssert.GetLastLine(lines));
}
[TestMethod]
public void ReadLineReplacesTokenTest()
{
lock (ConsoleLock)
{
TextReader originalIn = Console.In;
try
{
Console.SetIn(new StringReader("typed value" + Environment.NewLine));
List<string> lines =
[
"let value = %read_line",
"${value}"
];
YesNtAssert.IsLastLineEqual(lines, "typed value");
}
finally
{
Console.SetIn(originalIn);
}
}
}
[TestMethod]
public void ReadKeyCanBeInterruptedByStopTest()
{
YesNtInterpreter interpreter = new YesNtInterpreter();
interpreter.Initialize();
AutoResetEvent onDone = new AutoResetEvent(false);
StringBuilder output = new StringBuilder();
interpreter.OnDebugOutput += (s) => _ = output.Append(s);
interpreter.OnLineExecuted += (e) =>
{
if (e is null)
{
_ = onDone.Set();
}
};
List<string> lines =
[
"let value = %read_key"
];
_ = Task.Run(() => interpreter.Execute(lines, true));
Thread.Sleep(100);
interpreter.Stop();
_ = onDone.WaitOne(TimeSpan.FromSeconds(3));
StringAssert.Contains(output.ToString(), "Terminated by external process");
}
}