Skip to content

Commit

Permalink
Unify tests (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
sungam3r authored Jan 28, 2024
1 parent 170901f commit e89977b
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 418 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class PersonalData
public string? Name { get; set; }
}
```
<sup><a href='/src/Destructurama.Attributed.Tests/LogWithNameAttributedTests.cs#L36-L42' title='Snippet source file'>snippet source</a> | <a href='#snippet-logwithname' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Destructurama.Attributed.Tests/LogWithNameAttributeTests.cs#L28-L34' title='Snippet source file'>snippet source</a> | <a href='#snippet-logwithname' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## 2. Ignoring a property
Expand Down Expand Up @@ -278,7 +278,7 @@ public class CustomizedMaskedLogs
public string? ShowFirstAndLastThreeAndCustomMaskInTheMiddlePreservedLengthIgnored { get; set; }
}
```
<sup><a href='/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs#L9-L128' title='Snippet source file'>snippet source</a> | <a href='#snippet-customizedmaskedlogs' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs#L8-L127' title='Snippet source file'>snippet source</a> | <a href='#snippet-customizedmaskedlogs' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## 7. Masking a string property with regular expressions
Expand Down
22 changes: 3 additions & 19 deletions src/Destructurama.Attributed.Tests/AttributedDestructuringTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Destructurama.Attributed.Tests.Support;
using NUnit.Framework;
using Serilog;
using Serilog.Events;
using Shouldly;

Expand All @@ -12,17 +11,9 @@ public class AttributedDestructuringTests
[Test]
public void Throwing_Accessor_Should_Be_Handled()
{
// Setup
LogEvent evt = null!;
var customized = new ClassWithThrowingAccessor();

var log = new LoggerConfiguration()
.Destructure.UsingAttributes()
.WriteTo.Sink(new DelegatingSink(e => evt = e))
.CreateLogger();
var obj = new ClassWithThrowingAccessor();

// Execute
log.Information("Here is {@Customized}", obj);
var evt = DelegatingSink.Execute(customized);

// Verify
var sv = (StructureValue)evt.Properties["Customized"];
Expand All @@ -34,13 +25,6 @@ public void Throwing_Accessor_Should_Be_Handled()
[Test]
public void AttributesAreConsultedWhenDestructuring()
{
LogEvent evt = null!;

var log = new LoggerConfiguration()
.Destructure.UsingAttributes()
.WriteTo.Sink(new DelegatingSink(e => evt = e))
.CreateLogger();

var customized = new Customized
{
ImmutableScalar = new(),
Expand All @@ -55,7 +39,7 @@ public void AttributesAreConsultedWhenDestructuring()
}
};

log.Information("Here is {@Customized}", customized);
var evt = DelegatingSink.Execute(customized);

var sv = (StructureValue)evt.Properties["Customized"];
var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value);
Expand Down
80 changes: 11 additions & 69 deletions src/Destructurama.Attributed.Tests/IgnoreNullPropertiesTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections;
using Destructurama.Attributed.Tests.Support;
using NUnit.Framework;
using Serilog;
using Serilog.Events;
using Shouldly;

Expand Down Expand Up @@ -111,8 +110,8 @@ IEnumerator IEnumerable.GetEnumerator()
}

/// <summary>
/// At least one attribute from Destructurma.Attributed is enough to ignore all default properties on IEnumerable,
/// when IgnoreNullProperties is true.
/// At least one attribute from Destructurma.Attributed is enough to ignore all
/// default properties on IEnumerable, when IgnoreNullProperties is true.
/// </summary>
private class CustomEnumerableAttributed : IEnumerable<int>
{
Expand All @@ -130,8 +129,7 @@ public IEnumerator<int> GetEnumerator()
yield return 1;
}

IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

