using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Collections.Generic; namespace YesNt.Interpreter.Tests; [TestClass] public class CodeFlowTests { [TestMethod] public void FunctionTest() { List lines = [ "call yes", "func yes:", "global result = 1", "return", "${result}" ]; YesNtAssert.IsLastLineEqual(lines, "1"); } [TestMethod] public void LabelsTest() { List lines = [ "var result = 1", "goto yes", "var result = 0", "label yes:", "${result}" ]; YesNtAssert.IsLastLineEqual(lines, "1"); } [TestMethod] public void IfBlockTrueTest() { List lines = [ "var result = low", "if 6 > 5:", "var result = high", "end_if", "${result}" ]; YesNtAssert.IsLastLineEqual(lines, "high"); } [TestMethod] public void IfElseFalseBranchTest() { List lines = [ "var result = low", "if 6 < 5:", "var result = high", "else:", "var result = medium", "end_if", "${result}" ]; YesNtAssert.IsLastLineEqual(lines, "medium"); } [TestMethod] public void NestedIfElseTest() { List lines = [ "var result = 0", "if 1 == 1:", "if 2 == 3:", "var result = 1", "else:", "var result = 2", "end_if", "end_if", "${result}" ]; YesNtAssert.IsLastLineEqual(lines, "2"); } [TestMethod] public void WhileLoopTest() { List lines = [ "var i = 3", "while ${i} > 0:", "var i = ${i} - 1 calc", "end_while", "${i}" ]; YesNtAssert.IsLastLineEqual(lines, "0"); } [TestMethod] public void WhileSkipBodyWhenFalseTest() { List lines = [ "var i = 0", "while ${i} > 0:", "var i = 99", "end_while", "${i}" ]; YesNtAssert.IsLastLineEqual(lines, "0"); } [TestMethod] public void NestedWhileLoopTest() { List lines = [ "var outer = 2", "var count = 0", "while ${outer} > 0:", "var inner = 2", "while ${inner} > 0:", "var count = ${count} + 1 calc", "var inner = ${inner} - 1 calc", "end_while", "var outer = ${outer} - 1 calc", "end_while", "${count}" ]; YesNtAssert.IsLastLineEqual(lines, "4"); } [TestMethod] public void IfElseTrueSkipsElseBranchTest() { List lines = [ "var result = 0", "if 2 > 1:", "var result = 1", "else:", "var result = 2", "end_if", "${result}" ]; YesNtAssert.IsLastLineEqual(lines, "1"); } [TestMethod] public void IfWithoutElseFalseSkipsBodyTest() { List lines = [ "var result = 5", "if 1 == 2:", "var result = 1", "end_if", "${result}" ]; YesNtAssert.IsLastLineEqual(lines, "5"); } [TestMethod] public void IfGotoTrueTest() { List lines = [ "var result = 0", "if 1 == 1 goto done", "var result = 2", "label done:", "var result = ${result} + 1 calc", "${result}" ]; YesNtAssert.IsLastLineEqual(lines, "1"); } [TestMethod] public void IfCallTrueTest() { List lines = [ "func set_result:", "global result = ok", "return", "if 1 == 1 call set_result", "${result}" ]; YesNtAssert.IsLastLineEqual(lines, "ok"); } [TestMethod] public void LabelWithoutColonFailsTest() { List lines = [ "label loop" ]; YesNtAssert.ContainsTerminationMessage(lines, "Invalid statement"); } [TestMethod] public void FunctionWithoutColonFailsTest() { List lines = [ "func missing" ]; YesNtAssert.ContainsTerminationMessage(lines, "Invalid statement"); } [TestMethod] public void MissingEndIfFailsTest() { List lines = [ "if 1 == 2:", "var result = 1" ]; YesNtAssert.ContainsTerminationMessage(lines, "No matching end_if found"); } [TestMethod] public void ElseWithoutIfFailsTest() { List lines = [ "else:" ]; YesNtAssert.ContainsTerminationMessage(lines, "No matching end_if found"); } [TestMethod] public void MissingEndWhileFailsTest() { List lines = [ "var i = 0", "while ${i} > 1:", "var i = ${i} + 1 calc" ]; YesNtAssert.ContainsTerminationMessage(lines, "No matching end_while found"); } [TestMethod] public void EndWhileWithoutWhileFailsTest() { List lines = [ "end_while" ]; YesNtAssert.ContainsTerminationMessage(lines, "No matching while found"); } }