forked from mganss/XmlSchemaClassGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntegerTypeTests.cs
167 lines (150 loc) · 5.88 KB
/
IntegerTypeTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Schema;
using Xunit;
namespace XmlSchemaClassGenerator.Tests {
public class IntegerTypeTests
{
private static IEnumerable<string> ConvertXml(string name, string xsd, Generator generatorPrototype = null)
{
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"),
NamespaceProvider = generatorPrototype.NamespaceProvider,
GenerateNullables = generatorPrototype.GenerateNullables,
IntegerDataType = generatorPrototype.IntegerDataType,
UseIntegerDataTypeAsFallback = generatorPrototype.UseIntegerDataTypeAsFallback,
DataAnnotationMode = generatorPrototype.DataAnnotationMode,
GenerateDesignerCategoryAttribute = generatorPrototype.GenerateDesignerCategoryAttribute,
GenerateComplexTypesForCollections = generatorPrototype.GenerateComplexTypesForCollections,
EntityFramework = generatorPrototype.EntityFramework,
AssemblyVisible = generatorPrototype.AssemblyVisible,
GenerateInterfaces = generatorPrototype.GenerateInterfaces,
MemberVisitor = generatorPrototype.MemberVisitor,
CodeTypeReferenceOptions = generatorPrototype.CodeTypeReferenceOptions
};
var set = new XmlSchemaSet();
using (var stringReader = new StringReader(xsd))
{
var schema = XmlSchema.Read(stringReader, (s, e) =>
{
throw new InvalidOperationException($"{e.Severity}: {e.Message}",e.Exception);
});
set.Add(schema);
}
gen.Generate(set);
return writer.Content;
}
[Theory]
[InlineData(2, "sbyte")]
[InlineData(4, "short")]
[InlineData(9, "int")]
[InlineData(18, "long")]
[InlineData(28, "decimal")]
[InlineData(29, "string")]
public void TestTotalDigits(int totalDigits, string expectedType)
{
var xsd = @$"<?xml version=""1.0"" encoding=""UTF-8""?>
<xs:schema elementFormDefault=""qualified"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
<xs:complexType name=""document"">
<xs:sequence>
<xs:element name=""someValue"">
<xs:simpleType>
<xs:restriction base=""xs:integer"">
<xs:totalDigits value=""{totalDigits}""/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>";
var generatedType = ConvertXml(nameof(TestTotalDigits), xsd, new Generator
{
NamespaceProvider = new NamespaceProvider
{
GenerateNamespace = key => "Test"
}
});
var expectedProperty = $"public {expectedType} SomeValue";
Assert.Contains(expectedProperty, generatedType.First());
}
[Theory]
[InlineData(4, false, "long")]
[InlineData(30, false, "long")]
[InlineData(4, true, "short")]
[InlineData(30, true, "long")]
public void TestFallbackType(int totalDigits, bool useTypeAsFallback, string expectedType)
{
var xsd = @$"<?xml version=""1.0"" encoding=""UTF-8""?>
<xs:schema elementFormDefault=""qualified"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
<xs:complexType name=""document"">
<xs:sequence>
<xs:element name=""someValue"">
<xs:simpleType>
<xs:restriction base=""xs:integer"">
<xs:totalDigits value=""{totalDigits}""/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>";
var generatedType = ConvertXml(nameof(TestTotalDigits), xsd, new Generator
{
NamespaceProvider = new NamespaceProvider
{
GenerateNamespace = key => "Test"
},
IntegerDataType = typeof(long),
UseIntegerDataTypeAsFallback = useTypeAsFallback
});
var expectedProperty = $"public {expectedType} SomeValue";
Assert.Contains(expectedProperty, generatedType.First());
}
[Theory]
[InlineData(1, 100, "byte")]
[InlineData(-100, 100, "sbyte")]
[InlineData(1, 1000, "ushort")]
[InlineData(-1000, 1000, "short")]
[InlineData(1, 100000, "uint")]
[InlineData(-100000, 100000, "int")]
[InlineData(1, 10000000000, "ulong")]
[InlineData(-10000000000, 10000000000, "long")]
public void TestInclusiveRange(long minInclusive, long maxInclusive, string expectedType)
{
var xsd = @$"<?xml version=""1.0"" encoding=""UTF-8""?>
<xs:schema elementFormDefault=""qualified"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
<xs:complexType name=""document"">
<xs:sequence>
<xs:element name=""someValue"">
<xs:simpleType>
<xs:restriction base=""xs:integer"">
<xs:minInclusive value=""{minInclusive}""/>
<xs:maxInclusive value=""{maxInclusive}""/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>";
var generatedType = ConvertXml(nameof(TestTotalDigits), xsd, new Generator
{
NamespaceProvider = new NamespaceProvider
{
GenerateNamespace = key => "Test"
}
});
var expectedProperty = $"public {expectedType} SomeValue";
Assert.Contains(expectedProperty, generatedType.First());
}
}
}