Ejemplo n.º 1
0
        public void FormatterSet_recursively_formats_types_within_IEnumerable()
        {
            var list = new List <Widget>
            {
                new Widget {
                    Name = "widget x"
                },
                new Widget {
                    Name = "widget y"
                },
                new Widget {
                    Name = "widget z"
                }
            };

            var formatted = new FormatterSet()
                            .RegisterFormatter <Widget>(
                w => w.Name + ", Parts: " +
                (w.Parts == null ? "0" : w.Parts.Count.ToString()))
                            .Format(list);

            Console.WriteLine(formatted);

            Assert.That(formatted,
                        Is.EqualTo("{ widget x, Parts: 0, widget y, Parts: 0, widget z, Parts: 0 }"));
        }
Ejemplo n.º 2
0
        public void Exceptions_always_get_properties_formatters()
        {
            var exception = new ReflectionTypeLoadException(
                new[]
            {
                typeof(FileStyleUriParser),
                typeof(AssemblyKeyFileAttribute)
            },
                new Exception[]
            {
                new EventLogInvalidDataException()
            });
            var formatter = new FormatterSet();

            var message = formatter.Format(exception);

            Assert.That(message,
                        Contains.Substring("ReflectionTypeLoadException"));
            Assert.That(message,
                        Contains.Substring("FileStyleUriParser"));
            Assert.That(message,
                        Contains.Substring("AssemblyKeyFileAttribute"));
            Assert.That(message,
                        Contains.Substring("EventLogInvalidDataException"));
        }
Ejemplo n.º 3
0
        public void Anonymous_types_are_automatically_fully_formatted()
        {
            var formatter = new FormatterSet();
            var ints      = new[] { 3, 2, 1 };
            var output    = formatter.Format(new { ints, count = ints.Count() });

            Assert.That(output, Is.EqualTo("{ ints = { 3, 2, 1 } | count = 3 }"));
        }
Ejemplo n.º 4
0
        public void Default_formatter_for_Type_displays_only_the_name()
        {
            var formatter = new FormatterSet();

            Assert.That(formatter.Format(GetType()),
                        Is.EqualTo(GetType().Name));
            Assert.That(formatter.Format(typeof(FormatterSetTests)),
                        Is.EqualTo(typeof(FormatterSetTests).Name));
        }
Ejemplo n.º 5
0
        public void Custom_formatter_for_Type_can_be_registered()
        {
            var formatter = new FormatterSet();

            formatter.RegisterFormatter <Type>(t => t.GUID.ToString());

            Assert.That(formatter.Format(GetType()),
                        Is.EqualTo(GetType().GUID.ToString()));
        }
Ejemplo n.º 6
0
        public void Default_formatter_for_null_Nullable_indicates_null()
        {
            var formatter = new FormatterSet();
            int?nullable  = null;

            var output = formatter.Format(nullable);

            Assert.That(output, Is.EqualTo(formatter.Format <object>(null)));
        }
Ejemplo n.º 7
0
        public void Static_properties_are_not_written()
        {
            var formatter = new FormatterSet();

            formatter.RegisterPropertiesFormatter <BoundaryTests.NestedWidget>();

            Assert.That(formatter.Format(new Widget()),
                        !Contains.Substring("StaticProperty"));
        }
Ejemplo n.º 8
0
        public void CreatePropertiesFormatter_does_not_include_autoproperty_backing_fields()
        {
            var formatter = new FormatterSet().CreatePropertiesFormatter <Node>(true);

            var output = formatter(new Node());

            Assert.That(output, !Contains.Substring("<Nodes>k__BackingField"));
            Assert.That(output, !Contains.Substring("<NodesArray>k__BackingField"));
        }
Ejemplo n.º 9
0
        public void RegisterPropertiesFormatter_for_string_does_nothing()
        {
            var formatter = new FormatterSet();

            formatter.RegisterPropertiesFormatter <string>();

            Assert.That(formatter.Format("hello"),
                        Is.EqualTo("hello"));
        }
Ejemplo n.º 10
0
        public void CreatePropertiesFormatter_can_include_internal_properties()
        {
            var formatter = new FormatterSet().CreatePropertiesFormatter <Node>(true);

            var output = formatter(new Node {
                Id = "6"
            });

            Assert.That(output, Contains.Substring("InternalId = 6"));
        }
Ejemplo n.º 11
0
        public void FormatterSet_expands_IEnumerable()
        {
            var list = new List <string> {
                "this", "that", "the other thing"
            };

            var formatted = new FormatterSet().Format(list);

            Assert.That(formatted, Is.EqualTo("{ this, that, the other thing }"));
        }
Ejemplo n.º 12
0
        public void CreateFormatter_throws_when_an_expression_is_not_a_MemberExpression()
        {
            var formatter = new FormatterSet();

            var ex = Assert.Throws <ArgumentException>(() => formatter.CreateFormatterFor <SomethingWithLotsOfProperties>(
                                                           o => o.DateProperty.ToShortDateString(),
                                                           o => o.StringProperty));

            Assert.That(ex.Message,
                        Contains.Substring("o => o.DateProperty.ToShortDateString()"));
        }
