Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Src/FluentAssertions/Equivalency/EquivalencyOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,6 @@ public EquivalencyOptions()
IncludingFields();
IncludingProperties();

RespectingDeclaredTypes();
PreferringDeclaredMemberTypes();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ EqualityStrategy IEquivalencyOptions.GetEqualityStrategy(Type type)
/// </remarks>
public TSelf IncludingAllDeclaredProperties()
{
RespectingDeclaredTypes();
PreferringDeclaredMemberTypes();

ExcludingFields();
IncludingProperties();
Expand All @@ -222,7 +222,7 @@ public TSelf IncludingAllDeclaredProperties()
/// </remarks>
public TSelf IncludingAllRuntimeProperties()
{
RespectingRuntimeTypes();
PreferringRuntimeMemberTypes();

ExcludingFields();
IncludingProperties();
Expand Down Expand Up @@ -331,18 +331,24 @@ public TSelf IgnoringNonBrowsableMembersOnSubject()
}

/// <summary>
/// Instructs the comparison to respect the expectation's runtime type.
/// Instructs the structural equality comparison to use the run-time types
/// of the members to drive the assertion.
/// </summary>
public TSelf RespectingRuntimeTypes()
public TSelf PreferringRuntimeMemberTypes()
{
useRuntimeTyping = true;
return (TSelf)this;
}

/// <summary>
/// Instructs the comparison to respect the expectation's declared type.
/// Instructs the structural equality comparison to prefer the declared types
/// of the members when executing assertions.
/// </summary>
public TSelf RespectingDeclaredTypes()
/// <remarks>
/// In reality, the declared types of the members are only known for the members of the root object,
/// or the objects in a root collection. Beyond that, FluentAssertions only knows the run-time types of the members.
/// </remarks>
public TSelf PreferringDeclaredMemberTypes()
{
useRuntimeTyping = false;
return (TSelf)this;
Expand Down Expand Up @@ -778,9 +784,9 @@ public override string ToString()
{
var builder = new StringBuilder();

builder.Append("- Use ")
builder.Append("- Prefer the ")
.Append(useRuntimeTyping ? "runtime" : "declared")
.AppendLine(" types and members");
.AppendLine(" type of the members");

if (ignoreNonBrowsableOnSubject)
{
Expand Down
4 changes: 2 additions & 2 deletions Src/FluentAssertions/ObjectAssertionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static AndConstraint<ObjectAssertions> BeDataContractSerializable<T>(this
var deserializedObject = CreateCloneUsingDataContractSerializer(assertions.Subject);

EquivalencyOptions<T> defaultOptions = AssertionOptions.CloneDefaults<T>()
.RespectingRuntimeTypes().IncludingFields().IncludingProperties();
.PreferringRuntimeMemberTypes().IncludingFields().IncludingProperties();

((T)deserializedObject).Should().BeEquivalentTo((T)assertions.Subject, _ => options(defaultOptions));
}
Expand Down Expand Up @@ -105,7 +105,7 @@ public static AndConstraint<ObjectAssertions> BeXmlSerializable(this ObjectAsser
object deserializedObject = CreateCloneUsingXmlSerializer(assertions.Subject);

deserializedObject.Should().BeEquivalentTo(assertions.Subject,
options => options.RespectingRuntimeTypes().IncludingFields().IncludingProperties());
options => options.PreferringRuntimeMemberTypes().IncludingFields().IncludingProperties());
}
catch (Exception exc)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -903,8 +903,8 @@ namespace FluentAssertions.Equivalency
public TSelf IncludingInternalProperties() { }
public TSelf IncludingNestedObjects() { }
public TSelf IncludingProperties() { }
public TSelf RespectingDeclaredTypes() { }
public TSelf RespectingRuntimeTypes() { }
public TSelf PreferringDeclaredMemberTypes() { }
public TSelf PreferringRuntimeMemberTypes() { }
public TSelf ThrowingOnMissingMembers() { }
public override string ToString() { }
public TSelf Using(FluentAssertions.Equivalency.IEquivalencyStep equivalencyStep) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -916,8 +916,8 @@ namespace FluentAssertions.Equivalency
public TSelf IncludingInternalProperties() { }
public TSelf IncludingNestedObjects() { }
public TSelf IncludingProperties() { }
public TSelf RespectingDeclaredTypes() { }
public TSelf RespectingRuntimeTypes() { }
public TSelf PreferringDeclaredMemberTypes() { }
public TSelf PreferringRuntimeMemberTypes() { }
public TSelf ThrowingOnMissingMembers() { }
public override string ToString() { }
public TSelf Using(FluentAssertions.Equivalency.IEquivalencyStep equivalencyStep) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -895,8 +895,8 @@ namespace FluentAssertions.Equivalency
public TSelf IncludingInternalProperties() { }
public TSelf IncludingNestedObjects() { }
public TSelf IncludingProperties() { }
public TSelf RespectingDeclaredTypes() { }
public TSelf RespectingRuntimeTypes() { }
public TSelf PreferringDeclaredMemberTypes() { }
public TSelf PreferringRuntimeMemberTypes() { }
public TSelf ThrowingOnMissingMembers() { }
public override string ToString() { }
public TSelf Using(FluentAssertions.Equivalency.IEquivalencyStep equivalencyStep) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -903,8 +903,8 @@ namespace FluentAssertions.Equivalency
public TSelf IncludingInternalProperties() { }
public TSelf IncludingNestedObjects() { }
public TSelf IncludingProperties() { }
public TSelf RespectingDeclaredTypes() { }
public TSelf RespectingRuntimeTypes() { }
public TSelf PreferringDeclaredMemberTypes() { }
public TSelf PreferringRuntimeMemberTypes() { }
public TSelf ThrowingOnMissingMembers() { }
public override string ToString() { }
public TSelf Using(FluentAssertions.Equivalency.IEquivalencyStep equivalencyStep) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public void When_the_runtime_type_does_match_the_equality_comparer_type_it_shoul

// Act
Action act = () => subject.Should().BeEquivalentTo(expectation, opt => opt
.RespectingRuntimeTypes()
.PreferringRuntimeMemberTypes()
.Using<ConcreteClass, ConcreteClassEqualityComparer>());

// Assert
Expand Down Expand Up @@ -289,7 +289,7 @@ public void An_equality_comparer_of_non_nullable_type_is_invoked_on_non_nullable

// Act
subject.Should().BeEquivalentTo(expectation, opt => opt
.RespectingRuntimeTypes()
.PreferringRuntimeMemberTypes()
.Using(new DateTimeByYearComparer()));
}

Expand All @@ -303,7 +303,7 @@ public void An_equality_comparer_of_nullable_type_is_not_invoked_on_non_nullable

// Act
Action act = () => subject.Should().BeEquivalentTo(expectation, opt => opt
.RespectingRuntimeTypes()
.PreferringRuntimeMemberTypes()
.Using(new NullableDateTimeByYearComparer()));

// Assert
Expand Down
6 changes: 3 additions & 3 deletions Tests/FluentAssertions.Equivalency.Specs/CollectionSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ public void

// Act
Action act =
() => collection1.Should().BeEquivalentTo(collection2, opts => opts.RespectingRuntimeTypes());
() => collection1.Should().BeEquivalentTo(collection2, opts => opts.PreferringRuntimeMemberTypes());

// Assert
act.Should().Throw<XunitException>("the runtime type is assignable to two IEnumerable interfaces")
Expand Down Expand Up @@ -1131,7 +1131,7 @@ public void

// Act
Action act =
() => collection1.Should().BeEquivalentTo(collection2, opts => opts.RespectingRuntimeTypes());
() => collection1.Should().BeEquivalentTo(collection2, opts => opts.PreferringRuntimeMemberTypes());

// Assert
act.Should().Throw<XunitException>("the items have different runtime types");
Expand Down Expand Up @@ -1614,7 +1614,7 @@ public void
Action act =
() =>
collection1.Should().BeEquivalentTo(collection2,
opts => opts.RespectingRuntimeTypes());
opts => opts.PreferringRuntimeMemberTypes());

// Assert
act.Should().Throw<XunitException>("the types have different properties");
Expand Down
10 changes: 5 additions & 5 deletions Tests/FluentAssertions.Equivalency.Specs/DictionarySpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ public void
object object2 = new Dictionary<string, string> { ["greeting"] = "hello" };

// Act
Action act = () => object1.Should().BeEquivalentTo(object2, opts => opts.RespectingRuntimeTypes());
Action act = () => object1.Should().BeEquivalentTo(object2, opts => opts.PreferringRuntimeMemberTypes());

// Assert
act.Should().NotThrow("the runtime type is a dictionary and the dictionaries are equivalent");
Expand Down Expand Up @@ -500,7 +500,7 @@ public void
object object2 = new NonGenericDictionary { ["greeting"] = "hello" };

// Act
Action act = () => object1.Should().BeEquivalentTo(object2, opts => opts.RespectingRuntimeTypes());
Action act = () => object1.Should().BeEquivalentTo(object2, opts => opts.PreferringRuntimeMemberTypes());

// Assert
act.Should().NotThrow("the runtime type is a dictionary and the dictionaries are equivalent");
Expand All @@ -516,7 +516,7 @@ public void
var traceWriter = new StringBuilderTraceWriter();

// Act
object1.Should().BeEquivalentTo(object2, opts => opts.RespectingRuntimeTypes().WithTracing(traceWriter));
object1.Should().BeEquivalentTo(object2, opts => opts.PreferringRuntimeMemberTypes().WithTracing(traceWriter));

// Assert
string trace = traceWriter.ToString();
Expand Down Expand Up @@ -564,7 +564,7 @@ public void
Action act =
() =>
dictionary1.Should().BeEquivalentTo(dictionary2,
opts => opts.RespectingRuntimeTypes());
opts => opts.PreferringRuntimeMemberTypes());

