Skip to content

Commit 045e735

Browse files
author
Michael Ganss
committed
Add NamespaceKey tests
Remove code duplication
1 parent bda2431 commit 045e735

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

XmlSchemaClassGenerator.Tests/NamespaceProviderTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,23 @@ [new NamespaceKey("x")] = "c",
4545
Assert.Equal("y", ns[new NamespaceKey("y")]);
4646
Assert.Throws<KeyNotFoundException>(() => ns[new NamespaceKey("z")]);
4747
}
48+
49+
[Fact]
50+
public void NamespaceKeyComparableTest()
51+
{
52+
Assert.Equal(-1, new NamespaceKey((Uri)null).CompareTo(new NamespaceKey(new Uri("http://test"))));
53+
Assert.Equal(1, new NamespaceKey(new Uri("http://test")).CompareTo(new NamespaceKey((Uri)null)));
54+
Assert.NotEqual(0, new NamespaceKey(new Uri("http://test")).CompareTo(new NamespaceKey(new Uri("http://test2"))));
55+
Assert.True(new NamespaceKey("http://test").Equals((object)new NamespaceKey("http://test")));
56+
Assert.False(new NamespaceKey("http://test").Equals((object)null));
57+
Assert.NotEqual(0, ((IComparable)new NamespaceKey("http://test")).CompareTo(null));
58+
Assert.True(new NamespaceKey("http://test") == new NamespaceKey("http://test"));
59+
Assert.True(((NamespaceKey)null) == ((NamespaceKey)null));
60+
Assert.True(new NamespaceKey("http://test") > null);
61+
Assert.False(new NamespaceKey("http://test") < null);
62+
Assert.True(new NamespaceKey("http://test") >= null);
63+
Assert.False(new NamespaceKey("http://test") <= null);
64+
Assert.True(new NamespaceKey("http://test") != null);
65+
}
4866
}
4967
}

XmlSchemaClassGenerator/NamespaceKey.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public bool Equals(NamespaceKey other)
4545

4646
public int CompareTo(NamespaceKey other)
4747
{
48+
if (other == null) return 1;
49+
4850
if (Source == null && other.Source != null)
4951
{
5052
return -1;
@@ -107,10 +109,18 @@ int IComparable.CompareTo(object obj)
107109
{
108110
return left.CompareTo(right) > 0;
109111
}
112+
public static bool operator >=(NamespaceKey left, NamespaceKey right)
113+
{
114+
return left.CompareTo(right) >= 0;
115+
}
110116
public static bool operator <(NamespaceKey left, NamespaceKey right)
111117
{
112118
return left.CompareTo(right) < 0;
113119
}
120+
public static bool operator <=(NamespaceKey left, NamespaceKey right)
121+
{
122+
return left.CompareTo(right) <= 0;
123+
}
114124
public static bool operator !=(NamespaceKey left, NamespaceKey right)
115125
{
116126
return !(left == right);

XmlSchemaClassGenerator/TypeModel.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,22 +1131,17 @@ public SimpleModel(GeneratorConfiguration configuration)
11311131
public static string GetCollectionDefinitionName(string typeName, GeneratorConfiguration configuration)
11321132
{
11331133
var typeRef = new CodeTypeReference(configuration.CollectionType, configuration.CodeTypeReferenceOptions);
1134-
if (configuration.CollectionType.IsGenericTypeDefinition)
1135-
{
1136-
typeRef.TypeArguments.Add(typeName);
1137-
}
1138-
var typeOfExpr = new CodeTypeOfExpression(typeRef);
1139-
var writer = new System.IO.StringWriter();
1140-
CSharpProvider.GenerateCodeFromExpression(typeOfExpr, writer, new CodeGeneratorOptions());
1141-
var fullTypeName = writer.ToString();
1142-
Debug.Assert(fullTypeName.StartsWith("typeof(") && fullTypeName.EndsWith(")"));
1143-
fullTypeName = fullTypeName.Substring(7, fullTypeName.Length - 8);
1144-
return fullTypeName;
1134+
return GetFullTypeName(typeName, configuration, typeRef);
11451135
}
11461136

11471137
public static string GetCollectionImplementationName(string typeName, GeneratorConfiguration configuration)
11481138
{
11491139
var typeRef = new CodeTypeReference(configuration.CollectionImplementationType ?? configuration.CollectionType, configuration.CodeTypeReferenceOptions);
1140+
return GetFullTypeName(typeName, configuration, typeRef);
1141+
}
1142+
1143+
private static string GetFullTypeName(string typeName, GeneratorConfiguration configuration, CodeTypeReference typeRef)
1144+
{
11501145
if (configuration.CollectionType.IsGenericTypeDefinition)
11511146
{
11521147
typeRef.TypeArguments.Add(typeName);

0 commit comments

Comments
 (0)