Skip to content

Commit

Permalink
test: add tests for custom model props
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgerangel-msft committed Sep 18, 2024
1 parent 4234073 commit 0baa468
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
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()
{
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 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 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; }
}

0 comments on commit 0baa468

Please sign in to comment.