Esempio n. 1
0
        async Task VerifyInner(object?target, Func <Task>?cleanup, IEnumerable <Target> targets)
        {
            var targetList = targets.ToList();

            if (TryGetTargetBuilder(target, out var builder, out var extension))
            {
                ApplyScrubbers.Apply(extension, builder, settings);

                var received = builder.ToString();
                var stream   = new Target(extension, received);
                targetList.Insert(0, stream);
            }

            targetList.AddRange(VerifierSettings.GetFileAppenders(settings));
            var builders = targetList
                           .Select(
                stream =>
            {
                return(new ResultBuilder(
                           stream.Extension,
                           file => GetResult(settings, file, stream)));
            })
                           .ToList();

            VerifyEngine engine = new(fileNameBuilder, settings.autoVerify);

            await engine.HandleResults(builders);

            if (cleanup != null)
            {
                await cleanup();
            }

            await engine.ThrowIfRequired();
        }
Esempio n. 2
0
        public static void Enable()
        {
            VerifierSettings.ModifySerialization(settings =>
            {
                settings.AddExtraSettings(serializerSettings =>
                {
                    var converters = serializerSettings.Converters;
                    converters.Add(new MsConnectionConverter());
                    converters.Add(new SysConnectionConverter());
                });
            });

            VerifierSettings.RegisterJsonAppender(_ =>
            {
                if (!SqlRecording.TryFinishRecording(out var entries))
                {
                    return(null);
                }

                return(new("sql", entries !));
            });

            VerifierSettings.RegisterFileConverter <MsConnection>(ToSql);
            VerifierSettings.RegisterFileConverter <SysConnection>(ToSql);
        }
        public static void Initialize()
        {
            VerifierSettings.SetDefaultStringComparer((received, verified, _) =>
            {
                var diff = InlineDiffBuilder.Diff(verified, received);

                var builder = new StringBuilder();
                foreach (var line in diff.Lines)
                {
                    switch (line.Type)
                    {
                    case ChangeType.Inserted:
                        builder.Append("+ ");
                        break;

                    case ChangeType.Deleted:
                        builder.Append("- ");
                        break;

                    default:
                        builder.Append("  ");
                        break;
                    }
                    builder.AppendLine(line.Text);
                }

                var compareResult = CompareResult.NotEqual(builder.ToString());
                return(Task.FromResult(compareResult));
            });
        }
        public async Task Verify <T>(IAsyncEnumerable <T> target)
        {
            Guard.AgainstNull(target, nameof(target));
            List <T> list = new();

            await foreach (var item in target)
            {
                list.Add(item);
            }

            try
            {
                await SerializeAndVerify(list, VerifierSettings.GetJsonAppenders(settings));
            }
            finally
            {
                foreach (var item in list)
                {
                    if (item is IAsyncDisposable asyncDisposable)
                    {
                        await asyncDisposable.DisposeAsync();
                    }
                    else if (item is IDisposable disposable)
                    {
                        disposable.Dispose();
                    }
                }
            }
        }
Esempio n. 5
0
        async Task VerifyInner(object?target, Func <Task>?cleanup, IEnumerable <ConversionStream> streams)
        {
            VerifyEngine engine = new(settings, fileNameBuilder);

            var streamsList = streams.ToList();

            if (TryGetTargetBuilder(target, out var builder, out var extension))
            {
                ApplyScrubbers.Apply(builder, settings.instanceScrubbers);

                var received = builder.ToString();
                var stream   = new ConversionStream(extension, received);
                streamsList.Insert(0, stream);
            }

            streamsList.AddRange(VerifierSettings.GetFileAppenders(settings));
            var builders = streamsList
                           .Select(
                stream =>
            {
                return(new ResultBuilder(
                           stream.Extension,
                           file => GetResult(settings, file, stream)));
            })
                           .ToList();

            await engine.HandleResults(builders);

            if (cleanup != null)
            {
                await cleanup.Invoke();
            }

            await engine.ThrowIfRequired();
        }
Esempio n. 6
0
 public InnerVerifier(string testName, string sourceFile, Assembly assembly)
 {
     directory     = VerifierSettings.DeriveDirectory(sourceFile, assembly);
     this.testName = testName;
     this.assembly = assembly;
     CounterContext.Start();
 }
