コード例 #1
0
        private static T GetSingleResult <T>(IEnumerable <T> collection, Predicate <T> predicate, string expectedArgument, out Exception exceptionToThrow)
        {
            int count  = 0;
            T   result = default(T);

            foreach (T item in collection)
            {
                if (predicate == null || predicate(item))
                {
                    if (++count == 1)
                    {
                        result = item;
                    }
                }
            }

            switch (count)
            {
            case 0:
                exceptionToThrow = SingleException.Empty(expectedArgument);
                break;

            case 1:
                exceptionToThrow = null;
                break;

            default:
                exceptionToThrow = SingleException.MoreThanOne(count, expectedArgument);
                break;
            }

            return(result);
        }
コード例 #2
0
        /// <summary>
        /// Verifies that the given collection contains only a single
        /// element of the given type which matches the given predicate. The
        /// collection may or may not contain other values which do not
        /// match the given predicate.
        /// </summary>
        /// <typeparam name="T">The collection type.</typeparam>
        /// <param name="collection">The collection.</param>
        /// <param name="predicate">The item matching predicate.</param>
        /// <returns>The single item in the filtered collection.</returns>
        /// <exception cref="SingleException">Thrown when the filtered collection does
        /// not contain exactly one element.</exception>
        public static T Single <T>(IEnumerable <T> collection, Predicate <T> predicate)
        {
            Assert.GuardArgumentNotNull("collection", collection);
            Assert.GuardArgumentNotNull("predicate", predicate);

            int count  = 0;
            T   result = default(T);

            foreach (T item in collection)
            {
                if (predicate(item))
                {
                    if (++count > 1)
                    {
                        break;
                    }
                    result = item;
                }
            }

            switch (count)
            {
            case 0: throw SingleException.Empty();

            case 1: break;

            default: throw SingleException.MoreThanOne();
            }

            return(result);
        }
コード例 #3
0
        static T GetSingleResult <T>(IEnumerable <T> collection, Predicate <T> predicate, string expectedArgument)
#endif
        {
            var count  = 0;
            T   result = default(T);

            foreach (var item in collection)
            {
                if (predicate == null || predicate(item))
                {
                    if (++count == 1)
                    {
                        result = item;
                    }
                }
            }

            switch (count)
            {
            case 0:
                throw SingleException.Empty(expectedArgument);

            case 1:
#if XUNIT_NULLABLE
                return(result !);
#else
                return(result);
#endif
            default:
                throw SingleException.MoreThanOne(count, expectedArgument);
            }
        }
コード例 #4
0
        public void Should_notify_method_exception()
        {
            var exception = new SingleException(33);
            var method    = testClass.AddFailingTest("TestMethod1", exception);

            Run();

            Messages.AssertSameTask(method.Task).TaskException(exception);
        }
コード例 #5
0
        public void Should_notify_exception_before_method_finished()
        {
            var exception = new SingleException(23);
            var method    = testClass.AddFailingTest("TestMethod1", exception);

            Run();

            Messages.AssertSameTask(method.Task).OrderedActions(ServerAction.TaskStarting, ServerAction.TaskFinished);
        }
コード例 #6
0
        public void Should_notify_method_finished_with_errors()
        {
            var exception = new SingleException(23);
            var method    = testClass.AddFailingTest("TestMethod1", exception);

            Run();

            Messages.AssertSameTask(method.Task).TaskFinished(exception.UserMessage, TaskResult.Exception);
        }
コード例 #7
0
        public void Should_notify_output_for_failing_test()
        {
            const string expectedOutput = "This is some output";
            var          exception      = new SingleException(33);
            var          method         = testClass.AddFailingTest("TestMethod1", exception, expectedOutput);

            Run();

            Messages.AssertSameTask(method.Task).TaskOutput(expectedOutput);
        }
コード例 #8
0
        public void Should_fail_class_if_any_methods_fail()
        {
            var exception = new SingleException(23);

            testClass.AddFailingTest("TestMethod1", exception);

            Run();

            Messages.OfTask(testClass.ClassTask).AssertTaskFinishedWithFailingChildren();
        }
コード例 #9
0
        public void Should_continue_running_tests_after_failing_test_method()
        {
            var exception = new SingleException(33);
            var method1   = testClass.AddFailingTest("TestMethod1", exception);
            var method2   = testClass.AddPassingTest("TestMethod2");

            Run();

            Messages.AssertOrderWithSameTasks(new[]
            {
                TaskMessage.TaskException(method1.Task, exception),
                TaskMessage.TaskStarting(method2.Task)
            });
        }
コード例 #10
0
        /// <summary>
        /// Verifies that the given collection contains only a single
        /// element of the given type which matches the given predicate. The
        /// collection may or may not contain other values which do not
        /// match the given predicate.
        /// </summary>
        /// <typeparam name="T">The collection type.</typeparam>
        /// <param name="collection">The collection.</param>
        /// <param name="predicate">The item matching predicate.</param>
        /// <returns>The single item in the filtered collection.</returns>
        /// <exception cref="SingleException">Thrown when the filtered collection does
        /// not contain exactly one element.</exception>
        public static T Single <T>(IEnumerable <T> collection, Predicate <T> predicate)
        {
            if (collection == null)
            {
                throw new ArgumentNullException(nameof(collection));
            }
            if (predicate == null)
            {
                throw new ArgumentNullException(nameof(predicate));
            }

            int count  = 0;
            T   result = default(T);

            foreach (T item in collection)
            {
                if (predicate(item))
                {
                    if (++count > 1)
                    {
                        break;
                    }
                    result = item;
                }
            }

            switch (count)
            {
            case 0:
                throw SingleException.Empty();

            case 1:
                break;

            default:
                throw SingleException.MoreThanOne();
            }

            return(result);
        }