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 42a0bf3
Show file tree
Hide file tree
Showing 3 changed files with 42 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
Expand Up @@ -202,5 +202,38 @@ 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 property shouldn't be added to the model provider
Assert.AreEqual(1, modelTypeProvider.Properties.Count);
Assert.AreEqual("Prop1", modelTypeProvider.Properties[0].Name);

// 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 42a0bf3

Please sign in to comment.