diff options
author | Mikhail Filippov <Mikhail.Filippov@jetbrains.com> | 2017-08-08 16:58:56 +0300 |
---|---|---|
committer | Marek Safar <marek.safar@gmail.com> | 2017-08-08 22:55:36 +0300 |
commit | fd708a1ab67080b80ff1abca95e328177e535ced (patch) | |
tree | 54ce182f6ce3573172f8f178183ce299c224988d | |
parent | ad06981f78f63c5bce01310f3177a5b128041dab (diff) |
Add implementation and tests for SizeValueSerializer
5 files changed, 152 insertions, 15 deletions
diff --git a/mcs/class/WindowsBase/System.Windows.Converters/SizeValueSerializer.cs b/mcs/class/WindowsBase/System.Windows.Converters/SizeValueSerializer.cs index 280c6628b79..0a46f1d1e97 100644 --- a/mcs/class/WindowsBase/System.Windows.Converters/SizeValueSerializer.cs +++ b/mcs/class/WindowsBase/System.Windows.Converters/SizeValueSerializer.cs @@ -24,6 +24,8 @@ // using System; +using System.Diagnostics; +using System.Globalization; using System.Windows.Markup; namespace System.Windows.Converters { @@ -32,22 +34,26 @@ namespace System.Windows.Converters { { public override bool CanConvertFromString (string value, IValueSerializerContext context) { - throw new NotImplementedException (); + return true; } public override bool CanConvertToString (object value, IValueSerializerContext context) { - throw new NotImplementedException (); + return value is Size; } public override object ConvertFromString (string value, IValueSerializerContext context) { - throw new NotImplementedException (); + if (value == null) + throw new ArgumentNullException ("value"); + return Size.Parse (value); } public override string ConvertToString (object value, IValueSerializerContext context) { - throw new NotImplementedException (); + if (value is Size size) + return size.ToString (CultureInfo.InvariantCulture); + return base.ConvertToString (value, context); } } diff --git a/mcs/class/WindowsBase/System.Windows/Size.cs b/mcs/class/WindowsBase/System.Windows/Size.cs index 729bfd07882..d42beb95b73 100644 --- a/mcs/class/WindowsBase/System.Windows/Size.cs +++ b/mcs/class/WindowsBase/System.Windows/Size.cs @@ -25,6 +25,7 @@ using System; using System.ComponentModel; +using System.Globalization; using System.Windows.Converters; using System.Windows.Markup; @@ -64,29 +65,59 @@ namespace System.Windows { public override int GetHashCode () { - throw new NotImplementedException (); + unchecked + { + return (_width.GetHashCode () * 397) ^ _height.GetHashCode (); + } } public static Size Parse (string source) { - throw new NotImplementedException (); + if (source == null) + throw new ArgumentNullException ("source"); + Size value; + if (source.Trim () == "Empty") + { + value = Empty; + } + else + { + var parts = source.Split (','); + if (parts.Length != 2) + throw new FormatException (string.Format ("Invalid Size format: {0}", source)); + double width; + double height; + if (double.TryParse (parts[0], NumberStyles.Float, CultureInfo.InvariantCulture, out width) + && double.TryParse (parts[1], NumberStyles.Float, CultureInfo.InvariantCulture, out height)) + { + value = new Size (width, height); + } + else + { + throw new FormatException (string.Format ("Invalid Size format: {0}", source)); + } + } + return value; } public override string ToString () { - if (IsEmpty) - return "Empty"; - return String.Format ("{0},{1}", _width, _height); + return ConvertToString (null); } public string ToString (IFormatProvider provider) { - throw new NotImplementedException (); + return ConvertToString (provider); } string IFormattable.ToString (string format, IFormatProvider provider) { - throw new NotImplementedException (); + return ConvertToString (provider); + } + + private string ConvertToString (IFormatProvider provider) + { + return IsEmpty ? "Empty" : string.Concat (_width, ",", _height); } public bool IsEmpty { diff --git a/mcs/class/WindowsBase/Test/System.Windows/SizeTest.cs b/mcs/class/WindowsBase/Test/System.Windows/SizeTest.cs index 93150c6a5ce..6c04fd244d4 100644 --- a/mcs/class/WindowsBase/Test/System.Windows/SizeTest.cs +++ b/mcs/class/WindowsBase/Test/System.Windows/SizeTest.cs @@ -120,10 +120,32 @@ namespace MonoTests.System.Windows { } [Test] - [Category ("NotWorking")] - public void Parse () + public void ParseWithoutWhiteSpaces () { - Assert.AreEqual (new Size (1, 2), Size.Parse ("1, 2")); + Assert.AreEqual (new Size (1, 2), Size.Parse ("1,2")); + } + + [Test] + public void ParseWithWhiteSpaces () + { + Assert.AreEqual (new Size (1, 2), Size.Parse (" 1, 2 ")); + } + + [Test] + public void ParseValueWithFloatingPoint () + { + Assert.AreEqual (new Size (1.234, 5.678), Size.Parse ("1.234,5.678")); + } + [Test] + public void ParseEmpty () + { + Assert.AreEqual (Size.Empty, Size.Parse ("Empty")); + } + + [Test] + public void ParseEmptyWithWhiteSpaces () + { + Assert.AreEqual (Size.Empty, Size.Parse (" Empty ")); } [Test] @@ -136,7 +158,7 @@ namespace MonoTests.System.Windows { [Test] public void ToStringTest () { - Assert.AreEqual ("1,2", (new Size (1, 2)).ToString()); + Assert.AreEqual ("1,2", (new Size (1, 2)).ToString ()); } [Test] diff --git a/mcs/class/WindowsBase/Test/System.Windows/SizeValueSerializerTest.cs b/mcs/class/WindowsBase/Test/System.Windows/SizeValueSerializerTest.cs new file mode 100644 index 00000000000..003e9fc0505 --- /dev/null +++ b/mcs/class/WindowsBase/Test/System.Windows/SizeValueSerializerTest.cs @@ -0,0 +1,77 @@ +using System; +using System.Windows; +using System.Windows.Converters; +using NUnit.Framework; + +namespace MonoTests.System.Windows { + + [TestFixture] + public class SizeValueSerializerTest + { + [Test] + public void CanConvertFromString () + { + var serializer = new SizeValueSerializer (); + Assert.IsTrue (serializer.CanConvertFromString ("", null)); + } + + [Test] + public void CanConvertToString () + { + var serializer = new SizeValueSerializer (); + Assert.IsTrue (serializer.CanConvertToString (new Size (0, 0), null)); + Assert.IsFalse (serializer.CanConvertToString ("", null)); + } + + [Test] + public void ConvertFromString () + { + var serializer = new SizeValueSerializer (); + object obj = serializer.ConvertFromString ("3,4", null); + Assert.AreEqual (typeof (Size), obj.GetType ()); + Assert.AreEqual (new Size (3, 4), obj); + } + + [Test] + public void RoundTripConvert() + { + var serializer = new SizeValueSerializer (); + var size = new Size (1.234, 5.678); + var obj = serializer.ConvertFromString (serializer.ConvertToString (size, null), null); + Assert.AreEqual (size, obj); + } + + [Test] + [ExpectedException (typeof (FormatException))] + public void ConvertFromStringShouldThrowExceptionWhenStringHasInvalidFormat () + { + var serializer = new SizeValueSerializer (); + serializer.ConvertFromString ("a,b", null); + } + + [Test] + [ExpectedException (typeof (ArgumentNullException))] + public void ConvertFromStringShouldThrowExceptionWhenStringIsNull () + { + var serializer = new SizeValueSerializer (); + serializer.ConvertFromString (null, null); + } + + [Test] + [ExpectedException (typeof (NotSupportedException))] + public void ConvertToStringShouldThrowExceptionWhenInvalidType () + { + var serializer = new SizeValueSerializer (); + serializer.ConvertToString (10, null); + } + + [Test] + [ExpectedException (typeof (ArgumentException))] + public void ConvertToStringShouldThrowExceptionWhenHeightOrWidthIsNegative () + { + var serializer = new SizeValueSerializer (); + var result = serializer.ConvertFromString ("-1,-4", null); + } + } + +} diff --git a/mcs/class/WindowsBase/WindowsBase_test.dll.sources b/mcs/class/WindowsBase/WindowsBase_test.dll.sources index 620b1628eb8..51cb1c62d04 100644 --- a/mcs/class/WindowsBase/WindowsBase_test.dll.sources +++ b/mcs/class/WindowsBase/WindowsBase_test.dll.sources @@ -32,6 +32,7 @@ System.Windows/RectTest.cs System.Windows/RectConverterTest.cs System.Windows/SizeTest.cs System.Windows/SizeConverterTest.cs +System.Windows/SizeValueSerializerTest.cs System.Windows/VectorTest.cs System.Windows/VectorConverterTest.cs System.Windows.Markup/ConstructorArgumentAttributeTest.cs |