From acd840a5a4a022583bf931b926d147f07bf23721 Mon Sep 17 00:00:00 2001 From: Ivan Maximov Date: Sun, 15 Dec 2024 17:34:31 +0300 Subject: [PATCH] tests --- .../Tests/DestructureByIgnoringTests.cs | 100 +++++++++++++++++- 1 file changed, 95 insertions(+), 5 deletions(-) diff --git a/src/Destructurama.ByIgnoring.Tests/Tests/DestructureByIgnoringTests.cs b/src/Destructurama.ByIgnoring.Tests/Tests/DestructureByIgnoringTests.cs index 834c5d0..d9bbe82 100644 --- a/src/Destructurama.ByIgnoring.Tests/Tests/DestructureByIgnoringTests.cs +++ b/src/Destructurama.ByIgnoring.Tests/Tests/DestructureByIgnoringTests.cs @@ -112,6 +112,28 @@ public void TryDestructure_Should_Work_For_Structs() sv.Properties[0].Value.ShouldBeOfType().Value.ShouldBe("Tom"); } + [Test] + public void TryDestructure_Should_Ignore_Property_From_Options() + { + // Setup + LogEvent evt = null!; + + var log = new LoggerConfiguration() + .Destructure.ByIgnoring(o => o.Ignore(x => x.Id)) + .WriteTo.Sink(new DelegatingSink(e => evt = e)) + .CreateLogger(); + var obj = new DestructureMeClass { Id = 42 }; + + // Execute + log.Information("Here is {@Ignored}", obj); + + // Verify + var sv = (StructureValue)evt.Properties["Ignored"]; + sv.Properties.Count.ShouldBe(1); + sv.Properties[0].Name.ShouldBe("Name"); + sv.Properties[0].Value.ShouldBeOfType().Value.ShouldBe("Tom"); + } + [Test] public void TryDestructure_Should_Ignore_Null_String() { @@ -119,10 +141,10 @@ public void TryDestructure_Should_Ignore_Null_String() LogEvent evt = null!; var log = new LoggerConfiguration() - .Destructure.ByIgnoring(o => o.IgnoreValue((_, v) => v is null)) + .Destructure.ByIgnoring(o => o.IgnoreValue((_, v) => v is null)) .WriteTo.Sink(new DelegatingSink(e => evt = e)) .CreateLogger(); - var obj = new DestructureMeStruct { Name = null }; + var obj = new DestructureMeClass { Name = null }; // Execute log.Information("Here is {@Ignored}", obj); @@ -141,10 +163,10 @@ public void TryDestructure_Should_Ignore_Custom_Value() LogEvent evt = null!; var log = new LoggerConfiguration() - .Destructure.ByIgnoring(o => o.IgnoreValue((p, v) => p.Name is "Id" && v is 42)) + .Destructure.ByIgnoring(o => o.IgnoreValue((p, v) => p.Name is "Id" && v is 42)) .WriteTo.Sink(new DelegatingSink(e => evt = e)) .CreateLogger(); - var obj = new DestructureMeStruct { Id = 42 }; + var obj = new DestructureMeClass { Id = 42 }; // Execute log.Information("Here is {@Ignored}", obj); @@ -156,7 +178,64 @@ public void TryDestructure_Should_Ignore_Custom_Value() sv.Properties[0].Value.ShouldBeOfType().Value.ShouldBe("Tom"); } - public struct DestructureMeStruct + [Test] + public void TryDestructure_Should_Ignore_All_AssignableTo() + { + // Setup + LogEvent evt = null!; + + var log = new LoggerConfiguration() + .Destructure.ByIgnoring(o => o.IgnoreValue((_, v) => v is null).DestructureAssignableTo()) + .WriteTo.Sink(new DelegatingSink(e => evt = e)) + .CreateLogger(); + var obj1 = new DestructureMeStruct { Name = null }; + var obj2 = new DestructureMeClass { Name = null }; + + // Execute + log.Information("Here is {@Ignored1} and {@Ignored2}", obj1, obj2); + + // Verify + var sv = (StructureValue)evt.Properties["Ignored1"]; + sv.Properties.Count.ShouldBe(1); + sv.Properties[0].Name.ShouldBe("Id"); + sv.Properties[0].Value.ShouldBeOfType().Value.ShouldBe(0); + sv = (StructureValue)evt.Properties["Ignored2"]; + sv.Properties.Count.ShouldBe(1); + sv.Properties[0].Name.ShouldBe("Id"); + sv.Properties[0].Value.ShouldBeOfType().Value.ShouldBe(0); + } + + [Test] + public void TryDestructure_Should_Ignore_Exact_Type() + { + // Setup + LogEvent evt = null!; + + var log = new LoggerConfiguration() + .Destructure.ByIgnoring(o => o.IgnoreValue((_, v) => v is null).DestructureExactType()) + .WriteTo.Sink(new DelegatingSink(e => evt = e)) + .CreateLogger(); + var obj1 = new DestructureMeStruct { Name = null }; + var obj2 = new DestructureMeClass { Name = null }; + + // Execute + log.Information("Here is {@Ignored1} and {@Ignored2}", obj1, obj2); + + // Verify + var sv = (StructureValue)evt.Properties["Ignored1"]; + sv.Properties.Count.ShouldBe(2); + sv.Properties[0].Name.ShouldBe("Id"); + sv.Properties[0].Value.ShouldBeOfType().Value.ShouldBe(0); + sv.Properties[1].Name.ShouldBe("Name"); + sv.Properties[1].Value.ShouldBeOfType().Value.ShouldBeNull(); + + sv = (StructureValue)evt.Properties["Ignored2"]; + sv.Properties.Count.ShouldBe(1); + sv.Properties[0].Name.ShouldBe("Id"); + sv.Properties[0].Value.ShouldBeOfType().Value.ShouldBe(0); + } + + public struct DestructureMeStruct : IDestructureMe { public DestructureMeStruct() { @@ -166,4 +245,15 @@ public DestructureMeStruct() public string? Name { get; set; } = "Tom"; } + + public interface IDestructureMe + { + } + + public class DestructureMeClass : IDestructureMe + { + public int Id { get; set; } + + public string? Name { get; set; } = "Tom"; + } }