public void ValidateCalculatorInput_ValidInput_ErrorListEmpty()
        {
            var calculatorInput = "6+5";

            var errors = _validatorService.ValidateCalculatorInput(calculatorInput);

            Assert.IsEmpty(errors);
        }
Example #2
0
        private async void Result_Click(object sender, RoutedEventArgs e)
        {
            if (_isError)
            {
                return;
            }
            var str = textBlock.Text;

            if (string.IsNullOrEmpty(str))
            {
                return;
            }
            var errorList = _validatorService.ValidateCalculatorInput(str);

            if (!errorList.Any())
            {
                textBlock.Text = await _calculationService.CalculateAsync(str, CancellationToken.None);
            }
            else
            {
                _isError = true;
                var sb = new StringBuilder();
                errorList.ForEach(a => sb.Append($"{a} "));
                textBlock.Text = sb.ToString();
            }
        }
Example #3
0
        public async Task PublishCalculatedResultAsync(string fileName, CancellationToken cancellationToken)
        {
            var fileLines = await File.ReadAllLinesAsync(fileName, cancellationToken);

            var result = new List <string>();

            foreach (var line in fileLines)
            {
                var trimmedLine = line.Replace(" ", "");
                var errors      = _validatorService.ValidateCalculatorInput(trimmedLine);
                if (errors.Any())
                {
                    var stringBuilder = new StringBuilder();
                    foreach (var error in errors)
                    {
                        stringBuilder.Append(error);
                        stringBuilder.Append(" ");
                    }
                    result.Add(stringBuilder.ToString());
                    continue;
                }

                var calculatedResult = await _calculationService.CalculateAsync(trimmedLine, cancellationToken);

                result.Add(calculatedResult);
            }

            var indexOf = fileName.LastIndexOf(".", StringComparison.Ordinal);

            if (indexOf != -1)
            {
                var resultFileName =
                    $"{fileName.Substring(0, indexOf)}_output.{fileName.Substring(indexOf + 1, fileName.Length-indexOf - 1)}";
                await File.WriteAllLinesAsync(resultFileName, result);
            }
        }