/Dev/Contributions/FluentAssertions.Specs/PropertyAssertionSpecs.cs
C# | 743 lines | 457 code | 103 blank | 183 comment | 1 complexity | 33186057b664e7665e26c3b6b0579ba0 MD5 | raw file
- using System;
-
- using FluentAssertions.Assertions;
- using FluentAssertions.Specs;
-
- using Microsoft.VisualStudio.TestTools.UnitTesting;
-
- namespace FluentAssertions.specs
- {
- [TestClass]
- public class PropertyAssertionSpecs
- {
- #region Property Comparison
-
- [TestMethod]
- public void When_two_objects_have_the_same_property_values_it_should_not_throw()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- var subject = new
- {
- Age = 36,
- Birthdate = new DateTime(1973, 9, 20),
- Name = "Dennis"
- };
-
- var other = new
- {
- Age = 36,
- Birthdate = new DateTime(1973, 9, 20),
- Name = "Dennis"
- };
-
- //-----------------------------------------------------------------------------------------------------------
- // Act / Assert
- //-----------------------------------------------------------------------------------------------------------
- subject.ShouldHave().AllProperties().EqualTo(other);
- }
-
- [TestMethod]
- public void When_two_objects_have_the_same_properties_but_a_different_value_it_should_throw()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- var subject = new
- {
- Age = 36,
- };
-
- var other = new
- {
- Age = 37,
- };
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- Action act = () => subject.ShouldHave().AllProperties().EqualTo(other, "because {0} are the same", "they");
-
- //-----------------------------------------------------------------------------------------------------------
- // Assert
- //-----------------------------------------------------------------------------------------------------------
- act.ShouldThrow<AssertFailedException>().WithMessage(
- "Expected property Age to be 37 because they are the same, but found 36.");
- }
-
- [TestMethod]
- public void When_subject_has_a_valid_property_that_is_compared_with_a_null_property_it_should_throw()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- var subject = new
- {
- Name = "Dennis"
- };
-
- var other = new
- {
- Name = (string) null
- };
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- Action act = () => subject.ShouldHave().AllProperties().EqualTo(other);
-
- //-----------------------------------------------------------------------------------------------------------
- // Assert
- //-----------------------------------------------------------------------------------------------------------
- act.ShouldThrow<AssertFailedException>().WithMessage(
- "Expected property Name to be <null>, but found \"Dennis\".");
- }
-
- [TestMethod]
- public void When_two_collection_properties_dont_match_it_should_throw_and_specify_the_difference()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- var subject = new
- {
- Values = new [] { 1, 2, 3 }
- };
-
- var other = new
- {
- Values = new [] { 1, 4, 3 }
- };
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- Action act = () => subject.ShouldHave().AllProperties().EqualTo(other);
-
- //-----------------------------------------------------------------------------------------------------------
- // Assert
- //-----------------------------------------------------------------------------------------------------------
- act.ShouldThrow<AssertFailedException>().WithMessage(
- "Expected property Values to be equal to {1, 4, 3}, but {1, 2, 3} differs at index 1.");
- }
-
-
- [TestMethod]
- public void When_two_objects_have_the_same_properties_with_convertable_values_it_should_not_throw()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- var subject = new
- {
- Age = "37",
- Birthdate = "1973-09-20",
- };
-
- var other = new
- {
- Age = 37,
- Birthdate = new DateTime(1973, 9, 20)
- };
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- Action act = () => subject.ShouldHave().AllProperties().EqualTo(other);
-
- //-----------------------------------------------------------------------------------------------------------
- // Assert
- //-----------------------------------------------------------------------------------------------------------
- act.ShouldNotThrow();
- }
-
- [TestMethod]
- public void When_two_string_properties_do_not_match_it_should_throw_and_state_the_difference()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- var subject = new
- {
- Name = "Dennes"
- };
-
- var other = new
- {
- Name = "Dennis"
- };
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- Action act = () => subject.ShouldHave().Properties(d => d.Name).EqualTo(other);
-
- //-----------------------------------------------------------------------------------------------------------
- // Assert
- //-----------------------------------------------------------------------------------------------------------
- act.ShouldThrow<AssertFailedException>().WithMessage(
- "Expected property Name to be \"Dennis\", but \"Dennes\" differs near \"es\" (index 4).");
- }
-
- #endregion
-
- #region Structure Comparison
-
- [TestMethod]
- public void When_subject_has_a_property_not_available_in_expected_object_it_should_throw()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- var subject = new
- {
- City = "Rijswijk"
- };
-
- var other = new
- {
- };
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- Action act = () => subject.ShouldHave().AllProperties().EqualTo(other);
-
- //-----------------------------------------------------------------------------------------------------------
- // Assert
- //-----------------------------------------------------------------------------------------------------------
- act.ShouldThrow<AssertFailedException>().WithMessage(
- "Subject has property City that the other object does not have.");
- }
-
- [TestMethod]
- public void When_subjects_properties_are_compared_to_null_object_it_should_throw()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- var subject = new
- {
- };
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- Action act = () => subject.ShouldHave().AllProperties().EqualTo(null);
-
- //-----------------------------------------------------------------------------------------------------------
- // Assert
- //-----------------------------------------------------------------------------------------------------------
- act.ShouldThrow<NullReferenceException>().WithMessage(
- "Cannot compare subject's properties with a <null> object.");
- }
-
- [TestMethod]
- public void When_subject_is_null_it_should_throw()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- SomeDto subject = null;
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- Action act = () => subject.ShouldHave().AllProperties().EqualTo(new
- {
- });
-
- //-----------------------------------------------------------------------------------------------------------
- // Assert
- //-----------------------------------------------------------------------------------------------------------
- act.ShouldThrow<NullReferenceException>().WithMessage(
- "Cannot compare the properties of a <null> object.");
- }
-
- [TestMethod]
- public void When_comparing_objects_it_should_ignore_private_properties()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- var subject = new
- {
- Age = 36,
- Birthdate = new DateTime(1973, 9, 20),
- Name = "John",
- };
-
- var other = new
- {
- Age = 36,
- Birthdate = new DateTime(1973, 9, 20),
- Name = "John"
- };
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- Action act = () => subject.ShouldHave().AllProperties().EqualTo(other);
-
- //-----------------------------------------------------------------------------------------------------------
- // Assert
- //-----------------------------------------------------------------------------------------------------------
- act.ShouldNotThrow();
- }
-
- [TestMethod]
- public void When_equally_named_properties_are_type_incompatiblle_it_should_throw()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- var subject = new
- {
- Type = "A",
- };
-
- var other = new
- {
- Type = 36,
- };
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- Action act = () => subject.ShouldHave().AllProperties().EqualTo(other);
-
- //-----------------------------------------------------------------------------------------------------------
- // Assert
- //-----------------------------------------------------------------------------------------------------------
- act
- .ShouldThrow<AssertFailedException>()
- .WithMessage("Expected property Type to be 36, but found \"A\" which is of an incompatible type.");
- }
-
- #endregion
-
- #region Property Inclusion & Exclusion
-
- [TestMethod]
- public void When_specific_properties_of_a_subject_are_compared_with_another_object_it_should_ignore_the_other_properties()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- var subject = new
- {
- Age = 36,
- Birthdate = new DateTime(1973, 9, 20),
- Name = "John"
- };
-
- var customer = new
- {
- Age = 36,
- Birthdate = new DateTime(1973, 9, 20),
- Name = "Dennis"
- };
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- Action act = () => subject.ShouldHave().Properties(d => d.Age, d => d.Birthdate).EqualTo(customer);
-
- //-----------------------------------------------------------------------------------------------------------
- // Assert
- //-----------------------------------------------------------------------------------------------------------
- act.ShouldNotThrow();
- }
-
- [TestMethod]
- public void When_property_is_compared_but_a_nonproperty_is_specified_it_should_throw()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- var dto = new CustomerDto();
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- Action act = () => dto.ShouldHave().Properties(d => d.GetType()).EqualTo(dto);
-
- //-----------------------------------------------------------------------------------------------------------
- // Assert
- //-----------------------------------------------------------------------------------------------------------
- act.ShouldThrow<ArgumentException>().WithMessage(
- "Cannot use <d.GetType()> when a property expression is expected.");
- }
-
- [TestMethod]
- public void When_subject_property_lambda_is_null_it_should_throw()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- var dto = new CustomerDto();
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- Action act = () => dto.ShouldHave().Properties(null).EqualTo(dto);
-
- //-----------------------------------------------------------------------------------------------------------
- // Assert
- //-----------------------------------------------------------------------------------------------------------
- act.ShouldThrow<NullReferenceException>().WithMessage(
- "Expected a property expression, but found <null>.");
- }
-
- [TestMethod]
- public void When_all_properties_but_one_match_and_that_is_expected_it_should_not_throw()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- var dto = new CustomerDto
- {
- Age = 36,
- Birthdate = new DateTime(1973, 9, 20),
- Name = "John"
- };
-
- var customer = new Customer
- {
- Age = 36,
- Birthdate = new DateTime(1973, 9, 20),
- Name = "Dennis"
- };
-
- //-----------------------------------------------------------------------------------------------------------
- // Act / Assert
- //-----------------------------------------------------------------------------------------------------------
- dto.ShouldHave().AllPropertiesBut(d => d.Name).EqualTo(customer);
- }
-
- [TestMethod]
- public void When_comparing_objects_by_their_shared_properties_and_all_match_it_should_not_throw()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- var dto = new CustomerDto
- {
- Version = 2,
- Age = 36,
- Birthdate = new DateTime(1973, 9, 20),
- Name = "John"
- };
-
- var customer = new Customer
- {
- Id = 1,
- Version = 2,
- Age = 36,
- Birthdate = new DateTime(1973, 9, 20),
- Name = "John"
- };
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- Action act = () => customer.ShouldHave().SharedProperties().EqualTo(dto);
-
- //-----------------------------------------------------------------------------------------------------------
- // Assert
- //-----------------------------------------------------------------------------------------------------------
- act.ShouldNotThrow();
- }
-
- [TestMethod]
- public void When_comparing_objects_by_their_properties_and_no_properties_have_been_specified_it_should_throw()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- var dto = new CustomerDto();
- var customer = new Customer();
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- Action act = () => dto.ShouldHave().EqualTo(customer);
-
- //-----------------------------------------------------------------------------------------------------------
- // Assert
- //-----------------------------------------------------------------------------------------------------------
- act.ShouldThrow<InvalidOperationException>().WithMessage(
- "Please specify some properties to include in the comparison.");
- }
-
- #endregion
-
- #region Recursive property validation
-
- [TestMethod]
- public void When_recursing_on_incompatible_properties_that_have_equal_child_properties_it_should_succeed()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- var root = new Root
- {
- Text = "Root",
- Level = new Level1
- {
- Text = "Level1",
- Level = new Level2
- {
- Text = "Level2",
- }
- }
- };
-
- var equalRootDto = new RootDto
- {
- Text = "Root",
- Level = new Level1Dto
- {
- Text = "Level1",
- Level = new Level2Dto
- {
- Text = "Level2",
- }
- }
- };
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- Action act = () =>
- root.ShouldHave().AllProperties().RecurseIfIncompatible().EqualTo(equalRootDto);
-
- //-----------------------------------------------------------------------------------------------------------
- // Assert
- //-----------------------------------------------------------------------------------------------------------
- act.ShouldNotThrow();
- }
-
- [TestMethod]
- public void When_not_recursing_on_incompatible_properties_that_have_equal_child_properties_it_should_throw()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- var root = new Root
- {
- Text = "Root",
- Level = new Level1
- {
- Text = "Level1",
- }
- };
-
- var equalRootDto = new RootDto
- {
- Text = "Root",
- Level = new Level1Dto
- {
- Text = "Level1",
- }
- };
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- Action act = () =>
- root.ShouldHave().AllProperties().EqualTo(equalRootDto);
-
- //-----------------------------------------------------------------------------------------------------------
- // Assert
- //-----------------------------------------------------------------------------------------------------------
- act
- .ShouldThrow<AssertFailedException>()
- .WithMessage("Expected property Level to be \r\n\r\nFluentAssertions.specs.Level1Dto\r\n" +
- "{\r\n Level = <null>\r\n Text = \"Level1\"\r\n}" +
- ", but found \r\n\r\nFluentAssertions.specs.Level1\r\n" +
- "{\r\n Level = <null>\r\n Text = \"Level1\"\r\n}" +
- " which is of an incompatible type.");
- }
-
- [TestMethod]
- public void When_recursing_on_incompatible_properties_that_have_differences_in_their_child_properties_it_should_throw()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- var root = new Root
- {
- Text = "Root",
- Level = new Level1
- {
- Text = "Level1",
- Level = new Level2
- {
- Text = "Level2",
- }
- }
- };
-
- var rootDto = new RootDto
- {
- Text = "Root",
- Level = new Level1Dto
- {
- Text = "Level1",
- Level = new Level2Dto
- {
- Text = "A wrong text value",
- }
- }
- };
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- Action act = () =>
- root.ShouldHave().AllProperties().RecurseIfIncompatible().EqualTo(rootDto);
-
- //-----------------------------------------------------------------------------------------------------------
- // Assert
- //-----------------------------------------------------------------------------------------------------------
- act
- .ShouldThrow<AssertFailedException>()
- .WithMessage("Expected property Level to have all properties equal to \r\n\r\n" +
- "FluentAssertions.specs.Level1Dto\r\n" +
- "{\r\n Level = FluentAssertions.specs.Level2Dto" +
- "\r\n {\r\n Text = \"A wrong text value\"\r\n }\r\n Text = \"Level1\"\r\n}" +
- ", but found \r\n\r\n" +
- "FluentAssertions.specs.Level1\r\n" +
- "{\r\n Level = FluentAssertions.specs.Level2" +
- "\r\n {\r\n Text = \"Level2\"\r\n }\r\n Text = \"Level1\"\r\n}.");
- }
-
- [TestMethod]
- public void When_recursing_on_incompatible_properties_that_have_cyclic_references_it_should_throw()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- var cyclicRoot = new CyclicRoot
- {
- Text = "Root",
- };
- cyclicRoot.Level = new CyclicLevel1
- {
- Text = "Level1",
- Root = cyclicRoot
- };
-
- var cyclicRootDto = new CyclicRootDto
- {
- Text = "Root",
- };
- cyclicRootDto.Level = new CyclicLevel1Dto
- {
- Text = "Level1",
- Root = cyclicRootDto
- };
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- Action act = () =>
- cyclicRoot.ShouldHave().AllProperties().RecurseIfIncompatible().EqualTo(cyclicRootDto);
-
- //-----------------------------------------------------------------------------------------------------------
- // Assert
- //-----------------------------------------------------------------------------------------------------------
- act
- .ShouldThrow<AssertFailedException>()
- .WithMessage("Expected property Level to be FluentAssertions.specs.CyclicLevel1Dto, but found " +
- "FluentAssertions.specs.CyclicLevel1 which is of an incompatible type and has a cyclic reference.");
- }
-
- #endregion
- }
-
- internal class Customer : Entity
- {
- public string Name { get; set; }
- public int Age { get; set; }
- public DateTime Birthdate { get; set; }
- public long Id { get; set; }
- }
-
- public class Entity
- {
- internal long Version { get; set; }
- }
-
- internal class CustomerDto
- {
- public long Version { get; set; }
- public string Name { get; set; }
- public int Age { get; set; }
- public DateTime Birthdate { get; set; }
- }
-
- #region Nested classes for comparison
-
- internal class Root
- {
- public string Text { get; set; }
- public Level1 Level { get; set; }
- }
-
- internal class Level1
- {
- public string Text { get; set; }
- public Level2 Level { get; set; }
- }
-
- internal class Level2
- {
- public string Text { get; set; }
- }
-
- internal class RootDto
- {
- public string Text { get; set; }
- public Level1Dto Level { get; set; }
- }
-
- internal class Level1Dto
- {
- public string Text { get; set; }
- public Level2Dto Level { get; set; }
- }
-
- internal class Level2Dto
- {
- public string Text { get; set; }
- }
-
- internal class CyclicRoot
- {
- public string Text { get; set; }
- public CyclicLevel1 Level { get; set; }
- }
-
- internal class CyclicLevel1
- {
- public string Text { get; set; }
- public CyclicRoot Root { get; set; }
- }
-
- internal class CyclicRootDto
- {
- public string Text { get; set; }
- public CyclicLevel1Dto Level { get; set; }
- }
-
- internal class CyclicLevel1Dto
- {
- public string Text { get; set; }
- public CyclicRootDto Root { get; set; }
- }
-
- #endregion
- }