public void HashCodeIsConsistent() { var toto = new { entier = 2, lettre = 'b', sub = new { sub = (object)null } }; var sut = ReflectionWrapper.BuildFromInstance(toto.GetType(), toto, new Criteria(BindingFlags.Instance | BindingFlags.NonPublic, true, true)); Check.That(sut.GetHashCode()).IsEqualTo(147721457); }
/// <summary> /// </summary> /// <typeparam name="T"></typeparam> /// <param name="check"></param> /// <returns></returns> public static IMembersSelection Considering <T>(this ICheck <T> check) { var checker = ExtensibilityHelper.ExtractChecker(check); var fieldsWrapper = ReflectionWrapper.BuildFromInstance(checker.Value?.GetType() ?? typeof(T), checker.Value, new Criteria(BindingFlags.Instance)); var checkWithConsidering = new CheckWithConsidering(fieldsWrapper, checker.Negated); return(checkWithConsidering); }
/// <summary> /// Checks that the actual actualValue doesn't have all fields equal to the expected actualValue ones. /// </summary> /// <typeparam name="T"> /// Type of the checked actualValue. /// </typeparam> /// <param name="check"> /// The fluent check to be extended. /// </param> /// <param name="expected"> /// The expected actualValue. /// </param> /// <returns> /// A check link. /// </returns> /// <exception cref="FluentCheckException"> /// The actual actualValue has all fields equal to the expected actualValue ones. /// </exception> /// <remarks> /// The comparison is done field by field. /// </remarks> public static ICheckLink <ICheck <T> > HasNotFieldsWithSameValues <T>(this ICheck <T> check, object expected) { var checker = ExtensibilityHelper.ExtractChecker(check); var fieldsWrapper = ReflectionWrapper.BuildFromInstance(typeof(T), checker.Value, new Criteria(BindingFlags.Instance)); var checkWithConsidering = new CheckWithConsidering(fieldsWrapper, checker.Negated).All.Fields; ReflectionWrapperChecks.FieldEqualTest(checkWithConsidering, expected, false); return(ExtensibilityHelper.BuildCheckLink(check)); }
/// <inheritdoc cref="ObjectCheckExtensions.IsDistinctFrom{T,TU}" /> public static ICheckLink <ICheck <ReflectionWrapper> > IsDistinctFrom <TU>(this ICheck <ReflectionWrapper> check, TU expected) { var checker = ExtensibilityHelper.ExtractChecker(check); return(checker.ExecuteCheck(() => { var expectedWrapper = ReflectionWrapper.BuildFromInstance(typeof(TU), expected, checker.Value.Criteria); expectedWrapper.MapFields(checker.Value, 1, (scan, match, depth) => { if (depth > 0) { return true; } if (match == null) { var result = checker.BuildShortMessage( $"The {{1}}'s {scan.MemberLabel.DoubleCurlyBraces()} is absent from the {{0}}.") .For("value").Expected(scan.Value) .Label($"The {{0}} {scan.MemberLabel.DoubleCurlyBraces()}:"); throw new FluentCheckException(result.ToString()); } if (scan == null) { var result = checker.BuildShortMessage( $"The {{0}}'s {match.MemberLabel.DoubleCurlyBraces()} is absent from the {{1}}.") .For("value").On(match.Value) .Label($"The {{1}} {match.MemberLabel.DoubleCurlyBraces()}:"); throw new FluentCheckException(result.ToString()); } if (ReferenceEquals(scan.Value, match.Value)) { var message = checker.BuildShortMessage( $"The {{0}}'s {match.MemberLabel.DoubleCurlyBraces()} does reference the reference instance, whereas it should not.") .For("value").On(match); throw new FluentCheckException(message.ToString()); } return scan.Value != null; }); }, checker.BuildMessage("The {0} has no null member, whereas it should.").ToString())); }