Exemple #1
0
        public void StringConversion()
        {
            var emily = TestPerson.CreateEmily();

            emily.Name     = "X";
            using var expr = ActiveExpression.Create(p1 => p1.Name == "X" && p1.Name.Length == 1, emily);
            Assert.AreEqual("(({C} /* {X} */.Name /* \"X\" */ == {C} /* \"X\" */) /* True */ && ({C} /* {X} */.Name /* \"X\" */.Length /* 1 */ == {C} /* 1 */) /* True */) /* True */", expr.ToString());
        }
Exemple #2
0
        public void StringConversion()
        {
            var john  = TestPerson.CreateJohn();
            var emily = TestPerson.CreateEmily();

            using var expr = ActiveExpression.Create((p1, p2) => CombinePeople(p1, p2), john, emily);
            Assert.AreEqual($"{{C}} /* {this} */.CombinePeople({{C}} /* {john} */, {{C}} /* {emily} */) /* {expr.Value} */", expr.ToString());
        }
        public void StringConversion()
        {
            var emily = TestPerson.CreateEmily();

            emily.Name     = "X";
            using var expr = ActiveExpression.Create(p1 => - p1.Name !.Length, emily);
            Assert.AreEqual("(-{C} /* {X} */.Name /* \"X\" */.Length /* 1 */) /* -1 */", expr.ToString());
        }
Exemple #4
0
        public void ValueShortCircuiting()
        {
            var john  = TestPerson.CreateJohn();
            var emily = TestPerson.CreateEmily();

            using (var expr = ActiveExpression.Create((p1, p2) => p1.Name !.Length == 1 && p2.Name !.Length > 3, john, emily))
                Assert.IsFalse(expr.Value);
            Assert.AreEqual(0, emily.NameGets);
        }
Exemple #5
0
        public void LambdaValue()
        {
            var john            = TestPerson.CreateJohn();
            var emily           = TestPerson.CreateEmily();
            var firstParameter  = Expression.Parameter(typeof(TestPerson));
            var secondParameter = Expression.Parameter(typeof(TestPerson));

            using var expr = ActiveExpression.Create <TestPerson>(Expression.Lambda(Expression.Invoke((Expression <Func <TestPerson, TestPerson, TestPerson> >)((p1, p2) => CombinePeople(p1, p2)), firstParameter, secondParameter), firstParameter, secondParameter), john, emily);
            Assert.AreEqual("John Emily", expr.Value !.Name);
        }
Exemple #6
0
        public void StringConversion()
        {
            var john            = TestPerson.CreateJohn();
            var emily           = TestPerson.CreateEmily();
            var firstParameter  = Expression.Parameter(typeof(TestPerson));
            var secondParameter = Expression.Parameter(typeof(TestPerson));

            using var expr = ActiveExpression.Create <TestPerson>(Expression.Lambda(Expression.Invoke((Expression <Func <TestPerson, TestPerson, TestPerson> >)((p1, p2) => CombinePeople(p1, p2)), firstParameter, secondParameter), firstParameter, secondParameter), john, emily);
            Assert.AreEqual("λ({C} /* Cogs.ActiveExpressions.Tests.ActiveInvocationExpression */.CombinePeople({C} /* {John} */, {C} /* {Emily} */) /* {John Emily} */)", expr.ToString());
        }
Exemple #7
0
        public void ActuallyAProperty()
        {
            var emily = TestPerson.CreateEmily();

            using var expr = ActiveExpression.Create(Expression.Lambda <Func <string> >(Expression.Call(Expression.Constant(emily), typeof(TestPerson).GetProperty(nameof(TestPerson.Name)) !.GetMethod !)));
            Assert.IsNull(expr.Fault);
            Assert.AreEqual("Emily", expr.Value);
            emily.Name = "E";
            Assert.IsNull(expr.Fault);
            Assert.AreEqual("E", expr.Value);
        }
Exemple #8
0
        public void MethodDelegateValue()
        {
            Func <TestPerson, TestPerson, TestPerson> @delegate = CombinePeople;
            var john            = TestPerson.CreateJohn();
            var emily           = TestPerson.CreateEmily();
            var firstParameter  = Expression.Parameter(typeof(TestPerson));
            var secondParameter = Expression.Parameter(typeof(TestPerson));

            using var expr = ActiveExpression.Create <TestPerson>(Expression.Lambda(Expression.Invoke(Expression.Constant(@delegate), firstParameter, secondParameter), firstParameter, secondParameter), john, emily);
            Assert.AreEqual("John Emily", expr.Value !.Name);
        }
