Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for supporting adding custom model props #4474

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Linq;
using Microsoft.Generator.CSharp.Expressions;
using Microsoft.Generator.CSharp.Primitives;
using Microsoft.Generator.CSharp.SourceInput;
using Microsoft.Generator.CSharp.Statements;

namespace Microsoft.Generator.CSharp.Providers
Expand Down Expand Up @@ -151,23 +150,6 @@ private TypeSignatureModifiers GetDeclarationModifiersInternal()

protected virtual PropertyProvider[] BuildProperties() => [];

private HashSet<string> BuildPropertyNames()
jorgerangel-msft marked this conversation as resolved.
Show resolved Hide resolved
{
var propertyNames = new HashSet<string>();
foreach (var property in Properties)
{
propertyNames.Add(property.Name);
foreach (var attribute in property.Attributes ?? [])
{
if (CodeGenAttributes.TryGetCodeGenMemberAttributeValue(attribute, out var name))
{
propertyNames.Add(name);
}
}
}
return propertyNames;
}

protected virtual FieldProvider[] BuildFields() => [];

protected virtual CSharpType[] BuildImplements() => [];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Linq;
using System.Threading.Tasks;
using Microsoft.Generator.CSharp.Input;
using Microsoft.Generator.CSharp.Primitives;
Expand Down Expand Up @@ -202,5 +203,45 @@ public async Task CanChangeExtensibleEnumToEnum()
Assert.AreEqual("Val2", enumProvider.EnumValues[1].Name);
Assert.AreEqual("Val3", enumProvider.EnumValues[2].Name);
}

[Test]
public async Task CanAddProperties()
{
await MockHelpers.LoadMockPluginAsync(compilation: async () => await Helpers.GetCompilationFromDirectoryAsync());

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 custom properties shouldn't be added to the model provider
Assert.AreEqual(1, modelTypeProvider.Properties.Count);
Assert.AreEqual("Prop1", modelTypeProvider.Properties[0].Name);

// the custom properties shouldn't be parameters of the model's ctor
var modelCtors = modelTypeProvider.Constructors;
foreach (var ctor in modelCtors)
{
Assert.IsFalse(ctor.Signature.Parameters.Any(p => p.Name == "newProperty" || p.Name == "newProperty2"));
}

// the custom properties should be added to the custom code view
Assert.AreEqual(2, customCodeView!.Properties.Count);
Assert.AreEqual("NewProperty", customCodeView.Properties[0].Name);
Assert.AreEqual(new CSharpType(typeof(int)), customCodeView.Properties[0].Type);
Assert.AreEqual(MethodSignatureModifiers.Public, customCodeView.Properties[0].Modifiers);
Assert.IsTrue(customCodeView.Properties[0].Body.HasSetter);

Assert.AreEqual("NewProperty2", customCodeView.Properties[1].Name);
Assert.AreEqual(new CSharpType(typeof(string)), customCodeView.Properties[1].Type);
Assert.AreEqual(MethodSignatureModifiers.Public, customCodeView.Properties[1].Modifiers);
Assert.IsFalse(customCodeView.Properties[1].Body.HasSetter);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#nullable disable

namespace Sample.Models;

internal partial class MockInputModel
{
public int NewProperty { get; set; }
public string NewProperty2 { get; }
}
Loading