Skip to content

Commit

Permalink
(GH-4255) Introduce Verify for CodeGen Expectations
Browse files Browse the repository at this point in the history
* fixes #4255
  • Loading branch information
devlead committed Nov 15, 2023
1 parent 8f6e170 commit 416ecb8
Show file tree
Hide file tree
Showing 56 changed files with 59 additions and 111 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,7 @@ Thumbs.db
.DS_Store

# Generated Assembly info
AssemblyInfo.Generated.cs
AssemblyInfo.Generated.cs

# Verify
*.received
3 changes: 2 additions & 1 deletion src/Cake.Core.Tests/Cake.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<!-- Global packages -->
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Spectre.Verify.Extensions" Version="22.3.0" />
<PackageReference Include="Verify.Xunit" Version="22.4.1" />
<PackageReference Include="xunit" Version="2.6.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3">
<PrivateAssets>all</PrivateAssets>
Expand All @@ -24,7 +26,6 @@
<PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Unit\Scripting\CodeGen\Expected\Methods\*;Unit\Scripting\CodeGen\Expected\Properties\*" Exclude="bin\**;obj\**;**\*.xproj;packages\**;@(EmbeddedResource)" />
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
</Project>
26 changes: 0 additions & 26 deletions src/Cake.Core.Tests/Exceptions/CustomeExitCodeTests.cs

This file was deleted.

