diff --git a/Src/System.Linq.Dynamic.Test/DynamicExpressionTests.cs b/Src/System.Linq.Dynamic.Test/DynamicExpressionTests.cs index 8617299..b12e5d0 100644 --- a/Src/System.Linq.Dynamic.Test/DynamicExpressionTests.cs +++ b/Src/System.Linq.Dynamic.Test/DynamicExpressionTests.cs @@ -28,6 +28,70 @@ public void Parse_TupleToStringMethodCall_ReturnsStringLambdaExpression() Assert.AreEqual(typeof(string), expression.ReturnType); } + [TestMethod] + public void Parse_StringLiteral_ReturnsBooleanLambdaExpression() + { + var expression = DynamicExpression.Parse(new[] { Expression.Parameter(typeof(string), "Property1") }, typeof(Boolean), "Property1 == \"test\""); + Assert.AreEqual(typeof(Boolean), expression.Type); + } + + [TestMethod] + public void Parse_StringLiteralEmpty_ReturnsBooleanLambdaExpression() + { + var expression = DynamicExpression.Parse(new[] { Expression.Parameter(typeof(string), "Property1") }, typeof(Boolean), "Property1 == \"\""); + Assert.AreEqual(typeof(Boolean), expression.Type); + } + + [TestMethod] + public void Parse_StringLiteralEmbeddedQuote_ReturnsBooleanLambdaExpression() + { + var expression = DynamicExpression.Parse( + new[] { Expression.Parameter(typeof(string), "Property1") }, + typeof(Boolean), + string.Format("Property1 == {0}", "\"test \\\"string\"")); + + string rightValue = ((BinaryExpression) expression).Right.ToString(); + Assert.AreEqual(typeof(Boolean), expression.Type); + Assert.AreEqual("\"test \"string\"", rightValue); + } + + [TestMethod] + public void Parse_StringLiteralStartEmbeddedQuote_ReturnsBooleanLambdaExpression() + { + var expression = DynamicExpression.Parse( + new[] { Expression.Parameter(typeof(string), "Property1") }, + typeof(Boolean), + string.Format("Property1 == {0}", "\"\\\"test\"")); + + string rightValue = ((BinaryExpression)expression).Right.ToString(); + Assert.AreEqual(typeof(Boolean), expression.Type); + Assert.AreEqual("\"\"test\"", rightValue); + } + + [ExpectedException(typeof(ParseException))] + [TestMethod] + public void Parse_StringLiteral_MissingClosingQuote() + { + string expectedRightValue = "\"test\\\""; + var expression = DynamicExpression.Parse( + new[] { Expression.Parameter(typeof(string), "Property1") }, + typeof(Boolean), + string.Format("Property1 == {0}", expectedRightValue)); + } + + [TestMethod] + public void Parse_StringLiteralEscapedBackslash_ReturnsBooleanLambdaExpression() + { + var expression = DynamicExpression.Parse( + new[] { Expression.Parameter(typeof(string), "Property1") }, + typeof(Boolean), + string.Format("Property1 == {0}", "\"test\\\\string\"")); + + string rightValue = ((BinaryExpression)expression).Right.ToString(); + Assert.AreEqual(typeof(Boolean), expression.Type); + Assert.AreEqual("\"test\\string\"", rightValue); + } + [TestMethod] public void ParseLambda_DelegateTypeMethodCall_ReturnsEventHandlerLambdaExpression() { diff --git a/Src/System.Linq.Dynamic.Test/IntegrationTests.cs b/Src/System.Linq.Dynamic.Test/IntegrationTests.cs new file mode 100644 index 0000000..8557baf --- /dev/null +++ b/Src/System.Linq.Dynamic.Test/IntegrationTests.cs @@ -0,0 +1,121 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Linq; +using System.Linq.Dynamic; + +namespace System.Linq.Dynamic.Test +{ + [TestClass] + public class IntegrationTests + { + private class TestObject + { + public int Id { get; set; } + public string Color { get; set; } + public int Number { get; set; } + } + + private IList GetTestData() + { + return new List() + { + new TestObject() {Id = 0, Color = "Red", Number = 10 }, + new TestObject() {Id = 1, Color = "Red", Number = 20 }, + new TestObject() {Id = 2, Color = "\"Bright\" Red", Number = 30 }, + new TestObject() {Id = 3, Color = "Blue", Number = 5 }, + new TestObject() {Id = 4, Color = "Brown", Number = 15 }, + new TestObject() {Id = 5, Color = "\"Faded\" Blue", Number = 25 }, + new TestObject() {Id = 6, Color = "Blue\\Green", Number = 30 }, + new TestObject() {Id = 7, Color = "Yellow\tOrange", Number = 35 }, + new TestObject() {Id = 8, Color = "Bright Orange", Number = 30 }, + new TestObject() {Id = 9, Color = "Faded\u0009Orange", Number = 40 } + }; + } + + private bool TestItemsIncluded(IList data, IEnumerable expectedIds) + { + Assert.AreEqual(expectedIds.Count(), data.Count(), "Unexpected number of items returned."); + + foreach (int id in expectedIds) + { + var results = data.Where(x => x.Id == id); + Assert.IsTrue(results.Count() > 0, string.Format("Expected item {0} to be included.", id)); + } + + return true; + } + + [TestMethod] + public void Where_StringEquality() + { + var testData = GetTestData(); + var testResults = testData.Where("Color == \"Red\"").ToList(); + + TestItemsIncluded(testResults, new List() { 0, 1 }); + } + + [TestMethod] + public void Where_StringContains() + { + var testData = GetTestData(); + var testResults = testData.Where("Color.Contains(\"Red\")").ToList(); + + TestItemsIncluded(testResults, new List() { 0, 1, 2 }); + } + + [TestMethod] + public void Where_EscapedStringContains() + { + var testData = GetTestData(); + var testResults = testData.Where("Color.Contains(\"\\\"Bright\\\"\")").ToList(); + + TestItemsIncluded(testResults, new List() { 2 }); + } + + [TestMethod] + public void Where_EscapedStringEquality() + { + var testData = GetTestData(); + var testResults = testData.Where("Color == \"\\\"Faded\\\" Blue\"").ToList(); + + TestItemsIncluded(testResults, new List() { 5 }); + } + + [TestMethod] + [ExpectedException(typeof(System.Linq.Dynamic.ParseException))] + public void Where_InvalidClause() + { + var testData = GetTestData(); + var testResults = testData.Where("Color.Contains(\\\"Bright\\\")").ToList(); + } + + [TestMethod] + public void Where_NumberRange() + { + var testData = GetTestData(); + var testResults = testData.Where("Number <= 25 && Number > 5").ToList(); + + TestItemsIncluded(testResults, new List() { 0, 1, 4, 5 }); + } + + [TestMethod] + public void Where_EscapeChar() + { + var testData = GetTestData(); + var testResults = testData.Where("Color.Contains(\"\\\\\")").ToList(); + + TestItemsIncluded(testResults, new List() { 6 }); + } + + [TestMethod] + public void Where_EscapedTab() + { + var testData = GetTestData(); + var testResults = testData.Where("Color.Contains(\"\\t\")").ToList(); + + TestItemsIncluded(testResults, new List() { 7, 9 }); + } + } +} diff --git a/Src/System.Linq.Dynamic.Test/System.Linq.Dynamic.Test.csproj b/Src/System.Linq.Dynamic.Test/System.Linq.Dynamic.Test.csproj index ad1eb1d..9790fa3 100644 --- a/Src/System.Linq.Dynamic.Test/System.Linq.Dynamic.Test.csproj +++ b/Src/System.Linq.Dynamic.Test/System.Linq.Dynamic.Test.csproj @@ -11,9 +11,10 @@ Properties System.Linq.Dynamic.Test System.Linq.Dynamic.Test - v4.0 + v4.5 512 {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + true @@ -23,6 +24,7 @@ DEBUG;TRACE prompt 4 + false pdbonly @@ -31,9 +33,12 @@ TRACE prompt 4 + false - + + False + 3.5 @@ -48,19 +53,21 @@ + {b6edf157-6153-4684-a577-de33896dbaa8} System.Linq.Dynamic - - {b6edf157-6153-4684-a577-de33896dbaa8} System.Linq.Dynamic + + +