Skip to content

Commit 9951829

Browse files
committed
Added test for internal visibility.
1 parent 6a0e6ec commit 9951829

File tree

1 file changed

+113
-68
lines changed

1 file changed

+113
-68
lines changed

XmlSchemaClassGenerator.Tests/XmlTests.cs

Lines changed: 113 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ private IEnumerable<string> ConvertXml(string name, string xsd, Generator genera
4141
DataAnnotationMode = generatorPrototype.DataAnnotationMode,
4242
GenerateDesignerCategoryAttribute = generatorPrototype.GenerateDesignerCategoryAttribute,
4343
EntityFramework = generatorPrototype.EntityFramework,
44+
AssemblyVisible = generatorPrototype.AssemblyVisible,
4445
GenerateInterfaces = generatorPrototype.GenerateInterfaces,
4546
MemberVisitor = generatorPrototype.MemberVisitor,
46-
CodeTypeReferenceOptions = generatorPrototype.CodeTypeReferenceOptions
47+
CodeTypeReferenceOptions = generatorPrototype.CodeTypeReferenceOptions
4748
};
4849

4950
var set = new XmlSchemaSet();
@@ -389,12 +390,12 @@ public void DontGenerateElementForEmptyCollectionInChoice()
389390
}
390391

391392

392-
[Theory]
393-
[InlineData(CodeTypeReferenceOptions.GlobalReference, "[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]")]
394-
[InlineData((CodeTypeReferenceOptions)0, "[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]")]
395-
public void EditorBrowsableAttributeRespectsCodeTypeReferenceOptions(CodeTypeReferenceOptions codeTypeReferenceOptions, string expectedLine)
396-
{
397-
const string xsd = @"<?xml version=""1.0"" encoding=""UTF-8""?>
393+
[Theory]
394+
[InlineData(CodeTypeReferenceOptions.GlobalReference, "[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]")]
395+
[InlineData((CodeTypeReferenceOptions)0, "[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]")]
396+
public void EditorBrowsableAttributeRespectsCodeTypeReferenceOptions(CodeTypeReferenceOptions codeTypeReferenceOptions, string expectedLine)
397+
{
398+
const string xsd = @"<?xml version=""1.0"" encoding=""UTF-8""?>
398399
<xs:schema elementFormDefault=""qualified"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
399400
<xs:complexType name=""document"">
400401
<xs:attribute name=""some-value"">
@@ -409,26 +410,26 @@ public void EditorBrowsableAttributeRespectsCodeTypeReferenceOptions(CodeTypeRef
409410
</xs:complexType>
410411
</xs:schema>";
411412

412-
var generatedType = ConvertXml(nameof(EditorBrowsableAttributeRespectsCodeTypeReferenceOptions), xsd, new Generator
413-
{
414-
CodeTypeReferenceOptions = codeTypeReferenceOptions,
415-
GenerateNullables = true,
416-
GenerateInterfaces = false,
417-
NamespaceProvider = new NamespaceProvider
418-
{
419-
GenerateNamespace = key => "Test"
420-
}
421-
});
422-
423-
Assert.Contains(
424-
expectedLine,
425-
generatedType.First());
426-
}
427-
428-
[Fact]
429-
public void MixedTypeMustNotCollideWithExistingMembers()
430-
{
431-
const string xsd = @"<?xml version=""1.0"" encoding=""UTF-8""?>
413+
var generatedType = ConvertXml(nameof(EditorBrowsableAttributeRespectsCodeTypeReferenceOptions), xsd, new Generator
414+
{
415+
CodeTypeReferenceOptions = codeTypeReferenceOptions,
416+
GenerateNullables = true,
417+
GenerateInterfaces = false,
418+
NamespaceProvider = new NamespaceProvider
419+
{
420+
GenerateNamespace = key => "Test"
421+
}
422+
});
423+
424+
Assert.Contains(
425+
expectedLine,
426+
generatedType.First());
427+
}
428+
429+
[Fact]
430+
public void MixedTypeMustNotCollideWithExistingMembers()
431+
{
432+
const string xsd = @"<?xml version=""1.0"" encoding=""UTF-8""?>
432433
<xs:schema elementFormDefault=""qualified"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" targetNamespace=""http://local.none"" xmlns:l=""http://local.none"">
433434
<xs:element name=""document"" type=""l:elem"">
434435
</xs:element>
@@ -437,54 +438,54 @@ public void MixedTypeMustNotCollideWithExistingMembers()
437438
</xs:complexType>
438439
</xs:schema>";
439440

440-
var generatedType = ConvertXml(nameof(MixedTypeMustNotCollideWithExistingMembers), xsd, new Generator
441-
{
442-
NamespaceProvider = new NamespaceProvider
443-
{
444-
GenerateNamespace = key => "Test"
445-
}
446-
});
447-
448-
Assert.Contains(
449-
@"public string[] Text_1 { get; set; }",
450-
generatedType.First());
451-
}
452-
453-
[Fact]
454-
public void MixedTypeMustNotCollideWithContainingTypeName()
455-
{
456-
const string xsd = @"<?xml version=""1.0"" encoding=""UTF-8""?>
441+
var generatedType = ConvertXml(nameof(MixedTypeMustNotCollideWithExistingMembers), xsd, new Generator
442+
{
443+
NamespaceProvider = new NamespaceProvider
444+
{
445+
GenerateNamespace = key => "Test"
446+
}
447+
});
448+
449+
Assert.Contains(
450+
@"public string[] Text_1 { get; set; }",
451+
generatedType.First());
452+
}
453+
454+
[Fact]
455+
public void MixedTypeMustNotCollideWithContainingTypeName()
456+
{
457+
const string xsd = @"<?xml version=""1.0"" encoding=""UTF-8""?>
457458
<xs:schema elementFormDefault=""qualified"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" targetNamespace=""http://local.none"" xmlns:l=""http://local.none"">
458459
<xs:element name=""document"" type=""l:Text"">
459460
</xs:element>
460461
<xs:complexType name=""Text"" mixed=""true"">
461462
</xs:complexType>
462463
</xs:schema>";
463464

464-
var generatedType = ConvertXml(nameof(MixedTypeMustNotCollideWithExistingMembers), xsd, new Generator
465-
{
466-
NamespaceProvider = new NamespaceProvider
467-
{
468-
GenerateNamespace = key => "Test"
469-
}
470-
});
471-
472-
Assert.Contains(
473-
@"public string[] Text_1 { get; set; }",
474-
generatedType.First());
475-
}
476-
477-
[Theory]
478-
[InlineData(@"xml/sameattributenames.xsd", @"xml/sameattributenames_import.xsd")]
479-
public void CollidingAttributeAndPropertyNamesCanBeResolved(params string[] files)
480-
{
481-
// Compilation would previously throw due to duplicate type name within type
482-
var assembly = Compiler.GenerateFiles("AttributesWithSameName", files);
483-
484-
Assert.NotNull(assembly);
485-
}
486-
487-
[Fact]
465+
var generatedType = ConvertXml(nameof(MixedTypeMustNotCollideWithExistingMembers), xsd, new Generator
466+
{
467+
NamespaceProvider = new NamespaceProvider
468+
{
469+
GenerateNamespace = key => "Test"
470+
}
471+
});
472+
473+
Assert.Contains(
474+
@"public string[] Text_1 { get; set; }",
475+
generatedType.First());
476+
}
477+
478+
[Theory]
479+
[InlineData(@"xml/sameattributenames.xsd", @"xml/sameattributenames_import.xsd")]
480+
public void CollidingAttributeAndPropertyNamesCanBeResolved(params string[] files)
481+
{
482+
// Compilation would previously throw due to duplicate type name within type
483+
var assembly = Compiler.GenerateFiles("AttributesWithSameName", files);
484+
485+
Assert.NotNull(assembly);
486+
}
487+
488+
[Fact]
488489
public void ComplexTypeWithAttributeGroupExtension()
489490
{
490491
const string xsd = @"<?xml version=""1.0"" encoding=""UTF-8""?>
@@ -630,6 +631,50 @@ public void ChoiceMembersAreNullable()
630631
Assert.Contains("Opt4Specified", content);
631632
}
632633

634+
[Fact]
635+
public void AssemblyVisibleIsInternal()
636+
{
637+
// We test to see whether choices which are part of a larger ComplexType are marked as nullable.
638+
// Because nullability isn't directly exposed in the generated C#, we use "XXXSpecified" on a value type
639+
// as a proxy.
640+
641+
const string xsd = @"<?xml version=""1.0"" encoding=""UTF-8""?>
642+
<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:xlink=""http://www.w3.org/1999/xlink"" elementFormDefault=""qualified"" attributeFormDefault=""unqualified"">
643+
<xs:complexType name=""Root"">
644+
<xs:sequence>
645+
<!-- Choice directly inside a complex type -->
646+
<xs:element name=""Sub"">
647+
<xs:complexType>
648+
<xs:choice>
649+
<xs:element name=""Opt1"" type=""xs:int""/>
650+
<xs:element name=""Opt2"" type=""xs:int""/>
651+
</xs:choice>
652+
</xs:complexType>
653+
</xs:element>
654+
<!-- Choice as part of a larger sequence -->
655+
<xs:choice>
656+
<xs:element name=""Opt3"" type=""xs:int""/>
657+
<xs:element name=""Opt4"" type=""xs:int""/>
658+
</xs:choice>
659+
</xs:sequence>
660+
</xs:complexType>
661+
</xs:schema>";
662+
663+
var generator = new Generator
664+
{
665+
NamespaceProvider = new NamespaceProvider
666+
{
667+
GenerateNamespace = key => "Test"
668+
},
669+
AssemblyVisible = true
670+
};
671+
var contents = ConvertXml(nameof(ComplexTypeWithAttributeGroupExtension), xsd, generator);
672+
var content = Assert.Single(contents);
673+
674+
Assert.Contains("internal partial class RootSub", content);
675+
Assert.Contains("internal partial class Root", content);
676+
}
677+
633678
private static void CompareOutput(string expected, string actual)
634679
{
635680
string Normalize(string input) => Regex.Replace(input, @"[ \t]*\r\n", "\n");

0 commit comments

Comments
 (0)