Skip to content

Commit

Permalink
feat: added more snapshot InterfaceTests (#1802)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison authored Sep 3, 2024
1 parent f77f0f3 commit 47ce83e
Show file tree
Hide file tree
Showing 18 changed files with 751 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Refit.GeneratorTests/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,46 @@ public interface IGeneratedClient
return VerifyGenerator(source);
}

public static Task VerifyForType(string declarations)
{
var source =
$$"""
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Refit;
namespace RefitGeneratorTest;
{{declarations}}
""";

return VerifyGenerator(source);
}

public static Task VerifyForDeclaration(string declarations)
{
var source =
$$"""
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Refit;
{{declarations}}
""";

return VerifyGenerator(source);
}

private static CSharpCompilation CreateLibrary(params string[] source)
{
var references = new List<MetadataReference>();
Expand Down
85 changes: 85 additions & 0 deletions Refit.GeneratorTests/InterfaceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
namespace Refit.GeneratorTests;

public class InterfaceTests
{
[Fact]
public Task ContainedInterfaceTest()
{
return Fixture.VerifyForType(
"""
public class ContainerType
{
public interface IContainedInterface
{
[Get("/users")]
Task<string> Get();
}
}
""");
}

[Fact]
public Task DerivedRefitInterfaceTest()
{
return Fixture.VerifyForType(
"""
public interface IGeneratedInterface : IBaseInterface
{
[Get("/users")]
Task<string> Get();
}
public interface IBaseInterface
{
[Get("/posts")]
Task<string> GetPosts();
}
""");
}

[Fact]
public Task DerivedNonRefitInterfaceTest()
{
return Fixture.VerifyForType(
"""
public interface IGeneratedInterface : IBaseInterface
{
[Get("/users")]
Task<string> Get();
}
public interface IBaseInterface
{
void NonRefitMethod();
}
""");
}

[Fact]
public Task NestedNamespaceTest()
{
return Fixture.VerifyForDeclaration(
"""
namespace Nested.RefitGeneratorTest;
public interface IGeneratedInterface
{
[Get("/users")]
Task<string> Get();
}
""");
}

[Fact]
public Task GlobalNamespaceTest()
{
return Fixture.VerifyForDeclaration(
"""
public interface IGeneratedInterface
{
[Get("/users")]
Task<string> Get();
}
""");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//HintName: Generated.g.cs

#pragma warning disable
namespace Refit.Implementation
{

/// <inheritdoc />
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::RefitInternalGenerated.PreserveAttribute]
[global::System.Reflection.Obfuscation(Exclude=true)]
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
internal static partial class Generated
{
#if NET5_0_OR_GREATER
[System.Runtime.CompilerServices.ModuleInitializer]
[System.Diagnostics.CodeAnalysis.DynamicDependency(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All, typeof(global::Refit.Implementation.Generated))]
public static void Initialize()
{
}
#endif
}
}
#pragma warning restore
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//HintName: IContainedInterface.g.cs
#nullable disable
#pragma warning disable
namespace Refit.Implementation
{

partial class Generated
{

/// <inheritdoc />
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::RefitInternalGenerated.PreserveAttribute]
[global::System.Reflection.Obfuscation(Exclude=true)]
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
partial class RefitGeneratorTestContainerTypeIContainedInterface
: global::RefitGeneratorTest.ContainerType.IContainedInterface

{
/// <inheritdoc />
public global::System.Net.Http.HttpClient Client { get; }
readonly global::Refit.IRequestBuilder requestBuilder;

/// <inheritdoc />
public RefitGeneratorTestContainerTypeIContainedInterface(global::System.Net.Http.HttpClient client, global::Refit.IRequestBuilder requestBuilder)
{
Client = client;
this.requestBuilder = requestBuilder;
}



/// <inheritdoc />
public async global::System.Threading.Tasks.Task<string> Get()
{
var ______arguments = global::System.Array.Empty<object>();
var ______func = requestBuilder.BuildRestResultFuncForMethod("Get", global::System.Array.Empty<global::System.Type>() );
try
{
return await ((global::System.Threading.Tasks.Task<string>)______func(this.Client, ______arguments)).ConfigureAwait(false);
}
catch (global::System.Exception ______ex)
{
throw ______ex;
}
}

/// <inheritdoc />
async global::System.Threading.Tasks.Task<string> global::RefitGeneratorTest.ContainerType.IContainedInterface.Get()
{
var ______arguments = global::System.Array.Empty<object>();
var ______func = requestBuilder.BuildRestResultFuncForMethod("Get", global::System.Array.Empty<global::System.Type>() );
try
{
return await ((global::System.Threading.Tasks.Task<string>)______func(this.Client, ______arguments)).ConfigureAwait(false);
}
catch (global::System.Exception ______ex)
{
throw ______ex;
}
}
}
}
}

#pragma warning restore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//HintName: PreserveAttribute.g.cs

#pragma warning disable
namespace RefitInternalGenerated
{
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
[global::System.AttributeUsage (global::System.AttributeTargets.Class | global::System.AttributeTargets.Struct | global::System.AttributeTargets.Enum | global::System.AttributeTargets.Constructor | global::System.AttributeTargets.Method | global::System.AttributeTargets.Property | global::System.AttributeTargets.Field | global::System.AttributeTargets.Event | global::System.AttributeTargets.Interface | global::System.AttributeTargets.Delegate)]
sealed class PreserveAttribute : global::System.Attribute
{
//
// Fields
//
public bool AllMembers;

public bool Conditional;
}
}
#pragma warning restore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//HintName: Generated.g.cs

#pragma warning disable
namespace Refit.Implementation
{

/// <inheritdoc />
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::RefitInternalGenerated.PreserveAttribute]
[global::System.Reflection.Obfuscation(Exclude=true)]
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
internal static partial class Generated
{
#if NET5_0_OR_GREATER
[System.Runtime.CompilerServices.ModuleInitializer]
[System.Diagnostics.CodeAnalysis.DynamicDependency(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All, typeof(global::Refit.Implementation.Generated))]
public static void Initialize()
{
}
#endif
}
}
#pragma warning restore
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//HintName: IGeneratedInterface.g.cs
#nullable disable
#pragma warning disable
namespace Refit.Implementation
{

partial class Generated
{

/// <inheritdoc />
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::RefitInternalGenerated.PreserveAttribute]
[global::System.Reflection.Obfuscation(Exclude=true)]
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
partial class RefitGeneratorTestIGeneratedInterface
: global::RefitGeneratorTest.IGeneratedInterface

{
/// <inheritdoc />
public global::System.Net.Http.HttpClient Client { get; }
readonly global::Refit.IRequestBuilder requestBuilder;

/// <inheritdoc />
public RefitGeneratorTestIGeneratedInterface(global::System.Net.Http.HttpClient client, global::Refit.IRequestBuilder requestBuilder)
{
Client = client;
this.requestBuilder = requestBuilder;
}



/// <inheritdoc />
public async global::System.Threading.Tasks.Task<string> Get()
{
var ______arguments = global::System.Array.Empty<object>();
var ______func = requestBuilder.BuildRestResultFuncForMethod("Get", global::System.Array.Empty<global::System.Type>() );
try
{
return await ((global::System.Threading.Tasks.Task<string>)______func(this.Client, ______arguments)).ConfigureAwait(false);
}
catch (global::System.Exception ______ex)
{
throw ______ex;
}
}

/// <inheritdoc />
async global::System.Threading.Tasks.Task<string> global::RefitGeneratorTest.IGeneratedInterface.Get()
{
var ______arguments = global::System.Array.Empty<object>();
var ______func = requestBuilder.BuildRestResultFuncForMethod("Get", global::System.Array.Empty<global::System.Type>() );
try
{
return await ((global::System.Threading.Tasks.Task<string>)______func(this.Client, ______arguments)).ConfigureAwait(false);
}
catch (global::System.Exception ______ex)
{
throw ______ex;
}
}
}
}
}

