Test parsing and serialization of real numbers with > 5 decimals

This commit is contained in:
Frederik Carlier
2018-05-22 15:23:54 +02:00
parent 67877145b8
commit 15d53adf8e
5 changed files with 29 additions and 2 deletions

View File

@@ -147,6 +147,16 @@ namespace plistcil.test
Assert.Equal(expected, actual, false, true);
}
[Fact]
public static void RoundtripRealTest()
{
var expected = File.ReadAllText(@"test-files\RoundtripReal.plist");
var value = XmlPropertyListParser.Parse(new FileInfo(@"test-files\RoundtripReal.plist"));
var actual = value.ToXmlPropertyList();
Assert.Equal(expected, actual, false, true);
}
}
}

View File

@@ -20,6 +20,13 @@ namespace plistcil.test
Assert.Equal(10032936613, number.ToObject());
}
[Fact]
public static void NSNumberWithDecimalTest()
{
var number = new NSNumber("1360155352.748765", NSNumber.REAL);
Assert.Equal("1360155352.748765", number.ToString());
}
// The tests below make sure the numbers are being parsed correctly, and do not depend on the culture info
// being set. Especially, decimal point may vary between cultures and we don't want to take a dependency on that
// The value being used comes seen in a real property list:

View File

@@ -100,6 +100,9 @@
<None Include="test-files\Roundtrip.plist">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="test-files\RoundtripReal.plist">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<real>1360155352.748765</real>
</plist>

View File

@@ -331,7 +331,7 @@ namespace Claunia.PropertyList
}
case REAL:
{
return ToDouble().ToString(CultureInfo.InvariantCulture);
return ToDouble().ToString("G17", CultureInfo.InvariantCulture);
}
case BOOLEAN:
{
@@ -369,7 +369,9 @@ namespace Claunia.PropertyList
}
else
{
xml.Append(ToDouble().ToString(CultureInfo.InvariantCulture));
// ToString() can truncate the decimals, so use "G17". See
// https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#the-round-trip-r-format-specifier
xml.Append(ToDouble().ToString("G17", CultureInfo.InvariantCulture));
}
xml.Append("</real>");