Skip to content

Commit

Permalink
Merge pull request #199 from AVTit/master
Browse files Browse the repository at this point in the history
Check for null in *Specified properties
  • Loading branch information
mganss committed May 13, 2020
2 parents aee0512 + fbce9c4 commit ac035ab
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
67 changes: 67 additions & 0 deletions XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
Expand Down Expand Up @@ -220,6 +221,72 @@ public void TestListWithPublicPropertySettersWithoutConstructors()
Assert.Null(collectionPropertyInfo.GetValue(myClassInstance));
}
}

[Fact]
public void TestListWithPublicPropertySettersWithoutConstructorsSpecified()
{
var assembly = Compiler.Generate("ListPublicWithoutConstructorInitialization", ListPattern, new Generator
{
GenerateNullables = true,
IntegerDataType = typeof(int),
DataAnnotationMode = DataAnnotationMode.All,
GenerateDesignerCategoryAttribute = false,
GenerateComplexTypesForCollections = true,
EntityFramework = false,
GenerateInterfaces = true,
NamespacePrefix = "List",
GenerateDescriptionAttribute = true,
TextValuePropertyName = "Value",
CollectionSettersMode = CollectionSettersMode.PublicWithoutConstructorInitialization
});
Assert.NotNull(assembly);
var myClassType = assembly.GetType("List.MyClass");
Assert.NotNull(myClassType);
var iListType = typeof(Collection<>);
var collectionPropertyInfos = myClassType.GetProperties().Where(p => p.PropertyType.IsGenericType && iListType.IsAssignableFrom(p.PropertyType.GetGenericTypeDefinition())).OrderBy(p => p.Name).ToList();
var publicCollectionPropertyInfos = collectionPropertyInfos.Where(p => p.SetMethod.IsPublic).OrderBy(p => p.Name).ToList();
Assert.True(collectionPropertyInfos.Count > 0);
Assert.Equal(collectionPropertyInfos, publicCollectionPropertyInfos);
var myClassInstance = Activator.CreateInstance(myClassType);

var propertyNamesWithSpecifiedPostfix = publicCollectionPropertyInfos.Select(p => p.Name + "Specified").ToHashSet();
var propertiesWithSpecifiedPostfix =
myClassType.GetProperties().Where(p => propertyNamesWithSpecifiedPostfix.Contains(p.Name)).ToList();

//Null collection
foreach (var propertyInfo in propertiesWithSpecifiedPostfix)
{
Assert.False((bool)propertyInfo.GetValue(myClassInstance));
}

foreach (var propertyInfo in publicCollectionPropertyInfos)
{
var collection = Activator.CreateInstance(propertyInfo.PropertyType);
propertyInfo.SetValue(myClassInstance, collection);
}

//Not Null but empty collection
foreach (var propertyInfo in propertiesWithSpecifiedPostfix)
{
Assert.False((bool)propertyInfo.GetValue(myClassInstance));
}

foreach (var propertyInfo in publicCollectionPropertyInfos)
{

var collection = Activator.CreateInstance(propertyInfo.PropertyType);
propertyInfo.PropertyType.InvokeMember("Add", BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance, null, collection,
new object[] {null});
propertyInfo.SetValue(myClassInstance, collection);
}

//Not Null and not empty collection
foreach (var propertyInfo in propertiesWithSpecifiedPostfix)
{
Assert.True((bool)propertyInfo.GetValue(myClassInstance));
}
}


[Fact, TestPriority(1)]
[UseCulture("en-US")]
Expand Down
5 changes: 5 additions & 0 deletions XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,11 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi
var listReference = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), Name);
var countReference = new CodePropertyReferenceExpression(listReference, "Count");
var notZeroExpression = new CodeBinaryOperatorExpression(countReference, CodeBinaryOperatorType.IdentityInequality, new CodePrimitiveExpression(0));
if (Configuration.CollectionSettersMode == CollectionSettersMode.PublicWithoutConstructorInitialization)
{
var notNullExpression = new CodeBinaryOperatorExpression(listReference, CodeBinaryOperatorType.IdentityInequality, new CodePrimitiveExpression(null));
notZeroExpression = new CodeBinaryOperatorExpression(notNullExpression, CodeBinaryOperatorType.BooleanAnd, notZeroExpression);
}
var returnStatement = new CodeMethodReturnStatement(notZeroExpression);
specifiedProperty.GetStatements.Add(returnStatement);

Expand Down

0 comments on commit ac035ab

Please sign in to comment.