Skip to content
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
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ No open-source project is going to be successful without contributions. After we
* The [Pull Request](https://help.github.com/articles/using-pull-requests) is targeted at the `master` branch.
* The code complies with the [Coding Guidelines for C#](https://csharpcodingguidelines.com/).
* The changes are covered by a new or existing set of unit tests which follow the Arrange-Act-Assert syntax such as is used [in this example](https://github.com/fluentassertions/fluentassertions/blob/daaf35b9b59b622c96d0c034e8972a020b2bee55/Tests/FluentAssertions.Shared.Specs/BasicEquivalencySpecs.cs#L33).
* If the contribution changes the public API, the changes needs to be included by running [`AcceptApiChanges.ps1`](https://github.com/fluentassertions/fluentassertions.json/tree/master/AcceptApiChanges.ps1)/[`AcceptApiChanges.sh`](https://github.com/fluentassertions/fluentassertions/tree/master/AcceptApiChanges.sh) or using Rider's [Verify Support](https://plugins.jetbrains.com/plugin/17240-verify-support) plug-in.
* If the contribution changes the public API, the changes needs to be included by running [`AcceptApiChanges.ps1`](https://github.com/fluentassertions/fluentassertions.json/tree/master/AcceptApiChanges.ps1)/[`AcceptApiChanges.sh`](https://github.com/fluentassertions/fluentassertions/tree/master/AcceptApiChanges.sh).
* If the contribution affects the documentation, please update the [**readme.md**](https://github.com/fluentassertions/fluentassertions.json/tree/master/readme.md).
58 changes: 45 additions & 13 deletions Tests/Approval.Tests/ApiApproval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,31 @@
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Text;
using System.Xml.Linq;
using System.Xml.XPath;
using DiffPlex.DiffBuilder;
using DiffPlex.DiffBuilder.Model;
using PublicApiGenerator;
using VerifyTests;
using VerifyTests.DiffPlex;
using VerifyXunit;
using Xunit;

namespace Approval.Tests;

public class ApiApproval
{
static ApiApproval() => VerifyDiffPlex.Initialize(OutputType.Minimal);

[Theory]
[ClassData(typeof(TargetFrameworksTheoryData))]
public Task ApproveApi(string framework)
public void ApproveApi(string framework)
{
var configuration = typeof(ApiApproval).Assembly.GetCustomAttribute<AssemblyConfigurationAttribute>()!.Configuration;
var assemblyFile = CombinedPaths("Src", "FluentAssertions.Json", "bin", configuration, framework, "FluentAssertions.Json.dll");
var assembly = Assembly.LoadFile(assemblyFile);
var publicApi = assembly.GeneratePublicApi(options: null);
var expected = GetExpected(publicApi, framework);

var diff = InlineDiffBuilder.Diff(expected, publicApi);

return Verifier
.Verify(publicApi)
.ScrubLinesContaining("FrameworkDisplayName")
.UseDirectory(Path.Combine("ApprovedApi", "FluentAssertions.Json"))
.UseFileName(framework)
.DisableDiff();
Assert.False(diff.HasDifferences, StringifyDiff(diff));
}

private class TargetFrameworksTheoryData : TheoryData<string>
Expand All @@ -50,4 +45,41 @@ private static string GetSolutionDirectory([CallerFilePath] string path = "") =>

private static string CombinedPaths(params string[] paths) =>
Path.GetFullPath(Path.Combine(paths.Prepend(GetSolutionDirectory()).ToArray()));

private string GetExpected(string publicApi, string targetFramework, [CallerFilePath] string path = "")
{
var projectPath = Path.Combine(Path.GetDirectoryName(path)!, "ApprovedApi", "FluentAssertions.Json");
var receivedPath = Path.Combine(projectPath, $"{targetFramework}.received.txt");
var verifiedPath = Path.Combine(projectPath, $"{targetFramework}.verified.txt");

File.WriteAllText(receivedPath, publicApi);
return File.Exists(verifiedPath) ? File.ReadAllText(verifiedPath) : string.Empty;
}

private static string StringifyDiff(DiffPaneModel diff)
{
var builder = new StringBuilder();
if (diff.HasDifferences)
{
foreach (var line in diff.Lines)
{
switch (line.Type)
{
case ChangeType.Inserted:
builder.Append("+ ");
break;
case ChangeType.Deleted:
builder.Append("- ");
break;
default:
// omit unchanged files
continue;
}

builder.AppendLine(line.Text);
}
}

return builder.ToString();
}
}
3 changes: 1 addition & 2 deletions Tests/Approval.Tests/Approval.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="DiffPlex" Version="1.9.0" />
<PackageReference Include="FluentAssertions" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
<PackageReference Include="xunit" Version="2.9.3" />
Expand All @@ -13,8 +14,6 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="PublicApiGenerator" Version="11.5.4" />
<PackageReference Include="Verify.DiffPlex" Version="3.1.2" />
<PackageReference Include="Verify.Xunit" Version="31.12.5" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[assembly: System.Reflection.AssemblyMetadata("RepositoryUrl", "https://github.com/fluentassertions/fluentassertions.json.git")]
[assembly: System.Reflection.AssemblyMetadata("RepositoryUrl", "https://github.com/fluentassertions/fluentassertions.json.git")]
[assembly: System.Runtime.Versioning.TargetFramework(".NETFramework,Version=v4.7", FrameworkDisplayName=".NET Framework 4.7")]
namespace FluentAssertions.Json
{
public interface IJsonAssertionOptions<T>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[assembly: System.Reflection.AssemblyMetadata("RepositoryUrl", "https://github.com/fluentassertions/fluentassertions.json.git")]
[assembly: System.Reflection.AssemblyMetadata("RepositoryUrl", "https://github.com/fluentassertions/fluentassertions.json.git")]
[assembly: System.Runtime.Versioning.TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName=".NET Standard 2.0")]
namespace FluentAssertions.Json
{
public interface IJsonAssertionOptions<T>
Expand Down