Beispiel #1
0
 public void Cast()
 {
     using var expr = ActiveExpression.Create(p1 => (double)p1, 3);
     Assert.IsNull(expr.Fault);
     Assert.AreEqual(3D, expr.Value);
     Assert.IsInstanceOfType(expr.Value, typeof(double));
 }
Beispiel #2
0
        public void DateTimeStringConversion()
        {
            var now = DateTime.UtcNow;

            using (var expr = ActiveExpression.Create(p1 => p1, now))
                Assert.AreEqual($"{{C}} /* new System.DateTime({now.Ticks}, System.DateTimeKind.Utc) */", expr.ToString());
        }
Beispiel #3
0
        public void FaultedStringConversion()
        {
            TestPerson noOne = null;

            using (var expr = ActiveExpression.Create(p1 => p1.Name, noOne))
                Assert.AreEqual($"{{C}} /* null */.Name /* [{typeof(NullReferenceException).Name}: {new NullReferenceException().Message}] */", expr.ToString());
        }
Beispiel #4
0
        public void EvaluationFault()
        {
            TestPerson noOne = null;

            using (var expr = ActiveExpression.Create(() => - noOne))
                Assert.IsNotNull(expr.Fault);
        }
Beispiel #5
0
        public void CollectionChanges()
        {
            var numbers = new RangeObservableCollection <int>(Enumerable.Range(1, 10));
            var values  = new BlockingCollection <int>();

            using (var expr = ActiveExpression.Create(p1 => p1[5], numbers))
            {
                var disconnect = expr.OnPropertyChanged(ae => ae.Value, value => values.Add(value));
                values.Add(expr.Value);
                numbers.Add(11);
                numbers.Insert(0, 0);
                numbers.Remove(11);
                numbers.Remove(0);
                numbers[4] = 50;
                numbers[4] = 5;
                numbers[5] = 60;
                numbers[5] = 6;
                numbers[6] = 70;
                numbers[6] = 7;
                numbers.Move(0, 1);
                numbers.Move(0, 1);
                numbers.MoveRange(0, 5, 5);
                numbers.MoveRange(0, 5, 5);
                numbers.MoveRange(5, 0, 5);
                numbers.MoveRange(5, 0, 5);
                numbers.Reset(numbers.Select(i => i * 10).ToImmutableArray());
                disconnect();
            }
            Assert.IsTrue(new int[] { 6, 5, 6, 60, 6, 1, 6, 1, 6, 60 }.SequenceEqual(values));
        }
Beispiel #6
0
        public void DictionaryChanges()
        {
            var perfectNumbers = new ObservableDictionary <int, int>(Enumerable.Range(1, 10).ToDictionary(i => i, i => i * i));
            var values         = new BlockingCollection <int>();

            using (var expr = ActiveExpression.Create(p1 => p1[5], perfectNumbers))
            {
                var disconnect = expr.OnPropertyChanged(ae => ae.Value, value =>
                {
                    values.Add(value);
                });
                values.Add(expr.Value);
                perfectNumbers.Add(11, 11 * 11);
                perfectNumbers.AddRange(Enumerable.Range(12, 3).ToDictionary(i => i, i => i * i));
                perfectNumbers.Remove(11);
                perfectNumbers.RemoveRange(Enumerable.Range(12, 3));
                perfectNumbers.Remove(5);
                perfectNumbers.Add(5, 30);
                perfectNumbers[5] = 25;
                perfectNumbers.RemoveRange(Enumerable.Range(4, 3));
                perfectNumbers.AddRange(Enumerable.Range(4, 3).ToDictionary(i => i, i => i * i));
                perfectNumbers.Clear();
                disconnect();
            }
            Assert.IsTrue(new int[] { 25, 0, 30, 25, 0, 25, 0 }.SequenceEqual(values));
        }
Beispiel #7
0
        public void TimeSpanStringConversion()
        {
            var threeMinutes = TimeSpan.FromMinutes(3);

            using (var expr = ActiveExpression.Create(p1 => p1, threeMinutes))
                Assert.AreEqual($"{{C}} /* new System.TimeSpan({threeMinutes.Ticks}) */", expr.ToString());
        }
