From 9951829b4229f85b22b2b4eae9bac65d63b7ca53 Mon Sep 17 00:00:00 2001 From: Patrick Kranz Date: Fri, 28 Dec 2018 14:16:54 +0100 Subject: [PATCH] Added test for internal visibility. --- XmlSchemaClassGenerator.Tests/XmlTests.cs | 181 ++++++++++++++-------- 1 file changed, 113 insertions(+), 68 deletions(-) diff --git a/XmlSchemaClassGenerator.Tests/XmlTests.cs b/XmlSchemaClassGenerator.Tests/XmlTests.cs index bf6bac67..b830e08d 100644 --- a/XmlSchemaClassGenerator.Tests/XmlTests.cs +++ b/XmlSchemaClassGenerator.Tests/XmlTests.cs @@ -41,9 +41,10 @@ private IEnumerable ConvertXml(string name, string xsd, Generator genera DataAnnotationMode = generatorPrototype.DataAnnotationMode, GenerateDesignerCategoryAttribute = generatorPrototype.GenerateDesignerCategoryAttribute, EntityFramework = generatorPrototype.EntityFramework, + AssemblyVisible = generatorPrototype.AssemblyVisible, GenerateInterfaces = generatorPrototype.GenerateInterfaces, MemberVisitor = generatorPrototype.MemberVisitor, - CodeTypeReferenceOptions = generatorPrototype.CodeTypeReferenceOptions + CodeTypeReferenceOptions = generatorPrototype.CodeTypeReferenceOptions }; var set = new XmlSchemaSet(); @@ -389,12 +390,12 @@ public void DontGenerateElementForEmptyCollectionInChoice() } - [Theory] - [InlineData(CodeTypeReferenceOptions.GlobalReference, "[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]")] - [InlineData((CodeTypeReferenceOptions)0, "[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]")] - public void EditorBrowsableAttributeRespectsCodeTypeReferenceOptions(CodeTypeReferenceOptions codeTypeReferenceOptions, string expectedLine) - { - const string xsd = @" + [Theory] + [InlineData(CodeTypeReferenceOptions.GlobalReference, "[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]")] + [InlineData((CodeTypeReferenceOptions)0, "[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]")] + public void EditorBrowsableAttributeRespectsCodeTypeReferenceOptions(CodeTypeReferenceOptions codeTypeReferenceOptions, string expectedLine) + { + const string xsd = @" @@ -409,26 +410,26 @@ public void EditorBrowsableAttributeRespectsCodeTypeReferenceOptions(CodeTypeRef "; - var generatedType = ConvertXml(nameof(EditorBrowsableAttributeRespectsCodeTypeReferenceOptions), xsd, new Generator - { - CodeTypeReferenceOptions = codeTypeReferenceOptions, - GenerateNullables = true, - GenerateInterfaces = false, - NamespaceProvider = new NamespaceProvider - { - GenerateNamespace = key => "Test" - } - }); - - Assert.Contains( - expectedLine, - generatedType.First()); - } - - [Fact] - public void MixedTypeMustNotCollideWithExistingMembers() - { - const string xsd = @" + var generatedType = ConvertXml(nameof(EditorBrowsableAttributeRespectsCodeTypeReferenceOptions), xsd, new Generator + { + CodeTypeReferenceOptions = codeTypeReferenceOptions, + GenerateNullables = true, + GenerateInterfaces = false, + NamespaceProvider = new NamespaceProvider + { + GenerateNamespace = key => "Test" + } + }); + + Assert.Contains( + expectedLine, + generatedType.First()); + } + + [Fact] + public void MixedTypeMustNotCollideWithExistingMembers() + { + const string xsd = @" @@ -437,23 +438,23 @@ public void MixedTypeMustNotCollideWithExistingMembers() "; - var generatedType = ConvertXml(nameof(MixedTypeMustNotCollideWithExistingMembers), xsd, new Generator - { - NamespaceProvider = new NamespaceProvider - { - GenerateNamespace = key => "Test" - } - }); - - Assert.Contains( - @"public string[] Text_1 { get; set; }", - generatedType.First()); - } - - [Fact] - public void MixedTypeMustNotCollideWithContainingTypeName() - { - const string xsd = @" + var generatedType = ConvertXml(nameof(MixedTypeMustNotCollideWithExistingMembers), xsd, new Generator + { + NamespaceProvider = new NamespaceProvider + { + GenerateNamespace = key => "Test" + } + }); + + Assert.Contains( + @"public string[] Text_1 { get; set; }", + generatedType.First()); + } + + [Fact] + public void MixedTypeMustNotCollideWithContainingTypeName() + { + const string xsd = @" @@ -461,30 +462,30 @@ public void MixedTypeMustNotCollideWithContainingTypeName() "; - var generatedType = ConvertXml(nameof(MixedTypeMustNotCollideWithExistingMembers), xsd, new Generator - { - NamespaceProvider = new NamespaceProvider - { - GenerateNamespace = key => "Test" - } - }); - - Assert.Contains( - @"public string[] Text_1 { get; set; }", - generatedType.First()); - } - - [Theory] - [InlineData(@"xml/sameattributenames.xsd", @"xml/sameattributenames_import.xsd")] - public void CollidingAttributeAndPropertyNamesCanBeResolved(params string[] files) - { - // Compilation would previously throw due to duplicate type name within type - var assembly = Compiler.GenerateFiles("AttributesWithSameName", files); - - Assert.NotNull(assembly); - } - - [Fact] + var generatedType = ConvertXml(nameof(MixedTypeMustNotCollideWithExistingMembers), xsd, new Generator + { + NamespaceProvider = new NamespaceProvider + { + GenerateNamespace = key => "Test" + } + }); + + Assert.Contains( + @"public string[] Text_1 { get; set; }", + generatedType.First()); + } + + [Theory] + [InlineData(@"xml/sameattributenames.xsd", @"xml/sameattributenames_import.xsd")] + public void CollidingAttributeAndPropertyNamesCanBeResolved(params string[] files) + { + // Compilation would previously throw due to duplicate type name within type + var assembly = Compiler.GenerateFiles("AttributesWithSameName", files); + + Assert.NotNull(assembly); + } + + [Fact] public void ComplexTypeWithAttributeGroupExtension() { const string xsd = @" @@ -630,6 +631,50 @@ public void ChoiceMembersAreNullable() Assert.Contains("Opt4Specified", content); } + [Fact] + public void AssemblyVisibleIsInternal() + { + // We test to see whether choices which are part of a larger ComplexType are marked as nullable. + // Because nullability isn't directly exposed in the generated C#, we use "XXXSpecified" on a value type + // as a proxy. + + const string xsd = @" + + + + + + + + + + + + + + + + + + + +"; + + var generator = new Generator + { + NamespaceProvider = new NamespaceProvider + { + GenerateNamespace = key => "Test" + }, + AssemblyVisible = true + }; + var contents = ConvertXml(nameof(ComplexTypeWithAttributeGroupExtension), xsd, generator); + var content = Assert.Single(contents); + + Assert.Contains("internal partial class RootSub", content); + Assert.Contains("internal partial class Root", content); + } + private static void CompareOutput(string expected, string actual) { string Normalize(string input) => Regex.Replace(input, @"[ \t]*\r\n", "\n");