Esempio n. 7
0
        public async Task Verify <T>(T target)
        {
            if (target == null)
            {
                await VerifyInner(null, null, Enumerable.Empty <ConversionStream>());

                return;
            }

            if (VerifierSettings.TryGetToString(target, out var toString))
            {
                var stringResult = toString(target, settings.Context);
                if (stringResult.Extension != null)
                {
                    settings.UseExtension(stringResult.Extension);
                }

                await VerifyInner(stringResult.Value, null, Enumerable.Empty <ConversionStream>());

                return;
            }

            if (VerifierSettings.TryGetTypedConverter(target, settings, out var converter))
            {
                var result = await converter.Conversion(target !, settings.Context);
                await VerifyInner(result.Info, result.Cleanup, result.Streams);

                return;
            }

            if (target is Stream stream)
            {
                await VerifyStream(stream);

                return;
            }

            if (typeof(T).ImplementsStreamEnumerable())
            {
                var enumerable = (IEnumerable)target;
                var streams    = enumerable.Cast <Stream>()
                                 .Select(x =>
                {
                    if (x == null)
                    {
                        return(new ConversionStream(settings.ExtensionOrTxt(), "null"));
                    }

                    return(new ConversionStream(settings.ExtensionOrBin(), x));
                });
                await VerifyInner(null, null, streams);

                return;
            }

            await VerifyInner(target, null, Enumerable.Empty <ConversionStream>());
        }
Esempio n. 8
0
        public InnerVerifier(string sourceFile, Type type, VerifySettings settings, MethodInfo method)
        {
            var(projectDirectory, replacements) = AttributeReader.GetAssemblyInfo(type.Assembly);
            settings.instanceScrubbers.Add(replacements);
            fileNameBuilder = new(method, type, projectDirectory, sourceFile, settings);

            this.settings = settings;

            VerifierSettings.RunBeforeCallbacks();
        }
 static VerifyXamarin()
 {
     VerifierSettings.ModifySerialization(settings =>
     {
         settings.AddExtraSettings(serializerSettings =>
         {
             var converters = serializerSettings.Converters;
             converters.Add(new AppResultConverter());
         });
     });
 }
Esempio n. 10
0
        async Task VerifyStream(Stream stream, string?extension)
        {
#if NETSTANDARD2_0 || NETFRAMEWORK
            using (stream)
#else
            await using (stream)
#endif
            {
                if (extension != null)
                {
                    if (VerifierSettings.TryGetExtensionConverter(extension, out var conversion))
                    {
                        var result = await conversion(stream, settings.Context);
                        await VerifyBinary(result.Streams, extension, result.Info, result.Cleanup);

                        return;
                    }
                }

                extension ??= "bin";

                List <ConversionStream> streams = new()
                {
                    new(extension, stream)
                };
                await VerifyBinary(streams, extension, null, null);
            }
        }

        async Task VerifyBinary(IEnumerable <ConversionStream> streams, string infoExtension, object?info, Func <Task>?cleanup)
        {
            VerifyEngine engine = new(infoExtension, settings, directory, testName, assembly);

            var builders = streams
                           .Concat(VerifierSettings.GetFileAppenders(settings))
                           .Select(appender =>
            {
                return(new ResultBuilder(
                           appender.Extension,
                           file => GetResult(settings, file, appender)));
            })
                           .ToList();

            await VerifyInfo(engine, info);

            await HandleResults(builders, engine);

            if (cleanup != null)
            {
                await cleanup.Invoke();
            }

            await engine.ThrowIfRequired();
        }
Esempio n. 11
0
 public static void Initialize()
 {
     VerifierSettings.RegisterFileConverter <IRenderedFragment>(FragmentToStream.Convert);
     VerifierSettings.ModifySerialization(settings =>
     {
         settings.AddExtraSettings(serializerSettings =>
         {
             var converters = serializerSettings.Converters;
             converters.Add(new RenderedFragmentConverter());
         });
     });
 }
Esempio n. 12
0
 public static void RegisterComparer(
     string extension,
     float threshold = 0.999f,
     float sigma     = 3.5f,
     float gamma     = 1f,
     int angles      = 180)
 {
     Guard.AgainstNullOrEmpty(extension, nameof(extension));
     VerifierSettings.RegisterStreamComparer(
         extension,
         (received, verified, context) => Compare(context, received, verified, threshold, sigma, gamma, angles));
 }
Esempio n. 13
0
        public static void Initialize(Action <IDiffingStrategyCollection>?action = null)
        {
            Task <CompareResult> Func(string received, string verified, IReadOnlyDictionary <string, object> context)
            {
                var compare = Compare(received, verified, context, action);

                return(Task.FromResult(compare));
            }

            VerifierSettings.RegisterStringComparer("html", Func);
            VerifierSettings.RegisterStringComparer("htm", Func);
        }