Beispiel #8
0
        public void CharStringConversion()
        {
            var person = new TestPerson("\\");

            using (var expr = ActiveExpression.Create(p1 => p1.Name[0], person))
            {
                Assert.AreEqual("{C} /* {\\} */.Name /* \"\\\\\" */[{C} /* 0 */] /* '\\\\' */", expr.ToString());
                person.Name = "\0";
                Assert.AreEqual("{C} /* {\0} */.Name /* \"\\0\" */[{C} /* 0 */] /* '\\0' */", expr.ToString());
                person.Name = "\a";
                Assert.AreEqual("{C} /* {\a} */.Name /* \"\\a\" */[{C} /* 0 */] /* '\\a' */", expr.ToString());
                person.Name = "\b";
                Assert.AreEqual("{C} /* {\b} */.Name /* \"\\b\" */[{C} /* 0 */] /* '\\b' */", expr.ToString());
                person.Name = "\f";
                Assert.AreEqual("{C} /* {\f} */.Name /* \"\\f\" */[{C} /* 0 */] /* '\\f' */", expr.ToString());
                person.Name = "\n";
                Assert.AreEqual("{C} /* {\n} */.Name /* \"\\n\" */[{C} /* 0 */] /* '\\n' */", expr.ToString());
                person.Name = "\r";
                Assert.AreEqual("{C} /* {\r} */.Name /* \"\\r\" */[{C} /* 0 */] /* '\\r' */", expr.ToString());
                person.Name = "\t";
                Assert.AreEqual("{C} /* {\t} */.Name /* \"\\t\" */[{C} /* 0 */] /* '\\t' */", expr.ToString());
                person.Name = "\v";
                Assert.AreEqual("{C} /* {\v} */.Name /* \"\\v\" */[{C} /* 0 */] /* '\\v' */", expr.ToString());
                person.Name = "x";
                Assert.AreEqual("{C} /* {x} */.Name /* \"x\" */[{C} /* 0 */] /* 'x' */", expr.ToString());
            }
        }
Beispiel #9
0
        public void GuidStringConversion()
        {
            var guid = Guid.NewGuid();

            using (var expr = ActiveExpression.Create(p1 => p1, guid))
                Assert.AreEqual($"{{C}} /* new System.Guid(\"{guid}\") */", expr.ToString());
        }
Beispiel #10
0
        public async Task ValueAsyncDisposalAsync()
        {
            var people = new ObservableCollection <AsyncDisposableTestPerson>
            {
                AsyncDisposableTestPerson.CreateJohn(),
                AsyncDisposableTestPerson.CreateEmily()
            };
            var disposedTcs = new TaskCompletionSource <object?>();
            AsyncDisposableTestPerson?newPerson;

            using (var expr = ActiveExpression.Create(p => p[0] + p[1], people))
            {
                newPerson = expr.Value;
                Assert.IsFalse(newPerson !.IsDisposed);
                newPerson.Disposed += (sender, e) => disposedTcs.SetResult(null);
                people[0]           = AsyncDisposableTestPerson.CreateJohn();
                await Task.WhenAny(disposedTcs.Task, Task.Delay(TimeSpan.FromSeconds(1)));

                Assert.IsTrue(newPerson.IsDisposed);
                newPerson = expr.Value;
                Assert.IsFalse(newPerson !.IsDisposed);
                disposedTcs         = new TaskCompletionSource <object?>();
                newPerson.Disposed += (sender, e) => disposedTcs.SetResult(null);
            }
            await Task.WhenAny(disposedTcs.Task, Task.Delay(TimeSpan.FromSeconds(1)));

            Assert.IsTrue(newPerson.IsDisposed);
        }
Beispiel #11
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());
        }
Beispiel #12
0
        public void StringConversion()
        {
            var emily = TestPerson.CreateEmily();

            emily.Name = "X";
            using (var expr = ActiveExpression.Create(p1 => p1.Name.Length + 1, emily))
                Assert.AreEqual("({C} /* {X} */.Name /* \"X\" */.Length /* 1 */ + {C} /* 1 */) /* 2 */", expr.ToString());
        }
Beispiel #13
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());
        }
Beispiel #14
0
 public void ImplicitConversion()
 {
     using (var expr = ActiveExpression.Create(() => new A() ?? new B()))
     {
         Assert.IsNull(expr.Fault);
         Assert.IsNull(expr.Value);
     }
 }
