Exemplo n.º 1
0
        public void GivenBracketsInputErrorIsThrownAndNoResultGiven()
        {
            var viewModel = new CalculationViewModel()
            {
                Expression = "2*(2+8)"
            };

            var errors = viewModel.Validate();

            Assert.IsNotEmpty(errors);
        }
Exemplo n.º 2
0
        public void GivenInputBeginsWithOperatorErrorIsThrownAndNoResultGiven()
        {
            var viewModel = new CalculationViewModel()
            {
                Expression = "+2*2+8"
            };

            var errors = viewModel.Validate();

            Assert.IsNotEmpty(errors);
        }
Exemplo n.º 3
0
        public void GivenDecimalInputErrorIsThrownAndNoResultGiven()
        {
            var viewModel = new CalculationViewModel()
            {
                Expression = "1.3+4"
            };

            var errors = viewModel.Validate();

            Assert.IsNotEmpty(errors);
        }
Exemplo n.º 4
0
        public void GivenAlphabeticInputErrorIsThrownAndNoResultGiven()
        {
            var viewModel = new CalculationViewModel()
            {
                Expression = "House"
            };

            var errors = viewModel.Validate();

            Assert.IsNotEmpty(errors);
        }
Exemplo n.º 5
0
        public void GivenValidInputErrorIsNotThrownAndResultIsGiven(string expressionInput)
        {
            var viewModel = new CalculationViewModel()
            {
                Expression = expressionInput
            };

            var errors = viewModel.Validate();

            Assert.IsEmpty(errors);
        }
Exemplo n.º 6
0
        public void GivenInputHasConsecutiveOperatorsErrorIsThrownAndNoResultGiven()
        {
            var viewModel = new CalculationViewModel()
            {
                Expression = "12+-67"
            };

            var errors = viewModel.Validate();

            Assert.IsNotEmpty(errors);
        }
        public IActionResult PerformCalculation(CalculationViewModel model)
        {
            var errors = model.Validate();

            if (errors.Count > 0)
            {
                foreach (var error in errors)
                {
                    ModelState.AddModelError(error.Key, error.Value);
                }

                return(View("Index", model));
            }

            model.Result = CalculationHelper.GetExpressionResult(model.Expression);

            return(View("Index", model));
        }