[SetUp]
Expand All @@ -149,16 +147,9 @@ public void TearDown()
[Test]
public void NotLoggedIfNull_Uninitialized()
{
LogEvent? evt = null;

var log = new LoggerConfiguration()
.Destructure.UsingAttributes(x => x.IgnoreNullProperties = true)
.WriteTo.Sink(new DelegatingSink(e => evt = e))
.CreateLogger();

var customized = new NotLoggedIfNull();

log.Information("Here is {@Customized}", customized);
var evt = DelegatingSink.Execute(customized, configure: x => x.IgnoreNullProperties = true);

var sv = (StructureValue)evt!.Properties["Customized"];
var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value);
Expand All @@ -179,13 +170,6 @@ public void NotLoggedIfNull_Uninitialized()
[Test]
public void NotLoggedIfNull_Initialized()
{
LogEvent? evt = null;

var log = new LoggerConfiguration()
.Destructure.UsingAttributes(x => x.IgnoreNullProperties = true)
.WriteTo.Sink(new DelegatingSink(e => evt = e))
.CreateLogger();

var dateTime = DateTime.UtcNow;
var theStruct = new NotLoggedIfNullStruct
{
Expand Down Expand Up @@ -219,7 +203,7 @@ public void NotLoggedIfNull_Initialized()
StructPartiallyInitialized = theStructPartiallyUnitialized,
};

log.Information("Here is {@Customized}", customized);
var evt = DelegatingSink.Execute(customized, configure: x => x.IgnoreNullProperties = true);

var sv = (StructureValue)evt!.Properties["Customized"];
var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value);
Expand Down Expand Up @@ -275,16 +259,9 @@ public void NotLoggedIfNull_Initialized()
[Test]
public void WithMask_NotLoggedIfNull_Uninitialized()
{
LogEvent? evt = null;

var log = new LoggerConfiguration()
.Destructure.UsingAttributes(x => x.IgnoreNullProperties = true)
.WriteTo.Sink(new DelegatingSink(e => evt = e))
.CreateLogger();

var customized = new AttributedWithMask();

log.Information("Here is {@Customized}", customized);
var evt = DelegatingSink.Execute(customized, configure: x => x.IgnoreNullProperties = true);

var sv = (StructureValue)evt!.Properties["Customized"];
var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value);
Expand All @@ -296,20 +273,13 @@ public void WithMask_NotLoggedIfNull_Uninitialized()
[Test]
public void WithMask_NotLoggedIfNull_Initialized()
{
LogEvent? evt = null;

var log = new LoggerConfiguration()
.Destructure.UsingAttributes(x => x.IgnoreNullProperties = true)
.WriteTo.Sink(new DelegatingSink(e => evt = e))
.CreateLogger();

var customized = new AttributedWithMask
{
String = "Foo[Masked]",
Object = "Bar[Masked]",
};

log.Information("Here is {@Customized}", customized);
var evt = DelegatingSink.Execute(customized, configure: x => x.IgnoreNullProperties = true);

var sv = (StructureValue)evt!.Properties["Customized"];
var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value);
Expand All @@ -324,13 +294,6 @@ public void WithMask_NotLoggedIfNull_Initialized()
[Test]
public void EnumerableIgnored()
{
LogEvent? evt = null;

var log = new LoggerConfiguration()
.Destructure.UsingAttributes(x => x.IgnoreNullProperties = true)
.WriteTo.Sink(new DelegatingSink(e => evt = e))
.CreateLogger();

var customized = new CustomEnumerableDestructionIgnored()
{
Integer = 0,
Expand All @@ -340,7 +303,7 @@ public void EnumerableIgnored()
}
};

log.Information("Here is {@Customized}", customized);
var evt = DelegatingSink.Execute(customized, configure: x => x.IgnoreNullProperties = true);

var sv = evt!.Properties["Customized"];
sv.ShouldBeOfType<SequenceValue>();
Expand All @@ -349,13 +312,6 @@ public void EnumerableIgnored()
[Test]
public void EnumerableDestructedAsStruct()
{
LogEvent? evt = null;

var log = new LoggerConfiguration()
.Destructure.UsingAttributes(x => x.IgnoreNullProperties = true)
.WriteTo.Sink(new DelegatingSink(e => evt = e))
.CreateLogger();

var customized = new CustomEnumerableAttributed
{
Integer = 0,
Expand All @@ -366,7 +322,7 @@ public void EnumerableDestructedAsStruct()
},
};

log.Information("Here is {@Customized}", customized);
var evt = DelegatingSink.Execute(customized, configure: x => x.IgnoreNullProperties = true);

var sv = (StructureValue)evt!.Properties["Customized"];
var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value);
Expand All @@ -384,16 +340,9 @@ public void EnumerableDestructedAsStruct()
[Test]
public void NotLoggedIfNullAttribute_Uninitialized()
{
LogEvent? evt = null;

var log = new LoggerConfiguration()
.Destructure.UsingAttributes(x => x.IgnoreNullProperties = false)
.WriteTo.Sink(new DelegatingSink(e => evt = e))
.CreateLogger();

var customized = new NotLoggedIfNullAttributed();

log.Information("Here is {@Customized}", customized);
var evt = DelegatingSink.Execute(customized, configure: x => x.IgnoreNullProperties = false);

var sv = (StructureValue)evt!.Properties["Customized"];
var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value);
Expand All @@ -414,13 +363,6 @@ public void NotLoggedIfNullAttribute_Uninitialized()
[Test]
public void NotLoggedIfNullAttribute_Initialized()
{
LogEvent? evt = null;

var log = new LoggerConfiguration()
.Destructure.UsingAttributes(x => x.IgnoreNullProperties = false)
.WriteTo.Sink(new DelegatingSink(e => evt = e))
.CreateLogger();

var dateTime = DateTime.UtcNow;
var theStruct = new NotLoggedIfNullStruct
{
Expand Down Expand Up @@ -454,7 +396,7 @@ public void NotLoggedIfNullAttribute_Initialized()
StructPartiallyInitialized = theStructPartiallyUnitialized,
};

log.Information("Here is {@Customized}", customized);
var evt = DelegatingSink.Execute(customized, configure: x => x.IgnoreNullProperties = false);

var sv = (StructureValue)evt!.Properties["Customized"];
var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
using Destructurama.Attributed.Tests.Support;
using NUnit.Framework;
using Serilog;
using Serilog.Events;
using Shouldly;

namespace Destructurama.Attributed.Tests;

[TestFixture]
public class LogWithNameAttributedTests
public class LogWithNameAttributeTests
{
[Test]
public void AttributesAreConsultedWhenDestructuring()
{
LogEvent evt = null!;

var log = new LoggerConfiguration()
.Destructure.UsingAttributes()
.WriteTo.Sink(new DelegatingSink(e => evt = e))
.CreateLogger();

var personalData = new PersonalData
var customized = new PersonalData
{
Name = "John Doe"
};

log.Information("Here is {@PersonData}", personalData);
var evt = DelegatingSink.Execute(customized);

var sv = (StructureValue)evt.Properties["PersonData"];
var sv = (StructureValue)evt.Properties["Customized"];
var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value);

var literalValue = props["FullName"].LiteralValue();
Expand Down
Loading

0 comments on commit e89977b

Please sign in to comment.