#pragma warning restore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//HintName: PreserveAttribute.g.cs

#pragma warning disable
namespace RefitInternalGenerated
{
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
[global::System.AttributeUsage (global::System.AttributeTargets.Class | global::System.AttributeTargets.Struct | global::System.AttributeTargets.Enum | global::System.AttributeTargets.Constructor | global::System.AttributeTargets.Method | global::System.AttributeTargets.Property | global::System.AttributeTargets.Field | global::System.AttributeTargets.Event | global::System.AttributeTargets.Interface | global::System.AttributeTargets.Delegate)]
sealed class PreserveAttribute : global::System.Attribute
{
//
// Fields
//
public bool AllMembers;

public bool Conditional;
}
}
#pragma warning restore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//HintName: Generated.g.cs

#pragma warning disable
namespace Refit.Implementation
{

/// <inheritdoc />
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::RefitInternalGenerated.PreserveAttribute]
[global::System.Reflection.Obfuscation(Exclude=true)]
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
internal static partial class Generated
{
#if NET5_0_OR_GREATER
[System.Runtime.CompilerServices.ModuleInitializer]
[System.Diagnostics.CodeAnalysis.DynamicDependency(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All, typeof(global::Refit.Implementation.Generated))]
public static void Initialize()
{
}
#endif
}
}
#pragma warning restore
Loading

0 comments on commit 47ce83e

Please sign in to comment.