Exemple #9
0
        public void ConsistentHashCode()
        {
            int hashCode1, hashCode2;
            var john  = TestPerson.CreateJohn();
            var emily = TestPerson.CreateEmily();

            using (var expr = ActiveExpression.Create((p1, p2) => CombinePeople(p1, p2), john, emily))
                hashCode1 = expr.GetHashCode();
            using (var expr = ActiveExpression.Create((p1, p2) => CombinePeople(p1, p2), john, emily))
                hashCode2 = expr.GetHashCode();
            Assert.IsTrue(hashCode1 == hashCode2);
        }
Exemple #10
0
        public void ArgumentFaultPropagation()
        {
            var john  = TestPerson.CreateJohn();
            var emily = TestPerson.CreateEmily();

            using var expr = ActiveExpression.Create(() => CombinePeople(john.Name !.Length > 3 ? john : null !, emily));
            Assert.IsNull(expr.Fault);
            john.Name = null;
            Assert.IsNotNull(expr.Fault);
            john.Name = "John";
            Assert.IsNull(expr.Fault);
        }
Exemple #11
0
        public void FaultPropagationTest()
        {
            var john = TestPerson.CreateJohn();

            john.Name = null;
            var emily = TestPerson.CreateEmily();

            using var expr = ActiveExpression.Create((p1, p2) => p1.Name !.Length > 0 ? p1.Name : p2.Name, john, emily);
            Assert.IsNotNull(expr.Fault);
            john.Name = "John";
            Assert.IsNull(expr.Fault);
        }
Exemple #12
0
        public void ConsistentHashCode()
        {
            int hashCode1, hashCode2;
            var john  = TestPerson.CreateJohn();
            var emily = TestPerson.CreateEmily();

            using (var expr = ActiveExpression.Create((p1, p2) => p1.Name !.Length > 0 ? p1.Name : p2.Name, john, emily))
                hashCode1 = expr.GetHashCode();
            using (var expr = ActiveExpression.Create((p1, p2) => p1.Name !.Length > 0 ? p1.Name : p2.Name, john, emily))
                hashCode2 = expr.GetHashCode();
            Assert.IsTrue(hashCode1 == hashCode2);
        }
Exemple #13
0
        public void Equals()
        {
            var john  = TestPerson.CreateJohn();
            var emily = TestPerson.CreateEmily();

            using var expr1 = ActiveExpression.Create((p1, p2) => p1.Name !.Length > 0 ? p1.Name : p2.Name, john, emily);
            using var expr2 = ActiveExpression.Create((p1, p2) => p1.Name !.Length > 0 ? p1.Name : p2.Name, john, emily);
            using var expr3 = ActiveExpression.Create((p1, p2) => p2.Name !.Length > 0 ? p2.Name : p1.Name, john, emily);
            using var expr4 = ActiveExpression.Create((p1, p2) => p1.Name !.Length > 0 ? p1.Name : p2.Name, emily, john);
            Assert.IsTrue(expr1.Equals(expr2));
            Assert.IsFalse(expr1.Equals(expr3));
            Assert.IsFalse(expr1.Equals(expr4));
        }
Exemple #14
0
        public void Inequality()
        {
            var john  = TestPerson.CreateJohn();
            var emily = TestPerson.CreateEmily();

            using var expr1 = ActiveExpression.Create((p1, p2) => new string?[] { p1.Name, p2.Name }, john, emily);
            using var expr2 = ActiveExpression.Create((p1, p2) => new string?[] { p1.Name, p2.Name }, john, emily);
            using var expr3 = ActiveExpression.Create((p1, p2) => new string?[] { p2.Name, p1.Name }, john, emily);
            using var expr4 = ActiveExpression.Create((p1, p2) => new string?[] { p1.Name, p2.Name }, emily, john);
            Assert.IsFalse(expr1 != expr2);
            Assert.IsTrue(expr1 != expr3);
            Assert.IsTrue(expr1 != expr4);
        }
Exemple #15
0
        public void FaultPropagation()
        {
            var john  = TestPerson.CreateJohn();
            var emily = TestPerson.CreateEmily();

            using var expr = ActiveExpression.Create((p1, p2) => p1.Name !.ToString() ?? p2.Name !.ToString(), john, emily);
            Assert.IsNull(expr.Fault);
            john.Name = null;
            Assert.IsNotNull(expr.Fault);
            emily.Name = null;
            john.Name  = "John";
            Assert.IsNull(expr.Fault);
        }
