public void Test(TestCaseConfig config, TextReader reader) { var dependencies = config.Runtime ? new[] { runtimeDependency } : Enumerable.Empty<PackageReferenceSyntax>(); var package = new PackageSyntax($"Adamant.Exploratory.Compiler.Tests.{config.TestName}", true, dependencies); var unit = compiler.Parse(package, new SourceReader(config.FileName, reader)); package = package.With(new[] { unit }); if(package.Diagnostics.Count > 0) Assert.Fail(ToString(package.Diagnostics)); var compiledPackage = compiler.Compile(package, Enumerable.Empty<Package>()); if(compiledPackage.Diagnostics.Count > 0) Assert.Fail(ToString(compiledPackage.Diagnostics)); var cppSource = compiler.EmitCpp(compiledPackage); var cppSourceName = compiledPackage.Name + ".cpp"; CreateFile(cppSourceName, cppSource); CreateFile(CppRuntime.FileName, CppRuntime.Source); var targetPath = Path.Combine(WorkPath, compiledPackage.Name + ".exe"); var result = CppCompiler.Invoke(Path.Combine(WorkPath, cppSourceName), targetPath); if(result.ExitCode != 0) { result.WriteOutputToConsole(); Assert.Fail("C++ compiler error"); } // Execute the app using(var process = new Process()) { process.StartInfo.FileName = targetPath; process.StartInfo.WorkingDirectory = Path.GetDirectoryName(WorkPath); process.StartInfo.CreateNoWindow = true; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; var outputBuffer = new StringBuilder(); var errorBuffer = new StringBuilder(); process.OutputDataReceived += (s, e) => outputBuffer.AppendLine(e.Data); process.ErrorDataReceived += (s, e) => errorBuffer.AppendLine(e.Data); process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); process.WaitForExit(); if(config.Result != null) Assert.AreEqual(config.Result, process.ExitCode, "Exit Code"); if(config.VerifyConsoleOutput) Assert.AreEqual(config.ExpectedConsoleOutput, outputBuffer.ToString()); } }
public void Test(TestCaseConfig config, TextReader reader) { var dependencies = config.Runtime ? new[] { runtimeDependency } : Enumerable.Empty<PackageReferenceSyntax>(); var package = new PackageSyntax($"Adamant.Exploratory.Compiler.Tests.{config.TestName}", true, dependencies); var unit = compiler.Parse(package, new SourceReader(config.FileName, reader)); package = package.With(new[] { unit }); if(package.Diagnostics.Count > 0) Assert.Fail(ToString(package.Diagnostics)); var compiledPackage = compiler.Compile(package, Enumerable.Empty<Package>()); if(compiledPackage.Diagnostics.Count > 0) Assert.Fail(ToString(compiledPackage.Diagnostics)); var interpreter = new AdamantInterpreter(compiledPackage); var entryPoint = compiledPackage.EntryPoints.Single(); var outputBuffer = new StringBuilder(); var errorBuffer = new StringBuilder(); var exitCode = interpreter.Invoke(entryPoint, new string[0], new StringWriter(outputBuffer), new StringWriter(errorBuffer)); Assert.AreEqual(config.Result, exitCode, "Exit Code"); if(config.VerifyConsoleOutput) Assert.AreEqual(config.ExpectedConsoleOutput, outputBuffer.ToString()); }