Esempio n. 1
0
        private QueryValue VisitSingleExpression(LinqQueryMethodWithLambdaExpression expression, bool isOrDefault)
        {
            var source = this.EvaluateCollection(expression.Source);

            QueryCollectionValue collection = source;

            if (expression.Lambda != null)
            {
                collection = source.Where(v => this.EvaluateLambda <QueryScalarValue>(expression.Lambda, v));
            }

            if (collection.Elements.Count == 0)
            {
                if (isOrDefault)
                {
                    return(collection.Type.ElementType.NullValue);
                }
                else
                {
                    var expectedException = new ExpectedExceptions(new ExpectedExceptionTypeMessageVerifier <InvalidOperationException>(null, "Sequence contains no elements"));
                    return(source.Type.ElementType.CreateErrorValue(expectedException));
                }
            }
            else if (collection.Elements.Count == 1)
            {
                return(collection.Elements[0]);
            }
            else
            {
                var expectedException = new ExpectedExceptions(new ExpectedExceptionTypeMessageVerifier <InvalidOperationException>(null, "Sequence contains more than one element"));
                return(collection.Type.ElementType.CreateErrorValue(expectedException));
            }
        }
Esempio n. 2
0
        private QueryValue VisitFirstExpression(LinqQueryMethodWithLambdaExpression expression, bool isOrDefault)
        {
            var source = this.EvaluateCollection(expression.Source);

            IEnumerable <QueryValue> elements = source.Elements;

            if (expression.Lambda != null)
            {
                // Note: cannot directly use souce.Where(...) since underlying store semantics might change order!
                elements = source.Elements.Where(v =>
                {
                    QueryScalarValue predicate = this.EvaluateLambda <QueryScalarValue>(expression.Lambda, v);
                    return(!predicate.IsNull && (bool)predicate.Value);
                }).ToArray();
            }

            if (elements.Count() == 0)
            {
                if (isOrDefault)
                {
                    return(source.Type.ElementType.NullValue);
                }
                else
                {
                    var expectedException = new ExpectedExceptions(new ExpectedExceptionTypeMessageVerifier <InvalidOperationException>(null, "Sequence contains no elements"));
                    return(source.Type.ElementType.CreateErrorValue(expectedException));
                }
            }
            else
            {
                return(elements.First());
            }
        }
Esempio n. 3
0
        public async Task <bool> VerifyExistsOnDiskAsync(bool useDefaultPrompt = true)
        {
            var exists = IO.FileOrDirectoryExists(this.Files.First());

            if (useDefaultPrompt && !exists)
            {
                await ExpectedExceptions.FileNotFoundAsync(this.RootPath, "This subitem was not found on disk.");
            }

            return(exists);
        }
Esempio n. 4
0
        protected ComparisonResult CompareError(ExpectedExceptions expectedExceptions, Func <object> queryEvaluationFunc)
        {
            Exception exception = null;

            try
            {
                object result     = queryEvaluationFunc();
                var    enumerable = result as IEnumerable;
                if (enumerable != null)
                {
                    foreach (var obj in enumerable)
                    {
                    }
                }
            }
            catch (Exception ex)
            {
                exception = ex;
            }

            return(ExpectedExceptionsComparer.CompareException(expectedExceptions, exception));
        }
Esempio n. 5
0
 public void ThenIHaveAException(string message)
 {
     Assert.AreEqual(message, ExpectedExceptions.First().Message);
 }
