public void ThrowWhenUnspportedOperation()
        {
            var calculator = new ConsoleCalculator.Calculator();

            Assert.That(() => calculator.Calculate(1, 1, "+"),
                        Throws.TypeOf <CalculationOperationNotSupportedException>());

            Assert.That(() => calculator.Calculate(1, 1, "+"),
                        Throws.TypeOf <CalculationOperationNotSupportedException>()
                        .With.Property("Operation").EqualTo("+"));

            //fail
            //Assert.That(() => calculator.Calculate(1, 1, "+"),
            // Throws.TypeOf<CalculationException>());

            Assert.That(() => calculator.Calculate(1, 1, "+"),
                        Throws.InstanceOf <CalculationException>());
        }
Пример #2
0
        static void Main(string[] args)
        {
            string val = "";
            string next = "Operand 1";
            double result = 0;

            Console.WriteLine("This calculator can perform adding, subtraction, division and multiplication.");
            Console.WriteLine("You can use '+', '-', '/', '*' and '=' for calculations.");
            Console.WriteLine("Press 'x' for exit, 'c' for clear.");
            Console.WriteLine("------------------------------------------------------------------------");
            Console.WriteLine("");

            Calculator calc = new Calculator();
            do
            {

                if ((next == "Operator 1") || (next == "Operator 2"))
                    Console.Write("Enter operator: ");
                else
                    Console.Write("Enter value: ");

                val = Console.ReadLine();
                if (CheckIfExit(val))  // проверяем не введен ли 'x'
                { next = "Exit"; }
                else if (CheckIfClear(val)) // проверяем не введено ли 'c'
                {
                    calc.SetInitialValues();  //обнуляем все переменные
                    Console.WriteLine("");
                    Console.WriteLine("All entered data removed.");
                    Console.WriteLine("");
                    next = "Operand 1";
                }
                else
                {
                    calc.ToCalculate = string.Format("{0} {1}", calc.ToCalculate, val);
                    Console.WriteLine("");
                    switch (next)
                    {
                        case "Operand 1":
                            {
                                calc.Val1 = double.Parse(val);
                                next = "Operator 1";
                                continue;
                            }
                        case "Operator 1":
                            {
                                if (CheckIfOperator(val))
                                {
                                    calc.Operator = val;
                                    next = "Operand 2";
                                }
                                else
                                {
                                    Console.WriteLine("You entered incorrect operator: '{0}'!", val);
                                }
                                continue;
                            }
                        case "Operand 2":
                            {
                                calc.Val2 = double.Parse(val);
                                result = calc.GetResult();
                                next = "Operator 2";
                                continue;
                            }
                        case "Operator 2":
                            {
                                if (val == "=")
                                {
                                    Console.WriteLine("{0} {1}", calc.ToCalculate, result);
                                    Console.WriteLine("");
                                    calc.SetInitialValues();
                                    next = "Operand 1";

                                }
                                else if (CheckIfOperator(val))  // проверяем является ли введённое значение оператором (+, -, *, /)
                                {
                                    calc.Val1 = result;
                                    next = "Operand 2";

                                }
                                else
                                {
                                    Console.WriteLine("You entered incorrect operator: '{0}'!", val);

                                }
                                calc.Operator = val;
                                continue;
                            }
                    }

                }

            }
            while (next != "Exit");

            return;
        }