From 045e735616bed926243a23c0503ec8d1f29b0abd Mon Sep 17 00:00:00 2001 From: Michael Ganss Date: Tue, 25 Sep 2018 16:56:07 +0200 Subject: [PATCH] Add NamespaceKey tests Remove code duplication --- .../NamespaceProviderTests.cs | 18 ++++++++++++++++++ XmlSchemaClassGenerator/NamespaceKey.cs | 10 ++++++++++ XmlSchemaClassGenerator/TypeModel.cs | 17 ++++++----------- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/XmlSchemaClassGenerator.Tests/NamespaceProviderTests.cs b/XmlSchemaClassGenerator.Tests/NamespaceProviderTests.cs index d00a81e1..f65d2e36 100644 --- a/XmlSchemaClassGenerator.Tests/NamespaceProviderTests.cs +++ b/XmlSchemaClassGenerator.Tests/NamespaceProviderTests.cs @@ -45,5 +45,23 @@ [new NamespaceKey("x")] = "c", Assert.Equal("y", ns[new NamespaceKey("y")]); Assert.Throws(() => ns[new NamespaceKey("z")]); } + + [Fact] + public void NamespaceKeyComparableTest() + { + Assert.Equal(-1, new NamespaceKey((Uri)null).CompareTo(new NamespaceKey(new Uri("http://test")))); + Assert.Equal(1, new NamespaceKey(new Uri("http://test")).CompareTo(new NamespaceKey((Uri)null))); + Assert.NotEqual(0, new NamespaceKey(new Uri("http://test")).CompareTo(new NamespaceKey(new Uri("http://test2")))); + Assert.True(new NamespaceKey("http://test").Equals((object)new NamespaceKey("http://test"))); + Assert.False(new NamespaceKey("http://test").Equals((object)null)); + Assert.NotEqual(0, ((IComparable)new NamespaceKey("http://test")).CompareTo(null)); + Assert.True(new NamespaceKey("http://test") == new NamespaceKey("http://test")); + Assert.True(((NamespaceKey)null) == ((NamespaceKey)null)); + Assert.True(new NamespaceKey("http://test") > null); + Assert.False(new NamespaceKey("http://test") < null); + Assert.True(new NamespaceKey("http://test") >= null); + Assert.False(new NamespaceKey("http://test") <= null); + Assert.True(new NamespaceKey("http://test") != null); + } } } diff --git a/XmlSchemaClassGenerator/NamespaceKey.cs b/XmlSchemaClassGenerator/NamespaceKey.cs index 8a8789da..a67f7ef8 100644 --- a/XmlSchemaClassGenerator/NamespaceKey.cs +++ b/XmlSchemaClassGenerator/NamespaceKey.cs @@ -45,6 +45,8 @@ public bool Equals(NamespaceKey other) public int CompareTo(NamespaceKey other) { + if (other == null) return 1; + if (Source == null && other.Source != null) { return -1; @@ -107,10 +109,18 @@ int IComparable.CompareTo(object obj) { return left.CompareTo(right) > 0; } + public static bool operator >=(NamespaceKey left, NamespaceKey right) + { + return left.CompareTo(right) >= 0; + } public static bool operator <(NamespaceKey left, NamespaceKey right) { return left.CompareTo(right) < 0; } + public static bool operator <=(NamespaceKey left, NamespaceKey right) + { + return left.CompareTo(right) <= 0; + } public static bool operator !=(NamespaceKey left, NamespaceKey right) { return !(left == right); diff --git a/XmlSchemaClassGenerator/TypeModel.cs b/XmlSchemaClassGenerator/TypeModel.cs index 196199aa..f2d4a27d 100644 --- a/XmlSchemaClassGenerator/TypeModel.cs +++ b/XmlSchemaClassGenerator/TypeModel.cs @@ -1131,22 +1131,17 @@ public SimpleModel(GeneratorConfiguration configuration) public static string GetCollectionDefinitionName(string typeName, GeneratorConfiguration configuration) { var typeRef = new CodeTypeReference(configuration.CollectionType, configuration.CodeTypeReferenceOptions); - if (configuration.CollectionType.IsGenericTypeDefinition) - { - typeRef.TypeArguments.Add(typeName); - } - var typeOfExpr = new CodeTypeOfExpression(typeRef); - var writer = new System.IO.StringWriter(); - CSharpProvider.GenerateCodeFromExpression(typeOfExpr, writer, new CodeGeneratorOptions()); - var fullTypeName = writer.ToString(); - Debug.Assert(fullTypeName.StartsWith("typeof(") && fullTypeName.EndsWith(")")); - fullTypeName = fullTypeName.Substring(7, fullTypeName.Length - 8); - return fullTypeName; + return GetFullTypeName(typeName, configuration, typeRef); } public static string GetCollectionImplementationName(string typeName, GeneratorConfiguration configuration) { var typeRef = new CodeTypeReference(configuration.CollectionImplementationType ?? configuration.CollectionType, configuration.CodeTypeReferenceOptions); + return GetFullTypeName(typeName, configuration, typeRef); + } + + private static string GetFullTypeName(string typeName, GeneratorConfiguration configuration, CodeTypeReference typeRef) + { if (configuration.CollectionType.IsGenericTypeDefinition) { typeRef.TypeArguments.Add(typeName);