Skip to content

Commit

Permalink
Mocking client model (#3812)
Browse files Browse the repository at this point in the history
Follow new mocking pattern in mgc.cm as well.

Follow up to #3807
  • Loading branch information
m-nash authored Jul 10, 2024
1 parent 07193ba commit 86a0c56
Show file tree
Hide file tree
Showing 36 changed files with 107 additions and 312 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Microsoft.Generator.CSharp.ClientModel.Tests
{
internal class MockClientTypeProvider : ClientProvider
{
public static readonly ClientProvider Empty = new MockClientTypeProvider();

public MockClientTypeProvider() : base(new InputClient("TestClient", "TestClient description", [], [], null))
{
}
Expand Down
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);
}
}
}

This file was deleted.

This file was deleted.

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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class JsonModelCoreTests
{
public JsonModelCoreTests()
{
MockClientModelPlugin.LoadMockPlugin(serializationExtension: (provider, inputType)
MockHelpers.LoadMockPlugin(getSerializationTypeProviders: (provider, inputType)
=> inputType is InputModelType modeltype ? [new MockMrwProvider(provider, modeltype)] : []);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,26 @@
// Licensed under the MIT License.

using System;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using Microsoft.CodeAnalysis;
using Microsoft.Generator.CSharp.ClientModel.Providers;
using Microsoft.Generator.CSharp.Expressions;
using Microsoft.Generator.CSharp.Input;
using Microsoft.Generator.CSharp.Primitives;
using Microsoft.Generator.CSharp.Providers;
using Moq;
using Moq.Protected;
using NUnit.Framework;
using System.Text.Json;
using System.ClientModel;
using Microsoft.Generator.CSharp.ClientModel.Providers;

namespace Microsoft.Generator.CSharp.ClientModel.Tests.Providers.MrwSerializationTypeDefinitions
{
internal class MrwSerializationTypeDefinitionTests
{
private readonly string _mocksFolder = "Mocks";
private FieldInfo? _mockPlugin;

[SetUp]
public void Setup()
{
var configFilePath = Path.Combine(AppContext.BaseDirectory, _mocksFolder);
var mockTypeFactory = new Mock<ScmTypeFactory>() { };
mockTypeFactory.Protected().Setup<CSharpType>("CreateCSharpTypeCore", ItExpr.IsAny<InputType>()).Returns(new CSharpType(typeof(int)));
// initialize the mock singleton instance of the plugin
_mockPlugin = typeof(CodeModelPlugin).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);

_mockPlugin?.SetValue(null, mockPluginInstance.Object);
}

[TearDown]
public void Teardown()
public MrwSerializationTypeDefinitionTests()
{
_mockPlugin?.SetValue(null, null);
MockHelpers.LoadMockPlugin(createCSharpTypeCore: (inputType) => new CSharpType(typeof(int)));
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,20 @@

using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.Generator.CSharp.ClientModel.Providers;
using Microsoft.Generator.CSharp.Input;
using Microsoft.Generator.CSharp.Providers;
using Moq;
using NUnit.Framework;

namespace Microsoft.Generator.CSharp.ClientModel.Tests.Providers
{
internal class RestClientProviderTests
{
private FieldInfo? _mockPlugin;

[SetUp]
public void Setup()
{
_mockPlugin = typeof(ClientModelPlugin).GetField("_instance", BindingFlags.Static | BindingFlags.NonPublic);
}

[TearDown]
public void Teardown()
{
_mockPlugin?.SetValue(null, null);
}

public void MethodProviderSetUp(InputOperation inputOperation, TypeProvider typeProvider)
{
var mockTypeFactory = new Mock<ScmTypeFactory>() { };
var mockConfiguration = new Mock<Configuration>() { };
var mockGeneratorContext = new Mock<GeneratorContext>(mockConfiguration.Object);
var mockPluginInstance = new Mock<ClientModelPlugin>(mockGeneratorContext.Object) { };
mockTypeFactory.Setup(factory => factory.CreateMethods(inputOperation, typeProvider)).Returns(new ScmMethodProviderCollection(inputOperation, typeProvider));
mockPluginInstance.SetupGet(p => p.TypeFactory).Returns(mockTypeFactory.Object);
_mockPlugin?.SetValue(null, mockPluginInstance.Object);
}

[TestCaseSource(nameof(DefaultCSharpMethodCollectionTestCases))]
public void TestRestClientMethods(InputOperation inputOperation)
{
var inputClient = new InputClient("TestClient", "TestClient description", new[] { inputOperation }, new List<InputParameter>(), null);
var restClientProvider = new RestClientProvider(inputClient);
MethodProviderSetUp(inputOperation, restClientProvider.ClientProvider);
MockHelpers.LoadMockPlugin(createMethods: (inputOperation, typeProvider) => new ScmMethodProviderCollection(inputOperation, restClientProvider.ClientProvider));

var methods = restClientProvider.Methods;
Assert.IsNotNull(methods, "Methods should not be null.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,25 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Microsoft.Generator.CSharp.ClientModel.Providers;
using Microsoft.Generator.CSharp.Input;
using Microsoft.Generator.CSharp.Primitives;
using Microsoft.Generator.CSharp.Providers;
using Moq;
using Moq.Protected;
using NUnit.Framework;

namespace Microsoft.Generator.CSharp.ClientModel.Tests.Providers
{
internal class ScmMethodProviderCollectionTests
{
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
private ScmTypeFactory _typeFactory;
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
private readonly string _mocksFolder = "Mocks";
private FieldInfo? _mockPlugin;

[SetUp]
public void Setup()
{
var mockParameter = new ParameterProvider("mockParam", $"mock description", typeof(bool), null);
var mockTypeFactory = new Mock<ScmTypeFactory>() { };
mockTypeFactory.Protected().Setup<CSharpType>("CreateCSharpTypeCore", ItExpr.IsAny<InputType>()).Returns(new CSharpType(typeof(bool)));
mockTypeFactory.Setup(t => t.CreateParameter(It.IsAny<InputParameter>())).Returns(mockParameter);
_typeFactory = mockTypeFactory.Object;

var configFilePath = Path.Combine(AppContext.BaseDirectory, _mocksFolder);
// initialize the mock singleton instance of the plugin
_mockPlugin = typeof(ClientModelPlugin).GetField("_instance", BindingFlags.Static | BindingFlags.NonPublic);
var mockConfiguration = new Mock<Configuration>() { };
var mockGeneratorContext = new Mock<GeneratorContext>(mockConfiguration.Object);
var mockPluginInstance = new Mock<ClientModelPlugin>(mockGeneratorContext.Object) { };
mockPluginInstance.SetupGet(p => p.TypeFactory).Returns(_typeFactory);

_mockPlugin?.SetValue(null, mockPluginInstance.Object);
}

[TearDown]
public void Teardown()
{
_mockPlugin?.SetValue(null, null);
}

// Validate that the default method collection consists of the expected method kind(s)
[TestCaseSource(nameof(DefaultCSharpMethodCollectionTestCases))]
public void TestDefaultCSharpMethodCollection(InputOperation inputOperation)
{
var methodCollection = new ScmMethodProviderCollection(inputOperation, new MockClientTypeProvider());
MockHelpers.LoadMockPlugin(
createCSharpTypeCore: (inputType) => new CSharpType(typeof(bool)),
createParameter: (inputParameter) => new ParameterProvider("mockParam", $"mock description", typeof(bool), null));

var methodCollection = new ScmMethodProviderCollection(inputOperation, MockClientTypeProvider.Empty);
Assert.IsNotNull(methodCollection);
Assert.AreEqual(4, methodCollection.Count);

Expand Down
Loading

0 comments on commit 86a0c56

Please sign in to comment.