protected override bool MatchesCore(IMatcher other) { var valueMatcher = (IValueMatcher)other; var valueAsExpression = this.Value as Expression; var otherValueAsExpression = valueMatcher.Value as Expression; if (valueAsExpression != null && otherValueAsExpression != null) { valueAsExpression = ExpressionReducer.Reduce(valueAsExpression); otherValueAsExpression = ExpressionReducer.Reduce(otherValueAsExpression); return(ExpressionComparer.AreEqual(valueAsExpression, otherValueAsExpression)); } if (this.Value != null && valueMatcher.Value != null) { var thisType = this.Value.GetType(); var otherType = valueMatcher.Value.GetType(); var thisEnumerableType = thisType.GetImplementationOfGenericInterface(typeof(IEnumerable <>)); var otherEnumerableType = otherType.GetImplementationOfGenericInterface(typeof(IEnumerable <>)); if (thisEnumerableType != null && thisEnumerableType == otherEnumerableType && IsSystemCollection(thisType) && IsSystemCollection(otherType)) { var elementType = thisEnumerableType.GetGenericArguments()[0]; var sequenceEqualsMethod = typeof(Enumerable).GetMethods() .FirstOrDefault(method => method.Name == "SequenceEqual" && method.GetParameters().Length == 2) .MakeGenericMethod(elementType); return((bool)sequenceEqualsMethod.Invoke(null, new object[] { this.Value, valueMatcher.Value })); } } return(false); }
public void CanParseStringEqual() { var expected = new FilterExpression <Entity>(e => e.FooString == "42"); var actual = Filter.Parse("FooString='42'").ToFilterExpression <Entity>(); Assert.True(ExpressionComparer.AreEqual(expected.Expression, actual.Expression)); }
public void SortByDescReflectsSortByDesc() { var entities = Entity.BuildEntities().AsQueryable(); var expected = entities.SortBy(e => e.Parent.Foo.Name, SortDirection.Desc).Expression; var actual = entities.SortBy("Parent.Foo.Name", SortDirection.Desc).Expression; Assert.True(ExpressionComparer.AreEqual(expected, actual)); }
public void EquivalentExpressionsAreEqualRegardlessOfParameterName() { // Note one parameter is named "a" while the other is named "b". Expression <Func <Entity, bool> > actual = a => a.FooString == "42"; Expression <Func <Entity, bool> > expected = b => b.FooString == "42"; Assert.True(ExpressionComparer.AreEqual(expected, actual)); }
public void SortByAscMatchesOrderBy() { var entities = Entity.BuildEntities().AsQueryable(); var expected = entities.OrderBy(e => e.Parent.Foo.Name).Expression; var actual = entities.SortBy("Parent.Foo.Name").Expression; Assert.True(ExpressionComparer.AreEqual(expected, actual)); }
public bool Equals(ParameterizedExpression other) { if (other == null || hashCode != other.hashCode) { return(false); } return(ExpressionComparer.AreEqual(expression, other.expression)); }
private static void AssertNotEqual(Expression first, Expression second) { //Act bool result = ExpressionComparer.AreEqual(first, second); //Assert Assert.False(result); }
private static void AssertEqual(Expression first, Expression second) { //Act bool result = ExpressionComparer.AreEqual(first, second); //Assert Assert.That(result, Is.True); }
public void EquivalentExpressionsAreEqual() { // Note both parameters are named "e". Expression <Func <Entity, bool> > actual = e => e.FooString == "42"; Expression <Func <Entity, bool> > expected = e => e.FooString == "42"; Assert.True(ExpressionComparer.AreEqual(expected, actual)); }
public void Test001() { var xgr = new ExpressionGenerator(); var e1 = xgr.MultiProject( () => new { FieldOne = 1, FieldTwo = "ABC" }, x => new InitializerOne { IntField = x.FieldOne, StringField = x.FieldTwo }, _initializerOneTemplate, x => new InitializerTwo { StringFieldOne = x.FieldTwo + "DEF", StringFieldTwo = "GHI" + x.FieldTwo }, _initializerTwoTemplate, (x, y) => new { InitializerOneResult = x, InitializerTwoResult = y } ); var inputPlaceholder = xgr.FromFunc(() => new { FieldOne = 1, FieldTwo = "ABC" }); var expectations = xgr.FromFunc(inputPlaceholder, x => new { InitializerOneResult = (new InitializerOne { IntField = x.FieldOne, StringField = x.FieldTwo }).StringField + (new InitializerOne { IntField = x.FieldOne, StringField = x.FieldTwo }).IntField, InitializerTwoResult = (new InitializerTwo { StringFieldOne = x.FieldTwo + "DEF", StringFieldTwo = "GHI" + x.FieldTwo }).StringFieldTwo + (new InitializerTwo { StringFieldOne = x.FieldTwo + "DEF", StringFieldTwo = "GHI" + x.FieldTwo }).StringFieldOne }); var inputParam = Expression.Parameter(inputPlaceholder.ReturnType); var expected = Expression.Lambda( ExpressionReplacer.Replace(expectations.Body, inputPlaceholder.Body, inputParam), inputParam ); var comparer = new ExpressionComparer(); Assert.IsTrue(comparer.AreEqual(expected, e1)); }
public void Test001() { var xgr = new ExpressionGenerator(); var e1 = xgr.MultiProject( ()=>new { FieldOne = 1, FieldTwo = "ABC" }, x=>new InitializerOne { IntField = x.FieldOne, StringField = x.FieldTwo }, _initializerOneTemplate, x=>new InitializerTwo { StringFieldOne = x.FieldTwo + "DEF", StringFieldTwo = "GHI" + x.FieldTwo }, _initializerTwoTemplate, (x,y)=>new { InitializerOneResult = x, InitializerTwoResult = y } ); var inputPlaceholder = xgr.FromFunc(()=>new { FieldOne = 1, FieldTwo = "ABC" }); var expectations = xgr.FromFunc(inputPlaceholder, x=>new { InitializerOneResult = (new InitializerOne { IntField = x.FieldOne, StringField = x.FieldTwo }).StringField + (new InitializerOne { IntField = x.FieldOne, StringField = x.FieldTwo }).IntField, InitializerTwoResult = (new InitializerTwo { StringFieldOne = x.FieldTwo + "DEF", StringFieldTwo = "GHI" + x.FieldTwo }).StringFieldTwo + (new InitializerTwo { StringFieldOne = x.FieldTwo + "DEF", StringFieldTwo = "GHI" + x.FieldTwo }).StringFieldOne }); var inputParam = Expression.Parameter(inputPlaceholder.ReturnType); var expected = Expression.Lambda( ExpressionReplacer.Replace(expectations.Body, inputPlaceholder.Body, inputParam), inputParam ); var comparer = new ExpressionComparer(); Assert.IsTrue(comparer.AreEqual(expected, e1)); }
public void Test001() { var xgr = new ExpressionGenerator(); var expr = Expression.Default(typeof(string)); var expr2 = xgr.WithType<string>(expr); var expr3 = xgr.WithoutType(expr2); var xcr = new ExpressionComparer(); Assert.IsTrue(xcr.AreEqual(expr, expr3)); }
public override bool Equals(IMatcher other) { var predicateMatcher = other as PredicateMatcher <T>; if (predicateMatcher == null) { return(false); } return(ExpressionComparer.AreEqual(predicateExpression, predicateMatcher.predicateExpression)); }
public void Test001() { var xgr = new ExpressionGenerator(); var expr = Expression.Default(typeof(string)); var expr2 = xgr.WithType <string>(expr); var expr3 = xgr.WithoutType(expr2); var xcr = new ExpressionComparer(); Assert.IsTrue(xcr.AreEqual(expr, expr3)); }
public bool Equals(BindingExpression other) { if (ReferenceEquals(null, other)) { return(false); } if (ReferenceEquals(this, other)) { return(true); } return(ExpressionComparer.AreEqual(Expression, other.Expression)); }
public virtual void Test008() { var object01 = Enumerable.Range(0, 1).AsQueryable(); var object02 = Enumerable.Range(0, 1).AsQueryable(); var constant01 = Expression.Constant(object01.Where(i => i < 10)); var constant02 = Expression.Constant(object02.Where(i => i < 10)); var xc = new ExpressionComparer(); Assert.IsTrue(xc.AreEqual(constant01, constant02, (a, b) => (a == object01 && b == object02) || object.Equals(a, b))); }
public bool Equals(AlphaCondition other) { if (ReferenceEquals(null, other)) { return(false); } if (ReferenceEquals(this, other)) { return(true); } return(ExpressionComparer.AreEqual(_expression, other._expression)); }
public void Test010() { var xpr = new ExpressionReader(); var xcr = new ExpressionComparer(); var expr = Expression.UnaryPlus(Expression.Default(typeof(int))); var result = xpr.StripConvert(expr); Assert.AreEqual(expr, result); Assert.IsInstanceOfType(result, typeof(UnaryExpression)); Assert.IsTrue(xcr.AreEqual(Expression.UnaryPlus(Expression.Default(typeof(int))), result)); }
public void Test005() { var xpr = new ExpressionReader(); var xcr = new ExpressionComparer(); var expr = Expression.Convert(Expression.Convert(Expression.Default(typeof(byte)), typeof(short)), typeof(int)); var result = xpr.StripConvert(expr); Assert.AreNotEqual(expr, result); Assert.IsInstanceOfType(result, typeof(DefaultExpression)); Assert.IsTrue(xcr.AreEqual(Expression.Default(typeof(byte)), result)); }
private Expression VisitCorrelatedGroup(CorrelatedExpression node) { var groupExpression = (GroupByExpression)node.Expression; if (_accumulatorLookup != null && _accumulatorLookup.Contains(node.CorrelationId)) { var oldAccumulatorLookup = _accumulatorLookup; var source = Visit(groupExpression.Source); _accumulatorLookup = oldAccumulatorLookup; var accumulators = new List <AccumulatorExpression>(); var fieldExpressions = new List <FieldExpression>(); var comparer = new ExpressionComparer(); foreach (var correlatedAccumulator in _accumulatorLookup[node.CorrelationId]) { var index = accumulators.FindIndex(x => comparer.Compare((Expression)x, correlatedAccumulator.Expression)); FieldExpression fieldExpression; if (index == -1) { var accumulator = (AccumulatorExpression)correlatedAccumulator.Expression; // TODO: might not need to do any renames... accumulator = new AccumulatorExpression( accumulator.Type, "__agg" + accumulators.Count, accumulator.Serializer, accumulator.AccumulatorType, accumulator.Argument); accumulators.Add(accumulator); fieldExpression = new FieldExpression(accumulator.FieldName, accumulator.Serializer); fieldExpressions.Add(fieldExpression); } else { fieldExpression = fieldExpressions[index]; } _accumulatorReplacementMap[correlatedAccumulator] = fieldExpression; } groupExpression = new GroupByExpression( groupExpression.Type, source, groupExpression.KeySelector, accumulators.AsReadOnly()); } return(Visit(groupExpression)); }
private Expression VisitCorrelatedGroup(CorrelatedExpression node) { var groupExpression = (GroupByExpression)node.Expression; if (_accumulatorLookup != null && _accumulatorLookup.Contains(node.CorrelationId)) { var oldAccumulatorLookup = _accumulatorLookup; var source = Visit(groupExpression.Source); _accumulatorLookup = oldAccumulatorLookup; var accumulators = new List<AccumulatorExpression>(); var fieldExpressions = new List<FieldExpression>(); var comparer = new ExpressionComparer(); foreach (var correlatedAccumulator in _accumulatorLookup[node.CorrelationId]) { var index = accumulators.FindIndex(x => comparer.Compare((Expression)x, correlatedAccumulator.Expression)); FieldExpression fieldExpression; if (index == -1) { var accumulator = (AccumulatorExpression)correlatedAccumulator.Expression; // TODO: might not need to do any renames... accumulator = new AccumulatorExpression( accumulator.Type, "__agg" + accumulators.Count, accumulator.Serializer, accumulator.AccumulatorType, accumulator.Argument); accumulators.Add(accumulator); fieldExpression = new FieldExpression(accumulator.FieldName, accumulator.Serializer); fieldExpressions.Add(fieldExpression); } else { fieldExpression = fieldExpressions[index]; } _accumulatorReplacementMap[correlatedAccumulator] = fieldExpression; } groupExpression = new GroupByExpression( groupExpression.Type, source, groupExpression.KeySelector, accumulators.AsReadOnly()); } return Visit(groupExpression); }
public static bool AreEqual(ExpressionMap first, ExpressionMap second) { if (first.Count != second.Count) { return(false); } //Assume values are sorted var pairs = first.Zip(second, System.Tuple.Create); bool result = pairs.All(t => t.Item1.Name == t.Item2.Name && ExpressionComparer.AreEqual(t.Item1.Expression, t.Item2.Expression)); return(result); }
public void CompareExpression() { Expression <Func <int, int> > f1 = a => a; Expression <Func <int, int> > f2 = a => a; Expression <Func <int, int> > f3 = b => b; Assert.IsTrue(ExpressionComparer.AreEqual(f1, f2, checkParameterNames: true)); Assert.IsTrue(ExpressionComparer.AreEqual(f1, f2, checkParameterNames: false)); Assert.IsFalse(ExpressionComparer.AreEqual(f1, f3, checkParameterNames: true)); Assert.IsTrue(ExpressionComparer.AreEqual(f1, f3, checkParameterNames: false)); f1.Evaluate(1); Assert2.Throws <InvalidOperationException>("cache", () => f2.Evaluate(2)); }
public static bool AreEqual(ExpressionCollection first, ExpressionCollection second) { if (first.Count != second.Count) { return(false); } //Values must be in same order var pairs = first.Zip(second, System.Tuple.Create); bool result = pairs.All(t => t.Item1.Name == t.Item2.Name && ExpressionComparer.AreEqual(t.Item1.Expression, t.Item2.Expression)); return(result); }
private void BuildBindingNode(ReteBuilderContext context, BindingElement element) { var node = context.BetaSource .Sinks.OfType <BindingNode>() .FirstOrDefault(x => ExpressionComparer.AreEqual(x.BindingExpression.Expression, element.Expression)); if (node == null) { var bindingExpression = ExpressionCompiler.CompileBindingExpression(element, context.Declarations); node = new BindingNode(bindingExpression, element.ResultType, context.BetaSource); } BuildBetaMemoryNode(context, node); context.ResetAlphaSource(); }
public override bool Equals(object obj) { if (obj == this) { return(true); } CacheEntry other = obj as CacheEntry; if (other == null) { return(false); } return(ExpressionComparer.AreEqual(Expression, other.Expression, false)); }
public void Test001() { var xgr = new ExpressionGenerator(); var actual = xgr.FromAction(() => Console.WriteLine("hello world")); var expected = Expression.Call( null, typeof(Console).GetMethod("WriteLine", new[] { typeof(string) }), Expression.Constant("hello world") ); var comparer = new ExpressionComparer(); Assert.IsTrue(comparer.AreEqual(expected, actual)); }
public void Test002() { var xgr = new ExpressionGenerator(); var actual = xgr.FromAction(() => paramFunc <int>()); var expected = Expression.Call( null, typeof(ExpressionGeneratorFromActionTests).GetMethod("paramFunc").MakeGenericMethod(typeof(int)), Expression.NewArrayInit(typeof(int)) ); var comparer = new ExpressionComparer(); Assert.IsTrue(comparer.AreEqual(expected, actual)); }
public void Test001() { var xgr = new ExpressionGenerator(); var actual = xgr.FromFunc(() => Environment.GetCommandLineArgs()); var expected = Expression.Lambda <Func <string[]> >( Expression.Call( null, typeof(Environment).GetMethod("GetCommandLineArgs") ) ); var comparer = new ExpressionComparer(); Assert.IsTrue(comparer.AreEqual(expected, actual)); }
public void Test013() { var xpr = new ExpressionReader(); var xcr = new ExpressionComparer(); var expr = Expression.Convert( Expression.Convert( Expression.Default(typeof(int)), typeof(decimal)), typeof(long) ); var result = xpr.GetTrueUnderlyingType(expr); Assert.AreEqual(result, typeof(int)); }
/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> public virtual bool SequenceEquals(IEnumerable <Expression> x, IEnumerable <Expression> y) { if (x == null || y == null) { return(false); } if (x.Count() != y.Count()) { return(false); } var comparer = new ExpressionComparer(); return(x.Zip(y, (l, r) => comparer.Compare(l, r)).All(r => r)); }
public void Test003() { var xgr = new ExpressionGenerator(); var args = GetIntArgs(1); var actual = xgr.FromAction(args[0], a => paramFunc(a)); var expected = Expression.Call( null, typeof(ExpressionGeneratorFromActionTests).GetMethod("paramFunc").MakeGenericMethod(typeof(int)), Expression.NewArrayInit(typeof(int), args.Select(a => a.Body).ToArray()) ); var comparer = new ExpressionComparer(); Assert.IsTrue(comparer.AreEqual(expected, actual)); }
public void CheckExpressionComparerBehaviour() { // This test is just to validate how IQToolkit.ExpressionComparer behaves var data = new int[] { 1, 3, 5, 6 }; int mod = 2; var query1 = from entry in data.AsQueryable() where entry % mod == 0 select entry * 4; mod = 3; var query2 = from entry in data.AsQueryable() where entry % mod == 0 select entry * 4; var query3 = from entry in data.AsQueryable() where entry % mod == 0 select entry * 5; var query4 = from entry in data.AsQueryable() where entry % 2 == 0 select entry * 4; Assert.False(ExpressionComparer.AreEqual(query1.Expression, query2.Expression)); // Different parameter value Assert.True(ExpressionComparer.AreEqual(query1.Expression, query2.Expression, false)); // Different parameter value Assert.True(ExpressionComparer.AreEqual(query1.Expression, query3.Expression, false)); // Different constant Assert.False(ExpressionComparer.AreEqual(query1.Expression, query4.Expression, false)); // Constant versus parameter }
public void Test003() { var xgr = new ExpressionGenerator(); var xc = new ExpressionComparer(); var e2 = xgr.WithType<int>(Expression.Default(typeof(int))); var e3 = xgr.WithType<int>(Expression.Default(typeof(int))); var e1 = xgr.MakeEqualityComparison(e2, e3); var expected = Expression.Lambda<Func<bool>>( Expression.MakeBinary( ExpressionType.Equal, Expression.Default(typeof(int)), Expression.Default(typeof(int)) ) ); Assert.IsTrue(xc.AreEqual(expected, e1)); }
protected internal override Expression VisitCorrelatedGroupBy(CorrelatedGroupByExpression node) { if (_lookup != null && _lookup.Contains(node.CorrelationId)) { var source = Visit(node.Source); var accumulators = new List<SerializationExpression>(); var comparer = new ExpressionComparer(); foreach (var correlatedAccumulator in _lookup[node.CorrelationId]) { var index = accumulators.FindIndex(x => comparer.Compare(x.Expression, correlatedAccumulator.Accumulator)); if (index == -1) { var serializer = _serializerRegistry.GetSerializer(correlatedAccumulator.Type); var info = new BsonSerializationInfo( "__agg" + accumulators.Count, serializer, serializer.ValueType); var serializationExpression = new SerializationExpression(correlatedAccumulator.Accumulator, info); accumulators.Add(serializationExpression); _map[correlatedAccumulator] = serializationExpression; } else { _map[correlatedAccumulator] = accumulators[index]; } } node = node.Update( source, node.Id, accumulators.OfType<Expression>()); } return base.VisitCorrelatedGroupBy(node); }
public override bool AreEqual(Expression left, Expression right) { var xc = new ExpressionComparer(); return xc.AreEqual(left, right); }
public void Test073() { var xgr = new ExpressionGenerator(); var xc = new ExpressionComparer(); var e2 = xgr.WithType<int>(Expression.Default(typeof(int))); var e3 = xgr.WithType<int>(Expression.Default(typeof(int))); var e1 = xgr.MakeBinary(e2, ExpressionType.Add, e3); var expected = Expression.MakeBinary( ExpressionType.Add, Expression.Default(typeof(int)), Expression.Default(typeof(int)) ); Assert.IsTrue(xc.AreEqual(expected, e1)); }
public override bool AreEqual(Dictionary<ParameterExpression, ParameterExpression> mapping, Expression left, Expression right) { var xc = new ExpressionComparer(); return xc.AreEqual(mapping, left, right); }
public void Test004() { var xgr = new ExpressionGenerator(); var q = Enumerable.Range(0, 1).Select(x => new { FieldOne = 1, FieldTwo = "ABC" }).AsQueryable(); var e1 = xgr.MultiProject( q, x => x.FieldTwo + x.FieldOne, x => "GHI" + x.FieldTwo + x.FieldTwo + "DEF", (x, y) => new { InitializerOneResult = x, InitializerTwoResult = y } ); var inputPlaceholder = xgr.FromFunc(() => new { FieldOne = 1, FieldTwo = "ABC" }); var expectations = xgr.FromFunc(inputPlaceholder, x => new { InitializerOneResult = x.FieldTwo + x.FieldOne, InitializerTwoResult = "GHI" + x.FieldTwo + x.FieldTwo + "DEF" }); var inputParam = Expression.Parameter(inputPlaceholder.ReturnType); var expected = Expression.Lambda( ExpressionReplacer.Replace(expectations.Body, inputPlaceholder.Body, inputParam), inputParam ); var inner1 = e1.Expression as MethodCallExpression; var inner2 = inner1.Arguments[1] as UnaryExpression; var inner3 = inner2.Operand as LambdaExpression; var comparer = new ExpressionComparer(); Assert.IsTrue(comparer.AreEqual(expected, inner3)); }
public void Test093() { var xgr = new ExpressionGenerator(); var xc = new ExpressionComparer(); var e2 = xgr.WithType<bool>(Expression.Default(typeof(bool))); var e3 = xgr.WithType<bool>(Expression.Default(typeof(bool))); var e1 = xgr.MakeOrElse(e2, e3); var expected = Expression.MakeBinary( ExpressionType.OrElse, Expression.Default(typeof(bool)), Expression.Default(typeof(bool)) ); Assert.IsTrue(xc.AreEqual(expected, e1)); }
public void Test082() { var xgr = new ExpressionGenerator(); var xc = new ExpressionComparer(); var e2 = xgr.WithType<bool?>(Expression.Default(typeof(bool?))); var e3 = xgr.WithType<bool>(Expression.Default(typeof(bool))); var e1 = xgr.MakeAndAlso(e2, e3); var expected = Expression.MakeBinary( ExpressionType.AndAlso, Expression.Default(typeof(bool?)), Expression.Convert(Expression.Default(typeof(bool)), typeof(bool?)) ); Assert.IsTrue(xc.AreEqual(expected, e1)); }