Skip to content

Commit

Permalink
Merge pull request #407 from kronic/master
Browse files Browse the repository at this point in the history
Fix TotalDigitsRestriction for FractionDigitsRestrictionModel with 0
  • Loading branch information
mganss authored Aug 8, 2023
2 parents 32f94ea + 4e3182e commit bfcef72
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 57 deletions.
81 changes: 44 additions & 37 deletions XmlSchemaClassGenerator.Tests/IntegerTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,16 @@
using Xunit;

namespace XmlSchemaClassGenerator.Tests {
public class IntegerTypeTests
public sealed class IntegerTypeTests
{
private static IEnumerable<string> ConvertXml(string name, string xsd, Generator generatorPrototype = null)
private static IEnumerable<string> ConvertXml(string xsd, Generator generatorPrototype)
{
if (name is null)
{
throw new ArgumentNullException(nameof(name));
}

var writer = new MemoryOutputWriter();

var gen = new Generator
{
OutputWriter = writer,
Version = new VersionProvider("Tests", "1.0.0.1"),
Version = new("Tests", "1.0.0.1"),
NamespaceProvider = generatorPrototype.NamespaceProvider,
GenerateNullables = generatorPrototype.GenerateNullables,
IntegerDataType = generatorPrototype.IntegerDataType,
Expand All @@ -40,11 +35,8 @@ private static IEnumerable<string> ConvertXml(string name, string xsd, Generator

using (var stringReader = new StringReader(xsd))
{
var schema = XmlSchema.Read(stringReader, (s, e) =>
{
throw new InvalidOperationException($"{e.Severity}: {e.Message}",e.Exception);
});

var schema = XmlSchema.Read(stringReader, (_, e) => throw new InvalidOperationException($"{e.Severity}: {e.Message}",e.Exception));
ArgumentNullException.ThrowIfNull(schema);
set.Add(schema);
}

Expand Down Expand Up @@ -77,16 +69,19 @@ public void TestTotalDigits(int totalDigits, string expectedType)
</xs:complexType>
</xs:schema>";

var generatedType = ConvertXml(nameof(TestTotalDigits), xsd, new Generator
var generatedType = ConvertXml(
xsd, new()
{
NamespaceProvider = new NamespaceProvider
NamespaceProvider = new()
{
GenerateNamespace = key => "Test"
GenerateNamespace = _ => "Test"
}
});

var expectedProperty = $"public {expectedType} SomeValue";
Assert.Contains(expectedProperty, generatedType.First());
var generatedProperty = generatedType.First();

Assert.Contains(expectedProperty, generatedProperty);
}

[Theory]
Expand All @@ -111,18 +106,21 @@ public void TestFallbackType(int totalDigits, bool useTypeAsFallback, string exp
</xs:complexType>
</xs:schema>";

var generatedType = ConvertXml(nameof(TestTotalDigits), xsd, new Generator
var generatedType = ConvertXml(
xsd, new()
{
NamespaceProvider = new NamespaceProvider
NamespaceProvider = new()
{
GenerateNamespace = key => "Test"
GenerateNamespace = _ => "Test"
},
IntegerDataType = typeof(long),
UseIntegerDataTypeAsFallback = useTypeAsFallback
});

var expectedProperty = $"public {expectedType} SomeValue";
Assert.Contains(expectedProperty, generatedType.First());
var generatedProperty = generatedType.First();

Assert.Contains(expectedProperty, generatedProperty);
}

[Theory]
Expand Down Expand Up @@ -160,11 +158,12 @@ public void TestInclusiveRange(long minInclusive, ulong maxInclusive, string exp
</xs:complexType>
</xs:schema>";

var generatedType = ConvertXml(nameof(TestTotalDigits), xsd, new Generator
var generatedType = ConvertXml(
xsd, new()
{
NamespaceProvider = new NamespaceProvider
NamespaceProvider = new()
{
GenerateNamespace = key => "Test"
GenerateNamespace = _ => "Test"
}
});

Expand All @@ -189,25 +188,28 @@ public void TestDecimalFractionDigitsZeroTotalDigits(int totalDigits, string exp
<xs:sequence>
<xs:element name=""someValue"">
<xs:simpleType>
<xs:restriction base=""xs:integer"">
<xs:restriction base=""xs:decimal"">
<xs:totalDigits value=""{totalDigits}""/>
<xs:fractionDigits value=""0""/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>";

var generatedType = ConvertXml(nameof(TestTotalDigits), xsd, new Generator
var generatedType = ConvertXml(xsd, new()
{
NamespaceProvider = new NamespaceProvider
NamespaceProvider = new()
{
GenerateNamespace = key => "Test"
GenerateNamespace = _ => "Test"
}
});

var expectedProperty = $"public {expectedType} SomeValue";
Assert.Contains(expectedProperty, generatedType.First());
var generatedProperty = generatedType.First();

Assert.Contains(expectedProperty, generatedProperty);
}


Expand All @@ -224,27 +226,31 @@ public void TestDecimalFractionDigitsFallbackType(int totalDigits, bool useTypeA
<xs:sequence>
<xs:element name=""someValue"">
<xs:simpleType>
<xs:restriction base=""xs:integer"">
<xs:restriction base=""xs:decimal"">
<xs:totalDigits value=""{totalDigits}""/>
<xs:fractionDigits value=""0""/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>";

var generatedType = ConvertXml(nameof(TestTotalDigits), xsd, new Generator
var generatedType = ConvertXml(
xsd, new()
{
NamespaceProvider = new NamespaceProvider
NamespaceProvider = new()
{
GenerateNamespace = key => "Test"
GenerateNamespace = _ => "Test"
},
IntegerDataType = typeof(long),
UseIntegerDataTypeAsFallback = useTypeAsFallback
});

var expectedProperty = $"public {expectedType} SomeValue";
Assert.Contains(expectedProperty, generatedType.First());
var generatedProperty = generatedType.First();

Assert.Contains(expectedProperty, generatedProperty);
}

[Theory]
Expand Down Expand Up @@ -283,11 +289,12 @@ public void TestDecimalFractionDigitsZeroInclusiveRange(long minInclusive, ulong
</xs:complexType>
</xs:schema>";

var generatedType = ConvertXml(nameof(TestTotalDigits), xsd, new Generator
var generatedType = ConvertXml(
xsd, new()
{
NamespaceProvider = new NamespaceProvider
NamespaceProvider = new()
{
GenerateNamespace = key => "Test"
GenerateNamespace = _ => "Test"
}
});

Expand Down
53 changes: 33 additions & 20 deletions XmlSchemaClassGenerator/CodeUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,40 @@ private static Type GetIntegerDerivedType(XmlSchemaDatatype xml, GeneratorConfig
_ => typeof(decimal),
};

Type FromDigitRestriction(TotalDigitsRestrictionModel totalDigits) => xml.TypeCode switch
Type FromDigitRestriction(TotalDigitsRestrictionModel totalDigits
) => xml.TypeCode switch
{
XmlTypeCode.PositiveInteger or XmlTypeCode.NonNegativeInteger => totalDigits?.Value switch
{
< 3 => typeof(byte),
< 5 => typeof(ushort),
< 10 => typeof(uint),
< 20 => typeof(ulong),
< 30 => typeof(decimal),
_ => null
},
XmlTypeCode.Integer or XmlTypeCode.NegativeInteger or XmlTypeCode.NonPositiveInteger => totalDigits?.Value switch
{
< 3 => typeof(sbyte),
< 5 => typeof(short),
< 10 => typeof(int),
< 19 => typeof(long),
< 29 => typeof(decimal),
_ => null
},
_ => null,
XmlTypeCode.PositiveInteger or XmlTypeCode.NonNegativeInteger => totalDigits?.Value switch
{
< 3 => typeof(byte),
< 5 => typeof(ushort),
< 10 => typeof(uint),
< 20 => typeof(ulong),
< 30 => typeof(decimal),
_ => null
},
XmlTypeCode.Integer or XmlTypeCode.NegativeInteger or XmlTypeCode.NonPositiveInteger => totalDigits
?.Value switch
{
< 3 => typeof(sbyte),
< 5 => typeof(short),
< 10 => typeof(int),
< 19 => typeof(long),
< 29 => typeof(decimal),
_ => null
},
XmlTypeCode.Decimal
when restrictions.OfType<FractionDigitsRestrictionModel>().SingleOrDefault() is { IsSupported: true, Value: 0 } => totalDigits
?.Value switch
{
< 3 => typeof(sbyte),
< 5 => typeof(short),
< 10 => typeof(int),
< 19 => typeof(long),
< 29 => typeof(decimal),
_ => null
},
_ => null
};

Type FromFallback() => configuration.UseIntegerDataTypeAsFallback && configuration.IntegerDataType != null ? configuration.IntegerDataType : typeof(string);
Expand Down

0 comments on commit bfcef72

Please sign in to comment.