Пример #1
0
        public void ExpressionControllerTest_InvalidExpressionCalculate_BadRequest()
        {
            var calculator = new Mock <IExpressionCalculator <double> >();

            calculator.Setup(c => c.Calculate(It.IsAny <string>())).Throws(new Exception("Ошибка вычисления"));

            var controller       = new ExpressionController(calculator.Object);
            var badRequestResult = controller.Calculate("1++1") as BadRequestObjectResult;

            Assert.IsNotNull(badRequestResult);
            Assert.IsInstanceOfType(badRequestResult.Value, typeof(string));
            Assert.AreEqual((string)badRequestResult.Value, "Ошибка вычисления");
        }
Пример #2
0
        public void ExpressionControllerTest_ValidExpressionCalculate_OkResult()
        {
            var calculator = new Mock <IExpressionCalculator <double> >();

            calculator.Setup(c => c.Calculate(It.IsAny <string>())).Returns(2);

            var controller     = new ExpressionController(calculator.Object);
            var okActionResult = controller.Calculate("1+1") as OkObjectResult;

            Assert.IsNotNull(okActionResult);
            Assert.IsInstanceOfType(okActionResult.Value, typeof(double));
            Assert.AreEqual((double)okActionResult.Value, 2);
        }
Пример #3
0
        /// <summary>
        /// 輸入用戶id。body可能會有數字(int)、運算符(char)、左括號(bool)、右括號(bool)、單元運算列(List(char))
        /// </summary>
        /// <param name="userId">用戶id</param>
        /// <returns>Response</returns>
        public IHttpActionResult PostInputOperator(int userId)
        {
            //讀取body
            OperatorExpression   expression    = Request.Content.ReadAsAsync <OperatorExpression>().Result;
            ExpressionController expController = Users.GetExpressionController(userId);

            char BinaryName = expression.BinaryOperator.Value;

            //引入字典
            //BinaryOperator binaryOperator = Operators.BinaryDic[BinaryName];
            BinaryOperator       binaryOperator = Operators.GetBinary(BinaryName);
            List <UnaryOperator> unaryList      = (expression.UnaryList == null) ? new List <UnaryOperator>()
                                                                           : expression.UnaryList.Select(x => Operators.GetUnary(x)).ToList();

            decimal number = expression.Number ?? 0;

            //處理四種case
            switch (expression.Type())
            {
            case ExpType.OP:
                expController.Modify(binaryOperator);
                break;

            case ExpType.NUM_OP:
                expController.Add(number, unaryList);
                expController.Add(binaryOperator);
                break;

            case ExpType.LB_NUM_OP:
                expController.LeftBracket();
                expController.Add(number, unaryList);
                expController.Add(binaryOperator);
                break;

            case ExpType.NUM_RB_OP:
                expController.Add(number, unaryList);
                expController.RightBracket();
                expController.Add(binaryOperator);
                break;

            default:
                throw new Exception("運算表達格式錯誤");
            }

            return(Ok(new { msg = "success" }));
        }
Пример #4
0
        /// <summary>
        /// 取得運算結果。body可能會有數字(int)、右括號(bool)、單元運算列(List(char))
        /// </summary>
        /// <param name="userIdForAns">用戶ID</param>
        /// <returns>運算結果</returns>
        public IHttpActionResult PostAnswer(int userIdForAns)
        {
            //讀取body
            EqualExpression      equalexpression = Request.Content.ReadAsAsync <EqualExpression>().Result;
            ExpressionController expController   = Users.GetExpressionController(userIdForAns);

            List <UnaryOperator> unaryList = (equalexpression.UnaryList == null) ?
                                             new List <UnaryOperator>()
                : equalexpression.UnaryList.Select(x => Operators.GetUnary(x)).ToList();

            decimal?number = equalexpression.Number;
            decimal ans    = 0;

            try
            {
                switch (equalexpression.Type())
                {
                case EqualType.NUM_EQUAL:
                    expController.Add(number.Value, unaryList);
                    ans = expController.GetResult();
                    break;

                case EqualType.NUM_RB_EQUAL:
                    expController.Add(number.Value, unaryList);
                    expController.RightBracket();
                    ans = expController.GetResult();
                    break;

                default:
                    throw new Exception("EqualType錯誤");
                }
            }
            catch (Exception)
            {
                expController.Clear();
                return(Ok("fail"));
            }
            return(Ok(ans));
        }
Пример #5
0
 public InputMachine()
 {
     expController = new ExpressionController();
 }
Пример #6
0
 private Newcontroller()
 {
     expController = new ExpressionController();
 }
Пример #7
0
 void Awake()
 {
     instance = this;
     animator = gameObject.GetComponent <Animator>();
 }