public void WhenObjectContainsCyclicReferencesInList_ThenRecursiveDestructureIsImmediatelyStopped()
        {
            // Arrange
            var cyclic = new MyObjectCollection
            {
                Foo = "Cyclic",
            };

            cyclic.Reference = cyclic;
            var exception = new Cyclic2Exception
            {
                MyObjectCollection = new MyObjectCollection(),
            };

            exception.MyObjectCollection.Foo       = "bar";
            exception.MyObjectCollection.Reference = cyclic;

            // Act
            var result       = new ExceptionPropertiesBag(new Exception());
            var destructurer = new ReflectionBasedDestructurer(10);

            destructurer.Destructure(exception, result, EmptyDestructurer());

            // Assert
            var myObject = (List <object>)result.GetResultDictionary()[nameof(Cyclic2Exception.MyObjectCollection)];

            // exception.MyObjectCollection[0] is still list
            var firstLevelList = Assert.IsType <List <object> >(myObject[0]);

            // exception.MyObjectCollection[0][0] we notice that we would again destructure "cyclic"
            var secondLevelList = Assert.IsType <Dictionary <string, object> >(firstLevelList[0]);

            Assert.Equal("Cyclic reference", secondLevelList["$ref"]);
        }
        public void DestructuringDepthIsLimitedByConfiguredDepth()
        {
            // Arrange
            var exception = new RecursiveException()
            {
                Node = new RecursiveNode()
                {
                    Name  = "PARENT",
                    Child = new RecursiveNode()
                    {
                        Name  = "CHILD 1",
                        Child = new RecursiveNode()
                        {
                            Name = "CHILD 2",
                        },
                    },
                },
            };
            var destructurer = new ReflectionBasedDestructurer(1);

            // Act
            var propertiesBag = new ExceptionPropertiesBag(exception);

            destructurer.Destructure(exception, propertiesBag, EmptyDestructurer());

            // Assert
            // Parent is depth 1
            // First child is depth 2
            var properties = propertiesBag.GetResultDictionary();
            var parent     = (IDictionary <string, object>)properties[nameof(RecursiveException.Node)];

            Assert.Equal("PARENT", parent[nameof(RecursiveNode.Name)]);
            Assert.IsType <RecursiveNode>(parent[nameof(RecursiveNode.Child)]);
        }
        public void WhenObjectContainsCyclicReferences_ThenNoStackoverflowExceptionIsThrown()
        {
            // Arrange
            var exception = new CyclicException
            {
                MyObject = new MyObject(),
            };

            exception.MyObject.Foo        = "bar";
            exception.MyObject.Reference  = exception.MyObject;
            exception.MyObject.Reference2 = exception.MyObject;

            // Act
            var result       = new ExceptionPropertiesBag(new Exception());
            var destructurer = new ReflectionBasedDestructurer(10);

            destructurer.Destructure(exception, result, EmptyDestructurer());

            // Assert
            var myObject = (Dictionary <string, object>)result.GetResultDictionary()["MyObject"];

            Assert.Equal("bar", myObject["Foo"]);
            Assert.Equal(myObject["$id"], ((Dictionary <string, object>)myObject["Reference"])["$ref"]);
            Assert.Equal(myObject["$id"], ((Dictionary <string, object>)myObject["Reference2"])["$ref"]);
            Assert.Equal("1", myObject["$id"]);
        }
Example #4
0
        public void When_object_contains_cyclic_references_in_list_then_recursive_destructure_is_immediately_stopped()
        {
            // Arrange
            var cyclic = new MyObjectEnumerable
            {
                Foo = "Cyclic"
            };

            cyclic.Reference = cyclic;
            var exception = new CyclicException2
            {
                MyObjectEnumerable = new MyObjectEnumerable()
            };

            exception.MyObjectEnumerable.Foo       = "bar";
            exception.MyObjectEnumerable.Reference = cyclic;

            // Act
            var result       = new Dictionary <string, object>();
            var destructurer = new ReflectionBasedDestructurer(new List <string>());

            destructurer.Destructure(exception, result, null);

            // Assert
            var myObject = (List <object>)result["MyObjectEnumerable"];

            // exception.MyObjectEnumerable[0] is still list
            var firstLevelList = Assert.IsType <List <object> >(myObject[0]);

            // exception.MyObjectEnumerable[0][0] we notice that we would again destructure "cyclic"
            var secondLevelList = Assert.IsType <Dictionary <string, object> >(firstLevelList[0]);

            Assert.Equal("Cyclic reference", secondLevelList["$ref"]);
        }
Example #5
0
        public void When_object_contains_cyclic_references_then_no_stackoverflow_exception_is_thrown()
        {
            // Arrange
            var exception = new CyclicException
            {
                MyObject = new MyObject()
            };

            exception.MyObject.Foo        = "bar";
            exception.MyObject.Reference  = exception.MyObject;
            exception.MyObject.Reference2 = exception.MyObject;

            // Act
            var result       = new Dictionary <string, object>();
            var destructurer = new ReflectionBasedDestructurer(new List <string>());

            destructurer.Destructure(exception, result, null);

            // Assert
            var myObject = (Dictionary <string, object>)result["MyObject"];

            Assert.Equal("bar", myObject["Foo"]);
            Assert.Equal(myObject["$id"], ((Dictionary <string, object>)myObject["Reference"])["$ref"]);
            Assert.Equal(myObject["$id"], ((Dictionary <string, object>)myObject["Reference2"])["$ref"]);
            Assert.Equal("1", myObject["$id"]);
        }
