-
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.
Follow new mocking pattern in mgc.cm as well. Follow up to #3807
- Loading branch information
Showing
36 changed files
with
107 additions
and
312 deletions.
There are no files selected for viewing
46 changes: 0 additions & 46 deletions
46
...harp/generator/Microsoft.Generator.CSharp.ClientModel/test/Mocks/MockClientModelPlugin.cs
This file was deleted.
Oops, something went wrong.
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
76 changes: 76 additions & 0 deletions
76
...-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Mocks/MockHelpers.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,76 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Reflection; | ||
using Microsoft.Generator.CSharp.Input; | ||
using Microsoft.Generator.CSharp.Primitives; | ||
using Microsoft.Generator.CSharp.Providers; | ||
using Moq; | ||
using Moq.Protected; | ||
|
||
namespace Microsoft.Generator.CSharp.ClientModel.Tests | ||
{ | ||
internal static class MockHelpers | ||
{ | ||
public const string MocksFolder = "Mocks"; | ||
|
||
public static void LoadMockPlugin( | ||
Func<TypeProvider, InputType, IReadOnlyList<TypeProvider>>? getSerializationTypeProviders = null, | ||
Func<InputType, CSharpType>? createCSharpTypeCore = null, | ||
Func<CSharpType>? matchConditionsType = null, | ||
Func<CSharpType>? tokenCredentialType = null, | ||
Func<InputOperation, TypeProvider, MethodProviderCollection>? createMethods = null, | ||
Func<InputParameter, ParameterProvider>? createParameter = null) | ||
{ | ||
var mockTypeFactory = new Mock<ScmTypeFactory>() { CallBase = true }; | ||
|
||
if (matchConditionsType is not null) | ||
{ | ||
mockTypeFactory.Setup(p => p.MatchConditionsType()).Returns(matchConditionsType); | ||
} | ||
|
||
if (tokenCredentialType is not null) | ||
{ | ||
mockTypeFactory.Setup(p => p.TokenCredentialType()).Returns(tokenCredentialType); | ||
} | ||
|
||
if (createMethods is not null) | ||
{ | ||
mockTypeFactory.Setup(p => p.CreateMethods(It.IsAny<InputOperation>(), It.IsAny<TypeProvider>())).Returns(createMethods); | ||
} | ||
|
||
if (createParameter is not null) | ||
{ | ||
mockTypeFactory.Setup(p => p.CreateParameter(It.IsAny<InputParameter>())).Returns(createParameter); | ||
} | ||
|
||
var configFilePath = Path.Combine(AppContext.BaseDirectory, MocksFolder); | ||
// initialize the mock singleton instance of the plugin | ||
var codeModelInstance = typeof(CodeModelPlugin).GetField("_instance", BindingFlags.Static | BindingFlags.NonPublic); | ||
var clientModelInstance = typeof(ClientModelPlugin).GetField("_instance", BindingFlags.Static | BindingFlags.NonPublic); | ||
// invoke the load method with the config file path | ||
var loadMethod = typeof(Configuration).GetMethod("Load", BindingFlags.Static | BindingFlags.NonPublic); | ||
object?[] parameters = [configFilePath, null]; | ||
var config = loadMethod?.Invoke(null, parameters); | ||
var mockGeneratorContext = new Mock<GeneratorContext>(config!); | ||
var mockPluginInstance = new Mock<ClientModelPlugin>(mockGeneratorContext.Object) { }; | ||
mockPluginInstance.SetupGet(p => p.TypeFactory).Returns(mockTypeFactory.Object); | ||
|
||
if (getSerializationTypeProviders is not null) | ||
{ | ||
mockPluginInstance.Setup(p => p.GetSerializationTypeProviders(It.IsAny<TypeProvider>(), It.IsAny<InputType>())).Returns(getSerializationTypeProviders); | ||
} | ||
|
||
if (createCSharpTypeCore is not null) | ||
{ | ||
mockTypeFactory.Protected().Setup<CSharpType>("CreateCSharpTypeCore", ItExpr.IsAny<InputType>()).Returns(createCSharpTypeCore); | ||
} | ||
|
||
codeModelInstance!.SetValue(null, mockPluginInstance.Object); | ||
clientModelInstance!.SetValue(null, mockPluginInstance.Object); | ||
} | ||
} | ||
} |
17 changes: 0 additions & 17 deletions
17
...ent-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Mocks/MockTypeFactory.cs
This file was deleted.
Oops, something went wrong.
14 changes: 0 additions & 14 deletions
14
...nt-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Mocks/MockTypeProvider.cs
This file was deleted.
Oops, something went wrong.
25 changes: 8 additions & 17 deletions
25
...erator/Microsoft.Generator.CSharp.ClientModel/test/OutputTypes/ScmKnownParametersTests.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 |
---|---|---|
@@ -1,40 +1,31 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Microsoft.Generator.CSharp.ClientModel.Primitives; | ||
using Microsoft.Generator.CSharp.Primitives; | ||
using Moq; | ||
using NUnit.Framework; | ||
|
||
namespace Microsoft.Generator.CSharp.ClientModel.Tests.OutputTypes | ||
{ | ||
internal class ScmKnownParametersTests | ||
{ | ||
[OneTimeSetUp] | ||
public void OneTimeSetup() | ||
{ | ||
var mockPlugin = new Mock<ClientModelPlugin>(new Mock<GeneratorContext>().Object); | ||
mockPlugin.Setup(p => p.TypeFactory).Returns(new MockTypeFactory()); | ||
// initialize the singleton instance of the plugin | ||
_ = mockPlugin.Object; | ||
} | ||
|
||
[Test] | ||
public void TestTokenAuth() | ||
{ | ||
var result = ScmKnownParameters.TokenAuth; | ||
MockHelpers.LoadMockPlugin(tokenCredentialType: () => typeof(int)); | ||
|
||
var result = ClientModelPlugin.Instance.TypeFactory.TokenCredentialType(); | ||
Assert.IsNotNull(result); | ||
Assert.IsNotNull(result.Type); | ||
Assert.IsTrue(result.Type.Equals(new CSharpType(typeof(int)))); | ||
Assert.AreEqual(new CSharpType(typeof(int)), result); | ||
} | ||
|
||
[TestCase] | ||
public void TestMatchConditionsParameter() | ||
{ | ||
var result = ScmKnownParameters.MatchConditionsParameter; | ||
MockHelpers.LoadMockPlugin(matchConditionsType: () => typeof(int)); | ||
|
||
var result = ClientModelPlugin.Instance.TypeFactory.MatchConditionsType(); | ||
Assert.IsNotNull(result); | ||
Assert.IsNotNull(result.Type); | ||
Assert.IsTrue(result.Type.Equals(new CSharpType(typeof(int)))); | ||
Assert.AreEqual(new CSharpType(typeof(int)), result); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.