CSPspEmu.AutoTests.AutoTestsProgram.RunFile C# (CSharp) Method

RunFile() protected method

protected RunFile ( bool RunTestsViewOut, string PspAutoTestsFolder, string FileNameExecutable, string FileNameExpected, string FileNameBase ) : void
RunTestsViewOut bool
PspAutoTestsFolder string
FileNameExecutable string
FileNameExpected string
FileNameBase string
return void
        protected void RunFile(bool RunTestsViewOut, string PspAutoTestsFolder, string FileNameExecutable, string FileNameExpected, string FileNameBase)
        {
            ConsoleUtils.SaveRestoreConsoleColor(ConsoleColor.DarkCyan, () =>
            {
                Console.Write("{0}...", FileNameExecutable);
            });
            var ExpectedOutput = File.ReadAllText(FileNameExpected, Encoding.ASCII);
            var RealOutput = "";
            string CapturedOutput = "";

            // Execute.
            {
                RealOutput = RunExecutableAndGetOutput(RunTestsViewOut, PspAutoTestsFolder, FileNameExecutable, out CapturedOutput, FileNameBase);
            }

            var ExpectedOutputLines = ExpectedOutput.Trim().Replace("\r\n", "\n").Split('\n');
            var RealOutputLines = RealOutput.Trim().Replace("\r\n", "\n").Split('\n');
            var Result = Diff.DiffTextProcessed(ExpectedOutputLines, RealOutputLines);

            File.WriteAllText(
                Path.ChangeExtension(FileNameExpected, ".lastoutput"),
                RealOutput
            );

            File.WriteAllText(
                Path.ChangeExtension(FileNameExpected, ".lastdebug"),
                CapturedOutput
            );

            bool HadAnError = false;
            for (int n = 0; n < 10; n++)
            {
                var ImageReferenceFile = String.Format("{0}.reference.{1}.png", FileNameBase, n);
                var ImageOutputFile = String.Format("{0}.lastoutput.{1}.png", FileNameBase, n);
                if (File.Exists(ImageReferenceFile))
                {
                    if (File.Exists(ImageOutputFile))
                    {
                        var ReferenceBitmap = new Bitmap(ImageReferenceFile);
                        var OutputBitmap = new Bitmap(ImageOutputFile);
                        if (ReferenceBitmap.Size == OutputBitmap.Size)
                        {
                            var CompareResult = BitmapUtils.CompareBitmaps(ReferenceBitmap, OutputBitmap, 0.01);

                            if (CompareResult.Equal)
                            {
                                Console.Error.WriteLine(
                                    "Files '{0}:{1}' and '{2}:{3}' have different contents {4}/{5} different pixels {6}%",
                                    ImageReferenceFile, ReferenceBitmap.Size, ImageOutputFile, OutputBitmap.Size,
                                    CompareResult.DifferentPixelCount, CompareResult.TotalPixelCount, CompareResult.PixelTotalDifferencePercentage
                                );
                                HadAnError |= true;
                            }
                        }
                        else
                        {
                            Console.Error.WriteLine(
                                "Files '{0}:{1}' and '{2}:{3}' have different sizes",
                                ImageReferenceFile, ReferenceBitmap.Size, ImageOutputFile, OutputBitmap.Size
                            );
                            HadAnError |= true;
                        }
                    }
                    else
                    {
                        Console.Error.WriteLine(
                            "File '{0}' exists, but not exists '{1}'",
                            ImageReferenceFile, ImageOutputFile
                        );
                        HadAnError |= true;
                    }
                }
            }

            if (!Result.Items.All(Item => Item.Action == Diff.ProcessedItem.ActionEnum.Keep)) HadAnError |= true;

            if (!HadAnError)
            {
                ConsoleUtils.SaveRestoreConsoleColor(ConsoleColor.Green, () =>
                {
                    Console.WriteLine("Ok");
                });
            }
            else
            {
                ConsoleUtils.SaveRestoreConsoleColor(ConsoleColor.Red, () =>
                {
                    Console.WriteLine("Error");
                });
                Result.Print(AvoidKeep: true);
            }

            File.WriteAllText(
                Path.ChangeExtension(FileNameExpected, ".lastdiff"),
                Result.ToString()
            );
        }