-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathLogWithNameAttributeTests.cs
71 lines (60 loc) · 1.96 KB
/
LogWithNameAttributeTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using Destructurama.Attributed.Tests.Support;
using NUnit.Framework;
using Serilog.Events;
using Shouldly;
namespace Destructurama.Attributed.Tests;
[TestFixture]
public class LogWithNameAttributeTests
{
[TestCase("John Doe")]
[TestCase(null)]
public void AttributesAreConsultedWhenDestructuring(string name)
{
var customized = new PersonalData
{
Name = name
};
var evt = DelegatingSink.Execute(customized);
var sv = (StructureValue)evt.Properties["Customized"];
var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value);
var literalValue = props["FullName"].LiteralValue();
literalValue.ShouldBe(name);
}
// https://github.com/destructurama/attributed/issues/65
[Test]
public void Issue65()
{
var customized = new MessageBase
{
Context = new ContextClass(),
};
var evt = DelegatingSink.Execute(customized);
var sv = (StructureValue)evt.Properties["Customized"];
sv.Properties.Count.ShouldBe(1);
sv.Properties[0].Name.ShouldBe("messageContext");
var sv2 = sv.Properties[0].Value.ShouldBeOfType<StructureValue>();
sv2.Properties.Count.ShouldBe(2);
sv2.Properties[0].Name.ShouldBe("Foo");
sv2.Properties[1].Name.ShouldBe("Bar");
sv2.Properties[0].Value.LiteralValue().ShouldBe("MyFoo");
sv2.Properties[1].Value.LiteralValue().ShouldBe("MyBar");
}
public class MessageBase
{
[LogWithName("messageContext")]
public ContextClass? Context { get; set; }
}
public class ContextClass
{
public string Foo { get; set; } = "MyFoo";
public string Bar { get; set; } = "MyBar";
public override string ToString() => "ContextClass ToString Output";
}
#region LogWithName
public class PersonalData
{
[LogWithName("FullName")]
public string? Name { get; set; }
}
#endregion
}