Skip to content

Commit 167c5b8

Browse files
authored
Add tests for private, indexer and set-only properties (#102)
1 parent e89977b commit 167c5b8

File tree

3 files changed

+78
-44
lines changed

3 files changed

+78
-44
lines changed

src/Destructurama.Attributed.Tests/AttributedDestructuringTests.cs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,45 @@ public void Throwing_Accessor_Should_Be_Handled()
1515

1616
var evt = DelegatingSink.Execute(customized);
1717

18-
// Verify
1918
var sv = (StructureValue)evt.Properties["Customized"];
2019
sv.Properties.Count.ShouldBe(1);
2120
sv.Properties[0].Name.ShouldBe("BadProperty");
2221
sv.Properties[0].Value.ShouldBeOfType<ScalarValue>().Value.ShouldBe("***");
2322
}
2423

24+
[Test]
25+
public void Only_Settable_Accessor_Should_Be_Handled()
26+
{
27+
var customized = new ClassWithOnlySetters();
28+
29+
var evt = DelegatingSink.Execute(customized);
30+
31+
var sv = (StructureValue)evt.Properties["Customized"];
32+
sv.Properties.Count.ShouldBe(0);
33+
}
34+
35+
[Test]
36+
public void Private_Property_Should_Be_Handled()
37+
{
38+
var customized = new ClassWithPrivateProperty();
39+
40+
var evt = DelegatingSink.Execute(customized);
41+
42+
var sv = (StructureValue)evt.Properties["Customized"];
43+
sv.Properties.Count.ShouldBe(0);
44+
}
45+
46+
[Test]
47+
public void Indexer_Should_Be_Handled()
48+
{
49+
var customized = new ClassWithIndexer();
50+
51+
var evt = DelegatingSink.Execute(customized);
52+
53+
var sv = (StructureValue)evt.Properties["Customized"];
54+
sv.Properties.Count.ShouldBe(0);
55+
}
56+
2557
[Test]
2658
public void AttributesAreConsultedWhenDestructuring()
2759
{
@@ -63,6 +95,31 @@ public class ClassWithThrowingAccessor
6395
public string? BadProperty => throw new FormatException("oops");
6496
}
6597

98+
public class ClassWithOnlySetters
99+
{
100+
[LogMasked]
101+
public string? Name { set { } }
102+
103+
[LogAsScalar]
104+
public Struct1 Struct1 { set { } }
105+
}
106+
107+
public class ClassWithPrivateProperty
108+
{
109+
[LogMasked]
110+
private string? Name { get; set; } = "Tom";
111+
}
112+
113+
public class ClassWithIndexer
114+
{
115+
[LogMasked]
116+
public string? this[int index]
117+
{
118+
get => "Tom";
119+
set { }
120+
}
121+
}
122+
66123
[LogAsScalar]
67124
public class ImmutableScalar
68125
{

src/Destructurama.Attributed/Attributed/AttributedDestructuringPolicy.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,32 @@ public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyV
4747
return cached.CanDestructure;
4848
}
4949

50+
private static IEnumerable<PropertyInfo> GetPropertiesRecursive(Type type)
51+
{
52+
var seenNames = new HashSet<string>();
53+
54+
while (type != typeof(object))
55+
{
56+
var unseenProperties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
57+
.Where(p => p.CanRead && p.GetMethod.IsPublic && p.GetIndexParameters().Length == 0 && !seenNames.Contains(p.Name));
58+
59+
foreach (var propertyInfo in unseenProperties)
60+
{
61+
seenNames.Add(propertyInfo.Name);
62+
yield return propertyInfo;
63+
}
64+
65+
type = type.BaseType;
66+
}
67+
}
68+
5069
private CacheEntry CreateCacheEntry(Type type)
5170
{
5271
var classDestructurer = type.GetCustomAttribute<ITypeDestructuringAttribute>();
5372
if (classDestructurer != null)
5473
return new(classDestructurer.CreateLogEventPropertyValue);
5574

56-
var properties = type.GetPropertiesRecursive().ToList();
75+
var properties = GetPropertiesRecursive(type).ToList();
5776
if (!_options.IgnoreNullProperties && properties.All(pi =>
5877
pi.GetCustomAttribute<IPropertyDestructuringAttribute>() == null
5978
&& pi.GetCustomAttribute<IPropertyOptionalIgnoreAttribute>() == null))

src/Destructurama.Attributed/Util/GetablePropertyFinder.cs

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)