Beispiel #15
0
 public void CreateFromLambda()
 {
     using (var expr = ActiveExpression.Create <int>(Expression.Lambda(Expression.Negate(Expression.Constant(3)))))
     {
         Assert.IsNull(expr.Fault);
         Assert.AreEqual(-3, expr.Value);
     }
 }
Beispiel #16
0
        public void OptimizerAppliedDeMorgan()
        {
            var a = Expression.Parameter(typeof(bool));
            var b = Expression.Parameter(typeof(bool));

            using (var expr = ActiveExpression.Create <bool>(Expression.Lambda <Func <bool, bool, bool> >(Expression.AndAlso(Expression.Not(a), Expression.Not(b)), a, b), false, false))
                Assert.AreEqual("(!({C} /* False */ || {C} /* False */) /* False */) /* True */", expr.ToString());
        }
Beispiel #17
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.IsTrue(expr.Value);
            Assert.AreEqual(0, emily.NameGets);
        }
Beispiel #18
0
        public void ThreeArgumentStringConversion()
        {
            var john    = TestPerson.CreateJohn();
            var emily   = TestPerson.CreateEmily();
            var charles = new TestPerson("Charles");

            using (var expr = ActiveExpression.Create((a, b, c) => a + b + c, john, emily, charles))
                Assert.AreEqual("(({C} /* {John} */ + {C} /* {Emily} */) /* {John Emily} */ + {C} /* {Charles} */) /* {John Emily Charles} */", expr.ToString());
        }
Beispiel #19
0
        public void LambdaConsistentHashCode()
        {
            int hashCode1, hashCode2;

            using (var expr = ActiveExpression.Create <int>(Expression.Lambda(Expression.Negate(Expression.Constant(3)))))
                hashCode1 = expr.GetHashCode();
            using (var expr = ActiveExpression.Create <int>(Expression.Lambda(Expression.Negate(Expression.Constant(3)))))
                hashCode2 = expr.GetHashCode();
            Assert.IsTrue(hashCode1 == hashCode2);
        }
        public void FaultShortCircuiting()
        {
            var john = TestPerson.CreateJohn();

            using (var expr = ActiveExpression.Create <TestPerson, TestPerson, string>((p1, p2) => p1.Name.Length > 0 ? p1.Name : p2.Name, john, null))
            {
                Assert.AreEqual(john.Name, expr.Value);
                Assert.IsNull(expr.Fault);
            }
        }
Beispiel #21
0
        public void TwoArgumentConsistentHashCode()
        {
            int hashCode1, hashCode2;

            using (var expr = ActiveExpression.Create((a, b) => a + b, 1, 2))
                hashCode1 = expr.GetHashCode();
            using (var expr = ActiveExpression.Create((a, b) => a + b, 1, 2))
                hashCode2 = expr.GetHashCode();
            Assert.IsTrue(hashCode1 == hashCode2);
        }
Beispiel #22
0
        public void ConsistentHashCode()
        {
            int hashCode1, hashCode2;

            using (var expr = ActiveExpression.Create(() => new TestPerson("Charles")))
                hashCode1 = expr.GetHashCode();
            using (var expr = ActiveExpression.Create(() => new TestPerson("Charles")))
                hashCode2 = expr.GetHashCode();
            Assert.IsTrue(hashCode1 == hashCode2);
        }
Beispiel #23
0
        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);
        }
Beispiel #24
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());
        }
Beispiel #25
0
 public void Inequality()
 {
     using var expr1 = ActiveExpression.Create(() => new TestPerson("Charles"));
     using var expr2 = ActiveExpression.Create(() => new TestPerson("Charles"));
     using var expr3 = ActiveExpression.Create(() => new TestPerson());
     using var expr4 = ActiveExpression.Create(() => new TestPerson("Erin"));
     Assert.IsFalse(expr1 != expr2);
     Assert.IsTrue(expr1 != expr3);
     Assert.IsTrue(expr1 != expr4);
 }
Beispiel #26
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);
        }
Beispiel #27
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);
        }
Beispiel #28
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);
        }
Beispiel #29
0
        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);
        }
Beispiel #30
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);
        }