Skip to content

Commit

Permalink
Add NamespaceKey tests
Browse files Browse the repository at this point in the history
Remove code duplication
  • Loading branch information
Michael Ganss committed Sep 25, 2018
1 parent bda2431 commit 045e735
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
18 changes: 18 additions & 0 deletions XmlSchemaClassGenerator.Tests/NamespaceProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,23 @@ [new NamespaceKey("x")] = "c",
Assert.Equal("y", ns[new NamespaceKey("y")]);
Assert.Throws<KeyNotFoundException>(() => 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);
}
}
}
10 changes: 10 additions & 0 deletions XmlSchemaClassGenerator/NamespaceKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 6 additions & 11 deletions XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 045e735

Please sign in to comment.