Skip to content

Commit

Permalink
Add approval tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sungam3r committed Jan 21, 2024
1 parent 35783c5 commit 1c398b7
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 20 deletions.
39 changes: 39 additions & 0 deletions src/Destructurama.Attributed.Tests/Approval/ApiApprovalTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2017 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using NUnit.Framework;
using PublicApiGenerator;
using Shouldly;

namespace Destructurama.Attributed.Tests;

/// <summary>Tests for checking changes to the public API.</summary>
[TestFixture]
public class ApiApprovalTests
{
/// <summary>Check for changes to the public APIs.</summary>
/// <param name="type">The type used as a marker for the assembly whose public API change you want to check.</param>
[TestCase(typeof(LoggerConfigurationAppSettingsExtensions))]
public void PublicApi_Should_Not_Change_Unintentionally(Type type)
{
string publicApi = type.Assembly.GeneratePublicApi(new()
{
IncludeAssemblyAttributes = false,
AllowNamespacePrefixes = ["System", "Microsoft.Extensions.DependencyInjection"],
ExcludeAttributes = ["System.Diagnostics.DebuggerDisplayAttribute"],
});

publicApi.ShouldMatchApproved(options => options.NoDiff().WithFilenameGenerator((testMethodInfo, discriminator, fileType, fileExtension) => $"{type.Assembly.GetName().Name!}.{fileType}.{fileExtension}"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
namespace Destructurama.Attributed
{
public class AttributedDestructuringPolicyOptions
{
public AttributedDestructuringPolicyOptions() { }
public bool IgnoreNullProperties { get; set; }
}
public interface IPropertyDestructuringAttribute
{
bool TryCreateLogEventProperty(string name, object? value, Serilog.Core.ILogEventPropertyValueFactory propertyValueFactory, [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out Serilog.Events.LogEventProperty? property);
}
public interface IPropertyOptionalIgnoreAttribute
{
bool ShouldPropertyBeIgnored(string name, object? value, System.Type type);
}
public interface ITypeDestructuringAttribute
{
Serilog.Events.LogEventPropertyValue CreateLogEventPropertyValue(object? value, Serilog.Core.ILogEventPropertyValueFactory propertyValueFactory);
}
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Property)]
public class LogAsScalarAttribute : System.Attribute, Destructurama.Attributed.IPropertyDestructuringAttribute, Destructurama.Attributed.ITypeDestructuringAttribute
{
public LogAsScalarAttribute(bool isMutable = false) { }
public Serilog.Events.LogEventPropertyValue CreateLogEventPropertyValue(object? value, Serilog.Core.ILogEventPropertyValueFactory propertyValueFactory) { }
public bool TryCreateLogEventProperty(string name, object? value, Serilog.Core.ILogEventPropertyValueFactory propertyValueFactory, out Serilog.Events.LogEventProperty property) { }
}
[System.AttributeUsage(System.AttributeTargets.Property)]
public class LogMaskedAttribute : System.Attribute, Destructurama.Attributed.IPropertyDestructuringAttribute
{
public LogMaskedAttribute() { }
public bool PreserveLength { get; set; }
public int ShowFirst { get; set; }
public int ShowLast { get; set; }
public string Text { get; set; }
public bool TryCreateLogEventProperty(string name, object? value, Serilog.Core.ILogEventPropertyValueFactory propertyValueFactory, out Serilog.Events.LogEventProperty property) { }
}
[System.AttributeUsage(System.AttributeTargets.Property)]
public class LogReplacedAttribute : System.Attribute, Destructurama.Attributed.IPropertyDestructuringAttribute
{
public LogReplacedAttribute(string pattern, string replacement) { }
public System.Text.RegularExpressions.RegexOptions Options { get; set; }
public System.TimeSpan Timeout { get; set; }
public bool TryCreateLogEventProperty(string name, object? value, Serilog.Core.ILogEventPropertyValueFactory propertyValueFactory, [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out Serilog.Events.LogEventProperty? property) { }
}
[System.AttributeUsage(System.AttributeTargets.Property)]
public class LogWithNameAttribute : System.Attribute, Destructurama.Attributed.IPropertyDestructuringAttribute
{
public LogWithNameAttribute(string newName) { }
public bool TryCreateLogEventProperty(string name, object? value, Serilog.Core.ILogEventPropertyValueFactory propertyValueFactory, [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out Serilog.Events.LogEventProperty? property) { }
}
[System.AttributeUsage(System.AttributeTargets.Property)]
public class NotLoggedAttribute : System.Attribute, Destructurama.Attributed.IPropertyDestructuringAttribute
{
public NotLoggedAttribute() { }
public bool TryCreateLogEventProperty(string name, object? value, Serilog.Core.ILogEventPropertyValueFactory propertyValueFactory, [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out Serilog.Events.LogEventProperty? property) { }
}
[System.AttributeUsage(System.AttributeTargets.Property)]
public class NotLoggedIfDefaultAttribute : System.Attribute, Destructurama.Attributed.IPropertyOptionalIgnoreAttribute
{
public NotLoggedIfDefaultAttribute() { }
}
[System.AttributeUsage(System.AttributeTargets.Property)]
public class NotLoggedIfNullAttribute : System.Attribute, Destructurama.Attributed.IPropertyOptionalIgnoreAttribute
{
public NotLoggedIfNullAttribute() { }
}
}
namespace Destructurama
{
public static class LoggerConfigurationAppSettingsExtensions
{
public static Serilog.LoggerConfiguration UsingAttributes(this Serilog.Configuration.LoggerDestructuringConfiguration configuration) { }
public static Serilog.LoggerConfiguration UsingAttributes(this Serilog.Configuration.LoggerDestructuringConfiguration configuration, System.Action<Destructurama.Attributed.AttributedDestructuringPolicyOptions> configure) { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Serilog" Version="3.0.1" />
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="PublicApiGenerator" Version="11.1.0" />
<!--<PackageReference Include="MarkdownSnippets.MsBuild" Version="24.5.1" PrivateAssets="all" />-->
</ItemGroup>

Expand Down
19 changes: 2 additions & 17 deletions src/Destructurama.Attributed.Tests/Support/DelegatingSink.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using Serilog;
using Serilog.Core;
using Serilog.Events;

namespace Destructurama.Attributed.Tests.Support;

public class DelegatingSink : ILogEventSink
internal sealed class DelegatingSink : ILogEventSink
{
private readonly Action<LogEvent> _write;

Expand All @@ -13,19 +12,5 @@ public DelegatingSink(Action<LogEvent> write)
_write = write ?? throw new ArgumentNullException("write");
}

public void Emit(LogEvent logEvent)
{
_write(logEvent);
}

public static LogEvent GetLogEvent(Action<ILogger> writeAction)
{
LogEvent result = null!;
var l = new LoggerConfiguration()
.WriteTo.Sink(new DelegatingSink(le => result = le))
.CreateLogger();

writeAction(l);
return result;
}
public void Emit(LogEvent logEvent) => _write(logEvent);
}
6 changes: 3 additions & 3 deletions src/Destructurama.Attributed.Tests/Support/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Destructurama.Attributed.Tests.Support;

public static class Extensions
internal static class Extensions
{
public static object? LiteralValue(this LogEventPropertyValue @this) =>
((ScalarValue)@this).Value;
public static object? LiteralValue(this LogEventPropertyValue @this)
=> ((ScalarValue)@this).Value;
}

0 comments on commit 1c398b7

Please sign in to comment.