Exemple #16
0
        public void Equals()
        {
            var john  = TestPerson.CreateJohn();
            var emily = TestPerson.CreateEmily();

            using var expr1 = ActiveExpression.Create((p1, p2) => CombinePeople(p1, p2), john, emily);
            using var expr2 = ActiveExpression.Create((p1, p2) => CombinePeople(p1, p2), john, emily);
            using var expr3 = ActiveExpression.Create((p1, p2) => ReversedCombinePeople(p1, p2), john, emily);
            using var expr4 = ActiveExpression.Create((p1, p2) => CombinePeople(p1, p2), emily, john);
            Assert.IsTrue(expr1.Equals(expr2));
            Assert.IsFalse(expr1.Equals(expr3));
            Assert.IsFalse(expr1.Equals(expr4));
        }
        public void Inequality()
        {
            var john  = TestPerson.CreateJohn();
            var emily = TestPerson.CreateEmily();

            using var expr1 = ActiveExpression.Create(p1 => - p1.Name !.Length, john);
            using var expr2 = ActiveExpression.Create(p1 => - p1.Name !.Length, john);
            using var expr3 = ActiveExpression.Create(p1 => + p1.Name !.Length, john);
            using var expr4 = ActiveExpression.Create(p1 => - p1.Name !.Length, emily);
            Assert.IsFalse(expr1 != expr2);
            Assert.IsTrue(expr1 != expr3);
            Assert.IsTrue(expr1 != expr4);
        }
        public void Equals()
        {
            var john  = TestPerson.CreateJohn();
            var emily = TestPerson.CreateEmily();

            using var expr1 = ActiveExpression.Create(p1 => p1.Name !.Length + 2, john);
            using var expr2 = ActiveExpression.Create(p1 => p1.Name !.Length + 2, john);
            using var expr3 = ActiveExpression.Create(p1 => p1.Name !.Length - 2, john);
            using var expr4 = ActiveExpression.Create(p1 => p1.Name !.Length + 2, emily);
            Assert.IsTrue(expr1.Equals(expr2));
            Assert.IsFalse(expr1.Equals(expr3));
            Assert.IsFalse(expr1.Equals(expr4));
        }
Exemple #19
0
        public void Equals()
        {
            var john  = TestPerson.CreateJohn();
            var emily = TestPerson.CreateEmily();

            using var expr1 = ActiveExpression.Create(p1 => p1.Name ?? string.Empty, john);
            using var expr2 = ActiveExpression.Create(p1 => p1.Name ?? string.Empty, john);
            using var expr3 = ActiveExpression.Create(p1 => p1.Name ?? "Another String", john);
            using var expr4 = ActiveExpression.Create(p1 => p1.Name ?? string.Empty, emily);
            Assert.IsTrue(expr1.Equals(expr2));
            Assert.IsFalse(expr1.Equals(expr3));
            Assert.IsFalse(expr1.Equals(expr4));
        }
Exemple #20
0
        public void ArgumentChangePropagation()
        {
            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.AreEqual(9, expr.Value);
            emily.Name = "Arya";
            Assert.AreEqual(8, expr.Value);
        }
Exemple #21
0
        public void Inequality()
        {
            var john  = TestPerson.CreateJohn();
            var emily = TestPerson.CreateEmily();

            using var expr1 = ActiveExpression.Create(p1 => p1.Name != null && p1.Name.Length > 0, john);
            using var expr2 = ActiveExpression.Create(p1 => p1.Name != null && p1.Name.Length > 0, john);
            using var expr3 = ActiveExpression.Create(p1 => p1.Name == null || p1.Name.Length == 0, john);
            using var expr4 = ActiveExpression.Create(p1 => p1.Name != null && p1.Name.Length > 0, emily);
            Assert.IsFalse(expr1 != expr2);
            Assert.IsTrue(expr1 != expr3);
            Assert.IsTrue(expr1 != expr4);
        }
Exemple #22
0
        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  = string.Empty;
            Assert.IsNotNull(expr.Fault);
            emily.Name = "Emily";
            Assert.IsNull(expr.Fault);
        }
Exemple #23
0
        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);
        }
Exemple #24
0
        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);
        }
Exemple #25
0
        public void FaultPropagationIfTrue()
        {
            var john = TestPerson.CreateJohn();

            john.Name = null;
            var emily = TestPerson.CreateEmily();

            emily.Name     = null;
            using var expr = ActiveExpression.Create((p1, p2) => p2.Name == null ? p1.Name !.Length : p2.Name.Length, john, emily);
            Assert.IsNotNull(expr.Fault);
            emily.Name = "Emily";
            Assert.IsNull(expr.Fault);
            emily.Name = null;
            Assert.IsNotNull(expr.Fault);
            john.Name = "John";
            Assert.IsNull(expr.Fault);
        }
Exemple #26
0
        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));
        }
Exemple #27
0
        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));
Exemple #28
0
        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 ! });
Exemple #30
0
        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() }));