diff --git a/VAR.ExpressionEvaluator.Tests/ParserTests.cs b/VAR.ExpressionEvaluator.Tests/ParserTests.cs index 38621de..d96100d 100644 --- a/VAR.ExpressionEvaluator.Tests/ParserTests.cs +++ b/VAR.ExpressionEvaluator.Tests/ParserTests.cs @@ -300,6 +300,22 @@ namespace VAR.ExpressionEvaluator.Tests Assert.AreEqual(false, result); } + [TestMethod()] + public void Relations_StringEmptyGreatherThan1_EqualsFalse() + { + string expression = "\"\">1"; + object result = Parser.EvaluateString(expression); + Assert.AreEqual(false, result); + } + + [TestMethod()] + public void Relations_1GreatherThanStringEmpty_EqualsFalse() + { + string expression = "1>\"\""; + object result = Parser.EvaluateString(expression); + Assert.AreEqual(false, result); + } + [TestMethod()] public void Relations_1Equals1_EqualsTrue() { @@ -308,6 +324,22 @@ namespace VAR.ExpressionEvaluator.Tests Assert.AreEqual(true, result); } + [TestMethod()] + public void Relations_StringEmptyEquals1_EqualsFalse() + { + string expression = "\"\"=1"; + object result = Parser.EvaluateString(expression); + Assert.AreEqual(false, result); + } + + [TestMethod()] + public void Relations_1EqualsStringEmpty_EqualsFalse() + { + string expression = "1=\"\""; + object result = Parser.EvaluateString(expression); + Assert.AreEqual(false, result); + } + [TestMethod()] public void Relations_10NotEquals1_EqualsTrue() { @@ -316,6 +348,22 @@ namespace VAR.ExpressionEvaluator.Tests Assert.AreEqual(true, result); } + [TestMethod()] + public void Relations_StringEmptyNotEquals1_EqualsTrue() + { + string expression = "\"\"!=1"; + object result = Parser.EvaluateString(expression); + Assert.AreEqual(true, result); + } + + [TestMethod()] + public void Relations_1NotEqualsStringEmpty_EqualsTrue() + { + string expression = "1!=\"\""; + object result = Parser.EvaluateString(expression); + Assert.AreEqual(true, result); + } + [TestMethod()] public void Relations_10Different1_EqualsTrue() { @@ -324,6 +372,22 @@ namespace VAR.ExpressionEvaluator.Tests Assert.AreEqual(true, result); } + [TestMethod()] + public void Relations_StringEmptyDifferent1_EqualsTrue() + { + string expression = "\"\"<>1"; + object result = Parser.EvaluateString(expression); + Assert.AreEqual(true, result); + } + + [TestMethod()] + public void Relations_1DifferentStringEmpty_EqualsTrue() + { + string expression = "1<>\"\""; + object result = Parser.EvaluateString(expression); + Assert.AreEqual(true, result); + } + [TestMethod()] public void Relations_10LessThan1_EqualsFalse() { @@ -332,6 +396,22 @@ namespace VAR.ExpressionEvaluator.Tests Assert.AreEqual(false, result); } + [TestMethod()] + public void Relations_StringEmptyLessThan1_EqualsFalse() + { + string expression = "\"\"<1"; + object result = Parser.EvaluateString(expression); + Assert.AreEqual(false, result); + } + + [TestMethod()] + public void Relations_1LessThanStringEmpty_EqualsFalse() + { + string expression = "1<\"\""; + object result = Parser.EvaluateString(expression); + Assert.AreEqual(false, result); + } + [TestMethod()] public void Relations_1GreaterOrEqualThan1_EqualsTrue() { @@ -340,6 +420,22 @@ namespace VAR.ExpressionEvaluator.Tests Assert.AreEqual(true, result); } + [TestMethod()] + public void Relations_StringEmptyGreaterOrEqualThan1_EqualsFalse() + { + string expression = "\"\">=1"; + object result = Parser.EvaluateString(expression); + Assert.AreEqual(false, result); + } + + [TestMethod()] + public void Relations_1GreaterOrEqualThanStringEmpty_EqualsFalse() + { + string expression = "1>=\"\""; + object result = Parser.EvaluateString(expression); + Assert.AreEqual(false, result); + } + [TestMethod()] public void Relations_1LessOrEqualThan1_EqualsTrue() { @@ -364,6 +460,22 @@ namespace VAR.ExpressionEvaluator.Tests Assert.AreEqual(false, result); } + [TestMethod()] + public void Relations_StringEmptyLessOrEqualThan1_EqualsFalse() + { + string expression = "\"\"<=1"; + object result = Parser.EvaluateString(expression); + Assert.AreEqual(false, result); + } + + [TestMethod()] + public void Relations_1LessOrEqualThanStringEmpty_EqualsFalse() + { + string expression = "1<=\"\""; + object result = Parser.EvaluateString(expression); + Assert.AreEqual(false, result); + } + #endregion Relations #region BooleanOps diff --git a/VAR.ExpressionEvaluator/ExpressionNodes/ExpressionEqualsNode.cs b/VAR.ExpressionEvaluator/ExpressionNodes/ExpressionEqualsNode.cs index 20d0c99..85f0837 100644 --- a/VAR.ExpressionEvaluator/ExpressionNodes/ExpressionEqualsNode.cs +++ b/VAR.ExpressionEvaluator/ExpressionNodes/ExpressionEqualsNode.cs @@ -11,15 +11,6 @@ namespace VAR.ExpressionEvaluator private static object EqualsOp(object leftValue, object rightValue) { - if (leftValue == null && rightValue == null) - { - return true; - } - if (leftValue == null || rightValue == null) - { - return false; - } - if (leftValue is string && rightValue is string) { return string.Compare((string)leftValue, (string)rightValue) == 0; @@ -27,19 +18,42 @@ namespace VAR.ExpressionEvaluator if (leftValue is string) { - if (decimal.TryParse((string)leftValue, out decimal dec) == false) + if (string.IsNullOrEmpty((string)leftValue)) { - throw new Exception(string.Format("Can't convert to decimal string value \"{0}\"", (string)leftValue)); + leftValue = null; + } + else + { + if (decimal.TryParse((string)leftValue, out decimal dec) == false) + { + throw new Exception(string.Format("Can't convert to decimal string value \"{0}\"", (string)leftValue)); + } + leftValue = dec; } - leftValue = dec; } if (rightValue is string) { - if (decimal.TryParse((string)rightValue, out decimal dec) == false) + if (string.IsNullOrEmpty((string)rightValue)) { - throw new Exception(string.Format("Can't convert to decimal string value \"{0}\"", (string)rightValue)); + leftValue = null; } - rightValue = dec; + else + { + if (decimal.TryParse((string)rightValue, out decimal dec) == false) + { + throw new Exception(string.Format("Can't convert to decimal string value \"{0}\"", (string)rightValue)); + } + rightValue = dec; + } + } + + if (leftValue == null && rightValue == null) + { + return true; + } + if (leftValue == null || rightValue == null) + { + return false; } if ((leftValue is decimal) == false || (rightValue is decimal) == false) diff --git a/VAR.ExpressionEvaluator/ExpressionNodes/ExpressionGreaterOrEqualThanNode.cs b/VAR.ExpressionEvaluator/ExpressionNodes/ExpressionGreaterOrEqualThanNode.cs index c6f88d2..d538486 100644 --- a/VAR.ExpressionEvaluator/ExpressionNodes/ExpressionGreaterOrEqualThanNode.cs +++ b/VAR.ExpressionEvaluator/ExpressionNodes/ExpressionGreaterOrEqualThanNode.cs @@ -11,11 +11,6 @@ namespace VAR.ExpressionEvaluator private static object GreaterOrEqualThanOp(object leftValue, object rightValue) { - if (leftValue == null || rightValue == null) - { - return false; - } - if (leftValue is string && rightValue is string) { return string.Compare((string)leftValue, (string)rightValue) >= 0; @@ -23,19 +18,38 @@ namespace VAR.ExpressionEvaluator if (leftValue is string) { - if (decimal.TryParse((string)leftValue, out decimal dec) == false) + if (string.IsNullOrEmpty((string)leftValue)) { - throw new Exception(string.Format("Can't convert to decimal string value \"{0}\"", (string)leftValue)); + leftValue = null; + } + else + { + if (decimal.TryParse((string)leftValue, out decimal dec) == false) + { + throw new Exception(string.Format("Can't convert to decimal string value \"{0}\"", (string)leftValue)); + } + leftValue = dec; } - leftValue = dec; } if (rightValue is string) { - if (decimal.TryParse((string)rightValue, out decimal dec) == false) + if (string.IsNullOrEmpty((string)rightValue)) { - throw new Exception(string.Format("Can't convert to decimal string value \"{0}\"", (string)rightValue)); + leftValue = null; } - rightValue = dec; + else + { + if (decimal.TryParse((string)rightValue, out decimal dec) == false) + { + throw new Exception(string.Format("Can't convert to decimal string value \"{0}\"", (string)rightValue)); + } + rightValue = dec; + } + } + + if (leftValue == null || rightValue == null) + { + return false; } if ((leftValue is decimal) == false || (rightValue is decimal) == false) diff --git a/VAR.ExpressionEvaluator/ExpressionNodes/ExpressionGreaterThanNode.cs b/VAR.ExpressionEvaluator/ExpressionNodes/ExpressionGreaterThanNode.cs index 2029c58..02d1800 100644 --- a/VAR.ExpressionEvaluator/ExpressionNodes/ExpressionGreaterThanNode.cs +++ b/VAR.ExpressionEvaluator/ExpressionNodes/ExpressionGreaterThanNode.cs @@ -11,11 +11,6 @@ namespace VAR.ExpressionEvaluator private static object GreaterThanOp(object leftValue, object rightValue) { - if (leftValue == null || rightValue == null) - { - return false; - } - if (leftValue is string && rightValue is string) { return string.Compare((string)leftValue, (string)rightValue) > 0; @@ -23,19 +18,38 @@ namespace VAR.ExpressionEvaluator if (leftValue is string) { - if (decimal.TryParse((string)leftValue, out decimal dec) == false) + if (string.IsNullOrEmpty((string)leftValue)) { - throw new Exception(string.Format("Can't convert to decimal string value \"{0}\"", (string)leftValue)); + leftValue = null; + } + else + { + if (decimal.TryParse((string)leftValue, out decimal dec) == false) + { + throw new Exception(string.Format("Can't convert to decimal string value \"{0}\"", (string)leftValue)); + } + leftValue = dec; } - leftValue = dec; } if (rightValue is string) { - if (decimal.TryParse((string)rightValue, out decimal dec) == false) + if (string.IsNullOrEmpty((string)rightValue)) { - throw new Exception(string.Format("Can't convert to decimal string value \"{0}\"", (string)rightValue)); + leftValue = null; } - rightValue = dec; + else + { + if (decimal.TryParse((string)rightValue, out decimal dec) == false) + { + throw new Exception(string.Format("Can't convert to decimal string value \"{0}\"", (string)rightValue)); + } + rightValue = dec; + } + } + + if (leftValue == null || rightValue == null) + { + return false; } if ((leftValue is decimal) == false || (rightValue is decimal) == false) diff --git a/VAR.ExpressionEvaluator/ExpressionNodes/ExpressionLessOrEqualThanNode.cs b/VAR.ExpressionEvaluator/ExpressionNodes/ExpressionLessOrEqualThanNode.cs index b644ad1..703bedd 100644 --- a/VAR.ExpressionEvaluator/ExpressionNodes/ExpressionLessOrEqualThanNode.cs +++ b/VAR.ExpressionEvaluator/ExpressionNodes/ExpressionLessOrEqualThanNode.cs @@ -11,11 +11,6 @@ namespace VAR.ExpressionEvaluator private static object LessOrEqualThanOp(object leftValue, object rightValue) { - if (leftValue == null || rightValue == null) - { - return false; - } - if (leftValue is string && rightValue is string) { return string.Compare((string)leftValue, (string)rightValue) <= 0; @@ -23,19 +18,38 @@ namespace VAR.ExpressionEvaluator if (leftValue is string) { - if (decimal.TryParse((string)leftValue, out decimal dec) == false) + if (string.IsNullOrEmpty((string)leftValue)) { - throw new Exception(string.Format("Can't convert to decimal string value \"{0}\"", (string)leftValue)); + leftValue = null; + } + else + { + if (decimal.TryParse((string)leftValue, out decimal dec) == false) + { + throw new Exception(string.Format("Can't convert to decimal string value \"{0}\"", (string)leftValue)); + } + leftValue = dec; } - leftValue = dec; } if (rightValue is string) { - if (decimal.TryParse((string)rightValue, out decimal dec) == false) + if (string.IsNullOrEmpty((string)rightValue)) { - throw new Exception(string.Format("Can't convert to decimal string value \"{0}\"", (string)rightValue)); + leftValue = null; } - rightValue = dec; + else + { + if (decimal.TryParse((string)rightValue, out decimal dec) == false) + { + throw new Exception(string.Format("Can't convert to decimal string value \"{0}\"", (string)rightValue)); + } + rightValue = dec; + } + } + + if (leftValue == null || rightValue == null) + { + return false; } if ((leftValue is decimal) == false || (rightValue is decimal) == false) diff --git a/VAR.ExpressionEvaluator/ExpressionNodes/ExpressionLessThanNode.cs b/VAR.ExpressionEvaluator/ExpressionNodes/ExpressionLessThanNode.cs index 9e42daf..37ea31b 100644 --- a/VAR.ExpressionEvaluator/ExpressionNodes/ExpressionLessThanNode.cs +++ b/VAR.ExpressionEvaluator/ExpressionNodes/ExpressionLessThanNode.cs @@ -11,11 +11,6 @@ namespace VAR.ExpressionEvaluator private static object LessThanOp(object leftValue, object rightValue) { - if (leftValue == null || rightValue == null) - { - return false; - } - if (leftValue is string && rightValue is string) { return string.Compare((string)leftValue, (string)rightValue) < 0; @@ -23,19 +18,38 @@ namespace VAR.ExpressionEvaluator if (leftValue is string) { - if (decimal.TryParse((string)leftValue, out decimal dec) == false) + if (string.IsNullOrEmpty((string)leftValue)) { - throw new Exception(string.Format("Can't convert to decimal string value \"{0}\"", (string)leftValue)); + leftValue = null; + } + else + { + if (decimal.TryParse((string)leftValue, out decimal dec) == false) + { + throw new Exception(string.Format("Can't convert to decimal string value \"{0}\"", (string)leftValue)); + } + leftValue = dec; } - leftValue = dec; } if (rightValue is string) { - if (decimal.TryParse((string)rightValue, out decimal dec) == false) + if (string.IsNullOrEmpty((string)rightValue)) { - throw new Exception(string.Format("Can't convert to decimal string value \"{0}\"", (string)rightValue)); + leftValue = null; } - rightValue = dec; + else + { + if (decimal.TryParse((string)rightValue, out decimal dec) == false) + { + throw new Exception(string.Format("Can't convert to decimal string value \"{0}\"", (string)rightValue)); + } + rightValue = dec; + } + } + + if (leftValue == null || rightValue == null) + { + return false; } if ((leftValue is decimal) == false || (rightValue is decimal) == false) diff --git a/VAR.ExpressionEvaluator/ExpressionNodes/ExpressionNotEqualsNode.cs b/VAR.ExpressionEvaluator/ExpressionNodes/ExpressionNotEqualsNode.cs index 0428e12..fb5ac48 100644 --- a/VAR.ExpressionEvaluator/ExpressionNodes/ExpressionNotEqualsNode.cs +++ b/VAR.ExpressionEvaluator/ExpressionNodes/ExpressionNotEqualsNode.cs @@ -11,15 +11,6 @@ namespace VAR.ExpressionEvaluator private static object NotEqualsOp(object leftValue, object rightValue) { - if (leftValue == null && rightValue == null) - { - return false; - } - if (leftValue == null || rightValue == null) - { - return true; - } - if (leftValue is string && rightValue is string) { return string.Compare((string)leftValue, (string)rightValue) != 0; @@ -27,19 +18,42 @@ namespace VAR.ExpressionEvaluator if (leftValue is string) { - if (decimal.TryParse((string)leftValue, out decimal dec) == false) + if (string.IsNullOrEmpty((string)leftValue)) { - throw new Exception(string.Format("Can't convert to decimal string value \"{0}\"", (string)leftValue)); + leftValue = null; + } + else + { + if (decimal.TryParse((string)leftValue, out decimal dec) == false) + { + throw new Exception(string.Format("Can't convert to decimal string value \"{0}\"", (string)leftValue)); + } + leftValue = dec; } - leftValue = dec; } if (rightValue is string) { - if (decimal.TryParse((string)rightValue, out decimal dec) == false) + if (string.IsNullOrEmpty((string)rightValue)) { - throw new Exception(string.Format("Can't convert to decimal string value \"{0}\"", (string)rightValue)); + leftValue = null; } - rightValue = dec; + else + { + if (decimal.TryParse((string)rightValue, out decimal dec) == false) + { + throw new Exception(string.Format("Can't convert to decimal string value \"{0}\"", (string)rightValue)); + } + rightValue = dec; + } + } + + if (leftValue == null && rightValue == null) + { + return false; + } + if (leftValue == null || rightValue == null) + { + return true; } if ((leftValue is decimal) == false || (rightValue is decimal) == false)