Example #1
0
        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     = "";
            var 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
                );

            var hadAnError = false;

            for (var n = 0; n < 10; n++)
            {
                var imageReferenceFile = $"{fileNameBase}.reference.{n}.png";
                var imageOutputFile    = $"{fileNameBase}.lastoutput.{n}.png";
                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.Any(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()
                );
        }