public void TypeMapper_Options_ObjectMapper_RootObject()
        {
            var options = SnapshotOptions.Create(o =>
            {
                o.AddMapper <CustomData>((ctx, itm) =>
                {
                    //only add Value and Dbl but ignore Id
                    ctx.Map(new
                    {
                        InnerValue = itm.Inner.Value,
                        OuterValue = itm.Value
                    });
                });
            });

            var snapshot = new CustomData
            {
                Id    = 1,
                Dbl   = 2.2,
                Value = "value",
                Inner = new InnerData
                {
                    Id    = 2,
                    Value = "inner"
                }
            }.Tokenize(options);

            var expected = new
            {
                OuterValue = "value",
                InnerValue = "inner"
            }.Tokenize();

            snapshot.ToString().Should().Be(expected.ToString());
        }
        public void LineParser_MatchSnapshot()
        {
            var options = SnapshotOptions.Create(o =>
            {
                o.AddDirective(line => line.Replace(" ", string.Empty, StringComparison.OrdinalIgnoreCase));
                o.UpdateSavedSnapshot();
            });


            var sn = new StringBuilder()
                     .AppendLine("Line 1")
                     .AppendLine("Line 2")
                     .AppendLine("Line 3")
                     .ToString();

            sn.MatchSnapshot(options);


            sn = new StringBuilder()
                 .AppendLine("Line    1")
                 .AppendLine("   Line 2")
                 .AppendLine("  Line     3")
                 .ToString();

            options = SnapshotOptions.Create(o =>
            {
                o.AddDirective(line => line.Replace(" ", string.Empty, StringComparison.OrdinalIgnoreCase));
            });

            sn.MatchSnapshot(options);
        }
        public void DefaultSnapshotOptions_Comparer()
        {
            SnapshotOptions.Setup(o =>
            {
                o.SetComparer((newline, savedline) => newline.Value.Replace(" ", string.Empty, StringComparison.OrdinalIgnoreCase).Equals(savedline.Value.Replace(" ", string.Empty, StringComparison.OrdinalIgnoreCase)));
            });

            var options = SnapshotOptions.Create(o =>
            {
                o.UpdateSavedSnapshot();
            });


            var sn = new StringBuilder()
                     .AppendLine("Line 1")
                     .AppendLine("Line 2")
                     .AppendLine("Line 3")
                     .ToString();

            sn.MatchSnapshot(options);


            sn = new StringBuilder()
                 .AppendLine("Line    1")
                 .AppendLine("   Line 2")
                 .AppendLine("  Line     3")
                 .ToString();

            sn.MatchSnapshot();

            // reset
            SnapshotOptions.Setup(o => { });
        }
Beispiel #4
0
        public void Mapper_MockDateTime_SnapshotOptions_Tokenize_Nullable()
        {
            DateTime?date = new DateTime(2000, 1, 1, 1, 1, 1, 1);

            var options = SnapshotOptions.Create(o => o.MockDateTimes());

            new { Value = date }.Tokenize(options).ToString().Should().Be("Value: 0000-00-00T00:00:00.0000");
        }
        public void SnapshotOptions_Merge_Comparer_Default()
        {
            var options = SnapshotOptions.Create(o => { });

            options.MergeDefault();

            options.Comparer.Should().BeSameAs(SnapshotOptions.Default.Comparer);
        }
        public void SnapshotOptions_Merge_AddTypeMappers()
        {
            var options = SnapshotOptions.Create(o => { o.AddMapper <CustomType>((c, i) => { }); });

            options.MergeDefault();

            options.TypeMappers.Count().Should().Be(1);
        }
