using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using Luticate2.Auth.Utils.Business.Converters; using Luticate2.Auth.Utils.Business.Converters.ExpressionConverter; using Luticate2.Auth.Utils.Interfaces; using Microsoft.Extensions.DependencyInjection; using Xunit; namespace Luticate2.Auth.Tests.Business.ExpressionConverter { public class ExpressionConverterTests { protected IServiceProvider GetServiceProvider() { var services = new ServiceCollection(); services.AddLuObjectConverterDescriptors(); services.AddSingleton, LuOcdTest1>(); services.AddSingleton, LuOcdTest2>(); var serviceProvider = services.BuildServiceProvider(); return serviceProvider; } protected ILuExpressionConverterVisitorOptions GetConverterOptions() { var options = new LuConvertersOptions { Parameters = new Dictionary(), TypeConverter = new LuConvertersTypeConverter(new Dictionary { {typeof(TestDbo1), typeof(TestModel1)}, {typeof(TestDbo2), typeof(TestModel2)} }) }; return options; } protected LuExpressionConverterVisitor GetConverter() { return new LuExpressionConverterVisitor(GetConverterOptions(), GetServiceProvider()); } public static IEnumerable GetTests() { return new List { new object[] { "TestSimpleProperty1", (Expression>) (x => x.TestDbo1.Id), (Expression>) (Param_0 => Param_0.test_model1.id) }, new object[] { "TestSimpleProperty2", (Expression>) (x => x.TestDbo1.Id == Guid.Empty), (Expression>) (Param_0 => Param_0.test_model1.id == Guid.Empty) }, new object[] { "TestSimpleProperty3", (Expression>) (x => x.TestDbo1.Name.Length == 2), (Expression>) (Param_0 => Param_0.test_model1.name.Length == 2) }, new object[] { "TestStaticProperty1", (Expression>) (x => (x.TestDbo1.Name == "42") == StaticDbo.StaticField), (Expression>) (Param_0 => (Param_0.test_model1.name == "42") == StaticDbo.StaticField) }, new object[] { "TestStaticProperty2", (Expression>) (x => (x.TestDbo1.Name == "42") == StaticDbo.StaticProperty), (Expression>) (Param_0 => (Param_0.test_model1.name == "42") == StaticDbo.StaticProperty) }, new object[] { "TestVirtualProperty1", (Expression>) (x => x.NameVirtual), (Expression>) (Param_0 => Param_0.name == null ? "[no data]" : Param_0.name + " " + Param_0.name) }, new object[] { "TestVirtualProperty2", (Expression>) (x => x.Parent.NameVirtual), (Expression>) (Param_0 => Param_0.parent.name == null ? "[no data]" : Param_0.parent.name + " " + Param_0.parent.name) }, new object[] { "TestStaticValue1", (Expression>) (x => 0), (Expression>) (Param_0 => 0) }, new object[] { "TestStaticValue2", (Expression>) (x => x), (Expression>) (Param_0 => Param_0) }, new object[] { "TestStaticValue3", (Expression>) (x => StaticDbo.StaticField), (Expression>) (Param_0 => StaticDbo.StaticField) }, new object[] { "TestComplexExpression1", (Expression>) (x => (x.Name + " " + x.Name).Length), (Expression>) (Param_0 => (Param_0.name + " " + Param_0.name).Length) }, new object[] { "TestComplexExpression2", (Expression>) (x => (x.NameVirtual + " " + x.Name).Length), (Expression>) (Param_0 => ((Param_0.name == null ? "[no data]" : (Param_0.name + " " + Param_0.name)) + " " + Param_0.name).Length) }, new object[] { "TestSimpleMethodSimpleArg1", (Expression>) (x => x.Name.Contains("s")), (Expression>) (Param_0 => Param_0.name.Contains("s")) }, new object[] { "TestSimpleMethodSimpleArg2", (Expression>) (x => x.TestDbo1.Name.Contains("s")), (Expression>) (Param_0 => Param_0.test_model1.name.Contains("s")) }, new object[] { "TestSimpleMethodSimpleArg3", (Expression>) (x => x.TestDbo1.ToString()), (Expression>) (Param_0 => Param_0.test_model1.id + ": " + Param_0.test_model1.name) }, new object[] { "TestSimpleMethodAdvArg1", (Expression>) (x => x.TestDbo1.Name.Contains(x.Name)), (Expression>) (Param_0 => Param_0.test_model1.name.Contains(Param_0.name)) }, new object[] { "TestAdvMethodSimpleArg1", (Expression>) (x => x.TestDbo2s.Any()), (Expression>) (Param_0 => Param_0.test_model2.Any()) }, new object[] { "TestAdvMethodAdvArg1", (Expression>) (x => x.TestDbo2s.Any(y => y.Name.Contains(x.Name + "s"))), (Expression>) (Param_0 => Param_0.test_model2.Any(Param_1 => Param_1.name.Contains(Param_0.name + "s"))) } }; } [Theory] [MemberData(nameof(GetTests))] public void Test(string testName, LambdaExpression expDbo, LambdaExpression expModel) { var converter = GetConverter(); var result = converter.Visit(expDbo); Assert.Equal(expModel.ToString(), result?.ToString()); } } }