Skip to content

Commit

Permalink
Add unit tests for invalid interface generated
Browse files Browse the repository at this point in the history
  • Loading branch information
jmatss committed May 17, 2022
1 parent 59c49c6 commit 4fc5714
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2578,5 +2578,91 @@ public void TestArrayItemAttribute()
var optionList = applicationType.GetProperty("OptionList");
Assert.Equal("Test_Generation_Namespace.T_OptionList", optionList.PropertyType.FullName);
}

[Fact]
public void CollectionSetterInAttributeGroupInterfaceIsPrivateIfCollectionSetterModeIsPrivate()
{
const string xsd = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
<xs:element name=""Element"">
<xs:complexType>
<xs:attributeGroup ref=""AttrGroup""/>
</xs:complexType>
</xs:element>
<xs:attributeGroup name=""AttrGroup"">
<xs:attribute name=""Attr"">
<xs:simpleType>
<xs:list itemType=""xs:int""/>
</xs:simpleType>
</xs:attribute>
</xs:attributeGroup>
</xs:schema>";

var generator = new Generator
{
NamespaceProvider = new NamespaceProvider
{
GenerateNamespace = key => "Test",
},
GenerateInterfaces = true,
CollectionSettersMode = CollectionSettersMode.Private
};
var contents = ConvertXml(nameof(CollectionSetterInAttributeGroupInterfaceIsPrivateIfCollectionSetterModeIsPrivate), xsd, generator).ToArray();
var assembly = Compiler.Compile(nameof(CollectionSetterInAttributeGroupInterfaceIsPrivateIfCollectionSetterModeIsPrivate), contents);

var interfaceProperty = assembly.GetType("Test.IAttrGroup")?.GetProperty("Attr");
var implementerProperty = assembly.GetType("Test.Element")?.GetProperty("Attr");
Assert.NotNull(interfaceProperty);
Assert.NotNull(implementerProperty);

var interfaceHasPublicSetter = interfaceProperty.GetSetMethod() != null;
var implementerHasPublicSetter = implementerProperty.GetSetMethod() != null;
Assert.False(interfaceHasPublicSetter);
Assert.False(implementerHasPublicSetter);
}

[Fact]
public void CollectionSetterInAttributeGroupInterfaceIsPublicIfCollectionSetterModeIsPublic()
{
const string xsd = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
<xs:element name=""Element"">
<xs:complexType>
<xs:attributeGroup ref=""AttrGroup""/>
</xs:complexType>
</xs:element>
<xs:attributeGroup name=""AttrGroup"">
<xs:attribute name=""Attr"">
<xs:simpleType>
<xs:list itemType=""xs:int""/>
</xs:simpleType>
</xs:attribute>
</xs:attributeGroup>
</xs:schema>";

var generator = new Generator
{
NamespaceProvider = new NamespaceProvider
{
GenerateNamespace = key => "Test",
},
GenerateInterfaces = true,
CollectionSettersMode = CollectionSettersMode.Public
};
var contents = ConvertXml(nameof(CollectionSetterInAttributeGroupInterfaceIsPublicIfCollectionSetterModeIsPublic), xsd, generator).ToArray();
var assembly = Compiler.Compile(nameof(CollectionSetterInAttributeGroupInterfaceIsPublicIfCollectionSetterModeIsPublic), contents);

var interfaceProperty = assembly.GetType("Test.IAttrGroup")?.GetProperty("Attr");
var implementerProperty = assembly.GetType("Test.Element")?.GetProperty("Attr");
Assert.NotNull(interfaceProperty);
Assert.NotNull(implementerProperty);

var interfaceHasPublicSetter = interfaceProperty.GetSetMethod() != null;
var implementerHasPublicSetter = implementerProperty.GetSetMethod() != null;
Assert.True(interfaceHasPublicSetter);
Assert.True(implementerHasPublicSetter);
}
}
}

0 comments on commit 4fc5714

Please sign in to comment.