// Assert
act.Should().Throw<XunitException>("the types have different properties");
Expand Down Expand Up @@ -634,7 +634,7 @@ public void
Action act =
() =>
actual.Should().BeEquivalentTo(expectation, opts => opts
.RespectingRuntimeTypes()
.PreferringRuntimeMemberTypes()
.ComparingByMembers<CustomerType>()
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void When_not_all_the_properties_of_the_nested_objects_are_equal_it_shoul
.Should().Match(
@"Expected property subject.Level.Text to be ""Level2"", but ""Level1"" differs near ""1"" (index 5).*" +
"With configuration:*" +
"- Use declared types and members*" +
"- Prefer the declared type of the members*" +
"- Compare enums by value*" +
"- Compare tuples by their properties*" +
"- Compare anonymous types by their properties*" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public void Respecting_the_runtime_type_includes_both_properties_and_fields_incl

// Act
Action act =
() => class1.Should().BeEquivalentTo(class2, opts => opts.RespectingRuntimeTypes());
() => class1.Should().BeEquivalentTo(class2, opts => opts.PreferringRuntimeMemberTypes());

// Assert
act.Should().Throw<XunitException>().Which.Message.Should().Contain("Field1").And.Contain("Property1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ public void

// Act
Action act =
() => class1.Should().BeEquivalentTo(class2, opts => opts.ExcludingProperties().RespectingRuntimeTypes());
() => class1.Should().BeEquivalentTo(class2, opts => opts.ExcludingProperties().PreferringRuntimeMemberTypes());

// Assert
act.Should().NotThrow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public void When_respecting_declared_types_explicit_interface_member_on_interfac
};

// Act
Action action = () => subject.Should().BeEquivalentTo(expected, opt => opt.RespectingDeclaredTypes());
Action action = () => subject.Should().BeEquivalentTo(expected, opt => opt.PreferringDeclaredMemberTypes());

// Assert
action.Should().NotThrow();
Expand All @@ -229,7 +229,7 @@ public void When_respecting_runtime_types_explicit_interface_member_on_interface
subject.VehicleId = 1; // interface member

// Act
Action action = () => subject.Should().BeEquivalentTo(expected, opt => opt.RespectingRuntimeTypes());
Action action = () => subject.Should().BeEquivalentTo(expected, opt => opt.PreferringRuntimeMemberTypes());

// Assert
action.Should().Throw<XunitException>();
Expand All @@ -252,7 +252,7 @@ public void When_respecting_runtime_types_explicit_interface_member_on_interface
};

// Act
Action action = () => subject.Should().BeEquivalentTo(expected, opt => opt.RespectingRuntimeTypes());
Action action = () => subject.Should().BeEquivalentTo(expected, opt => opt.PreferringRuntimeMemberTypes());

// Assert
action.Should().Throw<XunitException>();
Expand Down Expand Up @@ -352,7 +352,7 @@ public void Excluding_an_interface_property_through_inheritance_should_work()
// Act / Assert
actual.Should().BeEquivalentTo(expected, options => options
.Excluding(a => a.Value1)
.RespectingRuntimeTypes());
.PreferringRuntimeMemberTypes());
}

