|
1 |
| -using System.Linq.Dynamic.Core.Parser; |
2 |
| -using System.Linq.Expressions; |
| 1 | +using Moq; |
3 | 2 | using NFluent;
|
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq.Dynamic.Core.CustomTypeProviders; |
| 5 | +using System.Linq.Dynamic.Core.Exceptions; |
| 6 | +using System.Linq.Dynamic.Core.Parser; |
| 7 | +using System.Linq.Dynamic.Core.Tests.Entities; |
| 8 | +using System.Linq.Expressions; |
4 | 9 | using Xunit;
|
5 | 10 |
|
6 | 11 | namespace System.Linq.Dynamic.Core.Tests.Parser
|
@@ -110,5 +115,50 @@ public void Parse_NullableShouldReturnNullable(string expression, object resultT
|
110 | 115 | Check.That(unaryExpression.Type).Equals(resultType);
|
111 | 116 | Check.That(unaryExpression.ToString()).Equals(result);
|
112 | 117 | }
|
| 118 | + |
| 119 | + private readonly ParsingConfig _parsingConfig; |
| 120 | + private readonly Mock<IDynamicLinkCustomTypeProvider> _dynamicTypeProviderMock; |
| 121 | + |
| 122 | + public ExpressionParserTests() |
| 123 | + { |
| 124 | + _dynamicTypeProviderMock = new Mock<IDynamicLinkCustomTypeProvider>(); |
| 125 | + _dynamicTypeProviderMock.Setup(dt => dt.GetCustomTypes()).Returns(new HashSet<Type>() { typeof(Company), typeof(MainCompany) }); |
| 126 | + _dynamicTypeProviderMock.Setup(dt => dt.ResolveType(typeof(Company).FullName)).Returns(typeof(Company)); |
| 127 | + _dynamicTypeProviderMock.Setup(dt => dt.ResolveType(typeof(MainCompany).FullName)).Returns(typeof(MainCompany)); |
| 128 | + _dynamicTypeProviderMock.Setup(dt => dt.ResolveTypeBySimpleName("Company")).Returns(typeof(Company)); |
| 129 | + _dynamicTypeProviderMock.Setup(dt => dt.ResolveTypeBySimpleName("MainCompany")).Returns(typeof(MainCompany)); |
| 130 | + |
| 131 | + _parsingConfig = new ParsingConfig |
| 132 | + { |
| 133 | + CustomTypeProvider = _dynamicTypeProviderMock.Object |
| 134 | + }; |
| 135 | + } |
| 136 | + |
| 137 | + [Theory] |
| 138 | + [InlineData("it.MainCompany.Name != null", "(company.MainCompany.Name != null)")] |
| 139 | + [InlineData("@MainCompany.Companies.Count() > 0", "(company.MainCompany.Companies.Count() > 0)")] |
| 140 | + [InlineData("Company.Equals(null, null)", "Equals(null, null)")] |
| 141 | + [InlineData("MainCompany.Name", "company.MainCompany.Name")] |
| 142 | + [InlineData("Company.Name", "No property or field 'Name' exists in type 'Company'")] |
| 143 | + public void Parse_PrioritizePropertyOrFieldOverTheType(string expression, string result) |
| 144 | + { |
| 145 | + // Arrange |
| 146 | + ParameterExpression[] parameters = { ParameterExpressionHelper.CreateParameterExpression(typeof(Company), "company") }; |
| 147 | + var sut = new ExpressionParser(parameters, expression, null, _parsingConfig); |
| 148 | + |
| 149 | + // Act |
| 150 | + string parsedExpression = null; |
| 151 | + try |
| 152 | + { |
| 153 | + parsedExpression = sut.Parse(null).ToString(); |
| 154 | + } |
| 155 | + catch (ParseException e) |
| 156 | + { |
| 157 | + parsedExpression = e.Message; |
| 158 | + } |
| 159 | + |
| 160 | + // Assert |
| 161 | + Check.That(parsedExpression).Equals(result); |
| 162 | + } |
113 | 163 | }
|
114 | 164 | }
|
0 commit comments