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 #2
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);
        }