public void Convert_Normal_ConvertsToDollars()
        {
            CurrencyConverter cc = new CurrencyConverter();
            object result = cc.Convert(10, typeof(string), null, CultureInfo.CurrentCulture);
            Assert.AreEqual("$10.00", (string)result, "Should convert 10 to $10.00");

            result = cc.Convert("10", typeof(string), null, CultureInfo.CurrentCulture);
            Assert.AreEqual("$10.00", (string)result, "Should convert \"10\" to $10.00");
        }
        public void Convert_Boundary_ReturnsEmptyStringForNullInputs()
        {
            CurrencyConverter cc = new CurrencyConverter();
            object expected = string.Empty;
            object result = cc.Convert(null, typeof(string), null, CultureInfo.CurrentCulture);
            Assert.AreEqual(expected, result, "Null inputs should become empty strings");

            result = cc.Convert(new int?(), typeof(string), null, CultureInfo.CurrentCulture);
            Assert.AreEqual(expected, result, "Null inputs should become empty strings");
        }
        public void Convert_Normal_RoundsCorrectly()
        {
            CurrencyConverter cc = new CurrencyConverter();

            object result = cc.Convert(9.99, typeof(string), null, CultureInfo.CurrentCulture);
            Assert.AreEqual("$9.99", (string)result, "Should convert 9.99 to $9.99");

            result = cc.Convert(9.995, typeof(string), null, CultureInfo.CurrentCulture);
            Assert.AreEqual("$10.00", (string)result, "Should convert 9.995 to $10.00");

            result = cc.Convert(9.994, typeof(string), null, CultureInfo.CurrentCulture);
            Assert.AreEqual("$9.99", (string)result, "Should convert 9.994 to $9.99");

            result = cc.Convert(.025, typeof(string), null, CultureInfo.CurrentCulture);
            Assert.AreEqual("$0.03", (string)result, "Should convert .025 to $0.03");

            result = cc.Convert(1234.567, typeof(string), null, CultureInfo.CurrentCulture);
            Assert.AreEqual("$1,234.57", (string)result, "Should convert 1234.567 to $1,234.57");
        }
        public void Convert_Normal_RoundsCorrectlyForCulture()
        {
            CurrencyConverter cc = new CurrencyConverter();

            System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
            System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");

            object result = cc.Convert(9.99, typeof(string), null, CultureInfo.CurrentCulture);
            Assert.AreEqual("£9.99", (string)result, "Should convert 9.99 to £9.99");

            result = cc.Convert(9.995, typeof(string), null, CultureInfo.CurrentCulture);
            Assert.AreEqual("£10.00", (string)result, "Should convert 9.995 to £10.00");

            result = cc.Convert(9.994, typeof(string), null, CultureInfo.CurrentCulture);
            Assert.AreEqual("£9.99", (string)result, "Should convert 9.994 to £9.99");

            result = cc.Convert(.025, typeof(string), null, CultureInfo.CurrentCulture);
            Assert.AreEqual("£0.03", (string)result, "Should convert .025 to £0.03");

            result = cc.Convert(1234.567, typeof(string), null, CultureInfo.CurrentCulture);
            Assert.AreEqual("£1,234.57", (string)result, "Should convert 1234.567 to £1,234.57");


            System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
            System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");

            result = cc.Convert(9.99, typeof(string), null, CultureInfo.CurrentCulture);
            Assert.AreEqual("9,99 €", (string)result, "Should convert 9.99 to 9,99 €");

            result = cc.Convert(9.995, typeof(string), null, CultureInfo.CurrentCulture);
            Assert.AreEqual("10,00 €", (string)result, "Should convert 9.995 to 10,00 €");

            result = cc.Convert(9.994, typeof(string), null, CultureInfo.CurrentCulture);
            Assert.AreEqual("9,99 €", (string)result, "Should convert 9.994 to 9,99 €");

            result = cc.Convert(.025, typeof(string), null, CultureInfo.CurrentCulture);
            Assert.AreEqual("0,03 €", (string)result, "Should convert .025 to 0,03 €");

            result = cc.Convert(1234.567, typeof(string), null, CultureInfo.CurrentCulture);
            Assert.AreEqual("1 234,57 €", (string)result, "Should convert 1234.567 to 1 234,57 €");
        }
 public void Convert_Boundary_ThrowsFormatExceptionForInvalidIFormattable()
 {
     CurrencyConverter cc = new CurrencyConverter();
     object result = cc.Convert(System.DateTime.Now, typeof(string), null, CultureInfo.CurrentCulture);
 }
 public void Convert_Boundary_ThrowsFormatExceptionForInvalidTypes()
 {
     CurrencyConverter cc = new CurrencyConverter();
     object result = cc.Convert(typeof(string), typeof(string), null, CultureInfo.CurrentCulture);
 }
        private void Helper(string culture)
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
            Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
            CurrencyConverter cc = new CurrencyConverter();
            double input = 9.99;
            object result = null;
            object output = null;
            if (Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalDigits > 0)
            {
                result = cc.Convert(input, typeof(string), null, CultureInfo.CurrentCulture);
                output = cc.ConvertBack(result, typeof(double), null, CultureInfo.CurrentCulture);
                Assert.AreEqual(input, output, "Input should be Converted and converted back to the same value. Culture = " + culture);

                input = 1234.57;
                result = cc.Convert(input, typeof(string), null, CultureInfo.CurrentCulture);
                output = cc.ConvertBack(result, typeof(double), null, CultureInfo.CurrentCulture);

                Assert.AreEqual(input, output, "Input should be Converted and converted back to the same value. Culture = " + culture);
            }
            else
            {
                input = 999;
                result = cc.Convert(input, typeof(string), null, CultureInfo.CurrentCulture);
                output = cc.ConvertBack(result, typeof(double), null, CultureInfo.CurrentCulture);
                Assert.AreEqual(input, output, "Input should be Converted and converted back to the same value. Culture = " + culture);

                input = 123457;
                result = cc.Convert(input, typeof(string), null, CultureInfo.CurrentCulture);
                output = cc.ConvertBack(result, typeof(double), null, CultureInfo.CurrentCulture);

                Assert.AreEqual(input, output, "Input should be Converted and converted back to the same value. Culture = " + culture);
            }
        }