From 4fc5714db690c96ee539779703770523ee063f86 Mon Sep 17 00:00:00 2001 From: jmatss Date: Tue, 17 May 2022 22:49:31 +0200 Subject: [PATCH] Add unit tests for invalid interface generated --- XmlSchemaClassGenerator.Tests/XmlTests.cs | 86 +++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/XmlSchemaClassGenerator.Tests/XmlTests.cs b/XmlSchemaClassGenerator.Tests/XmlTests.cs index 43162982..0216e8d5 100644 --- a/XmlSchemaClassGenerator.Tests/XmlTests.cs +++ b/XmlSchemaClassGenerator.Tests/XmlTests.cs @@ -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 = @" + + + + + + + + + + + + + + +"; + + 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 = @" + + + + + + + + + + + + + + +"; + + 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); + } } }