Beispiel #7
0
        public void Mapper_MockGuid_SnapshotOptions_Tokenize_Nullable()
        {
            Guid?guid = Guid.NewGuid();

            var options = SnapshotOptions.Create(o => o.MockGuids());

            new { Value = guid }.Tokenize(options).ToString().Should().Be("Value: 00000000-0000-0000-0000-000000000000");
        }
        public void TypeMapper_Options_OverrideMapper()
        {
            var options = SnapshotOptions.Create(o =>
            {
                o.AddMapper <InnerData>((ctx, itm) =>
                {
                    // only mapp the id
                    ctx.AddLine("Value", itm.Value);
                });

                o.AddMapper <CustomData>((ctx, itm) =>
                {
                    // overrid to only add Value and Dbl but ignore Id
                    o.AddMapper <InnerData>((ctx2, itm2) =>
                    {
                        // only mapp the id
                        ctx2.AddLine("Id", itm2.Id);
                    });

                    // only add Value and Dbl but ignore Id
                    ctx.Map("Inner", itm.Inner);
                    ctx.AddLine("OuterValue", itm.Value);
                });
            });

            var snapshot = new
            {
                Item = "item",
                Data = new CustomData
                {
                    Id    = 1,
                    Dbl   = 2.2,
                    Value = "value",
                    Inner = new InnerData
                    {
                        Id    = 2,
                        Value = "inner"
                    }
                }
            }.Tokenize(options);

            var expected = new
            {
                Data = new
                {
                    OuterValue = "value",
                    Inner      = new
                    {
                        Id = 2
                    }
                },
                Item = "item",
            }.Tokenize();

            snapshot.ToString().Should().Be(expected.ToString());
        }
Beispiel #9
0
        public void Snapshot_Options_UseBasicFormatters_CustomOptions()
        {
            // reset
            var options = SnapshotOptions.Create(o =>
            {
                o.UseBasicFormatters();
            });

            options.Formatters.Count().Should().Be(2);
        }
Beispiel #10
0
        public void Snapshot_Options_AddDirectives()
        {
            var options = SnapshotOptions.Create(o =>
            {
                // ignore Guids when comparing
                o.AddDirective(s => s.ReplaceRegex(@"(?im)[{(]?[0-9A-Fa-f]{8}[-]?(?:[0-9A-Fa-f]{4}[-]?){3}[0-9A-Fa-f]{12}[)}]?", "00000000-0000-0000-0000-000000000000"));
            });

            new { Value = Guid.NewGuid() }.MatchSnapshot(options);
        }
        public void SnapshotOptions_IsValueType_Custom()
        {
            var options = SnapshotOptions.Create(o =>
            {
                o.EvaluateValueType((type, obj) => type.IsValueType && type != typeof(string));
            });

            options.IsValueType(typeof(int), 1).Should().BeTrue();
            options.IsValueType(typeof(string), "1").Should().BeFalse();
        }
Beispiel #12
0
        public void Options_Directives_ReplaceDateTime()
        {
            var options = SnapshotOptions.Create(o =>
            {
                // ignore DateTime when comparing
                o.AddDirective(s => s.ReplaceDateTime());
            });

            new { Value = DateTime.Now }.MatchSnapshot(options);
        }
        public void SnapshotOptions_Merge_UpdateSnapshot_Default()
        {
            SnapshotOptions.Default.UpdateSavedSnapshot();

            var options = SnapshotOptions.Create(o => { });

            options.MergeDefault();

            options.UpdateSnapshot.Should().Be(SnapshotOptions.Default.UpdateSnapshot).And.BeTrue();
        }
Beispiel #14
0
        public void Options_Directives_ReplaceDateTime_CustomFormat()
        {
            var options = SnapshotOptions.Create(o =>
            {
                // ignore Guids when comparing
                o.AddDirective(s => s.ReplaceDateTime("datetime replacement"));
            });

            new { Value = DateTime.Now }.MatchSnapshot(options);
        }
        public void Directive_Extension_ReplaceGuid()
        {
            var options = SnapshotOptions.Create(o =>
            {
                // ignore Guids when comparing
                o.AddDirective(s => s.ReplaceGuid());
            });

            new { Value = Guid.NewGuid() }.MatchSnapshot(options);
        }