Esempio n. 6
0
        public void ExecuteProperty <V>(
            Func <V> setInitializer, Action <V> setExecutor, Action <V> setValidator,
            Func <V> getExecutor, Action <V, V> getValidator, [CallerMemberName] string callerName = "")
        {
            if (DisabledTests.Contains(callerName))
            {
                if (PassDisabledTests)
                {
                    Console.WriteLine("Test is on the Disabled Lists & Pass Disabled Tests is TRUE");
                    return;
                }
                if (!RunDisabledTests)
                {
                    throw new AssertInconclusiveException("Test is on the Disabled List");
                }
            }

            V setValue;
            V getValue;

            do
            {
                ++DataSequence;
                if (setExecutor != null)
                {
                    try
                    {
                        setValue = setInitializer();
                    }
                    catch (Exception ex)
                    {
                        throw new AssertInconclusiveException("Exception during Set Value Initialization", ex);
                    }

                    Type expected;
                    ExpectedExceptions.TryGetValue(callerName, out expected);
                    try
                    {
                        TrackObjectGraph(null, () =>
                        {
                            setExecutor(setValue);
                            if (expected != null)
                            {
                                throw new Exception(String.Format("Expected {0} was not thrown", expected.Name));
                            }
                        });
                    }
                    catch (ToBeImplementedException ex)
                    {
                        throw new AssertInconclusiveException("Target Set Accessor is yet to be implemented!", ex);
                    }
                    catch (Exception ex)
                    {
                        bool wasExpected = (expected != null) && (expected == ex.GetType());
                        if (!wasExpected)
                        {
                            throw;
                        }
                    }
                    try
                    {
                        setValidator(setValue);
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
                else
                {
                    setValue = default(V);
                }
                if (getExecutor != null)
                {
                    try
                    {
                        getValue = getExecutor();
                    }
                    catch (ToBeImplementedException ex)
                    {
                        throw new AssertInconclusiveException("Target Get Accessor is yet to be implemented!", ex);
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    try
                    {
                        getValidator(setValue, getValue);
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            } while (MoreData);
        }
Esempio n. 7
0
        /// <summary>
        ///     Executes the property.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="V"></typeparam>
        /// <param name="instanceCreator">The instance creator.</param>
        /// <param name="setInitializer">The set initializer.</param>
        /// <param name="setExecutor">The set executor.</param>
        /// <param name="setValidator">The set validator.</param>
        /// <param name="getExecutor">The get executor.</param>
        /// <param name="getValidator">The get validator.</param>
        /// <param name="callerName">Name of the caller.</param>
        /// <exception cref="Microsoft.VisualStudio.TestTools.UnitTesting.AssertInconclusiveException">
        ///     Test is on the Disabled List
        ///     or
        ///     Instance Creator returned Null!!
        ///     or
        ///     Unable to Aquire Trarget Instance
        ///     or
        ///     Exception during Set Value Initialization
        ///     or
        ///     Target Set Accessor is yet to be implemented!
        ///     or
        ///     Target Get Accessor is yet to be implemented!
        /// </exception>
        /// <exception cref="Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException">Unable to Aquire Trarget Instance</exception>
        public void ExecuteProperty <T, V>(Func <T> instanceCreator,
                                           Func <T, V> setInitializer, Action <T, V> setExecutor, Action <T, V> setValidator,
                                           Func <T, V> getExecutor, Action <T, V, V> getValidator, [CallerMemberName] string callerName = "")
        {
            if (DisabledTests.Contains(callerName))
            {
                if (PassDisabledTests)
                {
                    Console.WriteLine("Test is on the Disabled Lists & Pass Disabled Tests is TRUE");
                    return;
                }
                if (!RunDisabledTests)
                {
                    throw new AssertInconclusiveException("Test is on the Disabled List");
                }
            }

            T instance;
            V setValue;
            V getValue;

            do
            {
                ++DataSequence;
                try
                {
                    instance = instanceCreator();
                    if ((typeof(T).IsClass || typeof(T).IsInterface) & instance == null)
                    {
                        if (!AllowNullGetInstance)
                        {
                            throw new AssertInconclusiveException("Instance Creator returned Null!!");
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (PassOnTargetAquisitionError)
                    {
                        return;
                    }
                    if (SkipOnTargetAquisitionError)
                    {
                        throw new AssertInconclusiveException("Unable to Aquire Trarget Instance", ex);
                    }
                    throw new AssertFailedException("Unable to Aquire Trarget Instance", ex);
                }
                if (setExecutor != null)
                {
                    try
                    {
                        setValue = setInitializer(instance);
                    }
                    catch (Exception ex)
                    {
                        throw new AssertInconclusiveException("Exception during Set Value Initialization", ex);
                    }

                    Type expected;
                    ExpectedExceptions.TryGetValue(callerName, out expected);
                    try
                    {
                        TrackObjectGraph(instance, () =>
                        {
                            setExecutor(instance, setValue);
                            if (expected != null)
                            {
                                throw new Exception(String.Format("Expected {0} was not thrown", expected.Name));
                            }
                        });
                    }
                    catch (ToBeImplementedException ex)
                    {
                        if (PassToBeImplementedTests)
                        {
                            return;
                        }
                        string yetToBeImplementedMessage = "Target Set Accessor is yet to be implemented!";
                        if (UseFluentMessages)
                        {
                            string className  = instance.GetType().Name;
                            object methodName = callerName.Replace("_UnitTest", "");
                            yetToBeImplementedMessage = String.Format("{0}.{1} Set Accessor is yet to be implemented!", className, methodName);
                        }
                        if (FailToBeImplementedTests)
                        {
                            throw new AssertFailedException(yetToBeImplementedMessage, ex);
                        }
                        throw new AssertInconclusiveException(yetToBeImplementedMessage, ex);
                    }
                    catch (Exception ex)
                    {
                        bool wasExpected = (expected != null) && (expected == ex.GetType());
                        if (!wasExpected)
                        {
                            throw;
                        }
                    }
                    try
                    {
                        setValidator(instance, setValue);
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
                else
                {
                    setValue = default(V);
                }
                if (getExecutor != null)
                {
                    try
                    {
                        getValue = getExecutor(instance);
                    }
                    catch (ToBeImplementedException ex)
                    {
                        if (PassToBeImplementedTests)
                        {
                            return;
                        }
                        string yetToBeImplementedMessage = "Target Get Accessor is yet to be implemented!";
                        if (UseFluentMessages)
                        {
                            string className  = instance.GetType().Name;
                            object methodName = callerName.Replace("_UnitTest", "");
                            yetToBeImplementedMessage = String.Format("{0}.{1} Get Accessor is yet to be implemented!", className, methodName);
                        }
                        if (FailToBeImplementedTests)
                        {
                            throw new AssertFailedException(yetToBeImplementedMessage, ex);
                        }
                        throw new AssertInconclusiveException(yetToBeImplementedMessage, ex);
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    try
                    {
                        getValidator(instance, setValue, getValue);
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            } while (MoreData);
        }
Esempio n. 8
0
        /// <summary>
        ///     Executes the method.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="instanceCreator">The instance creator.</param>
        /// <param name="initializer">The initializer.</param>
        /// <param name="executor">The executor.</param>
        /// <param name="validator">The validator.</param>
        /// <param name="callerName">Name of the caller.</param>
        /// <exception cref="Microsoft.VisualStudio.TestTools.UnitTesting.AssertInconclusiveException">
        ///     Test is on the Disabled List
        ///     or
        ///     Instance Creator returned Null!!
        ///     or
        ///     Unable to Aquire Trarget Instance
        ///     or
        ///     Exception during Parameter Value Initialization
        ///     or
        ///     Target Method is yet to be implemented!
        /// </exception>
        /// <exception cref="Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException">Unable to Aquire Trarget Instance</exception>
        public void ExecuteMethod <T>(Func <T> instanceCreator, Action <T> initializer, Action <T> executor, Action <T> validator, [CallerMemberName] string callerName = "")
        {
            var count = Interlocked.Increment(ref m_Concurrent);

            if (count > 1)
            {
                throw new Exception();
            }
            if (DisabledTests.Contains(callerName))
            {
                if (PassDisabledTests)
                {
                    Console.WriteLine("Test is on the Disabled Lists & Pass Disabled Tests is TRUE");
                    return;
                }
                if (!RunDisabledTests)
                {
                    throw new AssertInconclusiveException("Test is on the Disabled List");
                }
            }
            do
            {
                T instance = default(T);
                ++DataSequence;
                try
                {
                    instance = instanceCreator();
                    if ((typeof(T).IsClass || typeof(T).IsInterface) & instance == null)
                    {
                        if (!AllowNullGetInstance)
                        {
                            throw new AssertInconclusiveException("Instance Creator returned Null!!");
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (PassOnTargetAquisitionError)
                    {
                        return;
                    }
                    if (SkipOnTargetAquisitionError)
                    {
                        throw new AssertInconclusiveException("Unable to Aquire Trarget Instance", ex);
                    }
                    throw new AssertFailedException("Unable to Aquire Trarget Instance", ex);
                }
                try
                {
                    initializer(instance);
                }
                catch (Exception ex)
                {
                    throw new AssertInconclusiveException("Exception during Parameter Value Initialization", ex);
                }
                try
                {
                    try
                    {
                        TrackObjectGraph(instance, () => executor(instance));
                    }
                    catch (AggregateException ex)
                    {
                        if (ex.InnerExceptions.Count == 1)
                        {
                            Exception realEx = ex.InnerException;
                            throw realEx;
                        }
                        throw;
                    }
                }
                catch (ToBeImplementedException ex)
                {
                    if (PassToBeImplementedTests)
                    {
                        return;
                    }
                    string yetToBeImplementedMessage = "Target Method is yet to be implemented!";
                    if (UseFluentMessages)
                    {
                        string className  = instance.GetType().Name;
                        object methodName = callerName.Replace("_UnitTest", "");
                        yetToBeImplementedMessage = String.Format("{0}.{1} is yet to be implemented!", className, methodName);
                    }
                    if (FailToBeImplementedTests)
                    {
                        throw new AssertFailedException(yetToBeImplementedMessage, ex);
                    }
                    throw new AssertInconclusiveException(yetToBeImplementedMessage, ex);
                }
                catch (Exception ex)
                {
                    bool wasExpected = false;
                    Type expected;
                    if (ExpectedExceptions.TryGetValue(callerName, out expected))
                    {
                        if (expected == ex.GetType())
                        {
                            wasExpected = true;
                        }
                    }
                    if (!wasExpected)
                    {
                        throw;
                    }
                }
                try
                {
                    validator(instance);
                }

                catch (Exception)
                {
                    throw;
                }
            } while (MoreData);
        }
Esempio n. 9
0
 /// <summary>
 ///     Executes the method.
 /// </summary>
 /// <param name="initializer">The initializer.</param>
 /// <param name="executor">The executor.</param>
 /// <param name="validator">The validator.</param>
 /// <param name="callerName">Name of the caller.</param>
 /// <exception cref="Microsoft.VisualStudio.TestTools.UnitTesting.AssertInconclusiveException">
 ///     Test is on the Disabled List
 ///     or
 ///     Exception during Parameter Value Initialization
 ///     or
 ///     Target Method is yet to be implemented!
 /// </exception>
 public void ExecuteMethod(Action initializer, Action executor, Action validator, [CallerMemberName] string callerName = "")
 {
     if (DisabledTests.Contains(callerName))
     {
         if (PassDisabledTests)
         {
             Console.WriteLine("Test is on the Disabled Lists & Pass Disabled Tests is TRUE");
             return;
         }
         if (!RunDisabledTests)
         {
             throw new AssertInconclusiveException("Test is on the Disabled List");
         }
     }
     do
     {
         ++DataSequence;
         try
         {
             initializer();
         }
         catch (Exception ex)
         {
             throw new AssertInconclusiveException("Exception during Parameter Value Initialization", ex);
         }
         try
         {
             try
             {
                 executor();
             }
             catch (AggregateException ex)
             {
                 if (ex.InnerExceptions.Count == 1)
                 {
                     Exception realEx = ex.InnerException;
                     throw realEx;
                 }
                 throw;
             }
         }
         catch (ToBeImplementedException ex)
         {
             if (PassToBeImplementedTests)
             {
                 return;
             }
             string yetToBeImplementedMessage = "Target Method is yet to be implemented!";
             if (UseFluentMessages)
             {
                 string className  = this.GetType().Name.Replace("_UnitTests", "");
                 object methodName = callerName.Replace("_UnitTest", "");
                 yetToBeImplementedMessage = String.Format("{0}.{1} is yet to be implemented!", className, methodName);
             }
             if (FailToBeImplementedTests)
             {
                 throw new AssertFailedException(yetToBeImplementedMessage, ex);
             }
             throw new AssertInconclusiveException(yetToBeImplementedMessage, ex);
         }
         catch (Exception ex)
         {
             bool wasExpected = false;
             Type expected;
             if (ExpectedExceptions.TryGetValue(callerName, out expected))
             {
                 if (expected == ex.GetType())
                 {
                     wasExpected = true;
                 }
             }
             if (!wasExpected)
             {
                 throw;
             }
         }
         try
         {
             validator();
         }
         catch (Exception)
         {
             throw;
         }
     } while (MoreData);
 }