Skip to content

Commit 4fc5714

Browse files
committed
Add unit tests for invalid interface generated
1 parent 59c49c6 commit 4fc5714

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

XmlSchemaClassGenerator.Tests/XmlTests.cs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2578,5 +2578,91 @@ public void TestArrayItemAttribute()
25782578
var optionList = applicationType.GetProperty("OptionList");
25792579
Assert.Equal("Test_Generation_Namespace.T_OptionList", optionList.PropertyType.FullName);
25802580
}
2581+
2582+
[Fact]
2583+
public void CollectionSetterInAttributeGroupInterfaceIsPrivateIfCollectionSetterModeIsPrivate()
2584+
{
2585+
const string xsd = @"<?xml version=""1.0"" encoding=""UTF-8""?>
2586+
<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
2587+
<xs:element name=""Element"">
2588+
<xs:complexType>
2589+
<xs:attributeGroup ref=""AttrGroup""/>
2590+
</xs:complexType>
2591+
</xs:element>
2592+
2593+
<xs:attributeGroup name=""AttrGroup"">
2594+
<xs:attribute name=""Attr"">
2595+
<xs:simpleType>
2596+
<xs:list itemType=""xs:int""/>
2597+
</xs:simpleType>
2598+
</xs:attribute>
2599+
</xs:attributeGroup>
2600+
</xs:schema>";
2601+
2602+
var generator = new Generator
2603+
{
2604+
NamespaceProvider = new NamespaceProvider
2605+
{
2606+
GenerateNamespace = key => "Test",
2607+
},
2608+
GenerateInterfaces = true,
2609+
CollectionSettersMode = CollectionSettersMode.Private
2610+
};
2611+
var contents = ConvertXml(nameof(CollectionSetterInAttributeGroupInterfaceIsPrivateIfCollectionSetterModeIsPrivate), xsd, generator).ToArray();
2612+
var assembly = Compiler.Compile(nameof(CollectionSetterInAttributeGroupInterfaceIsPrivateIfCollectionSetterModeIsPrivate), contents);
2613+
2614+
var interfaceProperty = assembly.GetType("Test.IAttrGroup")?.GetProperty("Attr");
2615+
var implementerProperty = assembly.GetType("Test.Element")?.GetProperty("Attr");
2616+
Assert.NotNull(interfaceProperty);
2617+
Assert.NotNull(implementerProperty);
2618+
2619+
var interfaceHasPublicSetter = interfaceProperty.GetSetMethod() != null;
2620+
var implementerHasPublicSetter = implementerProperty.GetSetMethod() != null;
2621+
Assert.False(interfaceHasPublicSetter);
2622+
Assert.False(implementerHasPublicSetter);
2623+
}
2624+
2625+
[Fact]
2626+
public void CollectionSetterInAttributeGroupInterfaceIsPublicIfCollectionSetterModeIsPublic()
2627+
{
2628+
const string xsd = @"<?xml version=""1.0"" encoding=""UTF-8""?>
2629+
<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
2630+
<xs:element name=""Element"">
2631+
<xs:complexType>
2632+
<xs:attributeGroup ref=""AttrGroup""/>
2633+
</xs:complexType>
2634+
</xs:element>
2635+
2636+
<xs:attributeGroup name=""AttrGroup"">
2637+
<xs:attribute name=""Attr"">
2638+
<xs:simpleType>
2639+
<xs:list itemType=""xs:int""/>
2640+
</xs:simpleType>
2641+
</xs:attribute>
2642+
</xs:attributeGroup>
2643+
</xs:schema>";
2644+
2645+
var generator = new Generator
2646+
{
2647+
NamespaceProvider = new NamespaceProvider
2648+
{
2649+
GenerateNamespace = key => "Test",
2650+
},
2651+
GenerateInterfaces = true,
2652+
CollectionSettersMode = CollectionSettersMode.Public
2653+
};
2654+
var contents = ConvertXml(nameof(CollectionSetterInAttributeGroupInterfaceIsPublicIfCollectionSetterModeIsPublic), xsd, generator).ToArray();
2655+
var assembly = Compiler.Compile(nameof(CollectionSetterInAttributeGroupInterfaceIsPublicIfCollectionSetterModeIsPublic), contents);
2656+
2657+
var interfaceProperty = assembly.GetType("Test.IAttrGroup")?.GetProperty("Attr");
2658+
var implementerProperty = assembly.GetType("Test.Element")?.GetProperty("Attr");
2659+
Assert.NotNull(interfaceProperty);
2660+
Assert.NotNull(implementerProperty);
2661+
2662+
var interfaceHasPublicSetter = interfaceProperty.GetSetMethod() != null;
2663+
var implementerHasPublicSetter = implementerProperty.GetSetMethod() != null;
2664+
Assert.True(interfaceHasPublicSetter);
2665+
Assert.True(implementerHasPublicSetter);
2666+
}
25812667
}
25822668
}

0 commit comments

Comments
 (0)