Beispiel #16
0
        public void Options_Directives_ReplaceGuid_CustomFormat()
        {
            var options = SnapshotOptions.Create(o =>
            {
                // ignore Guids when comparing
                o.AddDirective(s => s.ReplaceGuid("guid replacement"));
            });

            new { Value = Guid.NewGuid() }.MatchSnapshot(options);
        }
        public void SnapshotOptions_Merge_Comparer_Custom()
        {
            var options = SnapshotOptions.Create(o =>
            {
                o.SetComparer((l1, l2) => true);
            });

            options.MergeDefault();

            options.Comparer.Should().NotBeSameAs(SnapshotOptions.Default.Comparer);
        }
Beispiel #18
0
        public void Snapshot_Options_UseBasicFormatters_CustomOptions_DefaultSet()
        {
            SnapshotOptions.Setup(o =>
            {
                o.UseBasicFormatters();
            });

            var options = SnapshotOptions.Create(o => { });

            options.Formatters.Count().Should().Be(2);
        }
        public void Mapper_Set_Formatters_Generic()
        {
            var options = SnapshotOptions.Create(o =>
            {
                o.AddFormatter <double>(value => ((int)value).ToString());
            });

            new
            {
                Value = 2.2
            }.Tokenize(options).ToString().Should().Be("Value: 2");
        }
        public void SnapshotOptions_Merge_AddFormatters()
        {
            var formatter = new NumberFormatter();

            var options = SnapshotOptions.Create(o =>
            {
                o.AddFormatter(typeof(int), formatter);
            });

            options.MergeDefault();

            options.Formatters.Count().Should().Be(6);
            options.Formatters.Should().Contain(formatter);
        }
        public void SnapshotOptions_Merge_TypeMappers_MultipleSame()
        {
            SnapshotOptions.Setup(o =>
            {
                o.AddMapper <CustomType>((c, m) => { });
                o.AddMapper <CustomOtherType>((c, m) => { });
            });

            var options = SnapshotOptions.Create(o => { o.AddMapper <CustomType>((c, i) => { }); });

            options.MergeDefault();

            options.TypeMappers.Count().Should().Be(2);
        }
        public void Mapper_Set_Formatters_Options_MatchSnapshot()
        {
            var options = SnapshotOptions.Create(o =>
            {
                o.AddFormatter(typeof(CustomObject), new CustomObjectFormatter());
            });

            new
            {
                Value = new CustomObject {
                    Value = "TesT"
                },
                Str = (string)null
            }.MatchSnapshot(options);
        }
        public void Mapper_Set_Formatters_Options()
        {
            var options = SnapshotOptions.Create(o =>
            {
                o.AddFormatter(typeof(CustomObject), new CustomObjectFormatter());
            });

            new
            {
                Value = new CustomObject {
                    Value = "TesT"
                },
                Str = (string)null
            }.Tokenize(options).ToString().Should().Be($"Str: null{Environment.NewLine}Value: custom - TesT");
        }
        public void Mapper_Set_Formatters_TopLevel()
        {
            var options = SnapshotOptions.Create(o =>
            {
                o.AddFormatter(typeof(CustomObject), new CustomObjectFormatter());
            });

            new CustomObject {
                Value = "TesT"
            }
            .Tokenize(options)
            .ToString()
            .Should()
            .Be("custom - TesT");
        }
Beispiel #25
0
        public void Event_DataFactory()
        {
            var item = new
            {
                Id    = 1,
                Value = "value"
            };
            var e = new Event("id", f => f.BuildFrom(item));

            var options = SnapshotOptions.Create(o =>
            {
                // ignore regex when comparing
                o.AddDirective(s => s.ReplaceGuid());
            });

            e.MatchSnapshot(options);
        }
        public void DefaultSnapshotOptions_UpdateSnapshot()
        {
            var options = SnapshotOptions.Create(o =>
            {
                o.UpdateSavedSnapshot();
            });

            var sn = new StringBuilder()
                     .AppendLine("Line 1")
                     .AppendLine("Line 2")
                     .AppendLine("Line 3")
                     .ToString();

            // rewrite the snapshot
            sn.MatchSnapshot(options);


            sn = new StringBuilder()
                 .AppendLine("Line    1")
                 .AppendLine("   Line 2")
                 .AppendLine("  Line     3")
                 .ToString();

            Action fail = () => sn.MatchSnapshot();

            Assert.Throws <SnapshotMatchException>(() => fail.Invoke());

            // rewrite snapshot
            sn.MatchSnapshot(options);

            sn = new StringBuilder()
                 .AppendLine("Line    1")
                 .AppendLine("   Line 2")
                 .AppendLine("  Line     3")
                 .ToString();

            // Final Assert
            sn.MatchSnapshot();

            // reset
            SnapshotOptions.Setup(o => { });
        }
        public void SnapshotOptions_Merge_Formatters_MultipleSame()
        {
            SnapshotOptions.Setup(o =>
            {
                o.Formatters = new MapperCollection <Type, IValueFormatter>
                {
                    { typeof(int), new NumberFormatter() },
                    { typeof(double), new NumberFormatter() }
                };
            });

            var options = SnapshotOptions.Create(o =>
            {
                o.AddFormatter(typeof(int), new NumberFormatter());
            });

            options.MergeDefault();

            options.Formatters.Count().Should().Be(2);
        }
