public void MultipleWaitAsync_AfterSet_OnlyOneIsCompleted()
        {
            AsyncContext.Run(async() =>
            {
                var are = new AsyncAutoResetEvent();

                are.Set();
                var task1 = are.WaitAsync();
                var task2 = are.WaitAsync();

                Assert.IsTrue(task1.IsCompleted);
                await AssertEx.NeverCompletesAsync(task2);
            });
        }
        public void ConditionalAccess_ArgumentChecking()
        {
            var rec = Expression.Constant(1, typeof(int?));
            var nrc = CSharpExpression.ConditionalReceiver(typeof(int));
            var wnn = Expression.Constant(1);

            // null
            AssertEx.Throws <ArgumentNullException>(() => CSharpExpression.ConditionalAccess(default(Expression), nrc, wnn));
            AssertEx.Throws <ArgumentNullException>(() => CSharpExpression.ConditionalAccess(rec, default(ConditionalReceiver), wnn));
            AssertEx.Throws <ArgumentNullException>(() => CSharpExpression.ConditionalAccess(rec, nrc, default(Expression)));

            // invalid receiver type
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ConditionalAccess(Expression.Empty(), nrc, wnn));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ConditionalAccess(Expression.Default(typeof(int)), nrc, wnn));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ConditionalAccess(Expression.Default(typeof(int).MakeByRefType()), nrc, wnn));

            // type mismatch receiver
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ConditionalAccess(Expression.Default(typeof(long?)), nrc, wnn));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ConditionalAccess(Expression.Default(typeof(string)), nrc, wnn));
        }
Exemplo n.º 3
0
        public void ForEach_Factory_ArgumentChecking()
        {
            var variable      = Expression.Parameter(typeof(int));
            var collection    = Expression.Constant(new int[] { 2, 3, 5 });
            var body          = Expression.Empty();
            var breakLabel    = Expression.Label();
            var continueLabel = Expression.Label();

            // null
            AssertEx.Throws <ArgumentNullException>(() => CSharpExpression.ForEach(default(ParameterExpression), collection, body));
            AssertEx.Throws <ArgumentNullException>(() => CSharpExpression.ForEach(variable, default(Expression), body));
            AssertEx.Throws <ArgumentNullException>(() => CSharpExpression.ForEach(variable, collection, default(Expression)));

            // labels must be void
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ForEach(variable, collection, body, Expression.Label(typeof(int))));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ForEach(variable, collection, body, breakLabel, Expression.Label(typeof(int))));

            // conversion checks
            var p1_int32  = Expression.Parameter(typeof(int));
            var p2_int32  = Expression.Parameter(typeof(int));
            var p3_string = Expression.Parameter(typeof(string));
            var conv1     = Expression.Lambda(Expression.Default(typeof(int)), p1_int32, p2_int32);
            var conv2     = Expression.Lambda(Expression.Default(typeof(int)), p3_string);
            var conv3     = Expression.Lambda(Expression.Default(typeof(string)), p1_int32);

            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ForEach(variable, collection, body, breakLabel, continueLabel, conv1));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ForEach(variable, collection, body, breakLabel, continueLabel, conv2));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ForEach(variable, collection, body, breakLabel, continueLabel, conv3));

            // pattern mismatch
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ForEach(variable, Expression.Default(typeof(int)), body));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ForEach(variable, Expression.Default(typeof(Enumerable <NoPattern1>)), body));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ForEach(variable, Expression.Default(typeof(Enumerable <NoPattern2>)), body));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ForEach(variable, Expression.Default(typeof(Enumerable <NoPattern3>)), body));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ForEach(variable, Expression.Default(typeof(Enumerable <NoPattern4>)), body));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ForEach(variable, Expression.Default(typeof(Enumerable <NoPattern5>)), body));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ForEach(variable, Expression.Default(typeof(MoreThanOneEnumerable)), body));
        }