Esempio n. 14
0
        bool TryGetTargetBuilder(object?target, [NotNullWhen(true)] out StringBuilder?builder, [NotNullWhen(true)] out string?extension)
        {
            var appends = VerifierSettings.GetJsonAppenders(settings);

            var hasAppends = appends.Any();

            if (target == null)
            {
                if (!hasAppends)
                {
                    builder   = null;
                    extension = null;
                    return(false);
                }

                extension = "txt";
                if (VerifierSettings.StrictJson)
                {
                    extension = "json";
                }

                builder = JsonFormatter.AsJson(
                    null,
                    settings.serialization.currentSettings,
                    appends,
                    settings);
                return(true);
            }

            if (!hasAppends && target is string stringTarget)
            {
                builder = new StringBuilder(stringTarget);
                builder.FixNewlines();
                extension = settings.ExtensionOrTxt();
                return(true);
            }

            extension = "txt";

            if (VerifierSettings.StrictJson)
            {
                extension = "json";
            }

            builder = JsonFormatter.AsJson(
                target,
                settings.serialization.currentSettings,
                appends,
                settings);

            return(true);
        }
Esempio n. 15
0
        Task VerifyString(string target)
        {
            var           appenders = VerifierSettings.GetJsonAppenders(settings);
            StringBuilder builder   = new(target);

            builder.FixNewlines();
            if (appenders.Any())
            {
                return(SerializeAndVerify(builder.ToString(), appenders));
            }

            var extension = settings.ExtensionOrTxt();

            return(VerifyStringBuilder(builder, extension));
        }
Esempio n. 16
0
        public async Task Verify <T>(T target, VerifySettings?settings = null)
        {
            settings = settings.OrDefault();
            if (target == null)
            {
                await VerifyString("null", settings);

                return;
            }
            if (target != null &&
                VerifierSettings.typeToString.TryGetValue(target.GetType(), out var toString))
            {
                await VerifyString(toString.Invoke(target, settings), settings);

                return;
            }

            if (VerifierSettings.TryGetTypedConverter(target, settings, out var converter))
            {
                var result = await converter.Conversion(target !, settings);
                await VerifyBinary(result.Streams, settings.ExtensionOrTxt(), settings, result.Info, result.Cleanup);

                return;
            }

            if (target is Stream stream)
            {
                await VerifyStream(settings, stream);

                return;
            }

            if (typeof(T).ImplementsStreamEnumerable())
            {
                var enumerable = (IEnumerable)target !;
                var streams    = enumerable.Cast <Stream>().Select(x => new ConversionStream(settings.ExtensionOrBin(), x));
                await VerifyBinary(streams, settings.ExtensionOrTxt(), settings, null, null);

                return;
            }

            var formatJson = JsonFormatter.AsJson(
                target,
                settings.serialization.currentSettings,
                settings.IsNewLineEscapingDisabled);

            await Verify(formatJson, settings);
        }
Esempio n. 17
0
        async Task VerifyStream(Stream stream)
        {
            var extension = settings.extension;

#if NETSTANDARD2_0 || NETFRAMEWORK
            using (stream)
#else
            await using (stream)
#endif
            {
                if (extension != null)
                {
                    if (VerifierSettings.TryGetExtensionConverter(extension, out var conversion))
                    {
                        var result = await conversion(stream, settings.Context);
                        await VerifyInner(result.Info, result.Cleanup, result.Targets);

                        return;
                    }
                }

                extension ??= "bin";

                List <Target> targets;
                if (EmptyFiles.Extensions.IsText(extension))
                {
                    targets = new()
                    {
                        new(extension, await stream.ReadAsString())
                    };
                    await VerifyInner(null, null, targets);
                }
                else
                {
                    targets = new()
                    {
                        new(extension, stream)
                    };
                }

                await VerifyInner(null, null, targets);
            }
        }
    }
Esempio n. 18
0
 public static void Enable()
 {
     VerifierSettings.ModifySerialization(settings =>
     {
         settings.IgnoreMembers("ETag");
         settings.IgnoreMember <Database>(x => x.Client);
         settings.IgnoreMembersWithType <CosmosDiagnostics>();
         settings.IgnoreMembersWithType <IndexingPolicy>();
         settings.IgnoreMember <ContainerResponse>(x => x.Resource);
         settings.IgnoreMembersWithType <DatabaseProperties>();
         settings.AddExtraSettings(serializerSettings =>
         {
             var converters = serializerSettings.Converters;
             converters.Add(new HeadersConverter());
             converters.Add(new FeedResponseConverter());
             converters.Add(new ResponseConverter());
         });
     });
 }