Beispiel #28
0
        public void SnapshotOptions_Api_Full()
        {
            var options = SnapshotOptions.Create(o =>
            {
                // directive to change the string
                o.AddDirective(line => line.Replace(" ", string.Empty));

                o.AddFormatter(typeof(double), new NumberFormatter());
                o.AddFormatter <decimal>(value => value.ToString(CultureInfo.InvariantCulture));

                o.AddFormatter(typeof(MappableClass), new MappableClassFormatter());
                o.AddFormatter <MappableClass>(cls => $"Id: {cls.Id}");

                o.AddMapper <MappableClass>((ctx, item) =>
                {
                    ctx.AddLine("Id", item.Id);
                });

                o.SetComparer((line1, line2) => line1.Equals(line2));
            });
        }
Beispiel #29
0
        public void Snapshot_Options()
        {
            var sn = new StringBuilder()
                     .AppendLine("Line 1")
                     .AppendLine("Line 2")
                     .AppendLine("Line 3");

            var snapshot = SnapshotTokenizer.Tokenize(sn.ToString());

            snapshot.MatchSnapshot(SnapshotOptions.Create(c =>
            {
                c.UpdateSavedSnapshot();
            }));

            sn = new StringBuilder()
                 .AppendLine("Line    1")
                 .AppendLine("   Line 2")
                 .AppendLine("  Line     3");

            snapshot = SnapshotTokenizer.Tokenize(sn.ToString());

            var options = new SnapshotOptions
            {
                Comparer = new LineCompare((newline, savedline) => newline.Value.Replace(" ", string.Empty, StringComparison.OrdinalIgnoreCase).Equals(savedline.Value.Replace(" ", string.Empty, StringComparison.OrdinalIgnoreCase)))
            };

            snapshot.MatchSnapshot(options);



            options = SnapshotOptions.Create(o =>
            {
                o.SetComparer((newline, savedline) => newline.Value.Replace(" ", string.Empty, StringComparison.OrdinalIgnoreCase).Equals(savedline.Value.Replace(" ", string.Empty, StringComparison.OrdinalIgnoreCase)));
            });

            snapshot.MatchSnapshot(options);

            // reset
            SnapshotOptions.Setup(o => { });
        }
        public void TypeMapper_Options_ObjectMapper_MergeSubItems()
        {
            var options = SnapshotOptions.Create(o =>
            {
                o.AddMapper <CustomData>((ctx, itm) =>
                {
                    //only add Value and Dbl but ignore Id
                    ctx.AddLine("Inner", itm.Inner.Value);
                    ctx.AddLine("Outer", itm.Value);
                });
            });

            var snapshot = new
            {
                Item = "item",
                Data = new CustomData
                {
                    Id    = 1,
                    Dbl   = 2.2,
                    Value = "value",
                    Inner = new InnerData
                    {
                        Id    = 2,
                        Value = "inner"
                    }
                }
            }.Tokenize(options);

            var expected = new
            {
                Data = new
                {
                    Outer = "value",
                    Inner = "inner"
                },
                Item = "item",
            }.Tokenize();

            snapshot.ToString().Should().Be(expected.ToString());
        }