public static IAssertionResult <T, TSource> Assertion <T, TSource>(IValueProvider <T, TSource> valueProvider, IAssertionConfiguration <T> configuration) { var stopwatch = Stopwatch.StartNew(); while (true) { try { var actualValue = valueProvider.GetValue(); configuration.Assertion.Assert(actualValue); return(new AssertionResult <T, TSource>(valueProvider)); } catch (Exception exception) { var isAssertionException = AssertionExceptionHelper.IsAssertionException(exception); if (configuration.Timeout > 0 && configuration.Timeout > stopwatch.ElapsedMilliseconds) { if (isAssertionException || configuration.ExceptionMatcher.RetryOnException(exception)) { Thread.Sleep(configuration.Interval); continue; } } if (!isAssertionException) { throw; } var message = $"[timeout: {configuration.Timeout}]\n{valueProvider.GetMessage()}\n\n{exception.Message}"; throw AssertionExceptionHelper.CreateException(message); } } }
private static void AssertLengthGreaterOrEqual <T>(T[] array, int expected) { if (array.Length < expected) { throw AssertionExceptionHelper.CreateException($"Expected array length greater then or equal to {expected} but was {array.Length}"); } }
internal static void AssertStartsWith(this string actual, string expected, string message = null) { if (actual != null && expected != null && actual.StartsWith(expected)) { return; } var stringBuilder = new StringBuilder(); if (!string.IsNullOrEmpty(message)) { stringBuilder.AppendLine(message); } stringBuilder.AppendLine($"Expected starts with: {expected}"); stringBuilder.AppendLine($"But was: {actual}"); throw AssertionExceptionHelper.CreateException(stringBuilder.ToString()); }
internal static void AssertGreaterThan(this int actual, int expected, string message = null) { if (actual > expected) { return; } var stringBuilder = new StringBuilder(); if (!string.IsNullOrEmpty(message)) { stringBuilder.AppendLine(message); } stringBuilder.AppendLine($"Expected: {expected}"); stringBuilder.AppendLine($"But was: {actual}"); throw AssertionExceptionHelper.CreateException(stringBuilder.ToString()); }