Beispiel #1
0
        public void Demo_AddNewBracket()
        {
            var mockBraketOpen = new Mock <IExpressionBracket>();

            mockBraketOpen.Setup(el => el.Type).Returns(Calculator.Const.ExpressionElementTypes.Bracket);
            mockBraketOpen.Setup(el => el.Priority).Returns(Calculator.Const.OperationPriority.Zero);
            mockBraketOpen.Setup(el => el.BracketSign).Returns(Calculator.Const.BracketSign.Open);
            mockBraketOpen.Setup(el => el.GetHashCode()).Returns(1);
            mockBraketOpen.Setup(el => el.GetHashCode()).Returns(1);
            mockBraketOpen.Setup(el => el.Equals(It.IsAny <IExpressionBracket>())).Returns(true);

            var mockBraketClose = new Mock <IExpressionBracket>();

            mockBraketClose.Setup(el => el.Type).Returns(Calculator.Const.ExpressionElementTypes.Bracket);
            mockBraketClose.Setup(el => el.Priority).Returns(Calculator.Const.OperationPriority.Infinity);
            mockBraketClose.Setup(el => el.GetHashCode()).Returns(1);
            mockBraketClose.Setup(el => el.BracketSign).Returns(Calculator.Const.BracketSign.Close);
            mockBraketClose.Setup(el => el.Equals(It.IsAny <IExpressionBracket>())).Returns(true);


            var mockDetector = new Mock <IElementDetector>();

            mockDetector.Setup(detector => detector.GetElement("[")).Returns(mockBraketOpen.Object);
            mockDetector.Setup(detector => detector.GetElement("]")).Returns(mockBraketClose.Object);

            var calc = new NumericCalculator();

            calc.Resolver.AddDetector(mockDetector.Object);

            Assert.AreEqual(calc.Calculate("[1 + 1] * 2"), 4);
        }
        protected override void Update(object parameter)
        {
            var button = parameter as CalculatorKeyboardButton;

            if (button == null)
            {
                return;
            }

            try
            {
                switch (button.Key)
                {
                case KeyboardKeys.SWITCH:
                    KeyboardType = KeyboardType == KeyboardTypes.Number ? KeyboardTypes.Calculator : KeyboardTypes.Number;
                    break;

                case KeyboardKeys.Inv:
                    this.IsInverse = !IsInverse;
                    break;

                case KeyboardKeys.None:
                case KeyboardKeys.Close:
                case KeyboardKeys.Pin:
                case KeyboardKeys.CLR:
                case KeyboardKeys.Enter:
                    break;

                default:
                    try
                    {
                        String preValue;
                        if (CheckRules(button.Key, out preValue))
                        {
                            button.Handle(_inputList, preValue);
                        }
                    }
                    finally
                    {
                        this.InputValue = ConvertInputListToString();
                        if (button.Key == KeyboardKeys.Equal && !NeedReturn(KeyboardKeys.Equal))
                        {
                            var convertedInputValue = GetConvertedInputValue();
                            var expressionUnit      = NumericCalculator.Parse(convertedInputValue);
                            var computeResult       = expressionUnit != null?expressionUnit.Compute(AngleUnits.Degree) : Double.NaN;

                            this.InputValue = Double.IsNaN(computeResult) ? "Error" : computeResult.ToString();
                            this._inputList.Clear();
                            if (!this.IsResetAfterCalculation)
                            {
                                this._inputList.Add(new InputInfo(null, KeyboardKeys.D_Flag, this.InputValue));
                            }
                        }
                    }
                    break;
                }
            }
            catch (Exception) { }
        }
Beispiel #3
0
        public void Demo_AddNewOperandType()
        {
            var mockOperand = new Mock <IExpressionOperand <double> >();

            mockOperand.Setup(el => el.Type).Returns(Calculator.Const.ExpressionElementTypes.Operand);
            mockOperand.Setup(el => el.Value).Returns(3.1415);

            var mockDetector = new Mock <IElementDetector>();

            mockDetector.Setup(detector => detector.GetElement("Pi")).Returns(mockOperand.Object);

            var calc = new NumericCalculator();

            calc.Resolver.AddDetector(mockDetector.Object);

            Assert.AreEqual(calc.Calculate("2 * Pi"), 6.283);
        }
Beispiel #4
0
        public void Demo_AddNewFunction()
        {
            var mockOperator = new Mock <IExpressionOperator <double> >();

            mockOperator.Setup(el => el.Type).Returns(Calculator.Const.ExpressionElementTypes.Operator);
            mockOperator.Setup(el => el.Priority).Returns(Calculator.Const.OperationPriority.Highest);
            mockOperator.Setup(el => el.OperationType).Returns(Calculator.Const.OperationType.Unary);
            mockOperator.Setup(el => el.Calculate(0, 8)).Returns(1);

            var mockDetector = new Mock <IElementDetector>();

            mockDetector.Setup(detector => detector.GetElement("test")).Returns(mockOperator.Object);

            var calc = new NumericCalculator();

            calc.Resolver.AddDetector(mockDetector.Object);

            Assert.AreEqual(calc.Calculate("1 + test(8)"), 2);
        }
        public void ExpressionCalculator_OnePlusOne_WithRoundAndCurlyBracket_Good()
        {
            var result = new NumericCalculator().Calculate("{(1+1)}");

            Assert.AreEqual(result, 2);
        }
        public void ExpressionCalculator_OnePlusOneWithSpaces_Good()
        {
            var result = new NumericCalculator().Calculate("   1    + 1             ");

            Assert.AreEqual(result, 2);
        }