Exemple #1
0
        public void ArgumentChanges()
        {
            var reversedNumbersList = Enumerable.Range(1, 10).Reverse().ToImmutableList();
            var john   = TestPerson.CreateJohn();
            var values = new BlockingCollection <int>();

            using (var expr = ActiveExpression.Create((p1, p2) => p1[p2.Name !.Length], reversedNumbersList, john))
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());
        }
Exemple #3
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 #4
0
        public void FaultShortCircuiting()
        {
            var        john  = TestPerson.CreateJohn();
            TestPerson?noOne = null;

            using var expr = ActiveExpression.Create((p1, p2) => !string.IsNullOrEmpty(p1.Name) || !string.IsNullOrEmpty(p2 !.Name), john, noOne);
            Assert.IsTrue(expr.Value);
            Assert.IsNull(expr.Fault);
        }
        public void EvaluationFault()
        {
            var        john  = TestPerson.CreateJohn();
            TestPerson?noOne = null;

#pragma warning disable CS8604 // Possible null reference argument.
            using var expr = ActiveExpression.Create(() => john + noOne);
#pragma warning restore CS8604 // Possible null reference argument.
            Assert.IsNotNull(expr.Fault);
        }
Exemple #6
0
        public void FaultShortCircuiting()
        {
            var john = TestPerson.CreateJohn();

#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
            using var expr = ActiveExpression.Create <TestPerson, TestPerson, string>((p1, p2) => p1.Name !.Length > 0 ? p1.Name ! : p2.Name !, john, null);
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
            Assert.AreEqual(john.Name, expr.Value);
            Assert.IsNull(expr.Fault);
        }
Exemple #7
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 #8
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());
        }
        public void NullableConversion()
        {
            var john = TestPerson.CreateJohn();

            using var expr = ActiveExpression.Create(p1 => (p1 == null || p1.Name == null ? (int?)null : p1.Name.Length) + 3, john);
            Assert.IsTrue(expr.Value == 7);
            Assert.IsNull(expr.Fault);
            john.Name = null;
            Assert.IsNull(expr.Value);
            Assert.IsNull(expr.Fault);
        }
        public void FaultPropagation()
        {
            var john = TestPerson.CreateJohn();

            using var expr = ActiveExpression.Create(p1 => - p1.Name !.Length, john);
            Assert.IsNull(expr.Fault);
            john.Name = null;
            Assert.IsNotNull(expr.Fault);
            john.Name = "John";
            Assert.IsNull(expr.Fault);
        }
Exemple #11
0
        public void ArgumentFaultPropagation()
        {
            var john = TestPerson.CreateJohn();

            using var expr = ActiveExpression.Create(() => new TestPerson(john.Name !.Length.ToString()));
            Assert.IsNull(expr.Fault);
            john.Name = null;
            Assert.IsNotNull(expr.Fault);
            john.Name = "John";
            Assert.IsNull(expr.Fault);
        }
Exemple #12
0
        public void FaultShortCircuiting()
        {
            var        john  = TestPerson.CreateJohn();
            TestPerson?noOne = null;

#pragma warning disable CS8602 // Dereference of a possibly null reference.
            using var expr = ActiveExpression.Create((p1, p2) => string.IsNullOrEmpty(p1.Name) && string.IsNullOrEmpty(p2.Name), john, noOne);
#pragma warning restore CS8602 // Dereference of a possibly null reference.
            Assert.IsFalse(expr.Value);
            Assert.IsNull(expr.Fault);
        }
Exemple #13
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 #14
0
        public void ConsistentHashCode()
        {
            int hashCode1, hashCode2;
            var john = TestPerson.CreateJohn();

            using (var expr = ActiveExpression.Create(p1 => p1.Name != null && p1.Name.Length > 0, john))
                hashCode1 = expr.GetHashCode();
            using (var expr = ActiveExpression.Create(p1 => p1.Name != null && p1.Name.Length > 0, john))
                hashCode2 = expr.GetHashCode();
            Assert.IsTrue(hashCode1 == hashCode2);
        }
Exemple #15
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 #16
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 #17
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 #18
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 #19
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);
        }
        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 #21
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 #22
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));
        }
Exemple #23
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);
        }
        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);
        }
Exemple #25
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 #26
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 #27
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 #28
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 #29
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 #30
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);
        }