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 lines = [ "print_line hello" ]; YesNtAssert.ContainsDebugOutput(lines, "hello"); } [TestMethod] public void PrintWritesOutputTest() { List lines = [ "print hello" ]; YesNtAssert.ContainsDebugOutput(lines, "hello"); } [TestMethod] public void ClearThrowsInNonInteractiveConsoleTest() { List lines = [ "clear" ]; _ = Assert.Throws(() => YesNtAssert.GetLastLine(lines)); } [TestMethod] public void ReadLineReplacesTokenTest() { lock (ConsoleLock) { TextReader originalIn = Console.In; try { Console.SetIn(new StringReader("typed value" + Environment.NewLine)); List lines = [ "var value = %read_line", "${value}" ]; YesNtAssert.IsLastLineEqual(lines, "typed value"); } finally { Console.SetIn(originalIn); } } } [TestMethod] public void ReadKeyCanBeInterruptedByStopTest() { YesNtInterpreter interpreter = new YesNtInterpreter(); 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 lines = [ "var 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"); } }