Skip to content

Commit bc1923e

Browse files
authored
Add unit tests for "As acting on property" (#622)
* Add unit tests for "As acting on property" * 519
1 parent 38e5a76 commit bc1923e

File tree

6 files changed

+123
-10
lines changed

6 files changed

+123
-10
lines changed

src-console/ConsoleAppEF5/ConsoleApp_net5.0_EF5.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net5.0</TargetFramework>
66
<DefineConstants>EF5</DefineConstants>
7+
<LangVersion>10</LangVersion>
78
</PropertyGroup>
89

910
<ItemGroup>

src-console/ConsoleAppEF5/Program.cs

+59-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,71 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Linq.Dynamic.Core;
4-
using System.Threading;
5+
using System.Linq.Expressions;
56
using ConsoleAppEF2.Database;
67

78
namespace ConsoleAppEF5
89
{
10+
// https://github.com/zzzprojects/System.Linq.Dynamic.Core/issues/519
11+
// Here Appilicability is a property within Settings type, and WinApplicability is a derived type of Applicability. SKUs is a property within WinApplicability.
12+
public class Settings
13+
{
14+
public Applicability Applicability { get; set; }
15+
}
16+
17+
public class Applicability
18+
{
19+
20+
}
21+
22+
public class WinApplicability : Applicability
23+
{
24+
public List<string> SKUs { get; set; }
25+
}
26+
927
class Program
1028
{
1129
static void Main(string[] args)
1230
{
31+
var settingsList = new List<Settings>
32+
{
33+
new()
34+
{
35+
Applicability = new WinApplicability
36+
{
37+
SKUs = new List<string>
38+
{
39+
"a",
40+
"b"
41+
}
42+
}
43+
},
44+
new()
45+
{
46+
Applicability = new WinApplicability
47+
{
48+
SKUs = new List<string>
49+
{
50+
"c"
51+
}
52+
}
53+
}
54+
}.AsQueryable();
55+
56+
var sku = "a";
57+
58+
var p = new ParsingConfig { ResolveTypesBySimpleName = true };
59+
60+
Expression<Func<Settings, bool>> expr = c => (c.Applicability as WinApplicability).SKUs.Contains(sku);
61+
62+
var exprDynamic = DynamicExpressionParser.ParseLambda<Settings, bool>(p, true, "As(Applicability, \"WinApplicability\").SKUs.Contains(@0)", sku);
63+
64+
var result = settingsList.Where(expr).ToList();
65+
66+
var resultDynamic = settingsList.Where(exprDynamic).ToList();
67+
68+
1369
var e = new int[0].AsQueryable();
1470
var q = new[] { 1 }.AsQueryable();
1571

@@ -100,8 +156,8 @@ static void Main(string[] args)
100156
Console.WriteLine($"{car.Key}");
101157
}
102158

103-
var resultDynamic = users.Any("c => np(c.FirstName, string.Empty).ToUpper() == \"DOE\"");
104-
Console.WriteLine(resultDynamic);
159+
var resultDynamic1 = users.Any("c => np(c.FirstName, string.Empty).ToUpper() == \"DOE\"");
160+
Console.WriteLine(resultDynamic1);
105161

106162
var users2 = users.Select<User>(config, "new User(it.FirstName as FirstName, 1 as Field)");
107163
foreach (User u in users2)

test/EntityFramework.DynamicLinq.Tests.net452/EntityFramework.DynamicLinq.Tests.net452.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@
188188
<Compile Include="..\System.Linq.Dynamic.Core.Tests\Entities\Country.cs">
189189
<Link>Entities\Country.cs</Link>
190190
</Compile>
191+
<Compile Include="..\System.Linq.Dynamic.Core.Tests\Entities\Department.cs">
192+
<Link>Entities\Department.cs</Link>
193+
</Compile>
191194
<Compile Include="..\System.Linq.Dynamic.Core.Tests\Entities\Employee.cs">
192195
<Link>Entities\Employee.cs</Link>
193196
</Compile>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace System.Linq.Dynamic.Core.Tests.Entities
2+
{
3+
public class Department
4+
{
5+
public BaseEmployee Employee { get; set; }
6+
}
7+
}

test/System.Linq.Dynamic.Core.Tests/Entities/Worker.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ public class Worker : BaseEmployee
44
{
55
public string Other { get; set; }
66
}
7-
}
7+
}

test/System.Linq.Dynamic.Core.Tests/QueryableTests.Is,OfType,As,Cast.cs

+52-6
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,52 @@ public void As_Dynamic_ActingOnIt_WithType()
177177
Check.That(countAsDynamic).Equals(1);
178178
}
179179

