-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for adding custom client method (#4578)
This PR adds unit tests for adding custom client methods. fixes: #4262
- Loading branch information
1 parent
ac2de4b
commit 533cb75
Showing
6 changed files
with
203 additions
and
2 deletions.
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
...tor.CSharp.ClientModel/test/Providers/ClientProviders/ClientProviderCustomizationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.ClientModel; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.Generator.CSharp.ClientModel.Providers; | ||
using Microsoft.Generator.CSharp.Input; | ||
using Microsoft.Generator.CSharp.Tests.Common; | ||
using NUnit.Framework; | ||
|
||
namespace Microsoft.Generator.CSharp.ClientModel.Tests.Providers.ClientProviders | ||
{ | ||
public class ClientProviderCustomizationTests | ||
{ | ||
[Test] | ||
public async Task CanAddMethod() | ||
{ | ||
var inputOperation = InputFactory.Operation("HelloAgain", parameters: | ||
[ | ||
InputFactory.Parameter("p1", InputFactory.Array(InputPrimitiveType.String)) | ||
]); | ||
var inputClient = InputFactory.Client("TestClient", operations: [inputOperation]); | ||
var plugin = await MockHelpers.LoadMockPluginAsync( | ||
clients: () => [inputClient], | ||
compilation: async () => await Helpers.GetCompilationFromDirectoryAsync()); | ||
|
||
// Find the client provider | ||
var clientProvider = plugin.Object.OutputLibrary.TypeProviders.SingleOrDefault(t => t is ClientProvider); | ||
Assert.IsNotNull(clientProvider); | ||
|
||
var clientProviderMethods = clientProvider!.Methods; | ||
Assert.AreEqual(4, clientProviderMethods.Count); | ||
Assert.IsFalse(clientProviderMethods.Any(m => m.Signature.Name == "NewMethod")); | ||
|
||
// The custom code view should contain the method | ||
var customCodeView = clientProvider.CustomCodeView; | ||
Assert.IsNotNull(customCodeView); | ||
var customMethods = customCodeView!.Methods; | ||
Assert.AreEqual(1, customMethods.Count); | ||
Assert.AreEqual("NewMethod", customMethods[0].Signature.Name); | ||
Assert.AreEqual(customMethods[0].Signature.Parameters.Count, 2); | ||
Assert.IsNull(customMethods[0].BodyExpression); | ||
Assert.AreEqual(string.Empty, customMethods[0].BodyStatements!.ToDisplayString()); | ||
} | ||
|
||
[Test] | ||
public async Task CanAddMultipleMethods() | ||
{ | ||
var inputOperation = InputFactory.Operation("HelloAgain", parameters: | ||
[ | ||
InputFactory.Parameter("p1", InputFactory.Array(InputPrimitiveType.String)) | ||
]); | ||
var inputClient = InputFactory.Client("TestClient", operations: [inputOperation]); | ||
var plugin = await MockHelpers.LoadMockPluginAsync( | ||
clients: () => [inputClient], | ||
compilation: async () => await Helpers.GetCompilationFromDirectoryAsync()); | ||
|
||
// Find the client provider | ||
var clientProvider = plugin.Object.OutputLibrary.TypeProviders.SingleOrDefault(t => t is ClientProvider); | ||
Assert.IsNotNull(clientProvider); | ||
|
||
var clientProviderMethods = clientProvider!.Methods; | ||
Assert.AreEqual(4, clientProviderMethods.Count); | ||
|
||
// The custom code view should contain the method | ||
var customCodeView = clientProvider.CustomCodeView; | ||
Assert.IsNotNull(customCodeView); | ||
var customMethods = customCodeView!.Methods; | ||
Assert.AreEqual(4, customMethods.Count); | ||
Assert.AreEqual("NewMethodOne", customMethods[0].Signature.Name); | ||
Assert.AreEqual(customMethods[0].Signature.Parameters.Count, 2); | ||
Assert.AreEqual("NewMethodTwo", customMethods[1].Signature.Name); | ||
Assert.AreEqual(customMethods[1].Signature.Parameters.Count, 0); | ||
Assert.AreEqual("NewMethodThree", customMethods[2].Signature.Name); | ||
Assert.AreEqual(customMethods[2].Signature.Parameters.Count, 1); | ||
Assert.AreEqual("NewMethodFour", customMethods[3].Signature.Name); | ||
Assert.AreEqual(customMethods[3].Signature.Parameters.Count, 1); | ||
} | ||
|
||
// Validates that the custom method is added when the method has the same name as an existing method but different parameters | ||
[Test] | ||
public async Task CanAddMethodSameName() | ||
{ | ||
var inputOperation = InputFactory.Operation("HelloAgain", parameters: | ||
[ | ||
InputFactory.Parameter("p1", InputFactory.Array(InputPrimitiveType.String)) | ||
]); | ||
var inputClient = InputFactory.Client("TestClient", operations: [inputOperation]); | ||
var plugin = await MockHelpers.LoadMockPluginAsync( | ||
clients: () => [inputClient], | ||
compilation: async () => await Helpers.GetCompilationFromDirectoryAsync()); | ||
|
||
// Find the client provider | ||
var clientProvider = plugin.Object.OutputLibrary.TypeProviders.SingleOrDefault(t => t is ClientProvider); | ||
Assert.IsNotNull(clientProvider); | ||
|
||
var clientProviderMethods = clientProvider!.Methods; | ||
Assert.AreEqual(4, clientProviderMethods.Count); | ||
|
||
var helloAgainMethod = clientProviderMethods.FirstOrDefault(m | ||
=> m.Signature.Name == "HelloAgain" && m.Signature.Parameters.Count > 0 && m.Signature.Parameters[0].Name == "p1"); | ||
Assert.IsNotNull(helloAgainMethod); | ||
Assert.AreEqual(1, helloAgainMethod!.Signature.Parameters.Count); | ||
|
||
// The custom code view should contain the method | ||
var customCodeView = clientProvider.CustomCodeView; | ||
Assert.IsNotNull(customCodeView); | ||
var customMethods = customCodeView!.Methods; | ||
Assert.AreEqual(1, customMethods.Count); | ||
Assert.AreEqual("HelloAgain", customMethods[0].Signature.Name); | ||
Assert.AreEqual(customMethods[0].Signature.Parameters.Count, 2); | ||
Assert.IsNull(customMethods[0].BodyExpression); | ||
Assert.AreEqual(string.Empty, customMethods[0].BodyStatements!.ToDisplayString()); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...rs/ClientProviders/TestData/ClientProviderCustomizationTests/CanAddMethod/CanAddMethod.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// <auto-generated/> | ||
|
||
#nullable disable | ||
|
||
using System; | ||
using System.ClientModel; | ||
using System.ClientModel.Primitives; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Sample | ||
{ | ||
/// <summary></summary> | ||
public partial class TestClient | ||
{ | ||
public virtual ClientResult NewMethod(BinaryContent content, RequestOptions options) | ||
{ | ||
Argument.AssertNotNull(content, nameof(content)); | ||
|
||
using PipelineMessage message = CreateRequest(content, options); | ||
return ClientResult.FromResponse(Pipeline.ProcessMessage(message, options)); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...rs/TestData/ClientProviderCustomizationTests/CanAddMethodSameName/CanAddMethodSameName.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// <auto-generated/> | ||
|
||
#nullable disable | ||
|
||
using System; | ||
using System.ClientModel; | ||
using System.ClientModel.Primitives; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Sample | ||
{ | ||
/// <summary></summary> | ||
public partial class TestClient | ||
{ | ||
public virtual ClientResult HelloAgain(string[] p1, RequestOptions options) | ||
{ | ||
|
||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
.../TestData/ClientProviderCustomizationTests/CanAddMultipleMethods/CanAddMultipleMethods.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// <auto-generated/> | ||
|
||
#nullable disable | ||
|
||
using System; | ||
using System.ClientModel; | ||
using System.ClientModel.Primitives; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Sample | ||
{ | ||
/// <summary></summary> | ||
public partial class TestClient | ||
{ | ||
public virtual ClientResult NewMethodOne(BinaryContent content, RequestOptions options) | ||
{ | ||
Argument.AssertNotNull(content, nameof(content)); | ||
|
||
using PipelineMessage message = CreateRequest(content, options); | ||
return ClientResult.FromResponse(Pipeline.ProcessMessage(message, options)); | ||
} | ||
|
||
public void NewMethodTwo() { } | ||
|
||
public void NewMethodThree(CustomModel p1) { } | ||
public bool NewMethodFour(CustomModel? p1) { return false; } | ||
} | ||
|
||
public class CustomModel { } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters