/// <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)); }
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))); }
/// <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)); }
/// <summary> /// Creates expectation for strings to match regex pattern specified by <paramref name="pattern"/> parameter. /// </summary> /// <param name="composer">Composer</param> /// <param name="pattern">Expected pattern</param> public static Expectation <string> Match(this IExpectationComposer composer, string pattern) { return(Match(composer, pattern, RegexOptions.None, $"matches '{pattern}'")); }
/// <summary> /// Creates expectation for strings to match regex pattern specified by <paramref name="pattern"/> parameter, where character case is ignored. /// </summary> /// <param name="composer">Composer</param> /// <param name="pattern">Expected pattern</param> public static Expectation <string> MatchIgnoreCase(this IExpectationComposer composer, string pattern) { return(Match(composer, pattern, RegexOptions.IgnoreCase, $"matches '{pattern}' ignore case")); }
/// <summary> /// Creates expectation for strings to match pattern specified by <paramref name="pattern"/> parameter. /// /// The <paramref name="pattern"/> may have special characters: /// <list type="bullet"> /// <item><description>* - 0-more characters</description></item> /// <item><description>? - 1 character</description></item> /// <item><description># - 1 digit</description></item> /// </list> /// </summary> /// <param name="composer">Composer</param> /// <param name="pattern">Expected pattern</param> public static Expectation <string> BeLike(this IExpectationComposer composer, string pattern) { return(BeLike(composer, pattern, RegexOptions.None, $"like '{pattern}'")); }
/// <summary> /// Creates expectation for strings to match pattern specified by <paramref name="pattern"/> parameter, where character case is ignored. /// /// The <paramref name="pattern"/> may have special characters: /// <list type="bullet"> /// <item><description>* - 0-more characters</description></item> /// <item><description>? - 1 character</description></item> /// <item><description># - 1 digit</description></item> /// </list> /// </summary> /// <param name="composer">Composer</param> /// <param name="pattern">Expected pattern</param> public static Expectation <string> BeLikeIgnoreCase(this IExpectationComposer composer, string pattern) { return(BeLike(composer, pattern, RegexOptions.IgnoreCase, $"like '{pattern}' ignore case")); }
/// <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))); }
/// <summary> /// Creates expectation for collections with every item fulfilling expectation specified by <paramref name="expectationBuilder"/> parameter. /// </summary> /// <typeparam name="T">Expectation value type</typeparam> /// <param name="composer">Composer</param> /// <param name="expectationBuilder">Expectation builder</param> public static Expectation <IEnumerable <T> > EveryItem <T>(this IExpectationComposer composer, Func <IExpectationComposer, IExpectation <T> > expectationBuilder) { return(composer.Compose(new EveryItemExpectation <T>(expectationBuilder.Invoke(Expect.To)))); }
/// <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)); }
/// <summary> /// Creates expectation for values to fulfill any expectation specified by <paramref name="expectationBuilder"/> parameter. /// </summary> /// <typeparam name="T">Expectation value type</typeparam> /// <param name="composer">Composer</param> /// <param name="expectationBuilder">Expectation builder</param> public static Expectation <T> BeAnyTrue <T>(this IExpectationComposer composer, params Func <IExpectationComposer, IExpectation <T> >[] expectationBuilder) { return(composer.Compose(new OrExpectation <T>("any true ", expectationBuilder.Select(x => x.Invoke(Expect.To)).ToArray()))); }
/// <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))); }
/// <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())); }
/// <summary> /// Helper method creating simple expectation based on <paramref name="predicateFn"/> and <paramref name="descriptionFn"/>. /// </summary> /// <typeparam name="T">Expectation value type</typeparam> /// <param name="composer">Composer</param> /// <param name="descriptionFn">Expectation description</param> /// <param name="predicateFn">Expectation predicate</param> public static Expectation <T> ComposeSimple <T>(this IExpectationComposer composer, Func <IValueFormattingService, string> descriptionFn, Func <T, bool> predicateFn) { return(composer.Compose(new SimpleExpectation <T>(descriptionFn, predicateFn))); }