[Fact]
Expand Down Expand Up @@ -380,7 +380,7 @@ public void Including_an_interface_property_through_inheritance_should_work()
// Act / Assert
actual.Should().BeEquivalentTo(expected, options => options
.Including(a => a.Value2)
.RespectingRuntimeTypes());
.PreferringRuntimeMemberTypes());
}

public interface IInterfaceWithTwoProperties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void Run_type_typing_ignores_hidden_properties_even_when_using_a_referenc
expectation.Property = "ExpectedBaseValue";

// Act / Assert
subject.Should().BeEquivalentTo(expectation, o => o.RespectingRuntimeTypes());
subject.Should().BeEquivalentTo(expectation, o => o.PreferringRuntimeMemberTypes());
}

[Fact]
Expand Down Expand Up @@ -247,7 +247,7 @@ public void Run_type_typing_ignores_hidden_fields_even_when_using_a_reference_to
expectation.Field = "ExpectedBaseValue";

// Act / Assert
subject.Should().BeEquivalentTo(expectation, options => options.IncludingFields().RespectingRuntimeTypes());
subject.Should().BeEquivalentTo(expectation, options => options.IncludingFields().PreferringRuntimeMemberTypes());
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public void Public_methods_follow_fluent_syntax()
.IncludingInternalProperties()
.IncludingNestedObjects()
.IncludingProperties()
.RespectingDeclaredTypes()
.RespectingRuntimeTypes()
.PreferringDeclaredMemberTypes()
.PreferringRuntimeMemberTypes()
.ThrowingOnMissingMembers()
.Using(new ExtensibilitySpecs.DoEquivalencyStep(() => { }))
.Using(new MustMatchByNameRule())
Expand Down
8 changes: 4 additions & 4 deletions docs/_pages/objectgraphs.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ This behavior can be configured and you can choose to use run-time types if you
```csharp
Dto orderDto = new OrderDto();

// Use runtime type information of orderDto
// Use the runtime type of the members of orderDto
orderDto.Should().BeEquivalentTo(order, options =>
options.RespectingRuntimeTypes());
options.PreferringRuntimeMemberTypes());

// Use declared type information of orderDto
// Use the declared type information of the members of orderDto
orderDto.Should().BeEquivalentTo(order, options =>
options.RespectingDeclaredTypes());
options.PreferringDeclaredMemberTypes());
```

One exception to this rule is when the declared type is `object`.
Expand Down
1 change: 1 addition & 0 deletions docs/_pages/releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ sidebar:
* Removed `utcNow` overload for `.Monitor<T>()` - [#2629](https://github.com/fluentassertions/fluentassertions/pull/2629)
* The semantics of `BeLowerCased`/`BeUpperCased` have been changed to align with the behavior of `ToLower`/`ToUpper` - [#2660](https://github.com/fluentassertions/fluentassertions/pull/2660)
* Renamed `HaveAttribute` to `HaveAttributeWithValue` on `XElement` - [#2690](https://github.com/fluentassertions/fluentassertions/pull/2690)
* Renamed `RespectingRuntimeTypes` to `PreferringRuntimeMemberTypes` and `RespectingDeclaredTypes` to `PreferringDeclaredMemberTypes` - [#2866](https://github.com/fluentassertions/fluentassertions/pull/2866)

### Breaking Changes (for extensions)
* Add `ForConstraint` to `IAssertionsScope` to support chaining `.ForConstraint()` after `.Then` - [#2324](https://github.com/fluentassertions/fluentassertions/pull/2324)
Expand Down
Loading