Skip to content

Commit 24ee696

Browse files
author
Michael Ganss
committed
Add test for bool default value parsing
Make syntax consistent with project conventions
1 parent c19f626 commit 24ee696

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

XmlSchemaClassGenerator.Tests/XmlTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,39 @@ public void DecimalSeparatorTest()
766766
Assert.Contains("private decimal _someAttr = 1.5m;", content);
767767
}
768768

769+
[Fact]
770+
public void BoolTest()
771+
{
772+
// see https://github.com/mganss/XmlSchemaClassGenerator/issues/103
773+
774+
const string xsd = @"<?xml version=""1.0"" encoding=""UTF-8""?>
775+
<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"" elementFormDefault=""qualified"" attributeFormDefault=""unqualified"">
776+
<xs:complexType name=""NamedType"">
777+
<xs:attribute name=""b0"" type=""xs:boolean"" default=""0"" />
778+
<xs:attribute name=""b1"" type=""xs:boolean"" default=""1"" />
779+
<xs:attribute name=""bf"" type=""xs:boolean"" default=""false"" />
780+
<xs:attribute name=""bt"" type=""xs:boolean"" default=""true"" />
781+
</xs:complexType>
782+
</xs:schema>";
783+
784+
var generator = new Generator
785+
{
786+
NamespaceProvider = new NamespaceProvider
787+
{
788+
GenerateNamespace = key => "Test",
789+
},
790+
GenerateInterfaces = true,
791+
AssemblyVisible = true
792+
};
793+
var contents = ConvertXml(nameof(ComplexTypeWithAttributeGroupExtension), xsd, generator);
794+
var content = Assert.Single(contents);
795+
796+
Assert.Contains("private bool _b0 = false;", content);
797+
Assert.Contains("private bool _b1 = true;", content);
798+
Assert.Contains("private bool _bf = false;", content);
799+
Assert.Contains("private bool _bt = true;", content);
800+
}
801+
769802
private static void CompareOutput(string expected, string actual)
770803
{
771804
string Normalize(string input) => Regex.Replace(input, @"[ \t]*\r\n", "\n");

XmlSchemaClassGenerator/TypeModel.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,19 +1274,17 @@ public override CodeExpression GetDefaultValueFor(string defaultString, bool att
12741274
{
12751275
var rv = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(DateTime)), "Parse", new CodePrimitiveExpression(defaultString));
12761276
return rv;
1277-
}
1278-
else if (type == typeof(Boolean) && !String.IsNullOrEmpty(defaultString) && !String.IsNullOrWhiteSpace(defaultString)) {
1279-
if (defaultString == "0") {
1280-
//alternate false
1277+
}
1278+
else if (type == typeof(bool) && !string.IsNullOrWhiteSpace(defaultString))
1279+
{
1280+
if (defaultString == "0")
12811281
return new CodePrimitiveExpression(false);
1282-
} else if (defaultString == "1") {
1282+
else if (defaultString == "1")
12831283
return new CodePrimitiveExpression(true);
1284-
} else {
1284+
else
12851285
return new CodePrimitiveExpression(Convert.ChangeType(defaultString, ValueType));
1286-
}
12871286
}
12881287

1289-
12901288
return new CodePrimitiveExpression(Convert.ChangeType(defaultString, ValueType, CultureInfo.InvariantCulture));
12911289
}
12921290

0 commit comments

Comments
 (0)