Example #6
0
        public void ReflectionBasedDestructurer_MultiplePropertiesCanBeIgnored()
        {
            var exception         = this.GetException();
            var properties        = new Dictionary <string, object>();
            var localDestructurer = new ReflectionBasedDestructurer(new List <string> {
                nameof(TestException.Message), nameof(TestException.Source)
            });

            localDestructurer.Destructure(exception, properties, null);

            Assert.DoesNotContain(properties.Keys, p => p == nameof(TestException.Message));
            Assert.DoesNotContain(properties.Keys, p => p == nameof(TestException.Source));
        }
Example #7
0
        public void ReflectionBasedDestructurer_PropertiesCanBeIgnored()
        {
            // Arrange
            var exception         = this.GetException();
            var properties        = new Dictionary <string, object>();
            var localDestructurer = new ReflectionBasedDestructurer(new List <string> {
                nameof(TestException.Source)
            });

            // Act
            localDestructurer.Destructure(exception, properties, null);

            // Assert
            Assert.DoesNotContain(properties, x => string.Equals(x.Key, nameof(TestException.Source)));
        }
Example #8
0
        public void ReflectionBasedDestructurer_NestedPropertiesCanBeIgnored()
        {
            // Arrange
            var exception         = this.GetException();
            var properties        = new Dictionary <string, object>();
            var localDestructurer = new ReflectionBasedDestructurer(new List <string> {
                "NestedPropertyKey"
            });

            // Act
            localDestructurer.Destructure(exception, properties, null);
            var nestedProperty = properties[nameof(TestException.NestedProperty)];
            var nestedPair     = (Dictionary <string, object>)nestedProperty;

            // Assert
            Assert.NotNull(nestedPair);
            Assert.Empty(nestedPair);
        }
Example #9
0
        public void ReflectionBasedDestructurer_MultipleNestedPropertiesCanBeIgnored()
        {
            var exception         = this.GetException();
            var properties        = new Dictionary <string, object>();
            var localDestructurer = new ReflectionBasedDestructurer(new List <string> {
                nameof(TestException.Source), "NestedPropertyKey"
            });

            localDestructurer.Destructure(exception, properties, null);

            var nestedProperty = properties[nameof(TestException.NestedProperty)];
            var nestedPair     = (Dictionary <string, object>)nestedProperty;

            // Assert
            Assert.NotNull(nestedPair);
            Assert.Empty(nestedPair);
            Assert.DoesNotContain(properties.Keys, p => p == nameof(TestException.Source));
        }
        public void WhenObjectContainsCyclicReferencesInDict_ThenRecursiveDestructureIsImmediatelyStopped()
        {
            // Arrange
            var cyclic = new MyObjectDict
            {
                Foo       = "Cyclic",
                Reference = new Dictionary <string, object>(),
            };

            cyclic.Reference["x"] = cyclic.Reference;
            var exception = new CyclicDictException
            {
                MyObjectDict = cyclic,
            };

            // Act
            var result       = new ExceptionPropertiesBag(new Exception());
            var destructurer = new ReflectionBasedDestructurer(10);

            destructurer.Destructure(exception, result, EmptyDestructurer());

            // Assert
            var myObject = (Dictionary <string, object>)result.GetResultDictionary()["MyObjectDict"];

            // exception.MyObjectDict["Reference"] is still regular dictionary
            var firstLevelDict = Assert.IsType <Dictionary <string, object> >(myObject["Reference"]);
            var id             = firstLevelDict["$id"];

            Assert.Equal("1", id);

            // exception.MyObjectDict["Reference"]["x"] we notice that we are destructuring same dictionary
            var secondLevelDict = Assert.IsType <Dictionary <string, object> >(firstLevelDict["x"]);
            var refId           = Assert.IsType <string>(secondLevelDict["$ref"]);

            Assert.Equal(id, refId);
        }
Example #11
0
        public void When_object_contains_cyclic_references_in_dict_then_recursive_destructure_is_immediately_stopped()
        {
            // Arrange
            var cyclic = new MyObjectDict
            {
                Foo       = "Cyclic",
                Reference = new Dictionary <string, object>()
            };

            cyclic.Reference["x"] = cyclic.Reference;
            var exception = new CyclicExceptionDict
            {
                MyObjectDict = cyclic
            };

            // Act
            var result       = new Dictionary <string, object>();
            var destructurer = new ReflectionBasedDestructurer(new List <string>());

            destructurer.Destructure(exception, result, null);

            // Assert
            var myObject = (Dictionary <string, object>)result["MyObjectDict"];

            // exception.MyObjectDict["Reference"] is still regular dictionary
            var firstLevelDict = Assert.IsType <Dictionary <string, object> >(myObject["Reference"]);
            var id             = firstLevelDict["$id"];

            Assert.Equal("1", id);

            // exception.MyObjectDict["Reference"]["x"] we notice that we are destructuring same dictionary
            var secondLevelDict = Assert.IsType <Dictionary <string, object> >(firstLevelDict["x"]);
            var refId           = Assert.IsType <string>(secondLevelDict["$ref"]);

            Assert.Equal(id, refId);
        }
Example #12
0
 public ReflectionBasedDestructurerTest()
 {
     this.destructurer = new ReflectionBasedDestructurer(new List <string>());
 }
Example #13
0
 public ReflectionBasedDestructurerTest()
 {
     this.destructurer = new ReflectionBasedDestructurer();
 }