From 1260f9fc00edd8dcc43096a399b63010a19dbaef Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Tue, 5 Jul 2022 10:45:56 +0800 Subject: [PATCH 1/6] =?UTF-8?q?test:=20=E7=A7=BB=E9=99=A4=20LambdaTest=20?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=9B=B4=E6=96=B0=20TryParse=20=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/LambadaExtensionsTest.cs | 16 +++++ test/UnitTest/Utils/LambdaTest.cs | 66 ------------------- 2 files changed, 16 insertions(+), 66 deletions(-) delete mode 100644 test/UnitTest/Utils/LambdaTest.cs diff --git a/test/UnitTest/Extensions/LambadaExtensionsTest.cs b/test/UnitTest/Extensions/LambadaExtensionsTest.cs index c62a841b1..e70e3adae 100644 --- a/test/UnitTest/Extensions/LambadaExtensionsTest.cs +++ b/test/UnitTest/Extensions/LambadaExtensionsTest.cs @@ -434,6 +434,22 @@ public class LambadaExtensionsTest Assert.Equal(val, val2); } + [Fact] + public void TryParse_Ok() + { + Func func = _ => false; + var exp = Expression.Parameter(typeof(int?)); + var pi = typeof(int?).GetProperty("HasValue"); + + if (pi != null) + { + var exp_p = Expression.Property(exp, pi); + func = Expression.Lambda>(exp_p, exp).Compile(); + } + Assert.True(func.Invoke(10)); + Assert.False(func.Invoke(null)); + } + private abstract class MockFilterActionBase : IFilterAction { public abstract IEnumerable GetFilterConditions(); diff --git a/test/UnitTest/Utils/LambdaTest.cs b/test/UnitTest/Utils/LambdaTest.cs deleted file mode 100644 index 555de818f..000000000 --- a/test/UnitTest/Utils/LambdaTest.cs +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -// Website: https://www.blazor.zone or https://argozhang.github.io/ - -using System.Linq.Expressions; - -namespace UnitTest.Utils; - -/// -/// -/// -public class LambdaTest -{ - [Fact] - public void TryParse_Ok() - { - var exp = Expression.Parameter(typeof(int?)); - var pi = typeof(int?).GetProperty("HasValue"); - - if (pi != null) - { - var exp_p = Expression.Property(exp, pi); - - var func = Expression.Lambda>(exp_p, exp).Compile(); - var b = func.Invoke(10); - } - } - - [Fact] - public void NullContains_Ok() - { - var dummy = new Dummy(); - var filter = new FilterKeyValueAction - { - FieldKey = "Foo", - FilterAction = FilterAction.Contains, - FieldValue = "" - }; - var invoker = filter.GetFilterLambda().Compile(); - var ret = invoker.Invoke(dummy); - Assert.False(ret); - } - - private class Dummy - { - public virtual string? Foo { get; set; } - } - - private class Dog : Dummy - { - public override string? Foo { get; set; } - } - - private class Cat : Dummy - { - public new int Foo { get; set; } - } - - private class Fish : Dummy - { - } - - private class Persian : Cat - { - } -} -- Gitee From 39876efde373f295b8d4726cc232f954459e5128 Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Tue, 5 Jul 2022 10:59:21 +0800 Subject: [PATCH 2/6] =?UTF-8?q?test:=20=E6=9B=B4=E6=96=B0=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Extensions/LambadaExtensionsTest.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/UnitTest/Extensions/LambadaExtensionsTest.cs b/test/UnitTest/Extensions/LambadaExtensionsTest.cs index e70e3adae..8633a2ddb 100644 --- a/test/UnitTest/Extensions/LambadaExtensionsTest.cs +++ b/test/UnitTest/Extensions/LambadaExtensionsTest.cs @@ -36,9 +36,11 @@ public class LambadaExtensionsTest [Fact] public void GetFilterLambda_Nullable() { - var filters = new FilterKeyValueAction() { FieldKey = nameof(Foo.DateTime), FieldValue = DateTime.MinValue }; - var exp = filters.GetFilterLambda(); - Assert.True(exp.Compile().Invoke(new Foo() { DateTime = DateTime.MinValue })); + var filters = new FilterKeyValueAction() { FieldKey = nameof(Foo.DateTime), FieldValue = null }; + var invoker = filters.GetFilterLambda().Compile(); + + // FieldValue 为 null 值 直接返回 true + Assert.True(invoker.Invoke(new Foo() { DateTime = DateTime.MinValue })); } [Fact] -- Gitee From 4a4c1035cdaf166b69abcb0e55e1583bd9a582d9 Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Tue, 5 Jul 2022 11:54:51 +0800 Subject: [PATCH 3/6] =?UTF-8?q?fix:=20Table=20=E7=BB=84=E4=BB=B6=E8=BF=87?= =?UTF-8?q?=E6=BB=A4=E6=90=9C=E7=B4=A2=E6=94=AF=E6=8C=81=E5=8F=AF=E4=B8=BA?= =?UTF-8?q?=E7=A9=BA=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/LambdaExtensions.cs | 37 +++++++++++++------ 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/src/BootstrapBlazor/Extensions/LambdaExtensions.cs b/src/BootstrapBlazor/Extensions/LambdaExtensions.cs index 4fc600f37..60cc82118 100644 --- a/src/BootstrapBlazor/Extensions/LambdaExtensions.cs +++ b/src/BootstrapBlazor/Extensions/LambdaExtensions.cs @@ -148,11 +148,12 @@ public static class LambdaExtensions Expression> GetSimpleFilterExpression() { - var prop = typeof(TItem).GetPropertyByName(filter.FieldKey) ?? throw new InvalidOperationException($"the model {type.Name} not found the property {filter.FieldKey}"); ; + var prop = typeof(TItem).GetPropertyByName(filter.FieldKey) ?? throw new InvalidOperationException($"the model {type.Name} not found the property {filter.FieldKey}"); if (prop != null) { var p = Expression.Parameter(type); var fieldExpression = Expression.Property(p, prop); + var isNullable = false; Expression eq = fieldExpression; @@ -160,13 +161,16 @@ public static class LambdaExtensions if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) { + isNullable = true; eq = Expression.Convert(fieldExpression, prop.PropertyType.GenericTypeArguments[0]); } else if (prop.PropertyType.IsEnum && filter.FieldValue is string) { eq = Expression.Call(fieldExpression, prop.PropertyType.GetMethod("ToString", Array.Empty())!); } - eq = filter.GetExpression(eq); + eq = isNullable + ? Expression.AndAlso(Expression.NotEqual(fieldExpression, Expression.Constant(null)), filter.GetExpression(eq)) + : filter.GetExpression(eq); ret = Expression.Lambda>(eq, p); } return ret; @@ -174,8 +178,10 @@ public static class LambdaExtensions Expression> GetComplexFilterExpression() { + Expression> ret = t => true; var p = Expression.Parameter(type); var propertyNames = filter.FieldKey.Split('.'); + var isNullable = false; PropertyInfo? pInfo = null; Expression? fieldExpression = null; foreach (var name in propertyNames) @@ -192,17 +198,26 @@ public static class LambdaExtensions } } - // 可为空类型转化为具体类型 - if (pInfo!.PropertyType.IsGenericType && pInfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) + if (fieldExpression != null) { - fieldExpression = Expression.Convert(fieldExpression!, pInfo.PropertyType.GenericTypeArguments[0]); - } - else if (pInfo.PropertyType.IsEnum && filter.FieldValue is string) - { - fieldExpression = Expression.Call(fieldExpression, pInfo.PropertyType.GetMethod("ToString", Array.Empty())!); + var eq = fieldExpression; + + // 可为空类型转化为具体类型 + if (pInfo!.PropertyType.IsGenericType && pInfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) + { + isNullable = true; + eq = Expression.Convert(fieldExpression, pInfo.PropertyType.GenericTypeArguments[0]); + } + else if (pInfo.PropertyType.IsEnum && filter.FieldValue is string) + { + eq = Expression.Call(fieldExpression, pInfo.PropertyType.GetMethod("ToString", Array.Empty())!); + } + eq = isNullable + ? Expression.AndAlso(Expression.NotEqual(fieldExpression, Expression.Constant(null)), filter.GetExpression(eq)) + : filter.GetExpression(eq); + ret = Expression.Lambda>(eq, p); } - fieldExpression = filter.GetExpression(fieldExpression!); - return Expression.Lambda>(fieldExpression, p); + return ret; } } -- Gitee From 6150f9f5dd1f645c0fb03017afa5089886a0de8b Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Tue, 5 Jul 2022 11:55:08 +0800 Subject: [PATCH 4/6] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=E5=8F=AF?= =?UTF-8?q?=E4=B8=BA=E7=A9=BA=E7=B1=BB=E5=9E=8B=E6=90=9C=E7=B4=A2=E8=BF=87?= =?UTF-8?q?=E6=BB=A4=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/LambadaExtensionsTest.cs | 28 +++++++++++++++++-- test/UnitTest/Utils/UtilityTest.cs | 8 ------ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/test/UnitTest/Extensions/LambadaExtensionsTest.cs b/test/UnitTest/Extensions/LambadaExtensionsTest.cs index 8633a2ddb..1bd67c184 100644 --- a/test/UnitTest/Extensions/LambadaExtensionsTest.cs +++ b/test/UnitTest/Extensions/LambadaExtensionsTest.cs @@ -41,6 +41,13 @@ public class LambadaExtensionsTest // FieldValue 为 null 值 直接返回 true Assert.True(invoker.Invoke(new Foo() { DateTime = DateTime.MinValue })); + + // 过滤条件更改为 MinValue + filters = new FilterKeyValueAction() { FieldKey = nameof(Foo.DateTime), FieldValue = DateTime.MinValue }; + invoker = filters.GetFilterLambda().Compile(); + + Assert.True(invoker.Invoke(new Foo() { DateTime = DateTime.MinValue })); + Assert.False(invoker.Invoke(new Foo() { DateTime = DateTime.Now })); } [Fact] @@ -100,9 +107,13 @@ public class LambadaExtensionsTest [Fact] public void FilterKeyValueAction_FieldKey_Null() { - var filter = new FilterKeyValueAction(); + // FieldValue 为 null 时 均返回 true + var filter = new FilterKeyValueAction() { FieldKey = "Name", FieldValue = null }; var invoker = filter.GetFilterLambda().Compile(); + + // 符合条件 Assert.True(invoker.Invoke(new Foo())); + Assert.True(invoker.Invoke(new Foo() { Name = "Test" })); } [Fact] @@ -111,6 +122,7 @@ public class LambadaExtensionsTest var filter = new FilterKeyValueAction() { FieldKey = "Name", FieldValue = "Name" }; var invoker = filter.GetFilterLambda().Compile(); Assert.True(invoker.Invoke(new Foo() { Name = "Name" })); + Assert.False(invoker.Invoke(new Foo() { Name = "Name1" })); } [Fact] @@ -141,9 +153,21 @@ public class LambadaExtensionsTest [Fact] public void FilterKeyValueAction_ComplexFilterExpression_Nullable() { - var filter = new FilterKeyValueAction() { FieldKey = "Foo.DateTime", FieldValue = DateTime.MinValue }; + // 搜索条件为 DateTime.Now + var filter = new FilterKeyValueAction() { FieldKey = "Foo.DateTime", FieldValue = DateTime.Now }; var invoker = filter.GetFilterLambda().Compile(); + + // 均不符合条件 + Assert.False(invoker.Invoke(new Dummy() { Foo = new Foo() { DateTime = DateTime.MinValue } })); + Assert.False(invoker.Invoke(new Dummy() { Foo = new Foo() { DateTime = null } })); + + // 搜索条件为 Null + filter = new FilterKeyValueAction() { FieldKey = "Foo.DateTime", FieldValue = null }; + invoker = filter.GetFilterLambda().Compile(); + + // 均符合条件 Assert.True(invoker.Invoke(new Dummy() { Foo = new Foo() { DateTime = DateTime.MinValue } })); + Assert.True(invoker.Invoke(new Dummy() { Foo = new Foo() { DateTime = null } })); } [Fact] diff --git a/test/UnitTest/Utils/UtilityTest.cs b/test/UnitTest/Utils/UtilityTest.cs index cc65c75cf..556c23da4 100644 --- a/test/UnitTest/Utils/UtilityTest.cs +++ b/test/UnitTest/Utils/UtilityTest.cs @@ -20,10 +20,6 @@ public class UtilityTest : BootstrapBlazorTestBase var foo = new Foo() { Id = 1 }; var v = Utility.GetKeyValue(foo); Assert.Equal(1, v); - - object foo1 = new Foo() { Id = 2 }; - v = Utility.GetKeyValue(foo1); - Assert.Equal(2, v); } [Fact] @@ -32,10 +28,6 @@ public class UtilityTest : BootstrapBlazorTestBase var foo = new Cat() { Id = 1 }; var v = Utility.GetKeyValue(foo, typeof(CatKeyAttribute)); Assert.Equal(1, v); - - object foo1 = new Cat() { Id = 2 }; - v = Utility.GetKeyValue(foo1, typeof(CatKeyAttribute)); - Assert.Equal(2, v); } [Fact] -- Gitee From bcadbcf8014e268ef97ce983a3275d5b8a1ffd7f Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Tue, 5 Jul 2022 12:00:13 +0800 Subject: [PATCH 5/6] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=E5=8F=AF?= =?UTF-8?q?=E4=B8=BA=E7=A9=BA=E7=B1=BB=E5=9E=8B=E8=BF=87=E6=BB=A4=E4=B8=8E?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Extensions/LambadaExtensionsTest.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/test/UnitTest/Extensions/LambadaExtensionsTest.cs b/test/UnitTest/Extensions/LambadaExtensionsTest.cs index 1bd67c184..1f08dd656 100644 --- a/test/UnitTest/Extensions/LambadaExtensionsTest.cs +++ b/test/UnitTest/Extensions/LambadaExtensionsTest.cs @@ -48,6 +48,7 @@ public class LambadaExtensionsTest Assert.True(invoker.Invoke(new Foo() { DateTime = DateTime.MinValue })); Assert.False(invoker.Invoke(new Foo() { DateTime = DateTime.Now })); + Assert.False(invoker.Invoke(new Foo() { DateTime = null })); } [Fact] -- Gitee From 23852d49453091c11cd7e9e28c4168367ad4ab1e Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Tue, 5 Jul 2022 12:02:50 +0800 Subject: [PATCH 6/6] chore: bump version 6.7.31 --- src/BootstrapBlazor/BootstrapBlazor.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BootstrapBlazor/BootstrapBlazor.csproj b/src/BootstrapBlazor/BootstrapBlazor.csproj index 8f81d79a8..076703ee2 100644 --- a/src/BootstrapBlazor/BootstrapBlazor.csproj +++ b/src/BootstrapBlazor/BootstrapBlazor.csproj @@ -1,7 +1,7 @@ - 6.7.30 + 6.7.31 -- Gitee