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());
                }
            }
        }
Example #2
0
        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());
            }
        }
        private void CompileProject(string projectDirPath, ProjectConfig projectConfig, CompiledProjects projects)
        {
            Console.WriteLine($"Compiling {projectConfig.Name} ...");

            var sourceFiles = new DirectoryInfo(Path.Combine(projectDirPath, "src")).GetFiles("*.adam", SearchOption.AllDirectories);
            var isApp       = projectConfig.Template == "app";
            // TODO read trusted from config
            var package = new PackageSyntax(projectConfig.Name, isApp, projectConfig.Dependencies.Select(d => new PackageReferenceSyntax(d.Key, null, true)));

            package = package.With(sourceFiles.Select(fileInfo => compiler.Parse(package, new SourceFile(fileInfo))));
            if (package.Diagnostics.Count > 0)
            {
                PrintDiagnostics(package);
                return;
            }
            var compiledPackage = compiler.Compile(package, projects.Select(p => p.Package));
            var compiledProject = new CompiledProject(projectDirPath, compiledPackage);

            projects.Add(compiledProject);
            OnProjectCompiled(compiledProject, projects);
        }