public async Task Calc(CalculationContext context)
        {
            var input = await _inputService.GetInput();

            while (input != null)
            {
                try
                {
                    context.Execute(input);
                }
                catch (ParameterCountNotMatchExceptions e)
                {
                    await _outputService.Output(e.Message);
                }
                catch (InvalidOperationException invalidOperation)
                {
                    await _outputService.Output(invalidOperation.Message);
                }
                catch (Exception unExpectedException)
                {
                    if (IsDebugEnable)
                    {
                        await _outputService.Output(unExpectedException.ToString());
                    }
                    else
                    {
                        await _outputService.Output("system err");
                    }
                }

                await _outputService.Output(ViewModelBuilder.PresentStack(context.CalculationData));

                input = await _inputService.GetInput();
            }
        }
示例#2
0
        public void Test_Invalid_Input()
        {
            var context = new CalculationContext();
            var list    = new List <InputDto>();

            list.Add(new InputDto("5", 0));
            list.Add(new InputDto("2", 3));
            list.Add(new InputDto("-", 5));
            list.Add(new InputDto("invalid", 7));

            Assert.Throws <InvalidOperationException>(() => context.Execute(list));
        }
示例#3
0
        public void Test_ApplicationInput()
        {
            var context = new CalculationContext();
            var list    = new List <InputDto>();

            list.Add(new InputDto("5", 0));
            list.Add(new InputDto("2", 3));

            context.Execute(list);
            context.CalculationData.Count.Should().Be(2);
            context.CalculationData.Pop().Should().Be(2);
            context.CalculationData.Pop().Should().Be(5);

            context.HistoricalData.Count.Should().Be(2);
            context.HistoricalData.Pop().CalculationOperation.Should().Equals(ApplicationOperationType.Input.Name);
            context.HistoricalData.Pop().CalculationOperation.Should().Equals(ApplicationOperationType.Input.Name);
        }
示例#4
0
        public void Test_Application_Operation()
        {
            var context = new CalculationContext();
            var list    = new List <InputDto>();

            list.Add(new InputDto("5", 0));
            list.Add(new InputDto("2", 3));
            list.Add(new InputDto("-", 5));
            list.Add(new InputDto("clear", 7));

            context.Execute(list);
            context.CalculationData.Count.Should().Be(0);

            context.HistoricalData.Count.Should().Be(4);
            context.HistoricalData.Pop().CalculationOperation.Should().Equals(ApplicationOperationType.Clear.Name);
            context.HistoricalData.Pop().CalculationOperation.Should().Equals(CalculationOperationType.Sub.Name);
            context.HistoricalData.Pop().CalculationOperation.Should().Equals(ApplicationOperationType.Input.Name);
            context.HistoricalData.Pop().CalculationOperation.Should().Equals(ApplicationOperationType.Input.Name);
        }