Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add tests for SDK generation
  • Loading branch information
live1206 committed Oct 23, 2024
commit db7e9fe1906a779778a4dce07ad2ff37a3d1b592
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,15 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<Compile Remove="**\TestData\**\*.cs" />
</ItemGroup>

<ItemGroup>
<None Include="**\TestData\**\*.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;

namespace Azure.Generator.Tests.Common
{
internal static class Helpers
{
private static readonly string _assemblyLocation = Path.GetDirectoryName(typeof(Helpers).Assembly.Location)!;

public static string GetExpectedFromFile(string? parameters = null)
{
return File.ReadAllText(GetAssetFileOrDirectoryPath(true, parameters));
}

private static string GetAssetFileOrDirectoryPath(bool isFile, string? parameters = null)
{
var stackTrace = new StackTrace();
var stackFrame = GetRealMethodInvocation(stackTrace);
var method = stackFrame.GetMethod();
var callingClass = method!.DeclaringType;
var nsSplit = callingClass!.Namespace!.Split('.');
var paramString = parameters is null ? string.Empty : $"({parameters})";
var extName = isFile ? ".cs" : string.Empty;
var path = _assemblyLocation;
var nsSkip = 3;
for (int i = nsSkip; i < nsSplit.Length; i++)
{
path = Path.Combine(path, nsSplit[i]);
}
return Path.Combine(path, "TestData", callingClass.Name, $"{method.Name}{paramString}{extName}");
}

private static StackFrame GetRealMethodInvocation(StackTrace stackTrace)
{
int i = 1;
while (i < stackTrace.FrameCount)
{
var frame = stackTrace.GetFrame(i);
var declaringType = frame!.GetMethod()!.DeclaringType!;
// we need to skip those method invocations from this class, or from the async state machine when the caller is an async method
if (declaringType != typeof(Helpers) && declaringType.Name != "MockHelpers" && !IsCompilerGenerated(declaringType))
{
return frame;
}
i++;
}

throw new InvalidOperationException($"There is no method invocation outside the {typeof(Helpers)} class in the stack trace");

static bool IsCompilerGenerated(Type type)
{
return type.IsDefined(typeof(CompilerGeneratedAttribute), false) || (type.Namespace?.StartsWith("System.Runtime.CompilerServices") ?? false) ||
type.Name.StartsWith("<<", StringComparison.Ordinal);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Azure.Generator.Tests.Common;
using Azure.Generator.Tests.TestHelpers;
using Microsoft.Generator.CSharp.ClientModel.Providers;
using Microsoft.Generator.CSharp.ClientModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using System.ClientModel.Primitives;
using Azure.Generator.Providers;

namespace Azure.Generator.Tests.Providers.Abstractions
{
internal class AzureClientResponseProviderTests
{
[Test]
public void ValidateReturnTypeIsOverridden()
{
ClientProvider clientProvider = CreateMockClientProvider();

var method = clientProvider.Methods.FirstOrDefault(x => !x.Signature.Name.EndsWith("Async"));
Assert.NotNull(method);
Assert.NotNull(method!.Signature.ReturnType);
Assert.IsTrue(method!.Signature.ReturnType!.Equals(typeof(Response)));
}

[Test]
public void ValidateBodyOfClientOperationIsOverridden()
{
var clientProvider = CreateMockClientProvider();

var method = clientProvider.Methods.FirstOrDefault(x => x.Signature.Parameters.Any(p => p.Type.Equals(typeof(RequestContext))) && !x.Signature.Name.EndsWith("Async"));
Assert.NotNull(method);
Assert.NotNull(method!.BodyStatements);
var test = method.BodyStatements!.ToDisplayString();
Assert.AreEqual(Helpers.GetExpectedFromFile(), method.BodyStatements!.ToDisplayString());
}

private static ClientProvider CreateMockClientProvider()
{
var client = InputFactory.Client("TestClient", [InputFactory.Operation("foo")]);
MockHelpers.LoadMockPlugin(clientResponseApi: AzureClientResponseProvider.Instance);
var clientProvider = AzureClientPlugin.Instance.TypeFactory.CreateClient(client);
return clientProvider;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
using global::Azure.Core.HttpMessage message = this.CreateFooRequest(context);
return Pipeline.ProcessMessage(message, context);
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Microsoft.Generator.CSharp;
using Microsoft.Generator.CSharp.ClientModel;
using Microsoft.Generator.CSharp.ClientModel.Providers;
using Microsoft.Generator.CSharp.Input;
using Microsoft.Generator.CSharp.Primitives;
using Microsoft.Generator.CSharp.Providers;
Expand All @@ -28,7 +29,10 @@ public static Mock<AzureClientPlugin> LoadMockPlugin(
Func<IReadOnlyList<string>>? apiVersions = null,
Func<IReadOnlyList<InputEnumType>>? inputEnums = null,
Func<IReadOnlyList<InputModelType>>? inputModels = null,
Func<IReadOnlyList<InputClient>>? clients = null)
Func<IReadOnlyList<InputClient>>? clients = null,
ClientResponseApi? clientResponseApi = null,
ClientPipelineApi? clientPipelineApi = null,
HttpMessageApi? httpMessageApi = null)
{
IReadOnlyList<string> inputNsApiVersions = apiVersions?.Invoke() ?? [];
IReadOnlyList<InputEnumType> inputNsEnums = inputEnums?.Invoke() ?? [];
Expand Down