Skip to content

Commit

Permalink
Add test for bool default value parsing
Browse files Browse the repository at this point in the history
Make syntax consistent with project conventions
  • Loading branch information
Michael Ganss committed Feb 21, 2019
1 parent c19f626 commit 24ee696
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
33 changes: 33 additions & 0 deletions XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,39 @@ public void DecimalSeparatorTest()
Assert.Contains("private decimal _someAttr = 1.5m;", content);
}

[Fact]
public void BoolTest()
{
// see https://github.com/mganss/XmlSchemaClassGenerator/issues/103

const string xsd = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"" elementFormDefault=""qualified"" attributeFormDefault=""unqualified"">
<xs:complexType name=""NamedType"">
<xs:attribute name=""b0"" type=""xs:boolean"" default=""0"" />
<xs:attribute name=""b1"" type=""xs:boolean"" default=""1"" />
<xs:attribute name=""bf"" type=""xs:boolean"" default=""false"" />
<xs:attribute name=""bt"" type=""xs:boolean"" default=""true"" />
</xs:complexType>
</xs:schema>";

var generator = new Generator
{
NamespaceProvider = new NamespaceProvider
{
GenerateNamespace = key => "Test",
},
GenerateInterfaces = true,
AssemblyVisible = true
};
var contents = ConvertXml(nameof(ComplexTypeWithAttributeGroupExtension), xsd, generator);
var content = Assert.Single(contents);

Assert.Contains("private bool _b0 = false;", content);
Assert.Contains("private bool _b1 = true;", content);
Assert.Contains("private bool _bf = false;", content);
Assert.Contains("private bool _bt = true;", content);
}

private static void CompareOutput(string expected, string actual)
{
string Normalize(string input) => Regex.Replace(input, @"[ \t]*\r\n", "\n");
Expand Down
14 changes: 6 additions & 8 deletions XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1274,19 +1274,17 @@ public override CodeExpression GetDefaultValueFor(string defaultString, bool att
{
var rv = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(DateTime)), "Parse", new CodePrimitiveExpression(defaultString));
return rv;
}
else if (type == typeof(Boolean) && !String.IsNullOrEmpty(defaultString) && !String.IsNullOrWhiteSpace(defaultString)) {
if (defaultString == "0") {
//alternate false
}
else if (type == typeof(bool) && !string.IsNullOrWhiteSpace(defaultString))
{
if (defaultString == "0")
return new CodePrimitiveExpression(false);
} else if (defaultString == "1") {
else if (defaultString == "1")
return new CodePrimitiveExpression(true);
} else {
else
return new CodePrimitiveExpression(Convert.ChangeType(defaultString, ValueType));
}
}


return new CodePrimitiveExpression(Convert.ChangeType(defaultString, ValueType, CultureInfo.InvariantCulture));
}

Expand Down

0 comments on commit 24ee696

Please sign in to comment.