Пример #1
0
        private static Expectation <string> BeLike(IExpectationComposer composer, string pattern, RegexOptions options, string format)
        {
            var regexPattern = "^" + Regex.Escape(pattern).Replace("\\?", ".").Replace("\\*", ".*").Replace("\\#", "\\d") + "$";
            var regex        = new Regex(regexPattern, options);

            return(composer.ComposeSimple <string>(formatter => format, value => value != null && regex.IsMatch(value)));
        }
Пример #2
0
 /// <summary>
 /// Creates expectation for comparable types to be less or equal value specified by <paramref name="value"/>, where the value cannot be null.
 /// </summary>
 /// <param name="composer">Composer</param>
 /// <param name="value">Value</param>
 /// <exception cref="ArgumentNullException">Thrown when <paramref name="value"/> parameter value is null.</exception>
 public static Expectation <T> BeLessOrEqual <T>(this IExpectationComposer composer, T value) where T : IComparable <T>
 {
     if (value == null)
     {
         throw new ArgumentNullException(nameof(value));
     }
     return(composer.ComposeSimple <T>(formatter => $"less or equal '{formatter.FormatValue(value)}'", x => x != null && x.CompareTo(value) <= 0));
 }
Пример #3
0
 /// <summary>
 /// Creates expectation for comparable types to be greater than value specified by <paramref name="value"/>, where the value cannot be null.
 /// </summary>
 /// <param name="composer">Composer</param>
 /// <param name="value">Value</param>
 /// <exception cref="ArgumentNullException">Thrown when <paramref name="value"/> parameter value is null.</exception>
 public static Expectation <T> BeGreaterThan <T>(this IExpectationComposer composer, T value) where T : IComparable <T>
 {
     if (value == null)
     {
         throw new ArgumentNullException(nameof(value));
     }
     return(composer.ComposeSimple <T>(formatter => $"greater than '{formatter.FormatValue(value)}'", x => x != null && x.CompareTo(value) > 0));
 }
Пример #4
0
 /// <summary>
 /// Creates expectation for comparable types to be between values specified by <paramref name="a"/> and <paramref name="b"/> parameters, where parameter values are not included.
 /// The <paramref name="a"/> parameter value may be greater or lower than value of <paramref name="b"/> - both scenarios are supported.
 /// None of the provided parameters can be null.
 /// </summary>
 /// <param name="composer">Composer</param>
 /// <param name="a">Parameter A.</param>
 /// <param name="b">Parameter B.</param>
 /// <exception cref="ArgumentNullException">Thrown when <paramref name="a"/> or <paramref name="b"/> is null.</exception>
 public static Expectation <T> BeBetween <T>(this IExpectationComposer composer, T a, T b) where T : IComparable <T>
 {
     if (a == null)
     {
         throw new ArgumentNullException(nameof(a));
     }
     if (b == null)
     {
         throw new ArgumentNullException(nameof(b));
     }
     return(composer.ComposeSimple <T>(formatter => $"between '{formatter.FormatValue(a)}' and '{formatter.FormatValue(b)}'", x => x != null && Math.Abs(x.CompareTo(a) + x.CompareTo(b)) < 2));
 }
Пример #5
0
 /// <summary>
 /// Creates expectation for collections to contain value specified by <paramref name="value"/> parameter.
 /// </summary>
 /// <typeparam name="T">Expectation value type</typeparam>
 /// <param name="composer">Composer</param>
 /// <param name="value">Expected value</param>
 public static Expectation <IEnumerable <T> > Contain <T>(this IExpectationComposer composer, T value)
 {
     return(composer.ComposeSimple <IEnumerable <T> >(
                formatter => $"contains '{formatter.FormatValue(value)}'",
                x => x != null && x.Contains(value)));
 }
Пример #6
0
 /// <summary>
 /// Creates expectation for values to be equal one of values in <paramref name="expectedCollection"/>.
 /// </summary>
 /// <typeparam name="T">Expectation value type</typeparam>
 /// <param name="composer">Composer</param>
 /// <param name="expectedCollection">Collection of expected values</param>
 public static Expectation <T> BeIn <T>(this IExpectationComposer composer, params T[] expectedCollection)
 {
     return(composer.ComposeSimple <T>(
                formatter => $"in '{formatter.FormatValue(expectedCollection)}'",
                expectedCollection.Contains));
 }
Пример #7
0
 /// <summary>
 /// Creates expectation for values to be equal to <paramref name="expected"/> value.
 /// </summary>
 /// <typeparam name="T">Expectation value type</typeparam>
 /// <param name="composer">Composer</param>
 /// <param name="expected">Expected value</param>
 public static Expectation <T> Equal <T>(this IExpectationComposer composer, T expected)
 {
     return(composer.ComposeSimple <T>(
                formatter => $"equals '{formatter.FormatValue(expected)}'",
                x => Equals(x, expected)));
 }
Пример #8
0
 /// <summary>
 /// Creates expectation for collections to be empty.
 /// </summary>
 /// <param name="builder"></param>
 public static Expectation <T> BeEmpty <T>(this IExpectationComposer builder) where T : IEnumerable
 {
     return(builder.ComposeSimple <T>(formatter => "empty", x => x != null && !x.Cast <object>().Any()));
 }
Пример #9
0
 /// <summary>
 /// Creates expectation for values to be null.
 /// </summary>
 /// <param name="builder"></param>
 public static Expectation <T> BeNull <T>(this IExpectationComposer builder) where T : class
 {
     return(builder.ComposeSimple <T>(formatter => "null", x => x == null));
 }
Пример #10
0
 /// <summary>
 /// Creates expectation for values to be null.
 /// </summary>
 /// <param name="builder"></param>
 public static Expectation <object> BeNull(this IExpectationComposer builder)
 {
     return(builder.ComposeSimple <object>(formatter => "null", x => x == null));
 }
Пример #11
0
        private static Expectation <string> Match(IExpectationComposer composer, string pattern, RegexOptions options, string format)
        {
            var regex = new Regex($"^{pattern}$", options);

            return(composer.ComposeSimple(formatter => format, (string value) => value != null && regex.IsMatch(value)));
        }