Beispiel #1
0
        private static void Parse(string value, NumberStyles style, IFormatProvider provider, float expected)
        {
            bool isDefaultProvider = provider == null || provider == NumberFormatInfo.InvariantInfo;

            if (provider == null)
            {
                provider = NumberFormatInfo.InvariantInfo;
            }

            float result;

            if ((style & ~NumberStyles.Integer) == 0 && style != NumberStyles.None)
            {
                // Use Parse(string) or Parse(string, IFormatProvider)
                if (isDefaultProvider)
                {
                    Assert.IsTrue(Single.TryParse(value, out result));
                    Assert.AreEqual(expected, result);

                    Assert.AreEqual(expected, Single.Parse(value));
                }

                Assert.AreEqual(expected, Single.Parse(value, provider));
            }

            // Use Parse(string, NumberStyles, IFormatProvider)
            Assert.IsTrue(Single.TryParse(value, style, provider, out result));
            Assert.AreEqual(expected, result);

            Assert.AreEqual(expected, Single.Parse(value, style, provider));

            if (isDefaultProvider)
            {
                // Use Parse(string, NumberStyles) or Parse(string, NumberStyles, IFormatProvider)
                Assert.IsTrue(Single.TryParse(value, style, NumberFormatInfo.InvariantInfo, out result));
                Assert.AreEqual(expected, result);

                //Assert.AreEqual(expected, float.Parse(value, style));
                Assert.AreEqual(expected, Single.Parse(value, style, NumberFormatInfo.InvariantInfo));
            }
        }
Beispiel #2
0
        private static void Parse_Invalid(string value, NumberStyles style, IFormatProvider provider, Type exceptionType)
        {
            bool isDefaultProvider = provider == null || provider == NumberFormatInfo.InvariantInfo;

            if (provider == null)
            {
                provider = NumberFormatInfo.InvariantInfo;
            }

            float result;

            if ((style & ~NumberStyles.Integer) == 0 && style != NumberStyles.None && (style & NumberStyles.AllowLeadingWhite) == (style & NumberStyles.AllowTrailingWhite))
            {
                // Use Parse(string) or Parse(string, IFormatProvider)
                if (isDefaultProvider)
                {
                    //Assert.IsFalse(Single.TryParse(value, out result));
                    //Assert.AreEqual(default(float), result);

                    //AssertExtensions.Throws(exceptionType, () => float.Parse(value));
                }

                AssertExtensions.Throws(exceptionType, () => Single.Parse(value, provider));
            }

            // Use Parse(string, NumberStyles, IFormatProvider)
            Assert.IsFalse(Single.TryParse(value, style, provider, out result));
            Assert.AreEqual(default(float), result);

            AssertExtensions.Throws(exceptionType, () => Single.Parse(value, style, provider));

            if (isDefaultProvider)
            {
                // Use Parse(string, NumberStyles) or Parse(string, NumberStyles, IFormatProvider)
                Assert.IsFalse(Single.TryParse(value, style, NumberFormatInfo.InvariantInfo, out result));
                Assert.AreEqual(default(float), result);

                //AssertExtensions.Throws(exceptionType, () => float.Parse(value, style));
                AssertExtensions.Throws(exceptionType, () => Single.Parse(value, style, NumberFormatInfo.InvariantInfo));
            }
        }