/// <summary> /// Checks if the exception has a specific property having a specific value. /// </summary> /// <typeparam name="T">Exception type</typeparam> /// <typeparam name="TP">Property type</typeparam> /// <param name="checker">Syntax helper</param> /// <param name="propertyName">Name of property</param> /// <param name="propertyValue">Expected valued of property</param> /// <returns>A chainable check.</returns> public static ICheckLink <ILambdaExceptionCheck <T> > WithProperty <T, TP>(this ILambdaExceptionCheck <T> checker, string propertyName, TP propertyValue) where T : Exception { var found = false; ExtensibilityHelper.BeginCheck(checker as FluentSut <T>) .CantBeNegated("WithProperty") .SetSutName("exception") .CheckSutAttributes(sut => { var type = sut.GetType(); var property = type.GetProperty(propertyName); if (property == null) { return(null); } found = true; return(property.GetValue(sut, null)); }, $"property [{propertyName}]") .FailWhen(_ => !found, $"There is no property [{propertyName}] on exception type [{typeof(T).Name}].", MessageOption.NoCheckedBlock) .FailWhen(sut => !EqualityHelper.FluentEquals(sut, propertyValue), "The {0} does not have the expected value.") .DefineExpectedValue(propertyValue, "", "") .EndCheck(); return(new CheckLink <ILambdaExceptionCheck <T> >(checker)); }
/// <summary> /// Checks that the actual <see cref="IDictionary{K,V}"/> contains the expected expectedKey. /// </summary> /// <typeparam name="TK"> /// The type of the expectedKey element. /// </typeparam> /// <typeparam name="TU">Type for values.</typeparam> /// <param name="check"> /// The fluent check to be extended. /// </param> /// <param name="key"> /// The expected expectedKey value. /// </param> /// <returns> /// A check link. /// </returns> public static ICheckLink <ICheck <IEnumerable <KeyValuePair <TK, TU> > > > ContainsKey <TK, TU>(this ICheck <IEnumerable <KeyValuePair <TK, TU> > > check, TK key) { ExtensibilityHelper.BeginCheck(check). Analyze((sut, test) => { if (sut is IDictionary <TK, TU> dico) { if (dico.ContainsKey(key)) { return; } } #if !DOTNET_20 && !DOTNET_30 && !DOTNET_35 && !DOTNET_40 && !PORTABLE else if (sut is IReadOnlyDictionary <TK, TU> roDico) { if (roDico.ContainsKey(key)) { return; } } #endif else { if (sut.Any(keyValuePair => EqualityHelper.FluentEquals(keyValuePair.Key, key))) { return; } } test.Fails("The {0} does not contain the expected key."); }).Expecting(key, expectedLabel: "Expected key:", negatedLabel: "Forbidden key:") .Negates("The {0} does contain the given key, whereas it must not.").EndCheck(); return(ExtensibilityHelper.BuildCheckLink(check)); }
/// <summary> /// Checks that the actual <see cref="IDictionary{K,V}"/> contains the expected value. /// </summary> /// <typeparam name="TK"> /// The type of the expectedKey element. /// </typeparam> /// <typeparam name="TU"> /// Value type. /// </typeparam> /// <param name="check"> /// The fluent check to be extended. /// </param> /// <param name="expectedValue"> /// The expected value. /// </param> /// <returns> /// A check link. /// </returns> public static ICheckLink <ICheck <IEnumerable <KeyValuePair <TK, TU> > > > ContainsValue <TK, TU>(this ICheck <IEnumerable <KeyValuePair <TK, TU> > > check, TU expectedValue) { ExtensibilityHelper.BeginCheck(check). FailWhen(sut => !sut.Any(keyValuePair => EqualityHelper.FluentEquals(keyValuePair.Value, expectedValue)), "The {0} does not contain the expected value."). DefineExpectedResult(expectedValue, "Expected value:", "Forbidden value:"). OnNegate("The {0} does contain the given value, whereas it must not."). EndCheck(); return(ExtensibilityHelper.BuildCheckLink(check)); }
/// <summary> /// Checks that the actual <see cref="Hashtable"/> contains the expected key-value pair. /// </summary> /// <param name="check">Fluent check.</param> /// <param name="expectedKey">Expected key.</param> /// <param name="expectedValue">Expected value.</param> /// <returns>A check link.</returns> public static ICheckLink <ICheck <Hashtable> > ContainsPair( this ICheck <Hashtable> check, object expectedKey, object expectedValue) { ExtensibilityHelper.BeginCheck(check) .FailsIf(sut => !sut.ContainsKey(expectedKey), "The {0} does not contain the expected key-value pair. The given key was not found.") .FailsIf(sut => !EqualityHelper.FluentEquals(sut[expectedKey], expectedValue), "The {0} does not contain the expected value for the given key.") .Expecting(new KeyValuePair <object, object>(expectedKey, expectedValue), expectedLabel: "Expected pair:", negatedLabel: "Forbidden pair:") .Negates("The {0} does contain the given key-value pair, whereas it must not.") .EndCheck(); return(ExtensibilityHelper.BuildCheckLink(check)); }
/// <summary> /// Checks that the actual <see cref="IDictionary{K,V}"/> contains the expected key-value pair. /// </summary> /// <typeparam name="TK">The key type.</typeparam> /// <typeparam name="TU">The value type.</typeparam> /// <param name="check">Fluent check.</param> /// <param name="expectedKey">Expected key.</param> /// <param name="expectedValue">Expected value.</param> /// <returns>A check link.</returns> public static ICheckLink <ICheck <IEnumerable <KeyValuePair <TK, TU> > > > ContainsPair <TK, TU>( this ICheck <IEnumerable <KeyValuePair <TK, TU> > > check, TK expectedKey, TU expectedValue) { ExtensibilityHelper.BeginCheck(check). Analyze((sut, test) => { var foundValue = default(TU); var found = false; if (sut is IDictionary <TK, TU> dico) { found = dico.TryGetValue(expectedKey, out foundValue); } #if !DOTNET_20 && !DOTNET_30 && !DOTNET_35 && !DOTNET_40 && !PORTABLE else if (sut is IReadOnlyDictionary <TK, TU> roDico) { found = roDico.TryGetValue(expectedKey, out foundValue); } #endif else { foreach (var keyValuePair in sut) { if (!EqualityHelper.FluentEquals(keyValuePair.Key, expectedKey)) { continue; } found = true; foundValue = keyValuePair.Value; break; } } // check found value if (found && EqualityHelper.FluentEquals(foundValue, expectedValue)) { return; } test.Fail( !found ? "The {0} does not contain the expected key-value pair. The given key was not found." : "The {0} does not contain the expected value for the given key."); }). DefineExpectedResult(new KeyValuePair <TK, TU>(expectedKey, expectedValue), "Expected pair:", "Forbidden pair:"). OnNegate("The {0} does contain the given key-value pair, whereas it must not."). EndCheck(); return(ExtensibilityHelper.BuildCheckLink(check)); }
/// <summary> /// Checks that the actual <see cref="IDictionary{K,V}"/> contains the expected value. /// </summary> /// <typeparam name="TK"> /// The type of the expectedKey element. /// </typeparam> /// <typeparam name="TU"> /// Value type. /// </typeparam> /// <param name="check"> /// The fluent check to be extended. /// </param> /// <param name="expectedValue"> /// The expected value. /// </param> /// <returns> /// A check link. /// </returns> public static ICheckLink <ICheck <IEnumerable <KeyValuePair <TK, TU> > > > ContainsValue <TK, TU>(this ICheck <IEnumerable <KeyValuePair <TK, TU> > > check, TU expectedValue) { ExtensibilityHelper.BeginCheck(check). Analyze((sut, test) => { if (sut.Any(keyValuePair => EqualityHelper.FluentEquals(keyValuePair.Value, expectedValue))) { return; } test.Fails("The {0} does not contain the expected value."); }).Expecting(expectedValue, expectedLabel: "Expected value:", negatedLabel: "Forbidden value:") .Negates("The {0} does contain the given value, whereas it must not.").EndCheck(); return(ExtensibilityHelper.BuildCheckLink(check)); }
/// <summary> /// Checks if the exception has a specific property having a specific value. /// </summary> /// <typeparam name="T">Exception type</typeparam> /// <typeparam name="TP">Property type</typeparam> /// <param name="checker">Syntax helper</param> /// <param name="propertyExpression">Extracting expression</param> /// <param name="propertyValue">Expected valued of property</param> /// <returns>A chainable check.</returns> public static ICheckLink <ILambdaExceptionCheck <T> > WithProperty <T, TP>(this ILambdaExceptionCheck <T> checker, Expression <Func <T, TP> > propertyExpression, TP propertyValue) where T : Exception { var propertyName = ExpressionHelper.GetPropertyNameFromExpression(propertyExpression); ExtensibilityHelper.BeginCheck(checker as FluentSut <T>) .CantBeNegated("WithProperty") .SetSutName("exception") .CheckSutAttributes(sut => propertyExpression.Compile().Invoke(sut), $"property [{propertyName}]") .FailWhen(sut => !EqualityHelper.FluentEquals(sut, propertyValue), "The {0} does not have the expected value.") .DefineExpectedValue(propertyValue, "", "") .EndCheck(); return(new CheckLink <ILambdaExceptionCheck <T> >(checker)); }
/// <summary> /// Checks that the actual <see cref="IDictionary{K,V}"/> is equivalent to a given dictionary. /// </summary> /// <param name="check">The fluent check to be extended.</param> /// <param name="other">Reference dictionary</param> /// <typeparam name="TK">Type for keys.</typeparam> /// <typeparam name="TU">Type for values.</typeparam> /// <returns>A check link.</returns> public static ICheckLink <ICheck <IEnumerable <KeyValuePair <TK, TU> > > > IsEquivalentTo <TK, TU>( this ICheck <IEnumerable <KeyValuePair <TK, TU> > > check, IEnumerable <KeyValuePair <TK, TU> > other) { ExtensibilityHelper.BeginCheck(check).Analyze((sut, test) => { if (sut == null) { if (other != null) { test.Fail("The {checked} is null whereas it should not."); } return; } if (other == null) { test.Fail("The {checked} must be null."); return; } foreach (var pair in other) { if (!sut.TryGet(pair.Key, out var value)) { test.Fail( $"The {{checked}} is not equivalent to the {{expected}}. Missing entry ({pair.Key.ToStringProperlyFormatted().DoubleCurlyBraces()}, {pair.Value.ToStringProperlyFormatted().DoubleCurlyBraces()})."); return; } if (!EqualityHelper.FluentEquals(pair.Value, value)) { test.Fail( $"The {{checked}} is not equivalent to the {{expected}}. Entry ({pair.Key.ToStringProperlyFormatted().DoubleCurlyBraces()}) does not have the expected value." + Environment.NewLine + "Expected:" + Environment.NewLine + $"\t{pair.Value.ToStringProperlyFormatted().DoubleCurlyBraces()}" + Environment.NewLine + "Actual:" + Environment.NewLine + $"\t{value.ToStringProperlyFormatted().DoubleCurlyBraces()}"); return; } } }). DefineExpectedValue(other). OnNegate("The {checked} is equivalent to the {expected}, whereas it should not!", MessageOption.NoExpectedBlock). EndCheck(); return(ExtensibilityHelper.BuildCheckLink(check)); }
/// <summary> /// Checks that the actual <see cref="Hashtable"/> contains the expected value. /// </summary> /// <param name="check"> /// The fluent check to be extended. /// </param> /// <param name="expectedValue"> /// The expected value. /// </param> /// <returns> /// A check link. /// </returns> public static ICheckLink <ICheck <Hashtable> > ContainsValue(this ICheck <Hashtable> check, object expectedValue) { ExtensibilityHelper.BeginCheck(check). Analyze((sut, test) => { foreach (DictionaryEntry entry in sut) { if (EqualityHelper.FluentEquals(entry.Value, expectedValue)) { return; } } test.Fails("The {0} does not contain the expected value."); }).Expecting(expectedValue, expectedLabel: "Expected value:", negatedLabel: "Forbidden value:") .Negates("The {0} does contain the given value, whereas it must not.").EndCheck(); return(ExtensibilityHelper.BuildCheckLink(check)); }
/// <summary> /// Checks if the exception has a specific property having a specific value. /// </summary> /// <typeparam name="T">Exception type</typeparam> /// <typeparam name="TP">Property type</typeparam> /// <param name="checker">Syntax helper</param> /// <param name="propertyExpression">Extracting expression</param> /// <param name="propertyValue">Expected valued of property</param> /// <returns>A chainable check.</returns> public static ICheckLink <ILambdaExceptionCheck <T> > WithProperty <T, TP>(this ILambdaExceptionCheck <T> checker, Expression <Func <T, TP> > propertyExpression, TP propertyValue) where T : Exception { var memberExpression = propertyExpression.Body as MemberExpression; var propertyName = memberExpression?.Member.Name ?? propertyExpression.ToString(); ExtensibilityHelper.BeginCheck(checker as FluentSut <T>) .InvalidIf(sut => sut == null, "No exception. Can't be used when negated.") .SetSutName("exception") .CheckSutAttributes(sut => propertyExpression.Compile().Invoke(sut), $"property [{propertyName}]") .FailWhen(sut => !EqualityHelper.FluentEquals(sut, propertyValue), "The {0} does not have the expected value.") .DefineExpectedValue(propertyValue) .EndCheck(); return(new CheckLink <ILambdaExceptionCheck <T> >(checker)); }
/// <summary> /// Checks that the actual <see cref="Hashtable"/> contains the expected value. /// </summary> /// <param name="check"> /// The fluent check to be extended. /// </param> /// <param name="expectedValue"> /// The expected value. /// </param> /// <returns> /// A check link. /// </returns> public static ICheckLink <ICheck <Hashtable> > ContainsValue(this ICheck <Hashtable> check, object expectedValue) { ExtensibilityHelper.BeginCheck(check). FailWhen(sut => { foreach (DictionaryEntry entry in sut) { if (EqualityHelper.FluentEquals(entry.Value, expectedValue)) { return(false); } } return(true); }, "The {0} does not contain the expected value."). DefineExpectedResult(expectedValue, "Expected value:", "Forbidden value:"). OnNegate("The {0} does contain the given value, whereas it must not."). EndCheck(); return(ExtensibilityHelper.BuildCheckLink(check)); }
/// <summary> /// Checks that the actual <see cref="IDictionary{K,V}"/> contains the expected key-value pair. /// </summary> /// <typeparam name="TK">The key type.</typeparam> /// <typeparam name="TU">The value type.</typeparam> /// <param name="check">Fluent check.</param> /// <param name="expectedKey">Expected key.</param> /// <param name="expectedValue">Expected value.</param> /// <returns>A check link.</returns> public static ICheckLink <ICheck <IEnumerable <KeyValuePair <TK, TU> > > > ContainsPair <TK, TU>( this ICheck <IEnumerable <KeyValuePair <TK, TU> > > check, TK expectedKey, TU expectedValue) { ExtensibilityHelper.BeginCheck(check). Analyze((sut, test) => { var found = DictionaryExtensions.WrapDictionary <TK, TU>(sut).TryGetValue(expectedKey, out var foundValue); if (!found || !EqualityHelper.FluentEquals(foundValue, expectedValue)) { test.Fail( !found ? "The {0} does not contain the expected key-value pair. The given key was not found." : "The {0} does not contain the expected value for the given key."); } }). DefineExpectedResult(new KeyValuePair <TK, TU>(expectedKey, expectedValue), "Expected pair:", "Forbidden pair:"). OnNegate("The {0} does contain the given key-value pair, whereas it must not."). EndCheck(); return(ExtensibilityHelper.BuildCheckLink(check)); }