Skip to content

Commit

Permalink
Add tests to prevent breaking changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jnyrup committed Sep 6, 2018
1 parent 366fa91 commit 68ca857
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions Tests/Shared.Specs/CollectionEquivalencySpecs.cs
Expand Up @@ -14,6 +14,17 @@ namespace FluentAssertions.Specs
{
public class CollectionEquivalencySpecs
{
public interface IInterface
{
int InterfaceProperty { get; set; }
}

public class MyClass : IInterface
{
public int InterfaceProperty { get; set; }
public int ClassProperty { get; set; }
}

public class SubDummy
{
public SubDummy(int id)
Expand Down Expand Up @@ -181,6 +192,94 @@ public void Add(Guid userId, params string[] roles)
}
}

[Fact]
public void When_the_expectation_is_an_array_of_interface_type_it_should_respect_runtime_types()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var actual = new IInterface[] { new MyClass() { InterfaceProperty = 1, ClassProperty = 42 } };
var expected = new IInterface[] { new MyClass() { InterfaceProperty = 1, ClassProperty = 1337 } };

//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = () => actual.Should().BeEquivalentTo(expected);

//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>(@"Fluent Assertions 5.x.x has the params object[] overload,
which discards the compile time type.");
}

[Fact]
public void When_the_expectation_has_fewer_dimensions_than_a_multi_dimensional_subject_it_should_fail()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
object objectA = new object();
object objectB = new object();

var actual = new object[][] { new object[] { objectA, objectB } };
var expected = actual[0];

//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = () => actual.Should().BeEquivalentTo(expected);

//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>(@"Fluent Assertions 5.x.x has the params object[] overload,
which cannot distinguish an array of objects from an element which is an array of objects.");
}

[Fact]
public void When_the_expectation_has_fewer_dimensions_than_a_one_dimensional_subject_it_should_succeed()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
object objectA = new object();

var actual = new object[] { objectA };
var expected = actual[0];

//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = () => actual.Should().BeEquivalentTo(expected);

//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().NotThrow<XunitException>(@"Fluent Assertions 5.x.x has the params object[] overload,
which treats a single object as an element of a list.");
}

[Fact]
public void When_the_expectation_is_an_array_of_anonymous_types_it_should_respect_runtime_types()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var actual = new[] { new { A = 1, B = 2 }, new { A = 1, B = 2 } };
var expected = new object[] { new { A = 1 }, new { B = 2 } };

//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = () => actual.Should().BeEquivalentTo(expected);

//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().NotThrow();
}

[Fact]
public void When_a_byte_array_does_not_match_strictly_it_should_throw()
{
Expand Down

0 comments on commit 68ca857

Please sign in to comment.