public void ExpressionFaultPropagation() { var john = TestPerson.CreateJohn(); var emily = TestPerson.CreateEmily(); var firstParameter = Expression.Parameter(typeof(TestPerson)); var secondParameter = Expression.Parameter(typeof(TestPerson)); var testPersonNamePropertyInfo = typeof(TestPerson).GetProperty(nameof(TestPerson.Name)) !; using var expr = ActiveExpression.Create <int>(Expression.Lambda(Expression.Invoke((Expression <Func <string, string, int> >)((p1, p2) => p1.Length + p2.Length), Expression.MakeMemberAccess(firstParameter, testPersonNamePropertyInfo), Expression.MakeMemberAccess(secondParameter, testPersonNamePropertyInfo)), firstParameter, secondParameter), john, emily); Assert.IsNull(expr.Fault); emily.Name = null; Assert.IsNotNull(expr.Fault); emily.Name = "Emily"; Assert.IsNull(expr.Fault); }
public void FaultPropagation() { var john = TestPerson.CreateJohn(); var emily = TestPerson.CreateEmily(); using var expr = ActiveExpression.Create((p1, p2) => p1.Name !.Length > 0 && p2.Name !.Length > 0, john, emily); Assert.IsNull(expr.Fault); john.Name = null; Assert.IsNotNull(expr.Fault); emily.Name = null; john.Name = "John"; Assert.IsNotNull(expr.Fault); emily.Name = "Emily"; Assert.IsNull(expr.Fault); }
public void ConsistentHashCode() { int hashCode1, hashCode2; var john = TestPerson.CreateJohn(); var emily = TestPerson.CreateEmily(); var firstParameter1 = Expression.Parameter(typeof(TestPerson)); var secondParameter1 = Expression.Parameter(typeof(TestPerson)); using (var expr = ActiveExpression.Create <TestPerson>(Expression.Lambda(Expression.Invoke((Expression <Func <TestPerson, TestPerson, TestPerson> >)((p1, p2) => CombinePeople(p1, p2)), firstParameter1, secondParameter1), firstParameter1, secondParameter1), john, emily)) hashCode1 = expr.GetHashCode(); var firstParameter2 = Expression.Parameter(typeof(TestPerson)); var secondParameter2 = Expression.Parameter(typeof(TestPerson)); using (var expr = ActiveExpression.Create <TestPerson>(Expression.Lambda(Expression.Invoke((Expression <Func <TestPerson, TestPerson, TestPerson> >)((p1, p2) => CombinePeople(p1, p2)), firstParameter2, secondParameter2), firstParameter2, secondParameter2), john, emily)) hashCode2 = expr.GetHashCode(); Assert.IsTrue(hashCode1 == hashCode2); }
public void PropertyChanges() { var john = TestPerson.CreateJohn(); var values = new BlockingCollection <int>(); using (var expr = ActiveExpression.Create(p1 => - p1.Name !.Length, john)) { void propertyChanged(object?sender, PropertyChangedEventArgs e) => values.Add(expr.Value); expr.PropertyChanged += propertyChanged; values.Add(expr.Value); john.Name = "J"; john.Name = "John"; john.Name = "Jon"; john.Name = "Jhn"; john.Name = string.Empty; expr.PropertyChanged -= propertyChanged; } Assert.IsTrue(new int[] { -4, -1, -4, -3, 0 }.SequenceEqual(values)); }
public void PropertyChanges() { var john = TestPerson.CreateJohn(); var emily = TestPerson.CreateEmily(); var values = new BlockingCollection <bool>(); using (var expr = ActiveExpression.Create((p1, p2) => p1.Name !.Length == 1 && p2.Name !.Length == 1, john, emily)) { void propertyChanged(object?sender, PropertyChangedEventArgs e) => values.Add(expr.Value); expr.PropertyChanged += propertyChanged; values.Add(expr.Value); john.Name = "J"; emily.Name = "E"; john.Name = "John"; john.Name = "J"; emily.Name = "Emily"; emily.Name = "E"; expr.PropertyChanged -= propertyChanged; } Assert.IsTrue(new bool[] { false, true, false, true, false, true }.SequenceEqual(values)); }
public void PropertyChanges() { var john = TestPerson.CreateJohn(); var emily = TestPerson.CreateEmily(); var values = new BlockingCollection <string>(); using (var expr = ActiveExpression.Create((p1, p2) => string.IsNullOrEmpty(p1.Name) ? p2.Name : p1.Name, john, emily)) { void propertyChanged(object?sender, PropertyChangedEventArgs e) => values.Add(expr.Value !); expr.PropertyChanged += propertyChanged; values.Add(expr.Value !); john.Name = "J"; john.Name = "John"; john.Name = null; emily.Name = "E"; emily.Name = "Emily"; emily.Name = null; emily.Name = "Emily"; john.Name = "John"; expr.PropertyChanged -= propertyChanged; } Assert.IsTrue(new string[] { "John", "J", "John", "Emily", "E", "Emily", null !, "Emily", "John" }.SequenceEqual(values));
public void Equality() { var john = TestPerson.CreateJohn(); var emily = TestPerson.CreateEmily(); var firstParameter1 = Expression.Parameter(typeof(TestPerson)); var secondParameter1 = Expression.Parameter(typeof(TestPerson)); using var expr1 = ActiveExpression.Create <TestPerson>(Expression.Lambda(Expression.Invoke((Expression <Func <TestPerson, TestPerson, TestPerson> >)((p1, p2) => CombinePeople(p1, p2)), firstParameter1, secondParameter1), firstParameter1, secondParameter1), john, emily); var firstParameter2 = Expression.Parameter(typeof(TestPerson)); var secondParameter2 = Expression.Parameter(typeof(TestPerson)); using var expr2 = ActiveExpression.Create <TestPerson>(Expression.Lambda(Expression.Invoke((Expression <Func <TestPerson, TestPerson, TestPerson> >)((p1, p2) => CombinePeople(p1, p2)), firstParameter2, secondParameter2), firstParameter2, secondParameter2), john, emily); var firstParameter3 = Expression.Parameter(typeof(TestPerson)); var secondParameter3 = Expression.Parameter(typeof(TestPerson)); using var expr3 = ActiveExpression.Create <TestPerson>(Expression.Lambda(Expression.Invoke((Expression <Func <TestPerson, TestPerson, TestPerson> >)((p1, p2) => ReversedCombinePeople(p1, p2)), firstParameter3, secondParameter3), firstParameter3, secondParameter3), john, emily); var firstParameter4 = Expression.Parameter(typeof(TestPerson)); var secondParameter4 = Expression.Parameter(typeof(TestPerson)); using var expr4 = ActiveExpression.Create <TestPerson>(Expression.Lambda(Expression.Invoke((Expression <Func <TestPerson, TestPerson, TestPerson> >)((p1, p2) => CombinePeople(p1, p2)), firstParameter4, secondParameter4), firstParameter4, secondParameter4), emily, john); Assert.IsTrue(expr1 == expr2); Assert.IsFalse(expr1 == expr3); Assert.IsFalse(expr1 == expr4); }
public void ClassFields() { var emily = TestPerson.CreateEmily(); using var expr = ActiveExpression.Create(() => new FieldyTestPerson { Name = emily.Name ! });
static TestPerson ReversedCombinePeople(TestPerson a, TestPerson b) => new TestPerson { Name = $"{b.Name} {a.Name}" };
TestPerson CombinePeople(TestPerson a, TestPerson b) => new TestPerson { Name = $"{a.Name} {b.Name}" };
public void InitializerFaultPropagation() { var john = TestPerson.CreateJohn(); var emily = TestPerson.CreateEmily(); using var expr = ActiveExpression.Create(() => string.Concat(new string?[] { john.Name !.Length.ToString(), emily.Name !.Length.ToString() }));
public void CharStringConversion() { var person = new TestPerson("\\"); using var expr = ActiveExpression.Create(p1 => p1.Name ![0], person);