From 3f78299a1222ea1e7f436c3585a1835da03511b1 Mon Sep 17 00:00:00 2001 From: Dmitriy Barbul Date: Tue, 9 Feb 2016 14:53:25 +0200 Subject: [PATCH] Add throwing exception if variable is not decimal or boolean --- Calculator.Core.Tests/CalculatorTest.cs | 40 +++++++++++++++++++ Calculator.Core/Calculator.cs | 13 ++++-- .../Enum/CalculateExceptionCode.cs | 1 + Calculator.Core/FormulaTokenizer.cs | 5 +++ 4 files changed, 55 insertions(+), 4 deletions(-) diff --git a/Calculator.Core.Tests/CalculatorTest.cs b/Calculator.Core.Tests/CalculatorTest.cs index 1db824b..1e7fc71 100644 --- a/Calculator.Core.Tests/CalculatorTest.cs +++ b/Calculator.Core.Tests/CalculatorTest.cs @@ -255,5 +255,45 @@ public void Calculate_UndefinedVariable_ThrowsException() { Calculator.Calculate("a"); } + + [TestMethod] + [ExpectedExceptionWithCode(typeof(CalculateException), (int)CalculateExceptionCode.StringVariable)] + public void Calculate_StringVariable_ThrowsException() + { + Calculator.Calculate( + "a + c", + new Dictionary() + { + { "a", 1 }, + { "c", "test" } + } + ); + } + + [TestMethod] + [ExpectedExceptionWithCode(typeof(CalculateException), (int)CalculateExceptionCode.StringVariable)] + public void Calculate_OperationVariable_ThrowsException() + { + Calculator.Calculate( + "c / 0", + new Dictionary() + { + { "c", "-" } + } + ); + } + + [TestMethod] + [ExpectedExceptionWithCode(typeof(CalculateException), (int)CalculateExceptionCode.StringVariable)] + public void Calculate_SubformulaVariable_ThrowsException() + { + Calculator.Calculate( + "v", + new Dictionary() + { + { "v", "(1+2)" } + } + ); + } } } diff --git a/Calculator.Core/Calculator.cs b/Calculator.Core/Calculator.cs index 5fb2a32..5afcf9e 100644 --- a/Calculator.Core/Calculator.cs +++ b/Calculator.Core/Calculator.cs @@ -40,10 +40,15 @@ public static TResult Calculate(string formula, Dictionary