Exemple #1
0
        private static IVerificationTarget <bool?> ExpectBooleanValue(
            IVerificationTarget <bool?> target,
            bool?expectedValue,
            FormattableString?message)
        {
            return(CallbackAssertion.Create(
                       target,
                       context =>
            {
                if (target.Value == expectedValue)
                {
                    return true;
                }

                var writer = context.Writer;

                context.WriteMessage(message);

                using (writer.Indent())
                {
                    writer.WriteLine($"Expected: {expectedValue}");
                    writer.WriteLine($"But was:  {target}");
                }

                return false;
            }));
        }
Exemple #2
0
 public void WriteMessage(FormattableString?message)
 {
     if (message != null)
     {
         Writer.WriteLine(message);
     }
 }
        /// <summary>
        ///   Verifies that the given value is not null.
        /// </summary>
        public static IVerificationTarget <T?> NotNull <T>(
            this IVerificationTarget <T?> target,
            FormattableString?message = null)
            where T : struct
        {
            return(CallbackAssertion.Create(
                       target,
                       context =>
            {
                if (!target.Value.HasValue)
                {
                    return true;
                }

                context.WriteMessage(message);

                using (context.Writer.Indent())
                {
                    context.Writer.WriteLine($"Expected: not <null>");
                    context.Writer.WriteLine($"But was:  <null>");
                }

                return false;
            }));
        }
Exemple #4
0
        public FailedAnalyzedAssertion AnalyzeException(FailedAssertion part)
        {
            HandledException?        handledException        = null;
            IExceptionHandlerPattern?handledExceptionPattern = null;
            var exception = part.Exception !;

            foreach (var pattern in _patterns)
            {
                if (pattern.IsMatch(exception))
                {
                    handledException = pattern.Handle(part);

                    if (handledException != null)
                    {
                        handledExceptionPattern = pattern;
                        break;
                    }
                }
            }

            FormattableString?failedAssertionMessage = handledException?.Message;

            if (handledException == null)
            {
                failedAssertionMessage = $@"Assertion threw {exception.GetType().FullName}: {exception.Message}";
            }

            return(new FailedAnalyzedAssertion(part, FriendlyMessageFormatter.GetString(failedAssertionMessage, _context.EvaluatedExpressions),
                                               handledExceptionPattern,
                                               handledException?.CauseOfException));
        }
 public static void Info(object?thisOrContextObject, FormattableString?formattableString = null, [CallerMemberName] string?memberName = null)
 {
     DebugValidateArg(thisOrContextObject);
     DebugValidateArg(formattableString);
     if (IsEnabled)
     {
         Log.Info(IdOf(thisOrContextObject), memberName, formattableString != null ? Format(formattableString) : NoParameters);
     }
 }
        public static StringBuilder AppendInvariant(this StringBuilder sb, FormattableString?value)
        {
            if (value != null)
            {
                return(sb.Append(value.ToString(CultureInfo.InvariantCulture)));
            }

            return(sb);
        }
Exemple #7
0
 public static void NotNullOrEmpty(FormattableString?value, string paramName)
 {
     if (value == null)
     {
         throw new ArgumentNullException(paramName);
     }
     if (string.IsNullOrEmpty(value.Format))
     {
         throw new ArgumentException(paramName + " cannot be empty.", paramName);
     }
 }
Exemple #8
0
        public static string?GetString(FormattableString?formattableString, HashSet <Expression> evaluatedExpressions)
        {
            if (formattableString == null)
            {
                return(null);
            }

            var arguments = formattableString.GetArguments();

            for (var i = 0; i < arguments.Length; i++)
            {
                var a = arguments[i];

                if (a is Expression expression)
                {
                    arguments[i] = ExpressionHelper.ExpressionToString(expression);
                }
                else if (a is FormattableString innerFormattableString)
                {
                    arguments[i] = GetString(innerFormattableString, evaluatedExpressions);
                }
                else if (a is IEnumerable <FormattableString> formattableStrings)
                {
                    arguments[i] = string.Join(Environment.NewLine, formattableStrings.Select(f => GetString(f, evaluatedExpressions)));
                }
                else if (a is null)
                {
                    arguments[i] = "null";
                }
                else if (a is ExpressionValue expressionValue)
                {
                    var value = ExpressionHelper.EvaluateExpression(expressionValue.Expression);
                    evaluatedExpressions.Add(expressionValue.Expression);
                    arguments[i] = Serializer.Serialize(value);
                }
                else if (a is string s)
                {
                    arguments[i] = s;
                }
                else
                {
                    arguments[i] = Serializer.Serialize(arguments[i]);
                }
            }

            return(string.Format(CultureInfo.InvariantCulture, formattableString.Format, arguments));
        }
 private static void DebugValidateArg(FormattableString?arg)
 {
     Debug.Assert(IsEnabled || arg == null, $"Should not be formatting FormattableString \"{arg}\" if tracing isn't enabled");
 }
Exemple #10
0
 /// <summary>
 ///   Verifies that the given value is equal to false.
 /// </summary>
 public static IVerificationTarget <bool?> False(
     this IVerificationTarget <bool?> target,
     FormattableString?message = null)
 => ExpectBooleanValue(target, false, message);
Exemple #11
0
 public static void Info(object?thisOrContextObject, FormattableString?formattableString = null, [CallerMemberName] string?memberName = null) =>
 Log.Info(IdOf(thisOrContextObject), memberName, formattableString != null ? Format(formattableString) : NoParameters);
Exemple #12
0
 /// <summary>
 ///   Verifies that the given value is equal to the given value.
 /// </summary>
 public static IVerificationTarget <T> EqualTo <T>(
     this IVerificationTarget <T> target,
     T comparisonValue,
     FormattableString?message = null)
 => EqualTo(target, comparisonValue, null !, message);
 /// <summary>
 ///   Verifies that the given value is greater than the comparison value.
 /// </summary>
 public static IVerificationTarget <T?> GreaterThan <T>(
     this IVerificationTarget <T?> value,
     T comparisonValue,
     FormattableString?message = null)
     where T : struct, IComparable <T>
 => GreaterThan(value, comparisonValue, null !, message);