Esempio n. 1
0
        public async Task Verify <T>(T target, VerifySettings?settings = null)
        {
            settings = settings.OrDefault();
            if (target == null)
            {
                await VerifyString("null", settings);

                return;
            }

            if (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. 2
0
        public async Task Verify <T>(T target)
        {
            if (target == null)
            {
                AssertExtensionIsNull();
                await VerifyInner("null", null, emptyTargets);

                return;
            }

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

                var value = stringResult.Value;
                if (value == string.Empty)
                {
                    await VerifyInner("emptyString", null, emptyTargets);

                    return;
                }

                await VerifyInner(value, null, emptyTargets);

                return;
            }

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

                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 Target(settings.ExtensionOrTxt(), "null"));
                    }

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

                return;
            }

            AssertExtensionIsNull();
            await VerifyInner(target, null, emptyTargets);
        }