180+
[Fact]
181+
public void As_Dynamic_ActingOnProperty()
182+
{
183+
// Assign
184+
var qry = new[]
185+
{
186+
new Department
187+
{
188+
Employee = new Worker { Name = "1" }
189+
},
190+
new Department
191+
{
192+
Employee = new Boss { Name = "b" }
193+
}
194+
}.AsQueryable();
195+
196+
// Act
197+
int countAsDynamic = qry.Count("As(Employee, \"System.Linq.Dynamic.Core.Tests.Entities.Worker\") != null");
198+
199+
// Assert
200+
countAsDynamic.Should().Be(1);
201+
}
202+
203+
[Fact]
204+
public void As_Dynamic_ActingOnProperty_WithType()
205+
{
206+
// Assign
207+
var qry = new[]
208+
{
209+
new Department
210+
{
211+
Employee = new Worker { Name = "1" }
212+
},
213+
new Department
214+
{
215+
Employee = new Boss { Name = "b" }
216+
}
217+
}.AsQueryable();
218+
219+
// Act
220+
int countAsDynamic = qry.Count("As(Employee, @0) != null", typeof(Worker));
221+
222+
// Assert
223+
countAsDynamic.Should().Be(1);
224+
}
225+
180226
public class AS_A { }
181227
public class AS_B : AS_A
182228
{
@@ -190,7 +236,7 @@ public void As_UnaryExpression()
190236
// Arrange
191237
var a = new AS_A();
192238
var b = new AS_B { MyProperty = "x" };
193-
var lst = new List<AS_A>()
239+
var lst = new List<AS_A>
194240
{
195241
a,
196242
b
@@ -289,7 +335,7 @@ public void CastToType_Dynamic_ActingOnIt()
289335
// Assign
290336
var qry = new BaseEmployee[]
291337
{
292-
new Worker { Name = "1", Other = "x" },
338+
new Worker { Name = "1", Other = "x" },
293339
new Worker { Name = "2" }
294340
}.AsQueryable();
295341

@@ -307,7 +353,7 @@ public void IsAndCastToType_Dynamic_ActingOnIt_And_GetProperty()
307353
// Assign
308354
var qry = new BaseEmployee[]
309355
{
310-
new Worker { Name = "1", Other = "x" },
356+
new Worker { Name = "1", Other = "x" },
311357
new Boss { Name = "2", Function = "y" }
312358
}.AsQueryable();
313359

@@ -323,14 +369,14 @@ public void IsAndCastToType_Dynamic_ActingOnIt_And_GetProperty()
323369
public void IsAndCastToType_Dynamic_ActingOnProperty_And_GetProperty()
324370
{
325371
// Assign
326-
var qry = new []
372+
var qry = new[]
327373
{
328-
new EmployeeWrapper { Employee = new Worker { Name = "1", Other = "x" } },
374+
new EmployeeWrapper { Employee = new Worker { Name = "1", Other = "x" } },
329375
new EmployeeWrapper { Employee = new Boss { Name = "2", Function = "y" } }
330376
}.AsQueryable();
331377

332378
// Act
333-
var cast = qry.Select(c => c.Employee is Worker ? ((Worker) c.Employee).Other : "-").ToArray();
379+
var cast = qry.Select(c => c.Employee is Worker ? ((Worker)c.Employee).Other : "-").ToArray();
334380
var castDynamic = qry.Select("iif(Is(Employee, \"System.Linq.Dynamic.Core.Tests.Entities.Worker\"), Cast(Employee, \"System.Linq.Dynamic.Core.Tests.Entities.Worker\").Other, \"-\")").ToDynamicArray();
335381

336382
// Assert

0 commit comments

Comments
 (0)