Esempio n. 19
0
        public InnerVerifier(string testName, string sourceFile, Assembly assembly, VerifySettings settings)
        {
            var projectDirectory = AttributeReader.GetProjectDirectory(assembly);

            directory     = VerifierSettings.DeriveDirectory(sourceFile, projectDirectory);
            this.testName = testName;
            this.assembly = assembly;
            this.settings = settings;

            var altProjectDirectory        = projectDirectory.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
            var altProjectDirectoryTrimmed = altProjectDirectory.TrimEnd('/', '\\');
            var projectDirectoryTrimmed    = projectDirectory.TrimEnd('/', '\\');

            settings.instanceScrubbers.Add(builder =>
            {
                builder.Replace(projectDirectory, "{ProjectDirectory}");
                builder.Replace(projectDirectoryTrimmed, "{ProjectDirectory}");
                builder.Replace(altProjectDirectory, "{ProjectDirectory}");
                builder.Replace(altProjectDirectoryTrimmed, "{ProjectDirectory}");
            });

            if (AttributeReader.TryGetSolutionDirectory(assembly, out var solutionDirectory))
            {
                var altSolutionDirectory        = solutionDirectory !.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
                var altSolutionDirectoryTrimmed = altSolutionDirectory.TrimEnd('/', '\\');
                var solutionDirectoryTrimmed    = solutionDirectory.TrimEnd('/', '\\');
                settings.instanceScrubbers.Add(builder =>
                {
                    builder.Replace(solutionDirectory, "{SolutionDirectory}");
                    builder.Replace(solutionDirectoryTrimmed, "{SolutionDirectory}");
                    builder.Replace(altSolutionDirectory, "{SolutionDirectory}");
                    builder.Replace(altSolutionDirectoryTrimmed, "{SolutionDirectory}");
                });
            }


            CounterContext.Start();
        }
Esempio n. 20
0
        async Task VerifyStringBuilder(StringBuilder target, string extension)
        {
            ApplyScrubbers.Apply(target, settings.instanceScrubbers);

            VerifyEngine engine = new(extension, settings, directory, testName, assembly);

            List <ResultBuilder> builders = new()
            {
                new(extension, file => Comparer.Text(file, target, settings))
            };

            builders.AddRange(VerifierSettings.GetFileAppenders(settings)
                              .Select(appender =>
            {
                return(new ResultBuilder(
                           appender.Extension,
                           file => GetResult(settings, file, appender)));
            }));

            await HandleResults(builders, engine);

            await engine.ThrowIfRequired();
        }
Esempio n. 21
0
        async Task VerifyInfo(VerifyEngine engine, object?info)
        {
            var appends = VerifierSettings.GetJsonAppenders(settings);

            if (info == null && !appends.Any())
            {
                return;
            }

            var file = GetFileNames("txt", settings.Namer, "info");

            var builder = JsonFormatter.AsJson(
                info,
                settings.serialization.currentSettings,
                appends,
                settings);

            ApplyScrubbers.Apply(builder, settings.instanceScrubbers);

            var result = await Comparer.Text(file, builder, settings);

            engine.HandleCompareResult(result, file);
        }
Esempio n. 22
0
 public static void Enable()
 {
     VerifierSettings.RegisterFileConverter <IApp>(AppToImage);
     VerifierSettings.RegisterFileConverter <ControlData>(AppResultToImage);
 }
Esempio n. 23
0
 public void Dispose()
 {
     VerifierSettings.RunAfterCallbacks();
 }
Esempio n. 24
0
 public static void Enable()
 {
     VerifierSettings.RegisterFileConverter <TypeToDisassemble>(ConvertTypeDefinitionHandle);
     VerifierSettings.RegisterFileConverter <MethodToDisassemble>(ConvertMethodDefinitionHandle);
     VerifierSettings.RegisterFileConverter <PropertyToDisassemble>(ConvertPropertyDefinitionHandle);
 }
Esempio n. 25
0
        public Task Verify(Expression <Func <ITuple> > target)
        {
            var dictionary = TupleConverter.ExpressionToDictionary(target);

            return(SerializeAndVerify(dictionary, VerifierSettings.GetJsonAppenders(settings)));
        }