public void InvokeCoreMethodAwaitButNotAsync() { var program = new AlgorithmProgram("MyApp"); var firstClass = new AlgorithmClassDeclaration("FirstClass"); var firstMethod = new AlgorithmClassMethodDeclaration("FirstMethod", false); firstMethod.Statements.Add(new AlgorithmReturnStatement(new AlgorithmPrimitiveExpression(123))); firstClass.Members.Add(firstMethod); var entryPoint = new AlgorithmEntryPointMethod(); var invoke = new AlgorithmInvokeCoreMethodExpression(new AlgorithmClassReferenceExpression(typeof(Debug)), "WriteLine", new[] { typeof(string) }, new AlgorithmPrimitiveExpression("Hello")); invoke.Await = true; entryPoint.Statements.Add(new AlgorithmReturnStatement(invoke)); firstClass.Members.Add(entryPoint); program.Classes.Add(firstClass); program.UpdateEntryPointPath(); var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: true); task.Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Count, 7); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Running); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[3].LogMessage, "Calling core method 'System.Diagnostics.Debug.WriteLine'"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[4].LogMessage, "Reference to the class : System.Diagnostics.Debug"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[5].LogMessage, "Primitive value : 'Hello' (type:System.String)"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[6].State, AlgorithmInterpreterState.StoppedWithError); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.StoppedWithError); Assert.AreEqual(algorithmInterpreter.Error.Exception.Message, "The method 'System.Diagnostics.Debug.WriteLine' is not awaitable because this method does not has the property IsAsync on true."); AlgorithmInterpreter_Test.RunProgramWithoutDebug(program, true); }
public void InvokeCoreMethodAsyncAwaitResult() { var program = new AlgorithmProgram("MyApp"); var firstClass = new AlgorithmClassDeclaration("FirstClass"); var firstMethod = new AlgorithmClassMethodDeclaration("FirstMethod", false); firstMethod.Statements.Add(new AlgorithmReturnStatement(new AlgorithmPrimitiveExpression(123))); firstClass.Members.Add(firstMethod); var entryPoint = new AlgorithmEntryPointMethod(); var invoke = new AlgorithmInvokeCoreMethodExpression(new AlgorithmClassReferenceExpression(typeof(InvokeCoreMethod_Test)), "TaskTest", new Type[0]); invoke.Await = true; entryPoint.Statements.Add(new AlgorithmReturnStatement(invoke)); firstClass.Members.Add(entryPoint); program.Classes.Add(firstClass); program.UpdateEntryPointPath(); var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: true); task.Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Count, 7); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Running); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[3].LogMessage, "Calling core method 'Algo.Runtime.UnitTest.Build.Runtime.Interpreter.Expressions.InvokeCoreMethod_Test.TaskTest'"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[4].LogMessage, "Reference to the class : Algo.Runtime.UnitTest.Build.Runtime.Interpreter.Expressions.InvokeCoreMethod_Test"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[5].LogMessage, "(Main) Return : 'Hello' (type:System.String)"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[6].State, AlgorithmInterpreterState.Stopped); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.Stopped); AlgorithmInterpreter_Test.RunProgramWithoutDebug(program); }
private AlgorithmProgram GetAsyncProgram(bool awaitCall) { var program = new AlgorithmProgram("MyApp"); var firstClass = new AlgorithmClassDeclaration("FirstClass"); var firstMethod = new AlgorithmClassMethodDeclaration("FirstMethod", false); firstMethod.Statements.Add(new AlgorithmReturnStatement(new AlgorithmPrimitiveExpression(123))); firstClass.Members.Add(firstMethod); var entryPoint = new AlgorithmEntryPointMethod(); var invoke = new AlgorithmInvokeCoreMethodExpression(new AlgorithmClassReferenceExpression("System.Threading.Tasks", "Task"), "Delay", new[] { typeof(int) }, new AlgorithmPrimitiveExpression(50)); invoke.Await = awaitCall; entryPoint.Statements.Add(new AlgorithmReturnStatement(invoke)); firstClass.Members.Add(entryPoint); program.Classes.Add(firstClass); program.UpdateEntryPointPath(); return(program); }