Ejemplo n.º 1
0
        public void Calculator(string hasChanged)
        {
            try
            {
                //Sees if user wants to calculate from base value or quote value.
                //Checks if the values in the textboxes are correct for calling the calculate method.
                if ("BaseValue" == hasChanged && BaseValue != "." && BaseValue != "")
                {
                    QuoteValue = CurrenciesCalculator.CalculateQuoteValue(BaseValue, Rate).
                                 ToString(CultureInfo.GetCultureInfo("en-US"));
                }

                else if ("QuoteValue" == hasChanged && QuoteValue != "." && QuoteValue != "")
                {
                    BaseValue = CurrenciesCalculator.CalculateBaseValue(QuoteValue, Rate).
                                ToString(CultureInfo.GetCultureInfo("en-US"));
                }
                //If not, it clears them.
                else
                {
                    BaseValue  = "";
                    QuoteValue = "";
                }
            }
            catch (OverflowException)
            {
                MessageBox.Show("The input value is out of the accepted range.", "Error");
                BaseValue  = "";
                QuoteValue = "";
            }
            catch (FormatException)
            {
                MessageBox.Show($"The input value is invalid. Use only digits or point  \".\" as decimal separator. "
                                + $"For example: 15.34 ", "Error");
                BaseValue  = "";
                QuoteValue = "";
            }
            catch (DivideByZeroException)
            {
                MessageBox.Show("Couldn't retrieve the requested results (rate = 0).", "Error");
            }
            catch (Exception)
            {
                MessageBox.Show("Couldn't retrieve the requested results.", "Error");
            }
        }
Ejemplo n.º 2
0
 public void CalculateBaseValue_ShouldThrowsFormatExcpetion()
 {
     Assert.Throws <FormatException>(() => CurrenciesCalculator.CalculateBaseValue("h", 0.3M));
 }
Ejemplo n.º 3
0
 public void CalculateBaseValue_ShouldThrowOverflowException()
 {
     Assert.Throws <OverflowException>(() => CurrenciesCalculator.CalculateBaseValue("999999999999999999999999999", 0.000000000000000000001M));
 }
Ejemplo n.º 4
0
 //In case there is some error in the API retrieved values and rate is equal to 0.
 public void CalculateBaseValue_ShouldThrowDivideByZeroException()
 {
     Assert.Throws <DivideByZeroException>(() => CurrenciesCalculator.CalculateBaseValue("12.6", 0));
 }
Ejemplo n.º 5
0
        public void CalculateBaseValue_ShouldCalculate(string quoteValue, decimal rate, decimal expected)
        {
            decimal actual = CurrenciesCalculator.CalculateBaseValue(quoteValue, rate);

            Assert.Equal(expected, actual);
        }
Ejemplo n.º 6
0
 public void CalculateQuoteValue_ShouldThrowFormatException()
 {
     Assert.Throws <FormatException>(() => CurrenciesCalculator.CalculateQuoteValue("a", 0.5M));
 }