Ejemplo n.º 13
0
        public void CreateFormatter_throws_when_an_expression_is_not_a_MemberExpression()
        {
            var formatter = new FormatterSet();

            var ex = Assert.Throws<ArgumentException>(() => formatter.CreateFormatterFor<SomethingWithLotsOfProperties>(
                o => o.DateProperty.ToShortDateString(),
                o => o.StringProperty));

            Assert.That(ex.Message,
                        Contains.Substring("o => o.DateProperty.ToShortDateString()"));
        }
Ejemplo n.º 14
0
        public void CreatePropertiesFormatter_expands_properties_of_structs()
        {
            var func = new FormatterSet().CreatePropertiesFormatter <EntityId>();

            var id    = new EntityId("the typename", "the id");
            var value = func(id);

            Console.WriteLine(value);

            Assert.That(value,
                        Contains.Substring("TypeName = the typename"));
            Assert.That(value,
                        Contains.Substring("Id = the id"));
        }
Ejemplo n.º 15
0
        public void When_Clear_is_called_then_default_formatters_are_immediately_reregistered()
        {
            var formatterSet = new FormatterSet();
            var logEntry     = new LogEntry("hola!");

            var before = formatterSet.Format(logEntry);

            formatterSet.RegisterFormatter <LogEntry>(e => "hello!");

            Assert.That(formatterSet.Format(logEntry),
                        Is.Not.EqualTo(before));

            formatterSet.Clear();

            Assert.That(formatterSet.Format(logEntry),
                        Is.EqualTo(before));
        }
Ejemplo n.º 16
0
        public void CreatePropertiesFormatter_expands_fields_of_objects()
        {
            var func     = new FormatterSet().CreatePropertiesFormatter <SomeStruct>();
            var today    = DateTime.Today;
            var tomorrow = DateTime.Today.AddDays(1);
            var id       = new SomeStruct
            {
                DateField    = today,
                DateProperty = tomorrow
            };

            var value = func(id);

            Console.WriteLine(value);

            Assert.That(value,
                        Contains.Substring("DateField = "));
            Assert.That(value,
                        Contains.Substring("DateProperty = "));
        }
Ejemplo n.º 17
0
        public void FormatterSet_truncates_expansion_of_long_IEnumerable()
        {
            var list = new List <string>();

            for (var i = 1; i < 11; i++)
            {
                list.Add("number " + i);
            }

            var formatterSet = new FormatterSet
            {
                ListExpansionLimit = 4
            };
            var formatted = formatterSet.Format(list);

            Console.WriteLine(formatted);

            Assert.IsTrue(formatted.Contains("number 1"));
            Assert.IsTrue(formatted.Contains("number 4"));
            Assert.IsFalse(formatted.Contains("number 5"));
            Assert.IsTrue(formatted.Contains("6 more"));
        }
Ejemplo n.º 18
0
        public void Custom_formatter_for_Type_can_be_registered()
        {
            var formatter = new FormatterSet();

            formatter.RegisterFormatter<Type>(t => t.GUID.ToString());

            Assert.That(formatter.Format(GetType()),
                        Is.EqualTo(GetType().GUID.ToString()));
        }
Ejemplo n.º 19
0
        public void CreatePropertiesFormatter_expands_properties_of_structs()
        {
            var func = new FormatterSet().CreatePropertiesFormatter<EntityId>();

            var id = new EntityId("the typename", "the id");
            var value = func(id);

            Console.WriteLine(value);

            Assert.That(value,
                        Contains.Substring("TypeName = the typename"));
            Assert.That(value,
                        Contains.Substring("Id = the id"));
        }
Ejemplo n.º 20
0
        public void FormatterSet_recursively_formats_types_within_IEnumerable()
        {
            var list = new List<Widget>
            {
                new Widget { Name = "widget x" },
                new Widget { Name = "widget y" },
                new Widget { Name = "widget z" }
            };

            var formatted = new FormatterSet()
                .RegisterFormatter<Widget>(
                    w => w.Name + ", Parts: " +
                         (w.Parts == null ? "0" : w.Parts.Count.ToString()))
                .Format(list);
            Console.WriteLine(formatted);

            Assert.That(formatted,
                        Is.EqualTo("{ widget x, Parts: 0, widget y, Parts: 0, widget z, Parts: 0 }"));
        }
Ejemplo n.º 21
0
        public void Default_formatter_for_null_Nullable_indicates_null()
        {
            var formatter = new FormatterSet();
            int? nullable = null;

            var output = formatter.Format(nullable);

            Assert.That(output, Is.EqualTo(formatter.Format<object>(null)));
        }
