Skip to content

Commit

Permalink
Add tests for changing property type/accessibility (#4365)
Browse files Browse the repository at this point in the history
Validates that #4258 and
#4265 are fixed. They were
fixed by #4362.
  • Loading branch information
JoshLove-msft authored Sep 6, 2024
1 parent 782dca5 commit 86cc361
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using Microsoft.Generator.CSharp.Input;
using Microsoft.Generator.CSharp.Primitives;
using Microsoft.Generator.CSharp.Providers;
using Microsoft.Generator.CSharp.Tests.Common;
using NUnit.Framework;
Expand All @@ -25,11 +26,7 @@ public void TestCustomization_CanChangeModelName()
var modelTypeProvider = new ModelProvider(inputModel);
var customCodeView = modelTypeProvider.CustomCodeView;

Assert.IsNotNull(customCodeView);
Assert.AreEqual("CustomizedModel", modelTypeProvider.Type.Name);
Assert.AreEqual("NewNamespace.Models", modelTypeProvider.Type.Namespace);
Assert.AreEqual(customCodeView?.Name, modelTypeProvider.Type.Name);
Assert.AreEqual(customCodeView?.Type.Namespace, modelTypeProvider.Type.Namespace);
AssertCommon(customCodeView, modelTypeProvider, "NewNamespace.Models", "CustomizedModel");
}

[Test]
Expand All @@ -46,11 +43,7 @@ public void TestCustomization_CanChangePropertyName()
var modelTypeProvider = new ModelProvider(inputModel);
var customCodeView = modelTypeProvider.CustomCodeView;

Assert.IsNotNull(customCodeView);
Assert.AreEqual("MockInputModel", modelTypeProvider.Type.Name);
Assert.AreEqual("Sample.Models", modelTypeProvider.Type.Namespace);
Assert.AreEqual(customCodeView?.Name, modelTypeProvider.Type.Name);
Assert.AreEqual(customCodeView?.Type.Namespace, modelTypeProvider.Type.Namespace);
AssertCommon(customCodeView, modelTypeProvider, "Sample.Models", "MockInputModel");

// the property should be filtered from the model provider
Assert.AreEqual(0, modelTypeProvider.Properties.Count);
Expand All @@ -59,5 +52,64 @@ public void TestCustomization_CanChangePropertyName()
Assert.AreEqual(1, customCodeView!.Properties.Count);
Assert.AreEqual("Prop2", customCodeView.Properties[0].Name);
}

[Test]
public void TestCustomization_CanChangePropertyType()
{
MockHelpers.LoadMockPlugin(customization: Helpers.GetCompilationFromFile());

var props = new[]
{
InputFactory.Property("Prop1", InputFactory.Array(InputPrimitiveType.String))
};

var inputModel = InputFactory.Model("mockInputModel", properties: props);
var modelTypeProvider = new ModelProvider(inputModel);
var customCodeView = modelTypeProvider.CustomCodeView;

AssertCommon(customCodeView, modelTypeProvider, "Sample.Models", "MockInputModel");

// the property should be filtered from the model provider
Assert.AreEqual(0, modelTypeProvider.Properties.Count);

// the property should be added to the custom code view
Assert.AreEqual(1, customCodeView!.Properties.Count);
// the property type should be changed
Assert.AreEqual(new CSharpType(typeof(int[])), customCodeView.Properties[0].Type);
}

[Test]
public void TestCustomization_CanChangePropertyAccessibility()
{
MockHelpers.LoadMockPlugin(customization: Helpers.GetCompilationFromFile());

var props = new[]
{
InputFactory.Property("Prop1", InputFactory.Array(InputPrimitiveType.String))
};

var inputModel = InputFactory.Model("mockInputModel", properties: props);
var modelTypeProvider = new ModelProvider(inputModel);
var customCodeView = modelTypeProvider.CustomCodeView;

AssertCommon(customCodeView, modelTypeProvider, "Sample.Models", "MockInputModel");

// the property should be filtered from the model provider
Assert.AreEqual(0, modelTypeProvider.Properties.Count);

// the property should be added to the custom code view
Assert.AreEqual(1, customCodeView!.Properties.Count);
// the property accessibility should be changed
Assert.IsTrue(customCodeView.Properties[0].Modifiers.HasFlag(MethodSignatureModifiers.Internal));
}

private static void AssertCommon(TypeProvider? customCodeView, ModelProvider modelTypeProvider, string expectedNamespace, string expectedName)
{
Assert.IsNotNull(customCodeView);
Assert.AreEqual(expectedNamespace, modelTypeProvider.Type.Namespace);
Assert.AreEqual(expectedName, modelTypeProvider.Type.Name);
Assert.AreEqual(customCodeView?.Name, modelTypeProvider.Type.Name);
Assert.AreEqual(customCodeView?.Type.Namespace, modelTypeProvider.Type.Namespace);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#nullable disable

using System;

namespace Sample
{
// TODO: if we decide to use the public APIs, we do not have to define this attribute here. Tracking: https://github.com/Azure/autorest.csharp/issues/4551
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
internal class CodeGenMemberAttribute : Attribute
{
public CodeGenMemberAttribute() : base(null)
{
}

public CodeGenMemberAttribute(string originalName) : base(originalName)
{
}
}
}
namespace Sample.Models
{
public partial class MockInputModel
{
[CodeGenMember("Prop1")]
internal string[] Prop2 { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#nullable disable

using System;

namespace Sample
{
// TODO: if we decide to use the public APIs, we do not have to define this attribute here. Tracking: https://github.com/Azure/autorest.csharp/issues/4551
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
internal class CodeGenMemberAttribute : Attribute
{
public CodeGenMemberAttribute() : base(null)
{
}

public CodeGenMemberAttribute(string originalName) : base(originalName)
{
}
}
}
namespace Sample.Models
{
public partial class MockInputModel
{
[CodeGenMember("Prop1")]
public int[] Prop2 { get; set; }
}
}

0 comments on commit 86cc361

Please sign in to comment.