Skip to content

Commit

Permalink
Input type factory (microsoft#4191)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-nash committed Aug 19, 2024
1 parent e29eaa7 commit da540e1
Show file tree
Hide file tree
Showing 29 changed files with 573 additions and 729 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using Microsoft.Generator.CSharp.ClientModel.Providers;
using Microsoft.Generator.CSharp.Input;
using Microsoft.Generator.CSharp.Providers;
using Microsoft.Generator.CSharp.Tests.Common;
using Moq;
using Moq.Protected;
using NUnit.Framework;
Expand All @@ -32,41 +31,16 @@ public void Setup()
public void PreVisitsMethods()
{
_mockPlugin.Object.AddVisitor(_mockVisitor.Object);
var inputModelProperty =
new InputModelProperty("prop1", "prop1", "string", new InputPrimitiveType(InputPrimitiveTypeKind.Any, "foo", "bar"), true, true, false);
var inputModel = new InputModelType("foo", "id", "desc", "internal", "description",
InputModelTypeUsage.Input, [inputModelProperty], null, [], null, null, new Dictionary<string, InputModelType>(), null, false);
var inputModelProperty = InputFactory.Property("prop1", InputPrimitiveType.Any, isRequired: true, isReadOnly: true);
var inputModel = InputFactory.Model("foo", access: "internal", usage: InputModelTypeUsage.Input, properties: [inputModelProperty]);

var param = new InputParameter("param", "name", "desc",
new InputLiteralType(new InputPrimitiveType(InputPrimitiveTypeKind.String, "foo", "bar"), "bar"),
RequestLocation.Header, null, InputOperationParameterKind.Method, true, false, true, false, false,
false, false, null, null);
var inputOperation = new InputOperation("testoperation", "name", "desc", null, null, [param], new[] { new OperationResponse([200], new InputLiteralType(InputPrimitiveType.Any, "foo"), BodyMediaType.Json, [], false, []) },
"GET", BodyMediaType.Json, "http://example.com", "baz", null, null, true, null, null, true, true, "foo");
var inputClient = new InputClient(
"fooClient",
"desc",
[inputOperation],
[param],
null);
_mockInputLibrary.Setup(l => l.InputNamespace).Returns(new InputNamespace(
var param = InputFactory.Parameter("param", InputFactory.Literal.String("bar"), location: RequestLocation.Header, isRequired: true, isResourceParameter: true);
var inputOperation = InputFactory.Operation("testOperation", parameters: [param], responses: [InputFactory.OperationResponse(bodytype: InputPrimitiveType.Any)]);
var inputClient = InputFactory.Client("fooClient", operations: [inputOperation], parameters: [param]);
_mockInputLibrary.Setup(l => l.InputNamespace).Returns(InputFactory.Namespace(
"test library",
new List<string>(),
new List<InputEnumType>(),
new List<InputModelType> { inputModel },
new List<InputClient>
{
new InputClient(
"fooClient",
"desc",
new[]
{
inputOperation
},
[param],
null)
},
new InputAuth()));
models: [inputModel],
clients: [InputFactory.Client("fooClient", operations: [inputOperation], parameters: [param])]));

var mockClientProvider = new Mock<ClientProvider>(inputClient) { CallBase = true };
_ = mockClientProvider.Object.Methods;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ProjectReference Include="..\..\Microsoft.Generator.CSharp\test\common\Microsoft.Generator.CSharp.Tests.Common.csproj" />
<ProjectReference Include="..\..\TestProjects\Local.Tests\TestProjects.Local.Tests.csproj" />
<ProjectReference Include="..\src\Microsoft.Generator.CSharp.ClientModel.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Generator.CSharp.ClientModel.Providers;
using Microsoft.Generator.CSharp.Input;
using Microsoft.Generator.CSharp.Primitives;
using Microsoft.Generator.CSharp.Tests.Common;
using NUnit.Framework;

namespace Microsoft.Generator.CSharp.ClientModel.Tests.Providers
Expand All @@ -26,21 +27,8 @@ public void SetUp()
if (containsApiVersions)
{
List<string> apiVersions = ["1.0", "2.0"];
InputEnumTypeValue[] enumValues = new InputEnumTypeValue[apiVersions.Count];
for (var i = 0; i < apiVersions.Count; i++)
{
enumValues[i] = new InputEnumTypeValue(apiVersions[i], apiVersions[i], null);
}
var inputEnum = new InputEnumType(
"ServiceVersion",
string.Empty,
null,
null,
"ServiceVersion description",
InputModelTypeUsage.ApiVersionEnum,
InputPrimitiveType.Int64,
enumValues,
false);
var enumValues = apiVersions.Select(a => InputFactory.EnumMember.String(a, a));
var inputEnum = InputFactory.Enum("ServiceVersion", InputPrimitiveType.Int64, values: [.. enumValues], usage: InputModelTypeUsage.ApiVersionEnum);

MockHelpers.LoadMockPlugin(
apiVersions: () => apiVersions,
Expand All @@ -55,7 +43,7 @@ public void SetUp()
[Test]
public void TestImplements()
{
var client = new InputClient("TestClient", "TestClient description", [], [], null);
var client = InputFactory.Client("TestClient");
var clientProvider = new ClientProvider(client);
var clientOptionsProvider = new ClientOptionsProvider(client, clientProvider);

Expand All @@ -71,7 +59,7 @@ public void TestImplements()
[TestCase(false)]
public void TestFields(bool containsApiVersions)
{
var client = new InputClient("TestClient", "TestClient description", [], [], null);
var client = InputFactory.Client("TestClient");
var clientProvider = new ClientProvider(client);
var clientOptionsProvider = new ClientOptionsProvider(client, clientProvider);

Expand All @@ -94,7 +82,7 @@ public void TestFields(bool containsApiVersions)
[TestCase(false)]
public void TestNestedTypes(bool containsApiVersions)
{
var client = new InputClient("TestClient", "TestClient description", [], [], null);
var client = InputFactory.Client("TestClient");
var clientProvider = new ClientProvider(client);
var clientOptionsProvider = new ClientOptionsProvider(client, clientProvider);

Expand Down Expand Up @@ -122,7 +110,7 @@ public void TestNestedTypes(bool containsApiVersions)
[TestCase(false)]
public void TestConstructors(bool containsApiVersions)
{
var client = new InputClient("TestClient", "TestClient description", [], [], null);
var client = InputFactory.Client("TestClient");
var clientProvider = new ClientProvider(client);
var clientOptionsProvider = new ClientOptionsProvider(client, clientProvider);

Expand Down Expand Up @@ -151,7 +139,7 @@ public void TestConstructors(bool containsApiVersions)
[TestCase(false)]
public void TestProperties(bool containsApiVersions)
{
var client = new InputClient("TestClient", "TestClient description", [], [], null);
var client = InputFactory.Client("TestClient");
var clientProvider = new ClientProvider(client);
var clientOptionsProvider = new ClientOptionsProvider(client, clientProvider);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Generator.CSharp.Input;
using Microsoft.Generator.CSharp.Primitives;
using Microsoft.Generator.CSharp.Providers;
using Microsoft.Generator.CSharp.Tests.Common;
using NUnit.Framework;

namespace Microsoft.Generator.CSharp.ClientModel.Tests.Providers.ClientProviders
Expand All @@ -30,7 +31,7 @@ public void SetUp()
[Test]
public void ServiceClientWithSubClient()
{
var client = new InputClient(TestClientName, "TestClient description", [], [], null);
var client = InputFactory.Client(TestClientName);
string[] expectedSubClientFactoryMethodNames = [$"Get{_animalClient.Name.ToCleanName()}Client"];
var clientProvider = new MockClientProvider(client, expectedSubClientFactoryMethodNames);
var writer = new TypeProviderWriter(clientProvider);
Expand Down
Loading

0 comments on commit da540e1

Please sign in to comment.