Ejemplo n.º 22
0
        public void FormatterSet_truncates_expansion_of_long_IEnumerable()
        {
            var list = new List<string>();
            for (var i = 1; i < 11; i++)
            {
                list.Add("number " + i);
            }

            var formatterSet = new FormatterSet
            {
                ListExpansionLimit = 4
            };
            var formatted = formatterSet.Format(list);
            Console.WriteLine(formatted);

            Assert.IsTrue(formatted.Contains("number 1"));
            Assert.IsTrue(formatted.Contains("number 4"));
            Assert.IsFalse(formatted.Contains("number 5"));
            Assert.IsTrue(formatted.Contains("6 more"));
        }
Ejemplo n.º 23
0
        public void Anonymous_types_are_automatically_fully_formatted()
        {
            var formatter = new FormatterSet();
            var ints = new[] { 3, 2, 1 };
            var output = formatter.Format(new { ints, count = ints.Count() });

            Assert.That(output, Is.EqualTo("{ ints = { 3, 2, 1 } | count = 3 }"));
        }
Ejemplo n.º 24
0
        public void When_Clear_is_called_then_default_formatters_are_immediately_reregistered()
        {
            var formatterSet = new FormatterSet();
            var logEntry = new LogEntry("hola!");

            var before = formatterSet.Format(logEntry);

            formatterSet.RegisterFormatter<LogEntry>(e => "hello!");

            Assert.That(formatterSet.Format(logEntry),
                        Is.Not.EqualTo(before));

            formatterSet.Clear();

            Assert.That(formatterSet.Format(logEntry),
                        Is.EqualTo(before));
        }
Ejemplo n.º 25
0
        public void FormatterSet_expands_IEnumerable()
        {
            var list = new List<string> { "this", "that", "the other thing" };

            var formatted = new FormatterSet().Format(list);

            Assert.That(formatted, Is.EqualTo("{ this, that, the other thing }"));
        }
Ejemplo n.º 26
0
        public void Default_formatter_for_Type_displays_only_the_name()
        {
            var formatter = new FormatterSet();

            Assert.That(formatter.Format(GetType()),
                        Is.EqualTo(GetType().Name));
            Assert.That(formatter.Format(typeof (FormatterSetTests)),
                        Is.EqualTo(typeof (FormatterSetTests).Name));
        }
Ejemplo n.º 27
0
        public void Static_properties_are_not_written()
        {
            var formatter = new FormatterSet();
            formatter.RegisterPropertiesFormatter<BoundaryTests.NestedWidget>();

            Assert.That(formatter.Format(new Widget()),
                        !Contains.Substring("StaticProperty"));
        }
Ejemplo n.º 28
0
        public void CreatePropertiesFormatter_expands_fields_of_objects()
        {
            var func = new FormatterSet().CreatePropertiesFormatter<SomeStruct>();
            var today = DateTime.Today;
            var tomorrow = DateTime.Today.AddDays(1);
            var id = new SomeStruct
            {
                DateField = today,
                DateProperty = tomorrow
            };

            var value = func(id);

            Console.WriteLine(value);

            Assert.That(value,
                        Contains.Substring("DateField = "));
            Assert.That(value,
                        Contains.Substring("DateProperty = "));
        }
Ejemplo n.º 29
0
        public void Exceptions_always_get_properties_formatters()
        {
            var exception = new ReflectionTypeLoadException(
                new[]
                {
                    typeof (FileStyleUriParser),
                    typeof (AssemblyKeyFileAttribute)
                },
                new Exception[]
                {
                    new EventLogInvalidDataException()
                });
            var formatter = new FormatterSet();

            var message = formatter.Format(exception);

            Assert.That(message,
                        Contains.Substring("ReflectionTypeLoadException"));
            Assert.That(message,
                        Contains.Substring("FileStyleUriParser"));
            Assert.That(message,
                        Contains.Substring("AssemblyKeyFileAttribute"));
            Assert.That(message,
                        Contains.Substring("EventLogInvalidDataException"));
        }
Ejemplo n.º 30
0
        public void CreatePropertiesFormatter_does_not_include_autoproperty_backing_fields()
        {
            var formatter = new FormatterSet().CreatePropertiesFormatter<Node>(true);

            var output = formatter(new Node());

            Assert.That(output, !Contains.Substring("<Nodes>k__BackingField"));
            Assert.That(output, !Contains.Substring("<NodesArray>k__BackingField"));
        }
Ejemplo n.º 31
0
        public void RegisterPropertiesFormatter_for_string_does_nothing()
        {
            var formatter = new FormatterSet();
            formatter.RegisterPropertiesFormatter<string>();

            Assert.That(formatter.Format("hello"),
                        Is.EqualTo("hello"));
        }
Ejemplo n.º 32
0
        public void CreatePropertiesFormatter_can_include_internal_properties()
        {
            var formatter = new FormatterSet().CreatePropertiesFormatter<Node>(true);

            var output = formatter(new Node { Id = "6" });

            Assert.That(output, Contains.Substring("InternalId = 6"));
        }