Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: public API verification improvements #121

Merged
merged 3 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
[assembly: System.Reflection.AssemblyMetadata("RepositoryUrl", "https://github.com/skwasjer/MockHttp")]
[assembly: System.Runtime.Versioning.TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName=".NET Standard 2.0")]
namespace MockHttp.Json
{
public interface IJsonAdapter
{
string Serialize(object? value);
}
public static class JsonRequestMatchingExtensions
{
public static MockHttp.RequestMatching JsonBody<T>(this MockHttp.RequestMatching builder, T content) { }
public static MockHttp.RequestMatching JsonBody<T>(this MockHttp.RequestMatching builder, T body, MockHttp.Json.IJsonAdapter? adapter) { }
}
public static class MockConfigurationExtensions
{
public static MockHttp.IMockConfiguration UseJsonAdapter(this MockHttp.IMockConfiguration mockConfig, MockHttp.Json.IJsonAdapter jsonAdapter) { }
}
public static class ResponseBuilderExtensions
{
public static MockHttp.Language.Flow.Response.IWithContentResult JsonBody<T>(this MockHttp.Language.Response.IWithContent builder, System.Func<T> jsonContentFactory, System.Text.Encoding? encoding = null, MockHttp.Json.IJsonAdapter? adapter = null) { }
public static MockHttp.Language.Flow.Response.IWithContentResult JsonBody<T>(this MockHttp.Language.Response.IWithContent builder, T jsonContent, System.Text.Encoding? encoding = null, MockHttp.Json.IJsonAdapter? adapter = null) { }
}
}
namespace MockHttp.Json.Newtonsoft
{
public static class MockConfigurationExtensions
{
public static MockHttp.IMockConfiguration UseNewtonsoftJson(this MockHttp.IMockConfiguration mockConfig, Newtonsoft.Json.JsonSerializerSettings? serializerSettings = null) { }
}
public class NewtonsoftAdapter : MockHttp.Json.IJsonAdapter
{
public NewtonsoftAdapter(Newtonsoft.Json.JsonSerializerSettings? settings = null) { }
public string Serialize(object? value) { }
}
public static class RequestMatchingExtensions
{
public static MockHttp.RequestMatching JsonBody<T>(this MockHttp.RequestMatching builder, T body, Newtonsoft.Json.JsonSerializerSettings? serializerSettings) { }
}
public static class ResponseBuilderExtensions
{
public static MockHttp.Language.Flow.Response.IWithContentResult JsonBody<T>(this MockHttp.Language.Response.IWithContent builder, System.Func<T> jsonContentFactory, System.Text.Encoding? encoding = null, Newtonsoft.Json.JsonSerializerSettings? serializerSettings = null) { }
public static MockHttp.Language.Flow.Response.IWithContentResult JsonBody<T>(this MockHttp.Language.Response.IWithContent builder, T jsonContent, System.Text.Encoding? encoding = null, Newtonsoft.Json.JsonSerializerSettings? serializerSettings = null) { }
}
}
namespace MockHttp.Json.SystemTextJson
{
public static class MockConfigurationExtensions
{
public static MockHttp.IMockConfiguration UseSystemTextJson(this MockHttp.IMockConfiguration mockConfig, System.Text.Json.JsonSerializerOptions? options = null) { }
}
public static class RequestMatchingExtensions
{
public static MockHttp.RequestMatching JsonBody<T>(this MockHttp.RequestMatching builder, T body, System.Text.Json.JsonSerializerOptions? serializerOptions) { }
}
public static class ResponseBuilderExtensions
{
public static MockHttp.Language.Flow.Response.IWithContentResult JsonBody<T>(this MockHttp.Language.Response.IWithContent builder, System.Func<T> jsonContentFactory, System.Text.Encoding? encoding = null, System.Text.Json.JsonSerializerOptions? serializerOptions = null) { }
public static MockHttp.Language.Flow.Response.IWithContentResult JsonBody<T>(this MockHttp.Language.Response.IWithContent builder, T jsonContent, System.Text.Encoding? encoding = null, System.Text.Json.JsonSerializerOptions? serializerOptions = null) { }
}
public sealed class SystemTextJsonAdapter : MockHttp.Json.IJsonAdapter
{
public SystemTextJsonAdapter(System.Text.Json.JsonSerializerOptions? options = null) { }
public string Serialize(object? value) { }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
[assembly: System.Reflection.AssemblyMetadata("RepositoryUrl", "https://github.com/skwasjer/MockHttp")]
[assembly: System.Runtime.Versioning.TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName=".NET Standard 2.1")]
namespace MockHttp.Json
{
public interface IJsonAdapter
{
string Serialize(object? value);
}
public static class JsonRequestMatchingExtensions
{
public static MockHttp.RequestMatching JsonBody<T>(this MockHttp.RequestMatching builder, T content) { }
public static MockHttp.RequestMatching JsonBody<T>(this MockHttp.RequestMatching builder, T body, MockHttp.Json.IJsonAdapter? adapter) { }
}
public static class MockConfigurationExtensions
{
public static MockHttp.IMockConfiguration UseJsonAdapter(this MockHttp.IMockConfiguration mockConfig, MockHttp.Json.IJsonAdapter jsonAdapter) { }
}
public static class ResponseBuilderExtensions
{
public static MockHttp.Language.Flow.Response.IWithContentResult JsonBody<T>(this MockHttp.Language.Response.IWithContent builder, System.Func<T> jsonContentFactory, System.Text.Encoding? encoding = null, MockHttp.Json.IJsonAdapter? adapter = null) { }
public static MockHttp.Language.Flow.Response.IWithContentResult JsonBody<T>(this MockHttp.Language.Response.IWithContent builder, T jsonContent, System.Text.Encoding? encoding = null, MockHttp.Json.IJsonAdapter? adapter = null) { }
}
}
namespace MockHttp.Json.Newtonsoft
{
public static class MockConfigurationExtensions
{
public static MockHttp.IMockConfiguration UseNewtonsoftJson(this MockHttp.IMockConfiguration mockConfig, Newtonsoft.Json.JsonSerializerSettings? serializerSettings = null) { }
}
public class NewtonsoftAdapter : MockHttp.Json.IJsonAdapter
{
public NewtonsoftAdapter(Newtonsoft.Json.JsonSerializerSettings? settings = null) { }
public string Serialize(object? value) { }
}
public static class RequestMatchingExtensions
{
public static MockHttp.RequestMatching JsonBody<T>(this MockHttp.RequestMatching builder, T body, Newtonsoft.Json.JsonSerializerSettings? serializerSettings) { }
}
public static class ResponseBuilderExtensions
{
public static MockHttp.Language.Flow.Response.IWithContentResult JsonBody<T>(this MockHttp.Language.Response.IWithContent builder, System.Func<T> jsonContentFactory, System.Text.Encoding? encoding = null, Newtonsoft.Json.JsonSerializerSettings? serializerSettings = null) { }
public static MockHttp.Language.Flow.Response.IWithContentResult JsonBody<T>(this MockHttp.Language.Response.IWithContent builder, T jsonContent, System.Text.Encoding? encoding = null, Newtonsoft.Json.JsonSerializerSettings? serializerSettings = null) { }
}
}
namespace MockHttp.Json.SystemTextJson
{
public static class MockConfigurationExtensions
{
public static MockHttp.IMockConfiguration UseSystemTextJson(this MockHttp.IMockConfiguration mockConfig, System.Text.Json.JsonSerializerOptions? options = null) { }
}
public static class RequestMatchingExtensions
{
public static MockHttp.RequestMatching JsonBody<T>(this MockHttp.RequestMatching builder, T body, System.Text.Json.JsonSerializerOptions? serializerOptions) { }
}
public static class ResponseBuilderExtensions
{
public static MockHttp.Language.Flow.Response.IWithContentResult JsonBody<T>(this MockHttp.Language.Response.IWithContent builder, System.Func<T> jsonContentFactory, System.Text.Encoding? encoding = null, System.Text.Json.JsonSerializerOptions? serializerOptions = null) { }
public static MockHttp.Language.Flow.Response.IWithContentResult JsonBody<T>(this MockHttp.Language.Response.IWithContent builder, T jsonContent, System.Text.Encoding? encoding = null, System.Text.Json.JsonSerializerOptions? serializerOptions = null) { }
}
public sealed class SystemTextJsonAdapter : MockHttp.Json.IJsonAdapter
{
public SystemTextJsonAdapter(System.Text.Json.JsonSerializerOptions? options = null) { }
public string Serialize(object? value) { }
}
}
7 changes: 5 additions & 2 deletions test/MockHttp.Testing/MockHttp.Testing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
<IsTestProject>false</IsTestProject>

<RootNamespace>MockHttp</RootNamespace>

<VerifyVersion>26.6.0</VerifyVersion>
<VerifyVersion Condition="'$(TargetFramework)'=='net5.0' Or '$(TargetFramework)'=='netcoreapp3.1' Or '$(TargetFramework)'=='net462'">20.8.2</VerifyVersion>
<VerifyVersion Condition="'$(TargetFramework)'=='netcoreapp2.1'">18.1.0</VerifyVersion>
<VerifyPublicApi Condition="'$(VerifyPublicApi)'==''">true</VerifyPublicApi>
<VerifyPublicApi Condition="'$(VerifyPublicApi)'=='true' And '$(TargetFramework)'=='netcoreapp2.1'">false</VerifyPublicApi>
<DefineConstants Condition="'$(VerifyPublicApi)'=='true'">$(DefineConstants);VERIFY_PUBLIC_API</DefineConstants>
</PropertyGroup>

Expand All @@ -18,7 +21,7 @@

<ItemGroup Condition="'$(VerifyPublicApi)'=='true'">
<PackageReference Include="PublicApiGenerator" Version="11.1.0" />
<PackageReference Include="Verify.Xunit" Version="20.8.2" />
<PackageReference Include="Verify.Xunit" Version="$(VerifyVersion)" />
</ItemGroup>

<ItemGroup>
Expand Down
25 changes: 21 additions & 4 deletions test/MockHttp.Testing/Specs/PublicApiSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

namespace MockHttp.Specs;

#if !NET6_0_OR_GREATER && !NET47_OR_GREATER
[UsesVerify]
#endif
public abstract class PublicApiSpec
{
private readonly Type _assemblyMarkerType;
Expand Down Expand Up @@ -40,20 +42,35 @@ public Task Api_has_not_changed()
Assembly sut = _assemblyMarkerType.Assembly;
var settings = new VerifySettings();

string targetFramework = sut.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkDisplayName?.Replace(' ', '_')
?? throw new InvalidOperationException("Framework display name is required.");
settings.UseMethodName(nameof(Api_has_not_changed) + targetFramework);
string sutTargetFramework = GetTargetFramework(sut);
string testTargetFramework = GetTargetFramework(GetType().Assembly);
settings.UseFileName(
sutTargetFramework == testTargetFramework
? sutTargetFramework
: $"{sutTargetFramework}_via_{testTargetFramework}"
);
settings.UseDirectory("PublicApi");

// Act
string publicApi = sut.GeneratePublicApi(_options);

// Assert
// ReSharper disable once ExplicitCallerInfoArgument
#pragma warning disable S3236
return Verify(publicApi, settings, _sourceFile!);
#pragma warning restore S3236
}

private static string GetTargetFramework(Assembly asm)
{
return asm.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkDisplayName?.Replace(' ', '_')
?? throw new InvalidOperationException("Framework display name is required.");
}
}
#else
// Stub for unsupported frameworks
namespace MockHttp.Specs;

// Empty shim for when verification is disabled.
public abstract class PublicApiSpec
{
// ReSharper disable once UnusedParameter.Local
Expand Down
Loading
Loading