19 changes: 0 additions & 19 deletions src/Cake.Core.Tests/Fixtures/MethodAliasGeneratorFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,13 @@ namespace Cake.Core.Tests.Fixtures
{
public sealed class MethodAliasGeneratorFixture
{
private readonly Assembly _assembly;
private readonly MethodInfo[] _methods;

public MethodAliasGeneratorFixture()
{
_assembly = typeof(MethodAliasGeneratorFixture).GetTypeInfo().Assembly;
_methods = typeof(MethodAliasGeneratorData).GetMethods();
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")]
public string GetExpectedCode(string name)
{
var resource = string.Concat("Cake.Core.Tests.Unit.Scripting.CodeGen.Expected.Methods.", name);
using (var stream = _assembly.GetManifestResourceStream(resource))
{
if (stream == null)
{
throw new InvalidOperationException("Could not load manifest resource stream.");
}
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd().NormalizeGeneratedCode();
}
}
}

public string Generate(string name)
{
var method = _methods.SingleOrDefault(x => x.Name == name);
Expand Down
17 changes: 0 additions & 17 deletions src/Cake.Core.Tests/Fixtures/PropertyAliasGeneratorFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,6 @@ public PropertyAliasGeneratorFixture()
_methods = typeof(PropertyAliasGeneratorData).GetMethods();
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")]
public string GetExpectedData(string name)
{
var resource = string.Concat("Cake.Core.Tests.Unit.Scripting.CodeGen.Expected.Properties.", name);
using (var stream = _assembly.GetManifestResourceStream(resource))
{
if (stream == null)
{
throw new InvalidOperationException("Could not load manifest resource stream.");
}
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd().NormalizeGeneratedCode();
}
}
}

public string Generate(string name)
{
var method = _methods.SingleOrDefault(x => x.Name == name);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Threading.Tasks;
using Cake.Core.Scripting.CodeGen;
using Cake.Core.Tests.Fixtures;
using VerifyXunit;
using Xunit;
using static VerifyXunit.Verifier;

namespace Cake.Core.Tests.Unit.Scripting.CodeGen
{
[UsesVerify]
public sealed class MethodAliasGeneratorTests
{
[UsesVerify]
public sealed class TheGeneratorMethod : IClassFixture<MethodAliasGeneratorFixture>
{
private readonly MethodAliasGeneratorFixture _fixture;
Expand Down Expand Up @@ -56,50 +61,44 @@ public void Should_Throw_If_Method_Is_Null()
[InlineData("NonGeneric_ExtensionMethodWithGenericCollectionOfNestedType")]
[InlineData("NonGeneric_ExtensionMethodWithParameterAttributes")]
[InlineData("NonGeneric_ExtensionMethodWithDynamicReturnValue")]
public void Should_Return_Correct_Generated_Code_For_Non_Generic_Methods(string name)
public Task Should_Return_Correct_Generated_Code_For_Non_Generic_Methods(string name)
{
// Given
var expected = _fixture.GetExpectedCode(name);

// When
// Given / When
var result = _fixture.Generate(name);

// Then
Assert.Equal(expected, result);
return Verify(result)
.UseParameters(name);
}

[Theory]
[InlineData("Generic_ExtensionMethod")]
[InlineData("Generic_ExtensionMethodWithParameter")]
[InlineData("Generic_ExtensionMethodWithGenericReturnValue")]
[InlineData("Generic_ExtensionMethodWithGenericReturnValueAndTypeParamConstraints")]
public void Should_Return_Correct_Generated_Code_For_Generic_Methods(string name)
public Task Should_Return_Correct_Generated_Code_For_Generic_Methods(string name)
{
// Given
var expected = _fixture.GetExpectedCode(name);

// When
// Given / When
var result = _fixture.Generate(name);

// Then
Assert.Equal(expected, result);
return Verify(result)
.UseParameters(name);
}

[Theory]
[InlineData("Obsolete_ImplicitWarning_NoMessage")]
[InlineData("Obsolete_ImplicitWarning_WithMessage")]
[InlineData("Obsolete_ExplicitWarning_WithMessage")]
[InlineData("Obsolete_ExplicitError_WithMessage")]
public void Should_Return_Correct_Generated_Code_For_Obsolete_Methods(string name)
public Task Should_Return_Correct_Generated_Code_For_Obsolete_Methods(string name)
{
// Given
var expected = _fixture.GetExpectedCode(name);

// When
// Given / When
var result = _fixture.Generate(name);

// Then
Assert.Equal(expected, result);
return Verify(result)
.UseParameters(name);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
// See the LICENSE file in the project root for more information.

using System.Reflection;
using System.Threading.Tasks;
using Cake.Core.Scripting.CodeGen;
using Cake.Core.Tests.Fixtures;
using VerifyXunit;
using Xunit;
using static VerifyXunit.Verifier;

namespace Cake.Core.Tests.Unit.Scripting.CodeGen
{
[UsesVerify]
public sealed class PropertyAliasGeneratorTests
{
[UsesVerify]
public sealed class TheGenerateMethod : IClassFixture<PropertyAliasGeneratorFixture>
{
private readonly PropertyAliasGeneratorFixture _fixture;
Expand Down Expand Up @@ -113,66 +118,58 @@ public void Should_Throw_If_Property_Alias_Returns_Void()
[Theory]
[InlineData("NonCached_Value_Type")]
[InlineData("NonCached_Dynamic_Type")]
public void Should_Return_Correct_Generated_Code_For_Non_Cached_Properties(string name)
public Task Should_Return_Correct_Generated_Code_For_Non_Cached_Properties(string name)
{
// Given
var expected = _fixture.GetExpectedData(name);

// When
// Given / When
var result = _fixture.Generate(name);

// Then
Assert.Equal(expected, result);
return Verify(result)
.UseParameters(name);
}

[Theory]
[InlineData("Cached_Reference_Type")]
[InlineData("Cached_Value_Type")]
[InlineData("Cached_Dynamic_Type")]
public void Should_Return_Correct_Generated_Code_For_Cached_Properties(string name)
public Task Should_Return_Correct_Generated_Code_For_Cached_Properties(string name)
{
// Given
var expected = _fixture.GetExpectedData(name);

// When
// Given / When
var result = _fixture.Generate(name);

// Then
Assert.Equal(expected, result);
return Verify(result)
.UseParameters(name);
}

[Theory]
[InlineData("NonCached_Obsolete_ImplicitWarning_NoMessage")]
[InlineData("NonCached_Obsolete_ImplicitWarning_WithMessage")]
[InlineData("NonCached_Obsolete_ExplicitWarning_WithMessage")]
[InlineData("NonCached_Obsolete_ExplicitError_WithMessage")]
public void Should_Return_Correct_Generated_Code_For_Non_Cached_Obsolete_Properties(string name)
public Task Should_Return_Correct_Generated_Code_For_Non_Cached_Obsolete_Properties(string name)
{
// Given
var expected = _fixture.GetExpectedData(name);

// When
// Given / When
var result = _fixture.Generate(name);

// Then
Assert.Equal(expected, result);
return Verify(result)
.UseParameters(name);
}

[Theory]
[InlineData("Cached_Obsolete_ImplicitWarning_NoMessage")]
[InlineData("Cached_Obsolete_ImplicitWarning_WithMessage")]
[InlineData("Cached_Obsolete_ExplicitWarning_WithMessage")]
[InlineData("Cached_Obsolete_ExplicitError_WithMessage")]
public void Should_Return_Correct_Generated_Code_For_Cached_Obsolete_Properties(string name)
public Task Should_Return_Correct_Generated_Code_For_Cached_Obsolete_Properties(string name)
{
// Given
var expected = _fixture.GetExpectedData(name);

// When
// Given / When
var result = _fixture.Generate(name);

// Then
Assert.Equal(expected, result);
return Verify(result)
.UseParameters(name);
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/Cake.Core.Tests/VerifyConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Runtime.CompilerServices;
using VerifyTests;
using VerifyXunit;

namespace Cake.Core.Tests
{
public static class VerifyConfig
{
[ModuleInitializer]
public static void Init()
{
Verifier.DerivePathInfo(Expectations.Initialize);
}
}
}

0 comments on commit 416ecb8

Please sign in to comment.