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); }
/// <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); }
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); } }
public void Should_notify_method_exception() { var exception = new SingleException(33); var method = testClass.AddFailingTest("TestMethod1", exception); Run(); Messages.AssertSameTask(method.Task).TaskException(exception); }
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); }
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); }
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); }
public void Should_fail_class_if_any_methods_fail() { var exception = new SingleException(23); testClass.AddFailingTest("TestMethod1", exception); Run(); Messages.OfTask(testClass.ClassTask).AssertTaskFinishedWithFailingChildren(); }
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) }); }
/// <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); }