public async Task ProgramInterpreterInvokeExternMethod5() { var parser = new BaZicParser(); var inputCode = @"EXTERN FUNCTION Main(args[]) END FUNCTION FUNCTION Method1(arg) RETURN arg END FUNCTION"; using (var interpreter = new BaZicInterpreter(parser.Parse(inputCode, false).Program)) { var tempFile = Path.Combine(Path.GetTempPath(), "BaZic_Bin", Path.GetFileNameWithoutExtension(Path.GetTempFileName()) + ".exe"); var errors = await interpreter.Build(); Assert.IsNull(errors); var result = await interpreter.InvokeMethod(true, "Method1", true, 123); Assert.AreEqual(null, result); Assert.AreEqual("Unexpected and unmanaged error has been detected : The method 'Method1' does not exist in the type 'BaZicProgramReleaseMode.Program'.", interpreter.Error.Exception.Message); Assert.AreEqual(BaZicInterpreterState.StoppedWithError, interpreter.State); } }
public async Task ProgramInterpreterInvokeExternMethod7() { var parser = new BaZicParser(); var inputCode = @"EXTERN FUNCTION Main(args[]) END FUNCTION EXTERN ASYNC FUNCTION Method1(arg) VARIABLE var1 = AWAIT System.Threading.Tasks.Task.Delay(System.TimeSpan.FromSeconds(3.0)) RETURN arg END FUNCTION"; using (var interpreter = new BaZicInterpreter(parser.Parse(inputCode, false).Program)) { var errors = await interpreter.Build(); Assert.IsNull(errors); var t = interpreter.StartReleaseAsync(true); var result = (Task)interpreter.InvokeMethod(true, "Method1", true, 123); Assert.AreEqual(TaskStatus.WaitingForActivation, result.Status); await Task.Delay(5000); Assert.AreEqual(TaskStatus.RanToCompletion, result.Status); Assert.AreEqual(123, ((dynamic)result).Result); Assert.AreEqual(BaZicInterpreterState.Idle, interpreter.State); await interpreter.Stop(); Assert.AreEqual(BaZicInterpreterState.Stopped, interpreter.State); } }
public async Task ProgramInterpreterInvokeExternMethod4() { var parser = new BaZicParser(); var inputCode = @"EXTERN FUNCTION Main(args[]) END FUNCTION EXTERN FUNCTION Method1(arg) RETURN arg END FUNCTION"; using (var interpreter = new BaZicInterpreter(parser.Parse(inputCode, false).Program)) { var tempFile = Path.Combine(Path.GetTempPath(), "BaZic_Bin", Path.GetFileNameWithoutExtension(Path.GetTempFileName()) + ".exe"); var errors = await interpreter.Build(); Assert.IsNull(errors); var result = await interpreter.InvokeMethod(true, "Method1", true, 123); Assert.AreEqual(123, result); Assert.AreEqual(BaZicInterpreterState.Idle, interpreter.State); } }
public async Task BaZicCompile() { var inputCode = @"EXTERN FUNCTION Main(args[]) VARIABLE var1 = 1 VARIABLE Var1 = 2 VARIABLE result = var1 + Var1 System.Console.WriteLine(result.ToString()) RETURN result END FUNCTION"; using (var interpreter = new BaZicInterpreter(inputCode, false)) { var mscorlib = AssemblyInfoHelper.GetAssemblyDetailsFromNameOrLocation(typeof(object).Assembly.FullName); var baZicCoreTest = AssemblyInfoHelper.GetAssemblyDetailsFromNameOrLocation(typeof(LogMock).Assembly.Location); interpreter.SetDependencies(mscorlib, baZicCoreTest); var tempFile = Path.Combine(Path.GetTempPath(), "BaZic_Bin", Path.GetFileNameWithoutExtension(Path.GetTempFileName()) + ".exe"); var errors = await interpreter.Build(Core.Enums.BaZicCompilerOutputType.ConsoleApp, tempFile); Assert.IsNull(errors); Assert.IsTrue(File.Exists(tempFile)); Assert.IsTrue(File.Exists(tempFile.Replace(".exe", ".pdb"))); Assert.IsTrue(File.Exists(Path.Combine(Path.GetTempPath(), "BaZic_Bin", "BaZic.Core.Tests.dll"))); File.Delete(tempFile); File.Delete(tempFile.Replace(".exe", ".pdb")); File.Delete(Path.Combine(Path.GetTempPath(), "BaZic_Bin", "BaZic.Core.Tests.dll")); Directory.Delete(Path.Combine(Path.GetTempPath(), @"BaZic_Bin"), true); } }
public async Task ProgramInterpreterInvokeExternMethod6() { var parser = new BaZicParser(); var inputCode = @"EXTERN FUNCTION Main(args[]) AWAIT MethodAsync(345, 2.0) END FUNCTION EXTERN FUNCTION Method1(arg) RETURN arg END FUNCTION ASYNC FUNCTION MethodAsync(value, timeToWait) VARIABLE var1 = AWAIT System.Threading.Tasks.Task.Delay(System.TimeSpan.FromSeconds(timeToWait)) RETURN value END FUNCTION"; using (var interpreter = new BaZicInterpreter(parser.Parse(inputCode, false).Program)) { var errors = await interpreter.Build(); Assert.IsNull(errors); var t = interpreter.StartReleaseAsync(true); var result = await interpreter.InvokeMethod(true, "Method1", true, 123); Assert.AreEqual(123, result); // Idle is expected because InvokeMethod waits that the main // thread (used by Main()) is free before running. So the async // function is complete before reaching this assert. Assert.AreEqual(BaZicInterpreterState.Idle, interpreter.State); await interpreter.Stop(); Assert.AreEqual(BaZicInterpreterState.Stopped, interpreter.State); } using (var interpreter = new BaZicInterpreter(parser.Parse(inputCode, false).Program)) { var t = interpreter.StartReleaseAsync(true); var result = await interpreter.InvokeMethod(true, "Method1", true, 123); Assert.AreEqual(123, result); // Idle is expected because InvokeMethod waits that the main // thread (used by Main()) is free before running. So the async // function is complete before reaching this assert. Assert.AreEqual(BaZicInterpreterState.Idle, interpreter.State); await interpreter.Stop(); Assert.AreEqual(BaZicInterpreterState.Stopped, interpreter.State); } }
public async Task ProgramInterpreterInvokeExternMethodUI12() { var parser = new BaZicParser(); var inputCode = @" VARIABLE globVar = 1 EXTERN FUNCTION Main(args[]) END FUNCTION EXTERN FUNCTION Method1() DO WHILE TRUE LOOP END FUNCTION"; var xamlCode = @" <Window xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" Name=""Window1""> <StackPanel> <Button Name=""Button1"" Content=""Hello""/> </StackPanel> </Window>"; using (var interpreter = new BaZicInterpreter(inputCode, xamlCode)) { var t = interpreter.StartDebugAsync(true); t = interpreter.InvokeMethod(true, "Method1", true); await Task.Delay(3000); Assert.AreEqual(BaZicInterpreterState.Running, interpreter.State); await interpreter.Stop(); Assert.AreEqual(BaZicInterpreterState.Stopped, interpreter.State); } using (var interpreter = new BaZicInterpreter(inputCode, xamlCode)) { var errors = await interpreter.Build(); Assert.IsNull(errors); var t = interpreter.StartReleaseAsync(true); t = interpreter.InvokeMethod(true, "Method1", true); await Task.Delay(10000); Assert.AreEqual(BaZicInterpreterState.Running, interpreter.State); await interpreter.Stop(); Assert.AreEqual(BaZicInterpreterState.Stopped, interpreter.State); } }
public async Task BaZicCompileWithUI() { var inputCode = @" EXTERN FUNCTION Main(args[]) END FUNCTION EVENT FUNCTION Window1_Closed() RETURN ""Result of Window.Close"" END FUNCTION EVENT FUNCTION Window1_Loaded() VARIABLE var1 = Button1.Content IF var1 = ""Hello"" THEN Button1.Content = ""Hello World"" var1 = Button1.Content IF var1 = ""Hello World"" THEN RETURN TRUE END IF END IF END FUNCTION # The XAML will be provided separatly"; var xamlCode = @" <Window xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" Name=""Window1""> <StackPanel> <Button Name=""Button1"" Content=""Hello""/> </StackPanel> </Window>"; using (var interpreter = new BaZicInterpreter(inputCode, xamlCode, optimize: false)) { var tempFile = Path.Combine(Path.GetTempPath(), "BaZic_Bin", Path.GetFileNameWithoutExtension(Path.GetTempFileName()) + ".exe"); var errors = await interpreter.Build(Core.Enums.BaZicCompilerOutputType.WindowsApp, tempFile); Assert.IsNull(errors); Assert.IsTrue(File.Exists(tempFile)); Assert.IsTrue(File.Exists(tempFile.Replace(".exe", ".pdb"))); File.Delete(tempFile); File.Delete(tempFile.Replace(".exe", ".pdb")); Directory.Delete(Path.Combine(Path.GetTempPath(), @"BaZic_Bin"), true); } }
public async Task ProgramInterpreterInvokeExternMethodUI15() { var inputCode = @" EXTERN FUNCTION Main(args[]) END FUNCTION EXTERN FUNCTION Method1() VARIABLE var1 = Button1.Content IF var1 = ""Hello"" THEN Button1.Content = ""Hello World"" var1 = Button1.Content IF var1 = ""Hello World"" THEN RETURN TRUE END IF END IF RETURN FALSE END FUNCTION # The XAML will be provided separatly"; var xamlCode = @" <StackPanel xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""> <Button Name=""Button1"" Content=""Hello""/> </StackPanel>"; var task1 = Task.Run(() => { using (var interpreter = new BaZicInterpreter(inputCode, xamlCode)) { var result = interpreter.InvokeMethod(true, "Method1", true).Result; Assert.IsNull(result); Assert.AreEqual("The variable 'Button1' does not exist or is not accessible.", interpreter.Error.Exception.Message); var t = interpreter.StartDebugAsync(true); result = interpreter.InvokeMethod(true, "Method1", true).Result; Assert.AreEqual(true, result); } }); var task2 = Task.Run(() => { using (var interpreter = new BaZicInterpreter(inputCode, xamlCode)) { var errors = interpreter.Build().Result; Assert.IsNull(errors); var result = interpreter.InvokeMethod(true, "Method1", true).Result; Assert.IsNull(result); Assert.AreEqual("Object reference not set to an instance of an object.", interpreter.Error.Exception.InnerException.Message); var t = interpreter.StartReleaseAsync(true); result = interpreter.InvokeMethod(true, "Method1", true).Result; Assert.AreEqual(true, result); } }); var task3 = Task.Run(() => { using (var interpreter = new BaZicInterpreter(inputCode, xamlCode)) { var t = interpreter.StartReleaseAsync(true); var result = interpreter.InvokeMethod(true, "Method1", true).Result; Assert.AreEqual(true, result); } }); await Task.WhenAll(task1, task2, task3); }
public async Task ProgramInterpreterInvokeExternMethod9() { var parser = new BaZicParser(); var inputCode = @" VARIABLE globVar = 1 EXTERN FUNCTION Main(args[]) END FUNCTION EXTERN FUNCTION Method1() globVar = globVar + 1 RETURN globVar END FUNCTION"; using (var interpreter = new BaZicInterpreter(parser.Parse(inputCode, false).Program)) { var t = interpreter.StartDebugAsync(true); var result = await interpreter.InvokeMethod(true, "Method1", true); Assert.AreEqual(2, result); result = await interpreter.InvokeMethod(true, "Method1", true); Assert.AreEqual(3, result); } using (var interpreter = new BaZicInterpreter(parser.Parse(inputCode, false).Program)) { var result = await interpreter.InvokeMethod(true, "Method1", true); Assert.AreEqual(2, result); result = await interpreter.InvokeMethod(true, "Method1", true); Assert.AreEqual(3, result); } using (var interpreter = new BaZicInterpreter(parser.Parse(inputCode, false).Program)) { var errors = await interpreter.Build(); Assert.IsNull(errors); var t = interpreter.StartReleaseAsync(true); var result = await interpreter.InvokeMethod(true, "Method1", true); Assert.AreEqual(2, result); result = await interpreter.InvokeMethod(true, "Method1", true); Assert.AreEqual(3, result); } using (var interpreter = new BaZicInterpreter(parser.Parse(inputCode, false).Program)) { var errors = await interpreter.Build(); Assert.IsNull(errors); var result = await interpreter.InvokeMethod(true, "Method1", true); Assert.AreEqual(2, result); result = await interpreter.InvokeMethod(true, "Method1", true); Assert.AreEqual(3, result); } }
public async Task BaZicInterpreterWithUiProgramResource() { var parser = new BaZicParser(); var imageFile = Path.Combine(Directory.GetParent(Assembly.GetAssembly(this.GetType()).Location).FullName, "Image.png"); var inputCode = @" EXTERN FUNCTION Main(args[]) END FUNCTION # The XAML will be provided separatly"; var xamlCode = @" <Window Title=""Window"" BorderBrush=""#FFFFFFFF"" Background=""#FFFFFFFF"" Foreground=""#FFFFFFFF"" Name=""Window1"" Width=""154"" Height=""183"" Margin=""0,0,0,0"" xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" xmlns:b=""clr-namespace:ANamespace;assembly=AnAssembly""> <Grid Name=""Grid1""> <Grid.Background> <ImageBrush ImageSource=""{imageFile}"" Stretch=""Uniform"" /> </Grid.Background> </Grid> </Window>" .Replace("{imageFile}", $"file:///{imageFile.Replace("\\", "/")}"); var bazicProgram = (BaZicUiProgram)parser.Parse(inputCode, xamlCode, new List <string> { imageFile }, optimize: true).Program; Assert.AreEqual(1, bazicProgram.ResourceFilePaths.Count); var interpreter = new BaZicInterpreter(bazicProgram); var t = interpreter.StartDebugAsync(true); await Task.Delay(5000); await interpreter.Stop(); Assert.AreEqual(null, interpreter.ProgramResult); using (interpreter = new BaZicInterpreter(inputCode, xamlCode, new List <string> { imageFile }, optimize: false)) { var tempFile = Path.Combine(Path.GetTempPath(), "BaZic_Bin", Path.GetFileNameWithoutExtension(Path.GetTempFileName()) + ".exe"); var errors = await interpreter.Build(Core.Enums.BaZicCompilerOutputType.WindowsApp, tempFile); Assert.IsNull(errors); Assert.IsTrue(File.Exists(tempFile)); Assert.IsTrue(File.Exists(tempFile.Replace(".exe", ".pdb"))); File.Delete(tempFile); File.Delete(tempFile.Replace(".exe", ".pdb")); Directory.Delete(Path.Combine(Path.GetTempPath(), @"BaZic_Bin"), true); } }