diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/InputLibraryVisitorTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/InputLibraryVisitorTests.cs index d446d20b4d..cfeec9ac45 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/InputLibraryVisitorTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/InputLibraryVisitorTests.cs @@ -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; @@ -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(), 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(), - new List(), - new List { inputModel }, - new List - { - new InputClient( - "fooClient", - "desc", - new[] - { - inputOperation - }, - [param], - null) - }, - new InputAuth())); + models: [inputModel], + clients: [InputFactory.Client("fooClient", operations: [inputOperation], parameters: [param])])); var mockClientProvider = new Mock(inputClient) { CallBase = true }; _ = mockClientProvider.Object.Methods; diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Microsoft.Generator.CSharp.ClientModel.Tests.csproj b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Microsoft.Generator.CSharp.ClientModel.Tests.csproj index 13e22d783d..d7462b3bbf 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Microsoft.Generator.CSharp.ClientModel.Tests.csproj +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Microsoft.Generator.CSharp.ClientModel.Tests.csproj @@ -1,6 +1,7 @@ + diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/ClientOptionsProviderTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/ClientOptionsProviderTests.cs index 1d72538fea..9ce63288ed 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/ClientOptionsProviderTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/ClientOptionsProviderTests.cs @@ -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 @@ -26,21 +27,8 @@ public void SetUp() if (containsApiVersions) { List 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, @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/ClientProviders/ClientProviderSubClientTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/ClientProviders/ClientProviderSubClientTests.cs index e58c00b36a..6efe3740ea 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/ClientProviders/ClientProviderSubClientTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/ClientProviders/ClientProviderSubClientTests.cs @@ -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 @@ -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); diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/ClientProviders/ClientProviderTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/ClientProviders/ClientProviderTests.cs index 8152d35ffc..c86f817d43 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/ClientProviders/ClientProviderTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/ClientProviders/ClientProviderTests.cs @@ -12,6 +12,7 @@ using Microsoft.Generator.CSharp.Providers; using Microsoft.Generator.CSharp.Snippets; using Microsoft.Generator.CSharp.Statements; +using Microsoft.Generator.CSharp.Tests.Common; using NUnit.Framework; using static Microsoft.Generator.CSharp.Snippets.Snippet; @@ -46,7 +47,7 @@ public void SetUp() [Test] public void TestBuildProperties() { - var client = new InputClient(TestClientName, "TestClient description", [], [], null); + var client = InputFactory.Client(TestClientName); var clientProvider = new ClientProvider(client); Assert.IsNotNull(clientProvider); @@ -66,7 +67,7 @@ public void TestBuildProperties() [TestCaseSource(nameof(BuildFieldsTestCases))] public void TestBuildFields(List inputParameters, bool containsAdditionalOptionalParams) { - var client = new InputClient(TestClientName, "TestClient description", [], inputParameters, null); + var client = InputFactory.Client(TestClientName, parameters: [.. inputParameters]); var clientProvider = new ClientProvider(client); Assert.IsNotNull(clientProvider); @@ -140,7 +141,7 @@ public void TestBuildFields_WithSubClients(InputClient client, bool hasSubClient [TestCaseSource(nameof(BuildConstructorsTestCases))] public void TestBuildConstructors_PrimaryConstructor(List inputParameters) { - var client = new InputClient(TestClientName, "TestClient description", [], inputParameters, null); + var client = InputFactory.Client(TestClientName, parameters: [.. inputParameters]); var clientProvider = new ClientProvider(client); Assert.IsNotNull(clientProvider); @@ -156,7 +157,7 @@ public void TestBuildConstructors_PrimaryConstructor(List inputP [TestCaseSource(nameof(BuildConstructorsTestCases))] public void TestBuildConstructors_SecondaryConstructor(List inputParameters) { - var client = new InputClient(TestClientName, "TestClient description", [], inputParameters, null); + var client = InputFactory.Client(TestClientName, parameters: [.. inputParameters]); var clientProvider = new ClientProvider(client); Assert.IsNotNull(clientProvider); @@ -259,7 +260,7 @@ private void ValidateSecondaryConstructor( [TestCaseSource(nameof(EndpointParamInitializationValueTestCases))] public void EndpointInitializationValue(InputParameter endpointParameter, ValueExpression? expectedValue) { - var client = new InputClient(TestClientName, "TestClient description", [], [endpointParameter], null); + var client = InputFactory.Client(TestClientName, parameters: [endpointParameter]); var clientProvider = new ClientProvider(client); Assert.IsNotNull(clientProvider); @@ -286,7 +287,7 @@ public void TestGetClientOptions(bool isSubClient) parentClientName = "parent"; } - var client = new InputClient(TestClientName, "TestClient description", [], [], parentClientName); + var client = InputFactory.Client(TestClientName, parent: parentClientName); var clientProvider = new ClientProvider(client); if (isSubClient) @@ -365,63 +366,29 @@ public void ValidateQueryParamWriterDiff() } private static InputClient GetEnumQueryParamClient() - => new InputClient( + => InputFactory.Client( TestClientName, - "TestClient description", + operations: [ - new InputOperation( + InputFactory.Operation( "Operation", - null, - "Operation", - null, - "public", + parameters: [ - new InputParameter( - "queryParam", - "queryParam", + InputFactory.Parameter( "queryParam", - new InputEnumType( + InputFactory.Enum( "InputEnum", - "InputEnum", - "public", - null, - "InputEnum", - InputModelTypeUsage.Input, - new InputPrimitiveType(InputPrimitiveTypeKind.String, "string", "string", null, null), + InputPrimitiveType.String, + usage: InputModelTypeUsage.Input, + isExtensible: true, + values: [ - new InputEnumTypeValue("value1", "value1", "value1"), - new InputEnumTypeValue("value2", "value2", "value2"), - ], - true), - RequestLocation.Query, - defaultValue: null, - InputOperationParameterKind.Method, - isRequired: false, false, false, false, false, false, false, null, null) - ], - [ - new OperationResponse( - [200], - null, - BodyMediaType.None, - [], - false, - []) - ], - "GET", - BodyMediaType.None, - "/foo", - "/foo", - null, - null, - true, - null, - null, - true, - true, - "Operation") - ], - [], - null); + InputFactory.EnumMember.String("value1", "value1"), + InputFactory.EnumMember.String("value2", "value2") + ]), + location: RequestLocation.Query) + ]) + ]); private class ValidateQueryParamDiffClientProvider : ClientProvider { @@ -448,63 +415,45 @@ public static IEnumerable BuildFieldsTestCases { yield return new TestCaseData(new List { - new( - "optionalParam", - "optionalParam description", + InputFactory.Parameter( "optionalParam", InputPrimitiveType.String, - RequestLocation.None, - defaultValue: null, - InputOperationParameterKind.Client, - isRequired: false, false, false, false, false, false, false, null, null), - new( - KnownParameters.Endpoint.Name, - "endpoint description", + location: RequestLocation.None, + kind: InputOperationParameterKind.Client), + InputFactory.Parameter( KnownParameters.Endpoint.Name, InputPrimitiveType.String, - RequestLocation.None, - defaultValue: null, - InputOperationParameterKind.Client, - isRequired: false, false, false, false, isEndpoint: true, false, false, null, null) + location:RequestLocation.None, + kind: InputOperationParameterKind.Client, + isEndpoint: true) }, false); yield return new TestCaseData(new List { - new( - "optionalParam", - "optionalParam description", + InputFactory.Parameter( "optionalParam", InputPrimitiveType.String, - RequestLocation.None, - defaultValue: new InputConstant("someValue", InputPrimitiveType.String), - InputOperationParameterKind.Client, - isRequired: false, false, false, false, false, false, false, null, null), - new( - "optionalParam2", - "optionalParam description", + location: RequestLocation.None, + defaultValue: InputFactory.Constant.String("someValue"), + kind: InputOperationParameterKind.Client), + InputFactory.Parameter( "optionalParam2", InputPrimitiveType.String, - RequestLocation.None, - defaultValue: new InputConstant("someValue", InputPrimitiveType.String), - InputOperationParameterKind.Client, - isRequired: false, false, false, false, false, false, false, null, null), - new( - "optionalParam3", - "optionalParam description", + location: RequestLocation.None, + defaultValue: InputFactory.Constant.String("someValue"), + kind: InputOperationParameterKind.Client), + InputFactory.Parameter( "optionalParam3", InputPrimitiveType.Int64, - RequestLocation.None, - defaultValue: new InputConstant(2, InputPrimitiveType.Int64), - InputOperationParameterKind.Client, - isRequired: false, false, false, false, false, false, false, null, null), - new( - KnownParameters.Endpoint.Name, - "endpoint description", + location: RequestLocation.None, + defaultValue: InputFactory.Constant.Int64(2), + kind: InputOperationParameterKind.Client), + InputFactory.Parameter( KnownParameters.Endpoint.Name, InputPrimitiveType.String, - RequestLocation.None, + location: RequestLocation.None, defaultValue: null, - InputOperationParameterKind.Client, - isRequired: false, false, false, false, isEndpoint: true, false, false, null, null) + kind: InputOperationParameterKind.Client, + isEndpoint: true) }, true); } } @@ -513,7 +462,7 @@ public static IEnumerable SubClientTestCases { get { - yield return new TestCaseData(new InputClient(TestClientName, "TestClient description", [], [], null), true); + yield return new TestCaseData(InputFactory.Client(TestClientName), true); yield return new TestCaseData(_animalClient, true); yield return new TestCaseData(_dogClient, true); yield return new TestCaseData(_huskyClient, false); @@ -526,46 +475,34 @@ public static IEnumerable BuildConstructorsTestCases { yield return new TestCaseData(new List { - new( - "optionalParam", - "optionalParam description", + InputFactory.Parameter( "optionalParam", InputPrimitiveType.String, - RequestLocation.None, - defaultValue: null, - InputOperationParameterKind.Client, - isRequired: false, false, false, false, false, false, false, null, null), - new( - KnownParameters.Endpoint.Name, - "endpoint description", + location: RequestLocation.None, + kind: InputOperationParameterKind.Client), + InputFactory.Parameter( KnownParameters.Endpoint.Name, InputPrimitiveType.String, - RequestLocation.None, - defaultValue: new InputConstant("someValue", InputPrimitiveType.String), - InputOperationParameterKind.Client, - isRequired: false, false, false, false, isEndpoint: true, false, false, null, null) + location: RequestLocation.None, + defaultValue: InputFactory.Constant.String("someValue"), + kind: InputOperationParameterKind.Client, + isEndpoint: true) }); // scenario where endpoint is required yield return new TestCaseData(new List { - new( - KnownParameters.Endpoint.Name, - "endpoint description", + InputFactory.Parameter( KnownParameters.Endpoint.Name, InputPrimitiveType.String, - RequestLocation.None, - defaultValue: null, - InputOperationParameterKind.Client, - isRequired: true, false, false, false, isEndpoint: true, false, false, null, null), - new( - "optionalParam", - "optionalParam description", + location: RequestLocation.None, + kind: InputOperationParameterKind.Client, + isRequired: true, + isEndpoint: true), + InputFactory.Parameter( "optionalParam", InputPrimitiveType.String, - RequestLocation.None, - defaultValue: null, - InputOperationParameterKind.Client, - isRequired: false, false, false, false, false, false, false, null, null) + location: RequestLocation.None, + kind: InputOperationParameterKind.Client) }); } } @@ -574,15 +511,13 @@ private static IEnumerable EndpointParamInitializationValueTestCas { // string primitive type yield return new TestCaseData( - new InputParameter( - "param", - "param description", + InputFactory.Parameter( "param", InputPrimitiveType.String, - RequestLocation.None, - defaultValue: new InputConstant("mockValue", InputPrimitiveType.String), - InputOperationParameterKind.Client, - isRequired: false, false, false, false, true, false, false, null, null), + location: RequestLocation.None, + kind: InputOperationParameterKind.Client, + isEndpoint: true, + defaultValue: InputFactory.Constant.String("mockValue")), New.Instance(KnownParameters.Endpoint.Type, Literal("mockvalue"))); } } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/EnumProviderSerializationTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/EnumProviderSerializationTests.cs index 9f889a98e5..9b3e83d3fa 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/EnumProviderSerializationTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/EnumProviderSerializationTests.cs @@ -8,6 +8,7 @@ using Microsoft.Generator.CSharp.Input; using Microsoft.Generator.CSharp.Providers; using Microsoft.Generator.CSharp.Statements; +using Microsoft.Generator.CSharp.Tests.Common; using NUnit.Framework; namespace Microsoft.Generator.CSharp.ClientModel.Tests.Providers @@ -24,24 +25,24 @@ private static object[] ValidateTypes(bool isExtensible) { var intValues = new List { - new InputEnumTypeValue("One", 1, null), - new InputEnumTypeValue("Two", 2, null) + InputFactory.EnumMember.Int32("One", 1), + InputFactory.EnumMember.Int32("Two", 2) }; - var intType = new InputEnumType("mockInputEnum", "mockNamespace", "public", null, "The mock enum", InputModelTypeUsage.Input | InputModelTypeUsage.Output, InputPrimitiveType.Int32, intValues, isExtensible); + var intType = InputFactory.Enum("mockInputEnum", InputPrimitiveType.Int32, isExtensible: isExtensible, values: intValues); var floatValues = new List { - new InputEnumTypeValue("One", 1f, null), - new InputEnumTypeValue("Two", 2f, null) + InputFactory.EnumMember.Float32("One", 1f), + InputFactory.EnumMember.Float32("Two", 2f) }; - var floatType = new InputEnumType("mockInputEnum", "mockNamespace", "public", null, "The mock enum", InputModelTypeUsage.Input | InputModelTypeUsage.Output, InputPrimitiveType.Float32, floatValues, isExtensible); + var floatType = InputFactory.Enum("mockInputEnum", InputPrimitiveType.Float32, isExtensible: isExtensible, values: floatValues); var stringValues = new List { - new InputEnumTypeValue("One", "1", null), - new InputEnumTypeValue("Two", "2", null) + InputFactory.EnumMember.String("One", "1"), + InputFactory.EnumMember.String("Two", "2") }; - var stringType = new InputEnumType("mockInputEnum", "mockNamespace", "public", null, "The mock enum", InputModelTypeUsage.Input | InputModelTypeUsage.Output, InputPrimitiveType.String, stringValues, isExtensible); + var stringType = InputFactory.Enum("mockInputEnum", InputPrimitiveType.String, isExtensible: isExtensible, values: stringValues); return [intType, floatType, stringType]; } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/MrwSerializationTypeDefinitions/JsonModelCoreTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/MrwSerializationTypeDefinitions/JsonModelCoreTests.cs index 2cd534cbe9..5e0021982d 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/MrwSerializationTypeDefinitions/JsonModelCoreTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/MrwSerializationTypeDefinitions/JsonModelCoreTests.cs @@ -1,12 +1,12 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -using System.Collections.Generic; using System.Linq; using Microsoft.Generator.CSharp.ClientModel.Providers; 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.MrwSerializationTypeDefinitions @@ -38,29 +38,8 @@ protected override MethodProvider[] BuildMethods() [Test] public void KebabCaseSerializedName() { - var inputModelProperty = new InputModelProperty( - "kebab-case", - "kebab-case", - "A property with kebab-case name", - InputPrimitiveType.String, - true, - false, - false); - var inputModel = new InputModelType( - "TestModel", - "TestModel", - "public", - null, - "Test model.", - InputModelTypeUsage.Input | InputModelTypeUsage.Output, - [inputModelProperty], - null, - [], - null, - null, - new Dictionary(), - null, - false); + var inputModelProperty = InputFactory.Property("kebab-case", InputPrimitiveType.String, wireName: "kebab-case", isRequired: true); + var inputModel = InputFactory.Model("TestModel", properties: [inputModelProperty]); var mrwProvider = new ModelProvider(inputModel).SerializationProviders.First(); var writer = new TypeProviderWriter(mrwProvider); @@ -71,29 +50,8 @@ public void KebabCaseSerializedName() [Test] public void SnakeCaseSerializedName() { - var inputModelProperty = new InputModelProperty( - "snake_case", - "snake_case", - "A property with snake_case name", - InputPrimitiveType.String, - true, - false, - false); - var inputModel = new InputModelType( - "TestModel", - "TestModel", - "public", - null, - "Test model.", - InputModelTypeUsage.Input | InputModelTypeUsage.Output, - [inputModelProperty], - null, - [], - null, - null, - new Dictionary(), - null, - false); + var inputModelProperty = InputFactory.Property("snake_case", InputPrimitiveType.String, wireName: "snake_case", isRequired: true); + var inputModel = InputFactory.Model("TestModel", properties: [inputModelProperty]); var mrwProvider = new ModelProvider(inputModel).SerializationProviders.First(); var writer = new TypeProviderWriter(mrwProvider); @@ -104,29 +62,8 @@ public void SnakeCaseSerializedName() [Test] public void PascalCaseSerializedName() { - var inputModelProperty = new InputModelProperty( - "PascalCase", - "PascalCase", - "A property with PascalCase name", - InputPrimitiveType.String, - true, - false, - false); - var inputModel = new InputModelType( - "TestModel", - "TestModel", - "public", - null, - "Test model.", - InputModelTypeUsage.Input | InputModelTypeUsage.Output, - [inputModelProperty], - null, - [], - null, - null, - new Dictionary(), - null, - false); + var inputModelProperty = InputFactory.Property("PascalCase", InputPrimitiveType.String, wireName: "PascalCase", isRequired: true); + var inputModel = InputFactory.Model("TestModel", properties: [inputModelProperty]); var mrwProvider = new ModelProvider(inputModel).SerializationProviders.First(); var writer = new TypeProviderWriter(mrwProvider); @@ -137,29 +74,8 @@ public void PascalCaseSerializedName() [Test] public void CamelCaseSerializedName() { - var inputModelProperty = new InputModelProperty( - "camelCase", - "camelCase", - "A property with camelCase name", - InputPrimitiveType.String, - true, - false, - false); - var inputModel = new InputModelType( - "TestModel", - "TestModel", - "public", - null, - "Test model.", - InputModelTypeUsage.Input | InputModelTypeUsage.Output, - [inputModelProperty], - null, - [], - null, - null, - new Dictionary(), - null, - false); + var inputModelProperty = InputFactory.Property("camelCase", InputPrimitiveType.String, wireName: "camelCase", isRequired: true); + var inputModel = InputFactory.Model("TestModel", properties: [inputModelProperty]); var mrwProvider = new ModelProvider(inputModel).SerializationProviders.First(); var writer = new TypeProviderWriter(mrwProvider); diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/MrwSerializationTypeDefinitions/MrwSerializationTypeDefinitionTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/MrwSerializationTypeDefinitions/MrwSerializationTypeDefinitionTests.cs index 0ffd7305f2..6464706075 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/MrwSerializationTypeDefinitions/MrwSerializationTypeDefinitionTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/MrwSerializationTypeDefinitions/MrwSerializationTypeDefinitionTests.cs @@ -13,6 +13,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.MrwSerializationTypeDefinitions @@ -40,7 +41,7 @@ internal static (ModelProvider Model, MrwSerializationTypeDefinition Serializati public void TestBuildImplements() { // mock the model type provider - var inputModel = new InputModelType("mockInputModel", "mockNamespace", "public", null, null, InputModelTypeUsage.Input | InputModelTypeUsage.Output, Array.Empty(), null, new List(), null, null, new Dictionary(), null, false); + var inputModel = InputFactory.Model("mockInputModel"); var (model, serialization) = CreateModelAndSerialization(inputModel); var interfaces = serialization.Implements; @@ -57,7 +58,7 @@ public void TestBuildImplements() [Test] public void TestBuildJsonModelWriteCoreMethod() { - var inputModel = new InputModelType("mockInputModel", "mockNamespace", "public", null, null, InputModelTypeUsage.Input | InputModelTypeUsage.Output, Array.Empty(), null, new List(), null, null, new Dictionary(), null, false); + var inputModel = InputFactory.Model("mockInputModel"); var (model, serialization) = CreateModelAndSerialization(inputModel); var method = serialization.BuildJsonModelWriteCoreMethod(); @@ -92,7 +93,7 @@ public void TestBuildJsonModelWriteCoreMethod() [Test] public void TestBuildJsonModelWriteMethodObjectDeclaration() { - var inputModel = new InputModelType("mockInputModel", "mockNamespace", "public", null, null, InputModelTypeUsage.Input | InputModelTypeUsage.Output, Array.Empty(), null, new List(), null, null, new Dictionary(), null, true); + var inputModel = InputFactory.Model("mockInputModel", modelAsStruct: true); var (model, serialization) = CreateModelAndSerialization(inputModel); var method = serialization.BuildJsonModelWriteMethodObjectDeclaration(); @@ -126,7 +127,7 @@ public void TestBuildJsonModelWriteMethodObjectDeclaration() [Test] public void TestBuildJsonModelCreateMethod() { - var inputModel = new InputModelType("mockInputModel", "mockNamespace", "public", null, null, InputModelTypeUsage.Input | InputModelTypeUsage.Output, Array.Empty(), null, new List(), null, null, new Dictionary(), null, false); + var inputModel = InputFactory.Model("mockInputModel"); var (model, serialization) = CreateModelAndSerialization(inputModel); var method = serialization.BuildJsonModelCreateMethod(); @@ -146,7 +147,7 @@ public void TestBuildJsonModelCreateMethod() [Test] public void TestBuildJsonModelCreateCoreMethod() { - var inputModel = new InputModelType("mockInputModel", "mockNamespace", "public", null, null, InputModelTypeUsage.Input | InputModelTypeUsage.Output, Array.Empty(), null, new List(), null, null, new Dictionary(), null, false); + var inputModel = InputFactory.Model("mockInputModel"); var (model, serialization) = CreateModelAndSerialization(inputModel); var method = serialization.BuildJsonModelCreateCoreMethod(); @@ -181,7 +182,7 @@ public void TestBuildJsonModelCreateCoreMethod() [Test] public void TestBuildJsonModelCreateMethodObjectDeclaration() { - var inputModel = new InputModelType("mockInputModel", "mockNamespace", "public", null, null, InputModelTypeUsage.Input | InputModelTypeUsage.Output, Array.Empty(), null, new List(), null, null, new Dictionary(), null, true); + var inputModel = InputFactory.Model("mockInputModel", modelAsStruct: true); var (model, serialization) = CreateModelAndSerialization(inputModel); var method = serialization.BuildJsonModelCreateMethodObjectDeclaration(); @@ -216,7 +217,7 @@ public void TestBuildJsonModelCreateMethodObjectDeclaration() [Test] public void TestBuildPersistableModelWriteMethod() { - var inputModel = new InputModelType("mockInputModel", "mockNamespace", "public", null, null, InputModelTypeUsage.Input | InputModelTypeUsage.Output, Array.Empty(), null, new List(), null, null, new Dictionary(), null, false); + var inputModel = InputFactory.Model("mockInputModel"); var (model, serialization) = CreateModelAndSerialization(inputModel); var method = serialization.BuildPersistableModelWriteMethod(); @@ -236,7 +237,7 @@ public void TestBuildPersistableModelWriteMethod() [Test] public void TestBuildPersistableModelWriteCoreMethod() { - var inputModel = new InputModelType("mockInputModel", "mockNamespace", "public", null, null, InputModelTypeUsage.Input | InputModelTypeUsage.Output, Array.Empty(), null, new List(), null, null, new Dictionary(), null, false); + var inputModel = InputFactory.Model("mockInputModel"); var (model, serialization) = CreateModelAndSerialization(inputModel); var method = serialization.BuildPersistableModelWriteCoreMethod(); @@ -271,7 +272,7 @@ public void TestBuildPersistableModelWriteCoreMethod() [Test] public void TestBuildPersistableModelWriteMethodObjectDeclaration() { - var inputModel = new InputModelType("mockInputModel", "mockNamespace", "public", null, null, InputModelTypeUsage.Input | InputModelTypeUsage.Output, Array.Empty(), null, new List(), null, null, new Dictionary(), null, true); + var inputModel = InputFactory.Model("mockInputModel", modelAsStruct: true); var (model, serialization) = CreateModelAndSerialization(inputModel); var method = serialization.BuildPersistableModelWriteMethodObjectDeclaration(); @@ -305,7 +306,7 @@ public void TestBuildPersistableModelWriteMethodObjectDeclaration() [Test] public void TestBuildPersistableModelCreateMethod() { - var inputModel = new InputModelType("mockInputModel", "mockNamespace", "public", null, null, InputModelTypeUsage.Input | InputModelTypeUsage.Output, Array.Empty(), null, new List(), null, null, new Dictionary(), null, false); + var inputModel = InputFactory.Model("mockInputModel"); var (model, serialization) = CreateModelAndSerialization(inputModel); var method = serialization.BuildPersistableModelCreateMethod(); @@ -335,7 +336,7 @@ public void TestBuildPersistableModelCreateMethod() [Test] public void TestBuildPersistableModelCreateCoreMethod() { - var inputModel = new InputModelType("mockInputModel", "mockNamespace", "public", null, null, InputModelTypeUsage.Input | InputModelTypeUsage.Output, Array.Empty(), null, new List(), null, null, new Dictionary(), null, false); + var inputModel = InputFactory.Model("mockInputModel"); var (model, serialization) = CreateModelAndSerialization(inputModel); Assert.IsNotNull(serialization); @@ -364,7 +365,7 @@ public void TestBuildPersistableModelCreateCoreMethod() [Test] public void BuildPersistableModelCreateMethodObjectDeclaration() { - var inputModel = new InputModelType("mockInputModel", "mockNamespace", "public", null, null, InputModelTypeUsage.Input | InputModelTypeUsage.Output, Array.Empty(), null, new List(), null, null, new Dictionary(), null, true); + var inputModel = InputFactory.Model("mockInputModel", modelAsStruct: true); var (model, serialization) = CreateModelAndSerialization(inputModel); var method = serialization.BuildPersistableModelCreateMethodObjectDeclaration(); @@ -398,7 +399,7 @@ public void BuildPersistableModelCreateMethodObjectDeclaration() [Test] public void TestBuildPersistableModelDeserializationMethod() { - var inputModel = new InputModelType("mockInputModel", "mockNamespace", "public", null, null, InputModelTypeUsage.Input | InputModelTypeUsage.Output, Array.Empty(), null, new List(), null, null, new Dictionary(), null, false); + var inputModel = InputFactory.Model("mockInputModel"); var (model, serialization) = CreateModelAndSerialization(inputModel); var method = serialization.BuildPersistableModelCreateMethod(); @@ -418,7 +419,7 @@ public void TestBuildPersistableModelDeserializationMethod() [Test] public void TestBuildPersistableModelGetFormatMethod() { - var inputModel = new InputModelType("mockInputModel", "mockNamespace", "public", null, null, InputModelTypeUsage.Input | InputModelTypeUsage.Output, Array.Empty(), null, new List(), null, null, new Dictionary(), null, false); + var inputModel = InputFactory.Model("mockInputModel"); var (model, serialization) = CreateModelAndSerialization(inputModel); var method = serialization.BuildPersistableModelGetFormatFromOptionsMethod(); @@ -441,7 +442,7 @@ public void TestBuildPersistableModelGetFormatMethod() [Test] public void TestBuildPersistableModelGetFormatMethodObjectDeclaration() { - var inputModel = new InputModelType("mockInputModel", "mockNamespace", "public", null, null, InputModelTypeUsage.Input | InputModelTypeUsage.Output, Array.Empty(), null, new List(), null, null, new Dictionary(), null, true); + var inputModel = InputFactory.Model("mockInputModel", modelAsStruct: true); var (model, serialization) = CreateModelAndSerialization(inputModel); var method = serialization.BuildPersistableModelGetFormatFromOptionsObjectDeclaration(); @@ -474,7 +475,7 @@ public void TestBuildPersistableModelGetFormatMethodObjectDeclaration() [Test] public void TestBuildFields() { - var inputModel = new InputModelType("mockInputModel", "mockNamespace", "public", null, null, InputModelTypeUsage.Input | InputModelTypeUsage.Output, Array.Empty(), null, new List(), null, null, new Dictionary(), null, false); + var inputModel = InputFactory.Model("mockInputModel"); var (_, serialization) = CreateModelAndSerialization(inputModel); var fields = serialization.Fields; @@ -487,13 +488,13 @@ public void TestBuildConstructor_ValidateConstructors() { var properties = new List { - new InputModelProperty("requiredString", "requiredString", "", InputPrimitiveType.String, true, false, false), - new InputModelProperty("OptionalInt", "optionalInt", "", InputPrimitiveType.Int32, false, false, false), - new InputModelProperty("requiredCollection", "requiredCollection", "", new InputArrayType("List", "TypeSpec.Array", InputPrimitiveType.String), true, false, false), - new InputModelProperty("requiredDictionary", "requiredDictionary", "", new InputDictionaryType("Dictionary", InputPrimitiveType.String, InputPrimitiveType.String), true, false, false), + InputFactory.Property("requiredString", InputPrimitiveType.String, isRequired: true), + InputFactory.Property("OptionalInt", InputPrimitiveType.Int32), + InputFactory.Property("requiredCollection", InputFactory.Array(InputPrimitiveType.String), isRequired: true), + InputFactory.Property("requiredDictionary", InputFactory.Dictionary(InputPrimitiveType.String), isRequired: true), }; - var inputModel = new InputModelType("TestModel", "TestModel", "public", null, "Test model.", InputModelTypeUsage.Input | InputModelTypeUsage.Output, properties, null, Array.Empty(), null, null, new Dictionary(), null, false); + var inputModel = InputFactory.Model("TestModel", properties: properties); var (_, serialization) = CreateModelAndSerialization(inputModel); var ctors = serialization.Constructors; @@ -509,7 +510,7 @@ public void TestBuildConstructor_ValidateConstructors() [Test] public void TestBuildDeserializationMethod() { - var inputModel = new InputModelType("mockInputModel", "mockNamespace", "public", null, null, InputModelTypeUsage.Input | InputModelTypeUsage.Output, Array.Empty(), null, new List(), null, null, new Dictionary(), null, false); + var inputModel = InputFactory.Model("mockInputModel"); var (model, serialization) = CreateModelAndSerialization(inputModel); var deserializationMethod = serialization.BuildDeserializationMethod(); @@ -531,7 +532,7 @@ public void TestBuildDeserializationMethod() [Test] public void TestBuildImplicitToBinaryContent() { - var inputModel = new InputModelType("mockInputModel", "mockNamespace", "public", null, null, InputModelTypeUsage.Input | InputModelTypeUsage.Output, Array.Empty(), null, new List(), null, null, new Dictionary(), null, false); + var inputModel = InputFactory.Model("mockInputModel"); var (model, serialization) = CreateModelAndSerialization(inputModel); var methods = serialization.Methods; @@ -559,7 +560,7 @@ public void TestBuildImplicitToBinaryContent() [Test] public void TestBuildExplicitFromClientResult() { - var inputModel = new InputModelType("mockInputModel", "mockNamespace", "public", null, null, InputModelTypeUsage.Input | InputModelTypeUsage.Output, Array.Empty(), null, new List(), null, null, new Dictionary(), null, false); + var inputModel = InputFactory.Model("mockInputModel"); var (model, serialization) = CreateModelAndSerialization(inputModel); var methods = serialization.Methods; diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/MrwSerializationTypeDefinitions/PersistableModelCoreTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/MrwSerializationTypeDefinitions/PersistableModelCoreTests.cs index 7f95019d94..4a421cda84 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/MrwSerializationTypeDefinitions/PersistableModelCoreTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/MrwSerializationTypeDefinitions/PersistableModelCoreTests.cs @@ -7,6 +7,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.MrwSerializationTypeDefinitions @@ -38,9 +39,8 @@ protected override MethodProvider[] BuildMethods() [Test] public void TestBuildPersistableModelCreateCoreMethod_DerivedType() { - var inputBase = new InputModelType("mockBaseModel", "mockNamespace", "public", null, null, InputModelTypeUsage.Input | InputModelTypeUsage.Output, [], null, new List(), null, null, new Dictionary(), null, false); - var inputDerived = new InputModelType("mockDerivedModel", "mockNamespace", "public", null, null, InputModelTypeUsage.Input | InputModelTypeUsage.Output, - [], inputBase, new List(), null, null, new Dictionary(), null, false); + var inputBase = InputFactory.Model("mockBaseModel"); + var inputDerived = InputFactory.Model("mockDerivedModel", baseModel: inputBase); ((List)inputBase.DerivedModels).Add(inputDerived); var (baseModel, baseSerialization) = MrwSerializationTypeDefinitionTests.CreateModelAndSerialization(inputBase); var (derivedModel, derivedSerialization) = MrwSerializationTypeDefinitionTests.CreateModelAndSerialization(inputDerived); diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/RestClientProviderTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/RestClientProviderTests.cs index 58690865aa..c84ab0f86f 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/RestClientProviderTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/RestClientProviderTests.cs @@ -7,6 +7,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 @@ -96,31 +97,14 @@ public void ValidateProperties() Assert.IsFalse(pipelineMessageClassifier2xxAnd4xx.Body.HasSetter); } - private readonly static InputOperation BasicOperation = new InputOperation( - name: "CreateMessage", - resourceName: null, - deprecated: null, - description: string.Empty, - accessibility: null, + private readonly static InputOperation BasicOperation = InputFactory.Operation( + "CreateMessage", parameters: [ - new InputParameter("message", "message", "The message to create.", InputPrimitiveType.Boolean, RequestLocation.Body, null, InputOperationParameterKind.Method, true, false, false, false, false, false, false, null, null) - ], - responses: [new OperationResponse([200], null, BodyMediaType.Json, [], false, ["application/json"])], - httpMethod: "GET", - requestBodyMediaType: BodyMediaType.Json, - uri: "localhost", - path: "/api/messages", - externalDocsUrl: null, - requestMediaTypes: null, - bufferResponse: false, - longRunning: null, - paging: null, - generateProtocolMethod: true, - generateConvenienceMethod: true, - crossLanguageDefinitionId: "TestService.CreateMessage"); - - private readonly static InputClient SingleOpInputClient = new InputClient("TestClient", "TestClient description", [BasicOperation], [], null); + InputFactory.Parameter("message", InputPrimitiveType.Boolean, isRequired: true) + ]); + + private readonly static InputClient SingleOpInputClient = InputFactory.Client("TestClient", operations: [BasicOperation]); private static IEnumerable DefaultCSharpMethodCollectionTestCases => [ diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/ScmMethodProviderCollectionTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/ScmMethodProviderCollectionTests.cs index 9336fa97e9..ee8777147e 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/ScmMethodProviderCollectionTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/ScmMethodProviderCollectionTests.cs @@ -1,12 +1,12 @@ // 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.Primitives; using Microsoft.Generator.CSharp.Providers; +using Microsoft.Generator.CSharp.Tests.Common; using NUnit.Framework; namespace Microsoft.Generator.CSharp.ClientModel.Tests.Providers @@ -17,7 +17,7 @@ internal class ScmMethodProviderCollectionTests [TestCaseSource(nameof(DefaultCSharpMethodCollectionTestCases))] public void TestDefaultCSharpMethodCollection(InputOperation inputOperation) { - var inputClient = new InputClient("TestClient", "TestClient description", [inputOperation], [], null); + var inputClient = InputFactory.Client("TestClient", operations: [inputOperation]); MockHelpers.LoadMockPlugin( createCSharpTypeCore: (inputType) => new CSharpType(typeof(bool)), @@ -41,46 +41,15 @@ public static IEnumerable DefaultCSharpMethodCollectionTestCases { get { - yield return new TestCaseData(new InputOperation( - name: "CreateMessage", - resourceName: null, - deprecated: null, - description: string.Empty, - accessibility: null, + yield return new TestCaseData(InputFactory.Operation( + "CreateMessage", parameters: [ - new InputParameter( + InputFactory.Parameter( "message", - "message", - "The message to create.", InputPrimitiveType.Boolean, - RequestLocation.Body, - null, - InputOperationParameterKind.Method, - true, - false, - false, - false, - false, - false, - false, - null, - null) - ], - responses: [new OperationResponse([200], null, BodyMediaType.Json, [], false, ["application/json"])], - httpMethod: "GET", - requestBodyMediaType: BodyMediaType.Json, - uri: "localhost", - path: "/api/messages", - externalDocsUrl: null, - requestMediaTypes: null, - bufferResponse: false, - longRunning: null, - paging: null, - generateProtocolMethod: true, - generateConvenienceMethod: true, - crossLanguageDefinitionId: "TestService.CreateMessage" - )); + isRequired: true) + ])); } } } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/TestHelpers/TestClientTypeProvider.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/TestHelpers/TestClientTypeProvider.cs index 3d7fd26688..67fb8573b9 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/TestHelpers/TestClientTypeProvider.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/TestHelpers/TestClientTypeProvider.cs @@ -2,7 +2,7 @@ // Licensed under the MIT License. using Microsoft.Generator.CSharp.ClientModel.Providers; -using Microsoft.Generator.CSharp.Input; +using Microsoft.Generator.CSharp.Tests.Common; namespace Microsoft.Generator.CSharp.ClientModel.Tests { @@ -10,7 +10,7 @@ internal class TestClientTypeProvider : ClientProvider { public static readonly ClientProvider Empty = new TestClientTypeProvider(); - public TestClientTypeProvider() : base(new InputClient("TestClient", "TestClient description", [], [], null)) + public TestClientTypeProvider() : base(InputFactory.Client("TestClient")) { } } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.sln b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.sln index a1f7859572..159521d4f7 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.sln +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.sln @@ -33,6 +33,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestProjects.CadlRanch.Test EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Generator.CSharp.ClientModel.StubLibrary", "Microsoft.Generator.CSharp.ClientModel.StubLibrary\src\Microsoft.Generator.CSharp.ClientModel.StubLibrary.csproj", "{178CB351-5587-42CC-8EB7-C5C208E84BEB}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Generator.CSharp.Tests.Common", "Microsoft.Generator.CSharp\test\common\Microsoft.Generator.CSharp.Tests.Common.csproj", "{EA126741-2BE5-4CA4-BB76-DFE9B741653E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -87,6 +89,10 @@ Global {178CB351-5587-42CC-8EB7-C5C208E84BEB}.Debug|Any CPU.Build.0 = Debug|Any CPU {178CB351-5587-42CC-8EB7-C5C208E84BEB}.Release|Any CPU.ActiveCfg = Release|Any CPU {178CB351-5587-42CC-8EB7-C5C208E84BEB}.Release|Any CPU.Build.0 = Release|Any CPU + {EA126741-2BE5-4CA4-BB76-DFE9B741653E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EA126741-2BE5-4CA4-BB76-DFE9B741653E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EA126741-2BE5-4CA4-BB76-DFE9B741653E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EA126741-2BE5-4CA4-BB76-DFE9B741653E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/CSharpGenTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/CSharpGenTests.cs index b1b6d6998a..5af406bb08 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/CSharpGenTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/CSharpGenTests.cs @@ -2,13 +2,8 @@ // Licensed under the MIT License. using System; -using System.Collections.Generic; using System.IO; -using System.Threading.Tasks; -using Microsoft.Generator.CSharp.Input; -using Microsoft.Generator.CSharp.Primitives; using Moq; -using Moq.Protected; using NUnit.Framework; namespace Microsoft.Generator.CSharp.Tests diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Expressions/NewInstanceExpressionTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Expressions/NewInstanceExpressionTests.cs index 892286ec4d..7bf2b35a4c 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Expressions/NewInstanceExpressionTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Expressions/NewInstanceExpressionTests.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using Microsoft.Generator.CSharp.Expressions; using Microsoft.Generator.CSharp.Input; +using Microsoft.Generator.CSharp.Tests.Common; using NUnit.Framework; using static Microsoft.Generator.CSharp.Snippets.Snippet; @@ -44,7 +45,11 @@ public void ValidateAnonymousWithPropertiesSingleLine() [Test] public void ValidateNullableValueType() { - InputEnumType enumType = new InputEnumType("MyEnum", "MyEnum", "public", null, "MyEnum", InputModelTypeUsage.Input | InputModelTypeUsage.Output, InputPrimitiveType.String, [new InputEnumTypeValue("One", "one", null), new InputEnumTypeValue("Two", "two", null)], true); + InputEnumType enumType = InputFactory.Enum("MyEnum", InputPrimitiveType.String, isExtensible: true, values: + [ + InputFactory.EnumMember.Int32("One", 1), + InputFactory.EnumMember.Int32("Two", 2) + ]); var provider = CodeModelPlugin.Instance.TypeFactory.CreateEnum(enumType); Assert.NotNull(provider); var expr = New.Instance(provider!.Type, Literal("three")); diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/InputLibraryVisitorTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/InputLibraryVisitorTests.cs index 994fab02df..478874da4a 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/InputLibraryVisitorTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/InputLibraryVisitorTests.cs @@ -2,9 +2,9 @@ // Licensed under the MIT License. using System; -using System.Collections.Generic; using Microsoft.Generator.CSharp.Input; using Microsoft.Generator.CSharp.Providers; +using Microsoft.Generator.CSharp.Tests.Common; using Moq; using Moq.Protected; using NUnit.Framework; @@ -32,18 +32,10 @@ public void Setup() public void PreVisitsProperties() { _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(), null, false); - - _mockInputLibrary.Setup(l => l.InputNamespace).Returns(new InputNamespace( - "test library", - new List(), - new List(), - new List { inputModel }, - new List(), - new InputAuth())); + var inputModelProperty = InputFactory.Property("prop1", InputPrimitiveType.Any, true, true); + var inputModel = InputFactory.Model("foo", "internal", usage: InputModelTypeUsage.Input, properties: [inputModelProperty]); + + _mockInputLibrary.Setup(l => l.InputNamespace).Returns(InputFactory.Namespace("test library", models: [inputModel])); _mockVisitor.Object.Visit(_mockPlugin.Object.OutputLibrary); @@ -55,22 +47,11 @@ public void PreVisitsProperties() public void PreVisitsEnum() { _mockPlugin.Object.AddVisitor(_mockVisitor.Object); - var inputEnum = new InputEnumType("enum", "id", "desc", null, "description", InputModelTypeUsage.Input, InputPrimitiveType.Int32, new[] - { - new InputEnumTypeValue("value", 1, "desc") - }, false); - var inputModelProperty = - new InputModelProperty("prop1", "prop1", "string", inputEnum, true, true, false); - var inputModel = new InputModelType("foo", "id", "desc", "internal", "description", - InputModelTypeUsage.Input, [inputModelProperty], null, [], null, null, new Dictionary(), null, false); - - _mockInputLibrary.Setup(l => l.InputNamespace).Returns(new InputNamespace( - "test library", - new List(), - new List(), - new List { inputModel }, - new List(), - new InputAuth())); + var inputEnum = InputFactory.Enum("enum", InputPrimitiveType.Int32, usage: InputModelTypeUsage.Input, values: [InputFactory.EnumMember.Int32("value", 1)]); + var inputModelProperty = InputFactory.Property("prop1", inputEnum, true, true); + var inputModel = InputFactory.Model("foo", "internal", usage: InputModelTypeUsage.Input, properties: [inputModelProperty]); + + _mockInputLibrary.Setup(l => l.InputNamespace).Returns(InputFactory.Namespace("test library", models: [inputModel])); _mockVisitor.Object.Visit(_mockPlugin.Object.OutputLibrary); @@ -82,23 +63,14 @@ public void PreVisitsEnum() [Test] public void RemovedInputModelCausesExceptionWhenReferencedInDifferentModel() { - var inputModel1Property = - new InputModelProperty("prop1", "prop1", "string", new InputPrimitiveType(InputPrimitiveTypeKind.Any, "foo", "bar"), true, true, false); - var inputModel1 = new InputModelType("Model1", "id", "desc", "internal", "description", - InputModelTypeUsage.Input, [inputModel1Property], null, [], null, null, new Dictionary(), null, false); + var inputModel1Property = InputFactory.Property("prop1", InputPrimitiveType.Any, true, true); + var inputModel1 = InputFactory.Model("Model1", "internal", usage: InputModelTypeUsage.Input, properties: [inputModel1Property]); - var inputModel2Property = new InputModelProperty("prop2", "prop1", "string", inputModel1, true, true, false); + var inputModel2Property = InputFactory.Property("prop2", inputModel1, true, true); - var inputModel2 = new InputModelType("Model2", "id", "desc", "internal", "description", - InputModelTypeUsage.Input, [inputModel2Property], null, [], null, null, new Dictionary(), null, false); + var inputModel2 = InputFactory.Model("Model2", "internal", usage: InputModelTypeUsage.Input, properties: [inputModel2Property]); - _mockInputLibrary.Setup(l => l.InputNamespace).Returns(new InputNamespace( - "test library", - new List(), - new List(), - new List { inputModel1, inputModel2 }, - new List(), - new InputAuth())); + _mockInputLibrary.Setup(l => l.InputNamespace).Returns(InputFactory.Namespace("test library", models: [inputModel1, inputModel2])); var visitor = new PreVisitor(); _mockPlugin.Object.AddVisitor(visitor); @@ -108,23 +80,14 @@ public void RemovedInputModelCausesExceptionWhenReferencedInDifferentModel() [Test] public void CanCleanUpRemovedReferencesToRemovedModels() { - var inputModel1Property = - new InputModelProperty("prop1", "prop1", "string", new InputPrimitiveType(InputPrimitiveTypeKind.Any, "foo", "bar"), true, true, false); - var inputModel1 = new InputModelType("Model1", "id", "desc", "internal", "description", - InputModelTypeUsage.Input, [inputModel1Property], null, [], null, null, new Dictionary(), null, false); - - var inputModel2Property = new InputModelProperty("prop2", "prop1", "string", inputModel1, true, true, false); - - var inputModel2 = new InputModelType("Model2", "id", "desc", "internal", "description", - InputModelTypeUsage.Input, [inputModel2Property], null, [], null, null, new Dictionary(), null, false); - - _mockInputLibrary.Setup(l => l.InputNamespace).Returns(new InputNamespace( - "test library", - new List(), - new List(), - new List { inputModel1, inputModel2 }, - new List(), - new InputAuth())); + var inputModel1Property = InputFactory.Property("prop1", InputPrimitiveType.Any, true, true); + var inputModel1 = InputFactory.Model("Model1", "internal", usage: InputModelTypeUsage.Input, properties: [inputModel1Property]); + + var inputModel2Property = InputFactory.Property("prop2", inputModel1, true, true); + + var inputModel2 = InputFactory.Model("Model2", "internal", usage: InputModelTypeUsage.Input, properties: [inputModel2Property]); + + _mockInputLibrary.Setup(l => l.InputNamespace).Returns(InputFactory.Namespace("test library", models: [inputModel1, inputModel2])); var visitor = new PreVisitor(true); _mockPlugin.Object.AddVisitor(visitor); diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Microsoft.Generator.CSharp.Tests.csproj b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Microsoft.Generator.CSharp.Tests.csproj index 87d5eb65c2..8ef3e4cbfd 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Microsoft.Generator.CSharp.Tests.csproj +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Microsoft.Generator.CSharp.Tests.csproj @@ -2,6 +2,7 @@ + @@ -10,6 +11,7 @@ + diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/EnumProviderTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/EnumProviderTests.cs index 4fba038692..212934c011 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/EnumProviderTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/EnumProviderTests.cs @@ -8,6 +8,7 @@ using Microsoft.Generator.CSharp.Primitives; using Microsoft.Generator.CSharp.Providers; using Microsoft.Generator.CSharp.Snippets; +using Microsoft.Generator.CSharp.Tests.Common; using NUnit.Framework; namespace Microsoft.Generator.CSharp.Tests.Providers @@ -22,7 +23,11 @@ public void BuildEnumType_ValidateIntBasedFixedEnum() { MockHelpers.LoadMockPlugin(createCSharpTypeCore: (inputType) => typeof(int)); - var input = new InputEnumType("mockInputEnum", "mockNamespace", "public", null, "The mock enum", InputModelTypeUsage.Input | InputModelTypeUsage.Output, InputPrimitiveType.Int32, [new InputEnumTypeValue("One", 1, null), new InputEnumTypeValue("Two", 2, null)], false); + var input = InputFactory.Enum("mockInputEnum", InputPrimitiveType.Int32, values: + [ + InputFactory.EnumMember.Int32("One", 1), + InputFactory.EnumMember.Int32("Two", 2) + ]); var enumType = EnumProvider.Create(input); var fields = enumType.Fields; @@ -43,7 +48,11 @@ public void BuildEnumType_ValidateFloatBasedFixedEnum() { MockHelpers.LoadMockPlugin(createCSharpTypeCore: (inputType) => typeof(float)); - var input = new InputEnumType("mockInputEnum", "mockNamespace", "public", null, "The mock enum", InputModelTypeUsage.Input | InputModelTypeUsage.Output, InputPrimitiveType.Float32, [new InputEnumTypeValue("One", 1f, null), new InputEnumTypeValue("Two", 2f, null)], false); + var input = InputFactory.Enum("mockInputEnum", InputPrimitiveType.Float32, values: + [ + InputFactory.EnumMember.Float32("One", 1f), + InputFactory.EnumMember.Float32("Two", 2f) + ]); var enumType = EnumProvider.Create(input); var fields = enumType.Fields; @@ -61,7 +70,11 @@ public void BuildEnumType_ValidateStringBasedFixedEnum() { MockHelpers.LoadMockPlugin(createCSharpTypeCore: (inputType) => typeof(string)); - var input = new InputEnumType("mockInputEnum", "mockNamespace", "public", null, "The mock enum", InputModelTypeUsage.Input | InputModelTypeUsage.Output, InputPrimitiveType.String, [new InputEnumTypeValue("One", "1", null), new InputEnumTypeValue("Two", "2", null)], false); + var input = InputFactory.Enum("mockInputEnum", InputPrimitiveType.String, values: + [ + InputFactory.EnumMember.String("One", "1"), + InputFactory.EnumMember.String("Two", "2") + ]); var enumType = EnumProvider.Create(input); var fields = enumType.Fields; @@ -80,7 +93,11 @@ public void BuildEnumType_ValidateApiVersionEnum() MockHelpers.LoadMockPlugin(createCSharpTypeCore: (inputType) => typeof(string)); string[] apiVersions = ["2024-07-16", "2024-07-17"]; - var input = new InputEnumType("mockInputEnum", "mockNamespace", "public", null, "The mock enum", InputModelTypeUsage.ApiVersionEnum, InputPrimitiveType.String, [new InputEnumTypeValue(apiVersions[0], 1, null), new InputEnumTypeValue(apiVersions[1], 2, null)], false); + var input = InputFactory.Enum("mockInputEnum", InputPrimitiveType.Int32, usage: InputModelTypeUsage.ApiVersionEnum, values: + [ + InputFactory.EnumMember.Int32(apiVersions[0], 1), + InputFactory.EnumMember.Int32(apiVersions[1], 2) + ]); var enumType = EnumProvider.Create(input); var fields = enumType.Fields; @@ -98,7 +115,11 @@ public void BuildEnumType_ValidateIntBasedExtensibleEnum() { MockHelpers.LoadMockPlugin(createCSharpTypeCore: (inputType) => typeof(int)); - var input = new InputEnumType("mockInputEnum", "mockNamespace", "public", null, "The mock enum", InputModelTypeUsage.Input | InputModelTypeUsage.Output, InputPrimitiveType.Int32, [new InputEnumTypeValue("One", 1, null), new InputEnumTypeValue("Two", 2, null)], true); + var input = InputFactory.Enum("mockInputEnum", InputPrimitiveType.Int32, isExtensible: true, values: + [ + InputFactory.EnumMember.Int32("One", 1), + InputFactory.EnumMember.Int32("Two", 2) + ]); var enumType = EnumProvider.Create(input); var fields = enumType.Fields; var properties = enumType.Properties; @@ -136,7 +157,11 @@ public void BuildEnumType_ValidateFloatBasedExtensibleEnum() { MockHelpers.LoadMockPlugin(createCSharpTypeCore: (inputType) => typeof(float)); - var input = new InputEnumType("mockInputEnum", "mockNamespace", "public", null, "The mock enum", InputModelTypeUsage.Input | InputModelTypeUsage.Output, InputPrimitiveType.Float32, [new InputEnumTypeValue("One", 1f, null), new InputEnumTypeValue("Two", 2f, null)], true); + var input = InputFactory.Enum("mockInputEnum", InputPrimitiveType.Float32, isExtensible: true, values: + [ + InputFactory.EnumMember.Float32("One", 1f), + InputFactory.EnumMember.Float32("Two", 2f) + ]); var enumType = EnumProvider.Create(input); var fields = enumType.Fields; var properties = enumType.Properties; @@ -174,7 +199,11 @@ public void BuildEnumType_ValidateStringBasedExtensibleEnum() { MockHelpers.LoadMockPlugin(createCSharpTypeCore: (inputType) => typeof(string)); - var input = new InputEnumType("mockInputEnum", "mockNamespace", "public", null, "The mock enum", InputModelTypeUsage.Input | InputModelTypeUsage.Output, InputPrimitiveType.String, [new InputEnumTypeValue("One", "1", null), new InputEnumTypeValue("Two", "2", null)], true); + var input = InputFactory.Enum("mockInputEnum", InputPrimitiveType.String, isExtensible: true, values: + [ + InputFactory.EnumMember.String("One", "1"), + InputFactory.EnumMember.String("Two", "2") + ]); var enumType = EnumProvider.Create(input); var fields = enumType.Fields; var properties = enumType.Properties; diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/ModelFactoryProviderTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/ModelFactoryProviderTests.cs index cde85bb99b..3f6fa253fa 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/ModelFactoryProviderTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/ModelFactoryProviderTests.cs @@ -7,13 +7,13 @@ 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.Tests.Providers { public class ModelFactoryProviderTests { - private static readonly InputType StringInputType = new InputPrimitiveType(InputPrimitiveTypeKind.String, "string", "string"); private static readonly InputModelType[] ModelList = GetTestModels(); public ModelFactoryProviderTests() @@ -83,65 +83,17 @@ public void ModelFactoryName() private static InputModelType[] GetTestModels() { + InputModelProperty[] properties = + [ + InputFactory.Property("StringProp", InputPrimitiveType.String), + InputFactory.Property("ListProp", InputFactory.Array(InputPrimitiveType.String)), + InputFactory.Property("DictProp", InputFactory.Dictionary(InputPrimitiveType.String, InputPrimitiveType.String)) + ]; return [ - new InputModelType( - "InternalModel", - "InternalModel", - "internal", - null, - "InternalModel", - InputModelTypeUsage.Input | InputModelTypeUsage.Output, - [ - new InputModelProperty("StringProp", "stringProp", "StringProp", StringInputType, false, false, false, null), - new InputModelProperty("ListProp", "listProp", "ListProp", new InputArrayType("list", "list", StringInputType), false, false, false, null), - new InputModelProperty("DictProp", "DictProp", "DictProp", new InputDictionaryType("dict", StringInputType, StringInputType), false, false, false, null) - ], - null, - [], - null, - null, - new Dictionary(), - null, - false), - new InputModelType( - "PublicModel1", - "PublicModel1", - "public1", - null, - "PublicModel1", - InputModelTypeUsage.Input | InputModelTypeUsage.Output, - [ - new InputModelProperty("StringProp", "stringProp", "StringProp", StringInputType, false, false, false, null), - new InputModelProperty("ListProp", "listProp", "ListProp", new InputArrayType("list", "list", StringInputType), false, false, false, null), - new InputModelProperty("DictProp", "DictProp", "DictProp", new InputDictionaryType("dict", StringInputType, StringInputType), false, false, false, null) - ], - null, - [], - null, - null, - new Dictionary(), - null, - false), - new InputModelType( - "PublicModel2", - "PublicModel2", - "public2", - null, - "PublicModel2", - InputModelTypeUsage.Input | InputModelTypeUsage.Output, - [ - new InputModelProperty("StringProp", "stringProp", "StringProp", StringInputType, false, false, false, null), - new InputModelProperty("ListProp", "listProp", "ListProp", new InputArrayType("list", "list", StringInputType), false, false, false, null), - new InputModelProperty("DictProp", "DictProp", "DictProp", new InputDictionaryType("dict", StringInputType, StringInputType), false, false, false, null) - ], - null, - [], - null, - null, - new Dictionary(), - null, - false) + InputFactory.Model("InternalModel", "internal", properties: properties), + InputFactory.Model("PublicModel1", properties: properties), + InputFactory.Model("PublicModel2", properties: properties) ]; } } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/ModelProviderTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/ModelProviderTests.cs index 4384435efc..941b03be2e 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/ModelProviderTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/ModelProviderTests.cs @@ -7,6 +7,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.Tests.Providers @@ -24,7 +25,7 @@ public void BuildProperties_ValidatePropertySetters(InputModelProperty inputMode inputModelProperty }; - var inputModel = new InputModelType("mockInputModel", "mockNamespace", "public", null, null, InputModelTypeUsage.Input | InputModelTypeUsage.Output, props, null, [], null, null, new Dictionary(), null, false); + var inputModel = InputFactory.Model("mockInputModel", properties: props); var modelTypeProvider = new ModelProvider(inputModel); var properties = modelTypeProvider.Properties; @@ -46,42 +47,42 @@ public static IEnumerable BuildProperties_ValidatePropertySettersT { // list property yield return new TestCaseData( - new InputModelProperty("prop1", "prop1", "public", new InputArrayType("mockProp", "TypeSpec.Array", InputPrimitiveType.String), false, false, false), + InputFactory.Property("prop1", InputFactory.Array(InputPrimitiveType.String)), new CSharpType(typeof(IList)), false); // read only list property yield return new TestCaseData( - new InputModelProperty("prop1", "prop1", "public", new InputArrayType("mockProp", "TypeSpec.Array", InputPrimitiveType.String), false, true, false), + InputFactory.Property("prop1", InputFactory.Array(InputPrimitiveType.String), isReadOnly: true), new CSharpType(typeof(IReadOnlyList)), false); // nullable list property yield return new TestCaseData( - new InputModelProperty("prop1", "prop1", "public", new InputArrayType("mockProp", "TypeSpec.Array", InputPrimitiveType.String), false, false, false), + InputFactory.Property("prop1", InputFactory.Array(InputPrimitiveType.String)), new CSharpType(typeof(IList), true), true); // dictionary property yield return new TestCaseData( - new InputModelProperty("prop1", "prop1", "public", new InputDictionaryType("mockProp", InputPrimitiveType.String, InputPrimitiveType.String), false, false, false), + InputFactory.Property("prop1", InputFactory.Dictionary(InputPrimitiveType.String)), new CSharpType(typeof(IDictionary)), false); // nullable dictionary property yield return new TestCaseData( - new InputModelProperty("prop1", "prop1", "public", new InputDictionaryType("mockProp", InputPrimitiveType.String, InputPrimitiveType.String), false, false, false), + InputFactory.Property("prop1", InputFactory.Dictionary(InputPrimitiveType.String)), new CSharpType(typeof(IDictionary), true), true); // primitive type property yield return new TestCaseData( - new InputModelProperty("prop1", "prop1", "public", InputPrimitiveType.String, false, false, false), + InputFactory.Property("prop1", InputPrimitiveType.String), new CSharpType(typeof(string)), true); // read only primitive type property yield return new TestCaseData( - new InputModelProperty("prop1", "prop1", "public", InputPrimitiveType.String, false, true, false), + InputFactory.Property("prop1", InputPrimitiveType.String, isReadOnly: true), new CSharpType(typeof(string)), false); // readonlymemory property yield return new TestCaseData( - new InputModelProperty("prop1", "prop1", "public", new InputArrayType("mockProp", "TypeSpec.Array", InputPrimitiveType.String), false, false, false), + InputFactory.Property("prop1", InputFactory.Array(InputPrimitiveType.String)), new CSharpType(typeof(ReadOnlyMemory<>)), true); } @@ -104,12 +105,13 @@ public static IEnumerable BuildProperties_ValidatePropertySettersT [Test] public void TestBuildConstructor_ValidateConstructors() { - var properties = new List{ - new InputModelProperty("requiredString", "requiredString", "", InputPrimitiveType.String, true, false, false), - new InputModelProperty("OptionalInt", "optionalInt", "", InputPrimitiveType.Int32, false, false, false), - new InputModelProperty("requiredCollection", "requiredCollection", "", new InputArrayType("List", "TypeSpec.Array", InputPrimitiveType.String), true, false, false), - new InputModelProperty("requiredDictionary", "requiredDictionary", "", new InputDictionaryType("Dictionary", InputPrimitiveType.String, InputPrimitiveType.String), true, false, false), - new InputModelProperty("optionalUnknown", "optional unknown", "", InputPrimitiveType.Any, false, false, false), + var properties = new List + { + InputFactory.Property("requiredString", InputPrimitiveType.String, isRequired: true), + InputFactory.Property("OptionalInt", InputPrimitiveType.Int32), + InputFactory.Property("requiredCollection", InputFactory.Array(InputPrimitiveType.String), isRequired: true), + InputFactory.Property("requiredDictionary", InputFactory.Dictionary(InputPrimitiveType.String), isRequired: true), + InputFactory.Property("optionalUnknown", InputPrimitiveType.Any) }; MockHelpers.LoadMockPlugin(createCSharpTypeCore: (InputType inputType) => @@ -126,7 +128,7 @@ public void TestBuildConstructor_ValidateConstructors() } }); - var inputModel = new InputModelType("TestModel", "TestModel", "public", null, "Test model.", InputModelTypeUsage.Input | InputModelTypeUsage.Output, properties, null, Array.Empty(), null, null, new Dictionary(), null, false); + var inputModel = InputFactory.Model("TestModel", properties: properties); var modelTypeProvider = new ModelProvider(inputModel); var ctors = modelTypeProvider.Constructors; @@ -149,16 +151,16 @@ public void TestBuildConstructor_ValidateConstructorsInDerivedModel() { var baseProperties = new List { - new InputModelProperty("prop1", "prop1", string.Empty, InputPrimitiveType.String, true, false, false), - new InputModelProperty("prop2", "prop2", string.Empty, InputPrimitiveType.String, false, false, false), + InputFactory.Property("prop1", InputPrimitiveType.String, isRequired: true), + InputFactory.Property("prop2", InputPrimitiveType.String) }; var derivedProperties = new List { - new InputModelProperty("prop3", "prop3", string.Empty, InputPrimitiveType.String, true, false, false), - new InputModelProperty("prop4", "prop4", string.Empty, InputPrimitiveType.String, false, false, false), + InputFactory.Property("prop3", InputPrimitiveType.String, isRequired: true), + InputFactory.Property("prop4", InputPrimitiveType.String) }; - var inputBase = new InputModelType("baseModel", "baseModel", null, null, null, InputModelTypeUsage.Input, baseProperties, null, new List(), null, null, new Dictionary(), null, false); - var inputDerived = new InputModelType("derivedModel", "derivedModel", null, null, null, InputModelTypeUsage.Input, derivedProperties, inputBase, new List(), null, null, new Dictionary(), null, false); + var inputBase = InputFactory.Model("baseModel", usage: InputModelTypeUsage.Input, properties: baseProperties); + var inputDerived = InputFactory.Model("derivedModel", usage: InputModelTypeUsage.Input, properties: derivedProperties, baseModel: inputBase); ((List)inputBase.DerivedModels).Add(inputDerived); MockHelpers.LoadMockPlugin(); @@ -223,7 +225,7 @@ public void TestBuildConstructor_ValidateConstructorsInDerivedModel() [Test] public void TestBuildSecondaryConstructor() { - var inputModel = new InputModelType("TestModel", "TestModel", "public", null, "Test model.", InputModelTypeUsage.Input | InputModelTypeUsage.Output, [], null, Array.Empty(), null, null, new Dictionary(), null, false); + var inputModel = InputFactory.Model("TestModel", properties: []); var modelTypeProvider = new ModelProvider(inputModel); var secondaryConstructor = modelTypeProvider.Constructors.FirstOrDefault(c => c.Signature.Modifiers.HasFlag(MethodSignatureModifiers.Internal)); @@ -240,12 +242,12 @@ public void TestBuildSecondaryConstructor() [Test] public void BuildBaseType() { - var inputBase = new InputModelType("baseModel", "baseModel", null, null, null, InputModelTypeUsage.Input, [], null, new List(), null, null, new Dictionary(), null, false); - var inputDerived = new InputModelType("derivedModel", "derivedModel", null, null, null, InputModelTypeUsage.Input, [], inputBase, new List(), null, null, new Dictionary(), null, false); - ((List)inputBase.DerivedModels).Add(inputDerived); - MockHelpers.LoadMockPlugin(); + var inputBase = InputFactory.Model("baseModel", usage: InputModelTypeUsage.Input, properties: []); + var inputDerived = InputFactory.Model("derivedModel", usage: InputModelTypeUsage.Input, properties: [], baseModel: inputBase); + ((List)inputBase.DerivedModels).Add(inputDerived); + var baseModel = CodeModelPlugin.Instance.TypeFactory.CreateModel(inputBase); var derivedModel = CodeModelPlugin.Instance.TypeFactory.CreateModel(inputDerived); @@ -257,8 +259,8 @@ public void BuildModelAsStruct() { var properties = new List { - new InputModelProperty("requiredString", "requiredString", "", InputPrimitiveType.String, true, false, false), - new InputModelProperty("OptionalInt", "optionalInt", "", InputPrimitiveType.Int32, false, false, false), + InputFactory.Property("requiredString", InputPrimitiveType.String, isRequired: true), + InputFactory.Property("OptionalInt", InputPrimitiveType.Int32) }; MockHelpers.LoadMockPlugin(createCSharpTypeCore: (InputType inputType) => @@ -275,7 +277,7 @@ public void BuildModelAsStruct() } }); - var inputModel = new InputModelType("TestModel", "TestModel", "public", null, "Test model.", InputModelTypeUsage.Input | InputModelTypeUsage.Output, properties, null, Array.Empty(), null, null, new Dictionary(), null, modelAsStruct: true); + var inputModel = InputFactory.Model("TestModel", properties: properties, modelAsStruct: true); var modelTypeProvider = new ModelProvider(inputModel); Assert.AreEqual(TypeSignatureModifiers.Public | TypeSignatureModifiers.Struct | TypeSignatureModifiers.Partial | TypeSignatureModifiers.ReadOnly, modelTypeProvider.DeclarationModifiers); @@ -284,7 +286,7 @@ public void BuildModelAsStruct() [Test] public void TestBuildFields() { - var inputModel = new InputModelType("TestModel", "TestModel", "public", null, "Test model.", InputModelTypeUsage.Input | InputModelTypeUsage.Output, [], null, Array.Empty(), null, null, new Dictionary(), null, false); + var inputModel = InputFactory.Model("TestModel", properties: []); var modelTypeProvider = new ModelProvider(inputModel); var fields = modelTypeProvider.Fields; diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/ParameterProviderTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/ParameterProviderTests.cs index cd812c4375..b29f7693d7 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/ParameterProviderTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/ParameterProviderTests.cs @@ -5,6 +5,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.Tests.Providers @@ -36,7 +37,7 @@ public void NotSameInstance() { MockHelpers.LoadMockPlugin(); - var param = new InputParameter("name", "name", "description", new InputPrimitiveType(InputPrimitiveTypeKind.String, "string", "string"), RequestLocation.Body, null, InputOperationParameterKind.Spread, false, false, false, false, false, false, false, null, null); + var param = InputFactory.Parameter("name", InputPrimitiveType.String, kind: InputOperationParameterKind.Spread); var paramProvider1 = CodeModelPlugin.Instance.TypeFactory.CreateParameter(param); var paramProvider2 = CodeModelPlugin.Instance.TypeFactory.CreateParameter(param); Assert.IsFalse(ReferenceEquals(paramProvider1, paramProvider2)); @@ -46,46 +47,16 @@ public void NotSameInstance() public void ValueTypeHasNoValidation(InputType paramType) { MockHelpers.LoadMockPlugin(); - var inputType = GetInputParameter("testParam", paramType); + var inputType = InputFactory.Parameter("testParam", paramType, isRequired: true); var parameter = CodeModelPlugin.Instance.TypeFactory.CreateParameter(inputType); Assert.AreEqual(ParameterValidationType.None, parameter.Validation); } - private InputParameter GetInputParameter(string name, InputType inputType) - { - return new InputParameter( - name, - name, - name, - inputType, - RequestLocation.Body, - null, - InputOperationParameterKind.Method, - true, - false, - false, - false, - false, - false, - false, - null, - null); - } - private static IEnumerable ValueInputTypes() { - yield return new InputPrimitiveType(InputPrimitiveTypeKind.Int32, "int", "int"); - yield return new InputPrimitiveType(InputPrimitiveTypeKind.Float, "float", "float"); - yield return new InputEnumType( - "inputEnum", - "inputEnum", - "public", - null, - "inputEnum", - InputModelTypeUsage.Input | InputModelTypeUsage.Output, - new InputPrimitiveType(InputPrimitiveTypeKind.Int32, "int", "int"), - [new InputEnumTypeValue("foo", 1, "foo")], - true); + yield return InputPrimitiveType.Int32; + yield return InputPrimitiveType.Float32; + yield return InputFactory.Enum("inputEnum", InputPrimitiveType.Int32, isExtensible: true, values: [InputFactory.EnumMember.Int32("foo", 1)]); } private static IEnumerable NotEqualsTestCases() diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/PropertyDescriptionTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/PropertyDescriptionTests.cs index 8b58385029..ccfeb6ac09 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/PropertyDescriptionTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/PropertyDescriptionTests.cs @@ -6,6 +6,7 @@ using Microsoft.Generator.CSharp.Input; using Microsoft.Generator.CSharp.Primitives; using Microsoft.Generator.CSharp.Statements; +using Microsoft.Generator.CSharp.Tests.Common; using NUnit.Framework; namespace Microsoft.Generator.CSharp.Tests.Providers @@ -65,29 +66,17 @@ public static IEnumerable BuildPropertyDescriptionTestCases get { // list property - yield return new TestCaseData( - new InputModelProperty("prop1", "prop1", "public", new InputArrayType("mockProp", "TypeSpec.Array", InputPrimitiveType.String), false, false, false), - new CSharpType(typeof(IList))); + yield return new TestCaseData(InputFactory.Property("prop1", InputFactory.Array(InputPrimitiveType.String)), new CSharpType(typeof(IList))); // list of binary data property - yield return new TestCaseData( - new InputModelProperty("prop1", "prop1", "public", new InputArrayType("mockProp", "TypeSpec.Array", InputPrimitiveType.Any), false, true, false), - new CSharpType(typeof(IReadOnlyList))); + yield return new TestCaseData(InputFactory.Property("prop1", InputFactory.Array(InputPrimitiveType.Any), isReadOnly: true), new CSharpType(typeof(IReadOnlyList))); // dictionary property with binary data value - yield return new TestCaseData( - new InputModelProperty("prop1", "prop1", "public", new InputDictionaryType("mockProp", InputPrimitiveType.String, InputPrimitiveType.Any), false, false, false), - new CSharpType(typeof(IDictionary))); + yield return new TestCaseData(InputFactory.Property("prop1", InputFactory.Dictionary(InputPrimitiveType.Any)), new CSharpType(typeof(IDictionary))); // nullable dictionary property - yield return new TestCaseData( - new InputModelProperty("prop1", "prop1", "public", new InputDictionaryType("mockProp", InputPrimitiveType.String, InputPrimitiveType.String), false, false, false), - new CSharpType(typeof(IDictionary), true)); + yield return new TestCaseData(InputFactory.Property("prop1", InputFactory.Dictionary(InputPrimitiveType.String)), new CSharpType(typeof(IDictionary))); // primitive type property - yield return new TestCaseData( - new InputModelProperty("prop1", "prop1", "public", InputPrimitiveType.String, false, false, false), - new CSharpType(typeof(string))); + yield return new TestCaseData(InputFactory.Property("prop1", InputPrimitiveType.String), new CSharpType(typeof(string))); // binary data property - yield return new TestCaseData( - new InputModelProperty("prop1", "prop1", "public", InputPrimitiveType.Any, false, true, false), - new CSharpType(typeof(BinaryData))); + yield return new TestCaseData(InputFactory.Property("prop1", InputPrimitiveType.Any, isReadOnly: true), new CSharpType(typeof(BinaryData))); } } } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/PropertyProviderTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/PropertyProviderTests.cs index a776bcc442..28d5fdf1f6 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/PropertyProviderTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/PropertyProviderTests.cs @@ -5,6 +5,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.Tests.Providers @@ -19,81 +20,49 @@ public PropertyProviderTests() [Test] public void TestSnakeCaseProperty() { - InputModelProperty inputModelProperty = new InputModelProperty - ( - "snake_case", - "snake_case", - "A property with snake_case name", - InputPrimitiveType.String, - true, - false, - false); + InputModelProperty inputModelProperty = InputFactory.Property("snake_case", InputPrimitiveType.String, wireName: "snake_case", isRequired: true); var property = new PropertyProvider(inputModelProperty); Assert.AreEqual("SnakeCase", property.Name); Assert.AreEqual("snake_case", property.WireInfo?.SerializedName); - Assert.AreEqual("A property with snake_case name", property.Description.ToString()); + Assert.AreEqual("Description for snake_case", property.Description.ToString()); } [Test] public void TestPascalCaseProperty() { - InputModelProperty inputModelProperty = new InputModelProperty - ( - "PascalCase", - "PascalCase", - "A property with PascalCase name", - InputPrimitiveType.String, - true, - false, - false); + InputModelProperty inputModelProperty = InputFactory.Property("PascalCase", InputPrimitiveType.String, wireName: "PascalCase", isRequired: true); var property = new PropertyProvider(inputModelProperty); Assert.AreEqual("PascalCase", property.Name); Assert.AreEqual("PascalCase", property.WireInfo?.SerializedName); - Assert.AreEqual("A property with PascalCase name", property.Description.ToString()); + Assert.AreEqual("Description for PascalCase", property.Description.ToString()); } [Test] public void TestCamelCaseProperty() { - InputModelProperty inputModelProperty = new InputModelProperty - ( - "camelCase", - "camelCase", - "A property with camelCase name", - InputPrimitiveType.String, - true, - false, - false); + InputModelProperty inputModelProperty = InputFactory.Property("camelCase", InputPrimitiveType.String, wireName: "camelCase", isRequired: true); var property = new PropertyProvider(inputModelProperty); Assert.AreEqual("CamelCase", property.Name); Assert.AreEqual("camelCase", property.WireInfo?.SerializedName); - Assert.AreEqual("A property with camelCase name", property.Description.ToString()); + Assert.AreEqual("Description for camelCase", property.Description.ToString()); } [Test] public void TestKebabCaseProperty() { - InputModelProperty inputModelProperty = new InputModelProperty - ( - "kebab-case", - "kebab-case", - "A property with kebab-case name", - InputPrimitiveType.String, - true, - false, - false); + InputModelProperty inputModelProperty = InputFactory.Property("kebab-case", InputPrimitiveType.String, wireName: "kebab-case", isRequired: true); var property = new PropertyProvider(inputModelProperty); Assert.AreEqual("KebabCase", property.Name); Assert.AreEqual("kebab-case", property.WireInfo?.SerializedName); - Assert.AreEqual("A property with kebab-case name", property.Description.ToString()); + Assert.AreEqual("Description for kebab-case", property.Description.ToString()); } [TestCaseSource(nameof(CollectionPropertyTestCases))] @@ -115,29 +84,17 @@ private static IEnumerable CollectionPropertyTestCases() // List -> IReadOnlyList yield return new TestCaseData( new CSharpType(typeof(IList<>), typeof(string)), - new InputModelProperty("readOnlyCollection", "readOnlyCollection", string.Empty, - new InputArrayType("List", "id", InputPrimitiveType.String), - true, - true, - false), + InputFactory.Property("readOnlyCollection", InputFactory.Array(InputPrimitiveType.String), isRequired: true, isReadOnly: true), new CSharpType(typeof(IReadOnlyList<>), typeof(string))); // Dictionary -> IReadOnlyDictionary yield return new TestCaseData( new CSharpType(typeof(IDictionary<,>), typeof(string), typeof(int)), - new InputModelProperty("readOnlyDictionary", "readOnlyDictionary", string.Empty, - new InputDictionaryType("Dictionary", InputPrimitiveType.String, InputPrimitiveType.Int32), - true, - true, - false), + InputFactory.Property("readOnlyDictionary", InputFactory.Dictionary(InputPrimitiveType.Int32), isRequired: true, isReadOnly: true), new CSharpType(typeof(IReadOnlyDictionary<,>), typeof(string), typeof(int))); // string -> string yield return new TestCaseData( new CSharpType(typeof(string)), - new InputModelProperty("stringProperty", "stringProperty", string.Empty, - InputPrimitiveType.String, - true, - true, - false), + InputFactory.Property("stringProperty", InputPrimitiveType.String, isRequired: true, isReadOnly: true), new CSharpType(typeof(string))); } } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/TypeFactoryTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/TypeFactoryTests.cs index c61e7d58b3..3db4ed068b 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/TypeFactoryTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/TypeFactoryTests.cs @@ -4,6 +4,7 @@ using Microsoft.Generator.CSharp.Input; using Microsoft.Generator.CSharp.Primitives; using Microsoft.Generator.CSharp.Providers; +using Microsoft.Generator.CSharp.Tests.Common; using Moq; using NUnit.Framework; @@ -20,16 +21,11 @@ public void SetUp() [Test] public void ExtensibleStringEnumType() { - var input = new InputEnumType( - "sampleType", - "sampleType", - "public", - null, - "sampleType description", - InputModelTypeUsage.Input, - InputPrimitiveType.String, - [new InputEnumTypeValue("value1", "value1", null), new InputEnumTypeValue("value2", "value2", null)], - true); + var input = InputFactory.Enum("sampleType", InputPrimitiveType.String, isExtensible: true, usage: InputModelTypeUsage.Input, values: + [ + InputFactory.EnumMember.String("value1", "value1"), + InputFactory.EnumMember.String("value2", "value2") + ]); var expected = new CSharpType("SampleType", "Sample.Models", true, false, null, [], true, true, underlyingEnumType: typeof(string)); var actual = CodeModelPlugin.Instance.TypeFactory.CreateCSharpType(input); @@ -41,16 +37,11 @@ public void ExtensibleStringEnumType() [Test] public void ExtensibleStringNullableEnumType() { - var input = new InputEnumType( - "sampleType", - "sampleType", - "public", - null, - "sampleType description", - InputModelTypeUsage.Input, - InputPrimitiveType.String, - [new InputEnumTypeValue("value1", "value1", null), new InputEnumTypeValue("value2", "value2", null)], - true); + var input = InputFactory.Enum("sampleType", InputPrimitiveType.String, usage: InputModelTypeUsage.Input, isExtensible: true, values: + [ + InputFactory.EnumMember.String("value1", "value1"), + InputFactory.EnumMember.String("value2", "value2") + ]); var nullableInput = new InputNullableType(input); var expected = new CSharpType("SampleType", "Sample.Models", true, true, null, [], true, true, underlyingEnumType: typeof(string)); @@ -63,16 +54,11 @@ public void ExtensibleStringNullableEnumType() [Test] public void ExtensibleStringEnumTypeProvider() { - var input = new InputEnumType( - "sampleType", - "sampleType", - "public", - null, - "sampleType description", - InputModelTypeUsage.Input, - InputPrimitiveType.String, - [new InputEnumTypeValue("value1", "value1", null), new InputEnumTypeValue("value2", "value2", null)], - true); + var input = InputFactory.Enum("sampleType", InputPrimitiveType.String, usage: InputModelTypeUsage.Input, isExtensible: true, values: + [ + InputFactory.EnumMember.String("value1", "value1"), + InputFactory.EnumMember.String("value2", "value2") + ]); var expected = new CSharpType("SampleType", "Sample.Models", true, false, null, [], true, true, underlyingEnumType: typeof(string)); var enumProvider = CodeModelPlugin.Instance.TypeFactory.CreateEnum(input); @@ -84,16 +70,11 @@ public void ExtensibleStringEnumTypeProvider() [Test] public void FixedStringEnumType() { - var input = new InputEnumType( - "sampleType", - "sampleType", - "public", - null, - "sampleType description", - InputModelTypeUsage.Input, - InputPrimitiveType.String, - [new InputEnumTypeValue("value1", "value1", null), new InputEnumTypeValue("value2", "value2", null)], - false); + var input = InputFactory.Enum("sampleType", InputPrimitiveType.String, usage: InputModelTypeUsage.Input, values: + [ + InputFactory.EnumMember.String("value1", "value1"), + InputFactory.EnumMember.String("value2", "value2") + ]); var expected = new CSharpType("SampleType", "Sample.Models", true, false, null, [], true, false, underlyingEnumType: typeof(string)); var actual = CodeModelPlugin.Instance.TypeFactory.CreateCSharpType(input); @@ -105,16 +86,11 @@ public void FixedStringEnumType() [Test] public void CreateSameEnum() { - var input = new InputEnumType( - "sampleType", - "sampleType", - "public", - null, - "sampleType description", - InputModelTypeUsage.Input, - InputPrimitiveType.String, - [new InputEnumTypeValue("value1", "value1", null), new InputEnumTypeValue("value2", "value2", null)], - false); + var input = InputFactory.Enum("sampleType", InputPrimitiveType.String, usage: InputModelTypeUsage.Input, values: + [ + InputFactory.EnumMember.String("value1", "value1"), + InputFactory.EnumMember.String("value2", "value2") + ]); var expected = CodeModelPlugin.Instance.TypeFactory.CreateEnum(input); var actual = CodeModelPlugin.Instance.TypeFactory.CreateEnum(input); @@ -125,16 +101,11 @@ public void CreateSameEnum() [Test] public void CreateEnum_WithDeclaringType() { - var input = new InputEnumType( - "sampleType", - "sampleType", - "public", - null, - "sampleType description", - InputModelTypeUsage.Input, - InputPrimitiveType.String, - [new InputEnumTypeValue("value1", "value1", null), new InputEnumTypeValue("value2", "value2", null)], - false); + var input = InputFactory.Enum("sampleType", InputPrimitiveType.String, usage: InputModelTypeUsage.Input, values: + [ + InputFactory.EnumMember.String("value1", "value1"), + InputFactory.EnumMember.String("value2", "value2") + ]); var declaringType = new Mock().Object; var expected = CodeModelPlugin.Instance.TypeFactory.CreateEnum(input, declaringType); var actual = CodeModelPlugin.Instance.TypeFactory.CreateEnum(input, declaringType); diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Writers/TestData/TypeProviderWriterTests/TypeProviderWriter_WriteModel.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Writers/TestData/TypeProviderWriterTests/TypeProviderWriter_WriteModel.cs index d73f1b3e76..62e4db1a66 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Writers/TestData/TypeProviderWriterTests/TypeProviderWriter_WriteModel.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Writers/TestData/TypeProviderWriterTests/TypeProviderWriter_WriteModel.cs @@ -8,15 +8,15 @@ namespace Sample.Models { - /// Test model. + /// TestModel description. public partial class TestModel { /// Keeps track of any properties unknown to the library. private global::System.Collections.Generic.IDictionary _serializedAdditionalRawData; /// Initializes a new instance of . - /// Required string, illustrating a reference type property. - /// Required int, illustrating a value type property. + /// Description for requiredString. + /// Description for requiredInt. /// is null. public TestModel(string requiredString, int requiredInt) { @@ -33,10 +33,10 @@ internal TestModel(string requiredString, int requiredInt, global::System.Collec _serializedAdditionalRawData = serializedAdditionalRawData; } - /// Required string, illustrating a reference type property. + /// Description for requiredString. public string RequiredString { get; set; } - /// Required int, illustrating a value type property. + /// Description for requiredInt. public int RequiredInt { get; set; } } } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Writers/TestData/TypeProviderWriterTests/TypeProviderWriter_WriteModelAsStruct.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Writers/TestData/TypeProviderWriterTests/TypeProviderWriter_WriteModelAsStruct.cs index 5947efd6e6..9485182b92 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Writers/TestData/TypeProviderWriterTests/TypeProviderWriter_WriteModelAsStruct.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Writers/TestData/TypeProviderWriterTests/TypeProviderWriter_WriteModelAsStruct.cs @@ -8,15 +8,15 @@ namespace Sample.Models { - /// Test model. + /// TestModel description. public readonly partial struct TestModel { /// Keeps track of any properties unknown to the library. private global::System.Collections.Generic.IDictionary _serializedAdditionalRawData; /// Initializes a new instance of . - /// Required string, illustrating a reference type property. - /// Required int, illustrating a value type property. + /// Description for requiredString. + /// Description for requiredInt. /// is null. public TestModel(string requiredString, int requiredInt) { @@ -33,10 +33,10 @@ internal TestModel(string requiredString, int requiredInt, global::System.Collec _serializedAdditionalRawData = serializedAdditionalRawData; } - /// Required string, illustrating a reference type property. + /// Description for requiredString. public string RequiredString { get; set; } - /// Required int, illustrating a value type property. + /// Description for requiredInt. public int RequiredInt { get; set; } } } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Writers/TypeProviderWriterTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Writers/TypeProviderWriterTests.cs index 9e8e8e7ff5..d3eb3a4bce 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Writers/TypeProviderWriterTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Writers/TypeProviderWriterTests.cs @@ -7,6 +7,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.Tests.Writers @@ -42,8 +43,7 @@ public void TypeProviderWriter_WriteModel() var properties = new List { RequiredStringProperty, RequiredIntProperty }; MockHelpers.LoadMockPlugin(createCSharpTypeCore: MockPluginSetValue(properties)); - var inputModel = new InputModelType("TestModel", string.Empty, "public", null, "Test model.", InputModelTypeUsage.Input | InputModelTypeUsage.Output, - properties, null, new List(), null, null, new Dictionary(), null, false); + var inputModel = InputFactory.Model("TestModel", properties: properties); var modelProvider = new ModelProvider(inputModel); var codeFile = new TypeProviderWriter(modelProvider).Write(); @@ -60,8 +60,7 @@ public void TypeProviderWriter_WriteModelAsStruct() var properties = new List { RequiredStringProperty, RequiredIntProperty }; MockHelpers.LoadMockPlugin(createCSharpTypeCore: MockPluginSetValue(properties)); - var inputModel = new InputModelType("TestModel", string.Empty, "public", null, "Test model.", InputModelTypeUsage.Input | InputModelTypeUsage.Output, - properties, null, new List(), null, null, new Dictionary(), null, modelAsStruct: true); + var inputModel = InputFactory.Model("TestModel", properties: properties, modelAsStruct: true); var modelProvider = new ModelProvider(inputModel); var codeFile = new TypeProviderWriter(modelProvider).Write(); @@ -104,12 +103,12 @@ private Func MockPluginSetValue(List } // common usages definitions - internal static readonly InputModelProperty RequiredStringProperty = new InputModelProperty("requiredString", "requiredString", "Required string, illustrating a reference type property.", InputPrimitiveType.String, true, false, false); + internal static readonly InputModelProperty RequiredStringProperty = InputFactory.Property("requiredString", InputPrimitiveType.String, isRequired: true); - internal static readonly InputModelProperty RequiredIntProperty = new InputModelProperty("requiredInt", "requiredInt", "Required int, illustrating a value type property.", InputPrimitiveType.Int32, true, false, false); + internal static readonly InputModelProperty RequiredIntProperty = InputFactory.Property("requiredInt", InputPrimitiveType.Int32, isRequired: true); - internal static readonly InputModelProperty RequiredStringListProperty = new InputModelProperty("requiredStringList", "requiredStringList", "Required collection of strings, illustrating a collection of reference types.", new InputArrayType("requiredStringList", "TypeSpec.Array", InputPrimitiveType.String), true, false, false); + internal static readonly InputModelProperty RequiredStringListProperty = InputFactory.Property("requiredStringList", InputFactory.Array(InputPrimitiveType.String), isRequired: true); - internal static readonly InputModelProperty RequiredIntListProperty = new InputModelProperty("requiredIntList", "requiredIntList", "Required collection of ints, illustrating a collection of value types.", new InputArrayType("requiredIntList", "TypeSpec.Array", InputPrimitiveType.Int32), true, false, false); + internal static readonly InputModelProperty RequiredIntListProperty = InputFactory.Property("requiredIntList", InputFactory.Array(InputPrimitiveType.Int32), isRequired: true); } } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/common/InputFactory.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/common/InputFactory.cs new file mode 100644 index 0000000000..bb925de8a0 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/common/InputFactory.cs @@ -0,0 +1,218 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System.Collections.Generic; +using Microsoft.Generator.CSharp.Input; + +namespace Microsoft.Generator.CSharp.Tests.Common +{ + public static class InputFactory + { + public static class EnumMember + { + public static InputEnumTypeValue Int32(string name, int value) + { + return new InputEnumTypeValue(name, value, $"{name} description"); + } + + public static InputEnumTypeValue Float32(string name, float value) + { + return new InputEnumTypeValue(name, value, $"{name} description"); + } + + public static InputEnumTypeValue String(string name, string value) + { + return new InputEnumTypeValue(name, value, $"{name} description"); + } + } + + public static class Literal + { + public static InputLiteralType String(string value) + { + return new InputLiteralType(InputPrimitiveType.String, value); + } + + public static InputLiteralType Any(object value) + { + return new InputLiteralType(InputPrimitiveType.Any, value); + } + } + + public static class Constant + { + public static InputConstant String(string value) + { + return new InputConstant(value, InputPrimitiveType.String); + } + + public static InputConstant Int64(long value) + { + return new InputConstant(value, InputPrimitiveType.Int64); + } + } + + public static InputParameter Parameter( + string name, + InputType type, + InputConstant? defaultValue = null, + RequestLocation location = RequestLocation.Body, + bool isRequired = false, + InputOperationParameterKind kind = InputOperationParameterKind.Method, + bool isEndpoint = false, + bool isResourceParameter = false) + { + return new InputParameter( + name, + name, + $"{name} description", + type, + location, + defaultValue, + kind, + isRequired, + false, + isResourceParameter, + false, + isEndpoint, + false, + false, + null, + null); + } + + public static InputNamespace Namespace(string name, IEnumerable? models = null, IEnumerable? clients = null) + { + return new InputNamespace( + name, + [], + [], + models is null ? [] : [.. models], + clients is null ? [] : [.. clients], + new InputAuth()); + } + + public static InputEnumType Enum( + string name, + InputPrimitiveType underlyingType, + string access = "public", + InputModelTypeUsage usage = InputModelTypeUsage.Output | InputModelTypeUsage.Input, + IEnumerable? values = null, + bool isExtensible = false) + { + return new InputEnumType( + name, + name, + access, + null, + $"{name} description", + usage, + underlyingType, + values is null ? [new InputEnumTypeValue("Value", 1, "Value description")] : [.. values], + isExtensible); + } + + public static InputModelProperty Property( + string name, + InputType type, + bool isRequired = false, + bool isReadOnly = false, + bool isDiscriminator = false, + string? wireName = null) + { + return new InputModelProperty( + name, + wireName ?? name.ToVariableName(), + $"Description for {name}", + type, + isRequired, + isReadOnly, + isDiscriminator, + null); + } + + public static InputModelType Model( + string name, + string access = "public", + InputModelTypeUsage usage = InputModelTypeUsage.Output | InputModelTypeUsage.Input, + IEnumerable? properties = null, + InputModelType? baseModel = null, + bool modelAsStruct = false) + { + return new InputModelType( + name, + name, + access, + null, + $"{name} description", + usage, + properties is null ? [InputFactory.Property("StringProperty", InputPrimitiveType.String)] : [.. properties], + baseModel, + [], + null, + null, + new Dictionary(), + null, + modelAsStruct); + } + + public static InputType Array(InputType elementType) + { + return new InputArrayType("list", "list", elementType); + } + + public static InputType Dictionary(InputType valueType, InputType? keyType = null) + { + return new InputDictionaryType("dictionary", keyType ?? InputPrimitiveType.String, valueType); + } + + public static InputOperation Operation( + string name, + string access = "public", + IEnumerable? parameters = null, + IEnumerable? responses = null) + { + return new InputOperation( + name, + null, + $"{name} description", + null, + access, + parameters is null ? [] : [.. parameters], + responses is null ? [OperationResponse()] : [.. responses], + "GET", + BodyMediaType.Json, + "", + "", + null, + null, + false, + null, + null, + true, + true, + name); + } + + public static OperationResponse OperationResponse(IEnumerable? statusCodes = null, InputType? bodytype = null) + { + return new OperationResponse( + statusCodes is null ? [200] : [.. statusCodes], + bodytype, + BodyMediaType.Json, + [], + false, + ["application/json"]); + } + + public static InputClient Client(string name, IEnumerable? operations = null, IEnumerable? parameters = null, string? parent = null) + { + return new InputClient( + name, + $"{name} description", + operations is null ? [] : [.. operations], + parameters is null ? [] : [.. parameters], + parent); + } + } +} diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/common/Microsoft.Generator.CSharp.Tests.Common.csproj b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/common/Microsoft.Generator.CSharp.Tests.Common.csproj new file mode 100644 index 0000000000..03e148f5b8 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/common/Microsoft.Generator.CSharp.Tests.Common.csproj @@ -0,0 +1,15 @@ + + + + true + + + + + + + + + + +