Example #1
0
        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);
                }
            }
        }
Example #2
0
 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}");
     }
 }
Example #3
0
        public void NeverAssertionFailed(
            [CanBeNull] string description,
            [CanBeNull] TransformExceptionDelegate transformException,
            [CanBeNull] Assembly callerAssembly,
            [CanBeNull] string callerFilePath,
            [CanBeNull] int?callerLineNumber,
            [CanBeNull] string callerMemberName)
        {
            Exception[] _resolveDataErrorExceptions;
            var         _data   = _TryResolveData(_GetDataResolvers, out _resolveDataErrorExceptions);
            var         _record = AssertionRecordHelper.CreateRecord(
                _data,
                description,
                callerAssembly,
                callerFilePath,
                callerLineNumber,
                callerMemberName);

            Exception _reportErrorException;

            _ReportFailure(MustAssertionType.AssertNever, _record, out _reportErrorException);

            throw AssertionExceptionHelper.CreateAssertionException(
                      transformException: transformException,
                      assertionRecord: _record,
                      additionalExceptions: ExceptionHelper.Combine(_resolveDataErrorExceptions, ExceptionHelper.Yield(_reportErrorException)));
        }
Example #4
0
 public bool Satisfied(T value)
 {
     try
     {
         assert(value);
         return(true);
     }
     catch (Exception exception)
     {
         if (AssertionExceptionHelper.IsAssertionException(exception))
         {
             return(false);
         }
         throw;
     }
 }
Example #5
0
        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());
        }
Example #6
0
        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());
        }
Example #7
0
 public bool TryAssert(T value, out Exception assertionException)
 {
     try
     {
         assert(value);
         assertionException = null;
         return(true);
     }
     catch (Exception exception)
     {
         if (AssertionExceptionHelper.IsAssertionException(exception))
         {
             assertionException = exception;
             return(false);
         }
         throw;
     }
 }