public void AddedProperty_WhenFilterIsNotSetToIgnoreIt_IsIncluded() { // Arrange var properties = new ExceptionPropertiesBag( new Exception(), new IgnorePropertyByNameExceptionFilter(new[] { "not key" })); // Act properties.AddProperty("key", "value"); // Assert var results = properties.GetResultDictionary(); Assert.Equal(1, results.Count); Assert.Contains("key", results.Keys); var value = results["key"]; Assert.Equal("value", value); }
private static void Test_ResultOfReflectionDestructurerShouldBeEquivalentToCustomOne( Exception exception, IExceptionDestructurer customDestructurer) { // Arrange var reflectionBasedResult = new ExceptionPropertiesBag(exception); var customBasedResult = new ExceptionPropertiesBag(exception); var reflectionBasedDestructurer = CreateReflectionBasedDestructurer(); // Act reflectionBasedDestructurer.Destructure(exception, reflectionBasedResult, InnerDestructurer(reflectionBasedDestructurer)); customDestructurer.Destructure(exception, customBasedResult, InnerDestructurer(new ArgumentExceptionDestructurer())); // Assert var reflectionBasedDictionary = (Dictionary <string, object>)reflectionBasedResult.GetResultDictionary(); var customBasedDictionary = (Dictionary <string, object>)customBasedResult.GetResultDictionary(); reflectionBasedDictionary.Should().BeEquivalentTo(customBasedDictionary); }
public void CanDestructureObjectWithHiddenProperty() { var derived = new DerivedClass <int> { HiddenProperty = 123, }; var baseClass = (BaseClass)derived; baseClass.HiddenProperty = 456; var exception = new HiddenException("test", derived); var propertiesBag = new ExceptionPropertiesBag(exception); CreateReflectionBasedDestructurer().Destructure(exception, propertiesBag, EmptyDestructurer()); var properties = propertiesBag.GetResultDictionary(); var info = properties[nameof(HiddenException.Info)] as IDictionary <string, object>; Assert.Equal(derived.HiddenProperty, info[nameof(DerivedClass <object> .HiddenProperty)]); Assert.Equal(baseClass.HiddenProperty, info[$"{typeof(BaseClass).FullName}.{nameof(BaseClass.HiddenProperty)}"]); }
public void CanDestructureUriDataItem() { const string uriValue = "http://localhost/data-item"; var exception = new Exception("test") { Data = { { "UriDataItem", new Uri(uriValue) }, }, }; var propertiesBag = new ExceptionPropertiesBag(exception); CreateReflectionBasedDestructurer().Destructure(exception, propertiesBag, EmptyDestructurer()); var properties = propertiesBag.GetResultDictionary(); var data = (IDictionary)properties[nameof(Exception.Data)]; var uriDataValue = data["UriDataItem"]; Assert.IsType <string>(uriDataValue); Assert.Equal(uriValue, uriDataValue); }
public void CanDestructureStructDataItem() { // Arrange var exception = new Exception("test"); exception.Data["data"] = new TestStruct() { ValueType = 10, ReferenceType = "ABC", }; var propertiesBag = new ExceptionPropertiesBag(exception); // Act CreateReflectionBasedDestructurer().Destructure(exception, propertiesBag, EmptyDestructurer()); // Assert var properties = propertiesBag.GetResultDictionary(); var data = (IDictionary)properties[nameof(Exception.Data)]; var testStructDataValue = data["data"]; Assert.IsAssignableFrom <TestStruct>(testStructDataValue); }
public void CanDestructureTask() { Task task = new TaskFactory <int>().StartNew(() => 12, TaskCreationOptions.LongRunning | TaskCreationOptions.PreferFairness); var exception = new TaskCanceledException(task); var propertiesBag = new ExceptionPropertiesBag(exception); CreateReflectionBasedDestructurer().Destructure(exception, propertiesBag, EmptyDestructurer()); var properties = propertiesBag.GetResultDictionary(); var destructuredTaskObject = (IDictionary)properties[nameof(TaskCanceledException.Task)]; var destructuredTaskProperties = Assert.IsAssignableFrom <IDictionary <string, object> >(destructuredTaskObject); destructuredTaskProperties.Should().ContainKey(nameof(Task.Id)); destructuredTaskProperties.Should().ContainKey(nameof(Task.Status)) .WhichValue.Should().BeOfType <string>() .Which.Should().Be(nameof(TaskStatus.RanToCompletion)); destructuredTaskProperties.Should().ContainKey(nameof(Task.CreationOptions)) .WhichValue.Should().BeOfType <string>() .Which.Should().Contain(nameof(TaskCreationOptions.LongRunning)) .And.Contain(nameof(TaskCreationOptions.PreferFairness)); }
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); }
public void CanDestructureClassDataItem() { // Arrange var exception = new Exception("test"); exception.Data["data"] = new TestClass() { ValueType = 10, ReferenceType = "ABC", }; var propertiesBag = new ExceptionPropertiesBag(exception); // Act CreateReflectionBasedDestructurer().Destructure(exception, propertiesBag, EmptyDestructurer()); // Assert var properties = propertiesBag.GetResultDictionary(); var data = (IDictionary)properties[nameof(Exception.Data)]; var testStructDataValue = data["data"]; var destructuredStructDictionary = Assert.IsAssignableFrom <IDictionary <string, object> >(testStructDataValue); Assert.Equal(10, destructuredStructDictionary[nameof(TestClass.ValueType)]); Assert.Equal("ABC", destructuredStructDictionary[nameof(TestClass.ReferenceType)]); }
public void CanDestructureFaultedTask() { var taskException = new Exception("INNER EXCEPTION MESSAGE"); var task = Task.FromException(taskException); var exception = new TaskException("TASK EXCEPTION MESSAGE", task); var propertiesBag = new ExceptionPropertiesBag(exception); CreateReflectionBasedDestructurer().Destructure(exception, propertiesBag, InnerDestructurer(CreateReflectionBasedDestructurer())); var properties = propertiesBag.GetResultDictionary(); var destructuredTaskObject = (IDictionary)properties[nameof(TaskCanceledException.Task)]; var destructuredTaskProperties = Assert.IsAssignableFrom <IDictionary <string, object> >(destructuredTaskObject); destructuredTaskProperties.Should().ContainKey(nameof(Task.Id)); destructuredTaskProperties.Should().ContainKey(nameof(Task.Status)) .WhichValue.Should().BeOfType <string>() .Which.Should().Be(nameof(TaskStatus.Faulted)); destructuredTaskProperties.Should().ContainKey(nameof(Task.CreationOptions)) .WhichValue.Should().BeOfType <string>() .Which.Should().Be(nameof(TaskCreationOptions.None)); var taskFirstLevelExceptionDictionary = destructuredTaskProperties.Should().ContainKey(nameof(Task.Exception)) .WhichValue.Should().BeAssignableTo <IDictionary <string, object> >() .Which; taskFirstLevelExceptionDictionary.Should().ContainKey("Message") .WhichValue.Should().BeOfType <string>() .Which.Should().Contain("One or more errors occurred.", "task's first level exception is aggregate exception"); taskFirstLevelExceptionDictionary.Should().ContainKey("InnerExceptions") .WhichValue.Should().BeAssignableTo <IReadOnlyCollection <object> >() .Which.Should().ContainSingle() .Which.Should().BeAssignableTo <IDictionary <string, object> >() .Which.Should().ContainKey("Message") .WhichValue.Should().BeOfType <string>() .Which.Should().Be("INNER EXCEPTION MESSAGE"); }