예제 #1
0
        static void Main(string[] args)
        {
            var calc  = string.Join(" ", args);
            var parse = ArithmeticParser.Parse(calc);

            if (!parse.IsValid)
            {
                Console.Error.WriteLine("Invalid input:");
                Console.Error.WriteLine(parse.ErrorMessage);
                Environment.ExitCode = -1000;
                return;
            }

            var answer = Computer.Compute(parse);

            Console.WriteLine($"{calc} = {answer}");
        }
예제 #2
0
        public void ResultIsComputed()
        {
            //Arrange
            var testStrings = new [] {
                "5 + 3 * 6",
                "5 * 3 + 6",
                "5 * 2 + 3 / 4",
                "5 * (2 + 3) / 4",
            };

            //Act
            var results = testStrings
                          .Select(s => new { String = s, Result = ArithmeticParser.Parse(s) })
                          .ToList();

            //Assert
            var output = new Output();

            output.FormatTable(results.Select(r => new { r.String, Description = r.Result.Describe(), Result = Computer.Compute(r.Result) }));
            output.Report.Verify();
        }
예제 #3
0
        public void InputIsConverted()
        {
            //Arrange
            var testStrings = new [] {
                "1",
                "32-5",
                "(100 * 3)",
                "100 + (3 * 5)",
                "(3 * 5) + 100",
                "1 + 2 + 3",
                "1 + (3 * (5 + 6))",
                "1 +",
                "(1 + 3",
                "(",
                ")",
                "((1 + 2) * 3",
                "(1 + 2) * 3)",
                "5 + (10)"
            };

            //Act
            var results = testStrings
                          .Select(s => new { String = s, Result = ArithmeticParser.Parse(s) })
                          .ToList();

            //Assert
            var output = new Output();
            var report = results.AsReport(rep => rep
                                          .AddColumn(r => r.String, cc => cc.Heading("Input"))
                                          .AddColumn(r => r.Result.IsValid, cc => { })
                                          .AddColumn(r => r.Result.Describe(), cc => cc.Heading("Result"))
                                          .AddColumn(r => r.Result.ErrorMessage, cc => {})
                                          );

            output.FormatTable(report);
            output.Report.Verify();
        }