コード例 #1
0
        private void RunSingleTest(MethodInfo methodInfo, string inputFile, string outputFile)
        {
            Console.SetIn(File.OpenText(inputFile));
            var actualOutput        = new StringWriter();
            var expectedOutputLines = FileWithoutLock.ReadAllLines(outputFile).Select(l => l.Replace("\r\n", string.Empty)).ToArray();

            Console.SetOut(actualOutput);
            methodInfo.Invoke(null, null);
            Console.SetOut(new StreamWriter(Console.OpenStandardOutput())
            {
                AutoFlush = true
            });

            var actualOutputLines = new StringReader(actualOutput.ToString()).ReadAllLines()
                                    .Select(l => l.Replace("\r\n", string.Empty)).ToArray();
            var success     = CompareOutput(actualOutputLines, expectedOutputLines);
            var sucessLabel = success ? "OK" : "KO";

            Console.WriteLineStyled($"[{Path.GetFileName(_sourceCodeFile)}] Running test : {Path.GetFileName(inputFile)} {sucessLabel}", _styleSheet);

            if (!success)
            {
                PrintDiff(expectedOutputLines, actualOutputLines);
            }
        }
コード例 #2
0
        public void CompileAndRunTests()
        {
            var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(_sourceCodeFile);

            var compilation =
                CSharpCompilation.Create
                (
                    assemblyName: fileNameWithoutExtension,
                    syntaxTrees: new[] { CSharpSyntaxTree.ParseText(FileWithoutLock.ReadAllText(_sourceCodeFile)) },
                    references: new []
            {
                MetadataReference.CreateFromFile(typeof(object).Assembly.Location),                  //mscorlib.dll
                MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location),              //System.Core.dll
                MetadataReference.CreateFromFile(typeof(Uri).Assembly.Location)                      //System.dll
            },
                    options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
                );

            var buildedMethod = CompilationHelper.For(compilation)
                                .TryEmit(
                onSuccess: assembly =>
                assembly
                .GetTypes()
                .Single(t => t.Name == fileNameWithoutExtension)
                .GetMethod(_parameters.StaticMethodToRun),
                onErrorAction: diagnostic =>
            {
                var sourceFileName = Path.GetFileName(_sourceCodeFile);

                if (diagnostic.Severity == DiagnosticSeverity.Error || diagnostic.IsWarningAsError)
                {
                    var lineSpan  = diagnostic.Location.GetMappedLineSpan();
                    var startLine = lineSpan.StartLinePosition.Line + 1;
                    var color     = diagnostic.IsWarningAsError ? Color.Orange : Color.Red;
                    Console.WriteLine($"{sourceFileName} L{startLine} : {diagnostic.Id}: {diagnostic.GetMessage()}", color);
                }
            });

            RunTests(buildedMethod);
        }