From 28883bad06ac61b732b1043c27451fca2cc4b931 Mon Sep 17 00:00:00 2001 From: Lukas Gasselsberger Date: Sat, 23 Apr 2022 21:03:01 +0200 Subject: [PATCH 1/2] Seperate all `Xml` assertions into nested classes --- .../Xml/XAttributeAssertionSpecs.cs | 603 ++--- .../Xml/XDocumentAssertionSpecs.cs | 2027 ++++++++-------- .../Xml/XElementAssertionSpecs.cs | 2142 +++++++++-------- .../Xml/XmlElementAssertionSpecs.cs | 844 +++---- .../Xml/XmlNodeAssertionSpecs.cs | 1065 ++++---- 5 files changed, 3347 insertions(+), 3334 deletions(-) diff --git a/Tests/FluentAssertions.Specs/Xml/XAttributeAssertionSpecs.cs b/Tests/FluentAssertions.Specs/Xml/XAttributeAssertionSpecs.cs index f0958a42f9..46b318cef2 100644 --- a/Tests/FluentAssertions.Specs/Xml/XAttributeAssertionSpecs.cs +++ b/Tests/FluentAssertions.Specs/Xml/XAttributeAssertionSpecs.cs @@ -7,319 +7,322 @@ namespace FluentAssertions.Specs.Xml { public class XAttributeAssertionSpecs { - #region Be / NotBe - - [Fact] - public void When_asserting_an_xml_attribute_is_equal_to_the_same_xml_attribute_it_should_succeed() - { - // Arrange - var attribute = new XAttribute("name", "value"); - var sameXAttribute = new XAttribute("name", "value"); - - // Act - Action act = () => - attribute.Should().Be(sameXAttribute); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_an_xml_attribute_is_equal_to_a_different_xml_attribute_it_should_fail_with_descriptive_message() - { - // Arrange - var theAttribute = new XAttribute("name", "value"); - var otherAttribute = new XAttribute("name2", "value"); - - // Act - Action act = () => - theAttribute.Should().Be(otherAttribute, "because we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw().WithMessage( - $"Expected theAttribute to be {otherAttribute} because we want to test the failure message, but found {theAttribute}."); - } - - [Fact] - public void When_both_subject_and_expected_are_null_it_succeeds() - { - XAttribute theAttribute = null; - - // Act - Action act = () => theAttribute.Should().Be(null); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_the_expected_attribute_is_null_then_it_fails() - { - XAttribute theAttribute = null; - - // Act - Action act = () => - theAttribute.Should().Be(new XAttribute("name", "value"), "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected theAttribute to be name=\"value\" *failure message*, but found ."); - } - - [Fact] - public void When_the_attribute_is_expected_to_equal_null_it_fails() - { - XAttribute theAttribute = new XAttribute("name", "value"); - - // Act - Action act = () => theAttribute.Should().Be(null, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected theAttribute to be *failure message*, but found name=\"value\"."); - } - - [Fact] - public void When_asserting_an_xml_attribute_is_not_equal_to_a_different_xml_attribute_it_should_succeed() - { - // Arrange - var attribute = new XAttribute("name", "value"); - var otherXAttribute = new XAttribute("name2", "value"); - - // Act - Action act = () => - attribute.Should().NotBe(otherXAttribute); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_an_xml_attribute_is_not_equal_to_the_same_xml_attribute_it_should_throw() - { - // Arrange - var theAttribute = new XAttribute("name", "value"); - var sameXAttribute = theAttribute; - - // Act - Action act = () => - theAttribute.Should().NotBe(sameXAttribute); - - // Assert - act.Should().Throw().WithMessage( - "Did not expect theAttribute to be name=\"value\"."); - } - - [Fact] - public void When_asserting_an_xml_attribute_is_not_equal_to_the_same_xml_attribute_it_should_throw_with_descriptive_message() - { - // Arrange - var theAttribute = new XAttribute("name", "value"); - var sameAttribute = theAttribute; - - // Act - Action act = () => - theAttribute.Should().NotBe(sameAttribute, "because we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw().WithMessage( - $"Did not expect theAttribute to be {sameAttribute} because we want to test the failure message."); - } - - [Fact] - public void When_a_null_attribute_is_not_supposed_to_be_an_attribute_it_succeeds() - { - // Arrange - XAttribute theAttribute = null; - - // Act - Action act = () => theAttribute.Should().NotBe(new XAttribute("name", "value")); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_an_attribute_is_not_supposed_to_be_null_it_succeeds() - { - // Arrange - XAttribute theAttribute = new XAttribute("name", "value"); - - // Act - Action act = () => theAttribute.Should().NotBe(null); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_a_null_attribute_is_not_supposed_to_be_null_it_fails() - { - // Arrange - XAttribute theAttribute = null; - - // Act - Action act = () => theAttribute.Should().NotBe(null, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Did not expect theAttribute to be *failure message*."); - } - - #endregion - - #region BeNull / NotBeNull - - [Fact] - public void When_asserting_a_null_xml_attribute_is_null_it_should_succeed() - { - // Arrange - XAttribute attribute = null; - - // Act - Action act = () => - attribute.Should().BeNull(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_non_null_xml_attribute_is_null_it_should_fail() - { - // Arrange - var theAttribute = new XAttribute("name", "value"); - - // Act - Action act = () => - theAttribute.Should().BeNull(); - - // Assert - act.Should().Throw().WithMessage( - "Expected theAttribute to be , but found name=\"value\"."); - } - - [Fact] - public void When_asserting_a_non_null_xml_attribute_is_null_it_should_fail_with_descriptive_message() - { - // Arrange - var theAttribute = new XAttribute("name", "value"); - - // Act - Action act = () => - theAttribute.Should().BeNull("because we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw().WithMessage( - $"Expected theAttribute to be because we want to test the failure message, but found {theAttribute}."); - } - - [Fact] - public void When_asserting_a_non_null_xml_attribute_is_not_null_it_should_succeed() - { - // Arrange - var attribute = new XAttribute("name", "value"); - - // Act - Action act = () => - attribute.Should().NotBeNull(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_null_xml_attribute_is_not_null_it_should_fail() + public class Be { - // Arrange - XAttribute theAttribute = null; - - // Act - Action act = () => - theAttribute.Should().NotBeNull(); - - // Assert - act.Should().Throw().WithMessage( - "Expected theAttribute not to be ."); - } - - [Fact] - public void When_asserting_a_null_xml_attribute_is_not_null_it_should_fail_with_descriptive_message() - { - // Arrange - XAttribute theAttribute = null; - - // Act - Action act = () => - theAttribute.Should().NotBeNull("because we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw().WithMessage( - "Expected theAttribute not to be because we want to test the failure message."); + [Fact] + public void When_asserting_an_xml_attribute_is_equal_to_the_same_xml_attribute_it_should_succeed() + { + // Arrange + var attribute = new XAttribute("name", "value"); + var sameXAttribute = new XAttribute("name", "value"); + + // Act + Action act = () => + attribute.Should().Be(sameXAttribute); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_an_xml_attribute_is_equal_to_a_different_xml_attribute_it_should_fail_with_descriptive_message() + { + // Arrange + var theAttribute = new XAttribute("name", "value"); + var otherAttribute = new XAttribute("name2", "value"); + + // Act + Action act = () => + theAttribute.Should().Be(otherAttribute, "because we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw().WithMessage( + $"Expected theAttribute to be {otherAttribute} because we want to test the failure message, but found {theAttribute}."); + } + + [Fact] + public void When_both_subject_and_expected_are_null_it_succeeds() + { + XAttribute theAttribute = null; + + // Act + Action act = () => theAttribute.Should().Be(null); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_the_expected_attribute_is_null_then_it_fails() + { + XAttribute theAttribute = null; + + // Act + Action act = () => + theAttribute.Should().Be(new XAttribute("name", "value"), "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected theAttribute to be name=\"value\" *failure message*, but found ."); + } + + [Fact] + public void When_the_attribute_is_expected_to_equal_null_it_fails() + { + XAttribute theAttribute = new XAttribute("name", "value"); + + // Act + Action act = () => theAttribute.Should().Be(null, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected theAttribute to be *failure message*, but found name=\"value\"."); + } } - #endregion - - #region HaveValue - - [Fact] - public void When_asserting_attribute_has_a_specific_value_and_it_does_it_should_succeed() + public class NotBe { - // Arrange - var attribute = new XAttribute("age", "36"); - - // Act - Action act = () => - attribute.Should().HaveValue("36"); - - // Assert - act.Should().NotThrow(); + [Fact] + public void When_asserting_an_xml_attribute_is_not_equal_to_a_different_xml_attribute_it_should_succeed() + { + // Arrange + var attribute = new XAttribute("name", "value"); + var otherXAttribute = new XAttribute("name2", "value"); + + // Act + Action act = () => + attribute.Should().NotBe(otherXAttribute); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_an_xml_attribute_is_not_equal_to_the_same_xml_attribute_it_should_throw() + { + // Arrange + var theAttribute = new XAttribute("name", "value"); + var sameXAttribute = theAttribute; + + // Act + Action act = () => + theAttribute.Should().NotBe(sameXAttribute); + + // Assert + act.Should().Throw().WithMessage( + "Did not expect theAttribute to be name=\"value\"."); + } + + [Fact] + public void When_asserting_an_xml_attribute_is_not_equal_to_the_same_xml_attribute_it_should_throw_with_descriptive_message() + { + // Arrange + var theAttribute = new XAttribute("name", "value"); + var sameAttribute = theAttribute; + + // Act + Action act = () => + theAttribute.Should().NotBe(sameAttribute, "because we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw().WithMessage( + $"Did not expect theAttribute to be {sameAttribute} because we want to test the failure message."); + } + + [Fact] + public void When_a_null_attribute_is_not_supposed_to_be_an_attribute_it_succeeds() + { + // Arrange + XAttribute theAttribute = null; + + // Act + Action act = () => theAttribute.Should().NotBe(new XAttribute("name", "value")); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_an_attribute_is_not_supposed_to_be_null_it_succeeds() + { + // Arrange + XAttribute theAttribute = new XAttribute("name", "value"); + + // Act + Action act = () => theAttribute.Should().NotBe(null); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_a_null_attribute_is_not_supposed_to_be_null_it_fails() + { + // Arrange + XAttribute theAttribute = null; + + // Act + Action act = () => theAttribute.Should().NotBe(null, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Did not expect theAttribute to be *failure message*."); + } } - [Fact] - public void When_asserting_attribute_has_a_specific_value_but_it_has_a_different_value_it_should_throw() + public class BeNull { - // Arrange - var theAttribute = new XAttribute("age", "36"); - - // Act - Action act = () => - theAttribute.Should().HaveValue("16"); - - // Assert - act.Should().Throw().WithMessage( - "Expected theAttribute \"age\" to have value \"16\", but found \"36\"."); + [Fact] + public void When_asserting_a_null_xml_attribute_is_null_it_should_succeed() + { + // Arrange + XAttribute attribute = null; + + // Act + Action act = () => + attribute.Should().BeNull(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_non_null_xml_attribute_is_null_it_should_fail() + { + // Arrange + var theAttribute = new XAttribute("name", "value"); + + // Act + Action act = () => + theAttribute.Should().BeNull(); + + // Assert + act.Should().Throw().WithMessage( + "Expected theAttribute to be , but found name=\"value\"."); + } + + [Fact] + public void When_asserting_a_non_null_xml_attribute_is_null_it_should_fail_with_descriptive_message() + { + // Arrange + var theAttribute = new XAttribute("name", "value"); + + // Act + Action act = () => + theAttribute.Should().BeNull("because we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw().WithMessage( + $"Expected theAttribute to be because we want to test the failure message, but found {theAttribute}."); + } } - [Fact] - public void When_asserting_attribute_has_a_specific_value_but_it_has_a_different_value_it_should_throw_with_descriptive_message() + public class NotBeNull { - // Arrange - var theAttribute = new XAttribute("age", "36"); - - // Act - Action act = () => - theAttribute.Should().HaveValue("16", "because we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw().WithMessage( - "Expected theAttribute \"age\" to have value \"16\" because we want to test the failure message, but found \"36\"."); + [Fact] + public void When_asserting_a_non_null_xml_attribute_is_not_null_it_should_succeed() + { + // Arrange + var attribute = new XAttribute("name", "value"); + + // Act + Action act = () => + attribute.Should().NotBeNull(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_null_xml_attribute_is_not_null_it_should_fail() + { + // Arrange + XAttribute theAttribute = null; + + // Act + Action act = () => + theAttribute.Should().NotBeNull(); + + // Assert + act.Should().Throw().WithMessage( + "Expected theAttribute not to be ."); + } + + [Fact] + public void When_asserting_a_null_xml_attribute_is_not_null_it_should_fail_with_descriptive_message() + { + // Arrange + XAttribute theAttribute = null; + + // Act + Action act = () => + theAttribute.Should().NotBeNull("because we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw().WithMessage( + "Expected theAttribute not to be because we want to test the failure message."); + } } - [Fact] - public void When_an_attribute_is_null_then_have_value_should_fail() + public class HaveValue { - XAttribute theAttribute = null; - - // Act - Action act = () => - theAttribute.Should().HaveValue("value", "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected the attribute to have value \"value\" *failure message*, but theAttribute is ."); + [Fact] + public void When_asserting_attribute_has_a_specific_value_and_it_does_it_should_succeed() + { + // Arrange + var attribute = new XAttribute("age", "36"); + + // Act + Action act = () => + attribute.Should().HaveValue("36"); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_attribute_has_a_specific_value_but_it_has_a_different_value_it_should_throw() + { + // Arrange + var theAttribute = new XAttribute("age", "36"); + + // Act + Action act = () => + theAttribute.Should().HaveValue("16"); + + // Assert + act.Should().Throw().WithMessage( + "Expected theAttribute \"age\" to have value \"16\", but found \"36\"."); + } + + [Fact] + public void When_asserting_attribute_has_a_specific_value_but_it_has_a_different_value_it_should_throw_with_descriptive_message() + { + // Arrange + var theAttribute = new XAttribute("age", "36"); + + // Act + Action act = () => + theAttribute.Should().HaveValue("16", "because we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw().WithMessage( + "Expected theAttribute \"age\" to have value \"16\" because we want to test the failure message, but found \"36\"."); + } + + [Fact] + public void When_an_attribute_is_null_then_have_value_should_fail() + { + XAttribute theAttribute = null; + + // Act + Action act = () => + theAttribute.Should().HaveValue("value", "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected the attribute to have value \"value\" *failure message*, but theAttribute is ."); + } } - - #endregion } } diff --git a/Tests/FluentAssertions.Specs/Xml/XDocumentAssertionSpecs.cs b/Tests/FluentAssertions.Specs/Xml/XDocumentAssertionSpecs.cs index 837a6b0422..611b108557 100644 --- a/Tests/FluentAssertions.Specs/Xml/XDocumentAssertionSpecs.cs +++ b/Tests/FluentAssertions.Specs/Xml/XDocumentAssertionSpecs.cs @@ -9,1320 +9,1323 @@ namespace FluentAssertions.Specs.Xml { public class XDocumentAssertionSpecs { - #region Be / NotBe - - [Fact] - public void When_asserting_a_xml_document_is_equal_to_the_same_xml_document_it_should_succeed() - { - // Arrange - var document = new XDocument(); - var sameXDocument = document; - - // Act - Action act = () => - document.Should().Be(sameXDocument); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_xml_document_is_equal_to_a_different_xml_document_it_should_fail() + public class Be { - // Arrange - var theDocument = new XDocument(); - var otherXDocument = new XDocument(); + [Fact] + public void When_asserting_a_xml_document_is_equal_to_the_same_xml_document_it_should_succeed() + { + // Arrange + var document = new XDocument(); + var sameXDocument = document; - // Act - Action act = () => - theDocument.Should().Be(otherXDocument); + // Act + Action act = () => + document.Should().Be(sameXDocument); - // Assert - act.Should().Throw().WithMessage( - "Expected theDocument to be [XML document without root element], but found [XML document without root element]."); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_the_expected_element_is_null_it_fails() - { - // Arrange - XDocument theDocument = null; + [Fact] + public void When_asserting_a_xml_document_is_equal_to_a_different_xml_document_it_should_fail() + { + // Arrange + var theDocument = new XDocument(); + var otherXDocument = new XDocument(); - // Act - Action act = () => theDocument.Should().Be(new XDocument(), "we want to test the failure {0}", "message"); + // Act + Action act = () => + theDocument.Should().Be(otherXDocument); - // Assert - act.Should().Throw().WithMessage( - "Expected theDocument to be [XML document without root element] *failure message*, but found ."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected theDocument to be [XML document without root element], but found [XML document without root element]."); + } - [Fact] - public void When_both_subject_and_expected_documents_are_null_it_succeeds() - { - // Arrange - XDocument theDocument = null; + [Fact] + public void When_the_expected_element_is_null_it_fails() + { + // Arrange + XDocument theDocument = null; - // Act - Action act = () => theDocument.Should().Be(null); + // Act + Action act = () => theDocument.Should().Be(new XDocument(), "we want to test the failure {0}", "message"); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().Throw().WithMessage( + "Expected theDocument to be [XML document without root element] *failure message*, but found ."); + } - [Fact] - public void When_a_document_is_expected_to_equal_null_it_fails() - { - // Arrange - XDocument theDocument = new XDocument(); + [Fact] + public void When_both_subject_and_expected_documents_are_null_it_succeeds() + { + // Arrange + XDocument theDocument = null; - // Act - Action act = () => theDocument.Should().Be(null, "we want to test the failure {0}", "message"); + // Act + Action act = () => theDocument.Should().Be(null); - // Assert - act.Should().Throw().WithMessage( - "Expected theDocument to be *failure message*, but found [XML document without root element]."); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_asserting_a_xml_document_is_equal_to_a_different_xml_document_it_should_fail_with_descriptive_message() - { - // Arrange - var theDocument = XDocument.Parse(""); - var otherXDocument = XDocument.Parse(""); + [Fact] + public void When_a_document_is_expected_to_equal_null_it_fails() + { + // Arrange + XDocument theDocument = new XDocument(); - // Act - Action act = () => - theDocument.Should().Be(otherXDocument, "because we want to test the failure {0}", "message"); + // Act + Action act = () => theDocument.Should().Be(null, "we want to test the failure {0}", "message"); - // Assert - act.Should().Throw().WithMessage( - "Expected theDocument to be because we want to test the failure message, but found ."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected theDocument to be *failure message*, but found [XML document without root element]."); + } - [Fact] - public void When_asserting_a_xml_document_is_not_equal_to_a_different_xml_document_it_should_succeed() - { - // Arrange - var document = new XDocument(); - var otherXDocument = new XDocument(); + [Fact] + public void When_asserting_a_xml_document_is_equal_to_a_different_xml_document_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = XDocument.Parse(""); + var otherXDocument = XDocument.Parse(""); - // Act - Action act = () => - document.Should().NotBe(otherXDocument); + // Act + Action act = () => + theDocument.Should().Be(otherXDocument, "because we want to test the failure {0}", "message"); - // Assert - act.Should().NotThrow(); + // Assert + act.Should().Throw().WithMessage( + "Expected theDocument to be because we want to test the failure message, but found ."); + } } - [Fact] - public void When_asserting_a_xml_document_is_not_equal_to_the_same_xml_document_it_should_fail() + public class NotBe { - // Arrange - var theDocument = new XDocument(); - var sameXDocument = theDocument; - - // Act - Action act = () => - theDocument.Should().NotBe(sameXDocument); + [Fact] + public void When_asserting_a_xml_document_is_not_equal_to_a_different_xml_document_it_should_succeed() + { + // Arrange + var document = new XDocument(); + var otherXDocument = new XDocument(); - // Assert - act.Should().Throw().WithMessage( - "Did not expect theDocument to be [XML document without root element]."); - } + // Act + Action act = () => + document.Should().NotBe(otherXDocument); - [Fact] - public void When_a_null_document_is_not_supposed_to_be_a_document_it_succeeds() - { - // Arrange - XDocument theDocument = null; + // Assert + act.Should().NotThrow(); + } - // Act - Action act = () => theDocument.Should().NotBe(new XDocument()); + [Fact] + public void When_asserting_a_xml_document_is_not_equal_to_the_same_xml_document_it_should_fail() + { + // Arrange + var theDocument = new XDocument(); + var sameXDocument = theDocument; - // Assert - act.Should().NotThrow(); - } + // Act + Action act = () => + theDocument.Should().NotBe(sameXDocument); - [Fact] - public void When_a_document_is_not_supposed_to_be_null_it_succeeds() - { - // Arrange - XDocument theDocument = new XDocument(); + // Assert + act.Should().Throw().WithMessage( + "Did not expect theDocument to be [XML document without root element]."); + } - // Act - Action act = () => theDocument.Should().NotBe(null); + [Fact] + public void When_a_null_document_is_not_supposed_to_be_a_document_it_succeeds() + { + // Arrange + XDocument theDocument = null; - // Assert - act.Should().NotThrow(); - } + // Act + Action act = () => theDocument.Should().NotBe(new XDocument()); - [Fact] - public void When_a_null_document_is_not_supposed_to_be_equal_to_null_it_fails() - { - // Arrange - XDocument theDocument = null; + // Assert + act.Should().NotThrow(); + } - // Act - Action act = () => theDocument.Should().NotBe(null, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Did not expect theDocument to be *failure message*."); - } + [Fact] + public void When_a_document_is_not_supposed_to_be_null_it_succeeds() + { + // Arrange + XDocument theDocument = new XDocument(); - [Fact] - public void When_asserting_a_xml_document_is_not_equal_to_the_same_xml_document_it_should_fail_with_descriptive_message() - { - // Arrange - var theDocument = XDocument.Parse(""); - var sameXDocument = theDocument; + // Act + Action act = () => theDocument.Should().NotBe(null); - // Act - Action act = () => - theDocument.Should().NotBe(sameXDocument, "we want to test the failure {0}", "message"); + // Assert + act.Should().NotThrow(); + } - // Assert - act.Should().Throw().WithMessage( - "Did not expect theDocument to be because we want to test the failure message."); - } + [Fact] + public void When_a_null_document_is_not_supposed_to_be_equal_to_null_it_fails() + { + // Arrange + XDocument theDocument = null; - #endregion + // Act + Action act = () => theDocument.Should().NotBe(null, "we want to test the failure {0}", "message"); - #region BeEquivalentTo / NotBeEquivalentTo + // Assert + act.Should().Throw() + .WithMessage("Did not expect theDocument to be *failure message*."); + } - [Fact] - public void When_asserting_a_xml_document_is_equivalent_to_the_same_xml_document_it_should_succeed() - { - // Arrange - var document = new XDocument(); - var sameXDocument = document; + [Fact] + public void When_asserting_a_xml_document_is_not_equal_to_the_same_xml_document_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = XDocument.Parse(""); + var sameXDocument = theDocument; - // Act - Action act = () => - document.Should().BeEquivalentTo(sameXDocument); + // Act + Action act = () => + theDocument.Should().NotBe(sameXDocument, "we want to test the failure {0}", "message"); - // Assert - act.Should().NotThrow(); + // Assert + act.Should().Throw().WithMessage( + "Did not expect theDocument to be because we want to test the failure message."); + } } - [Fact] - public void When_asserting_a_xml_selfclosing_document_is_equivalent_to_a_different_xml_document_with_same_structure_it_should_succeed() + public class BeEquivalentTo { - // Arrange - var document = XDocument.Parse(""); - var otherXDocument = XDocument.Parse(""); - - // Act - Action act = () => - document.Should().BeEquivalentTo(otherXDocument); - - // Assert - act.Should().NotThrow(); - } + [Fact] + public void When_asserting_a_xml_document_is_equivalent_to_the_same_xml_document_it_should_succeed() + { + // Arrange + var document = new XDocument(); + var sameXDocument = document; - [Fact] - public void When_asserting_a_xml_document_is_equivalent_to_a_different_xml_document_with_same_structure_it_should_succeed() - { - // Arrange - var document = XDocument.Parse(""); - var otherXDocument = XDocument.Parse(""); + // Act + Action act = () => + document.Should().BeEquivalentTo(sameXDocument); - // Act - Action act = () => - document.Should().BeEquivalentTo(otherXDocument); + // Assert + act.Should().NotThrow(); + } - // Assert - act.Should().NotThrow(); - } + [Fact] + public void When_asserting_a_xml_selfclosing_document_is_equivalent_to_a_different_xml_document_with_same_structure_it_should_succeed() + { + // Arrange + var document = XDocument.Parse(""); + var otherXDocument = XDocument.Parse(""); - [Fact] - public void When_asserting_a_xml_document_is_equivalent_to_a_xml_document_with_elements_missing_it_should_fail() - { - // Arrange - var theDocument = XDocument.Parse(""); - var otherXDocument = XDocument.Parse(""); + // Act + Action act = () => + document.Should().BeEquivalentTo(otherXDocument); - // Act - Action act = () => - theDocument.Should().BeEquivalentTo(otherXDocument); + // Assert + act.Should().NotThrow(); + } - // Assert - act.Should().Throw().WithMessage( - "Expected EndElement \"parent\" in theDocument at \"/parent\", but found Element \"child2\"."); - } + [Fact] + public void When_asserting_a_xml_document_is_equivalent_to_a_different_xml_document_with_same_structure_it_should_succeed() + { + // Arrange + var document = XDocument.Parse(""); + var otherXDocument = XDocument.Parse(""); - [Fact] - public void When_asserting_a_xml_document_is_equivalent_to_a_different_xml_document_with_extra_elements_it_should_fail() - { - // Arrange - var theDocument = XDocument.Parse(""); - var expected = XDocument.Parse(""); + // Act + Action act = () => + document.Should().BeEquivalentTo(otherXDocument); - // Act - Action act = () => - theDocument.Should().BeEquivalentTo(expected); + // Assert + act.Should().NotThrow(); + } - // Assert - act.Should().Throw().WithMessage( - "Expected Element \"child2\" in theDocument at \"/parent\", but found EndElement \"parent\"."); - } + [Fact] + public void When_asserting_a_xml_document_is_equivalent_to_a_xml_document_with_elements_missing_it_should_fail() + { + // Arrange + var theDocument = XDocument.Parse(""); + var otherXDocument = XDocument.Parse(""); - [Fact] - public void When_asserting_a_xml_document_with_selfclosing_child_is_equivalent_to_a_different_xml_document_with_subchild_child_it_should_fail() - { - // Arrange - var theDocument = XDocument.Parse(""); - var otherXDocument = XDocument.Parse(""); + // Act + Action act = () => + theDocument.Should().BeEquivalentTo(otherXDocument); - // Act - Action act = () => - theDocument.Should().BeEquivalentTo(otherXDocument); + // Assert + act.Should().Throw().WithMessage( + "Expected EndElement \"parent\" in theDocument at \"/parent\", but found Element \"child2\"."); + } - // Assert - act.Should().Throw().WithMessage( - "Expected Element \"child\" in theDocument at \"/parent/child\", but found EndElement \"parent\"."); - } + [Fact] + public void When_asserting_a_xml_document_is_equivalent_to_a_different_xml_document_with_extra_elements_it_should_fail() + { + // Arrange + var theDocument = XDocument.Parse(""); + var expected = XDocument.Parse(""); - [Fact] - public void When_asserting_a_xml_document_is_equivalent_to_a_different_xml_document_elements_missing_it_should_fail_with_descriptive_message() - { - // Arrange - var theDocument = XDocument.Parse(""); - var expected = XDocument.Parse(""); - - // Act - Action act = () => - theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw().WithMessage( - "Expected EndElement \"parent\" in theDocument at \"/parent\" because we want to test the failure message," - + " but found Element \"child2\"."); - } + // Act + Action act = () => + theDocument.Should().BeEquivalentTo(expected); - [Fact] - public void When_asserting_a_xml_document_is_equivalent_to_a_different_xml_document_with_extra_elements_it_should_fail_with_descriptive_message() - { - // Arrange - var theDocument = XDocument.Parse(""); - var expected = XDocument.Parse(""); - - // Act - Action act = () => - theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw().WithMessage( - "Expected Element \"child2\" in theDocument at \"/parent\" because we want to test the failure message," - + " but found EndElement \"parent\"."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected Element \"child2\" in theDocument at \"/parent\", but found EndElement \"parent\"."); + } - [Fact] - public void When_a_document_is_null_then_be_equivalent_to_null_succeeds() - { - XDocument theDocument = null; + [Fact] + public void When_asserting_a_xml_document_with_selfclosing_child_is_equivalent_to_a_different_xml_document_with_subchild_child_it_should_fail() + { + // Arrange + var theDocument = XDocument.Parse(""); + var otherXDocument = XDocument.Parse(""); - // Act - Action act = () => theDocument.Should().BeEquivalentTo(null); + // Act + Action act = () => + theDocument.Should().BeEquivalentTo(otherXDocument); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().Throw().WithMessage( + "Expected Element \"child\" in theDocument at \"/parent/child\", but found EndElement \"parent\"."); + } - [Fact] - public void When_a_document_is_null_then_be_equivalent_to_a_document_fails() - { - XDocument theDocument = null; - - // Act - Action act = () => - theDocument.Should().BeEquivalentTo( - XDocument.Parse(""), "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected theDocument to be equivalent to *failure message*" + - ", but found \"\"."); - } - - [Fact] - public void When_a_document_is_equivalent_to_null_it_fails() - { - XDocument theDocument = XDocument.Parse(""); + [Fact] + public void When_asserting_a_xml_document_is_equivalent_to_a_different_xml_document_elements_missing_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = XDocument.Parse(""); + var expected = XDocument.Parse(""); + + // Act + Action act = () => + theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw().WithMessage( + "Expected EndElement \"parent\" in theDocument at \"/parent\" because we want to test the failure message," + + " but found Element \"child2\"."); + } + + [Fact] + public void When_asserting_a_xml_document_is_equivalent_to_a_different_xml_document_with_extra_elements_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = XDocument.Parse(""); + var expected = XDocument.Parse(""); + + // Act + Action act = () => + theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw().WithMessage( + "Expected Element \"child2\" in theDocument at \"/parent\" because we want to test the failure message," + + " but found EndElement \"parent\"."); + } + + [Fact] + public void When_a_document_is_null_then_be_equivalent_to_null_succeeds() + { + XDocument theDocument = null; - // Act - Action act = () => - theDocument.Should().BeEquivalentTo(null, "we want to test the failure {0}", "message"); + // Act + Action act = () => theDocument.Should().BeEquivalentTo(null); - // Assert - act.Should().Throw() - .WithMessage( - "Expected theDocument to be equivalent to \"\" *failure message*" + - ", but found ."); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_asserting_a_xml_document_is_not_equivalent_to_a_different_xml_document_with_elements_missing_it_should_succeed() - { - // Arrange - var document = XDocument.Parse(""); - var otherXDocument = XDocument.Parse(""); + [Fact] + public void When_a_document_is_null_then_be_equivalent_to_a_document_fails() + { + XDocument theDocument = null; + + // Act + Action act = () => + theDocument.Should().BeEquivalentTo( + XDocument.Parse(""), "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected theDocument to be equivalent to *failure message*" + + ", but found \"\"."); + } + + [Fact] + public void When_a_document_is_equivalent_to_null_it_fails() + { + XDocument theDocument = XDocument.Parse(""); + + // Act + Action act = () => + theDocument.Should().BeEquivalentTo(null, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected theDocument to be equivalent to \"\" *failure message*" + + ", but found ."); + } + + [Fact] + public void When_assertion_an_xml_document_is_equivalent_to_a_different_xml_document_with_different_namespace_prefix_it_should_succeed() + { + // Arrange + var subject = XDocument.Parse(""); + var expected = XDocument.Parse(""); - // Act - Action act = () => - document.Should().NotBeEquivalentTo(otherXDocument); + // Act + Action act = () => + subject.Should().BeEquivalentTo(expected); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_asserting_a_xml_document_is_not_equivalent_to_a_different_xml_document_with_extra_elements_it_should_succeed() - { - // Arrange - var document = XDocument.Parse(""); - var otherXDocument = XDocument.Parse(""); + [Fact] + public void When_asserting_an_xml_document_is_equivalent_to_a_different_xml_document_which_differs_only_on_unused_namespace_declaration_it_should_succeed() + { + // Arrange + var subject = XDocument.Parse(""); + var expected = XDocument.Parse(""); - // Act - Action act = () => - document.Should().NotBeEquivalentTo(otherXDocument); + // Act + Action act = () => + subject.Should().BeEquivalentTo(expected); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_asserting_a_xml_document_is_not_equivalent_to_a_different_xml_document_with_same_structure_it_should_fail() - { - // Arrange - var theDocument = XDocument.Parse(""); - var otherXDocument = XDocument.Parse(""); + [Fact] + public void When_asserting_an_xml_document_is_equivalent_to_different_xml_document_which_lacks_attributes_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = XDocument.Parse(""); + var expected = XDocument.Parse(""); - // Act - Action act = () => - theDocument.Should().NotBeEquivalentTo(otherXDocument); + // Act + Action act = () => + theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); - // Assert - act.Should().Throw().WithMessage( - "Did not expect theDocument to be equivalent, but it is."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected attribute \"a\" in theDocument at \"/xml/element\" because we want to test the failure message, but found none."); + } - [Fact] - public void When_asserting_a_xml_document_is_not_equivalent_to_the_same_xml_document_it_should_fail() - { - // Arrange - var theDocument = new XDocument(); - var sameXDocument = theDocument; + [Fact] + public void When_asserting_an_xml_document_is_equivalent_to_different_xml_document_which_has_extra_attributes_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = XDocument.Parse(""); + var expected = XDocument.Parse(""); - // Act - Action act = () => - theDocument.Should().NotBeEquivalentTo(sameXDocument); + // Act + Action act = () => + theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); - // Assert - act.Should().Throw().WithMessage( - "Did not expect theDocument to be equivalent, but it is."); - } + // Assert + act.Should().Throw().WithMessage( + "Did not expect to find attribute \"a\" in theDocument at \"/xml/element\" because we want to test the failure message."); + } - [Fact] - public void When_asserting_a_xml_document_is_not_equivalent_to_a_different_xml_document_with_same_structure_it_should_fail_with_descriptive_message() - { - // Arrange - var theDocument = XDocument.Parse(""); - var otherDocument = XDocument.Parse(""); + [Fact] + public void When_asserting_an_xml_document_is_equivalent_to_different_xml_document_which_has_different_attribute_values_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = XDocument.Parse(""); + var expected = XDocument.Parse(""); - // Act - Action act = () => - theDocument.Should().NotBeEquivalentTo(otherDocument, "we want to test the failure {0}", "message"); + // Act + Action act = () => + theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); - // Assert - act.Should().Throw().WithMessage( - "Did not expect theDocument to be equivalent because we want to test the failure message, but it is."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected attribute \"a\" in theDocument at \"/xml/element\" to have value \"c\" because we want to test the failure message, but found \"b\"."); + } - [Fact] - public void When_asserting_a_xml_document_is_not_equivalent_to_a_different_xml_document_with_same_contents_but_different_ns_prefixes_it_should_fail() - { - // Arrange - var theDocument = XDocument.Parse(@""); - var otherXDocument = XDocument.Parse(@""); + [Fact] + public void When_asserting_an_xml_document_is_equivalent_to_different_xml_document_which_has_attribute_with_different_namespace_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = XDocument.Parse(""); + var expected = XDocument.Parse(""); - // Act - Action act = () => - theDocument.Should().NotBeEquivalentTo(otherXDocument, "because we want to test the failure {0}", "message"); + // Act + Action act = () => + theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); - // Assert - act.Should().Throw().WithMessage( - "Did not expect theDocument to be equivalent because we want to test the failure message, but it is."); - } + // Assert + act.Should().Throw().WithMessage( + "Did not expect to find attribute \"ns:a\" in theDocument at \"/xml/element\" because we want to test the failure message."); + } - [Fact] - public void When_asserting_a_xml_document_is_not_equivalent_to_a_different_xml_document_with_same_contents_but_extra_unused_xmlns_declaration_it_should_fail() - { - // Arrange - var theDocument = XDocument.Parse(@""); - var otherDocument = XDocument.Parse(""); + [Fact] + public void When_asserting_an_xml_document_is_equivalent_to_different_xml_document_which_has_different_text_contents_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = XDocument.Parse("a"); + var expected = XDocument.Parse("b"); - // Act - Action act = () => - theDocument.Should().NotBeEquivalentTo(otherDocument); + // Act + Action act = () => + theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); - // Assert - act.Should().Throw().WithMessage( - "Did not expect theDocument to be equivalent, but it is."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected content to be \"b\" in theDocument at \"/xml\" because we want to test the failure message, but found \"a\"."); + } - [Fact] - public void When_asserting_a_xml_document_is_not_equivalent_to_the_same_xml_document_it_should_fail_with_descriptive_message() - { - // Arrange - var theDocument = XDocument.Parse(""); - var sameXDocument = theDocument; + [Fact] + public void When_asserting_an_xml_document_is_equivalent_to_different_xml_document_with_different_comments_it_should_succeed() + { + // Arrange + var subject = XDocument.Parse(""); + var expected = XDocument.Parse(""); - // Act - Action act = () => - theDocument.Should().NotBeEquivalentTo(sameXDocument, "we want to test the failure {0}", "message"); + // Act + Action act = () => subject.Should().BeEquivalentTo(expected); - // Assert - act.Should().Throw().WithMessage( - "Did not expect theDocument to be equivalent because we want to test the failure message, but it is."); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_assertion_an_xml_document_is_equivalent_to_a_different_xml_document_with_different_namespace_prefix_it_should_succeed() - { - // Arrange - var subject = XDocument.Parse(""); - var expected = XDocument.Parse(""); + [Fact] + public void When_asserting_equivalence_of_an_xml_document_but_has_different_attribute_value_it_should_fail_with_xpath_to_difference() + { + // Arrange + XDocument actual = XDocument.Parse(""); + XDocument expected = XDocument.Parse(""); - // Act - Action act = () => - subject.Should().BeEquivalentTo(expected); + // Act + Action act = () => actual.Should().BeEquivalentTo(expected); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().Throw().WithMessage("*\"/xml/b\"*"); + } - [Fact] - public void When_asserting_an_xml_document_is_equivalent_to_a_different_xml_document_which_differs_only_on_unused_namespace_declaration_it_should_succeed() - { - // Arrange - var subject = XDocument.Parse(""); - var expected = XDocument.Parse(""); + [Fact] + public void When_asserting_equivalence_of_document_with_repeating_element_names_but_differs_it_should_fail_with_index_xpath_to_difference() + { + // Arrange + XDocument actual = XDocument.Parse( + ""); + XDocument expected = XDocument.Parse( + ""); - // Act - Action act = () => - subject.Should().BeEquivalentTo(expected); + // Act + Action act = () => actual.Should().BeEquivalentTo(expected); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().Throw().WithMessage("*\"/xml/xml2[3]/a[2]\"*"); + } - [Fact] - public void When_asserting_an_xml_document_is_equivalent_to_different_xml_document_which_lacks_attributes_it_should_fail_with_descriptive_message() - { - // Arrange - var theDocument = XDocument.Parse(""); - var expected = XDocument.Parse(""); + [Fact] + public void When_asserting_equivalence_of_document_with_repeating_element_names_on_different_levels_but_differs_it_should_fail_with_index_xpath_to_difference() + { + // Arrange + XDocument actual = XDocument.Parse( + ""); + XDocument expected = XDocument.Parse( + ""); - // Act - Action act = () => - theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); + // Act + Action act = () => actual.Should().BeEquivalentTo(expected); - // Assert - act.Should().Throw().WithMessage( - "Expected attribute \"a\" in theDocument at \"/xml/element\" because we want to test the failure message, but found none."); - } + // Assert + act.Should().Throw().WithMessage("*\"/xml/xml[3]/xml[2]\"*"); + } - [Fact] - public void When_asserting_an_xml_document_is_equivalent_to_different_xml_document_which_has_extra_attributes_it_should_fail_with_descriptive_message() - { - // Arrange - var theDocument = XDocument.Parse(""); - var expected = XDocument.Parse(""); + [Fact] + public void When_asserting_equivalence_of_document_with_repeating_element_names_with_different_parents_but_differs_it_should_fail_with_index_xpath_to_difference() + { + // Arrange + XDocument actual = XDocument.Parse( + ""); + XDocument expected = XDocument.Parse( + ""); - // Act - Action act = () => - theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); + // Act + Action act = () => actual.Should().BeEquivalentTo(expected); - // Assert - act.Should().Throw().WithMessage( - "Did not expect to find attribute \"a\" in theDocument at \"/xml/element\" because we want to test the failure message."); + // Assert + act.Should().Throw().WithMessage("*\"/root/xml1[3]/xml2[2]\"*"); + } } - [Fact] - public void When_asserting_an_xml_document_is_equivalent_to_different_xml_document_which_has_different_attribute_values_it_should_fail_with_descriptive_message() + public class NotBeEquivalentTo { - // Arrange - var theDocument = XDocument.Parse(""); - var expected = XDocument.Parse(""); + [Fact] + public void When_asserting_a_xml_document_is_not_equivalent_to_a_different_xml_document_with_elements_missing_it_should_succeed() + { + // Arrange + var document = XDocument.Parse(""); + var otherXDocument = XDocument.Parse(""); - // Act - Action act = () => - theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); + // Act + Action act = () => + document.Should().NotBeEquivalentTo(otherXDocument); - // Assert - act.Should().Throw().WithMessage( - "Expected attribute \"a\" in theDocument at \"/xml/element\" to have value \"c\" because we want to test the failure message, but found \"b\"."); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_asserting_an_xml_document_is_equivalent_to_different_xml_document_which_has_attribute_with_different_namespace_it_should_fail_with_descriptive_message() - { - // Arrange - var theDocument = XDocument.Parse(""); - var expected = XDocument.Parse(""); + [Fact] + public void When_asserting_a_xml_document_is_not_equivalent_to_a_different_xml_document_with_extra_elements_it_should_succeed() + { + // Arrange + var document = XDocument.Parse(""); + var otherXDocument = XDocument.Parse(""); - // Act - Action act = () => - theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); + // Act + Action act = () => + document.Should().NotBeEquivalentTo(otherXDocument); - // Assert - act.Should().Throw().WithMessage( - "Did not expect to find attribute \"ns:a\" in theDocument at \"/xml/element\" because we want to test the failure message."); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_asserting_an_xml_document_is_equivalent_to_different_xml_document_which_has_different_text_contents_it_should_fail_with_descriptive_message() - { - // Arrange - var theDocument = XDocument.Parse("a"); - var expected = XDocument.Parse("b"); + [Fact] + public void When_asserting_a_xml_document_is_not_equivalent_to_a_different_xml_document_with_same_structure_it_should_fail() + { + // Arrange + var theDocument = XDocument.Parse(""); + var otherXDocument = XDocument.Parse(""); - // Act - Action act = () => - theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); + // Act + Action act = () => + theDocument.Should().NotBeEquivalentTo(otherXDocument); - // Assert - act.Should().Throw().WithMessage( - "Expected content to be \"b\" in theDocument at \"/xml\" because we want to test the failure message, but found \"a\"."); - } + // Assert + act.Should().Throw().WithMessage( + "Did not expect theDocument to be equivalent, but it is."); + } - [Fact] - public void When_asserting_an_xml_document_is_equivalent_to_different_xml_document_with_different_comments_it_should_succeed() - { - // Arrange - var subject = XDocument.Parse(""); - var expected = XDocument.Parse(""); + [Fact] + public void When_asserting_a_xml_document_is_not_equivalent_to_the_same_xml_document_it_should_fail() + { + // Arrange + var theDocument = new XDocument(); + var sameXDocument = theDocument; - // Act - Action act = () => subject.Should().BeEquivalentTo(expected); + // Act + Action act = () => + theDocument.Should().NotBeEquivalentTo(sameXDocument); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().Throw().WithMessage( + "Did not expect theDocument to be equivalent, but it is."); + } - [Fact] - public void When_asserting_equivalence_of_an_xml_document_but_has_different_attribute_value_it_should_fail_with_xpath_to_difference() - { - // Arrange - XDocument actual = XDocument.Parse(""); - XDocument expected = XDocument.Parse(""); + [Fact] + public void When_asserting_a_xml_document_is_not_equivalent_to_a_different_xml_document_with_same_structure_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = XDocument.Parse(""); + var otherDocument = XDocument.Parse(""); - // Act - Action act = () => actual.Should().BeEquivalentTo(expected); + // Act + Action act = () => + theDocument.Should().NotBeEquivalentTo(otherDocument, "we want to test the failure {0}", "message"); - // Assert - act.Should().Throw().WithMessage("*\"/xml/b\"*"); - } + // Assert + act.Should().Throw().WithMessage( + "Did not expect theDocument to be equivalent because we want to test the failure message, but it is."); + } - [Fact] - public void When_asserting_equivalence_of_document_with_repeating_element_names_but_differs_it_should_fail_with_index_xpath_to_difference() - { - // Arrange - XDocument actual = XDocument.Parse( - ""); - XDocument expected = XDocument.Parse( - ""); + [Fact] + public void When_asserting_a_xml_document_is_not_equivalent_to_a_different_xml_document_with_same_contents_but_different_ns_prefixes_it_should_fail() + { + // Arrange + var theDocument = XDocument.Parse(@""); + var otherXDocument = XDocument.Parse(@""); - // Act - Action act = () => actual.Should().BeEquivalentTo(expected); + // Act + Action act = () => + theDocument.Should().NotBeEquivalentTo(otherXDocument, "because we want to test the failure {0}", "message"); - // Assert - act.Should().Throw().WithMessage("*\"/xml/xml2[3]/a[2]\"*"); - } + // Assert + act.Should().Throw().WithMessage( + "Did not expect theDocument to be equivalent because we want to test the failure message, but it is."); + } - [Fact] - public void When_asserting_equivalence_of_document_with_repeating_element_names_on_different_levels_but_differs_it_should_fail_with_index_xpath_to_difference() - { - // Arrange - XDocument actual = XDocument.Parse( - ""); - XDocument expected = XDocument.Parse( - ""); + [Fact] + public void When_asserting_a_xml_document_is_not_equivalent_to_a_different_xml_document_with_same_contents_but_extra_unused_xmlns_declaration_it_should_fail() + { + // Arrange + var theDocument = XDocument.Parse(@""); + var otherDocument = XDocument.Parse(""); - // Act - Action act = () => actual.Should().BeEquivalentTo(expected); + // Act + Action act = () => + theDocument.Should().NotBeEquivalentTo(otherDocument); - // Assert - act.Should().Throw().WithMessage("*\"/xml/xml[3]/xml[2]\"*"); - } + // Assert + act.Should().Throw().WithMessage( + "Did not expect theDocument to be equivalent, but it is."); + } - [Fact] - public void When_asserting_equivalence_of_document_with_repeating_element_names_with_different_parents_but_differs_it_should_fail_with_index_xpath_to_difference() - { - // Arrange - XDocument actual = XDocument.Parse( - ""); - XDocument expected = XDocument.Parse( - ""); + [Fact] + public void When_asserting_a_xml_document_is_not_equivalent_to_the_same_xml_document_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = XDocument.Parse(""); + var sameXDocument = theDocument; - // Act - Action act = () => actual.Should().BeEquivalentTo(expected); + // Act + Action act = () => + theDocument.Should().NotBeEquivalentTo(sameXDocument, "we want to test the failure {0}", "message"); - // Assert - act.Should().Throw().WithMessage("*\"/root/xml1[3]/xml2[2]\"*"); - } + // Assert + act.Should().Throw().WithMessage( + "Did not expect theDocument to be equivalent because we want to test the failure message, but it is."); + } - [Fact] - public void When_a_null_document_is_unexpected_equivalent_to_null_it_fails() - { - XDocument theDocument = null; + [Fact] + public void When_a_null_document_is_unexpected_equivalent_to_null_it_fails() + { + XDocument theDocument = null; - // Act - Action act = () => theDocument.Should().NotBeEquivalentTo(null, "we want to test the failure {0}", "message"); + // Act + Action act = () => theDocument.Should().NotBeEquivalentTo(null, "we want to test the failure {0}", "message"); - // Assert - act.Should().Throw() - .WithMessage("Did not expect theDocument to be equivalent *failure message*, but it is."); - } + // Assert + act.Should().Throw() + .WithMessage("Did not expect theDocument to be equivalent *failure message*, but it is."); + } - [Fact] - public void When_a_null_document_is_not_equivalent_to_a_document_it_succeeds() - { - XDocument theDocument = null; + [Fact] + public void When_a_null_document_is_not_equivalent_to_a_document_it_succeeds() + { + XDocument theDocument = null; - // Act - Action act = () => theDocument.Should().NotBeEquivalentTo(XDocument.Parse("")); + // Act + Action act = () => theDocument.Should().NotBeEquivalentTo(XDocument.Parse("")); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_a_document_is_not_equivalent_to_null_it_succeeds() - { - XDocument theDocument = XDocument.Parse(""); + [Fact] + public void When_a_document_is_not_equivalent_to_null_it_succeeds() + { + XDocument theDocument = XDocument.Parse(""); - // Act - Action act = () => theDocument.Should().NotBeEquivalentTo(null); + // Act + Action act = () => theDocument.Should().NotBeEquivalentTo(null); - // Assert - act.Should().NotThrow(); + // Assert + act.Should().NotThrow(); + } } - #endregion - - #region BeNull / NotBeNull - - [Fact] - public void When_asserting_a_null_xml_document_is_null_it_should_succeed() + public class BeNull { - // Arrange - XDocument document = null; + [Fact] + public void When_asserting_a_null_xml_document_is_null_it_should_succeed() + { + // Arrange + XDocument document = null; - // Act - Action act = () => - document.Should().BeNull(); + // Act + Action act = () => + document.Should().BeNull(); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_asserting_a_non_null_xml_document_is_null_it_should_fail() - { - // Arrange - var theDocument = new XDocument(); + [Fact] + public void When_asserting_a_non_null_xml_document_is_null_it_should_fail() + { + // Arrange + var theDocument = new XDocument(); - // Act - Action act = () => - theDocument.Should().BeNull(); + // Act + Action act = () => + theDocument.Should().BeNull(); - // Assert - act.Should().Throw().WithMessage( - "Expected theDocument to be , but found [XML document without root element]."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected theDocument to be , but found [XML document without root element]."); + } - [Fact] - public void When_asserting_a_non_null_xml_document_is_null_it_should_fail_with_descriptive_message() - { - // Arrange - var theDocument = XDocument.Parse(""); + [Fact] + public void When_asserting_a_non_null_xml_document_is_null_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = XDocument.Parse(""); - // Act - Action act = () => - theDocument.Should().BeNull("because we want to test the failure {0}", "message"); + // Act + Action act = () => + theDocument.Should().BeNull("because we want to test the failure {0}", "message"); - // Assert - act.Should().Throw().WithMessage( - "Expected theDocument to be because we want to test the failure message, but found ."); + // Assert + act.Should().Throw().WithMessage( + "Expected theDocument to be because we want to test the failure message, but found ."); + } } - [Fact] - public void When_asserting_a_non_null_xml_document_is_not_null_it_should_succeed() + public class NotBeNull { - // Arrange - var document = new XDocument(); + [Fact] + public void When_asserting_a_non_null_xml_document_is_not_null_it_should_succeed() + { + // Arrange + var document = new XDocument(); - // Act - Action act = () => - document.Should().NotBeNull(); + // Act + Action act = () => + document.Should().NotBeNull(); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_asserting_a_null_xml_document_is_not_null_it_should_fail() - { - // Arrange - XDocument theDocument = null; + [Fact] + public void When_asserting_a_null_xml_document_is_not_null_it_should_fail() + { + // Arrange + XDocument theDocument = null; - // Act - Action act = () => - theDocument.Should().NotBeNull(); + // Act + Action act = () => + theDocument.Should().NotBeNull(); - // Assert - act.Should().Throw().WithMessage("Expected theDocument not to be ."); - } + // Assert + act.Should().Throw().WithMessage("Expected theDocument not to be ."); + } - [Fact] - public void When_asserting_a_null_xml_document_is_not_null_it_should_fail_with_descriptive_message() - { - // Arrange - XDocument theDocument = null; + [Fact] + public void When_asserting_a_null_xml_document_is_not_null_it_should_fail_with_descriptive_message() + { + // Arrange + XDocument theDocument = null; - // Act - Action act = () => - theDocument.Should().NotBeNull("because we want to test the failure {0}", "message"); + // Act + Action act = () => + theDocument.Should().NotBeNull("because we want to test the failure {0}", "message"); - // Assert - act.Should().Throw().WithMessage( - "Expected theDocument not to be because we want to test the failure message."); + // Assert + act.Should().Throw().WithMessage( + "Expected theDocument not to be because we want to test the failure message."); + } } - #endregion - - #region HaveRoot - - [Fact] - public void When_asserting_document_has_root_element_and_it_does_it_should_succeed_and_return_it_for_chaining() + public class HaveRoot { - // Arrange - var document = XDocument.Parse( - @" + [Fact] + public void When_asserting_document_has_root_element_and_it_does_it_should_succeed_and_return_it_for_chaining() + { + // Arrange + var document = XDocument.Parse( + @" "); - // Act - XElement root = document.Should().HaveRoot("parent").Subject; + // Act + XElement root = document.Should().HaveRoot("parent").Subject; - // Assert - root.Should().BeSameAs(document.Root); - } + // Assert + root.Should().BeSameAs(document.Root); + } - [Fact] - public void When_asserting_document_has_root_element_but_it_does_not_it_should_fail() - { - // Arrange - var theDocument = XDocument.Parse( - @" + [Fact] + public void When_asserting_document_has_root_element_but_it_does_not_it_should_fail() + { + // Arrange + var theDocument = XDocument.Parse( + @" "); - // Act - Action act = () => theDocument.Should().HaveRoot("unknown"); + // Act + Action act = () => theDocument.Should().HaveRoot("unknown"); - // Assert - act.Should().Throw().WithMessage( - "Expected theDocument to have root element \"unknown\", but found …."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected theDocument to have root element \"unknown\", but found …."); + } - [Fact] - public void When_asserting_document_has_root_element_but_it_does_not_it_should_fail_with_descriptive_message() - { - // Arrange - var theDocument = XDocument.Parse( - @" + [Fact] + public void When_asserting_document_has_root_element_but_it_does_not_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = XDocument.Parse( + @" "); - // Act - Action act = () => - theDocument.Should().HaveRoot("unknown", "because we want to test the failure message"); + // Act + Action act = () => + theDocument.Should().HaveRoot("unknown", "because we want to test the failure message"); - // Assert - string expectedMessage = "Expected theDocument to have root element \"unknown\"" + - " because we want to test the failure message" + - $", but found {Formatter.ToString(theDocument)}."; + // Assert + string expectedMessage = "Expected theDocument to have root element \"unknown\"" + + " because we want to test the failure message" + + $", but found {Formatter.ToString(theDocument)}."; - act.Should().Throw().WithMessage(expectedMessage); - } + act.Should().Throw().WithMessage(expectedMessage); + } - [Fact] - public void When_asserting_a_null_document_has_root_element_it_should_fail() - { - // Arrange - XDocument theDocument = null; + [Fact] + public void When_asserting_a_null_document_has_root_element_it_should_fail() + { + // Arrange + XDocument theDocument = null; - // Act - Action act = () => theDocument.Should().HaveRoot("unknown"); + // Act + Action act = () => theDocument.Should().HaveRoot("unknown"); - // Assert - act.Should().Throw().WithMessage( - "Cannot assert the document has a root element if the document itself is ."); - } + // Assert + act.Should().Throw().WithMessage( + "Cannot assert the document has a root element if the document itself is ."); + } - [Fact] - public void When_asserting_a_document_has_a_root_element_with_a_null_name_it_should_fail() - { - // Arrange - var theDocument = XDocument.Parse( - @" + [Fact] + public void When_asserting_a_document_has_a_root_element_with_a_null_name_it_should_fail() + { + // Arrange + var theDocument = XDocument.Parse( + @" "); - // Act - Action act = () => theDocument.Should().HaveRoot(null); + // Act + Action act = () => theDocument.Should().HaveRoot(null); - // Assert - act.Should().Throw().WithMessage( - "Cannot assert the document has a root element if the expected name is *"); - } + // Assert + act.Should().Throw().WithMessage( + "Cannot assert the document has a root element if the expected name is *"); + } - [Fact] - public void When_asserting_a_document_has_a_root_element_with_a_null_xname_it_should_fail() - { - // Arrange - var theDocument = XDocument.Parse( - @" + [Fact] + public void When_asserting_a_document_has_a_root_element_with_a_null_xname_it_should_fail() + { + // Arrange + var theDocument = XDocument.Parse( + @" "); - // Act - Action act = () => theDocument.Should().HaveRoot((XName)null); + // Act + Action act = () => theDocument.Should().HaveRoot((XName)null); - // Assert - act.Should().Throw().WithMessage( - "Cannot assert the document has a root element if the expected name is *"); - } + // Assert + act.Should().Throw().WithMessage( + "Cannot assert the document has a root element if the expected name is *"); + } - [Fact] - public void When_asserting_document_has_root_element_with_ns_and_it_does_it_should_succeed() - { - // Arrange - var document = XDocument.Parse( - @" + [Fact] + public void When_asserting_document_has_root_element_with_ns_and_it_does_it_should_succeed() + { + // Arrange + var document = XDocument.Parse( + @" "); - // Act - Action act = () => - document.Should().HaveRoot(XName.Get("parent", "http://www.example.com/2012/test")); + // Act + Action act = () => + document.Should().HaveRoot(XName.Get("parent", "http://www.example.com/2012/test")); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_asserting_document_has_root_element_with_ns_but_it_does_not_it_should_fail() - { - // Arrange - var theDocument = XDocument.Parse( - @" + [Fact] + public void When_asserting_document_has_root_element_with_ns_but_it_does_not_it_should_fail() + { + // Arrange + var theDocument = XDocument.Parse( + @" "); - // Act - Action act = () => - theDocument.Should().HaveRoot(XName.Get("unknown", "http://www.example.com/2012/test")); + // Act + Action act = () => + theDocument.Should().HaveRoot(XName.Get("unknown", "http://www.example.com/2012/test")); - // Assert - act.Should().Throw().WithMessage( - "Expected theDocument to have root element \"{http://www.example.com/2012/test}unknown\", but found …."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected theDocument to have root element \"{http://www.example.com/2012/test}unknown\", but found …."); + } - [Fact] - public void When_asserting_document_has_root_element_with_ns_but_it_does_not_it_should_fail_with_descriptive_message() - { - // Arrange - var theDocument = XDocument.Parse( - @" + [Fact] + public void When_asserting_document_has_root_element_with_ns_but_it_does_not_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = XDocument.Parse( + @" "); - // Act - Action act = () => - theDocument.Should().HaveRoot(XName.Get("unknown", "http://www.example.com/2012/test"), - "because we want to test the failure message"); + // Act + Action act = () => + theDocument.Should().HaveRoot(XName.Get("unknown", "http://www.example.com/2012/test"), + "because we want to test the failure message"); - // Assert - string expectedMessage = - "Expected theDocument to have root element \"{http://www.example.com/2012/test}unknown\"" + - " because we want to test the failure message" + - $", but found {Formatter.ToString(theDocument)}."; + // Assert + string expectedMessage = + "Expected theDocument to have root element \"{http://www.example.com/2012/test}unknown\"" + + " because we want to test the failure message" + + $", but found {Formatter.ToString(theDocument)}."; - act.Should().Throw().WithMessage(expectedMessage); + act.Should().Throw().WithMessage(expectedMessage); + } } - #endregion - - #region HaveElement - - [Fact] - public void When_document_has_the_expected_child_element_it_should_not_throw_and_return_the_element_for_chaining() + public class HaveElement { - // Arrange - var document = XDocument.Parse( - @" + [Fact] + public void When_document_has_the_expected_child_element_it_should_not_throw_and_return_the_element_for_chaining() + { + // Arrange + var document = XDocument.Parse( + @" "); - // Act - XElement element = document.Should().HaveElement("child").Subject; + // Act + XElement element = document.Should().HaveElement("child").Subject; - // Assert - element.Should().BeSameAs(document.Element("parent").Element("child")); - } + // Assert + element.Should().BeSameAs(document.Element("parent").Element("child")); + } - [Fact] - public void When_asserting_document_has_root_with_child_element_but_it_does_not_it_should_fail() - { - // Arrange - var theDocument = XDocument.Parse( - @" + [Fact] + public void When_asserting_document_has_root_with_child_element_but_it_does_not_it_should_fail() + { + // Arrange + var theDocument = XDocument.Parse( + @" "); - // Act - Action act = () => - theDocument.Should().HaveElement("unknown"); + // Act + Action act = () => + theDocument.Should().HaveElement("unknown"); - // Assert - act.Should().Throw().WithMessage( - "Expected theDocument to have root element with child \"unknown\", but no such child element was found."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected theDocument to have root element with child \"unknown\", but no such child element was found."); + } - [Fact] - public void When_asserting_document_has_root_with_child_element_but_it_does_not_it_should_fail_with_descriptive_message() - { - // Arrange - var theDocument = XDocument.Parse( - @" + [Fact] + public void When_asserting_document_has_root_with_child_element_but_it_does_not_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = XDocument.Parse( + @" "); - // Act - Action act = () => - theDocument.Should().HaveElement("unknown", "because we want to test the failure message"); + // Act + Action act = () => + theDocument.Should().HaveElement("unknown", "because we want to test the failure message"); - // Assert - act.Should().Throw().WithMessage( - "Expected theDocument to have root element with child \"unknown\" because we want to test the failure message," - + " but no such child element was found."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected theDocument to have root element with child \"unknown\" because we want to test the failure message," + + " but no such child element was found."); + } - [Fact] - public void When_asserting_document_has_root_with_child_element_with_ns_and_it_does_it_should_succeed() - { - // Arrange - var document = XDocument.Parse( - @" + [Fact] + public void When_asserting_document_has_root_with_child_element_with_ns_and_it_does_it_should_succeed() + { + // Arrange + var document = XDocument.Parse( + @" "); - // Act - Action act = () => - document.Should().HaveElement(XName.Get("child", "http://www.example.org/2012/test")); + // Act + Action act = () => + document.Should().HaveElement(XName.Get("child", "http://www.example.org/2012/test")); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_asserting_document_has_root_with_child_element_with_ns_but_it_does_not_it_should_fail() - { - // Arrange - var document = XDocument.Parse( - @" + [Fact] + public void When_asserting_document_has_root_with_child_element_with_ns_but_it_does_not_it_should_fail() + { + // Arrange + var document = XDocument.Parse( + @" "); - // Act - Action act = () => - document.Should().HaveElement(XName.Get("unknown", "http://www.example.org/2012/test")); + // Act + Action act = () => + document.Should().HaveElement(XName.Get("unknown", "http://www.example.org/2012/test")); - // Assert - act.Should().Throw().WithMessage( - "Expected document to have root element with child \"{http://www.example.org/2012/test}unknown\"," - + " but no such child element was found."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected document to have root element with child \"{http://www.example.org/2012/test}unknown\"," + + " but no such child element was found."); + } - [Fact] - public void When_asserting_document_has_root_with_child_element_with_ns_but_it_does_not_it_should_fail_with_descriptive_message() - { - // Arrange - var theDocument = XDocument.Parse( - @" + [Fact] + public void When_asserting_document_has_root_with_child_element_with_ns_but_it_does_not_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = XDocument.Parse( + @" "); - // Act - Action act = () => - theDocument.Should().HaveElement(XName.Get("unknown", "http://www.example.org/2012/test"), - "because we want to test the failure message"); + // Act + Action act = () => + theDocument.Should().HaveElement(XName.Get("unknown", "http://www.example.org/2012/test"), + "because we want to test the failure message"); - // Assert - act.Should().Throw().WithMessage( - "Expected theDocument to have root element with child \"{http://www.example.org/2012/test}unknown\"" - + " because we want to test the failure message, but no such child element was found."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected theDocument to have root element with child \"{http://www.example.org/2012/test}unknown\"" + + " because we want to test the failure message, but no such child element was found."); + } - [Fact] - public void When_asserting_document_has_root_with_child_element_with_attributes_it_should_be_possible_to_use_which_to_assert_on_the_element() - { - // Arrange - var document = XDocument.Parse( - @" + [Fact] + public void When_asserting_document_has_root_with_child_element_with_attributes_it_should_be_possible_to_use_which_to_assert_on_the_element() + { + // Arrange + var document = XDocument.Parse( + @" "); - // Act - XElement matchedElement = document.Should().HaveElement("child").Subject; + // Act + XElement matchedElement = document.Should().HaveElement("child").Subject; - // Assert - matchedElement.Should().BeOfType().And.HaveAttribute("attr", "1"); - matchedElement.Name.Should().Be(XName.Get("child")); - } + // Assert + matchedElement.Should().BeOfType().And.HaveAttribute("attr", "1"); + matchedElement.Name.Should().Be(XName.Get("child")); + } - [Fact] - public void When_asserting_a_null_document_has_an_element_it_should_fail() - { - // Arrange - XDocument document = null; + [Fact] + public void When_asserting_a_null_document_has_an_element_it_should_fail() + { + // Arrange + XDocument document = null; - // Act - Action act = () => document.Should().HaveElement("unknown"); + // Act + Action act = () => document.Should().HaveElement("unknown"); - // Assert - act.Should().Throw().WithMessage( - "Cannot assert the document has an element if the document itself is ."); - } + // Assert + act.Should().Throw().WithMessage( + "Cannot assert the document has an element if the document itself is ."); + } - [Fact] - public void When_asserting_a_document_without_root_element_has_an_element_it_should_fail() - { - // Arrange - XDocument document = new(); + [Fact] + public void When_asserting_a_document_without_root_element_has_an_element_it_should_fail() + { + // Arrange + XDocument document = new(); - // Act - Action act = () => document.Should().HaveElement("unknown"); + // Act + Action act = () => document.Should().HaveElement("unknown"); - // Assert - act.Should().Throw().WithMessage( - "Expected document to have root element with child \"unknown\", but it has no root element."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected document to have root element with child \"unknown\", but it has no root element."); + } - [Fact] - public void When_asserting_a_document_has_an_element_with_a_null_name_it_should_fail() - { - // Arrange - var document = XDocument.Parse( - @" + [Fact] + public void When_asserting_a_document_has_an_element_with_a_null_name_it_should_fail() + { + // Arrange + var document = XDocument.Parse( + @" "); - // Act - Action act = () => document.Should().HaveElement(null); + // Act + Action act = () => document.Should().HaveElement(null); - // Assert - act.Should().Throw().WithMessage( - "Cannot assert the document has an element if the expected name is *"); - } + // Assert + act.Should().Throw().WithMessage( + "Cannot assert the document has an element if the expected name is *"); + } - [Fact] - public void When_asserting_a_document_has_an_element_with_a_null_xname_it_should_fail() - { - // Arrange - var document = XDocument.Parse( - @" + [Fact] + public void When_asserting_a_document_has_an_element_with_a_null_xname_it_should_fail() + { + // Arrange + var document = XDocument.Parse( + @" "); - // Act - Action act = () => document.Should().HaveElement((XName)null); + // Act + Action act = () => document.Should().HaveElement((XName)null); - // Assert - act.Should().ThrowExactly().WithMessage( - "Cannot assert the document has an element if the expected name is *"); + // Assert + act.Should().ThrowExactly().WithMessage( + "Cannot assert the document has an element if the expected name is *"); + } } - #endregion - - #region HaveElement (with occurrence) - - [Fact] - public void When_asserting_document_has_two_child_elements_and_it_does_it_succeeds() + public class HaveElementWithOccurrence { - // Arrange - var document = XDocument.Parse( - @" + [Fact] + public void When_asserting_document_has_two_child_elements_and_it_does_it_succeeds() + { + // Arrange + var document = XDocument.Parse( + @" "); - // Act / Assert - document.Should().HaveElement("child", Exactly.Twice()); - } - - [Fact] - public void Asserting_document_null_inside_an_assertion_scope_it_checks_the_whole_assertion_scope_before_failing() - { - // Arrange - XDocument document = null; + // Act / Assert + document.Should().HaveElement("child", Exactly.Twice()); + } - // Act - Action act = () => + [Fact] + public void Asserting_document_null_inside_an_assertion_scope_it_checks_the_whole_assertion_scope_before_failing() { - using (new AssertionScope()) - { - document.Should().HaveElement("child", Exactly.Twice()); - document.Should().HaveElement("child", Exactly.Twice()); - } - }; - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void Asserting_with_document_root_null_inside_an_assertion_scope_it_checks_the_whole_assertion_scope_before_failing() - { - // Arrange - XDocument document = new(); + // Arrange + XDocument document = null; - // Act - Action act = () => - { - using (new AssertionScope()) + // Act + Action act = () => { - document.Should().HaveElement("child", Exactly.Twice()); - document.Should().HaveElement("child", Exactly.Twice()); - } - }; - - // Assert - act.Should().NotThrow(); - } + using (new AssertionScope()) + { + document.Should().HaveElement("child", Exactly.Twice()); + document.Should().HaveElement("child", Exactly.Twice()); + } + }; + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void Asserting_with_document_root_null_inside_an_assertion_scope_it_checks_the_whole_assertion_scope_before_failing() + { + // Arrange + XDocument document = new(); - [Fact] - public void When_asserting_document_has_two_child_elements_but_it_does_have_three_it_fails() - { - // Arrange - var document = XDocument.Parse( - @" + // Act + Action act = () => + { + using (new AssertionScope()) + { + document.Should().HaveElement("child", Exactly.Twice()); + document.Should().HaveElement("child", Exactly.Twice()); + } + }; + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_document_has_two_child_elements_but_it_does_have_three_it_fails() + { + // Arrange + var document = XDocument.Parse( + @" "); - // Act - Action act = () => document.Should().HaveElement("child", Exactly.Twice()); + // Act + Action act = () => document.Should().HaveElement("child", Exactly.Twice()); - // Assert - act.Should().Throw() - .WithMessage("Expected document to have a root element containing a child \"child\"*exactly*2 times, but found it 3 times*"); - } + // Assert + act.Should().Throw() + .WithMessage("Expected document to have a root element containing a child \"child\"*exactly*2 times, but found it 3 times*"); + } - [Fact] - public void Document_is_valid_and_expected_null_with_string_overload_it_fails() - { - // Arrange - var document = XDocument.Parse( - @" + [Fact] + public void Document_is_valid_and_expected_null_with_string_overload_it_fails() + { + // Arrange + var document = XDocument.Parse( + @" "); - // Act - Action act = () => document.Should().HaveElement(null, Exactly.Twice()); + // Act + Action act = () => document.Should().HaveElement(null, Exactly.Twice()); - // Assert - act.Should().Throw().WithMessage( - "Cannot assert the document has an element if the expected name is .*"); - } + // Assert + act.Should().Throw().WithMessage( + "Cannot assert the document has an element if the expected name is .*"); + } - [Fact] - public void Document_is_valid_and_expected_null_with_x_name_overload_it_fails() - { - // Arrange - var document = XDocument.Parse( - @" + [Fact] + public void Document_is_valid_and_expected_null_with_x_name_overload_it_fails() + { + // Arrange + var document = XDocument.Parse( + @" "); - // Act - Action act = () => document.Should().HaveElement((XName)null, Exactly.Twice()); + // Act + Action act = () => document.Should().HaveElement((XName)null, Exactly.Twice()); - // Assert - act.Should().Throw().WithMessage( - "Cannot assert the document has an element count if the element name is .*"); - } + // Assert + act.Should().Throw().WithMessage( + "Cannot assert the document has an element count if the element name is .*"); + } - [Fact] - public void Chaining_after_a_successful_occurrence_check_does_continue_the_assertion() - { - // Arrange - var document = XDocument.Parse( - @" + [Fact] + public void Chaining_after_a_successful_occurrence_check_does_continue_the_assertion() + { + // Arrange + var document = XDocument.Parse( + @" "); - // Act / Assert - document.Should().HaveElement("child", AtLeast.Twice()) - .Which.Should().NotBeNull(); - } + // Act / Assert + document.Should().HaveElement("child", AtLeast.Twice()) + .Which.Should().NotBeNull(); + } - [Fact] - public void Chaining_after_a_non_successful_occurrence_check_does_not_continue_the_assertion() - { - // Arrange - var document = XDocument.Parse( - @" + [Fact] + public void Chaining_after_a_non_successful_occurrence_check_does_not_continue_the_assertion() + { + // Arrange + var document = XDocument.Parse( + @" "); - // Act - Action act = () => document.Should().HaveElement("child", Exactly.Once()) - .Which.Should().NotBeNull(); + // Act + Action act = () => document.Should().HaveElement("child", Exactly.Once()) + .Which.Should().NotBeNull(); - // Assert - act.Should().Throw() - .WithMessage("Expected document to have a root element containing a child \"child\"*exactly*1 time, but found it 3 times."); - } + // Assert + act.Should().Throw() + .WithMessage("Expected document to have a root element containing a child \"child\"*exactly*1 time, but found it 3 times."); + } - [Fact] - public void When_asserting_a_null_document_to_have_an_element_count_it_should_fail() - { - // Arrange - XDocument xDocument = null; + [Fact] + public void When_asserting_a_null_document_to_have_an_element_count_it_should_fail() + { + // Arrange + XDocument xDocument = null; - // Act - Action act = () => xDocument.Should().HaveElement("child", AtLeast.Once()); + // Act + Action act = () => xDocument.Should().HaveElement("child", AtLeast.Once()); - // Assert - act.Should().Throw().WithMessage( - "Cannot assert the count if the document itself is ."); + // Assert + act.Should().Throw().WithMessage( + "Cannot assert the count if the document itself is ."); + } } - - #endregion } } diff --git a/Tests/FluentAssertions.Specs/Xml/XElementAssertionSpecs.cs b/Tests/FluentAssertions.Specs/Xml/XElementAssertionSpecs.cs index 176bd01d27..fc59b30b1b 100644 --- a/Tests/FluentAssertions.Specs/Xml/XElementAssertionSpecs.cs +++ b/Tests/FluentAssertions.Specs/Xml/XElementAssertionSpecs.cs @@ -8,1371 +8,1373 @@ namespace FluentAssertions.Specs.Xml { public class XElementAssertionSpecs { - #region Be / NotBe - - [Fact] - public void When_asserting_an_xml_element_is_equal_to_the_same_xml_element_it_should_succeed() + public class Be { - // Arrange - var theElement = new XElement("element"); - var sameElement = new XElement("element"); - - // Act - Action act = () => - theElement.Should().Be(sameElement); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_an_xml_element_is_equal_to_a_different_xml_element_it_should_fail_with_descriptive_message() - { - // Arrange - var theElement = new XElement("element"); - var otherElement = new XElement("other"); - - // Act - Action act = () => - theElement.Should().Be(otherElement, "because we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw().WithMessage( - "Expected theElement to be*other*because we want to test the failure message, but found *element*"); - } + [Fact] + public void When_asserting_an_xml_element_is_equal_to_the_same_xml_element_it_should_succeed() + { + // Arrange + var theElement = new XElement("element"); + var sameElement = new XElement("element"); - [Fact] - public void When_asserting_an_xml_element_is_equal_to_an_xml_element_with_a_deep_difference_it_should_fail() - { - // Arrange - var theElement = - new XElement("parent", - new XElement("child", - new XElement("grandChild2"))); - var expected = - new XElement("parent", - new XElement("child", - new XElement("grandChild"))); - - // Act - Action act = () => theElement.Should().Be(expected); - - // Assert - act.Should().Throw().WithMessage( - "Expected theElement to be …, but found …."); - } + // Act + Action act = () => + theElement.Should().Be(sameElement); - [Fact] - public void When_the_expected_element_is_null_it_fails() - { - // Arrange - XElement theElement = null; + // Assert + act.Should().NotThrow(); + } - // Act - Action act = () => - theElement.Should().Be(new XElement("other"), "we want to test the failure {0}", "message"); + [Fact] + public void When_asserting_an_xml_element_is_equal_to_a_different_xml_element_it_should_fail_with_descriptive_message() + { + // Arrange + var theElement = new XElement("element"); + var otherElement = new XElement("other"); - // Assert - act.Should().Throw() - .WithMessage("Expected theElement to be *failure message*, but found ."); - } + // Act + Action act = () => + theElement.Should().Be(otherElement, "because we want to test the failure {0}", "message"); - [Fact] - public void When_element_is_expected_to_equal_null_it_fails() - { - // Arrange - XElement theElement = new XElement("element"); + // Assert + act.Should().Throw().WithMessage( + "Expected theElement to be*other*because we want to test the failure message, but found *element*"); + } - // Act - Action act = () => theElement.Should().Be(null, "we want to test the failure {0}", "message"); + [Fact] + public void When_asserting_an_xml_element_is_equal_to_an_xml_element_with_a_deep_difference_it_should_fail() + { + // Arrange + var theElement = + new XElement("parent", + new XElement("child", + new XElement("grandChild2"))); + var expected = + new XElement("parent", + new XElement("child", + new XElement("grandChild"))); + + // Act + Action act = () => theElement.Should().Be(expected); + + // Assert + act.Should().Throw().WithMessage( + "Expected theElement to be …, but found …."); + } + + [Fact] + public void When_the_expected_element_is_null_it_fails() + { + // Arrange + XElement theElement = null; - // Assert - act.Should().Throw() - .WithMessage("Expected theElement to be *failure message*, but found ."); - } + // Act + Action act = () => + theElement.Should().Be(new XElement("other"), "we want to test the failure {0}", "message"); - [Fact] - public void When_both_subject_and_expected_are_null_it_succeeds() - { - // Arrange - XElement theElement = null; + // Assert + act.Should().Throw() + .WithMessage("Expected theElement to be *failure message*, but found ."); + } - // Act - Action act = () => theElement.Should().Be(null); + [Fact] + public void When_element_is_expected_to_equal_null_it_fails() + { + // Arrange + XElement theElement = new XElement("element"); - // Assert - act.Should().NotThrow(); - } + // Act + Action act = () => theElement.Should().Be(null, "we want to test the failure {0}", "message"); - [Fact] - public void When_asserting_an_xml_element_is_not_equal_to_a_different_xml_element_it_should_succeed() - { - // Arrange - var element = new XElement("element"); - var otherElement = new XElement("other"); + // Assert + act.Should().Throw() + .WithMessage("Expected theElement to be *failure message*, but found ."); + } - // Act - Action act = () => - element.Should().NotBe(otherElement); + [Fact] + public void When_both_subject_and_expected_are_null_it_succeeds() + { + // Arrange + XElement theElement = null; - // Assert - act.Should().NotThrow(); - } + // Act + Action act = () => theElement.Should().Be(null); - [Fact] - public void When_asserting_a_deep_xml_element_is_not_equal_to_a_different_xml_element_it_should_succeed() - { - // Arrange - var differentElement = - new XElement("parent", - new XElement("child", - new XElement("grandChild"))); - var element = - new XElement("parent", - new XElement("child", - new XElement("grandChild2"))); - - // Act - Action act = () => element.Should().NotBe(differentElement); - - // Assert - act.Should().NotThrow(); + // Assert + act.Should().NotThrow(); + } } - [Fact] - public void When_asserting_an_xml_element_is_not_equal_to_the_same_xml_element_it_should_fail() + public class NotBe { - // Arrange - var theElement = new XElement("element"); - var sameElement = theElement; - - // Act - Action act = () => - theElement.Should().NotBe(sameElement, "because we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Did not expect theElement to be because we want to test the failure message."); - } + [Fact] + public void When_asserting_an_xml_element_is_not_equal_to_a_different_xml_element_it_should_succeed() + { + // Arrange + var element = new XElement("element"); + var otherElement = new XElement("other"); - [Fact] - public void When_an_element_is_not_supposed_to_be_null_it_succeeds() - { - // Arrange - XElement theElement = new XElement("element"); + // Act + Action act = () => + element.Should().NotBe(otherElement); - // Act - Action act = () => theElement.Should().NotBe(null); + // Assert + act.Should().NotThrow(); + } - // Assert - act.Should().NotThrow(); - } + [Fact] + public void When_asserting_a_deep_xml_element_is_not_equal_to_a_different_xml_element_it_should_succeed() + { + // Arrange + var differentElement = + new XElement("parent", + new XElement("child", + new XElement("grandChild"))); + var element = + new XElement("parent", + new XElement("child", + new XElement("grandChild2"))); + + // Act + Action act = () => element.Should().NotBe(differentElement); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_an_xml_element_is_not_equal_to_the_same_xml_element_it_should_fail() + { + // Arrange + var theElement = new XElement("element"); + var sameElement = theElement; - [Fact] - public void When_a_null_element_is_not_supposed_to_be_an_element_it_succeeds() - { - // Arrange - XElement theElement = null; + // Act + Action act = () => + theElement.Should().NotBe(sameElement, "because we want to test the failure {0}", "message"); - // Act - Action act = () => theElement.Should().NotBe(new XElement("other")); + // Assert + act.Should().Throw() + .WithMessage("Did not expect theElement to be because we want to test the failure message."); + } - // Assert - act.Should().NotThrow(); - } + [Fact] + public void When_an_element_is_not_supposed_to_be_null_it_succeeds() + { + // Arrange + XElement theElement = new XElement("element"); - [Fact] - public void When_a_null_element_is_not_supposed_to_be_null_it_fails() - { - // Arrange - XElement theElement = null; + // Act + Action act = () => theElement.Should().NotBe(null); - // Act - Action act = () => theElement.Should().NotBe(null, "we want to test the failure {0}", "message"); + // Assert + act.Should().NotThrow(); + } - // Assert - act.Should().Throw() - .WithMessage("Did not expect theElement to be *failure message*."); - } + [Fact] + public void When_a_null_element_is_not_supposed_to_be_an_element_it_succeeds() + { + // Arrange + XElement theElement = null; - #endregion + // Act + Action act = () => theElement.Should().NotBe(new XElement("other")); - #region BeNull / NotBeNull + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_asserting_an_xml_element_is_null_and_it_is_it_should_succeed() - { - // Arrange - XElement element = null; + [Fact] + public void When_a_null_element_is_not_supposed_to_be_null_it_fails() + { + // Arrange + XElement theElement = null; - // Act - Action act = () => - element.Should().BeNull(); + // Act + Action act = () => theElement.Should().NotBe(null, "we want to test the failure {0}", "message"); - // Assert - act.Should().NotThrow(); + // Assert + act.Should().Throw() + .WithMessage("Did not expect theElement to be *failure message*."); + } } - [Fact] - public void When_asserting_an_xml_element_is_null_but_it_is_not_it_should_fail() + public class BeNull { - // Arrange - var theElement = new XElement("element"); + [Fact] + public void When_asserting_an_xml_element_is_null_and_it_is_it_should_succeed() + { + // Arrange + XElement element = null; - // Act - Action act = () => - theElement.Should().BeNull(); + // Act + Action act = () => + element.Should().BeNull(); - // Assert - act.Should().Throw().WithMessage( - "Expected theElement to be , but found ."); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_asserting_an_xml_element_is_null_but_it_is_not_it_should_fail_with_descriptive_message() - { - // Arrange - var theElement = new XElement("element"); + [Fact] + public void When_asserting_an_xml_element_is_null_but_it_is_not_it_should_fail() + { + // Arrange + var theElement = new XElement("element"); - // Act - Action act = () => - theElement.Should().BeNull("because we want to test the failure {0}", "message"); + // Act + Action act = () => + theElement.Should().BeNull(); - // Assert - act.Should().Throw().WithMessage( - "Expected theElement to be because we want to test the failure message, but found ."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected theElement to be , but found ."); + } - [Fact] - public void When_asserting_a_non_null_xml_element_is_not_null_it_should_succeed() - { - // Arrange - var element = new XElement("element"); + [Fact] + public void When_asserting_an_xml_element_is_null_but_it_is_not_it_should_fail_with_descriptive_message() + { + // Arrange + var theElement = new XElement("element"); - // Act - Action act = () => - element.Should().NotBeNull(); + // Act + Action act = () => + theElement.Should().BeNull("because we want to test the failure {0}", "message"); - // Assert - act.Should().NotThrow(); + // Assert + act.Should().Throw().WithMessage( + "Expected theElement to be because we want to test the failure message, but found ."); + } } - [Fact] - public void When_asserting_a_null_xml_element_is_not_null_it_should_fail() + public class NotBeNull { - // Arrange - XElement theElement = null; - - // Act - Action act = () => - theElement.Should().NotBeNull(); - - // Assert - act.Should().Throw().WithMessage("Expected theElement not to be ."); - } + [Fact] + public void When_asserting_a_non_null_xml_element_is_not_null_it_should_succeed() + { + // Arrange + var element = new XElement("element"); - [Fact] - public void When_asserting_a_null_xml_element_is_not_null_it_should_fail_with_descriptive_message() - { - // Arrange - XElement theElement = null; + // Act + Action act = () => + element.Should().NotBeNull(); - // Act - Action act = () => - theElement.Should().NotBeNull("because we want to test the failure {0}", "message"); + // Assert + act.Should().NotThrow(); + } - // Assert - act.Should().Throw().WithMessage( - "Expected theElement not to be because we want to test the failure message."); - } + [Fact] + public void When_asserting_a_null_xml_element_is_not_null_it_should_fail() + { + // Arrange + XElement theElement = null; - #endregion + // Act + Action act = () => + theElement.Should().NotBeNull(); - #region BeEquivalentTo / NotBeEquivalentTo + // Assert + act.Should().Throw().WithMessage("Expected theElement not to be ."); + } - [Fact] - public void When_asserting_a_xml_element_is_equivalent_to_the_same_xml_element_it_should_succeed() - { - // Arrange - var element = new XElement("element"); - var sameXElement = element; + [Fact] + public void When_asserting_a_null_xml_element_is_not_null_it_should_fail_with_descriptive_message() + { + // Arrange + XElement theElement = null; - // Act - Action act = () => - element.Should().BeEquivalentTo(sameXElement); + // Act + Action act = () => + theElement.Should().NotBeNull("because we want to test the failure {0}", "message"); - // Assert - act.Should().NotThrow(); + // Assert + act.Should().Throw().WithMessage( + "Expected theElement not to be because we want to test the failure message."); + } } - [Fact] - public void When_asserting_a_xml_element_is_equivalent_to_a_different_xml_element_with_same_structure_it_should_succeed() + public class BeEquivalentTo { - // Arrange - var element = XElement.Parse(""); - var otherXElement = XElement.Parse(""); + [Fact] + public void When_asserting_a_xml_element_is_equivalent_to_the_same_xml_element_it_should_succeed() + { + // Arrange + var element = new XElement("element"); + var sameXElement = element; - // Act - Action act = () => - element.Should().BeEquivalentTo(otherXElement); + // Act + Action act = () => + element.Should().BeEquivalentTo(sameXElement); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_asserting_an_empty_xml_element_is_equivalent_to_a_different_selfclosing_xml_element_it_should_succeed() - { - // Arrange - var element = XElement.Parse(""); - var otherElement = XElement.Parse(""); + [Fact] + public void When_asserting_a_xml_element_is_equivalent_to_a_different_xml_element_with_same_structure_it_should_succeed() + { + // Arrange + var element = XElement.Parse(""); + var otherXElement = XElement.Parse(""); - // Act - Action act = () => - element.Should().BeEquivalentTo(otherElement); + // Act + Action act = () => + element.Should().BeEquivalentTo(otherXElement); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_asserting_a_selfclosing_xml_element_is_equivalent_to_a_different_empty_xml_element_it_should_succeed() - { - // Arrange - var element = XElement.Parse(""); - var otherElement = XElement.Parse(""); + [Fact] + public void When_asserting_an_empty_xml_element_is_equivalent_to_a_different_selfclosing_xml_element_it_should_succeed() + { + // Arrange + var element = XElement.Parse(""); + var otherElement = XElement.Parse(""); - // Act - Action act = () => - element.Should().BeEquivalentTo(otherElement); + // Act + Action act = () => + element.Should().BeEquivalentTo(otherElement); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_asserting_a_xml_element_is_equivalent_to_a_xml_element_with_elements_missing_it_should_fail() - { - // Arrange - var theElement = XElement.Parse(""); - var otherXElement = XElement.Parse(""); + [Fact] + public void When_asserting_a_selfclosing_xml_element_is_equivalent_to_a_different_empty_xml_element_it_should_succeed() + { + // Arrange + var element = XElement.Parse(""); + var otherElement = XElement.Parse(""); - // Act - Action act = () => - theElement.Should().BeEquivalentTo(otherXElement); + // Act + Action act = () => + element.Should().BeEquivalentTo(otherElement); - // Assert - act.Should().Throw().WithMessage( - "Expected EndElement \"parent\" in theElement at \"/parent\", but found Element \"child2\"."); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_asserting_a_xml_element_is_equivalent_to_a_different_xml_element_with_extra_elements_it_should_fail() - { - // Arrange - var theElement = XElement.Parse(""); - var otherXElement = XElement.Parse(""); + [Fact] + public void When_asserting_a_xml_element_is_equivalent_to_a_xml_element_with_elements_missing_it_should_fail() + { + // Arrange + var theElement = XElement.Parse(""); + var otherXElement = XElement.Parse(""); - // Act - Action act = () => - theElement.Should().BeEquivalentTo(otherXElement); + // Act + Action act = () => + theElement.Should().BeEquivalentTo(otherXElement); - // Assert - act.Should().Throw().WithMessage( - "Expected Element \"child2\" in theElement at \"/parent\", but found EndElement \"parent\"."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected EndElement \"parent\" in theElement at \"/parent\", but found Element \"child2\"."); + } - [Fact] - public void When_asserting_a_xml_element_is_equivalent_to_a_different_xml_element_elements_missing_it_should_fail_with_descriptive_message() - { - // Arrange - var theElement = XElement.Parse(""); - var otherXElement = XElement.Parse(""); + [Fact] + public void When_asserting_a_xml_element_is_equivalent_to_a_different_xml_element_with_extra_elements_it_should_fail() + { + // Arrange + var theElement = XElement.Parse(""); + var otherXElement = XElement.Parse(""); - // Act - Action act = () => - theElement.Should().BeEquivalentTo(otherXElement, "because we want to test the failure {0}", "message"); + // Act + Action act = () => + theElement.Should().BeEquivalentTo(otherXElement); - // Assert - act.Should().Throw().WithMessage( - "Expected EndElement \"parent\" in theElement at \"/parent\" because we want to test the failure message, but found Element \"child2\"."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected Element \"child2\" in theElement at \"/parent\", but found EndElement \"parent\"."); + } - [Fact] - public void When_asserting_a_xml_element_is_equivalent_to_a_different_xml_element_with_extra_elements_it_should_fail_with_descriptive_message() - { - // Arrange - var theElement = XElement.Parse(""); - var otherXElement = XElement.Parse(""); + [Fact] + public void When_asserting_a_xml_element_is_equivalent_to_a_different_xml_element_elements_missing_it_should_fail_with_descriptive_message() + { + // Arrange + var theElement = XElement.Parse(""); + var otherXElement = XElement.Parse(""); - // Act - Action act = () => - theElement.Should().BeEquivalentTo(otherXElement, "because we want to test the failure {0}", "message"); + // Act + Action act = () => + theElement.Should().BeEquivalentTo(otherXElement, "because we want to test the failure {0}", "message"); - // Assert - act.Should().Throw().WithMessage( - "Expected Element \"child2\" in theElement at \"/parent\" because we want to test the failure message, but found EndElement \"parent\"."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected EndElement \"parent\" in theElement at \"/parent\" because we want to test the failure message, but found Element \"child2\"."); + } - [Fact] - public void When_asserting_an_empty_xml_element_is_equivalent_to_a_different_xml_element_with_text_content_it_should_fail() - { - // Arrange - var theElement = XElement.Parse(""); - var otherXElement = XElement.Parse("text"); + [Fact] + public void When_asserting_a_xml_element_is_equivalent_to_a_different_xml_element_with_extra_elements_it_should_fail_with_descriptive_message() + { + // Arrange + var theElement = XElement.Parse(""); + var otherXElement = XElement.Parse(""); - // Act - Action act = () => - theElement.Should().BeEquivalentTo(otherXElement, "because we want to test the failure {0}", "message"); + // Act + Action act = () => + theElement.Should().BeEquivalentTo(otherXElement, "because we want to test the failure {0}", "message"); - // Assert - act.Should().Throw().WithMessage( - "Expected content \"text\" in theElement at \"/parent/child\" because we want to test the failure message, but found EndElement \"parent\"."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected Element \"child2\" in theElement at \"/parent\" because we want to test the failure message, but found EndElement \"parent\"."); + } - [Fact] - public void When_an_element_is_null_then_be_equivalent_to_null_succeeds() - { - XElement theElement = null; + [Fact] + public void When_asserting_an_empty_xml_element_is_equivalent_to_a_different_xml_element_with_text_content_it_should_fail() + { + // Arrange + var theElement = XElement.Parse(""); + var otherXElement = XElement.Parse("text"); - // Act - Action act = () => theElement.Should().BeEquivalentTo(null); + // Act + Action act = () => + theElement.Should().BeEquivalentTo(otherXElement, "because we want to test the failure {0}", "message"); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().Throw().WithMessage( + "Expected content \"text\" in theElement at \"/parent/child\" because we want to test the failure message, but found EndElement \"parent\"."); + } - [Fact] - public void When_an_element_is_null_then_be_equivalent_to_an_element_fails() - { - XElement theElement = null; + [Fact] + public void When_an_element_is_null_then_be_equivalent_to_null_succeeds() + { + XElement theElement = null; - // Act - Action act = () => - theElement.Should().BeEquivalentTo(new XElement("element"), "we want to test the failure {0}", "message"); + // Act + Action act = () => theElement.Should().BeEquivalentTo(null); - // Assert - act.Should().Throw() - .WithMessage("Expected theElement to be equivalent to *failure message*, but found \"\"."); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_an_element_is_equivalent_to_null_it_fails() - { - XElement theElement = new XElement("element"); + [Fact] + public void When_an_element_is_null_then_be_equivalent_to_an_element_fails() + { + XElement theElement = null; - // Act - Action act = () => - theElement.Should().BeEquivalentTo(null, "we want to test the failure {0}", "message"); + // Act + Action act = () => + theElement.Should().BeEquivalentTo(new XElement("element"), "we want to test the failure {0}", "message"); - // Assert - act.Should().Throw() - .WithMessage("Expected theElement to be equivalent to \"\" *failure message*, but found ."); - } + // Assert + act.Should().Throw() + .WithMessage("Expected theElement to be equivalent to *failure message*, but found \"\"."); + } - [Fact] - public void When_asserting_a_xml_element_is_not_equivalent_to_a_different_xml_element_with_elements_missing_it_should_succeed() - { - // Arrange - var element = XElement.Parse(""); - var otherXElement = XElement.Parse(""); + [Fact] + public void When_an_element_is_equivalent_to_null_it_fails() + { + XElement theElement = new XElement("element"); - // Act - Action act = () => - element.Should().NotBeEquivalentTo(otherXElement); + // Act + Action act = () => + theElement.Should().BeEquivalentTo(null, "we want to test the failure {0}", "message"); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().Throw() + .WithMessage("Expected theElement to be equivalent to \"\" *failure message*, but found ."); + } - [Fact] - public void When_asserting_a_xml_element_is_not_equivalent_to_a_different_xml_element_with_extra_elements_it_should_succeed() - { - // Arrange - var element = XElement.Parse(""); - var otherXElement = XElement.Parse(""); + [Fact] + public void + When_asserting_an_xml_element_is_equivalent_to_a_different_xml_element_with_different_namespace_prefix_it_should_succeed() + { + // Arrange + var subject = XElement.Parse(""); + var expected = XElement.Parse(""); - // Act - Action act = () => - element.Should().NotBeEquivalentTo(otherXElement); + // Act + Action act = () => + subject.Should().BeEquivalentTo(expected); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_asserting_a_xml_element_is_not_equivalent_to_a_different_xml_element_with_same_structure_it_should_fail() - { - // Arrange - var theElement = XElement.Parse(""); - var otherXElement = XElement.Parse(""); + [Fact] + public void When_asserting_an_xml_element_is_equivalent_to_a_different_xml_element_which_differs_only_on_unused_namespace_declaration_it_should_succeed() + { + // Arrange + var subject = XElement.Parse(""); + var expected = XElement.Parse(""); - // Act - Action act = () => - theElement.Should().NotBeEquivalentTo(otherXElement); + // Act + Action act = () => + subject.Should().BeEquivalentTo(expected); - // Assert - act.Should().Throw().WithMessage( - "Did not expect theElement to be equivalent, but it is."); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_asserting_a_xml_element_is_not_equivalent_to_a_different_xml_element_with_same_contents_but_different_ns_prefixes_it_should_fail() - { - // Arrange - var theElement = XElement.Parse(@""); - var otherXElement = XElement.Parse(@""); + [Fact] + public void When_asserting_an_xml_element_is_equivalent_to_different_xml_element_which_lacks_attributes_it_should_fail_with_descriptive_message() + { + // Arrange + var theElement = XElement.Parse(""); + var expected = XElement.Parse(""); - // Act - Action act = () => - theElement.Should().NotBeEquivalentTo(otherXElement); + // Act + Action act = () => + theElement.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); - // Assert - act.Should().Throw().WithMessage( - "Did not expect theElement to be equivalent, but it is."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected attribute \"a\" in theElement at \"/xml/element\" because we want to test the failure message, but found none."); + } - [Fact] - public void When_asserting_a_xml_element_is_not_equivalent_to_a_different_xml_element_with_same_contents_but_extra_unused_xmlns_declaration_it_should_fail() - { - // Arrange - var theElement = XElement.Parse(@""); - var otherXElement = XElement.Parse(""); + [Fact] + public void When_asserting_an_xml_element_is_equivalent_to_different_xml_element_which_has_extra_attributes_it_should_fail_with_descriptive_message() + { + // Arrange + var theElement = XElement.Parse(""); + var expected = XElement.Parse(""); + + // Act + Action act = () => + theElement.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw().WithMessage( + "Did not expect to find attribute \"a\" in theElement at \"/xml/element\" because we want to test the failure message."); + } + + [Fact] + public void + When_asserting_an_xml_element_is_equivalent_to_different_xml_element_which_has_different_attribute_values_it_should_fail_with_descriptive_message() + { + // Arrange + var theElement = XElement.Parse(""); + var expected = XElement.Parse(""); - // Act - Action act = () => - theElement.Should().NotBeEquivalentTo(otherXElement); + // Act + Action act = () => + theElement.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); - // Assert - act.Should().Throw().WithMessage( - "Did not expect theElement to be equivalent, but it is."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected attribute \"a\" in theElement at \"/xml/element\" to have value \"c\" because we want to test the failure message, but found \"b\"."); + } - [Fact] - public void When_asserting_a_xml_element_is_not_equivalent_to_the_same_xml_element_it_should_fail() - { - // Arrange - var theElement = new XElement("element"); - var sameXElement = theElement; + [Fact] + public void When_asserting_an_xml_element_is_equivalent_to_different_xml_element_which_has_attribute_with_different_namespace_it_should_fail_with_descriptive_message() + { + // Arrange + var theElement = XElement.Parse(""); + var expected = XElement.Parse(""); - // Act - Action act = () => - theElement.Should().NotBeEquivalentTo(sameXElement); + // Act + Action act = () => + theElement.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); - // Assert - act.Should().Throw().WithMessage( - "Did not expect theElement to be equivalent, but it is."); - } + // Assert + act.Should().Throw().WithMessage( + "Did not expect to find attribute \"ns:a\" in theElement at \"/xml/element\" because we want to test the failure message."); + } - [Fact] - public void When_asserting_a_xml_element_is_not_equivalent_to_a_different_xml_element_with_same_structure_it_should_fail_with_descriptive_message() - { - // Arrange - var theElement = XElement.Parse(""); - var otherXElement = XElement.Parse(""); + [Fact] + public void When_asserting_an_xml_element_is_equivalent_to_different_xml_element_which_has_different_text_contents_it_should_fail_with_descriptive_message() + { + // Arrange + var theElement = XElement.Parse("a"); + var expected = XElement.Parse("b"); - // Act - Action act = () => - theElement.Should().NotBeEquivalentTo(otherXElement, "because we want to test the failure {0}", "message"); + // Act + Action act = () => + theElement.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); - // Assert - act.Should().Throw().WithMessage( - "Did not expect theElement to be equivalent because we want to test the failure message, but it is."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected content to be \"b\" in theElement at \"/xml\" because we want to test the failure message, but found \"a\"."); + } - [Fact] - public void When_asserting_a_xml_element_is_not_equivalent_to_the_same_xml_element_it_should_fail_with_descriptive_message() - { - // Arrange - var theElement = XElement.Parse(""); - var sameXElement = theElement; + [Fact] + public void When_asserting_an_xml_element_is_equivalent_to_different_xml_element_with_different_comments_it_should_succeed() + { + // Arrange + var subject = XElement.Parse(""); + var expected = XElement.Parse(""); - // Act - Action act = () => - theElement.Should().NotBeEquivalentTo(sameXElement, "because we want to test the failure {0}", "message"); + // Act + Action act = () => subject.Should().BeEquivalentTo(expected); - // Assert - act.Should().Throw().WithMessage( - "Did not expect theElement to be equivalent because we want to test the failure message, but it is."); + // Assert + act.Should().NotThrow(); + } } - [Fact] - public void - When_asserting_an_xml_element_is_equivalent_to_a_different_xml_element_with_different_namespace_prefix_it_should_succeed() + public class NotBeEquivalentTo { - // Arrange - var subject = XElement.Parse(""); - var expected = XElement.Parse(""); + [Fact] + public void When_asserting_a_xml_element_is_not_equivalent_to_a_different_xml_element_with_elements_missing_it_should_succeed() + { + // Arrange + var element = XElement.Parse(""); + var otherXElement = XElement.Parse(""); - // Act - Action act = () => - subject.Should().BeEquivalentTo(expected); + // Act + Action act = () => + element.Should().NotBeEquivalentTo(otherXElement); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_asserting_an_xml_element_is_equivalent_to_a_different_xml_element_which_differs_only_on_unused_namespace_declaration_it_should_succeed() - { - // Arrange - var subject = XElement.Parse(""); - var expected = XElement.Parse(""); + [Fact] + public void When_asserting_a_xml_element_is_not_equivalent_to_a_different_xml_element_with_extra_elements_it_should_succeed() + { + // Arrange + var element = XElement.Parse(""); + var otherXElement = XElement.Parse(""); - // Act - Action act = () => - subject.Should().BeEquivalentTo(expected); + // Act + Action act = () => + element.Should().NotBeEquivalentTo(otherXElement); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_asserting_an_xml_element_is_equivalent_to_different_xml_element_which_lacks_attributes_it_should_fail_with_descriptive_message() - { - // Arrange - var theElement = XElement.Parse(""); - var expected = XElement.Parse(""); + [Fact] + public void When_asserting_a_xml_element_is_not_equivalent_to_a_different_xml_element_with_same_structure_it_should_fail() + { + // Arrange + var theElement = XElement.Parse(""); + var otherXElement = XElement.Parse(""); - // Act - Action act = () => - theElement.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); + // Act + Action act = () => + theElement.Should().NotBeEquivalentTo(otherXElement); - // Assert - act.Should().Throw().WithMessage( - "Expected attribute \"a\" in theElement at \"/xml/element\" because we want to test the failure message, but found none."); - } + // Assert + act.Should().Throw().WithMessage( + "Did not expect theElement to be equivalent, but it is."); + } - [Fact] - public void When_asserting_an_xml_element_is_equivalent_to_different_xml_element_which_has_extra_attributes_it_should_fail_with_descriptive_message() - { - // Arrange - var theElement = XElement.Parse(""); - var expected = XElement.Parse(""); + [Fact] + public void When_asserting_a_xml_element_is_not_equivalent_to_a_different_xml_element_with_same_contents_but_different_ns_prefixes_it_should_fail() + { + // Arrange + var theElement = XElement.Parse(@""); + var otherXElement = XElement.Parse(@""); - // Act - Action act = () => - theElement.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); + // Act + Action act = () => + theElement.Should().NotBeEquivalentTo(otherXElement); - // Assert - act.Should().Throw().WithMessage( - "Did not expect to find attribute \"a\" in theElement at \"/xml/element\" because we want to test the failure message."); - } + // Assert + act.Should().Throw().WithMessage( + "Did not expect theElement to be equivalent, but it is."); + } - [Fact] - public void - When_asserting_an_xml_element_is_equivalent_to_different_xml_element_which_has_different_attribute_values_it_should_fail_with_descriptive_message() - { - // Arrange - var theElement = XElement.Parse(""); - var expected = XElement.Parse(""); + [Fact] + public void When_asserting_a_xml_element_is_not_equivalent_to_a_different_xml_element_with_same_contents_but_extra_unused_xmlns_declaration_it_should_fail() + { + // Arrange + var theElement = XElement.Parse(@""); + var otherXElement = XElement.Parse(""); - // Act - Action act = () => - theElement.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); + // Act + Action act = () => + theElement.Should().NotBeEquivalentTo(otherXElement); - // Assert - act.Should().Throw().WithMessage( - "Expected attribute \"a\" in theElement at \"/xml/element\" to have value \"c\" because we want to test the failure message, but found \"b\"."); - } + // Assert + act.Should().Throw().WithMessage( + "Did not expect theElement to be equivalent, but it is."); + } - [Fact] - public void When_asserting_an_xml_element_is_equivalent_to_different_xml_element_which_has_attribute_with_different_namespace_it_should_fail_with_descriptive_message() - { - // Arrange - var theElement = XElement.Parse(""); - var expected = XElement.Parse(""); + [Fact] + public void When_asserting_a_xml_element_is_not_equivalent_to_the_same_xml_element_it_should_fail() + { + // Arrange + var theElement = new XElement("element"); + var sameXElement = theElement; - // Act - Action act = () => - theElement.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); + // Act + Action act = () => + theElement.Should().NotBeEquivalentTo(sameXElement); - // Assert - act.Should().Throw().WithMessage( - "Did not expect to find attribute \"ns:a\" in theElement at \"/xml/element\" because we want to test the failure message."); - } + // Assert + act.Should().Throw().WithMessage( + "Did not expect theElement to be equivalent, but it is."); + } - [Fact] - public void When_asserting_an_xml_element_is_equivalent_to_different_xml_element_which_has_different_text_contents_it_should_fail_with_descriptive_message() - { - // Arrange - var theElement = XElement.Parse("a"); - var expected = XElement.Parse("b"); + [Fact] + public void When_asserting_a_xml_element_is_not_equivalent_to_a_different_xml_element_with_same_structure_it_should_fail_with_descriptive_message() + { + // Arrange + var theElement = XElement.Parse(""); + var otherXElement = XElement.Parse(""); - // Act - Action act = () => - theElement.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); + // Act + Action act = () => + theElement.Should().NotBeEquivalentTo(otherXElement, "because we want to test the failure {0}", "message"); - // Assert - act.Should().Throw().WithMessage( - "Expected content to be \"b\" in theElement at \"/xml\" because we want to test the failure message, but found \"a\"."); - } + // Assert + act.Should().Throw().WithMessage( + "Did not expect theElement to be equivalent because we want to test the failure message, but it is."); + } - [Fact] - public void When_asserting_an_xml_element_is_equivalent_to_different_xml_element_with_different_comments_it_should_succeed() - { - // Arrange - var subject = XElement.Parse(""); - var expected = XElement.Parse(""); + [Fact] + public void When_asserting_a_xml_element_is_not_equivalent_to_the_same_xml_element_it_should_fail_with_descriptive_message() + { + // Arrange + var theElement = XElement.Parse(""); + var sameXElement = theElement; - // Act - Action act = () => subject.Should().BeEquivalentTo(expected); + // Act + Action act = () => + theElement.Should().NotBeEquivalentTo(sameXElement, "because we want to test the failure {0}", "message"); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().Throw().WithMessage( + "Did not expect theElement to be equivalent because we want to test the failure message, but it is."); + } - [Fact] - public void When_a_null_element_is_unexpected_equivalent_to_null_it_fails() - { - XElement theElement = null; + [Fact] + public void When_a_null_element_is_unexpected_equivalent_to_null_it_fails() + { + XElement theElement = null; - // Act - Action act = () => theElement.Should().NotBeEquivalentTo(null, "we want to test the failure {0}", "message"); + // Act + Action act = () => theElement.Should().NotBeEquivalentTo(null, "we want to test the failure {0}", "message"); - // Assert - act.Should().Throw() - .WithMessage("Did not expect theElement to be equivalent *failure message*, but it is."); - } + // Assert + act.Should().Throw() + .WithMessage("Did not expect theElement to be equivalent *failure message*, but it is."); + } - [Fact] - public void When_a_null_element_is_not_equivalent_to_an_element_it_succeeds() - { - XElement theElement = null; + [Fact] + public void When_a_null_element_is_not_equivalent_to_an_element_it_succeeds() + { + XElement theElement = null; - // Act - Action act = () => theElement.Should().NotBeEquivalentTo(new XElement("element")); + // Act + Action act = () => theElement.Should().NotBeEquivalentTo(new XElement("element")); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_an_element_is_not_equivalent_to_null_it_succeeds() - { - XElement theElement = new XElement("element"); + [Fact] + public void When_an_element_is_not_equivalent_to_null_it_succeeds() + { + XElement theElement = new XElement("element"); - // Act - Action act = () => theElement.Should().NotBeEquivalentTo(null); + // Act + Action act = () => theElement.Should().NotBeEquivalentTo(null); - // Assert - act.Should().NotThrow(); + // Assert + act.Should().NotThrow(); + } } - #endregion - - #region HaveValue - - [Fact] - public void When_asserting_element_has_a_specific_value_and_it_does_it_should_succeed() + public class HaveValue { - // Arrange - var element = XElement.Parse("grega"); + [Fact] + public void When_asserting_element_has_a_specific_value_and_it_does_it_should_succeed() + { + // Arrange + var element = XElement.Parse("grega"); - // Act - Action act = () => - element.Should().HaveValue("grega"); + // Act + Action act = () => + element.Should().HaveValue("grega"); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_asserting_element_has_a_specific_value_but_it_has_a_different_value_it_should_throw() - { - // Arrange - var theElement = XElement.Parse("grega"); + [Fact] + public void When_asserting_element_has_a_specific_value_but_it_has_a_different_value_it_should_throw() + { + // Arrange + var theElement = XElement.Parse("grega"); - // Act - Action act = () => - theElement.Should().HaveValue("stamac"); + // Act + Action act = () => + theElement.Should().HaveValue("stamac"); - // Assert - act.Should().Throw().WithMessage( - "Expected theElement 'user' to have value \"stamac\", but found \"grega\"."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected theElement 'user' to have value \"stamac\", but found \"grega\"."); + } - [Fact] - public void When_asserting_element_has_a_specific_value_but_it_has_a_different_value_it_should_throw_with_descriptive_message() - { - // Arrange - var theElement = XElement.Parse("grega"); + [Fact] + public void When_asserting_element_has_a_specific_value_but_it_has_a_different_value_it_should_throw_with_descriptive_message() + { + // Arrange + var theElement = XElement.Parse("grega"); - // Act - Action act = () => - theElement.Should().HaveValue("stamac", "because we want to test the failure {0}", "message"); + // Act + Action act = () => + theElement.Should().HaveValue("stamac", "because we want to test the failure {0}", "message"); - // Assert - act.Should().Throw().WithMessage( - "Expected theElement 'user' to have value \"stamac\" because we want to test the failure message, but found \"grega\"."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected theElement 'user' to have value \"stamac\" because we want to test the failure message, but found \"grega\"."); + } - [Fact] - public void When_xml_element_is_null_then_have_value_should_fail() - { - XElement theElement = null; + [Fact] + public void When_xml_element_is_null_then_have_value_should_fail() + { + XElement theElement = null; - // Act - Action act = () => - theElement.Should().HaveValue("value", "we want to test the failure {0}", "message"); + // Act + Action act = () => + theElement.Should().HaveValue("value", "we want to test the failure {0}", "message"); - // Assert - act.Should().Throw() - .WithMessage("Expected the element to have value \"value\" *failure message*, but theElement is ."); + // Assert + act.Should().Throw() + .WithMessage("Expected the element to have value \"value\" *failure message*, but theElement is ."); + } } - #endregion - - #region HaveAttribute - - [Fact] - public void When_asserting_element_has_attribute_with_specific_value_and_it_does_it_should_succeed() + public class HaveAttribute { - // Arrange - var element = XElement.Parse(@""); - - // Act - Action act = () => - element.Should().HaveAttribute("name", "martin"); - - // Assert - act.Should().NotThrow(); - } + [Fact] + public void When_asserting_element_has_attribute_with_specific_value_and_it_does_it_should_succeed() + { + // Arrange + var element = XElement.Parse(@""); - [Fact] - public void When_asserting_element_has_attribute_with_ns_and_specific_value_and_it_does_it_should_succeed() - { - // Arrange - var element = XElement.Parse(@""); + // Act + Action act = () => + element.Should().HaveAttribute("name", "martin"); - // Act - Action act = () => - element.Should().HaveAttribute(XName.Get("name", "http://www.example.com/2012/test"), "martin"); + // Assert + act.Should().NotThrow(); + } - // Assert - act.Should().NotThrow(); - } + [Fact] + public void When_asserting_element_has_attribute_with_ns_and_specific_value_and_it_does_it_should_succeed() + { + // Arrange + var element = XElement.Parse(@""); - [Fact] - public void When_asserting_element_has_attribute_with_specific_value_but_attribute_does_not_exist_it_should_fail() - { - // Arrange - var theElement = XElement.Parse(@""); + // Act + Action act = () => + element.Should().HaveAttribute(XName.Get("name", "http://www.example.com/2012/test"), "martin"); - // Act - Action act = () => - theElement.Should().HaveAttribute("age", "36"); + // Assert + act.Should().NotThrow(); + } - // Assert - act.Should().Throw().WithMessage( - "Expected theElement to have attribute \"age\" with value \"36\", but found no such attribute in "); - } + [Fact] + public void When_asserting_element_has_attribute_with_specific_value_but_attribute_does_not_exist_it_should_fail() + { + // Arrange + var theElement = XElement.Parse(@""); - [Fact] - public void When_asserting_element_has_attribute_with_ns_and_specific_value_but_attribute_does_not_exist_it_should_fail() - { - // Arrange - var theElement = XElement.Parse(@""); + // Act + Action act = () => + theElement.Should().HaveAttribute("age", "36"); - // Act - Action act = () => - theElement.Should().HaveAttribute(XName.Get("age", "http://www.example.com/2012/test"), "36"); + // Assert + act.Should().Throw().WithMessage( + "Expected theElement to have attribute \"age\" with value \"36\", but found no such attribute in "); + } - // Assert - act.Should().Throw().WithMessage( - "Expected theElement to have attribute \"{http://www.example.com/2012/test}age\" with value \"36\"," - + " but found no such attribute in "); - } + [Fact] + public void When_asserting_element_has_attribute_with_ns_and_specific_value_but_attribute_does_not_exist_it_should_fail() + { + // Arrange + var theElement = XElement.Parse(@""); - [Fact] - public void When_asserting_element_has_attribute_with_specific_value_but_attribute_does_not_exist_it_should_fail_with_descriptive_message() - { - // Arrange - var theElement = XElement.Parse(@""); + // Act + Action act = () => + theElement.Should().HaveAttribute(XName.Get("age", "http://www.example.com/2012/test"), "36"); - // Act - Action act = () => - theElement.Should().HaveAttribute("age", "36", "because we want to test the failure {0}", "message"); + // Assert + act.Should().Throw().WithMessage( + "Expected theElement to have attribute \"{http://www.example.com/2012/test}age\" with value \"36\"," + + " but found no such attribute in "); + } - // Assert - act.Should().Throw().WithMessage( - "Expected theElement to have attribute \"age\" with value \"36\" because we want to test the failure message," - + " but found no such attribute in "); - } + [Fact] + public void When_asserting_element_has_attribute_with_specific_value_but_attribute_does_not_exist_it_should_fail_with_descriptive_message() + { + // Arrange + var theElement = XElement.Parse(@""); - [Fact] - public void When_asserting_element_has_attribute_with_ns_and_specific_value_but_attribute_does_not_exist_it_should_fail_with_descriptive_message() - { - // Arrange - var theElement = XElement.Parse(@""); - - // Act - Action act = () => - theElement.Should().HaveAttribute(XName.Get("age", "http://www.example.com/2012/test"), "36", - "because we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw().WithMessage( - "Expected theElement to have attribute \"{http://www.example.com/2012/test}age\" with value \"36\"" - + " because we want to test the failure message," - + " but found no such attribute in "); - } + // Act + Action act = () => + theElement.Should().HaveAttribute("age", "36", "because we want to test the failure {0}", "message"); - [Fact] - public void When_asserting_element_has_attribute_with_specific_value_but_attribute_has_different_value_it_should_fail() - { - // Arrange - var theElement = XElement.Parse(@""); + // Assert + act.Should().Throw().WithMessage( + "Expected theElement to have attribute \"age\" with value \"36\" because we want to test the failure message," + + " but found no such attribute in "); + } - // Act - Action act = () => - theElement.Should().HaveAttribute("name", "dennis"); + [Fact] + public void When_asserting_element_has_attribute_with_ns_and_specific_value_but_attribute_does_not_exist_it_should_fail_with_descriptive_message() + { + // Arrange + var theElement = XElement.Parse(@""); + + // Act + Action act = () => + theElement.Should().HaveAttribute(XName.Get("age", "http://www.example.com/2012/test"), "36", + "because we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw().WithMessage( + "Expected theElement to have attribute \"{http://www.example.com/2012/test}age\" with value \"36\"" + + " because we want to test the failure message," + + " but found no such attribute in "); + } + + [Fact] + public void When_asserting_element_has_attribute_with_specific_value_but_attribute_has_different_value_it_should_fail() + { + // Arrange + var theElement = XElement.Parse(@""); - // Assert - act.Should().Throw().WithMessage( - "Expected attribute \"name\" in theElement to have value \"dennis\", but found \"martin\"."); - } + // Act + Action act = () => + theElement.Should().HaveAttribute("name", "dennis"); - [Fact] - public void When_asserting_element_has_attribute_with_ns_and_specific_value_but_attribute_has_different_value_it_should_fail() - { - // Arrange - var theElement = XElement.Parse(@""); + // Assert + act.Should().Throw().WithMessage( + "Expected attribute \"name\" in theElement to have value \"dennis\", but found \"martin\"."); + } - // Act - Action act = () => - theElement.Should().HaveAttribute(XName.Get("name", "http://www.example.com/2012/test"), "dennis"); + [Fact] + public void When_asserting_element_has_attribute_with_ns_and_specific_value_but_attribute_has_different_value_it_should_fail() + { + // Arrange + var theElement = XElement.Parse(@""); - // Assert - act.Should().Throw().WithMessage( - "Expected attribute \"{http://www.example.com/2012/test}name\" in theElement to have value \"dennis\", but found \"martin\"."); - } + // Act + Action act = () => + theElement.Should().HaveAttribute(XName.Get("name", "http://www.example.com/2012/test"), "dennis"); - [Fact] - public void When_asserting_element_has_attribute_with_specific_value_but_attribute_has_different_value_it_should_fail_with_descriptive_message() - { - // Arrange - var theElement = XElement.Parse(@""); + // Assert + act.Should().Throw().WithMessage( + "Expected attribute \"{http://www.example.com/2012/test}name\" in theElement to have value \"dennis\", but found \"martin\"."); + } - // Act - Action act = () => - theElement.Should().HaveAttribute("name", "dennis", "because we want to test the failure {0}", "message"); + [Fact] + public void When_asserting_element_has_attribute_with_specific_value_but_attribute_has_different_value_it_should_fail_with_descriptive_message() + { + // Arrange + var theElement = XElement.Parse(@""); - // Assert - act.Should().Throw().WithMessage( - "Expected attribute \"name\" in theElement to have value \"dennis\"" - + " because we want to test the failure message, but found \"martin\"."); - } + // Act + Action act = () => + theElement.Should().HaveAttribute("name", "dennis", "because we want to test the failure {0}", "message"); - [Fact] - public void When_asserting_element_has_attribute_with_ns_and_specific_value_but_attribute_has_different_value_it_should_fail_with_descriptive_message() - { - // Arrange - var theElement = XElement.Parse(@""); - - // Act - Action act = () => - theElement.Should().HaveAttribute(XName.Get("name", "http://www.example.com/2012/test"), "dennis", - "because we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw().WithMessage( - "Expected attribute \"{http://www.example.com/2012/test}name\" in theElement to have value \"dennis\"" - + " because we want to test the failure message, but found \"martin\"."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected attribute \"name\" in theElement to have value \"dennis\"" + + " because we want to test the failure message, but found \"martin\"."); + } - [Fact] - public void When_xml_element_is_null_then_have_attribute_should_fail() - { - XElement theElement = null; + [Fact] + public void When_asserting_element_has_attribute_with_ns_and_specific_value_but_attribute_has_different_value_it_should_fail_with_descriptive_message() + { + // Arrange + var theElement = XElement.Parse(@""); + + // Act + Action act = () => + theElement.Should().HaveAttribute(XName.Get("name", "http://www.example.com/2012/test"), "dennis", + "because we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw().WithMessage( + "Expected attribute \"{http://www.example.com/2012/test}name\" in theElement to have value \"dennis\"" + + " because we want to test the failure message, but found \"martin\"."); + } + + [Fact] + public void When_xml_element_is_null_then_have_attribute_should_fail() + { + XElement theElement = null; - // Act - Action act = () => - theElement.Should().HaveAttribute("name", "value", "we want to test the failure {0}", "message"); + // Act + Action act = () => + theElement.Should().HaveAttribute("name", "value", "we want to test the failure {0}", "message"); - // Assert - act.Should().Throw() - .WithMessage( - "Expected attribute \"name\" in element to have value \"value\" *failure message*" + - ", but theElement is ."); - } + // Assert + act.Should().Throw() + .WithMessage( + "Expected attribute \"name\" in element to have value \"value\" *failure message*" + + ", but theElement is ."); + } - [Fact] - public void When_xml_element_is_null_then_have_attribute_with_XName_should_fail() - { - XElement theElement = null; + [Fact] + public void When_xml_element_is_null_then_have_attribute_with_XName_should_fail() + { + XElement theElement = null; - // Act - Action act = () => - theElement.Should().HaveAttribute((XName)"name", "value", "we want to test the failure {0}", "message"); + // Act + Action act = () => + theElement.Should().HaveAttribute((XName)"name", "value", "we want to test the failure {0}", "message"); - // Assert - act.Should().Throw() - .WithMessage( - "Expected attribute \"name\" in element to have value \"value\" *failure message*" + - ", but theElement is ."); - } + // Assert + act.Should().Throw() + .WithMessage( + "Expected attribute \"name\" in element to have value \"value\" *failure message*" + + ", but theElement is ."); + } - [Fact] - public void When_asserting_element_has_an_attribute_with_a_null_name_it_should_throw() - { - XElement theElement = new XElement("element"); + [Fact] + public void When_asserting_element_has_an_attribute_with_a_null_name_it_should_throw() + { + XElement theElement = new XElement("element"); - // Act - Action act = () => - theElement.Should().HaveAttribute(null, "value"); + // Act + Action act = () => + theElement.Should().HaveAttribute(null, "value"); - // Assert - act.Should().ThrowExactly() - .WithParameterName("expectedName"); - } + // Assert + act.Should().ThrowExactly() + .WithParameterName("expectedName"); + } - [Fact] - public void When_asserting_element_has_an_attribute_with_a_null_XName_it_should_throw() - { - XElement theElement = new XElement("element"); + [Fact] + public void When_asserting_element_has_an_attribute_with_a_null_XName_it_should_throw() + { + XElement theElement = new XElement("element"); - // Act - Action act = () => - theElement.Should().HaveAttribute((XName)null, "value"); + // Act + Action act = () => + theElement.Should().HaveAttribute((XName)null, "value"); - // Assert - act.Should().ThrowExactly() - .WithParameterName("expectedName"); - } + // Assert + act.Should().ThrowExactly() + .WithParameterName("expectedName"); + } - [Fact] - public void When_asserting_element_has_an_attribute_with_an_empty_name_it_should_throw() - { - XElement theElement = new XElement("element"); + [Fact] + public void When_asserting_element_has_an_attribute_with_an_empty_name_it_should_throw() + { + XElement theElement = new XElement("element"); - // Act - Action act = () => - theElement.Should().HaveAttribute(string.Empty, "value"); + // Act + Action act = () => + theElement.Should().HaveAttribute(string.Empty, "value"); - // Assert - act.Should().ThrowExactly() - .WithParameterName("expectedName"); + // Assert + act.Should().ThrowExactly() + .WithParameterName("expectedName"); + } } - #endregion - - #region HaveElement - - [Fact] - public void When_asserting_element_has_child_element_and_it_does_it_should_succeed() + public class HaveElement { - // Arrange - var element = XElement.Parse( - @" + [Fact] + public void When_asserting_element_has_child_element_and_it_does_it_should_succeed() + { + // Arrange + var element = XElement.Parse( + @" "); - // Act - Action act = () => - element.Should().HaveElement("child"); + // Act + Action act = () => + element.Should().HaveElement("child"); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_asserting_element_has_child_element_with_ns_and_it_does_it_should_succeed() - { - // Arrange - var element = XElement.Parse( - @" + [Fact] + public void When_asserting_element_has_child_element_with_ns_and_it_does_it_should_succeed() + { + // Arrange + var element = XElement.Parse( + @" "); - // Act - Action act = () => - element.Should().HaveElement(XName.Get("child", "http://www.example.com/2012/test")); + // Act + Action act = () => + element.Should().HaveElement(XName.Get("child", "http://www.example.com/2012/test")); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_asserting_element_has_child_element_but_it_does_not_it_should_fail() - { - // Arrange - var theElement = XElement.Parse( - @" + [Fact] + public void When_asserting_element_has_child_element_but_it_does_not_it_should_fail() + { + // Arrange + var theElement = XElement.Parse( + @" "); - // Act - Action act = () => - theElement.Should().HaveElement("unknown"); + // Act + Action act = () => + theElement.Should().HaveElement("unknown"); - // Assert - act.Should().Throw().WithMessage( - "Expected theElement to have child element \"unknown\", but no such child element was found."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected theElement to have child element \"unknown\", but no such child element was found."); + } - [Fact] - public void When_asserting_element_has_child_element_with_ns_but_it_does_not_it_should_fail() - { - // Arrange - var theElement = XElement.Parse( - @" + [Fact] + public void When_asserting_element_has_child_element_with_ns_but_it_does_not_it_should_fail() + { + // Arrange + var theElement = XElement.Parse( + @" "); - // Act - Action act = () => - theElement.Should().HaveElement(XName.Get("unknown", "http://www.example.com/2012/test")); + // Act + Action act = () => + theElement.Should().HaveElement(XName.Get("unknown", "http://www.example.com/2012/test")); - // Assert - act.Should().Throw().WithMessage( - "Expected theElement to have child element \"{{http://www.example.com/2012/test}}unknown\", but no such child element was found."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected theElement to have child element \"{{http://www.example.com/2012/test}}unknown\", but no such child element was found."); + } - [Fact] - public void When_asserting_element_has_child_element_but_it_does_not_it_should_fail_with_descriptive_message() - { - // Arrange - var theElement = XElement.Parse( - @" + [Fact] + public void When_asserting_element_has_child_element_but_it_does_not_it_should_fail_with_descriptive_message() + { + // Arrange + var theElement = XElement.Parse( + @" "); - // Act - Action act = () => - theElement.Should().HaveElement("unknown", "because we want to test the failure message"); + // Act + Action act = () => + theElement.Should().HaveElement("unknown", "because we want to test the failure message"); - // Assert - act.Should().Throw().WithMessage( - "Expected theElement to have child element \"unknown\" because we want to test the failure message," - + " but no such child element was found."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected theElement to have child element \"unknown\" because we want to test the failure message," + + " but no such child element was found."); + } - [Fact] - public void When_asserting_element_has_child_element_with_ns_but_it_does_not_it_should_fail_with_descriptive_message() - { - // Arrange - var theElement = XElement.Parse( - @" + [Fact] + public void When_asserting_element_has_child_element_with_ns_but_it_does_not_it_should_fail_with_descriptive_message() + { + // Arrange + var theElement = XElement.Parse( + @" "); - // Act - Action act = () => - theElement.Should().HaveElement(XName.Get("unknown", "http://www.example.com/2012/test"), - "because we want to test the failure message"); + // Act + Action act = () => + theElement.Should().HaveElement(XName.Get("unknown", "http://www.example.com/2012/test"), + "because we want to test the failure message"); - // Assert - act.Should().Throw().WithMessage( - "Expected theElement to have child element \"{{http://www.example.com/2012/test}}unknown\"" - + " because we want to test the failure message, but no such child element was found."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected theElement to have child element \"{{http://www.example.com/2012/test}}unknown\"" + + " because we want to test the failure message, but no such child element was found."); + } - [Fact] - public void When_asserting_element_has_child_element_it_should_return_the_matched_element_in_the_which_property() - { - // Arrange - var element = XElement.Parse( - @" + [Fact] + public void When_asserting_element_has_child_element_it_should_return_the_matched_element_in_the_which_property() + { + // Arrange + var element = XElement.Parse( + @" "); - // Act - var matchedElement = element.Should().HaveElement("child").Subject; + // Act + var matchedElement = element.Should().HaveElement("child").Subject; - // Assert - matchedElement.Should().BeOfType() - .And.HaveAttribute("attr", "1"); - matchedElement.Name.Should().Be(XName.Get("child")); - } + // Assert + matchedElement.Should().BeOfType() + .And.HaveAttribute("attr", "1"); + matchedElement.Name.Should().Be(XName.Get("child")); + } - [Fact] - public void When_asserting_element_has_a_child_element_with_a_null_name_it_should_throw() - { - XElement theElement = new XElement("element"); + [Fact] + public void When_asserting_element_has_a_child_element_with_a_null_name_it_should_throw() + { + XElement theElement = new XElement("element"); - // Act - Action act = () => - theElement.Should().HaveElement(null); + // Act + Action act = () => + theElement.Should().HaveElement(null); - // Assert - act.Should().ThrowExactly() - .WithParameterName("expected"); - } + // Assert + act.Should().ThrowExactly() + .WithParameterName("expected"); + } - [Fact] - public void When_asserting_element_has_a_child_element_with_a_null_XName_it_should_throw() - { - XElement theElement = new XElement("element"); + [Fact] + public void When_asserting_element_has_a_child_element_with_a_null_XName_it_should_throw() + { + XElement theElement = new XElement("element"); - // Act - Action act = () => - theElement.Should().HaveElement((XName)null); + // Act + Action act = () => + theElement.Should().HaveElement((XName)null); - // Assert - act.Should().ThrowExactly() - .WithParameterName("expected"); - } + // Assert + act.Should().ThrowExactly() + .WithParameterName("expected"); + } - [Fact] - public void When_asserting_element_has_a_child_element_with_an_empty_name_it_should_throw() - { - XElement theElement = new XElement("element"); + [Fact] + public void When_asserting_element_has_a_child_element_with_an_empty_name_it_should_throw() + { + XElement theElement = new XElement("element"); - // Act - Action act = () => - theElement.Should().HaveElement(string.Empty); + // Act + Action act = () => + theElement.Should().HaveElement(string.Empty); - // Assert - act.Should().ThrowExactly() - .WithParameterName("expected"); + // Assert + act.Should().ThrowExactly() + .WithParameterName("expected"); + } } - #endregion - - #region HaveElement (with occurrence) - - [Fact] - public void Element_has_two_child_elements_and_it_expected_does_it_succeeds() + public class HaveElementWithOccurrence { - // Arrange - var element = XElement.Parse( - @" + [Fact] + public void Element_has_two_child_elements_and_it_expected_does_it_succeeds() + { + // Arrange + var element = XElement.Parse( + @" "); - // Act / Assert - element.Should().HaveElement("child", Exactly.Twice()); - } - - [Fact] - public void Asserting_element_inside_an_assertion_scope_it_checks_the_whole_assertion_scope_before_failing() - { - // Arrange - XElement element = null; + // Act / Assert + element.Should().HaveElement("child", Exactly.Twice()); + } - // Act - Action act = () => + [Fact] + public void Asserting_element_inside_an_assertion_scope_it_checks_the_whole_assertion_scope_before_failing() { - using (new AssertionScope()) - { - element.Should().HaveElement("child", Exactly.Twice()); - element.Should().HaveElement("child", Exactly.Twice()); - } - }; - - // Assert - act.Should().NotThrow(); - } + // Arrange + XElement element = null; - [Fact] - public void Element_has_two_child_elements_and_three_expected_it_fails() - { - // Arrange - var element = XElement.Parse( - @" + // Act + Action act = () => + { + using (new AssertionScope()) + { + element.Should().HaveElement("child", Exactly.Twice()); + element.Should().HaveElement("child", Exactly.Twice()); + } + }; + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void Element_has_two_child_elements_and_three_expected_it_fails() + { + // Arrange + var element = XElement.Parse( + @" "); - // Act - Action act = () => element.Should().HaveElement("child", Exactly.Twice()); + // Act + Action act = () => element.Should().HaveElement("child", Exactly.Twice()); - // Assert - act.Should().Throw() - .WithMessage("Expected element to have an element \"child\"*exactly*2 times, but found it 3 times."); - } + // Assert + act.Should().Throw() + .WithMessage("Expected element to have an element \"child\"*exactly*2 times, but found it 3 times."); + } - [Fact] - public void Element_is_valid_and_expected_null_with_string_overload_it_fails() - { - // Arrange - var element = XElement.Parse( - @" + [Fact] + public void Element_is_valid_and_expected_null_with_string_overload_it_fails() + { + // Arrange + var element = XElement.Parse( + @" "); - // Act - Action act = () => element.Should().HaveElement(null, Exactly.Twice()); + // Act + Action act = () => element.Should().HaveElement(null, Exactly.Twice()); - // Assert - act.Should().Throw().WithMessage( - "Cannot assert the element has an element if the expected name is .*"); - } + // Assert + act.Should().Throw().WithMessage( + "Cannot assert the element has an element if the expected name is .*"); + } - [Fact] - public void Element_is_valid_and_expected_null_with_x_name_overload_it_fails() - { - // Arrange - var element = XElement.Parse( - @" + [Fact] + public void Element_is_valid_and_expected_null_with_x_name_overload_it_fails() + { + // Arrange + var element = XElement.Parse( + @" "); - // Act - Action act = () => element.Should().HaveElement((XName)null, Exactly.Twice()); + // Act + Action act = () => element.Should().HaveElement((XName)null, Exactly.Twice()); - // Assert - act.Should().Throw().WithMessage( - "Cannot assert the element has an element count if the element name is .*"); - } + // Assert + act.Should().Throw().WithMessage( + "Cannot assert the element has an element count if the element name is .*"); + } - [Fact] - public void Chaining_after_a_successful_occurrence_check_does_continue_the_assertion() - { - // Arrange - var element = XElement.Parse( - @" + [Fact] + public void Chaining_after_a_successful_occurrence_check_does_continue_the_assertion() + { + // Arrange + var element = XElement.Parse( + @" "); - // Act / Assert - element.Should().HaveElement("child", AtLeast.Twice()) - .Which.Should().NotBeNull(); - } + // Act / Assert + element.Should().HaveElement("child", AtLeast.Twice()) + .Which.Should().NotBeNull(); + } - [Fact] - public void Chaining_after_a_non_successful_occurrence_check_does_not_continue_the_assertion() - { - // Arrange - var element = XElement.Parse( - @" + [Fact] + public void Chaining_after_a_non_successful_occurrence_check_does_not_continue_the_assertion() + { + // Arrange + var element = XElement.Parse( + @" "); - // Act - Action act = () => element.Should().HaveElement("child", Exactly.Once()) - .Which.Should().NotBeNull(); + // Act + Action act = () => element.Should().HaveElement("child", Exactly.Once()) + .Which.Should().NotBeNull(); - // Assert - act.Should().Throw() - .WithMessage("Expected element to have an element \"child\"*exactly*1 time, but found it 3 times."); - } + // Assert + act.Should().Throw() + .WithMessage("Expected element to have an element \"child\"*exactly*1 time, but found it 3 times."); + } - [Fact] - public void Null_element_is_expected_to_have_an_element_count_it_should_fail() - { - // Arrange - XElement xElement = null; + [Fact] + public void Null_element_is_expected_to_have_an_element_count_it_should_fail() + { + // Arrange + XElement xElement = null; - // Act - Action act = () => xElement.Should().HaveElement("child", AtLeast.Once()); + // Act + Action act = () => xElement.Should().HaveElement("child", AtLeast.Once()); - // Assert - act.Should().Throw().WithMessage( - "Expected* to have an element with count of *, but the element itself is ."); + // Assert + act.Should().Throw().WithMessage( + "Expected* to have an element with count of *, but the element itself is ."); + } } - - #endregion } } diff --git a/Tests/FluentAssertions.Specs/Xml/XmlElementAssertionSpecs.cs b/Tests/FluentAssertions.Specs/Xml/XmlElementAssertionSpecs.cs index fa37811a04..5f4ff8a6d2 100644 --- a/Tests/FluentAssertions.Specs/Xml/XmlElementAssertionSpecs.cs +++ b/Tests/FluentAssertions.Specs/Xml/XmlElementAssertionSpecs.cs @@ -7,455 +7,457 @@ namespace FluentAssertions.Specs.Xml { public class XmlElementAssertionSpecs { - #region BeEquivalent - - [Fact] - public void When_asserting_xml_element_is_equivalent_to_another_xml_element_with_same_contents_it_should_succeed() + public class BeEquivalent { - // This test is basically just a check that the BeEquivalent method - // is available on XmlElementAssertions, which it should be if - // XmlElementAssertions inherits XmlNodeAssertions. - - // Arrange - var xmlDoc = new XmlDocument(); - xmlDoc.LoadXml("grega"); - var element = xmlDoc.DocumentElement; - var expectedDoc = new XmlDocument(); - expectedDoc.LoadXml("grega"); - var expected = expectedDoc.DocumentElement; - - // Act - Action act = () => - element.Should().BeEquivalentTo(expected); - - // Assert - act.Should().NotThrow(); - } - - #endregion - - #region HaveValue - - [Fact] - public void When_asserting_xml_element_has_a_specific_inner_text_and_it_does_it_should_succeed() - { - // Arrange - var xmlDoc = new XmlDocument(); - xmlDoc.LoadXml("grega"); - var element = xmlDoc.DocumentElement; - - // Act - Action act = () => - element.Should().HaveInnerText("grega"); - - // Assert - act.Should().NotThrow(); + [Fact] + public void When_asserting_xml_element_is_equivalent_to_another_xml_element_with_same_contents_it_should_succeed() + { + // This test is basically just a check that the BeEquivalent method + // is available on XmlElementAssertions, which it should be if + // XmlElementAssertions inherits XmlNodeAssertions. + + // Arrange + var xmlDoc = new XmlDocument(); + xmlDoc.LoadXml("grega"); + var element = xmlDoc.DocumentElement; + var expectedDoc = new XmlDocument(); + expectedDoc.LoadXml("grega"); + var expected = expectedDoc.DocumentElement; + + // Act + Action act = () => + element.Should().BeEquivalentTo(expected); + + // Assert + act.Should().NotThrow(); + } } - [Fact] - public void When_asserting_xml_element_has_a_specific_inner_text_but_it_has_a_different_inner_text_it_should_throw() + public class HaveInnerText { - // Arrange - var xmlDoc = new XmlDocument(); - xmlDoc.LoadXml("grega"); - var element = xmlDoc.DocumentElement; - - // Act - Action act = () => - element.Should().HaveInnerText("stamac"); - - // Assert - act.Should().Throw(); + [Fact] + public void When_asserting_xml_element_has_a_specific_inner_text_and_it_does_it_should_succeed() + { + // Arrange + var xmlDoc = new XmlDocument(); + xmlDoc.LoadXml("grega"); + var element = xmlDoc.DocumentElement; + + // Act + Action act = () => + element.Should().HaveInnerText("grega"); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_xml_element_has_a_specific_inner_text_but_it_has_a_different_inner_text_it_should_throw() + { + // Arrange + var xmlDoc = new XmlDocument(); + xmlDoc.LoadXml("grega"); + var element = xmlDoc.DocumentElement; + + // Act + Action act = () => + element.Should().HaveInnerText("stamac"); + + // Assert + act.Should().Throw(); + } + + [Fact] + public void + When_asserting_xml_element_has_a_specific_inner_text_but_it_has_a_different_inner_text_it_should_throw_with_descriptive_message() + { + // Arrange + var document = new XmlDocument(); + document.LoadXml("grega"); + var theElement = document.DocumentElement; + + // Act + Action act = () => + theElement.Should().HaveInnerText("stamac", "because we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw().WithMessage( + "Expected theElement to have value \"stamac\" because we want to test the failure message, but found \"grega\"."); + } } - [Fact] - public void - When_asserting_xml_element_has_a_specific_inner_text_but_it_has_a_different_inner_text_it_should_throw_with_descriptive_message() + public class HaveAttribute { - // Arrange - var document = new XmlDocument(); - document.LoadXml("grega"); - var theElement = document.DocumentElement; - - // Act - Action act = () => - theElement.Should().HaveInnerText("stamac", "because we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw().WithMessage( - "Expected theElement to have value \"stamac\" because we want to test the failure message, but found \"grega\"."); + [Fact] + public void When_asserting_xml_element_has_attribute_with_specific_value_and_it_does_it_should_succeed() + { + // Arrange + var xmlDoc = new XmlDocument(); + xmlDoc.LoadXml(@""); + var element = xmlDoc.DocumentElement; + + // Act + Action act = () => + element.Should().HaveAttribute("name", "martin"); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_xml_element_has_attribute_with_specific_value_but_attribute_does_not_exist_it_should_fail() + { + // Arrange + var xmlDoc = new XmlDocument(); + xmlDoc.LoadXml(@""); + var element = xmlDoc.DocumentElement; + + // Act + Action act = () => + element.Should().HaveAttribute("age", "36"); + + // Assert + act.Should().Throw(); + } + + [Fact] + public void + When_asserting_xml_element_has_attribute_with_specific_value_but_attribute_does_not_exist_it_should_fail_with_descriptive_message() + { + // Arrange + var document = new XmlDocument(); + document.LoadXml(@""); + var theElement = document.DocumentElement; + + // Act + Action act = () => + theElement.Should().HaveAttribute("age", "36", "because we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected theElement to have attribute \"age\" with value \"36\"" + + " because we want to test the failure message" + + ", but found no such attribute in "); + var element = xmlDoc.DocumentElement; + + // Act + Action act = () => + element.Should().HaveAttribute("name", "dennis"); + + // Assert + act.Should().Throw(); + } + + [Fact] + public void + When_asserting_xml_element_has_attribute_with_specific_value_but_attribute_has_different_value_it_should_fail_with_descriptive_message() + { + // Arrange + var document = new XmlDocument(); + document.LoadXml(@""); + var theElement = document.DocumentElement; + + // Act + Action act = () => + theElement.Should().HaveAttribute("name", "dennis", "because we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected attribute \"name\" in theElement to have value \"dennis\"" + + " because we want to test the failure message" + + ", but found \"martin\"."); + } } - #endregion - - #region HaveAttribute - - [Fact] - public void When_asserting_xml_element_has_attribute_with_specific_value_and_it_does_it_should_succeed() + public class HaveAttributeWithNamespace { - // Arrange - var xmlDoc = new XmlDocument(); - xmlDoc.LoadXml(@""); - var element = xmlDoc.DocumentElement; - - // Act - Action act = () => - element.Should().HaveAttribute("name", "martin"); - - // Assert - act.Should().NotThrow(); + [Fact] + public void When_asserting_xml_element_has_attribute_with_ns_and_specific_value_and_it_does_it_should_succeed() + { + // Arrange + var xmlDoc = new XmlDocument(); + xmlDoc.LoadXml(@""); + var element = xmlDoc.DocumentElement; + + // Act + Action act = () => + element.Should().HaveAttributeWithNamespace("name", "http://www.example.com/2012/test", "martin"); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void + When_asserting_xml_element_has_attribute_with_ns_and_specific_value_but_attribute_does_not_exist_it_should_fail() + { + // Arrange + var xmlDoc = new XmlDocument(); + xmlDoc.LoadXml(@""); + var element = xmlDoc.DocumentElement; + + // Act + Action act = () => + element.Should().HaveAttributeWithNamespace("age", "http://www.example.com/2012/test", "36"); + + // Assert + act.Should().Throw(); + } + + [Fact] + public void + When_asserting_xml_element_has_attribute_with_ns_and_specific_value_but_attribute_does_not_exist_it_should_fail_with_descriptive_message() + { + // Arrange + var document = new XmlDocument(); + document.LoadXml(@""); + var theElement = document.DocumentElement; + + // Act + Action act = () => + theElement.Should().HaveAttributeWithNamespace("age", "http://www.example.com/2012/test", "36", + "because we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected theElement to have attribute \"{http://www.example.com/2012/test}age\" with value \"36\"" + + " because we want to test the failure message" + + ", but found no such attribute in "); + var element = xmlDoc.DocumentElement; + + // Act + Action act = () => + element.Should().HaveAttributeWithNamespace("name", "http://www.example.com/2012/test", "dennis"); + + // Assert + act.Should().Throw(); + } + + [Fact] + public void + When_asserting_xml_element_has_attribute_with_ns_and_specific_value_but_attribute_has_different_value_it_should_fail_with_descriptive_message() + { + // Arrange + var document = new XmlDocument(); + document.LoadXml(@""); + var theElement = document.DocumentElement; + + // Act + Action act = () => + theElement.Should().HaveAttributeWithNamespace("name", "http://www.example.com/2012/test", "dennis", + "because we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected attribute \"{http://www.example.com/2012/test}name\" in theElement to have value \"dennis\"" + + " because we want to test the failure message" + + ", but found \"martin\"."); + } } - [Fact] - public void When_asserting_xml_element_has_attribute_with_ns_and_specific_value_and_it_does_it_should_succeed() - { - // Arrange - var xmlDoc = new XmlDocument(); - xmlDoc.LoadXml(@""); - var element = xmlDoc.DocumentElement; - - // Act - Action act = () => - element.Should().HaveAttributeWithNamespace("name", "http://www.example.com/2012/test", "martin"); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_xml_element_has_attribute_with_specific_value_but_attribute_does_not_exist_it_should_fail() - { - // Arrange - var xmlDoc = new XmlDocument(); - xmlDoc.LoadXml(@""); - var element = xmlDoc.DocumentElement; - - // Act - Action act = () => - element.Should().HaveAttribute("age", "36"); - - // Assert - act.Should().Throw(); - } - - [Fact] - public void - When_asserting_xml_element_has_attribute_with_ns_and_specific_value_but_attribute_does_not_exist_it_should_fail() - { - // Arrange - var xmlDoc = new XmlDocument(); - xmlDoc.LoadXml(@""); - var element = xmlDoc.DocumentElement; - - // Act - Action act = () => - element.Should().HaveAttributeWithNamespace("age", "http://www.example.com/2012/test", "36"); - - // Assert - act.Should().Throw(); - } - - [Fact] - public void - When_asserting_xml_element_has_attribute_with_specific_value_but_attribute_does_not_exist_it_should_fail_with_descriptive_message() - { - // Arrange - var document = new XmlDocument(); - document.LoadXml(@""); - var theElement = document.DocumentElement; - - // Act - Action act = () => - theElement.Should().HaveAttribute("age", "36", "because we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected theElement to have attribute \"age\" with value \"36\"" + - " because we want to test the failure message" + - ", but found no such attribute in "); - var theElement = document.DocumentElement; - - // Act - Action act = () => - theElement.Should().HaveAttributeWithNamespace("age", "http://www.example.com/2012/test", "36", - "because we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected theElement to have attribute \"{http://www.example.com/2012/test}age\" with value \"36\"" + - " because we want to test the failure message" + - ", but found no such attribute in "); - var element = xmlDoc.DocumentElement; - - // Act - Action act = () => - element.Should().HaveAttribute("name", "dennis"); - - // Assert - act.Should().Throw(); - } - - [Fact] - public void - When_asserting_xml_element_has_attribute_with_ns_and_specific_value_but_attribute_has_different_value_it_should_fail() - { - // Arrange - var xmlDoc = new XmlDocument(); - xmlDoc.LoadXml(@""); - var element = xmlDoc.DocumentElement; - - // Act - Action act = () => - element.Should().HaveAttributeWithNamespace("name", "http://www.example.com/2012/test", "dennis"); - - // Assert - act.Should().Throw(); - } - - [Fact] - public void - When_asserting_xml_element_has_attribute_with_specific_value_but_attribute_has_different_value_it_should_fail_with_descriptive_message() - { - // Arrange - var document = new XmlDocument(); - document.LoadXml(@""); - var theElement = document.DocumentElement; - - // Act - Action act = () => - theElement.Should().HaveAttribute("name", "dennis", "because we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected attribute \"name\" in theElement to have value \"dennis\"" + - " because we want to test the failure message" + - ", but found \"martin\"."); - } - - [Fact] - public void - When_asserting_xml_element_has_attribute_with_ns_and_specific_value_but_attribute_has_different_value_it_should_fail_with_descriptive_message() - { - // Arrange - var document = new XmlDocument(); - document.LoadXml(@""); - var theElement = document.DocumentElement; - - // Act - Action act = () => - theElement.Should().HaveAttributeWithNamespace("name", "http://www.example.com/2012/test", "dennis", - "because we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected attribute \"{http://www.example.com/2012/test}name\" in theElement to have value \"dennis\"" + - " because we want to test the failure message" + - ", but found \"martin\"."); - } - - #endregion - - #region HaveElement - - [Fact] - public void When_asserting_xml_element_has_child_element_and_it_does_it_should_succeed() + public class HaveElement { - // Arrange - var xml = new XmlDocument(); - xml.LoadXml( - @" + [Fact] + public void When_asserting_xml_element_has_child_element_and_it_does_it_should_succeed() + { + // Arrange + var xml = new XmlDocument(); + xml.LoadXml( + @" "); - var element = xml.DocumentElement; - - // Act - Action act = () => - element.Should().HaveElement("child"); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_xml_element_has_child_element_with_ns_and_it_does_it_should_succeed() - { - // Arrange - var xmlDoc = new XmlDocument(); - xmlDoc.LoadXml( - @" - - "); - var element = xmlDoc.DocumentElement; - - // Act - Action act = () => - element.Should().HaveElementWithNamespace("child", "http://www.example.com/2012/test"); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_xml_element_has_child_element_but_it_does_not_it_should_fail() - { - // Arrange - var xmlDoc = new XmlDocument(); - xmlDoc.LoadXml( - @" - - "); - var element = xmlDoc.DocumentElement; - - // Act - Action act = () => - element.Should().HaveElement("unknown"); - - // Assert - act.Should().Throw(); - } - - [Fact] - public void When_asserting_xml_element_has_child_element_with_ns_but_it_does_not_it_should_fail() - { - // Arrange - var xmlDoc = new XmlDocument(); - xmlDoc.LoadXml( - @" - - "); - var element = xmlDoc.DocumentElement; - - // Act - Action act = () => - element.Should().HaveElementWithNamespace("unknown", "http://www.example.com/2012/test"); - - // Assert - act.Should().Throw(); - } - - [Fact] - public void When_asserting_xml_element_has_child_element_but_it_does_not_it_should_fail_with_descriptive_message() - { - // Arrange - var document = new XmlDocument(); - document.LoadXml( - @" + var element = xml.DocumentElement; + + // Act + Action act = () => + element.Should().HaveElement("child"); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_xml_element_has_child_element_but_it_does_not_it_should_fail() + { + // Arrange + var xmlDoc = new XmlDocument(); + xmlDoc.LoadXml( + @" "); - var theElement = document.DocumentElement; - - // Act - Action act = () => - theElement.Should().HaveElement("unknown", "because we want to test the failure message"); - - // Assert - act.Should().Throw().WithMessage( - "Expected theElement to have child element \"unknown\"" - + " because we want to test the failure message" - + ", but no such child element was found."); - } - - [Fact] - public void When_asserting_xml_element_has_child_element_with_ns_but_it_does_not_it_should_fail_with_descriptive_message() - { - // Arrange - var document = new XmlDocument(); - document.LoadXml( - @" + var element = xmlDoc.DocumentElement; + + // Act + Action act = () => + element.Should().HaveElement("unknown"); + + // Assert + act.Should().Throw(); + } + + [Fact] + public void When_asserting_xml_element_has_child_element_but_it_does_not_it_should_fail_with_descriptive_message() + { + // Arrange + var document = new XmlDocument(); + document.LoadXml( + @" "); - var theElement = document.DocumentElement; - - // Act - Action act = () => - theElement.Should().HaveElementWithNamespace("unknown", "http://www.example.com/2012/test", - "because we want to test the failure message"); - - // Assert - act.Should().Throw().WithMessage( - "Expected theElement to have child element \"{{http://www.example.com/2012/test}}unknown\"" - + " because we want to test the failure message" - + ", but no such child element was found."); - } - - [Fact] - public void When_asserting_xml_element_has_child_element_it_should_return_the_matched_element_in_the_which_property() - { - // Arrange - var xmlDoc = new XmlDocument(); - xmlDoc.LoadXml( - @" + var theElement = document.DocumentElement; + + // Act + Action act = () => + theElement.Should().HaveElement("unknown", "because we want to test the failure message"); + + // Assert + act.Should().Throw().WithMessage( + "Expected theElement to have child element \"unknown\"" + + " because we want to test the failure message" + + ", but no such child element was found."); + } + + [Fact] + public void When_asserting_xml_element_has_child_element_it_should_return_the_matched_element_in_the_which_property() + { + // Arrange + var xmlDoc = new XmlDocument(); + xmlDoc.LoadXml( + @" "); - var element = xmlDoc.DocumentElement; - - // Act - var matchedElement = element.Should().HaveElement("child").Subject; - - // Assert - matchedElement.Should().BeOfType() - .And.HaveAttribute("attr", "1"); - matchedElement.Name.Should().Be("child"); - } - - [Fact] - public void When_asserting_xml_element_with_ns_has_child_element_and_it_does_it_should_succeed() - { - // Arrange - var xmlDoc = new XmlDocument(); - xmlDoc.LoadXml( - @" + var element = xmlDoc.DocumentElement; + + // Act + var matchedElement = element.Should().HaveElement("child").Subject; + + // Assert + matchedElement.Should().BeOfType() + .And.HaveAttribute("attr", "1"); + matchedElement.Name.Should().Be("child"); + } + + [Fact] + public void When_asserting_xml_element_with_ns_has_child_element_and_it_does_it_should_succeed() + { + // Arrange + var xmlDoc = new XmlDocument(); + xmlDoc.LoadXml( + @" value "); - var element = xmlDoc.DocumentElement; - - // Act - Action act = () => - element.Should().HaveElement("child"); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_xml_element_has_child_element_and_it_does_with_ns_it_should_succeed2() - { - // Arrange - var xmlDoc = new XmlDocument(); - xmlDoc.LoadXml( - @" + var element = xmlDoc.DocumentElement; + + // Act + Action act = () => + element.Should().HaveElement("child"); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_xml_element_has_child_element_and_it_does_with_ns_it_should_succeed2() + { + // Arrange + var xmlDoc = new XmlDocument(); + xmlDoc.LoadXml( + @" value "); - var element = xmlDoc.DocumentElement; + var element = xmlDoc.DocumentElement; - // Act - Action act = () => - element.Should().HaveElement("child"); + // Act + Action act = () => + element.Should().HaveElement("child"); - // Assert - act.Should().NotThrow(); + // Assert + act.Should().NotThrow(); + } } - #endregion + public class HaveElementWithNamespace + { + [Fact] + public void When_asserting_xml_element_has_child_element_with_ns_and_it_does_it_should_succeed() + { + // Arrange + var xmlDoc = new XmlDocument(); + xmlDoc.LoadXml( + @" + + "); + var element = xmlDoc.DocumentElement; + + // Act + Action act = () => + element.Should().HaveElementWithNamespace("child", "http://www.example.com/2012/test"); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_xml_element_has_child_element_with_ns_but_it_does_not_it_should_fail() + { + // Arrange + var xmlDoc = new XmlDocument(); + xmlDoc.LoadXml( + @" + + "); + var element = xmlDoc.DocumentElement; + + // Act + Action act = () => + element.Should().HaveElementWithNamespace("unknown", "http://www.example.com/2012/test"); + + // Assert + act.Should().Throw(); + } + + [Fact] + public void When_asserting_xml_element_has_child_element_with_ns_but_it_does_not_it_should_fail_with_descriptive_message() + { + // Arrange + var document = new XmlDocument(); + document.LoadXml( + @" + + "); + var theElement = document.DocumentElement; + + // Act + Action act = () => + theElement.Should().HaveElementWithNamespace("unknown", "http://www.example.com/2012/test", + "because we want to test the failure message"); + + // Assert + act.Should().Throw().WithMessage( + "Expected theElement to have child element \"{{http://www.example.com/2012/test}}unknown\"" + + " because we want to test the failure message" + + ", but no such child element was found."); + } + } } } diff --git a/Tests/FluentAssertions.Specs/Xml/XmlNodeAssertionSpecs.cs b/Tests/FluentAssertions.Specs/Xml/XmlNodeAssertionSpecs.cs index ddd58b3b6b..042ff0498b 100644 --- a/Tests/FluentAssertions.Specs/Xml/XmlNodeAssertionSpecs.cs +++ b/Tests/FluentAssertions.Specs/Xml/XmlNodeAssertionSpecs.cs @@ -7,549 +7,552 @@ namespace FluentAssertions.Specs.Xml { public class XmlNodeAssertionSpecs { - #region BeSameAs / NotBeSameAs - - [Fact] - public void When_asserting_an_xml_node_is_the_same_as_the_same_xml_node_it_should_succeed() - { - // Arrange - var doc = new XmlDocument(); - - // Act - Action act = () => - doc.Should().BeSameAs(doc); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_an_xml_node_is_same_as_a_different_xml_node_it_should_fail_with_descriptive_message() - { - // Arrange - var theDocument = new XmlDocument(); - theDocument.LoadXml(""); - var otherNode = new XmlDocument(); - otherNode.LoadXml(""); - - // Act - Action act = () => - theDocument.Should().BeSameAs(otherNode, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected theDocument to refer to because we want to test the failure message, but found ."); - } - - [Fact] - public void When_asserting_an_xml_node_is_same_as_a_different_xml_node_it_should_fail_with_descriptive_message_and_truncate_xml() - { - // Arrange - var theDocument = new XmlDocument(); - theDocument.LoadXml("Some very long text that should be truncated."); - var otherNode = new XmlDocument(); - otherNode.LoadXml(""); - - // Act - Action act = () => - theDocument.Should().BeSameAs(otherNode, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected theDocument to refer to because we want to test the failure message, but found Some very long…."); - } - - [Fact] - public void When_asserting_the_equality_of_an_xml_node_but_is_null_it_should_throw_appropriately() - { - // Arrange - XmlDocument theDocument = null; - var expected = new XmlDocument(); - expected.LoadXml(""); - - // Act - Action act = () => theDocument.Should().BeSameAs(expected); - - // Assert - act.Should().Throw() - .WithMessage("Expected theDocument to refer to *xml*, but found ."); - } - - #endregion - - #region BeNull / NotBeNull - - [Fact] - public void When_asserting_an_xml_node_is_null_and_it_is_it_should_succeed() - { - // Arrange - XmlNode node = null; - - // Act - Action act = () => - node.Should().BeNull(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_an_xml_node_is_null_but_it_is_not_it_should_fail() - { - // Arrange - var xmlDoc = new XmlDocument(); - xmlDoc.LoadXml(""); - - // Act - Action act = () => - xmlDoc.Should().BeNull(); - - // Assert - act.Should().Throw(); - } - - [Fact] - public void When_asserting_an_xml_node_is_null_but_it_is_not_it_should_fail_with_descriptive_message() - { - // Arrange - var theDocument = new XmlDocument(); - theDocument.LoadXml(""); - - // Act - Action act = () => - theDocument.Should().BeNull("because we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected theDocument to be because we want to test the failure message," + - " but found ."); - } - - [Fact] - public void When_asserting_a_non_null_xml_node_is_not_null_it_should_succeed() - { - // Arrange - var xmlDoc = new XmlDocument(); - xmlDoc.LoadXml(""); - - // Act - Action act = () => - xmlDoc.Should().NotBeNull(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_null_xml_node_is_not_null_it_should_fail() - { - // Arrange - XmlDocument xmlDoc = null; - - // Act - Action act = () => - xmlDoc.Should().NotBeNull(); - - // Assert - act.Should().Throw(); - } - - [Fact] - public void When_asserting_a_null_xml_node_is_not_null_it_should_fail_with_descriptive_message() - { - // Arrange - XmlDocument theDocument = null; - - // Act - Action act = () => - theDocument.Should().NotBeNull("because we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected theDocument not to be because we want to test the failure message."); - } - - #endregion - - #region BeEquivalentTo / NotBeEquivalentTo - - [Fact] - public void When_asserting_an_xml_node_is_equivalent_to_the_same_xml_node_it_should_succeed() - { - // Arrange - var doc = new XmlDocument(); - - // Act - Action act = () => - doc.Should().BeEquivalentTo(doc); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_an_xml_node_is_not_equivalent_to_som_other_xml_node_it_should_succeed() - { - // Arrange - var subject = new XmlDocument(); - subject.LoadXml("a"); - var unexpected = new XmlDocument(); - unexpected.LoadXml("b"); - - // Act - Action act = () => - subject.Should().NotBeEquivalentTo(unexpected); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_an_xml_node_is_not_equivalent_to_same_xml_node_it_should_fail_with_descriptive_message() - { - // Arrange - var theDocument = new XmlDocument(); - theDocument.LoadXml("a"); - - // Act - Action act = () => - theDocument.Should().NotBeEquivalentTo(theDocument, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Did not expect theDocument to be equivalent because we want to test the failure message, but it is."); - } - - [Fact] - public void When_asserting_an_xml_node_is_equivalent_to_a_different_xml_node_with_other_contents_it_should_fail_with_descriptive_message() - { - // Arrange - var theDocument = new XmlDocument(); - theDocument.LoadXml(""); - var expected = new XmlDocument(); - expected.LoadXml(""); - - // Act - Action act = () => - theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected local name of element in theDocument at \"/\" to be \"expected\" because we want to test the failure message, but found \"subject\"."); - } - - [Fact] - public void When_asserting_an_xml_node_is_equivalent_to_a_different_xml_node_with_same_contents_it_should_succeed() - { - // Arrange - var xml = "datadata"; - - var subject = new XmlDocument(); - subject.LoadXml(xml); - var expected = new XmlDocument(); - expected.LoadXml(xml); - - // Act - Action act = () => - subject.Should().BeEquivalentTo(expected); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_assertion_an_xml_node_is_equivalent_to_a_different_xml_node_with_different_namespace_prefix_it_should_succeed() + public class BeSameAs { - // Arrange - var subject = new XmlDocument(); - subject.LoadXml(""); - var expected = new XmlDocument(); - expected.LoadXml(""); - - // Act - Action act = () => - subject.Should().BeEquivalentTo(expected); - - // Assert - act.Should().NotThrow(); + [Fact] + public void When_asserting_an_xml_node_is_the_same_as_the_same_xml_node_it_should_succeed() + { + // Arrange + var doc = new XmlDocument(); + + // Act + Action act = () => + doc.Should().BeSameAs(doc); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_an_xml_node_is_same_as_a_different_xml_node_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = new XmlDocument(); + theDocument.LoadXml(""); + var otherNode = new XmlDocument(); + otherNode.LoadXml(""); + + // Act + Action act = () => + theDocument.Should().BeSameAs(otherNode, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected theDocument to refer to because we want to test the failure message, but found ."); + } + + [Fact] + public void When_asserting_an_xml_node_is_same_as_a_different_xml_node_it_should_fail_with_descriptive_message_and_truncate_xml() + { + // Arrange + var theDocument = new XmlDocument(); + theDocument.LoadXml("Some very long text that should be truncated."); + var otherNode = new XmlDocument(); + otherNode.LoadXml(""); + + // Act + Action act = () => + theDocument.Should().BeSameAs(otherNode, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected theDocument to refer to because we want to test the failure message, but found Some very long…."); + } + + [Fact] + public void When_asserting_the_equality_of_an_xml_node_but_is_null_it_should_throw_appropriately() + { + // Arrange + XmlDocument theDocument = null; + var expected = new XmlDocument(); + expected.LoadXml(""); + + // Act + Action act = () => theDocument.Should().BeSameAs(expected); + + // Assert + act.Should().Throw() + .WithMessage("Expected theDocument to refer to *xml*, but found ."); + } } - [Fact] - public void When_asserting_an_xml_node_is_equivalent_to_a_different_xml_node_which_differs_only_on_unused_namespace_declaration_it_should_succeed() + public class BeNull { - // Arrange - var subject = new XmlDocument(); - subject.LoadXml(""); - var expected = new XmlDocument(); - expected.LoadXml(""); - - // Act - Action act = () => - subject.Should().BeEquivalentTo(expected); - - // Assert - act.Should().NotThrow(); + [Fact] + public void When_asserting_an_xml_node_is_null_and_it_is_it_should_succeed() + { + // Arrange + XmlNode node = null; + + // Act + Action act = () => + node.Should().BeNull(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_an_xml_node_is_null_but_it_is_not_it_should_fail() + { + // Arrange + var xmlDoc = new XmlDocument(); + xmlDoc.LoadXml(""); + + // Act + Action act = () => + xmlDoc.Should().BeNull(); + + // Assert + act.Should().Throw(); + } + + [Fact] + public void When_asserting_an_xml_node_is_null_but_it_is_not_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = new XmlDocument(); + theDocument.LoadXml(""); + + // Act + Action act = () => + theDocument.Should().BeNull("because we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected theDocument to be because we want to test the failure message," + + " but found ."); + } } - [Fact] - public void When_asserting_an_xml_node_is_equivalent_to_a_different_XmlDocument_which_differs_on_a_child_element_name_it_should_fail_with_descriptive_message() + public class NotBeNull { - // Arrange - var theDocument = new XmlDocument(); - theDocument.LoadXml(""); - var expected = new XmlDocument(); - expected.LoadXml(""); - - // Act - Action act = () => - theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected local name of element in theDocument at \"/xml/child\" to be \"expected\" because we want to test the failure message, but found \"subject\"."); + [Fact] + public void When_asserting_a_non_null_xml_node_is_not_null_it_should_succeed() + { + // Arrange + var xmlDoc = new XmlDocument(); + xmlDoc.LoadXml(""); + + // Act + Action act = () => + xmlDoc.Should().NotBeNull(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_null_xml_node_is_not_null_it_should_fail() + { + // Arrange + XmlDocument xmlDoc = null; + + // Act + Action act = () => + xmlDoc.Should().NotBeNull(); + + // Assert + act.Should().Throw(); + } + + [Fact] + public void When_asserting_a_null_xml_node_is_not_null_it_should_fail_with_descriptive_message() + { + // Arrange + XmlDocument theDocument = null; + + // Act + Action act = () => + theDocument.Should().NotBeNull("because we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected theDocument not to be because we want to test the failure message."); + } } - - [Fact] - public void When_asserting_an_xml_node_is_equivalent_to_a_different_xml_node_which_differs_on_a_child_element_namespace_it_should_fail_with_descriptive_message() - { - // Arrange - var theDocument = new XmlDocument(); - theDocument.LoadXml(""); - var expected = new XmlDocument(); - expected.LoadXml(""); - - // Act - Action act = () => - theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected namespace of element \"data\" in theDocument at \"/xml/child\" to be \"urn:b\" because we want to test the failure message, but found \"urn:a\"."); - } - - [Fact] - public void When_asserting_an_xml_node_is_equivalent_to_different_xml_node_which_contains_an_unexpected_node_type_it_should_fail_with_descriptive_message() - { - // Arrange - var theDocument = new XmlDocument(); - theDocument.LoadXml("data"); - var expected = new XmlDocument(); - expected.LoadXml(""); - - // Act - Action act = () => - theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected Element \"data\" in theDocument at \"/xml\" because we want to test the failure message, but found content \"data\"."); - } - - [Fact] - public void When_asserting_an_xml_node_is_equivalent_to_different_xml_node_which_contains_extra_elements_it_should_fail_with_descriptive_message() + + public class BeEquivalentTo { - // Arrange - var theDocument = new XmlDocument(); - theDocument.LoadXml(""); - var expected = new XmlDocument(); - expected.LoadXml(""); - - // Act - Action act = () => - theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected end of document in theDocument because we want to test the failure message, but found \"data\"."); + [Fact] + public void When_asserting_an_xml_node_is_equivalent_to_the_same_xml_node_it_should_succeed() + { + // Arrange + var doc = new XmlDocument(); + + // Act + Action act = () => + doc.Should().BeEquivalentTo(doc); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_an_xml_node_is_equivalent_to_a_different_xml_node_with_other_contents_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = new XmlDocument(); + theDocument.LoadXml(""); + var expected = new XmlDocument(); + expected.LoadXml(""); + + // Act + Action act = () => + theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected local name of element in theDocument at \"/\" to be \"expected\" because we want to test the failure message, but found \"subject\"."); + } + + [Fact] + public void When_asserting_an_xml_node_is_equivalent_to_a_different_xml_node_with_same_contents_it_should_succeed() + { + // Arrange + var xml = "datadata"; + + var subject = new XmlDocument(); + subject.LoadXml(xml); + var expected = new XmlDocument(); + expected.LoadXml(xml); + + // Act + Action act = () => + subject.Should().BeEquivalentTo(expected); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_assertion_an_xml_node_is_equivalent_to_a_different_xml_node_with_different_namespace_prefix_it_should_succeed() + { + // Arrange + var subject = new XmlDocument(); + subject.LoadXml(""); + var expected = new XmlDocument(); + expected.LoadXml(""); + + // Act + Action act = () => + subject.Should().BeEquivalentTo(expected); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_an_xml_node_is_equivalent_to_a_different_xml_node_which_differs_only_on_unused_namespace_declaration_it_should_succeed() + { + // Arrange + var subject = new XmlDocument(); + subject.LoadXml(""); + var expected = new XmlDocument(); + expected.LoadXml(""); + + // Act + Action act = () => + subject.Should().BeEquivalentTo(expected); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_an_xml_node_is_equivalent_to_a_different_XmlDocument_which_differs_on_a_child_element_name_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = new XmlDocument(); + theDocument.LoadXml(""); + var expected = new XmlDocument(); + expected.LoadXml(""); + + // Act + Action act = () => + theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected local name of element in theDocument at \"/xml/child\" to be \"expected\" because we want to test the failure message, but found \"subject\"."); + } + + [Fact] + public void When_asserting_an_xml_node_is_equivalent_to_a_different_xml_node_which_differs_on_a_child_element_namespace_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = new XmlDocument(); + theDocument.LoadXml(""); + var expected = new XmlDocument(); + expected.LoadXml(""); + + // Act + Action act = () => + theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected namespace of element \"data\" in theDocument at \"/xml/child\" to be \"urn:b\" because we want to test the failure message, but found \"urn:a\"."); + } + + [Fact] + public void When_asserting_an_xml_node_is_equivalent_to_different_xml_node_which_contains_an_unexpected_node_type_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = new XmlDocument(); + theDocument.LoadXml("data"); + var expected = new XmlDocument(); + expected.LoadXml(""); + + // Act + Action act = () => + theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected Element \"data\" in theDocument at \"/xml\" because we want to test the failure message, but found content \"data\"."); + } + + [Fact] + public void When_asserting_an_xml_node_is_equivalent_to_different_xml_node_which_contains_extra_elements_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = new XmlDocument(); + theDocument.LoadXml(""); + var expected = new XmlDocument(); + expected.LoadXml(""); + + // Act + Action act = () => + theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected end of document in theDocument because we want to test the failure message, but found \"data\"."); + } + + [Fact] + public void When_asserting_an_xml_node_is_equivalent_to_different_xml_node_which_lacks_elements_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = new XmlDocument(); + theDocument.LoadXml(""); + var expected = new XmlDocument(); + expected.LoadXml(""); + + // Act + Action act = () => + theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected \"data\" in theDocument because we want to test the failure message, but found end of document."); + } + + [Fact] + public void When_asserting_an_xml_node_is_equivalent_to_different_xml_node_which_lacks_attributes_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = new XmlDocument(); + theDocument.LoadXml(""); + var expected = new XmlDocument(); + expected.LoadXml(""); + + // Act + Action act = () => + theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected attribute \"a\" in theDocument at \"/xml/element\" because we want to test the failure message, but found none."); + } + + [Fact] + public void When_asserting_an_xml_node_is_equivalent_to_different_xml_node_which_has_extra_attributes_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = new XmlDocument(); + theDocument.LoadXml(""); + var expected = new XmlDocument(); + expected.LoadXml(""); + + // Act + Action act = () => + theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Did not expect to find attribute \"a\" in theDocument at \"/xml/element\" because we want to test the failure message."); + } + + [Fact] + public void When_asserting_an_xml_node_is_equivalent_to_different_xml_node_which_has_different_attribute_values_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = new XmlDocument(); + theDocument.LoadXml(""); + var expected = new XmlDocument(); + expected.LoadXml(""); + + // Act + Action act = () => + theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected attribute \"a\" in theDocument at \"/xml/element\" to have value \"c\" because we want to test the failure message, but found \"b\"."); + } + + [Fact] + public void When_asserting_an_xml_node_is_equivalent_to_different_xml_node_which_has_attribute_with_different_namespace_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = new XmlDocument(); + theDocument.LoadXml(""); + var expected = new XmlDocument(); + expected.LoadXml(""); + + // Act + Action act = () => + theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Did not expect to find attribute \"ns:a\" in theDocument at \"/xml/element\" because we want to test the failure message."); + } + + [Fact] + public void When_asserting_an_xml_node_is_equivalent_to_different_xml_node_which_has_different_text_contents_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = new XmlDocument(); + theDocument.LoadXml("a"); + var expected = new XmlDocument(); + expected.LoadXml("b"); + + // Act + Action act = () => + theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected content to be \"b\" in theDocument at \"/xml\" because we want to test the failure message, but found \"a\"."); + } + + [Fact] + public void When_asserting_an_xml_node_is_equivalent_to_different_xml_node_with_different_comments_it_should_succeed() + { + // Arrange + var subject = new XmlDocument(); + subject.LoadXml(""); + var expected = new XmlDocument(); + expected.LoadXml(""); + + // Act + Action act = () => subject.Should().BeEquivalentTo(expected); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_an_xml_node_is_equivalent_to_different_xml_node_with_different_insignificant_whitespace_it_should_succeed() + { + // Arrange + var subject = new XmlDocument() { PreserveWhitespace = true }; + subject.LoadXml(""); + var expected = new XmlDocument() { PreserveWhitespace = true }; + expected.LoadXml("\n \n \r\n"); + + // Act + Action act = () => subject.Should().BeEquivalentTo(expected); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_an_xml_node_is_equivalent_that_contains_an_unsupported_node_it_should_throw_a_descriptive_message() + { + // Arrange + var theDocument = new XmlDocument(); + theDocument.LoadXml(""); + + // Act + Action act = () => theDocument.Should().BeEquivalentTo(theDocument); + + // Assert + act.Should().Throw() + .WithMessage("CDATA found at /xml is not supported for equivalency comparison."); + } + + [Fact] + public void + When_asserting_an_xml_node_is_equivalent_that_isnt_it_should_include_the_right_location_in_the_descriptive_message() + { + // Arrange + var theDocument = new XmlDocument(); + theDocument.LoadXml(""); + var expected = new XmlDocument(); + expected.LoadXml(""); + + // Act + Action act = () => theDocument.Should().BeEquivalentTo(expected); + + // Assert + act.Should().Throw() + .WithMessage("Expected attribute \"c\" in theDocument at \"/xml/b\" to have value \"e\", but found \"d\"."); + } } - [Fact] - public void When_asserting_an_xml_node_is_equivalent_to_different_xml_node_which_lacks_elements_it_should_fail_with_descriptive_message() + public class NotBeEquivalentTo { - // Arrange - var theDocument = new XmlDocument(); - theDocument.LoadXml(""); - var expected = new XmlDocument(); - expected.LoadXml(""); - - // Act - Action act = () => - theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected \"data\" in theDocument because we want to test the failure message, but found end of document."); + [Fact] + public void When_asserting_an_xml_node_is_not_equivalent_to_som_other_xml_node_it_should_succeed() + { + // Arrange + var subject = new XmlDocument(); + subject.LoadXml("a"); + var unexpected = new XmlDocument(); + unexpected.LoadXml("b"); + + // Act + Action act = () => + subject.Should().NotBeEquivalentTo(unexpected); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_an_xml_node_is_not_equivalent_to_same_xml_node_it_should_fail_with_descriptive_message() + { + // Arrange + var theDocument = new XmlDocument(); + theDocument.LoadXml("a"); + + // Act + Action act = () => + theDocument.Should().NotBeEquivalentTo(theDocument, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Did not expect theDocument to be equivalent because we want to test the failure message, but it is."); + } } - - [Fact] - public void When_asserting_an_xml_node_is_equivalent_to_different_xml_node_which_lacks_attributes_it_should_fail_with_descriptive_message() - { - // Arrange - var theDocument = new XmlDocument(); - theDocument.LoadXml(""); - var expected = new XmlDocument(); - expected.LoadXml(""); - - // Act - Action act = () => - theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected attribute \"a\" in theDocument at \"/xml/element\" because we want to test the failure message, but found none."); - } - - [Fact] - public void When_asserting_an_xml_node_is_equivalent_to_different_xml_node_which_has_extra_attributes_it_should_fail_with_descriptive_message() - { - // Arrange - var theDocument = new XmlDocument(); - theDocument.LoadXml(""); - var expected = new XmlDocument(); - expected.LoadXml(""); - - // Act - Action act = () => - theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Did not expect to find attribute \"a\" in theDocument at \"/xml/element\" because we want to test the failure message."); - } - - [Fact] - public void When_asserting_an_xml_node_is_equivalent_to_different_xml_node_which_has_different_attribute_values_it_should_fail_with_descriptive_message() - { - // Arrange - var theDocument = new XmlDocument(); - theDocument.LoadXml(""); - var expected = new XmlDocument(); - expected.LoadXml(""); - - // Act - Action act = () => - theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected attribute \"a\" in theDocument at \"/xml/element\" to have value \"c\" because we want to test the failure message, but found \"b\"."); - } - - [Fact] - public void When_asserting_an_xml_node_is_equivalent_to_different_xml_node_which_has_attribute_with_different_namespace_it_should_fail_with_descriptive_message() - { - // Arrange - var theDocument = new XmlDocument(); - theDocument.LoadXml(""); - var expected = new XmlDocument(); - expected.LoadXml(""); - - // Act - Action act = () => - theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Did not expect to find attribute \"ns:a\" in theDocument at \"/xml/element\" because we want to test the failure message."); - } - - [Fact] - public void When_asserting_an_xml_node_is_equivalent_to_different_xml_node_which_has_different_text_contents_it_should_fail_with_descriptive_message() - { - // Arrange - var theDocument = new XmlDocument(); - theDocument.LoadXml("a"); - var expected = new XmlDocument(); - expected.LoadXml("b"); - - // Act - Action act = () => - theDocument.Should().BeEquivalentTo(expected, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected content to be \"b\" in theDocument at \"/xml\" because we want to test the failure message, but found \"a\"."); - } - - [Fact] - public void When_asserting_an_xml_node_is_equivalent_to_different_xml_node_with_different_comments_it_should_succeed() - { - // Arrange - var subject = new XmlDocument(); - subject.LoadXml(""); - var expected = new XmlDocument(); - expected.LoadXml(""); - - // Act - Action act = () => subject.Should().BeEquivalentTo(expected); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_an_xml_node_is_equivalent_to_different_xml_node_with_different_insignificant_whitespace_it_should_succeed() - { - // Arrange - var subject = new XmlDocument() { PreserveWhitespace = true }; - subject.LoadXml(""); - var expected = new XmlDocument() { PreserveWhitespace = true }; - expected.LoadXml("\n \n \r\n"); - - // Act - Action act = () => subject.Should().BeEquivalentTo(expected); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_an_xml_node_is_equivalent_that_contains_an_unsupported_node_it_should_throw_a_descriptive_message() - { - // Arrange - var theDocument = new XmlDocument(); - theDocument.LoadXml(""); - - // Act - Action act = () => theDocument.Should().BeEquivalentTo(theDocument); - - // Assert - act.Should().Throw() - .WithMessage("CDATA found at /xml is not supported for equivalency comparison."); - } - - [Fact] - public void - When_asserting_an_xml_node_is_equivalent_that_isnt_it_should_include_the_right_location_in_the_descriptive_message() - { - // Arrange - var theDocument = new XmlDocument(); - theDocument.LoadXml(""); - var expected = new XmlDocument(); - expected.LoadXml(""); - - // Act - Action act = () => theDocument.Should().BeEquivalentTo(expected); - - // Assert - act.Should().Throw() - .WithMessage("Expected attribute \"c\" in theDocument at \"/xml/b\" to have value \"e\", but found \"d\"."); - } - - #endregion } } From d96b587e469bdd79d45adefea78a2de7d6890a8d Mon Sep 17 00:00:00 2001 From: Lukas Gasselsberger Date: Sat, 23 Apr 2022 21:03:12 +0200 Subject: [PATCH 2/2] Seperate all `Types` assertions into nested classes --- .../Types/MethodBaseAssertionSpecs.cs | 1526 ++++++++-------- .../Types/MethodInfoAssertionSpecs.cs | 1190 ++++++------ .../Types/MethodInfoSelectorAssertionSpecs.cs | 790 ++++---- .../Types/PropertyInfoAssertionSpecs.cs | 1606 ++++++++--------- .../PropertyInfoSelectorAssertionSpecs.cs | 535 +++--- .../Types/TypeAssertionSpecs.Be.cs | 396 ++-- .../Types/TypeAssertionSpecs.BeAbstract.cs | 264 ++- .../TypeAssertionSpecs.BeAssignableTo.cs | 642 ++++--- .../TypeAssertionSpecs.BeDecoratedWith.cs | 376 ++-- ...AssertionSpecs.BeDecoratedWithOrInherit.cs | 378 ++-- .../Types/TypeAssertionSpecs.BeDerivedFrom.cs | 488 +++-- .../Types/TypeAssertionSpecs.BeSealed.cs | 264 ++- .../Types/TypeAssertionSpecs.BeStatic.cs | 264 ++- .../TypeAssertionSpecs.HaveAccessModifier.cs | 622 +++---- .../TypeAssertionSpecs.HaveConstructor.cs | 252 ++- ...peAssertionSpecs.HaveDefaultConstructor.cs | 324 ++-- ...ionSpecs.HaveExplicitConversionOperator.cs | 520 +++--- .../TypeAssertionSpecs.HaveExplicitMethod.cs | 1010 ++++++----- ...TypeAssertionSpecs.HaveExplicitProperty.cs | 890 +++++---- ...ionSpecs.HaveImplicitConversionOperator.cs | 556 +++--- .../Types/TypeAssertionSpecs.HaveIndexer.cs | 318 ++-- .../Types/TypeAssertionSpecs.HaveMethod.cs | 436 +++-- .../Types/TypeAssertionSpecs.HaveProperty.cs | 473 +++-- .../Types/TypeAssertionSpecs.Implement.cs | 314 ++-- .../Types/TypeSelectorAssertionSpecs.cs | 1384 +++++++------- 25 files changed, 7858 insertions(+), 7960 deletions(-) diff --git a/Tests/FluentAssertions.Specs/Types/MethodBaseAssertionSpecs.cs b/Tests/FluentAssertions.Specs/Types/MethodBaseAssertionSpecs.cs index fbdec52df0..107a2bb2d9 100644 --- a/Tests/FluentAssertions.Specs/Types/MethodBaseAssertionSpecs.cs +++ b/Tests/FluentAssertions.Specs/Types/MethodBaseAssertionSpecs.cs @@ -8,774 +8,766 @@ namespace FluentAssertions.Specs.Types { public class MethodBaseAssertionSpecs { - #region Return - - [Fact] - public void When_asserting_an_int_method_returns_int_it_succeeds() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("IntMethod"); - - // Act - Action act = () => - methodInfo.Should().Return(typeof(int)); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_an_int_method_returns_string_it_fails_with_a_useful_message() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("IntMethod"); - - // Act - Action act = () => - methodInfo.Should().Return(typeof(string), "we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected the return type of method IntMethod to be System.String because we want to test the " + - "error message, but it is \"System.Int32\"."); - } - - [Fact] - public void When_asserting_a_void_method_returns_string_it_fails_with_a_useful_message() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("VoidMethod"); - - // Act - Action act = () => - methodInfo.Should().Return(typeof(string), "we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected the return type of method VoidMethod to be System.String because we want to test the " + - "error message, but it is \"System.Void\"."); - } - - [Fact] - public void When_subject_is_null_return_should_fail() - { - // Arrange - MethodInfo methodInfo = null; - - // Act - Action act = () => - methodInfo.Should().Return(typeof(string), "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected the return type of method to be *.String *failure message*, but methodInfo is ."); - } - - [Fact] - public void When_asserting_method_return_type_is_null_it_should_throw() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("IntMethod"); - - // Act - Action act = () => - methodInfo.Should().Return(null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("returnType"); - } - - #endregion - - #region NotReturn - - [Fact] - public void When_asserting_an_int_method_does_not_return_string_it_succeeds() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("IntMethod"); - - // Act - Action act = () => - methodInfo.Should().NotReturn(typeof(string)); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_an_int_method_does_not_return_int_it_fails_with_a_useful_message() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("IntMethod"); - - // Act - Action act = () => - methodInfo.Should().NotReturn(typeof(int), "we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected the return type of*IntMethod*not to be System.Int32*because we want to test the " + - "error message, but it is."); - } - - [Fact] - public void When_subject_is_null_not_return_should_fail() - { - // Arrange - MethodInfo methodInfo = null; - - // Act - Action act = () => - methodInfo.Should().NotReturn(typeof(string), "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected the return type of method not to be *.String *failure message*, but methodInfo is ."); - } - - [Fact] - public void When_asserting_method_return_type_is_not_null_it_should_throw() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("IntMethod"); - - // Act - Action act = () => - methodInfo.Should().NotReturn(null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("returnType"); - } - - #endregion - - #region ReturnOfT - - [Fact] - public void When_asserting_an_int_method_returnsOfT_int_it_succeeds() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("IntMethod"); - - // Act - Action act = () => - methodInfo.Should().Return(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_an_int_method_returnsOfT_string_it_fails_with_a_useful_message() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("IntMethod"); - - // Act - Action act = () => - methodInfo.Should().Return("we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected the return type of method IntMethod to be System.String because we want to test the " + - "error message, but it is \"System.Int32\"."); - } - - [Fact] - public void When_subject_is_null_returnOfT_should_fail() - { - // Arrange - MethodInfo methodInfo = null; - - // Act - Action act = () => - methodInfo.Should().Return("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected the return type of method to be *.String *failure message*, but methodInfo is ."); - } - - #endregion - - #region NotReturnOfT - - [Fact] - public void When_asserting_an_int_method_does_not_returnsOfT_string_it_succeeds() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("IntMethod"); - - // Act - Action act = () => - methodInfo.Should().NotReturn(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_an_int_method_does_not_returnsOfT_int_it_fails_with_a_useful_message() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("IntMethod"); - - // Act - Action act = () => - methodInfo.Should().NotReturn("we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected the return type of*IntMethod*not to be System.Int32*because we want to test the " + - "error message, but it is."); - } - - [Fact] - public void When_subject_is_null_not_returnOfT_should_fail() - { - // Arrange - MethodInfo methodInfo = null; - - // Act - Action act = () => - methodInfo.Should().NotReturn("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected the return type of method not to be *.String *failure message*, but methodInfo is ."); - } - - #endregion - - #region ReturnVoid - - [Fact] - public void When_asserting_a_void_method_returns_void_it_succeeds() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("VoidMethod"); - - // Act - Action act = () => - methodInfo.Should().ReturnVoid(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_an_int_method_returns_void_it_fails_with_a_useful_message() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("IntMethod"); - - // Act - Action act = () => - methodInfo.Should().ReturnVoid("because we want to test the error message {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected the return type of method IntMethod to be void because we want to test the error message " + - "message, but it is \"System.Int32\"."); - } - - [Fact] - public void When_subject_is_null_return_void_should_fail() - { - // Arrange - MethodInfo methodInfo = null; - - // Act - Action act = () => - methodInfo.Should().ReturnVoid("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected the return type of method to be void *failure message*, but methodInfo is ."); - } - - #endregion - - #region NotReturnVoid - - [Fact] - public void When_asserting_an_int_method_does_not_return_void_it_succeeds() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("IntMethod"); - - // Act - Action act = () => - methodInfo.Should().NotReturnVoid(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_void_method_does_not_return_void_it_fails_with_a_useful_message() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("VoidMethod"); - - // Act - Action act = () => - methodInfo.Should().NotReturnVoid("because we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected the return type of*VoidMethod*not to be void*because we want to test the error message*"); - } - - [Fact] - public void When_subject_is_null_not_return_void_should_fail() - { - // Arrange - MethodInfo methodInfo = null; - - // Act - Action act = () => - methodInfo.Should().NotReturnVoid("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected the return type of method not to be void *failure message*, but methodInfo is ."); - } - - #endregion - - #region HaveAccessModifier - - [Fact] - public void When_asserting_a_private_member_is_private_it_succeeds() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("PrivateMethod"); - - // Act - Action act = () => - methodInfo.Should().HaveAccessModifier(CSharpAccessModifier.Private); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_private_protected_member_is_private_protected_it_succeeds() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("PrivateProtectedMethod"); - - // Act - Action act = () => - methodInfo.Should().HaveAccessModifier(CSharpAccessModifier.PrivateProtected); - - // Assert - act.Should().NotThrow(); + public class Return + { + [Fact] + public void When_asserting_an_int_method_returns_int_it_succeeds() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("IntMethod"); + + // Act + Action act = () => + methodInfo.Should().Return(typeof(int)); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_an_int_method_returns_string_it_fails_with_a_useful_message() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("IntMethod"); + + // Act + Action act = () => + methodInfo.Should().Return(typeof(string), "we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected the return type of method IntMethod to be System.String because we want to test the " + + "error message, but it is \"System.Int32\"."); + } + + [Fact] + public void When_asserting_a_void_method_returns_string_it_fails_with_a_useful_message() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("VoidMethod"); + + // Act + Action act = () => + methodInfo.Should().Return(typeof(string), "we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected the return type of method VoidMethod to be System.String because we want to test the " + + "error message, but it is \"System.Void\"."); + } + + [Fact] + public void When_subject_is_null_return_should_fail() + { + // Arrange + MethodInfo methodInfo = null; + + // Act + Action act = () => + methodInfo.Should().Return(typeof(string), "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected the return type of method to be *.String *failure message*, but methodInfo is ."); + } + + [Fact] + public void When_asserting_method_return_type_is_null_it_should_throw() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("IntMethod"); + + // Act + Action act = () => + methodInfo.Should().Return(null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("returnType"); + } + } + + public class NotReturn + { + [Fact] + public void When_asserting_an_int_method_does_not_return_string_it_succeeds() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("IntMethod"); + + // Act + Action act = () => + methodInfo.Should().NotReturn(typeof(string)); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_an_int_method_does_not_return_int_it_fails_with_a_useful_message() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("IntMethod"); + + // Act + Action act = () => + methodInfo.Should().NotReturn(typeof(int), "we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected the return type of*IntMethod*not to be System.Int32*because we want to test the " + + "error message, but it is."); + } + + [Fact] + public void When_subject_is_null_not_return_should_fail() + { + // Arrange + MethodInfo methodInfo = null; + + // Act + Action act = () => + methodInfo.Should().NotReturn(typeof(string), "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected the return type of method not to be *.String *failure message*, but methodInfo is ."); + } + + [Fact] + public void When_asserting_method_return_type_is_not_null_it_should_throw() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("IntMethod"); + + // Act + Action act = () => + methodInfo.Should().NotReturn(null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("returnType"); + } + } + + public class ReturnOfT + { + [Fact] + public void When_asserting_an_int_method_returnsOfT_int_it_succeeds() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("IntMethod"); + + // Act + Action act = () => + methodInfo.Should().Return(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_an_int_method_returnsOfT_string_it_fails_with_a_useful_message() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("IntMethod"); + + // Act + Action act = () => + methodInfo.Should().Return("we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected the return type of method IntMethod to be System.String because we want to test the " + + "error message, but it is \"System.Int32\"."); + } + + [Fact] + public void When_subject_is_null_returnOfT_should_fail() + { + // Arrange + MethodInfo methodInfo = null; + + // Act + Action act = () => + methodInfo.Should().Return("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected the return type of method to be *.String *failure message*, but methodInfo is ."); + } + } + + public class NotReturnOfT + { + [Fact] + public void When_asserting_an_int_method_does_not_returnsOfT_string_it_succeeds() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("IntMethod"); + + // Act + Action act = () => + methodInfo.Should().NotReturn(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_an_int_method_does_not_returnsOfT_int_it_fails_with_a_useful_message() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("IntMethod"); + + // Act + Action act = () => + methodInfo.Should().NotReturn("we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected the return type of*IntMethod*not to be System.Int32*because we want to test the " + + "error message, but it is."); + } + + [Fact] + public void When_subject_is_null_not_returnOfT_should_fail() + { + // Arrange + MethodInfo methodInfo = null; + + // Act + Action act = () => + methodInfo.Should().NotReturn("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected the return type of method not to be *.String *failure message*, but methodInfo is ."); + } + } + + public class ReturnVoid + { + [Fact] + public void When_asserting_a_void_method_returns_void_it_succeeds() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("VoidMethod"); + + // Act + Action act = () => + methodInfo.Should().ReturnVoid(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_an_int_method_returns_void_it_fails_with_a_useful_message() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("IntMethod"); + + // Act + Action act = () => + methodInfo.Should().ReturnVoid("because we want to test the error message {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected the return type of method IntMethod to be void because we want to test the error message " + + "message, but it is \"System.Int32\"."); + } + + [Fact] + public void When_subject_is_null_return_void_should_fail() + { + // Arrange + MethodInfo methodInfo = null; + + // Act + Action act = () => + methodInfo.Should().ReturnVoid("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected the return type of method to be void *failure message*, but methodInfo is ."); + } + } + + public class NotReturnVoid + { + [Fact] + public void When_asserting_an_int_method_does_not_return_void_it_succeeds() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("IntMethod"); + + // Act + Action act = () => + methodInfo.Should().NotReturnVoid(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_void_method_does_not_return_void_it_fails_with_a_useful_message() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("VoidMethod"); + + // Act + Action act = () => + methodInfo.Should().NotReturnVoid("because we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected the return type of*VoidMethod*not to be void*because we want to test the error message*"); + } + + [Fact] + public void When_subject_is_null_not_return_void_should_fail() + { + // Arrange + MethodInfo methodInfo = null; + + // Act + Action act = () => + methodInfo.Should().NotReturnVoid("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected the return type of method not to be void *failure message*, but methodInfo is ."); + } + } + + public class HaveAccessModifier + { + [Fact] + public void When_asserting_a_private_member_is_private_it_succeeds() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("PrivateMethod"); + + // Act + Action act = () => + methodInfo.Should().HaveAccessModifier(CSharpAccessModifier.Private); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_private_protected_member_is_private_protected_it_succeeds() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("PrivateProtectedMethod"); + + // Act + Action act = () => + methodInfo.Should().HaveAccessModifier(CSharpAccessModifier.PrivateProtected); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_private_member_is_protected_it_throws_with_a_useful_message() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("PrivateMethod"); + + // Act + Action act = () => + methodInfo.Should().HaveAccessModifier(CSharpAccessModifier.Protected, "we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected method PrivateMethod to be Protected because we want to test the error message, but it is " + + "Private."); + } + + [Fact] + public void When_asserting_a_protected_member_is_protected_it_succeeds() + { + // Arrange + PropertyInfo propertyInfo = typeof(TestClass).FindPropertyByName("ProtectedSetProperty"); + + MethodInfo setMethod = propertyInfo.SetMethod; + + // Act + Action act = () => + setMethod.Should().HaveAccessModifier(CSharpAccessModifier.Protected); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_protected_member_is_public_it_throws_with_a_useful_message() + { + // Arrange + PropertyInfo propertyInfo = typeof(TestClass).FindPropertyByName("ProtectedSetProperty"); + + MethodInfo setMethod = propertyInfo.SetMethod; + + // Act + Action act = () => + setMethod + .Should() + .HaveAccessModifier(CSharpAccessModifier.Public, "we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected method set_ProtectedSetProperty to be Public because we want to test the error message, but it" + + " is Protected."); + } + + [Fact] + public void When_asserting_a_public_member_is_public_it_succeeds() + { + // Arrange + PropertyInfo propertyInfo = typeof(TestClass).FindPropertyByName("PublicGetProperty"); + + MethodInfo getMethod = propertyInfo.GetMethod; + + // Act + Action act = () => + getMethod.Should().HaveAccessModifier(CSharpAccessModifier.Public); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_public_member_is_internal_it_throws_with_a_useful_message() + { + // Arrange + PropertyInfo propertyInfo = typeof(TestClass).FindPropertyByName("PublicGetProperty"); + + MethodInfo getMethod = propertyInfo.GetMethod; + + // Act + Action act = () => + getMethod + .Should() + .HaveAccessModifier(CSharpAccessModifier.Internal, "we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected method get_PublicGetProperty to be Internal because we want to test the error message, but it" + + " is Public."); + } + + [Fact] + public void When_asserting_an_internal_member_is_internal_it_succeeds() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("InternalMethod"); + + // Act + Action act = () => + methodInfo.Should().HaveAccessModifier(CSharpAccessModifier.Internal); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_an_internal_member_is_protectedInternal_it_throws_with_a_useful_message() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("InternalMethod"); + + // Act + Action act = () => + methodInfo.Should().HaveAccessModifier(CSharpAccessModifier.ProtectedInternal, "because we want to test the" + + " error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected method InternalMethod to be ProtectedInternal because we want to test the error message, but" + + " it is Internal."); + } + + [Fact] + public void When_asserting_a_protected_internal_member_is_protected_internal_it_succeeds() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("ProtectedInternalMethod"); + + // Act + Action act = () => + methodInfo.Should().HaveAccessModifier(CSharpAccessModifier.ProtectedInternal); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_protected_internal_member_is_private_it_throws_with_a_useful_message() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("ProtectedInternalMethod"); + + // Act + Action act = () => + methodInfo.Should().HaveAccessModifier(CSharpAccessModifier.Private, "we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected method ProtectedInternalMethod to be Private because we want to test the error message, but it is " + + "ProtectedInternal."); + } + + [Fact] + public void When_subject_is_null_have_access_modifier_should_fail() + { + // Arrange + MethodInfo methodInfo = null; + + // Act + Action act = () => + methodInfo.Should().HaveAccessModifier(CSharpAccessModifier.Public, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected method to be Public *failure message*, but methodInfo is ."); + } + + [Fact] + public void When_asserting_method_has_access_modifier_with_an_invalid_enum_value_it_should_throw() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("PrivateMethod"); + + // Act + Action act = () => + methodInfo.Should().HaveAccessModifier((CSharpAccessModifier)int.MaxValue); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("accessModifier"); + } + } + + public class NotHaveAccessModifier + { + [Fact] + public void When_asserting_a_private_member_is_not_protected_it_succeeds() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("PrivateMethod"); + + // Act + Action act = () => + methodInfo.Should().NotHaveAccessModifier(CSharpAccessModifier.Protected); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_private_member_is_not_private_protected_it_succeeds() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("PrivateMethod"); + + // Act + Action act = () => + methodInfo.Should().NotHaveAccessModifier(CSharpAccessModifier.PrivateProtected); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_private_member_is_not_private_it_throws_with_a_useful_message() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("PrivateMethod"); + + // Act + Action act = () => + methodInfo.Should().NotHaveAccessModifier(CSharpAccessModifier.Private, "we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected method PrivateMethod not to be Private*because we want to test the error message*"); + } + + [Fact] + public void When_asserting_a_protected_member_is_not_internal_it_succeeds() + { + // Arrange + PropertyInfo propertyInfo = typeof(TestClass).FindPropertyByName("ProtectedSetProperty"); + MethodInfo setMethod = propertyInfo.SetMethod; + + // Act + Action act = () => + setMethod.Should().NotHaveAccessModifier(CSharpAccessModifier.Internal); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_protected_member_is_not_protected_it_throws_with_a_useful_message() + { + // Arrange + PropertyInfo propertyInfo = typeof(TestClass).FindPropertyByName("ProtectedSetProperty"); + MethodInfo setMethod = propertyInfo.SetMethod; + + // Act + Action act = () => + setMethod + .Should() + .NotHaveAccessModifier(CSharpAccessModifier.Protected, "we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected method set_ProtectedSetProperty not to be Protected*because we want to test the error message*"); + } + + [Fact] + public void When_asserting_a_public_member_is_not_private_it_succeeds() + { + // Arrange + PropertyInfo propertyInfo = typeof(TestClass).FindPropertyByName("PublicGetProperty"); + MethodInfo getMethod = propertyInfo.GetMethod; + + // Act + Action act = () => + getMethod.Should().NotHaveAccessModifier(CSharpAccessModifier.Private); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_private_protected_member_is_not_private_it_succeeds() + { + // Arrange + PropertyInfo propertyInfo = typeof(TestClass).FindPropertyByName("PublicGetPrivateProtectedSet"); + MethodInfo setMethod = propertyInfo.SetMethod; + + // Act + Action act = () => + setMethod.Should().NotHaveAccessModifier(CSharpAccessModifier.Private); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_public_member_is_not_public_it_throws_with_a_useful_message() + { + // Arrange + PropertyInfo propertyInfo = typeof(TestClass).FindPropertyByName("PublicGetProperty"); + MethodInfo getMethod = propertyInfo.GetMethod; + + // Act + Action act = () => + getMethod + .Should() + .NotHaveAccessModifier(CSharpAccessModifier.Public, "we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected method get_PublicGetProperty not to be Public*because we want to test the error message*"); + } + + [Fact] + public void When_asserting_an_internal_member_is_not_protectedInternal_it_succeeds() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("InternalMethod"); + + // Act + Action act = () => + methodInfo.Should().NotHaveAccessModifier(CSharpAccessModifier.ProtectedInternal); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_an_internal_member_is_not_internal_it_throws_with_a_useful_message() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("InternalMethod"); + + // Act + Action act = () => + methodInfo.Should().NotHaveAccessModifier(CSharpAccessModifier.Internal, "because we want to test the" + + " error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected method InternalMethod not to be Internal*because we want to test the error message*"); + } + + [Fact] + public void When_asserting_a_protected_internal_member_is_not_public_it_succeeds() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("ProtectedInternalMethod"); + + // Act + Action act = () => + methodInfo.Should().NotHaveAccessModifier(CSharpAccessModifier.Public); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_protected_internal_member_is_not_protected_internal_it_throws_with_a_useful_message() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("ProtectedInternalMethod"); + + // Act + Action act = () => + methodInfo.Should().NotHaveAccessModifier(CSharpAccessModifier.ProtectedInternal, "we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected method ProtectedInternalMethod not to be ProtectedInternal*because we want to test the error message*"); + } + + [Fact] + public void When_subject_is_not_null_have_access_modifier_should_fail() + { + // Arrange + MethodInfo methodInfo = null; + + // Act + Action act = () => + methodInfo.Should().NotHaveAccessModifier( + CSharpAccessModifier.Public, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected method not to be Public *failure message*, but methodInfo is ."); + } + + [Fact] + public void When_asserting_method_does_not_have_access_modifier_with_an_invalid_enum_value_it_should_throw() + { + // Arrange + MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("PrivateMethod"); + + // Act + Action act = () => + methodInfo.Should().NotHaveAccessModifier((CSharpAccessModifier)int.MaxValue); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("accessModifier"); + } } - - [Fact] - public void When_asserting_a_private_member_is_protected_it_throws_with_a_useful_message() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("PrivateMethod"); - - // Act - Action act = () => - methodInfo.Should().HaveAccessModifier(CSharpAccessModifier.Protected, "we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected method PrivateMethod to be Protected because we want to test the error message, but it is " + - "Private."); - } - - [Fact] - public void When_asserting_a_protected_member_is_protected_it_succeeds() - { - // Arrange - PropertyInfo propertyInfo = typeof(TestClass).FindPropertyByName("ProtectedSetProperty"); - - MethodInfo setMethod = propertyInfo.SetMethod; - - // Act - Action act = () => - setMethod.Should().HaveAccessModifier(CSharpAccessModifier.Protected); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_protected_member_is_public_it_throws_with_a_useful_message() - { - // Arrange - PropertyInfo propertyInfo = typeof(TestClass).FindPropertyByName("ProtectedSetProperty"); - - MethodInfo setMethod = propertyInfo.SetMethod; - - // Act - Action act = () => - setMethod - .Should() - .HaveAccessModifier(CSharpAccessModifier.Public, "we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected method set_ProtectedSetProperty to be Public because we want to test the error message, but it" + - " is Protected."); - } - - [Fact] - public void When_asserting_a_public_member_is_public_it_succeeds() - { - // Arrange - PropertyInfo propertyInfo = typeof(TestClass).FindPropertyByName("PublicGetProperty"); - - MethodInfo getMethod = propertyInfo.GetMethod; - - // Act - Action act = () => - getMethod.Should().HaveAccessModifier(CSharpAccessModifier.Public); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_public_member_is_internal_it_throws_with_a_useful_message() - { - // Arrange - PropertyInfo propertyInfo = typeof(TestClass).FindPropertyByName("PublicGetProperty"); - - MethodInfo getMethod = propertyInfo.GetMethod; - - // Act - Action act = () => - getMethod - .Should() - .HaveAccessModifier(CSharpAccessModifier.Internal, "we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected method get_PublicGetProperty to be Internal because we want to test the error message, but it" + - " is Public."); - } - - [Fact] - public void When_asserting_an_internal_member_is_internal_it_succeeds() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("InternalMethod"); - - // Act - Action act = () => - methodInfo.Should().HaveAccessModifier(CSharpAccessModifier.Internal); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_an_internal_member_is_protectedInternal_it_throws_with_a_useful_message() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("InternalMethod"); - - // Act - Action act = () => - methodInfo.Should().HaveAccessModifier(CSharpAccessModifier.ProtectedInternal, "because we want to test the" + - " error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected method InternalMethod to be ProtectedInternal because we want to test the error message, but" + - " it is Internal."); - } - - [Fact] - public void When_asserting_a_protected_internal_member_is_protected_internal_it_succeeds() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("ProtectedInternalMethod"); - - // Act - Action act = () => - methodInfo.Should().HaveAccessModifier(CSharpAccessModifier.ProtectedInternal); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_protected_internal_member_is_private_it_throws_with_a_useful_message() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("ProtectedInternalMethod"); - - // Act - Action act = () => - methodInfo.Should().HaveAccessModifier(CSharpAccessModifier.Private, "we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected method ProtectedInternalMethod to be Private because we want to test the error message, but it is " + - "ProtectedInternal."); - } - - [Fact] - public void When_subject_is_null_have_access_modifier_should_fail() - { - // Arrange - MethodInfo methodInfo = null; - - // Act - Action act = () => - methodInfo.Should().HaveAccessModifier(CSharpAccessModifier.Public, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected method to be Public *failure message*, but methodInfo is ."); - } - - [Fact] - public void When_asserting_method_has_access_modifier_with_an_invalid_enum_value_it_should_throw() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("PrivateMethod"); - - // Act - Action act = () => - methodInfo.Should().HaveAccessModifier((CSharpAccessModifier)int.MaxValue); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("accessModifier"); - } - - #endregion - - #region NotHaveAccessModifier - - [Fact] - public void When_asserting_a_private_member_is_not_protected_it_succeeds() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("PrivateMethod"); - - // Act - Action act = () => - methodInfo.Should().NotHaveAccessModifier(CSharpAccessModifier.Protected); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_private_member_is_not_private_protected_it_succeeds() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("PrivateMethod"); - - // Act - Action act = () => - methodInfo.Should().NotHaveAccessModifier(CSharpAccessModifier.PrivateProtected); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_private_member_is_not_private_it_throws_with_a_useful_message() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("PrivateMethod"); - - // Act - Action act = () => - methodInfo.Should().NotHaveAccessModifier(CSharpAccessModifier.Private, "we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected method PrivateMethod not to be Private*because we want to test the error message*"); - } - - [Fact] - public void When_asserting_a_protected_member_is_not_internal_it_succeeds() - { - // Arrange - PropertyInfo propertyInfo = typeof(TestClass).FindPropertyByName("ProtectedSetProperty"); - MethodInfo setMethod = propertyInfo.SetMethod; - - // Act - Action act = () => - setMethod.Should().NotHaveAccessModifier(CSharpAccessModifier.Internal); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_protected_member_is_not_protected_it_throws_with_a_useful_message() - { - // Arrange - PropertyInfo propertyInfo = typeof(TestClass).FindPropertyByName("ProtectedSetProperty"); - MethodInfo setMethod = propertyInfo.SetMethod; - - // Act - Action act = () => - setMethod - .Should() - .NotHaveAccessModifier(CSharpAccessModifier.Protected, "we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected method set_ProtectedSetProperty not to be Protected*because we want to test the error message*"); - } - - [Fact] - public void When_asserting_a_public_member_is_not_private_it_succeeds() - { - // Arrange - PropertyInfo propertyInfo = typeof(TestClass).FindPropertyByName("PublicGetProperty"); - MethodInfo getMethod = propertyInfo.GetMethod; - - // Act - Action act = () => - getMethod.Should().NotHaveAccessModifier(CSharpAccessModifier.Private); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_private_protected_member_is_not_private_it_succeeds() - { - // Arrange - PropertyInfo propertyInfo = typeof(TestClass).FindPropertyByName("PublicGetPrivateProtectedSet"); - MethodInfo setMethod = propertyInfo.SetMethod; - - // Act - Action act = () => - setMethod.Should().NotHaveAccessModifier(CSharpAccessModifier.Private); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_public_member_is_not_public_it_throws_with_a_useful_message() - { - // Arrange - PropertyInfo propertyInfo = typeof(TestClass).FindPropertyByName("PublicGetProperty"); - MethodInfo getMethod = propertyInfo.GetMethod; - - // Act - Action act = () => - getMethod - .Should() - .NotHaveAccessModifier(CSharpAccessModifier.Public, "we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected method get_PublicGetProperty not to be Public*because we want to test the error message*"); - } - - [Fact] - public void When_asserting_an_internal_member_is_not_protectedInternal_it_succeeds() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("InternalMethod"); - - // Act - Action act = () => - methodInfo.Should().NotHaveAccessModifier(CSharpAccessModifier.ProtectedInternal); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_an_internal_member_is_not_internal_it_throws_with_a_useful_message() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("InternalMethod"); - - // Act - Action act = () => - methodInfo.Should().NotHaveAccessModifier(CSharpAccessModifier.Internal, "because we want to test the" + - " error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected method InternalMethod not to be Internal*because we want to test the error message*"); - } - - [Fact] - public void When_asserting_a_protected_internal_member_is_not_public_it_succeeds() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("ProtectedInternalMethod"); - - // Act - Action act = () => - methodInfo.Should().NotHaveAccessModifier(CSharpAccessModifier.Public); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_protected_internal_member_is_not_protected_internal_it_throws_with_a_useful_message() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("ProtectedInternalMethod"); - - // Act - Action act = () => - methodInfo.Should().NotHaveAccessModifier(CSharpAccessModifier.ProtectedInternal, "we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected method ProtectedInternalMethod not to be ProtectedInternal*because we want to test the error message*"); - } - - [Fact] - public void When_subject_is_not_null_have_access_modifier_should_fail() - { - // Arrange - MethodInfo methodInfo = null; - - // Act - Action act = () => - methodInfo.Should().NotHaveAccessModifier( - CSharpAccessModifier.Public, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected method not to be Public *failure message*, but methodInfo is ."); - } - - [Fact] - public void When_asserting_method_does_not_have_access_modifier_with_an_invalid_enum_value_it_should_throw() - { - // Arrange - MethodInfo methodInfo = typeof(TestClass).GetParameterlessMethod("PrivateMethod"); - - // Act - Action act = () => - methodInfo.Should().NotHaveAccessModifier((CSharpAccessModifier)int.MaxValue); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("accessModifier"); - } - - #endregion } #region Internal classes used in unit tests diff --git a/Tests/FluentAssertions.Specs/Types/MethodInfoAssertionSpecs.cs b/Tests/FluentAssertions.Specs/Types/MethodInfoAssertionSpecs.cs index 3c6fdd695f..cfb0145cc5 100644 --- a/Tests/FluentAssertions.Specs/Types/MethodInfoAssertionSpecs.cs +++ b/Tests/FluentAssertions.Specs/Types/MethodInfoAssertionSpecs.cs @@ -10,604 +10,598 @@ namespace FluentAssertions.Specs.Types { public class MethodInfoAssertionSpecs { - #region BeVirtual - - [Fact] - public void When_asserting_a_method_is_virtual_and_it_is_then_it_succeeds() - { - // Arrange - MethodInfo methodInfo = typeof(ClassWithAllMethodsVirtual).GetParameterlessMethod("PublicVirtualDoNothing"); - - // Act - Action act = () => - methodInfo.Should().BeVirtual(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_method_is_virtual_but_it_is_not_then_it_throws_with_a_useful_message() - { - // Arrange - MethodInfo methodInfo = typeof(ClassWithNonVirtualPublicMethods).GetParameterlessMethod("PublicDoNothing"); - - // Act - Action act = () => - methodInfo.Should().BeVirtual("we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected method Void FluentAssertions*ClassWithNonVirtualPublicMethods.PublicDoNothing" + - " to be virtual because we want to test the error message," + - " but it is not virtual."); - } - - [Fact] - public void When_subject_is_null_be_virtual_should_fail() - { - // Arrange - MethodInfo methodInfo = null; - - // Act - Action act = () => - methodInfo.Should().BeVirtual("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected method to be virtual *failure message*, but methodInfo is ."); - } - - #endregion - - #region NotBeVirtual - - [Fact] - public void When_asserting_a_method_is_not_virtual_and_it_is_not_then_it_succeeds() - { - // Arrange - MethodInfo methodInfo = typeof(ClassWithNonVirtualPublicMethods).GetParameterlessMethod("PublicDoNothing"); - - // Act - Action act = () => - methodInfo.Should().NotBeVirtual(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_method_is_not_virtual_but_it_is_then_it_throws_with_a_useful_message() - { - // Arrange - MethodInfo methodInfo = typeof(ClassWithAllMethodsVirtual).GetParameterlessMethod("PublicVirtualDoNothing"); - - // Act - Action act = () => - methodInfo.Should().NotBeVirtual("we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected method *ClassWithAllMethodsVirtual.PublicVirtualDoNothing" + - " not to be virtual because we want to test the error message," + - " but it is."); - } - - [Fact] - public void When_subject_is_null_not_be_virtual_should_fail() - { - // Arrange - MethodInfo methodInfo = null; - - // Act - Action act = () => - methodInfo.Should().NotBeVirtual("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected method not to be virtual *failure message*, but methodInfo is ."); - } - - #endregion - - #region BeDecoratedWithOfT - - [Fact] - public void When_asserting_a_method_is_decorated_with_attribute_and_it_is_it_succeeds() - { - // Arrange - MethodInfo methodInfo = typeof(ClassWithAllMethodsDecoratedWithDummyAttribute).GetParameterlessMethod("PublicDoNothing"); - - // Act - Action act = () => - methodInfo.Should().BeDecoratedWith(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_method_is_decorated_with_MethodImpl_attribute_and_it_is_it_succeeds() - { - // Arrange - MethodInfo methodInfo = typeof(ClassWithMethodWithImplementationAttribute).GetParameterlessMethod("DoNotInlineMe"); - - // Act - Action act = () => - methodInfo.Should().BeDecoratedWith(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_constructor_is_decorated_with_MethodImpl_attribute_and_it_is_it_succeeds() - { - // Arrange - ConstructorInfo constructorMethodInfo = typeof(ClassWithMethodWithImplementationAttribute).GetConstructor(Type.EmptyTypes); - - // Act - Action act = () => - constructorMethodInfo.Should().BeDecoratedWith(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_method_is_decorated_with_MethodImpl_attribute_and_it_is_not_it_throws() - { - // Arrange - MethodInfo methodInfo = typeof(ClassWithAllMethodsDecoratedWithDummyAttribute).GetParameterlessMethod("PublicDoNothing"); - - // Act - Action act = () => - methodInfo.Should().BeDecoratedWith(); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected method Void FluentAssertions*ClassWithAllMethodsDecoratedWithDummyAttribute.PublicDoNothing to be decorated with " + - "System.Runtime.CompilerServices.MethodImplAttribute, but that attribute was not found."); - } - - [Fact] - public void When_asserting_a_method_is_decorated_with_MethodImpl_attribute_with_no_options_and_it_is_it_throws() - { - // Arrange - MethodInfo methodInfo = typeof(ClassWithMethodWithImplementationAttribute).GetParameterlessMethod("NoOptions"); - - // Act - Action act = () => - methodInfo.Should().BeDecoratedWith(); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected method Void FluentAssertions*ClassWithMethodWithImplementationAttribute.NoOptions to be decorated with " + - "System.Runtime.CompilerServices.MethodImplAttribute, but that attribute was not found."); - } - - [Fact] - public void When_asserting_a_method_is_decorated_with_MethodImpl_attribute_with_zero_as_options_and_it_is_it_throws() - { - // Arrange - MethodInfo methodInfo = typeof(ClassWithMethodWithImplementationAttribute).GetParameterlessMethod("ZeroOptions"); - - // Act - Action act = () => - methodInfo.Should().BeDecoratedWith(); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected method Void FluentAssertions*ClassWithMethodWithImplementationAttribute.ZeroOptions to be decorated with " + - "System.Runtime.CompilerServices.MethodImplAttribute, but that attribute was not found."); - } - - [Fact] - public void When_asserting_a_class_is_decorated_with_MethodImpl_attribute_and_it_is_not_it_throws() - { - // Arrange - var type = typeof(ClassWithAllMethodsDecoratedWithDummyAttribute); - - // Act - Action act = () => - type.Should().BeDecoratedWith(); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type FluentAssertions*ClassWithAllMethodsDecoratedWithDummyAttribute to be decorated with " + - "System.Runtime.CompilerServices.MethodImplAttribute, but the attribute was not found."); - } - - [Fact] - public void When_a_method_is_decorated_with_an_attribute_it_should_allow_chaining_assertions_on_it() - { - // Arrange - MethodInfo methodInfo = typeof(ClassWithAllMethodsDecoratedWithDummyAttribute).GetParameterlessMethod("PublicDoNothing"); - - // Act - Action act = () => methodInfo.Should().BeDecoratedWith().Which.Filter.Should().BeFalse(); - - // Assert - act.Should().Throw(); - } - - [Fact] - public void When_asserting_a_method_is_decorated_with_an_attribute_but_it_is_not_it_throws_with_a_useful_message() - { - // Arrange - MethodInfo methodInfo = typeof(ClassWithMethodsThatAreNotDecoratedWithDummyAttribute).GetParameterlessMethod("PublicDoNothing"); - - // Act - Action act = () => - methodInfo.Should().BeDecoratedWith("because we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected method Void FluentAssertions*ClassWithMethodsThatAreNotDecoratedWithDummyAttribute.PublicDoNothing to be decorated with " + - "FluentAssertions*DummyMethodAttribute because we want to test the error message," + - " but that attribute was not found."); - } - - [Fact] - public void When_injecting_a_null_predicate_into_BeDecoratedWith_it_should_throw() - { - // Arrange - MethodInfo methodInfo = typeof(ClassWithAllMethodsDecoratedWithDummyAttribute).GetParameterlessMethod("PublicDoNothing"); - - // Act - Action act = () => methodInfo.Should().BeDecoratedWith(isMatchingAttributePredicate: null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("isMatchingAttributePredicate"); - } - - [Fact] - public void When_asserting_a_method_is_decorated_with_attribute_matching_a_predicate_and_it_is_it_succeeds() - { - // Arrange - MethodInfo methodInfo = typeof(ClassWithAllMethodsDecoratedWithDummyAttribute).GetParameterlessMethod("PublicDoNothing"); - - // Act - Action act = () => - methodInfo.Should().BeDecoratedWith(d => d.Filter); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_method_is_decorated_with_MethodImpl_attribute_matching_a_predicate_and_it_is_it_succeeds() - { - // Arrange - MethodInfo methodInfo = typeof(ClassWithMethodWithImplementationAttribute).GetParameterlessMethod("DoNotInlineMe"); - - // Act - Action act = () => - methodInfo.Should().BeDecoratedWith(x => x.Value == MethodImplOptions.NoInlining); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_method_is_decorated_with_an_attribute_matching_a_predicate_but_it_is_not_it_throws_with_a_useful_message() - { - // Arrange - MethodInfo methodInfo = typeof(ClassWithMethodsThatAreNotDecoratedWithDummyAttribute).GetParameterlessMethod("PublicDoNothing"); - - // Act - Action act = () => - methodInfo.Should().BeDecoratedWith(d => !d.Filter, "because we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() + public class BeVirtual + { + [Fact] + public void When_asserting_a_method_is_virtual_and_it_is_then_it_succeeds() + { + // Arrange + MethodInfo methodInfo = typeof(ClassWithAllMethodsVirtual).GetParameterlessMethod("PublicVirtualDoNothing"); + + // Act + Action act = () => + methodInfo.Should().BeVirtual(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_method_is_virtual_but_it_is_not_then_it_throws_with_a_useful_message() + { + // Arrange + MethodInfo methodInfo = typeof(ClassWithNonVirtualPublicMethods).GetParameterlessMethod("PublicDoNothing"); + + // Act + Action act = () => + methodInfo.Should().BeVirtual("we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected method Void FluentAssertions*ClassWithNonVirtualPublicMethods.PublicDoNothing" + + " to be virtual because we want to test the error message," + + " but it is not virtual."); + } + + [Fact] + public void When_subject_is_null_be_virtual_should_fail() + { + // Arrange + MethodInfo methodInfo = null; + + // Act + Action act = () => + methodInfo.Should().BeVirtual("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected method to be virtual *failure message*, but methodInfo is ."); + } + } + + public class NotBeVirtual + { + [Fact] + public void When_asserting_a_method_is_not_virtual_and_it_is_not_then_it_succeeds() + { + // Arrange + MethodInfo methodInfo = typeof(ClassWithNonVirtualPublicMethods).GetParameterlessMethod("PublicDoNothing"); + + // Act + Action act = () => + methodInfo.Should().NotBeVirtual(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_method_is_not_virtual_but_it_is_then_it_throws_with_a_useful_message() + { + // Arrange + MethodInfo methodInfo = typeof(ClassWithAllMethodsVirtual).GetParameterlessMethod("PublicVirtualDoNothing"); + + // Act + Action act = () => + methodInfo.Should().NotBeVirtual("we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected method *ClassWithAllMethodsVirtual.PublicVirtualDoNothing" + + " not to be virtual because we want to test the error message," + + " but it is."); + } + + [Fact] + public void When_subject_is_null_not_be_virtual_should_fail() + { + // Arrange + MethodInfo methodInfo = null; + + // Act + Action act = () => + methodInfo.Should().NotBeVirtual("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected method not to be virtual *failure message*, but methodInfo is ."); + } + } + + public class BeDecoratedWithOfT + { + [Fact] + public void When_asserting_a_method_is_decorated_with_attribute_and_it_is_it_succeeds() + { + // Arrange + MethodInfo methodInfo = typeof(ClassWithAllMethodsDecoratedWithDummyAttribute).GetParameterlessMethod("PublicDoNothing"); + + // Act + Action act = () => + methodInfo.Should().BeDecoratedWith(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_method_is_decorated_with_MethodImpl_attribute_and_it_is_it_succeeds() + { + // Arrange + MethodInfo methodInfo = typeof(ClassWithMethodWithImplementationAttribute).GetParameterlessMethod("DoNotInlineMe"); + + // Act + Action act = () => + methodInfo.Should().BeDecoratedWith(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_constructor_is_decorated_with_MethodImpl_attribute_and_it_is_it_succeeds() + { + // Arrange + ConstructorInfo constructorMethodInfo = typeof(ClassWithMethodWithImplementationAttribute).GetConstructor(Type.EmptyTypes); + + // Act + Action act = () => + constructorMethodInfo.Should().BeDecoratedWith(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_method_is_decorated_with_MethodImpl_attribute_and_it_is_not_it_throws() + { + // Arrange + MethodInfo methodInfo = typeof(ClassWithAllMethodsDecoratedWithDummyAttribute).GetParameterlessMethod("PublicDoNothing"); + + // Act + Action act = () => + methodInfo.Should().BeDecoratedWith(); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected method Void FluentAssertions*ClassWithAllMethodsDecoratedWithDummyAttribute.PublicDoNothing to be decorated with " + + "System.Runtime.CompilerServices.MethodImplAttribute, but that attribute was not found."); + } + + [Fact] + public void When_asserting_a_method_is_decorated_with_MethodImpl_attribute_with_no_options_and_it_is_it_throws() + { + // Arrange + MethodInfo methodInfo = typeof(ClassWithMethodWithImplementationAttribute).GetParameterlessMethod("NoOptions"); + + // Act + Action act = () => + methodInfo.Should().BeDecoratedWith(); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected method Void FluentAssertions*ClassWithMethodWithImplementationAttribute.NoOptions to be decorated with " + + "System.Runtime.CompilerServices.MethodImplAttribute, but that attribute was not found."); + } + + [Fact] + public void When_asserting_a_method_is_decorated_with_MethodImpl_attribute_with_zero_as_options_and_it_is_it_throws() + { + // Arrange + MethodInfo methodInfo = typeof(ClassWithMethodWithImplementationAttribute).GetParameterlessMethod("ZeroOptions"); + + // Act + Action act = () => + methodInfo.Should().BeDecoratedWith(); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected method Void FluentAssertions*ClassWithMethodWithImplementationAttribute.ZeroOptions to be decorated with " + + "System.Runtime.CompilerServices.MethodImplAttribute, but that attribute was not found."); + } + + [Fact] + public void When_asserting_a_class_is_decorated_with_MethodImpl_attribute_and_it_is_not_it_throws() + { + // Arrange + var type = typeof(ClassWithAllMethodsDecoratedWithDummyAttribute); + + // Act + Action act = () => + type.Should().BeDecoratedWith(); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type FluentAssertions*ClassWithAllMethodsDecoratedWithDummyAttribute to be decorated with " + + "System.Runtime.CompilerServices.MethodImplAttribute, but the attribute was not found."); + } + + [Fact] + public void When_a_method_is_decorated_with_an_attribute_it_should_allow_chaining_assertions_on_it() + { + // Arrange + MethodInfo methodInfo = typeof(ClassWithAllMethodsDecoratedWithDummyAttribute).GetParameterlessMethod("PublicDoNothing"); + + // Act + Action act = () => methodInfo.Should().BeDecoratedWith().Which.Filter.Should().BeFalse(); + + // Assert + act.Should().Throw(); + } + + [Fact] + public void When_asserting_a_method_is_decorated_with_an_attribute_but_it_is_not_it_throws_with_a_useful_message() + { + // Arrange + MethodInfo methodInfo = typeof(ClassWithMethodsThatAreNotDecoratedWithDummyAttribute).GetParameterlessMethod("PublicDoNothing"); + + // Act + Action act = () => + methodInfo.Should().BeDecoratedWith("because we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected method Void FluentAssertions*ClassWithMethodsThatAreNotDecoratedWithDummyAttribute.PublicDoNothing to be decorated with " + + "FluentAssertions*DummyMethodAttribute because we want to test the error message," + + " but that attribute was not found."); + } + + [Fact] + public void When_injecting_a_null_predicate_into_BeDecoratedWith_it_should_throw() + { + // Arrange + MethodInfo methodInfo = typeof(ClassWithAllMethodsDecoratedWithDummyAttribute).GetParameterlessMethod("PublicDoNothing"); + + // Act + Action act = () => methodInfo.Should().BeDecoratedWith(isMatchingAttributePredicate: null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("isMatchingAttributePredicate"); + } + + [Fact] + public void When_asserting_a_method_is_decorated_with_attribute_matching_a_predicate_and_it_is_it_succeeds() + { + // Arrange + MethodInfo methodInfo = typeof(ClassWithAllMethodsDecoratedWithDummyAttribute).GetParameterlessMethod("PublicDoNothing"); + + // Act + Action act = () => + methodInfo.Should().BeDecoratedWith(d => d.Filter); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_method_is_decorated_with_MethodImpl_attribute_matching_a_predicate_and_it_is_it_succeeds() + { + // Arrange + MethodInfo methodInfo = typeof(ClassWithMethodWithImplementationAttribute).GetParameterlessMethod("DoNotInlineMe"); + + // Act + Action act = () => + methodInfo.Should().BeDecoratedWith(x => x.Value == MethodImplOptions.NoInlining); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_method_is_decorated_with_an_attribute_matching_a_predicate_but_it_is_not_it_throws_with_a_useful_message() + { + // Arrange + MethodInfo methodInfo = typeof(ClassWithMethodsThatAreNotDecoratedWithDummyAttribute).GetParameterlessMethod("PublicDoNothing"); + + // Act + Action act = () => + methodInfo.Should().BeDecoratedWith(d => !d.Filter, "because we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected method Void FluentAssertions*ClassWithMethodsThatAreNotDecoratedWithDummyAttribute.PublicDoNothing to be decorated with " + + "FluentAssertions*DummyMethodAttribute because we want to test the error message," + + " but that attribute was not found."); + } + + [Fact] + public void When_asserting_a_method_is_decorated_with_an_MethodImpl_attribute_matching_a_predicate_but_it_is_not_it_throws() + { + // Arrange + MethodInfo methodInfo = typeof(ClassWithMethodWithImplementationAttribute).GetParameterlessMethod("DoNotInlineMe"); + + // Act + Action act = () => + methodInfo.Should().BeDecoratedWith(x => x.Value == MethodImplOptions.AggressiveInlining); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected method Void FluentAssertions*ClassWithMethodWithImplementationAttribute.DoNotInlineMe to be decorated with " + + "System.Runtime.CompilerServices.MethodImplAttribute, but that attribute was not found."); + } + + [Fact] + public void When_asserting_a_method_is_decorated_with_an_attribute_and_multiple_attributes_match_continuation_using_the_matched_value_should_fail() + { + // Arrange + MethodInfo methodInfo = typeof(ClassWithAllMethodsDecoratedWithDummyAttribute).GetParameterlessMethod("PublicDoNothingWithSameAttributeTwice"); + + // Act + Action act = + () => + methodInfo.Should() + .BeDecoratedWith() + .Which.Filter.Should() + .BeTrue(); + + // Assert + act.Should().Throw(); + } + + [Fact] + public void When_subject_is_null_be_decorated_withOfT_should_fail() + { + // Arrange + MethodInfo methodInfo = null; + + // Act + Action act = () => + methodInfo.Should().BeDecoratedWith("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected method to be decorated with *.DummyMethodAttribute *failure message*, but methodInfo is ."); + } + } + + public class NotBeDecoratedWithOfT + { + [Fact] + public void When_asserting_a_method_is_not_decorated_with_attribute_and_it_is_not_it_succeeds() + { + // Arrange + MethodInfo methodInfo = typeof(ClassWithMethodsThatAreNotDecoratedWithDummyAttribute).GetParameterlessMethod("PublicDoNothing"); + + // Act + Action act = () => + methodInfo.Should().NotBeDecoratedWith(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_method_is_not_decorated_with_MethodImpl_attribute_and_it_is_not_it_succeeds() + { + // Arrange + MethodInfo methodInfo = typeof(ClassWithMethodsThatAreNotDecoratedWithDummyAttribute).GetParameterlessMethod("PublicDoNothing"); + + // Act + Action act = () => + methodInfo.Should().NotBeDecoratedWith(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_constructor_is_not_decorated_with_MethodImpl_attribute_and_it_is_not_it_succeeds() + { + // Arrange + ConstructorInfo constructorMethodInfo = typeof(ClassWithMethodWithImplementationAttribute).GetConstructor(new[] { typeof(string) }); + + // Act + Action act = () => + constructorMethodInfo.Should().NotBeDecoratedWith(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_method_is_not_decorated_with_an_attribute_but_it_is_it_throws_with_a_useful_message() + { + // Arrange + MethodInfo methodInfo = typeof(ClassWithAllMethodsDecoratedWithDummyAttribute).GetParameterlessMethod("PublicDoNothing"); + + // Act + Action act = () => + methodInfo.Should().NotBeDecoratedWith("because we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected method Void FluentAssertions*ClassWithAllMethodsDecoratedWithDummyAttribute.PublicDoNothing to not be decorated with " + + "FluentAssertions*DummyMethodAttribute because we want to test the error message," + + " but that attribute was found."); + } + + [Fact] + public void When_asserting_a_method_is_not_decorated_with_MethodImpl_attribute_and_it_is_it_throws() + { + // Arrange + MethodInfo methodInfo = typeof(ClassWithMethodWithImplementationAttribute).GetParameterlessMethod("DoNotInlineMe"); + + // Act + Action act = () => + methodInfo.Should().NotBeDecoratedWith(); + + // Assert + act.Should().Throw() .WithMessage( - "Expected method Void FluentAssertions*ClassWithMethodsThatAreNotDecoratedWithDummyAttribute.PublicDoNothing to be decorated with " + - "FluentAssertions*DummyMethodAttribute because we want to test the error message," + - " but that attribute was not found."); + "Expected method Void FluentAssertions*ClassWithMethodWithImplementationAttribute.DoNotInlineMe to not be decorated with " + + "System.Runtime.CompilerServices.MethodImplAttribute, but that attribute was found."); + } + + [Fact] + public void When_asserting_a_method_is_not_decorated_with_attribute_matching_a_predicate_and_it_is_not_it_succeeds() + { + // Arrange + MethodInfo methodInfo = typeof(ClassWithAllMethodsDecoratedWithDummyAttribute).GetParameterlessMethod("PublicDoNothing"); + + // Act + Action act = () => + methodInfo.Should().NotBeDecoratedWith(d => !d.Filter); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_injecting_a_null_predicate_into_NotBeDecoratedWith_it_should_throw() + { + // Arrange + MethodInfo methodInfo = typeof(ClassWithAllMethodsDecoratedWithDummyAttribute).GetParameterlessMethod("PublicDoNothing"); + + // Act + Action act = () => methodInfo.Should().NotBeDecoratedWith(isMatchingAttributePredicate: null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("isMatchingAttributePredicate"); + } + + [Fact] + public void When_asserting_a_method_is_not_decorated_with_an_attribute_matching_a_predicate_but_it_is_it_throws_with_a_useful_message() + { + // Arrange + MethodInfo methodInfo = typeof(ClassWithAllMethodsDecoratedWithDummyAttribute).GetParameterlessMethod("PublicDoNothing"); + + // Act + Action act = () => + methodInfo.Should().NotBeDecoratedWith(d => d.Filter, "because we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected method Void FluentAssertions*ClassWithAllMethodsDecoratedWithDummyAttribute.PublicDoNothing to not be decorated with " + + "FluentAssertions*DummyMethodAttribute because we want to test the error message," + + " but that attribute was found."); + } + + [Fact] + public void When_subject_is_null_not_be_decorated_withOfT_should_fail() + { + // Arrange + MethodInfo methodInfo = null; + + // Act + Action act = () => + methodInfo.Should().NotBeDecoratedWith("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected method to not be decorated with *.DummyMethodAttribute *failure message*" + + ", but methodInfo is ."); + } + } + + public class BeAsync + { + [Fact] + public void When_asserting_a_method_is_async_and_it_is_then_it_succeeds() + { + // Arrange + MethodInfo methodInfo = typeof(ClassWithAllMethodsAsync).GetParameterlessMethod("PublicAsyncDoNothing"); + + // Act + Action act = () => + methodInfo.Should().BeAsync(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_method_is_async_but_it_is_not_then_it_throws_with_a_useful_message() + { + // Arrange + MethodInfo methodInfo = typeof(ClassWithNonAsyncMethods).GetParameterlessMethod("PublicDoNothing"); + + // Act + Action act = () => + methodInfo.Should().BeAsync("we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected method Task FluentAssertions*ClassWithNonAsyncMethods.PublicDoNothing" + + " to be async because we want to test the error message," + + " but it is not."); + } + + [Fact] + public void When_subject_is_null_be_async_should_fail() + { + // Arrange + MethodInfo methodInfo = null; + + // Act + Action act = () => + methodInfo.Should().BeAsync("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected method to be async *failure message*, but methodInfo is ."); + } + } + + public class NotBeAsync + { + [Fact] + public void When_asserting_a_method_is_not_async_and_it_is_not_then_it_succeeds() + { + // Arrange + MethodInfo methodInfo = typeof(ClassWithNonAsyncMethods).GetParameterlessMethod("PublicDoNothing"); + + // Act + Action act = () => + methodInfo.Should().NotBeAsync(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_method_is_not_async_but_it_is_then_it_throws_with_a_useful_message() + { + // Arrange + MethodInfo methodInfo = typeof(ClassWithAllMethodsAsync).GetParameterlessMethod("PublicAsyncDoNothing"); + + // Act + Action act = () => + methodInfo.Should().NotBeAsync("we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("*ClassWithAllMethodsAsync.PublicAsyncDoNothing*" + + "not to be async*because we want to test the error message*"); + } + + [Fact] + public void When_subject_is_null_not_be_async_should_fail() + { + // Arrange + MethodInfo methodInfo = null; + + // Act + Action act = () => + methodInfo.Should().NotBeAsync("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected method not to be async *failure message*, but methodInfo is ."); + } } - - [Fact] - public void When_asserting_a_method_is_decorated_with_an_MethodImpl_attribute_matching_a_predicate_but_it_is_not_it_throws() - { - // Arrange - MethodInfo methodInfo = typeof(ClassWithMethodWithImplementationAttribute).GetParameterlessMethod("DoNotInlineMe"); - - // Act - Action act = () => - methodInfo.Should().BeDecoratedWith(x => x.Value == MethodImplOptions.AggressiveInlining); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected method Void FluentAssertions*ClassWithMethodWithImplementationAttribute.DoNotInlineMe to be decorated with " + - "System.Runtime.CompilerServices.MethodImplAttribute, but that attribute was not found."); - } - - [Fact] - public void When_asserting_a_method_is_decorated_with_an_attribute_and_multiple_attributes_match_continuation_using_the_matched_value_should_fail() - { - // Arrange - MethodInfo methodInfo = typeof(ClassWithAllMethodsDecoratedWithDummyAttribute).GetParameterlessMethod("PublicDoNothingWithSameAttributeTwice"); - - // Act - Action act = - () => - methodInfo.Should() - .BeDecoratedWith() - .Which.Filter.Should() - .BeTrue(); - - // Assert - act.Should().Throw(); - } - - [Fact] - public void When_subject_is_null_be_decorated_withOfT_should_fail() - { - // Arrange - MethodInfo methodInfo = null; - - // Act - Action act = () => - methodInfo.Should().BeDecoratedWith("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected method to be decorated with *.DummyMethodAttribute *failure message*, but methodInfo is ."); - } - - #endregion - - #region NotBeDecoratedWithOfT - - [Fact] - public void When_asserting_a_method_is_not_decorated_with_attribute_and_it_is_not_it_succeeds() - { - // Arrange - MethodInfo methodInfo = typeof(ClassWithMethodsThatAreNotDecoratedWithDummyAttribute).GetParameterlessMethod("PublicDoNothing"); - - // Act - Action act = () => - methodInfo.Should().NotBeDecoratedWith(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_method_is_not_decorated_with_MethodImpl_attribute_and_it_is_not_it_succeeds() - { - // Arrange - MethodInfo methodInfo = typeof(ClassWithMethodsThatAreNotDecoratedWithDummyAttribute).GetParameterlessMethod("PublicDoNothing"); - - // Act - Action act = () => - methodInfo.Should().NotBeDecoratedWith(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_constructor_is_not_decorated_with_MethodImpl_attribute_and_it_is_not_it_succeeds() - { - // Arrange - ConstructorInfo constructorMethodInfo = typeof(ClassWithMethodWithImplementationAttribute).GetConstructor(new[] { typeof(string) }); - - // Act - Action act = () => - constructorMethodInfo.Should().NotBeDecoratedWith(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_method_is_not_decorated_with_an_attribute_but_it_is_it_throws_with_a_useful_message() - { - // Arrange - MethodInfo methodInfo = typeof(ClassWithAllMethodsDecoratedWithDummyAttribute).GetParameterlessMethod("PublicDoNothing"); - - // Act - Action act = () => - methodInfo.Should().NotBeDecoratedWith("because we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected method Void FluentAssertions*ClassWithAllMethodsDecoratedWithDummyAttribute.PublicDoNothing to not be decorated with " + - "FluentAssertions*DummyMethodAttribute because we want to test the error message," + - " but that attribute was found."); - } - - [Fact] - public void When_asserting_a_method_is_not_decorated_with_MethodImpl_attribute_and_it_is_it_throws() - { - // Arrange - MethodInfo methodInfo = typeof(ClassWithMethodWithImplementationAttribute).GetParameterlessMethod("DoNotInlineMe"); - - // Act - Action act = () => - methodInfo.Should().NotBeDecoratedWith(); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected method Void FluentAssertions*ClassWithMethodWithImplementationAttribute.DoNotInlineMe to not be decorated with " + - "System.Runtime.CompilerServices.MethodImplAttribute, but that attribute was found."); - } - - [Fact] - public void When_asserting_a_method_is_not_decorated_with_attribute_matching_a_predicate_and_it_is_not_it_succeeds() - { - // Arrange - MethodInfo methodInfo = typeof(ClassWithAllMethodsDecoratedWithDummyAttribute).GetParameterlessMethod("PublicDoNothing"); - - // Act - Action act = () => - methodInfo.Should().NotBeDecoratedWith(d => !d.Filter); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_injecting_a_null_predicate_into_NotBeDecoratedWith_it_should_throw() - { - // Arrange - MethodInfo methodInfo = typeof(ClassWithAllMethodsDecoratedWithDummyAttribute).GetParameterlessMethod("PublicDoNothing"); - - // Act - Action act = () => methodInfo.Should().NotBeDecoratedWith(isMatchingAttributePredicate: null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("isMatchingAttributePredicate"); - } - - [Fact] - public void When_asserting_a_method_is_not_decorated_with_an_attribute_matching_a_predicate_but_it_is_it_throws_with_a_useful_message() - { - // Arrange - MethodInfo methodInfo = typeof(ClassWithAllMethodsDecoratedWithDummyAttribute).GetParameterlessMethod("PublicDoNothing"); - - // Act - Action act = () => - methodInfo.Should().NotBeDecoratedWith(d => d.Filter, "because we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected method Void FluentAssertions*ClassWithAllMethodsDecoratedWithDummyAttribute.PublicDoNothing to not be decorated with " + - "FluentAssertions*DummyMethodAttribute because we want to test the error message," + - " but that attribute was found."); - } - - [Fact] - public void When_subject_is_null_not_be_decorated_withOfT_should_fail() - { - // Arrange - MethodInfo methodInfo = null; - - // Act - Action act = () => - methodInfo.Should().NotBeDecoratedWith("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected method to not be decorated with *.DummyMethodAttribute *failure message*" + - ", but methodInfo is ."); - } - - #endregion - - #region BeAsync - - [Fact] - public void When_asserting_a_method_is_async_and_it_is_then_it_succeeds() - { - // Arrange - MethodInfo methodInfo = typeof(ClassWithAllMethodsAsync).GetParameterlessMethod("PublicAsyncDoNothing"); - - // Act - Action act = () => - methodInfo.Should().BeAsync(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_method_is_async_but_it_is_not_then_it_throws_with_a_useful_message() - { - // Arrange - MethodInfo methodInfo = typeof(ClassWithNonAsyncMethods).GetParameterlessMethod("PublicDoNothing"); - - // Act - Action act = () => - methodInfo.Should().BeAsync("we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected method Task FluentAssertions*ClassWithNonAsyncMethods.PublicDoNothing" + - " to be async because we want to test the error message," + - " but it is not."); - } - - [Fact] - public void When_subject_is_null_be_async_should_fail() - { - // Arrange - MethodInfo methodInfo = null; - - // Act - Action act = () => - methodInfo.Should().BeAsync("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected method to be async *failure message*, but methodInfo is ."); - } - - #endregion - - #region NotBeAsync - - [Fact] - public void When_asserting_a_method_is_not_async_and_it_is_not_then_it_succeeds() - { - // Arrange - MethodInfo methodInfo = typeof(ClassWithNonAsyncMethods).GetParameterlessMethod("PublicDoNothing"); - - // Act - Action act = () => - methodInfo.Should().NotBeAsync(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_method_is_not_async_but_it_is_then_it_throws_with_a_useful_message() - { - // Arrange - MethodInfo methodInfo = typeof(ClassWithAllMethodsAsync).GetParameterlessMethod("PublicAsyncDoNothing"); - - // Act - Action act = () => - methodInfo.Should().NotBeAsync("we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("*ClassWithAllMethodsAsync.PublicAsyncDoNothing*" + - "not to be async*because we want to test the error message*"); - } - - [Fact] - public void When_subject_is_null_not_be_async_should_fail() - { - // Arrange - MethodInfo methodInfo = null; - - // Act - Action act = () => - methodInfo.Should().NotBeAsync("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected method not to be async *failure message*, but methodInfo is ."); - } - - #endregion } #region Internal classes used in unit tests @@ -764,6 +758,6 @@ private void DoNothingWithAnotherParameter(string _) { } } - - #endregion } + +#endregion diff --git a/Tests/FluentAssertions.Specs/Types/MethodInfoSelectorAssertionSpecs.cs b/Tests/FluentAssertions.Specs/Types/MethodInfoSelectorAssertionSpecs.cs index ca457d9c36..cba590149e 100644 --- a/Tests/FluentAssertions.Specs/Types/MethodInfoSelectorAssertionSpecs.cs +++ b/Tests/FluentAssertions.Specs/Types/MethodInfoSelectorAssertionSpecs.cs @@ -8,427 +8,419 @@ namespace FluentAssertions.Specs.Types { public class MethodInfoSelectorAssertionSpecs { - #region BeVirtual - - [Fact] - public void When_asserting_methods_are_virtual_and_they_are_it_should_succeed() - { - // Arrange - var methodSelector = new MethodInfoSelector(typeof(ClassWithAllMethodsVirtual)); - - // Act - Action act = () => - methodSelector.Should().BeVirtual(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_methods_are_virtual_but_non_virtual_methods_are_found_it_should_throw() - { - // Arrange - var methodSelector = new MethodInfoSelector(typeof(ClassWithNonVirtualPublicMethods)); - - // Act - Action act = () => - methodSelector.Should().BeVirtual(); - - // Assert - act.Should().Throw(); - } - - [Fact] - public void When_asserting_methods_are_virtual_but_non_virtual_methods_are_found_it_should_throw_with_descriptive_message() - { - // Arrange - var methodSelector = new MethodInfoSelector(typeof(ClassWithNonVirtualPublicMethods)); - - // Act - Action act = () => - methodSelector.Should().BeVirtual("we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected all selected methods" + - " to be virtual because we want to test the error message," + - " but the following methods are not virtual:*" + - "Void FluentAssertions*ClassWithNonVirtualPublicMethods.PublicDoNothing*" + - "Void FluentAssertions*ClassWithNonVirtualPublicMethods.InternalDoNothing*" + - "Void FluentAssertions*ClassWithNonVirtualPublicMethods.ProtectedDoNothing"); - } - - #endregion - - #region NotBeVirtual - - [Fact] - public void When_asserting_methods_are_not_virtual_and_they_are_not_it_should_succeed() - { - // Arrange - var methodSelector = new MethodInfoSelector(typeof(ClassWithNonVirtualPublicMethods)); - - // Act - Action act = () => - methodSelector.Should().NotBeVirtual(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_methods_are_not_virtual_but_virtual_methods_are_found_it_should_throw() - { - // Arrange - var methodSelector = new MethodInfoSelector(typeof(ClassWithAllMethodsVirtual)); - - // Act - Action act = () => - methodSelector.Should().NotBeVirtual(); - - // Assert - act.Should().Throw(); - } - - [Fact] - public void When_asserting_methods_are_not_virtual_but_virtual_methods_are_found_it_should_throw_with_descriptive_message() - { - // Arrange - var methodSelector = new MethodInfoSelector(typeof(ClassWithAllMethodsVirtual)); - - // Act - Action act = () => - methodSelector.Should().NotBeVirtual("we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected all selected methods" + - " not to be virtual because we want to test the error message," + - " but the following methods are virtual" + - "*ClassWithAllMethodsVirtual.PublicVirtualDoNothing" + - "*ClassWithAllMethodsVirtual.InternalVirtualDoNothing" + - "*ClassWithAllMethodsVirtual.ProtectedVirtualDoNothing*"); - } - - #endregion - - #region BeDecoratedWith - - [Fact] - public void When_injecting_a_null_predicate_into_BeDecoratedWith_it_should_throw() - { - // Arrange - var methodSelector = new MethodInfoSelector(typeof(ClassWithAllMethodsDecoratedWithDummyAttribute)); - - // Act - Action act = () => - methodSelector.Should().BeDecoratedWith(isMatchingAttributePredicate: null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("isMatchingAttributePredicate"); - } - - [Fact] - public void When_asserting_methods_are_decorated_with_attribute_and_they_are_it_should_succeed() - { - // Arrange - var methodSelector = new MethodInfoSelector(typeof(ClassWithAllMethodsDecoratedWithDummyAttribute)); - - // Act - Action act = () => - methodSelector.Should().BeDecoratedWith(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_methods_are_decorated_with_attribute_but_they_are_not_it_should_throw() - { - // Arrange - MethodInfoSelector methodSelector = - new MethodInfoSelector(typeof(ClassWithMethodsThatAreNotDecoratedWithDummyAttribute)) - .ThatArePublicOrInternal; - - // Act - Action act = () => - methodSelector.Should().BeDecoratedWith(); - - // Assert - act.Should().Throw(); - } - - [Fact] - public void When_asserting_methods_are_decorated_with_attribute_but_they_are_not_it_should_throw_with_descriptive_message() + public class BeVirtual { - // Arrange - var methodSelector = new MethodInfoSelector(typeof(ClassWithMethodsThatAreNotDecoratedWithDummyAttribute)); - - // Act - Action act = () => - methodSelector.Should().BeDecoratedWith("because we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected all selected methods to be decorated with" + - " FluentAssertions*DummyMethodAttribute because we want to test the error message," + - " but the following methods are not:*" + - "Void FluentAssertions*ClassWithMethodsThatAreNotDecoratedWithDummyAttribute.PublicDoNothing*" + - "Void FluentAssertions*ClassWithMethodsThatAreNotDecoratedWithDummyAttribute.ProtectedDoNothing*" + - "Void FluentAssertions*ClassWithMethodsThatAreNotDecoratedWithDummyAttribute.PrivateDoNothing"); + [Fact] + public void When_asserting_methods_are_virtual_and_they_are_it_should_succeed() + { + // Arrange + var methodSelector = new MethodInfoSelector(typeof(ClassWithAllMethodsVirtual)); + + // Act + Action act = () => + methodSelector.Should().BeVirtual(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_methods_are_virtual_but_non_virtual_methods_are_found_it_should_throw() + { + // Arrange + var methodSelector = new MethodInfoSelector(typeof(ClassWithNonVirtualPublicMethods)); + + // Act + Action act = () => + methodSelector.Should().BeVirtual(); + + // Assert + act.Should().Throw(); + } + + [Fact] + public void When_asserting_methods_are_virtual_but_non_virtual_methods_are_found_it_should_throw_with_descriptive_message() + { + // Arrange + var methodSelector = new MethodInfoSelector(typeof(ClassWithNonVirtualPublicMethods)); + + // Act + Action act = () => + methodSelector.Should().BeVirtual("we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected all selected methods" + + " to be virtual because we want to test the error message," + + " but the following methods are not virtual:*" + + "Void FluentAssertions*ClassWithNonVirtualPublicMethods.PublicDoNothing*" + + "Void FluentAssertions*ClassWithNonVirtualPublicMethods.InternalDoNothing*" + + "Void FluentAssertions*ClassWithNonVirtualPublicMethods.ProtectedDoNothing"); + } } - #endregion - - #region NotBeDecoratedWith - - [Fact] - public void When_injecting_a_null_predicate_into_NotBeDecoratedWith_it_should_throw() - { - // Arrange - var methodSelector = new MethodInfoSelector(typeof(ClassWithMethodsThatAreNotDecoratedWithDummyAttribute)); - - // Act - Action act = () => - methodSelector.Should().NotBeDecoratedWith(isMatchingAttributePredicate: null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("isMatchingAttributePredicate"); - } - - [Fact] - public void When_asserting_methods_are_not_decorated_with_attribute_and_they_are_not_it_should_succeed() - { - // Arrange - var methodSelector = new MethodInfoSelector(typeof(ClassWithMethodsThatAreNotDecoratedWithDummyAttribute)); - - // Act - Action act = () => - methodSelector.Should().NotBeDecoratedWith(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_methods_are_not_decorated_with_attribute_but_they_are_it_should_throw() - { - // Arrange - MethodInfoSelector methodSelector = - new MethodInfoSelector(typeof(ClassWithAllMethodsDecoratedWithDummyAttribute)) - .ThatArePublicOrInternal; - - // Act - Action act = () => - methodSelector.Should().NotBeDecoratedWith(); - - // Assert - act.Should().Throw(); - } - - [Fact] - public void When_asserting_methods_are_not_decorated_with_attribute_but_they_are_it_should_throw_with_descriptive_message() - { - // Arrange - var methodSelector = new MethodInfoSelector(typeof(ClassWithAllMethodsDecoratedWithDummyAttribute)); - - // Act - Action act = () => - methodSelector.Should().NotBeDecoratedWith("because we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected all selected methods to not be decorated*DummyMethodAttribute*because we want to test the error message" + - "*ClassWithAllMethodsDecoratedWithDummyAttribute.PublicDoNothing*" + - "*ClassWithAllMethodsDecoratedWithDummyAttribute.PublicDoNothingWithSameAttributeTwice*" + - "*ClassWithAllMethodsDecoratedWithDummyAttribute.ProtectedDoNothing*" + - "*ClassWithAllMethodsDecoratedWithDummyAttribute.PrivateDoNothing"); - } - - #endregion - - #region Be - - [Fact] - public void When_all_methods_have_specified_accessor_it_should_succeed() - { - // Arrange - var methodSelector = new MethodInfoSelector(typeof(ClassWithPublicMethods)); - - // Act - Action act = () => - methodSelector.Should().Be(CSharpAccessModifier.Public); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_not_all_methods_have_specified_accessor_it_should_throw() + public class NotBeVirtual { - // Arrange - var methodSelector = new MethodInfoSelector(typeof(ClassWithNonPublicMethods)); - - // Act - Action act = () => - methodSelector.Should().Be(CSharpAccessModifier.Public); - - // Assert - act.Should().Throw() - .WithMessage("Expected all selected methods to be Public" + - ", but the following methods are not:*" + - "Void FluentAssertions*ClassWithNonPublicMethods.PublicDoNothing*" + - "Void FluentAssertions*ClassWithNonPublicMethods.DoNothingWithParameter*" + - "Void FluentAssertions*ClassWithNonPublicMethods.DoNothingWithAnotherParameter"); + [Fact] + public void When_asserting_methods_are_not_virtual_and_they_are_not_it_should_succeed() + { + // Arrange + var methodSelector = new MethodInfoSelector(typeof(ClassWithNonVirtualPublicMethods)); + + // Act + Action act = () => + methodSelector.Should().NotBeVirtual(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_methods_are_not_virtual_but_virtual_methods_are_found_it_should_throw() + { + // Arrange + var methodSelector = new MethodInfoSelector(typeof(ClassWithAllMethodsVirtual)); + + // Act + Action act = () => + methodSelector.Should().NotBeVirtual(); + + // Assert + act.Should().Throw(); + } + + [Fact] + public void When_asserting_methods_are_not_virtual_but_virtual_methods_are_found_it_should_throw_with_descriptive_message() + { + // Arrange + var methodSelector = new MethodInfoSelector(typeof(ClassWithAllMethodsVirtual)); + + // Act + Action act = () => + methodSelector.Should().NotBeVirtual("we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected all selected methods" + + " not to be virtual because we want to test the error message," + + " but the following methods are virtual" + + "*ClassWithAllMethodsVirtual.PublicVirtualDoNothing" + + "*ClassWithAllMethodsVirtual.InternalVirtualDoNothing" + + "*ClassWithAllMethodsVirtual.ProtectedVirtualDoNothing*"); + } } - [Fact] - public void When_not_all_methods_have_specified_accessor_it_should_throw_with_descriptive_message() + public class BeDecoratedWith { - // Arrange - var methodSelector = new MethodInfoSelector(typeof(ClassWithNonPublicMethods)); - - // Act - Action act = () => - methodSelector.Should().Be(CSharpAccessModifier.Public, "we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected all selected methods to be Public" + - " because we want to test the error message" + - ", but the following methods are not:*" + - "Void FluentAssertions*ClassWithNonPublicMethods.PublicDoNothing*" + - "Void FluentAssertions*ClassWithNonPublicMethods.DoNothingWithParameter*" + - "Void FluentAssertions*ClassWithNonPublicMethods.DoNothingWithAnotherParameter"); + [Fact] + public void When_injecting_a_null_predicate_into_BeDecoratedWith_it_should_throw() + { + // Arrange + var methodSelector = new MethodInfoSelector(typeof(ClassWithAllMethodsDecoratedWithDummyAttribute)); + + // Act + Action act = () => + methodSelector.Should().BeDecoratedWith(isMatchingAttributePredicate: null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("isMatchingAttributePredicate"); + } + + [Fact] + public void When_asserting_methods_are_decorated_with_attribute_and_they_are_it_should_succeed() + { + // Arrange + var methodSelector = new MethodInfoSelector(typeof(ClassWithAllMethodsDecoratedWithDummyAttribute)); + + // Act + Action act = () => + methodSelector.Should().BeDecoratedWith(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_methods_are_decorated_with_attribute_but_they_are_not_it_should_throw() + { + // Arrange + MethodInfoSelector methodSelector = + new MethodInfoSelector(typeof(ClassWithMethodsThatAreNotDecoratedWithDummyAttribute)) + .ThatArePublicOrInternal; + + // Act + Action act = () => + methodSelector.Should().BeDecoratedWith(); + + // Assert + act.Should().Throw(); + } + + [Fact] + public void When_asserting_methods_are_decorated_with_attribute_but_they_are_not_it_should_throw_with_descriptive_message() + { + // Arrange + var methodSelector = new MethodInfoSelector(typeof(ClassWithMethodsThatAreNotDecoratedWithDummyAttribute)); + + // Act + Action act = () => + methodSelector.Should().BeDecoratedWith("because we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected all selected methods to be decorated with" + + " FluentAssertions*DummyMethodAttribute because we want to test the error message," + + " but the following methods are not:*" + + "Void FluentAssertions*ClassWithMethodsThatAreNotDecoratedWithDummyAttribute.PublicDoNothing*" + + "Void FluentAssertions*ClassWithMethodsThatAreNotDecoratedWithDummyAttribute.ProtectedDoNothing*" + + "Void FluentAssertions*ClassWithMethodsThatAreNotDecoratedWithDummyAttribute.PrivateDoNothing"); + } } - #endregion - - #region NotBe - - [Fact] - public void When_all_methods_does_not_have_specified_accessor_it_should_succeed() + public class NotBeDecoratedWith { - // Arrange - var methodSelector = new MethodInfoSelector(typeof(ClassWithNonPublicMethods)); - - // Act - Action act = () => - methodSelector.Should().NotBe(CSharpAccessModifier.Public); - - // Assert - act.Should().NotThrow(); + [Fact] + public void When_injecting_a_null_predicate_into_NotBeDecoratedWith_it_should_throw() + { + // Arrange + var methodSelector = new MethodInfoSelector(typeof(ClassWithMethodsThatAreNotDecoratedWithDummyAttribute)); + + // Act + Action act = () => + methodSelector.Should().NotBeDecoratedWith(isMatchingAttributePredicate: null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("isMatchingAttributePredicate"); + } + + [Fact] + public void When_asserting_methods_are_not_decorated_with_attribute_and_they_are_not_it_should_succeed() + { + // Arrange + var methodSelector = new MethodInfoSelector(typeof(ClassWithMethodsThatAreNotDecoratedWithDummyAttribute)); + + // Act + Action act = () => + methodSelector.Should().NotBeDecoratedWith(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_methods_are_not_decorated_with_attribute_but_they_are_it_should_throw() + { + // Arrange + MethodInfoSelector methodSelector = + new MethodInfoSelector(typeof(ClassWithAllMethodsDecoratedWithDummyAttribute)) + .ThatArePublicOrInternal; + + // Act + Action act = () => + methodSelector.Should().NotBeDecoratedWith(); + + // Assert + act.Should().Throw(); + } + + [Fact] + public void When_asserting_methods_are_not_decorated_with_attribute_but_they_are_it_should_throw_with_descriptive_message() + { + // Arrange + var methodSelector = new MethodInfoSelector(typeof(ClassWithAllMethodsDecoratedWithDummyAttribute)); + + // Act + Action act = () => + methodSelector.Should().NotBeDecoratedWith("because we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected all selected methods to not be decorated*DummyMethodAttribute*because we want to test the error message" + + "*ClassWithAllMethodsDecoratedWithDummyAttribute.PublicDoNothing*" + + "*ClassWithAllMethodsDecoratedWithDummyAttribute.PublicDoNothingWithSameAttributeTwice*" + + "*ClassWithAllMethodsDecoratedWithDummyAttribute.ProtectedDoNothing*" + + "*ClassWithAllMethodsDecoratedWithDummyAttribute.PrivateDoNothing"); + } } - [Fact] - public void When_any_method_have_specified_accessor_it_should_throw() + public class Be { - // Arrange - var methodSelector = new MethodInfoSelector(typeof(ClassWithPublicMethods)); - - // Act - Action act = () => - methodSelector.Should().NotBe(CSharpAccessModifier.Public); - - // Assert - act.Should().Throw() - .WithMessage("Expected all selected methods to not be Public" + - ", but the following methods are:*" + - "Void FluentAssertions*ClassWithPublicMethods.PublicDoNothing*"); + [Fact] + public void When_all_methods_have_specified_accessor_it_should_succeed() + { + // Arrange + var methodSelector = new MethodInfoSelector(typeof(ClassWithPublicMethods)); + + // Act + Action act = () => + methodSelector.Should().Be(CSharpAccessModifier.Public); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_not_all_methods_have_specified_accessor_it_should_throw() + { + // Arrange + var methodSelector = new MethodInfoSelector(typeof(ClassWithNonPublicMethods)); + + // Act + Action act = () => + methodSelector.Should().Be(CSharpAccessModifier.Public); + + // Assert + act.Should().Throw() + .WithMessage("Expected all selected methods to be Public" + + ", but the following methods are not:*" + + "Void FluentAssertions*ClassWithNonPublicMethods.PublicDoNothing*" + + "Void FluentAssertions*ClassWithNonPublicMethods.DoNothingWithParameter*" + + "Void FluentAssertions*ClassWithNonPublicMethods.DoNothingWithAnotherParameter"); + } + + [Fact] + public void When_not_all_methods_have_specified_accessor_it_should_throw_with_descriptive_message() + { + // Arrange + var methodSelector = new MethodInfoSelector(typeof(ClassWithNonPublicMethods)); + + // Act + Action act = () => + methodSelector.Should().Be(CSharpAccessModifier.Public, "we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected all selected methods to be Public" + + " because we want to test the error message" + + ", but the following methods are not:*" + + "Void FluentAssertions*ClassWithNonPublicMethods.PublicDoNothing*" + + "Void FluentAssertions*ClassWithNonPublicMethods.DoNothingWithParameter*" + + "Void FluentAssertions*ClassWithNonPublicMethods.DoNothingWithAnotherParameter"); + } } - [Fact] - public void When_any_method_have_specified_accessor_it_should_throw_with_descriptive_message() + public class NotBe { - // Arrange - var methodSelector = new MethodInfoSelector(typeof(ClassWithPublicMethods)); - - // Act - Action act = () => - methodSelector.Should().NotBe(CSharpAccessModifier.Public, "we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected all selected methods to not be Public" + - " because we want to test the error message" + - ", but the following methods are:*" + - "Void FluentAssertions*ClassWithPublicMethods.PublicDoNothing*"); + [Fact] + public void When_all_methods_does_not_have_specified_accessor_it_should_succeed() + { + // Arrange + var methodSelector = new MethodInfoSelector(typeof(ClassWithNonPublicMethods)); + + // Act + Action act = () => + methodSelector.Should().NotBe(CSharpAccessModifier.Public); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_any_method_have_specified_accessor_it_should_throw() + { + // Arrange + var methodSelector = new MethodInfoSelector(typeof(ClassWithPublicMethods)); + + // Act + Action act = () => + methodSelector.Should().NotBe(CSharpAccessModifier.Public); + + // Assert + act.Should().Throw() + .WithMessage("Expected all selected methods to not be Public" + + ", but the following methods are:*" + + "Void FluentAssertions*ClassWithPublicMethods.PublicDoNothing*"); + } + + [Fact] + public void When_any_method_have_specified_accessor_it_should_throw_with_descriptive_message() + { + // Arrange + var methodSelector = new MethodInfoSelector(typeof(ClassWithPublicMethods)); + + // Act + Action act = () => + methodSelector.Should().NotBe(CSharpAccessModifier.Public, "we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected all selected methods to not be Public" + + " because we want to test the error message" + + ", but the following methods are:*" + + "Void FluentAssertions*ClassWithPublicMethods.PublicDoNothing*"); + } } - #endregion - - #region BeAsync - - [Fact] - public void When_asserting_methods_are_async_and_they_are_then_it_succeeds() + public class BeAsync { - // Arrange - var methodSelector = new MethodInfoSelector(typeof(ClassWithAllMethodsAsync)); - - // Act - Action act = () => methodSelector.Should().BeAsync(); - - // Assert - act.Should().NotThrow(); + [Fact] + public void When_asserting_methods_are_async_and_they_are_then_it_succeeds() + { + // Arrange + var methodSelector = new MethodInfoSelector(typeof(ClassWithAllMethodsAsync)); + + // Act + Action act = () => methodSelector.Should().BeAsync(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_methods_are_async_but_non_async_methods_are_found_it_should_throw_with_descriptive_message() + { + // Arrange + var methodSelector = new MethodInfoSelector(typeof(ClassWithNonAsyncMethods)); + + // Act + Action act = () => methodSelector.Should().BeAsync("we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected all selected methods" + + " to be async because we want to test the error message," + + " but the following methods are not:" + Environment.NewLine + + "Task FluentAssertions.Specs.Types.ClassWithNonAsyncMethods.PublicDoNothing" + Environment.NewLine + + "Task FluentAssertions.Specs.Types.ClassWithNonAsyncMethods.InternalDoNothing" + Environment.NewLine + + "Task FluentAssertions.Specs.Types.ClassWithNonAsyncMethods.ProtectedDoNothing"); + } } - [Fact] - public void When_asserting_methods_are_async_but_non_async_methods_are_found_it_should_throw_with_descriptive_message() + public class NotBeAsync { - // Arrange - var methodSelector = new MethodInfoSelector(typeof(ClassWithNonAsyncMethods)); - - // Act - Action act = () => methodSelector.Should().BeAsync("we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected all selected methods" + - " to be async because we want to test the error message," + - " but the following methods are not:" + Environment.NewLine + - "Task FluentAssertions.Specs.Types.ClassWithNonAsyncMethods.PublicDoNothing" + Environment.NewLine + - "Task FluentAssertions.Specs.Types.ClassWithNonAsyncMethods.InternalDoNothing" + Environment.NewLine + - "Task FluentAssertions.Specs.Types.ClassWithNonAsyncMethods.ProtectedDoNothing"); + [Fact] + public void When_asserting_methods_are_not_async_and_they_are_not_then_it_succeeds() + { + // Arrange + var methodSelector = new MethodInfoSelector(typeof(ClassWithNonAsyncMethods)); + + // Act + Action act = () => methodSelector.Should().NotBeAsync(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_methods_are_not_async_but_async_methods_are_found_it_should_throw_with_descriptive_message() + { + // Arrange + var methodSelector = new MethodInfoSelector(typeof(ClassWithAllMethodsAsync)); + + // Act + Action act = () => methodSelector.Should().NotBeAsync("we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected all selected methods" + + " not to be async because we want to test the error message," + + " but the following methods are:" + Environment.NewLine + + "Task FluentAssertions.Specs.Types.ClassWithAllMethodsAsync.PublicAsyncDoNothing" + Environment.NewLine + + "Task FluentAssertions.Specs.Types.ClassWithAllMethodsAsync.InternalAsyncDoNothing" + Environment.NewLine + + "Task FluentAssertions.Specs.Types.ClassWithAllMethodsAsync.ProtectedAsyncDoNothing"); + } } - - #endregion - - #region NotBeAsync - - [Fact] - public void When_asserting_methods_are_not_async_and_they_are_not_then_it_succeeds() - { - // Arrange - var methodSelector = new MethodInfoSelector(typeof(ClassWithNonAsyncMethods)); - - // Act - Action act = () => methodSelector.Should().NotBeAsync(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_methods_are_not_async_but_async_methods_are_found_it_should_throw_with_descriptive_message() - { - // Arrange - var methodSelector = new MethodInfoSelector(typeof(ClassWithAllMethodsAsync)); - - // Act - Action act = () => methodSelector.Should().NotBeAsync("we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected all selected methods" + - " not to be async because we want to test the error message," + - " but the following methods are:" + Environment.NewLine + - "Task FluentAssertions.Specs.Types.ClassWithAllMethodsAsync.PublicAsyncDoNothing" + Environment.NewLine + - "Task FluentAssertions.Specs.Types.ClassWithAllMethodsAsync.InternalAsyncDoNothing" + Environment.NewLine + - "Task FluentAssertions.Specs.Types.ClassWithAllMethodsAsync.ProtectedAsyncDoNothing"); - } - - #endregion } } diff --git a/Tests/FluentAssertions.Specs/Types/PropertyInfoAssertionSpecs.cs b/Tests/FluentAssertions.Specs/Types/PropertyInfoAssertionSpecs.cs index 85b2a120fa..ca91a91de5 100644 --- a/Tests/FluentAssertions.Specs/Types/PropertyInfoAssertionSpecs.cs +++ b/Tests/FluentAssertions.Specs/Types/PropertyInfoAssertionSpecs.cs @@ -9,821 +9,807 @@ namespace FluentAssertions.Specs.Types { public class PropertyInfoAssertionSpecs { - #region BeVirtual + public class BeVirtual + { + [Fact] + public void When_asserting_that_a_property_is_virtual_and_it_is_then_it_succeeds() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithAllPropertiesVirtual).GetRuntimeProperty("PublicVirtualProperty"); + + // Act + Action act = () => + propertyInfo.Should().BeVirtual(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_that_a_property_is_virtual_and_it_is_not_then_it_fails_with_useful_message() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithNonVirtualPublicProperties).GetRuntimeProperty("PublicNonVirtualProperty"); + + // Act + Action act = () => + propertyInfo.Should().BeVirtual("we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected property String FluentAssertions*ClassWithNonVirtualPublicProperties.PublicNonVirtualProperty" + + " to be virtual because we want to test the error message," + + " but it is not."); + } + + [Fact] + public void When_subject_is_null_be_virtual_should_fail() + { + // Arrange + PropertyInfo propertyInfo = null; + + // Act + Action act = () => + propertyInfo.Should().BeVirtual("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected property to be virtual *failure message*, but propertyInfo is ."); + } + } + + public class NotBeVirtual + { + [Fact] + public void When_asserting_that_a_property_is_not_virtual_and_it_is_not_then_it_succeeds() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithNonVirtualPublicProperties).GetRuntimeProperty("PublicNonVirtualProperty"); + + // Act + Action act = () => + propertyInfo.Should().NotBeVirtual(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_that_a_property_is_not_virtual_and_it_is_then_it_fails_with_useful_message() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithAllPropertiesVirtual).GetRuntimeProperty("PublicVirtualProperty"); + + // Act + Action act = () => + propertyInfo.Should().NotBeVirtual("we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected property *ClassWithAllPropertiesVirtual.PublicVirtualProperty" + + " not to be virtual because we want to test the error message," + + " but it is."); + } + + [Fact] + public void When_subject_is_null_not_be_virtual_should_fail() + { + // Arrange + PropertyInfo propertyInfo = null; + + // Act + Action act = () => + propertyInfo.Should().NotBeVirtual("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected property not to be virtual *failure message*, but propertyInfo is ."); + } + } + + public class BeDecortatedWithOfT + { + [Fact] + public void When_asserting_a_property_is_decorated_with_attribute_and_it_is_it_succeeds() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithAllPropertiesDecoratedWithDummyAttribute).GetRuntimeProperty("PublicProperty"); + + // Act + Action act = () => + propertyInfo.Should().BeDecoratedWith(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_a_property_is_decorated_with_an_attribute_it_allow_chaining_assertions() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithAllPropertiesDecoratedWithDummyAttribute).GetRuntimeProperty("PublicProperty"); + + // Act + Action act = () => + propertyInfo.Should().BeDecoratedWith().Which.Value.Should().Be("OtherValue"); + + // Assert + act.Should().Throw().WithMessage("Expected*OtherValue*Value*"); + } + + [Fact] + public void When_a_property_is_decorated_with_an_attribute_and_multiple_attributes_match_continuation_using_the_matched_value_fail() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithAllPropertiesDecoratedWithDummyAttribute).GetRuntimeProperty("PublicPropertyWithSameAttributeTwice"); + + // Act + Action act = () => + propertyInfo.Should().BeDecoratedWith().Which.Value.Should().Be("OtherValue"); + + // Assert + act.Should().Throw(); + } + + [Fact] + public void When_asserting_a_property_is_decorated_with_attribute_and_it_is_not_it_throw_with_useful_message() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithPropertiesThatAreNotDecoratedWithDummyAttribute).GetRuntimeProperty("PublicProperty"); + + // Act + Action act = () => + propertyInfo.Should().BeDecoratedWith("because we want to test the error message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected property String " + + "FluentAssertions*ClassWithPropertiesThatAreNotDecoratedWithDummyAttribute.PublicProperty to be decorated with " + + "FluentAssertions*DummyPropertyAttribute because we want to test the error message, but that attribute was not found."); + } + + [Fact] + public void When_asserting_a_property_is_decorated_with_an_attribute_matching_a_predicate_but_it_is_not_it_throw_with_useful_message() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithPropertiesThatAreNotDecoratedWithDummyAttribute).GetRuntimeProperty("PublicProperty"); + + // Act + Action act = () => + propertyInfo.Should().BeDecoratedWith(d => d.Value == "NotARealValue", "because we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected property String FluentAssertions*ClassWithPropertiesThatAreNotDecoratedWithDummyAttribute.PublicProperty to be decorated with " + + "FluentAssertions*DummyPropertyAttribute because we want to test the error message," + + " but that attribute was not found."); + } + + [Fact] + public void When_asserting_a_property_is_decorated_with_attribute_matching_a_predicate_and_it_is_it_succeeds() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithAllPropertiesDecoratedWithDummyAttribute).GetRuntimeProperty("PublicProperty"); + + // Act + Action act = () => + propertyInfo.Should().BeDecoratedWith(d => d.Value == "Value"); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_subject_is_null_be_decorated_withOfT_should_fail() + { + // Arrange + PropertyInfo propertyInfo = null; + + // Act + Action act = () => + propertyInfo.Should().BeDecoratedWith("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected property to be decorated with *.DummyPropertyAttribute *failure message*, but propertyInfo is ."); + } + + [Fact] + public void When_asserting_property_is_decorated_with_null_it_should_throw() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithAllPropertiesDecoratedWithDummyAttribute).GetRuntimeProperty("PublicProperty"); + + // Act + Action act = () => + propertyInfo.Should().BeDecoratedWith((Expression>)null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("isMatchingAttributePredicate"); + } + } + + public class NotBeDecoratedWithOfT + { + [Fact] + public void When_subject_is_null_not_be_decorated_withOfT_should_fail() + { + // Arrange + PropertyInfo propertyInfo = null; + + // Act + Action act = () => + propertyInfo.Should().NotBeDecoratedWith("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected property to not be decorated with *.DummyPropertyAttribute *failure message*, but propertyInfo is ."); + } + + [Fact] + public void When_asserting_property_is_not_decorated_with_null_it_should_throw() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("StringProperty"); + + // Act + Action act = () => + propertyInfo.Should().NotBeDecoratedWith((Expression>)null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("isMatchingAttributePredicate"); + } + } + + public class BeWritable + { + [Fact] + public void When_asserting_a_readonly_property_is_writable_it_fails_with_useful_message() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("ReadOnlyProperty"); + + // Act + Action action = () => propertyInfo.Should().BeWritable("we want to test the error {0}", "message"); + + // Assert + action + .Should().Throw() + .WithMessage("Expected propertyInfo ReadOnlyProperty to have a setter because we want to test the error message."); + } + + [Fact] + public void When_asserting_a_readwrite_property_is_writable_it_succeeds() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("ReadWriteProperty"); + + // Act + Action action = () => propertyInfo.Should().BeWritable("that's required"); + + // Assert + action.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_writeonly_property_is_writable_it_succeeds() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("WriteOnlyProperty"); + + // Act + Action action = () => propertyInfo.Should().BeWritable("that's required"); + + // Assert + action.Should().NotThrow(); + } + + [Fact] + public void When_subject_is_null_be_writable_should_fail() + { + // Arrange + PropertyInfo propertyInfo = null; + + // Act + Action act = () => + propertyInfo.Should().BeWritable("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected property to have a setter *failure message*, but propertyInfo is ."); + } + } + + public class BeReadable + { + [Fact] + public void When_asserting_a_readonly_property_is_readable_it_succeeds() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("ReadOnlyProperty"); + + // Act + Action action = () => propertyInfo.Should().BeReadable("that's required"); + + // Assert + action.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_readwrite_property_is_readable_it_succeeds() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithReadOnlyProperties).GetRuntimeProperty("ReadWriteProperty"); + + // Act + Action action = () => propertyInfo.Should().BeReadable("that's required"); + + // Assert + action.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_writeonly_property_is_readable_it_fails_with_useful_message() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("WriteOnlyProperty"); + + // Act + Action action = () => propertyInfo.Should().BeReadable("we want to test the error {0}", "message"); + + // Assert + action + .Should().Throw() + .WithMessage("Expected property WriteOnlyProperty to have a getter because we want to test the error message, but it does not."); + } + + [Fact] + public void When_subject_is_null_be_readable_should_fail() + { + // Arrange + PropertyInfo propertyInfo = null; + + // Act + Action act = () => + propertyInfo.Should().BeReadable("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected property to have a getter *failure message*, but propertyInfo is ."); + } + } + + public class NotBeWritable + { + [Fact] + public void When_asserting_a_readonly_property_is_not_writable_it_succeeds() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithReadOnlyProperties).GetRuntimeProperty("ReadOnlyProperty"); + + // Act + Action action = () => propertyInfo.Should().NotBeWritable("that's required"); + + // Assert + action.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_readwrite_property_is_not_writable_it_fails_with_useful_message() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithReadOnlyProperties).GetRuntimeProperty("ReadWriteProperty"); + + // Act + Action action = () => propertyInfo.Should().NotBeWritable("we want to test the error {0}", "message"); + + // Assert + action + .Should().Throw() + .WithMessage("Expected propertyInfo ReadWriteProperty not to have a setter because we want to test the error message."); + } + + [Fact] + public void When_asserting_a_writeonly_property_is_not_writable_it_fails_with_useful_message() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("WriteOnlyProperty"); + + // Act + Action action = () => propertyInfo.Should().NotBeWritable("we want to test the error {0}", "message"); + + // Assert + action + .Should().Throw() + .WithMessage("Expected propertyInfo WriteOnlyProperty not to have a setter because we want to test the error message."); + } + + [Fact] + public void When_subject_is_null_not_be_writable_should_fail() + { + // Arrange + PropertyInfo propertyInfo = null; + + // Act + Action act = () => + propertyInfo.Should().NotBeWritable("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected property not to have a setter *failure message*, but propertyInfo is ."); + } + } + + public class NotBeReadable + { + [Fact] + public void When_asserting_a_readonly_property_is_not_readable_it_fails_with_useful_message() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithReadOnlyProperties).GetRuntimeProperty("ReadOnlyProperty"); + + // Act + Action action = () => propertyInfo.Should().NotBeReadable("we want to test the error {0}", "message"); + + // Assert + action + .Should().Throw() + .WithMessage("Expected propertyInfo ReadOnlyProperty not to have a getter because we want to test the error message."); + } + + [Fact] + public void When_asserting_a_readwrite_property_is_not_readable_it_fails_with_useful_message() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithReadOnlyProperties).GetRuntimeProperty("ReadWriteProperty"); + + // Act + Action action = () => propertyInfo.Should().NotBeReadable("we want to test the error {0}", "message"); + + // Assert + action + .Should().Throw() + .WithMessage("Expected propertyInfo ReadWriteProperty not to have a getter because we want to test the error message."); + } + + [Fact] + public void When_asserting_a_writeonly_property_is_not_readable_it_succeeds() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("WriteOnlyProperty"); + + // Act + Action action = () => propertyInfo.Should().NotBeReadable("that's required"); + + // Assert + action.Should().NotThrow(); + } + + [Fact] + public void When_subject_is_null_not_be_readable_should_fail() + { + // Arrange + PropertyInfo propertyInfo = null; + + // Act + Action act = () => + propertyInfo.Should().NotBeReadable("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected property not to have a getter *failure message*, but propertyInfo is ."); + } + } + + public class BeReadableAccessModifier + { + [Fact] + public void When_asserting_a_public_read_private_write_property_is_public_readable_it_succeeds() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("ReadPrivateWriteProperty"); + + // Act + Action action = () => propertyInfo.Should().BeReadable(CSharpAccessModifier.Public, "that's required"); + + // Assert + action.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_private_read_public_write_property_is_public_readable_it_fails_with_useful_message() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("WritePrivateReadProperty"); + + // Act + Action action = () => propertyInfo.Should().BeReadable(CSharpAccessModifier.Public, "we want to test the error {0}", "message"); + + // Assert + action.Should().Throw() + .WithMessage("Expected method get_WritePrivateReadProperty to be Public because we want to test the error message, but it is Private."); + } + + [Fact] + public void When_subject_is_null_be_readable_with_accessmodifier_should_fail() + { + // Arrange + PropertyInfo propertyInfo = null; + + // Act + Action act = () => + propertyInfo.Should().BeReadable(CSharpAccessModifier.Public, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected property to be Public *failure message*, but propertyInfo is ."); + } + + [Fact] + public void When_asserting_is_readable_with_an_invalid_enum_value_it_should_throw() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("StringProperty"); + + // Act + Action act = () => + propertyInfo.Should().BeReadable((CSharpAccessModifier)int.MaxValue); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("accessModifier"); + } + } + + public class BeWritableAccessModifier + { + [Fact] + public void When_asserting_a_public_write_private_read_property_is_public_writable_it_succeeds() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("WritePrivateReadProperty"); + + // Act + Action action = () => propertyInfo.Should().BeWritable(CSharpAccessModifier.Public, "that's required"); + + // Assert + action.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_private_write_public_read_property_is_public_writable_it_fails_with_useful_message() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("ReadPrivateWriteProperty"); + + // Act + Action action = () => propertyInfo.Should().BeWritable(CSharpAccessModifier.Public, "we want to test the error {0}", "message"); + + // Assert + action.Should().Throw() + .WithMessage("Expected method set_ReadPrivateWriteProperty to be Public because we want to test the error message, but it is Private."); + } + + [Fact] + public void When_subject_is_null_be_writable_with_accessmodifier_should_fail() + { + // Arrange + PropertyInfo propertyInfo = null; + + // Act + Action act = () => + propertyInfo.Should().BeWritable(CSharpAccessModifier.Public, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected property to be Public *failure message*, but propertyInfo is ."); + } + + [Fact] + public void When_asserting_is_writable_with_an_invalid_enum_value_it_should_throw() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("StringProperty"); + + // Act + Action act = () => + propertyInfo.Should().BeWritable((CSharpAccessModifier)int.MaxValue); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("accessModifier"); + } + } + + public class Return + { + [Fact] + public void When_asserting_a_String_property_returns_a_String_it_succeeds() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("StringProperty"); + + // Act + Action action = () => propertyInfo.Should().Return(typeof(string)); + + // Assert + action.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_String_property_returns_an_Int32_it_throw_with_a_useful_message() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("StringProperty"); + + // Act + Action action = () => propertyInfo.Should().Return(typeof(int), "we want to test the error {0}", "message"); + + // Assert + action.Should().Throw() + .WithMessage("Expected Type of property StringProperty to be System.Int32 because we want to test the error " + + "message, but it is System.String."); + } + + [Fact] + public void When_subject_is_null_return_should_fail() + { + // Arrange + PropertyInfo propertyInfo = null; + + // Act + Action act = () => + propertyInfo.Should().Return(typeof(int), "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected type of property to be *.Int32 *failure message*, but propertyInfo is ."); + } + + [Fact] + public void When_asserting_property_type_is_null_it_should_throw() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("StringProperty"); + + // Act + Action act = () => + propertyInfo.Should().Return(null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("propertyType"); + } + } + + public class ReturnOfT + { + [Fact] + public void When_asserting_a_String_property_returnsOfT_a_String_it_succeeds() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("StringProperty"); + + // Act + Action action = () => propertyInfo.Should().Return(); + + // Assert + action.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_String_property_returnsOfT_an_Int32_it_throw_with_a_useful_message() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("StringProperty"); + + // Act + Action action = () => propertyInfo.Should().Return("we want to test the error {0}", "message"); + + // Assert + action.Should().Throw() + .WithMessage("Expected Type of property StringProperty to be System.Int32 because we want to test the error " + + "message, but it is System.String."); + } + } + + public class NotReturn + { + [Fact] + public void When_asserting_a_String_property_does_not_returns_an_Int32_it_succeeds() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("StringProperty"); + + // Act + Action action = () => propertyInfo.Should().NotReturn(typeof(int)); + + // Assert + action.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_String_property_does_not_return_a_String_it_throw_with_a_useful_message() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("StringProperty"); + + // Act + Action action = () => propertyInfo.Should().NotReturn(typeof(string), "we want to test the error {0}", "message"); + + // Assert + action.Should().Throw() + .WithMessage("Expected Type of property StringProperty not to be*String*because we want to test the error " + + "message, but it is."); + } + + [Fact] + public void When_subject_is_null_not_return_should_fail() + { + // Arrange + PropertyInfo propertyInfo = null; + + // Act + Action act = () => + propertyInfo.Should().NotReturn(typeof(int), "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected type of property not to be *.Int32 *failure message*, but propertyInfo is ."); + } + + [Fact] + public void When_asserting_property_type_is_not_null_it_should_throw() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("StringProperty"); + + // Act + Action act = () => + propertyInfo.Should().NotReturn(null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("propertyType"); + } + } - [Fact] - public void When_asserting_that_a_property_is_virtual_and_it_is_then_it_succeeds() + public class NotReturnOfT { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithAllPropertiesVirtual).GetRuntimeProperty("PublicVirtualProperty"); + [Fact] + public void When_asserting_a_String_property_does_not_returnOfT_an_Int32_it_succeeds() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("StringProperty"); - // Act - Action act = () => - propertyInfo.Should().BeVirtual(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_that_a_property_is_virtual_and_it_is_not_then_it_fails_with_useful_message() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithNonVirtualPublicProperties).GetRuntimeProperty("PublicNonVirtualProperty"); - - // Act - Action act = () => - propertyInfo.Should().BeVirtual("we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected property String FluentAssertions*ClassWithNonVirtualPublicProperties.PublicNonVirtualProperty" + - " to be virtual because we want to test the error message," + - " but it is not."); - } - - [Fact] - public void When_subject_is_null_be_virtual_should_fail() - { - // Arrange - PropertyInfo propertyInfo = null; - - // Act - Action act = () => - propertyInfo.Should().BeVirtual("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected property to be virtual *failure message*, but propertyInfo is ."); + // Act + Action action = () => propertyInfo.Should().NotReturn(); + + // Assert + action.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_String_property_does_not_returnsOfT_a_String_it_throw_with_a_useful_message() + { + // Arrange + PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("StringProperty"); + + // Act + Action action = () => propertyInfo.Should().NotReturn("we want to test the error {0}", "message"); + + // Assert + action.Should().Throw() + .WithMessage("Expected Type of property StringProperty not to be*String*because we want to test the error " + + "message, but it is."); + } } - #endregion - - #region NotBeVirtual - - [Fact] - public void When_asserting_that_a_property_is_not_virtual_and_it_is_not_then_it_succeeds() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithNonVirtualPublicProperties).GetRuntimeProperty("PublicNonVirtualProperty"); - - // Act - Action act = () => - propertyInfo.Should().NotBeVirtual(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_that_a_property_is_not_virtual_and_it_is_then_it_fails_with_useful_message() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithAllPropertiesVirtual).GetRuntimeProperty("PublicVirtualProperty"); - - // Act - Action act = () => - propertyInfo.Should().NotBeVirtual("we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected property *ClassWithAllPropertiesVirtual.PublicVirtualProperty" + - " not to be virtual because we want to test the error message," + - " but it is."); - } - - [Fact] - public void When_subject_is_null_not_be_virtual_should_fail() - { - // Arrange - PropertyInfo propertyInfo = null; - - // Act - Action act = () => - propertyInfo.Should().NotBeVirtual("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected property not to be virtual *failure message*, but propertyInfo is ."); - } - - #endregion - - #region BeDecortatedWithOfT - - [Fact] - public void When_asserting_a_property_is_decorated_with_attribute_and_it_is_it_succeeds() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithAllPropertiesDecoratedWithDummyAttribute).GetRuntimeProperty("PublicProperty"); - - // Act - Action act = () => - propertyInfo.Should().BeDecoratedWith(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_a_property_is_decorated_with_an_attribute_it_allow_chaining_assertions() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithAllPropertiesDecoratedWithDummyAttribute).GetRuntimeProperty("PublicProperty"); - - // Act - Action act = () => - propertyInfo.Should().BeDecoratedWith().Which.Value.Should().Be("OtherValue"); - - // Assert - act.Should().Throw().WithMessage("Expected*OtherValue*Value*"); - } - - [Fact] - public void When_a_property_is_decorated_with_an_attribute_and_multiple_attributes_match_continuation_using_the_matched_value_fail() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithAllPropertiesDecoratedWithDummyAttribute).GetRuntimeProperty("PublicPropertyWithSameAttributeTwice"); - - // Act - Action act = () => - propertyInfo.Should().BeDecoratedWith().Which.Value.Should().Be("OtherValue"); - - // Assert - act.Should().Throw(); - } - - [Fact] - public void When_asserting_a_property_is_decorated_with_attribute_and_it_is_not_it_throw_with_useful_message() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithPropertiesThatAreNotDecoratedWithDummyAttribute).GetRuntimeProperty("PublicProperty"); - - // Act - Action act = () => - propertyInfo.Should().BeDecoratedWith("because we want to test the error message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected property String " + - "FluentAssertions*ClassWithPropertiesThatAreNotDecoratedWithDummyAttribute.PublicProperty to be decorated with " + - "FluentAssertions*DummyPropertyAttribute because we want to test the error message, but that attribute was not found."); - } - - [Fact] - public void When_asserting_a_property_is_decorated_with_an_attribute_matching_a_predicate_but_it_is_not_it_throw_with_useful_message() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithPropertiesThatAreNotDecoratedWithDummyAttribute).GetRuntimeProperty("PublicProperty"); - - // Act - Action act = () => - propertyInfo.Should().BeDecoratedWith(d => d.Value == "NotARealValue", "because we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected property String FluentAssertions*ClassWithPropertiesThatAreNotDecoratedWithDummyAttribute.PublicProperty to be decorated with " + - "FluentAssertions*DummyPropertyAttribute because we want to test the error message," + - " but that attribute was not found."); - } - - [Fact] - public void When_asserting_a_property_is_decorated_with_attribute_matching_a_predicate_and_it_is_it_succeeds() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithAllPropertiesDecoratedWithDummyAttribute).GetRuntimeProperty("PublicProperty"); - - // Act - Action act = () => - propertyInfo.Should().BeDecoratedWith(d => d.Value == "Value"); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_subject_is_null_be_decorated_withOfT_should_fail() - { - // Arrange - PropertyInfo propertyInfo = null; - - // Act - Action act = () => - propertyInfo.Should().BeDecoratedWith("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected property to be decorated with *.DummyPropertyAttribute *failure message*, but propertyInfo is ."); - } - - [Fact] - public void When_asserting_property_is_decorated_with_null_it_should_throw() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithAllPropertiesDecoratedWithDummyAttribute).GetRuntimeProperty("PublicProperty"); - - // Act - Action act = () => - propertyInfo.Should().BeDecoratedWith((Expression>)null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("isMatchingAttributePredicate"); - } - - #endregion - - #region NotBeDecoratedWithOfT - - [Fact] - public void When_subject_is_null_not_be_decorated_withOfT_should_fail() - { - // Arrange - PropertyInfo propertyInfo = null; - - // Act - Action act = () => - propertyInfo.Should().NotBeDecoratedWith("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected property to not be decorated with *.DummyPropertyAttribute *failure message*, but propertyInfo is ."); - } - - [Fact] - public void When_asserting_property_is_not_decorated_with_null_it_should_throw() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("StringProperty"); - - // Act - Action act = () => - propertyInfo.Should().NotBeDecoratedWith((Expression>)null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("isMatchingAttributePredicate"); - } - - #endregion - - #region BeWritable - - [Fact] - public void When_asserting_a_readonly_property_is_writable_it_fails_with_useful_message() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("ReadOnlyProperty"); - - // Act - Action action = () => propertyInfo.Should().BeWritable("we want to test the error {0}", "message"); - - // Assert - action - .Should().Throw() - .WithMessage("Expected propertyInfo ReadOnlyProperty to have a setter because we want to test the error message."); - } - - [Fact] - public void When_asserting_a_readwrite_property_is_writable_it_succeeds() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("ReadWriteProperty"); - - // Act - Action action = () => propertyInfo.Should().BeWritable("that's required"); - - // Assert - action.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_writeonly_property_is_writable_it_succeeds() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("WriteOnlyProperty"); - - // Act - Action action = () => propertyInfo.Should().BeWritable("that's required"); - - // Assert - action.Should().NotThrow(); - } - - [Fact] - public void When_subject_is_null_be_writable_should_fail() - { - // Arrange - PropertyInfo propertyInfo = null; - - // Act - Action act = () => - propertyInfo.Should().BeWritable("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected property to have a setter *failure message*, but propertyInfo is ."); - } - - #endregion - - #region BeReadable - - [Fact] - public void When_asserting_a_readonly_property_is_readable_it_succeeds() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("ReadOnlyProperty"); - - // Act - Action action = () => propertyInfo.Should().BeReadable("that's required"); - - // Assert - action.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_readwrite_property_is_readable_it_succeeds() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithReadOnlyProperties).GetRuntimeProperty("ReadWriteProperty"); - - // Act - Action action = () => propertyInfo.Should().BeReadable("that's required"); - - // Assert - action.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_writeonly_property_is_readable_it_fails_with_useful_message() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("WriteOnlyProperty"); - - // Act - Action action = () => propertyInfo.Should().BeReadable("we want to test the error {0}", "message"); - - // Assert - action - .Should().Throw() - .WithMessage("Expected property WriteOnlyProperty to have a getter because we want to test the error message, but it does not."); - } - - [Fact] - public void When_subject_is_null_be_readable_should_fail() - { - // Arrange - PropertyInfo propertyInfo = null; - - // Act - Action act = () => - propertyInfo.Should().BeReadable("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected property to have a getter *failure message*, but propertyInfo is ."); - } - - #endregion - - #region NotBeWritable - - [Fact] - public void When_asserting_a_readonly_property_is_not_writable_it_succeeds() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithReadOnlyProperties).GetRuntimeProperty("ReadOnlyProperty"); - - // Act - Action action = () => propertyInfo.Should().NotBeWritable("that's required"); - - // Assert - action.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_readwrite_property_is_not_writable_it_fails_with_useful_message() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithReadOnlyProperties).GetRuntimeProperty("ReadWriteProperty"); - - // Act - Action action = () => propertyInfo.Should().NotBeWritable("we want to test the error {0}", "message"); - - // Assert - action - .Should().Throw() - .WithMessage("Expected propertyInfo ReadWriteProperty not to have a setter because we want to test the error message."); - } - - [Fact] - public void When_asserting_a_writeonly_property_is_not_writable_it_fails_with_useful_message() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("WriteOnlyProperty"); - - // Act - Action action = () => propertyInfo.Should().NotBeWritable("we want to test the error {0}", "message"); - - // Assert - action - .Should().Throw() - .WithMessage("Expected propertyInfo WriteOnlyProperty not to have a setter because we want to test the error message."); - } - - [Fact] - public void When_subject_is_null_not_be_writable_should_fail() - { - // Arrange - PropertyInfo propertyInfo = null; - - // Act - Action act = () => - propertyInfo.Should().NotBeWritable("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected property not to have a setter *failure message*, but propertyInfo is ."); - } - - #endregion - - #region NotBeReadable - - [Fact] - public void When_asserting_a_readonly_property_is_not_readable_it_fails_with_useful_message() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithReadOnlyProperties).GetRuntimeProperty("ReadOnlyProperty"); - - // Act - Action action = () => propertyInfo.Should().NotBeReadable("we want to test the error {0}", "message"); - - // Assert - action - .Should().Throw() - .WithMessage("Expected propertyInfo ReadOnlyProperty not to have a getter because we want to test the error message."); - } - - [Fact] - public void When_asserting_a_readwrite_property_is_not_readable_it_fails_with_useful_message() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithReadOnlyProperties).GetRuntimeProperty("ReadWriteProperty"); - - // Act - Action action = () => propertyInfo.Should().NotBeReadable("we want to test the error {0}", "message"); - - // Assert - action - .Should().Throw() - .WithMessage("Expected propertyInfo ReadWriteProperty not to have a getter because we want to test the error message."); - } - - [Fact] - public void When_asserting_a_writeonly_property_is_not_readable_it_succeeds() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("WriteOnlyProperty"); - - // Act - Action action = () => propertyInfo.Should().NotBeReadable("that's required"); - - // Assert - action.Should().NotThrow(); - } - - [Fact] - public void When_subject_is_null_not_be_readable_should_fail() - { - // Arrange - PropertyInfo propertyInfo = null; - - // Act - Action act = () => - propertyInfo.Should().NotBeReadable("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected property not to have a getter *failure message*, but propertyInfo is ."); - } - - #endregion - - #region BeReadableAccessModifier - - [Fact] - public void When_asserting_a_public_read_private_write_property_is_public_readable_it_succeeds() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("ReadPrivateWriteProperty"); - - // Act - Action action = () => propertyInfo.Should().BeReadable(CSharpAccessModifier.Public, "that's required"); - - // Assert - action.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_private_read_public_write_property_is_public_readable_it_fails_with_useful_message() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("WritePrivateReadProperty"); - - // Act - Action action = () => propertyInfo.Should().BeReadable(CSharpAccessModifier.Public, "we want to test the error {0}", "message"); - - // Assert - action.Should().Throw() - .WithMessage("Expected method get_WritePrivateReadProperty to be Public because we want to test the error message, but it is Private."); - } - - [Fact] - public void When_subject_is_null_be_readable_with_accessmodifier_should_fail() - { - // Arrange - PropertyInfo propertyInfo = null; - - // Act - Action act = () => - propertyInfo.Should().BeReadable(CSharpAccessModifier.Public, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected property to be Public *failure message*, but propertyInfo is ."); - } - - [Fact] - public void When_asserting_is_readable_with_an_invalid_enum_value_it_should_throw() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("StringProperty"); - - // Act - Action act = () => - propertyInfo.Should().BeReadable((CSharpAccessModifier)int.MaxValue); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("accessModifier"); - } - - #endregion - - #region BeWritableAccessModifier - - [Fact] - public void When_asserting_a_public_write_private_read_property_is_public_writable_it_succeeds() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("WritePrivateReadProperty"); - - // Act - Action action = () => propertyInfo.Should().BeWritable(CSharpAccessModifier.Public, "that's required"); - - // Assert - action.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_private_write_public_read_property_is_public_writable_it_fails_with_useful_message() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("ReadPrivateWriteProperty"); - - // Act - Action action = () => propertyInfo.Should().BeWritable(CSharpAccessModifier.Public, "we want to test the error {0}", "message"); - - // Assert - action.Should().Throw() - .WithMessage("Expected method set_ReadPrivateWriteProperty to be Public because we want to test the error message, but it is Private."); - } - - [Fact] - public void When_subject_is_null_be_writable_with_accessmodifier_should_fail() - { - // Arrange - PropertyInfo propertyInfo = null; - - // Act - Action act = () => - propertyInfo.Should().BeWritable(CSharpAccessModifier.Public, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected property to be Public *failure message*, but propertyInfo is ."); - } - - [Fact] - public void When_asserting_is_writable_with_an_invalid_enum_value_it_should_throw() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("StringProperty"); - - // Act - Action act = () => - propertyInfo.Should().BeWritable((CSharpAccessModifier)int.MaxValue); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("accessModifier"); - } - - #endregion - - #region Return - - [Fact] - public void When_asserting_a_String_property_returns_a_String_it_succeeds() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("StringProperty"); - - // Act - Action action = () => propertyInfo.Should().Return(typeof(string)); - - // Assert - action.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_String_property_returns_an_Int32_it_throw_with_a_useful_message() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("StringProperty"); - - // Act - Action action = () => propertyInfo.Should().Return(typeof(int), "we want to test the error {0}", "message"); - - // Assert - action.Should().Throw() - .WithMessage("Expected Type of property StringProperty to be System.Int32 because we want to test the error " + - "message, but it is System.String."); - } - - [Fact] - public void When_subject_is_null_return_should_fail() - { - // Arrange - PropertyInfo propertyInfo = null; - - // Act - Action act = () => - propertyInfo.Should().Return(typeof(int), "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type of property to be *.Int32 *failure message*, but propertyInfo is ."); - } - - [Fact] - public void When_asserting_property_type_is_null_it_should_throw() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("StringProperty"); - - // Act - Action act = () => - propertyInfo.Should().Return(null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("propertyType"); - } - - #endregion - - #region ReturnOfT - - [Fact] - public void When_asserting_a_String_property_returnsOfT_a_String_it_succeeds() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("StringProperty"); - - // Act - Action action = () => propertyInfo.Should().Return(); - - // Assert - action.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_String_property_returnsOfT_an_Int32_it_throw_with_a_useful_message() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("StringProperty"); - - // Act - Action action = () => propertyInfo.Should().Return("we want to test the error {0}", "message"); - - // Assert - action.Should().Throw() - .WithMessage("Expected Type of property StringProperty to be System.Int32 because we want to test the error " + - "message, but it is System.String."); - } - - #endregion - - #region NotReturn - - [Fact] - public void When_asserting_a_String_property_does_not_returns_an_Int32_it_succeeds() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("StringProperty"); - - // Act - Action action = () => propertyInfo.Should().NotReturn(typeof(int)); - - // Assert - action.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_String_property_does_not_return_a_String_it_throw_with_a_useful_message() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("StringProperty"); - - // Act - Action action = () => propertyInfo.Should().NotReturn(typeof(string), "we want to test the error {0}", "message"); - - // Assert - action.Should().Throw() - .WithMessage("Expected Type of property StringProperty not to be*String*because we want to test the error " + - "message, but it is."); - } - - [Fact] - public void When_subject_is_null_not_return_should_fail() - { - // Arrange - PropertyInfo propertyInfo = null; - - // Act - Action act = () => - propertyInfo.Should().NotReturn(typeof(int), "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type of property not to be *.Int32 *failure message*, but propertyInfo is ."); - } - - [Fact] - public void When_asserting_property_type_is_not_null_it_should_throw() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("StringProperty"); - - // Act - Action act = () => - propertyInfo.Should().NotReturn(null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("propertyType"); - } - - #endregion - - #region NotReturnOfT - - [Fact] - public void When_asserting_a_String_property_does_not_returnOfT_an_Int32_it_succeeds() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("StringProperty"); - - // Act - Action action = () => propertyInfo.Should().NotReturn(); - - // Assert - action.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_String_property_does_not_returnsOfT_a_String_it_throw_with_a_useful_message() - { - // Arrange - PropertyInfo propertyInfo = typeof(ClassWithProperties).GetRuntimeProperty("StringProperty"); - - // Act - Action action = () => propertyInfo.Should().NotReturn("we want to test the error {0}", "message"); - - // Assert - action.Should().Throw() - .WithMessage("Expected Type of property StringProperty not to be*String*because we want to test the error " + - "message, but it is."); - } - - #endregion - #region Internal classes used in unit tests private class ClassWithProperties diff --git a/Tests/FluentAssertions.Specs/Types/PropertyInfoSelectorAssertionSpecs.cs b/Tests/FluentAssertions.Specs/Types/PropertyInfoSelectorAssertionSpecs.cs index baef7fa9d3..5328a9f8e8 100644 --- a/Tests/FluentAssertions.Specs/Types/PropertyInfoSelectorAssertionSpecs.cs +++ b/Tests/FluentAssertions.Specs/Types/PropertyInfoSelectorAssertionSpecs.cs @@ -7,293 +7,287 @@ namespace FluentAssertions.Specs.Types { public class PropertyInfoSelectorAssertionSpecs { - #region BeVirtual - - [Fact] - public void When_asserting_properties_are_virtual_and_they_are_it_should_succeed() - { - // Arrange - var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithAllPropertiesVirtual)); - - // Act - Action act = () => - propertyInfoSelector.Should().BeVirtual(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_properties_are_virtual_but_non_virtual_properties_are_found_it_should_throw() - { - // Arrange - var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithNonVirtualPublicProperties)); - - // Act - Action act = () => - propertyInfoSelector.Should().BeVirtual(); - - // Assert - act.Should().Throw(); - } - - [Fact] - public void - When_asserting_properties_are_virtual_but_non_virtual_properties_are_found_it_should_throw_with_descriptive_message() - { - // Arrange - var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithNonVirtualPublicProperties)); - - // Act - Action act = () => - propertyInfoSelector.Should().BeVirtual("we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected all selected properties" + - " to be virtual because we want to test the error message," + - " but the following properties are not virtual:*" + - "String FluentAssertions*ClassWithNonVirtualPublicProperties.PublicNonVirtualProperty*" + - "String FluentAssertions*ClassWithNonVirtualPublicProperties.InternalNonVirtualProperty*" + - "String FluentAssertions*ClassWithNonVirtualPublicProperties.ProtectedNonVirtualProperty"); - } - - #endregion - - #region NotBeVirtual - - [Fact] - public void When_asserting_properties_are_not_virtual_and_they_are_not_it_should_succeed() + public class BeVirtual { - // Arrange - var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithNonVirtualPublicProperties)); - - // Act - Action act = () => - propertyInfoSelector.Should().NotBeVirtual(); - - // Assert - act.Should().NotThrow(); + [Fact] + public void When_asserting_properties_are_virtual_and_they_are_it_should_succeed() + { + // Arrange + var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithAllPropertiesVirtual)); + + // Act + Action act = () => + propertyInfoSelector.Should().BeVirtual(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_properties_are_virtual_but_non_virtual_properties_are_found_it_should_throw() + { + // Arrange + var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithNonVirtualPublicProperties)); + + // Act + Action act = () => + propertyInfoSelector.Should().BeVirtual(); + + // Assert + act.Should().Throw(); + } + + [Fact] + public void + When_asserting_properties_are_virtual_but_non_virtual_properties_are_found_it_should_throw_with_descriptive_message() + { + // Arrange + var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithNonVirtualPublicProperties)); + + // Act + Action act = () => + propertyInfoSelector.Should().BeVirtual("we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected all selected properties" + + " to be virtual because we want to test the error message," + + " but the following properties are not virtual:*" + + "String FluentAssertions*ClassWithNonVirtualPublicProperties.PublicNonVirtualProperty*" + + "String FluentAssertions*ClassWithNonVirtualPublicProperties.InternalNonVirtualProperty*" + + "String FluentAssertions*ClassWithNonVirtualPublicProperties.ProtectedNonVirtualProperty"); + } } - [Fact] - public void When_asserting_properties_are_not_virtual_but_virtual_properties_are_found_it_should_throw() + public class NotBeVirtual { - // Arrange - var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithAllPropertiesVirtual)); - - // Act - Action act = () => - propertyInfoSelector.Should().NotBeVirtual(); - - // Assert - act.Should().Throw(); + [Fact] + public void When_asserting_properties_are_not_virtual_and_they_are_not_it_should_succeed() + { + // Arrange + var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithNonVirtualPublicProperties)); + + // Act + Action act = () => + propertyInfoSelector.Should().NotBeVirtual(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_properties_are_not_virtual_but_virtual_properties_are_found_it_should_throw() + { + // Arrange + var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithAllPropertiesVirtual)); + + // Act + Action act = () => + propertyInfoSelector.Should().NotBeVirtual(); + + // Assert + act.Should().Throw(); + } + + [Fact] + public void + When_asserting_properties_are_not_virtual_but_virtual_properties_are_found_it_should_throw_with_descriptive_message() + { + // Arrange + var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithAllPropertiesVirtual)); + + // Act + Action act = () => + propertyInfoSelector.Should().NotBeVirtual("we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected all selected properties" + + " not to be virtual because we want to test the error message," + + " but the following properties are virtual*" + + "*ClassWithAllPropertiesVirtual.PublicVirtualProperty" + + "*ClassWithAllPropertiesVirtual.InternalVirtualProperty" + + "*ClassWithAllPropertiesVirtual.ProtectedVirtualProperty"); + } } - [Fact] - public void - When_asserting_properties_are_not_virtual_but_virtual_properties_are_found_it_should_throw_with_descriptive_message() + public class BeDecoratedWith { - // Arrange - var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithAllPropertiesVirtual)); - - // Act - Action act = () => - propertyInfoSelector.Should().NotBeVirtual("we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected all selected properties" + - " not to be virtual because we want to test the error message," + - " but the following properties are virtual*" + - "*ClassWithAllPropertiesVirtual.PublicVirtualProperty" + - "*ClassWithAllPropertiesVirtual.InternalVirtualProperty" + - "*ClassWithAllPropertiesVirtual.ProtectedVirtualProperty"); + [Fact] + public void When_asserting_properties_are_decorated_with_attribute_and_they_are_it_should_succeed() + { + // Arrange + var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithAllPropertiesDecoratedWithDummyAttribute)); + + // Act + Action act = () => + propertyInfoSelector.Should().BeDecoratedWith(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_properties_are_decorated_with_attribute_and_they_are_not_it_should_throw() + { + // Arrange + var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithPropertiesThatAreNotDecoratedWithDummyAttribute)) + .ThatArePublicOrInternal; + + // Act + Action act = () => + propertyInfoSelector.Should().BeDecoratedWith(); + + // Assert + act.Should().Throw(); + } + + [Fact] + public void + When_asserting_properties_are_decorated_with_attribute_and_they_are_not_it_should_throw_with_descriptive_message() + { + // Arrange + var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithPropertiesThatAreNotDecoratedWithDummyAttribute)); + + // Act + Action act = () => + propertyInfoSelector.Should() + .BeDecoratedWith("because we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected all selected properties to be decorated with" + + " FluentAssertions*DummyPropertyAttribute because we want to test the error message," + + " but the following properties are not:*" + + "String FluentAssertions*ClassWithPropertiesThatAreNotDecoratedWithDummyAttribute.PublicProperty*" + + "String FluentAssertions*ClassWithPropertiesThatAreNotDecoratedWithDummyAttribute.InternalProperty*" + + "String FluentAssertions*ClassWithPropertiesThatAreNotDecoratedWithDummyAttribute.ProtectedProperty"); + } } - #endregion - - #region BeDecoratedWith - - [Fact] - public void When_asserting_properties_are_decorated_with_attribute_and_they_are_it_should_succeed() + public class NotBeDecoratedWith { - // Arrange - var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithAllPropertiesDecoratedWithDummyAttribute)); - - // Act - Action act = () => - propertyInfoSelector.Should().BeDecoratedWith(); - - // Assert - act.Should().NotThrow(); + [Fact] + public void When_asserting_properties_are_not_decorated_with_attribute_and_they_are_not_it_should_succeed() + { + // Arrange + var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithPropertiesThatAreNotDecoratedWithDummyAttribute)); + + // Act + Action act = () => + propertyInfoSelector.Should().NotBeDecoratedWith(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_properties_are_not_decorated_with_attribute_and_they_are_it_should_throw() + { + // Arrange + var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithAllPropertiesDecoratedWithDummyAttribute)) + .ThatArePublicOrInternal; + + // Act + Action act = () => + propertyInfoSelector.Should().NotBeDecoratedWith(); + + // Assert + act.Should().Throw(); + } + + [Fact] + public void + When_asserting_properties_are_not_decorated_with_attribute_and_they_are_it_should_throw_with_descriptive_message() + { + // Arrange + var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithAllPropertiesDecoratedWithDummyAttribute)); + + // Act + Action act = () => + propertyInfoSelector.Should() + .NotBeDecoratedWith("because we want to test the error {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected all selected properties not to be decorated*" + + "DummyPropertyAttribute*" + + "because we want to test the error message*" + + "ClassWithAllPropertiesDecoratedWithDummyAttribute.PublicProperty*" + + "ClassWithAllPropertiesDecoratedWithDummyAttribute.InternalProperty*" + + "ClassWithAllPropertiesDecoratedWithDummyAttribute.ProtectedProperty*"); + } } - [Fact] - public void When_asserting_properties_are_decorated_with_attribute_and_they_are_not_it_should_throw() + public class BeWritable { - // Arrange - var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithPropertiesThatAreNotDecoratedWithDummyAttribute)) - .ThatArePublicOrInternal; - - // Act - Action act = () => - propertyInfoSelector.Should().BeDecoratedWith(); - - // Assert - act.Should().Throw(); + [Fact] + public void When_a_read_only_property_is_expected_to_be_writable_it_should_throw_with_descriptive_message() + { + // Arrange + var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithReadOnlyProperties)); + + // Act + Action action = () => propertyInfoSelector.Should().BeWritable("because we want to test the error {0}", "message"); + + // Assert + action + .Should().Throw() + .WithMessage( + "Expected all selected properties to have a setter because we want to test the error message, " + + "but the following properties do not:*" + + "String FluentAssertions*ClassWithReadOnlyProperties.ReadOnlyProperty*" + + "String FluentAssertions*ClassWithReadOnlyProperties.ReadOnlyProperty2"); + } + + [Fact] + public void When_writable_properties_are_expected_to_be_writable_it_should_not_throw() + { + // Arrange + var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithOnlyWritableProperties)); + + // Act + Action action = () => propertyInfoSelector.Should().BeWritable(); + + // Assert + action.Should().NotThrow(); + } } - [Fact] - public void - When_asserting_properties_are_decorated_with_attribute_and_they_are_not_it_should_throw_with_descriptive_message() + public class NotBeWritable { - // Arrange - var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithPropertiesThatAreNotDecoratedWithDummyAttribute)); - - // Act - Action act = () => - propertyInfoSelector.Should() - .BeDecoratedWith("because we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected all selected properties to be decorated with" + - " FluentAssertions*DummyPropertyAttribute because we want to test the error message," + - " but the following properties are not:*" + - "String FluentAssertions*ClassWithPropertiesThatAreNotDecoratedWithDummyAttribute.PublicProperty*" + - "String FluentAssertions*ClassWithPropertiesThatAreNotDecoratedWithDummyAttribute.InternalProperty*" + - "String FluentAssertions*ClassWithPropertiesThatAreNotDecoratedWithDummyAttribute.ProtectedProperty"); + [Fact] + public void When_a_writable_property_is_expected_to_be_read_only_it_should_throw_with_descriptive_message() + { + // Arrange + var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithWritableProperties)); + + // Act + Action action = () => propertyInfoSelector.Should().NotBeWritable("because we want to test the error {0}", "message"); + + // Assert + action + .Should().Throw() + .WithMessage( + "Expected selected properties to not have a setter because we want to test the error message, " + + "but the following properties do:*" + + "String FluentAssertions*ClassWithWritableProperties.ReadWriteProperty*" + + "String FluentAssertions*ClassWithWritableProperties.ReadWriteProperty2"); + } + + [Fact] + public void When_read_only_properties_are_expected_to_not_be_writable_it_should_not_throw() + { + // Arrange + var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithOnlyReadOnlyProperties)); + + // Act + Action action = () => propertyInfoSelector.Should().NotBeWritable(); + + // Assert + action.Should().NotThrow(); + } } - - #endregion - - #region NotBeDecoratedWith - - [Fact] - public void When_asserting_properties_are_not_decorated_with_attribute_and_they_are_not_it_should_succeed() - { - // Arrange - var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithPropertiesThatAreNotDecoratedWithDummyAttribute)); - - // Act - Action act = () => - propertyInfoSelector.Should().NotBeDecoratedWith(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_properties_are_not_decorated_with_attribute_and_they_are_it_should_throw() - { - // Arrange - var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithAllPropertiesDecoratedWithDummyAttribute)) - .ThatArePublicOrInternal; - - // Act - Action act = () => - propertyInfoSelector.Should().NotBeDecoratedWith(); - - // Assert - act.Should().Throw(); - } - - [Fact] - public void - When_asserting_properties_are_not_decorated_with_attribute_and_they_are_it_should_throw_with_descriptive_message() - { - // Arrange - var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithAllPropertiesDecoratedWithDummyAttribute)); - - // Act - Action act = () => - propertyInfoSelector.Should() - .NotBeDecoratedWith("because we want to test the error {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected all selected properties not to be decorated*" + - "DummyPropertyAttribute*" + - "because we want to test the error message*" + - "ClassWithAllPropertiesDecoratedWithDummyAttribute.PublicProperty*" + - "ClassWithAllPropertiesDecoratedWithDummyAttribute.InternalProperty*" + - "ClassWithAllPropertiesDecoratedWithDummyAttribute.ProtectedProperty*"); - } - - #endregion - - #region BeWritable - - [Fact] - public void When_a_read_only_property_is_expected_to_be_writable_it_should_throw_with_descriptive_message() - { - // Arrange - var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithReadOnlyProperties)); - - // Act - Action action = () => propertyInfoSelector.Should().BeWritable("because we want to test the error {0}", "message"); - - // Assert - action - .Should().Throw() - .WithMessage( - "Expected all selected properties to have a setter because we want to test the error message, " + - "but the following properties do not:*" + - "String FluentAssertions*ClassWithReadOnlyProperties.ReadOnlyProperty*" + - "String FluentAssertions*ClassWithReadOnlyProperties.ReadOnlyProperty2"); - } - - [Fact] - public void When_writable_properties_are_expected_to_be_writable_it_should_not_throw() - { - // Arrange - var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithOnlyWritableProperties)); - - // Act - Action action = () => propertyInfoSelector.Should().BeWritable(); - - // Assert - action.Should().NotThrow(); - } - - #endregion - - #region NotBeWritable - - [Fact] - public void When_a_writable_property_is_expected_to_be_read_only_it_should_throw_with_descriptive_message() - { - // Arrange - var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithWritableProperties)); - - // Act - Action action = () => propertyInfoSelector.Should().NotBeWritable("because we want to test the error {0}", "message"); - - // Assert - action - .Should().Throw() - .WithMessage( - "Expected selected properties to not have a setter because we want to test the error message, " + - "but the following properties do:*" + - "String FluentAssertions*ClassWithWritableProperties.ReadWriteProperty*" + - "String FluentAssertions*ClassWithWritableProperties.ReadWriteProperty2"); - } - - [Fact] - public void When_read_only_properties_are_expected_to_not_be_writable_it_should_not_throw() - { - // Arrange - var propertyInfoSelector = new PropertyInfoSelector(typeof(ClassWithOnlyReadOnlyProperties)); - - // Act - Action action = () => propertyInfoSelector.Should().NotBeWritable(); - - // Assert - action.Should().NotThrow(); - } - - #endregion } #region Internal classes used in unit tests @@ -375,6 +369,5 @@ internal class ClassWithPropertiesThatAreNotDecoratedWithDummyAttribute protected string ProtectedProperty { get; set; } } - #endregion } diff --git a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.Be.cs b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.Be.cs index 29108f08ca..cc783493d7 100644 --- a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.Be.cs +++ b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.Be.cs @@ -11,215 +11,213 @@ namespace FluentAssertions.Specs.Types /// public partial class TypeAssertionSpecs { - #region Be - - [Fact] - public void When_type_is_equal_to_the_same_type_it_succeeds() - { - // Arrange - Type type = typeof(ClassWithAttribute); - Type sameType = typeof(ClassWithAttribute); - - // Act - Action act = () => - type.Should().Be(sameType); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_type_is_equal_to_another_type_it_fails() - { - // Arrange - Type type = typeof(ClassWithAttribute); - Type differentType = typeof(ClassWithoutAttribute); - - // Act - Action act = () => - type.Should().Be(differentType, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type to be *.ClassWithoutAttribute *failure message*, but found *.ClassWithAttribute."); - } - - [Fact] - public void When_asserting_equality_of_two_null_types_it_succeeds() - { - // Arrange - Type nullType = null; - Type someType = null; - - // Act - Action act = () => nullType.Should().Be(someType); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_equality_of_a_type_but_the_type_is_null_it_fails() + public class Be { - // Arrange - Type nullType = null; - Type someType = typeof(ClassWithAttribute); - - // Act - Action act = () => - nullType.Should().Be(someType, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type to be *.ClassWithAttribute *failure message*, but found ."); - } - - [Fact] - public void When_asserting_equality_of_a_type_with_null_it_fails() - { - // Arrange - Type someType = typeof(ClassWithAttribute); - Type nullType = null; - - // Act - Action act = () => - someType.Should().Be(nullType, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type to be *failure message*, but found *.ClassWithAttribute."); - } - - [Fact] - public void When_type_is_equal_to_same_type_from_different_assembly_it_fails_with_assembly_qualified_name() - { - // Arrange + [Fact] + public void When_type_is_equal_to_the_same_type_it_succeeds() + { + // Arrange + Type type = typeof(ClassWithAttribute); + Type sameType = typeof(ClassWithAttribute); + + // Act + Action act = () => + type.Should().Be(sameType); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_type_is_equal_to_another_type_it_fails() + { + // Arrange + Type type = typeof(ClassWithAttribute); + Type differentType = typeof(ClassWithoutAttribute); + + // Act + Action act = () => + type.Should().Be(differentType, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected type to be *.ClassWithoutAttribute *failure message*, but found *.ClassWithAttribute."); + } + + [Fact] + public void When_asserting_equality_of_two_null_types_it_succeeds() + { + // Arrange + Type nullType = null; + Type someType = null; + + // Act + Action act = () => nullType.Should().Be(someType); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_equality_of_a_type_but_the_type_is_null_it_fails() + { + // Arrange + Type nullType = null; + Type someType = typeof(ClassWithAttribute); + + // Act + Action act = () => + nullType.Should().Be(someType, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected type to be *.ClassWithAttribute *failure message*, but found ."); + } + + [Fact] + public void When_asserting_equality_of_a_type_with_null_it_fails() + { + // Arrange + Type someType = typeof(ClassWithAttribute); + Type nullType = null; + + // Act + Action act = () => + someType.Should().Be(nullType, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected type to be *failure message*, but found *.ClassWithAttribute."); + } + + [Fact] + public void When_type_is_equal_to_same_type_from_different_assembly_it_fails_with_assembly_qualified_name() + { + // Arrange #pragma warning disable 436 // disable the warning on conflicting types, as this is the intention for the spec - Type typeFromThisAssembly = typeof(AssemblyB.ClassC); + Type typeFromThisAssembly = typeof(AssemblyB.ClassC); - Type typeFromOtherAssembly = - new AssemblyA.ClassA().ReturnClassC().GetType(); + Type typeFromOtherAssembly = + new AssemblyA.ClassA().ReturnClassC().GetType(); #pragma warning restore 436 - // Act - Action act = () => - typeFromThisAssembly.Should().Be(typeFromOtherAssembly, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type to be [AssemblyB.ClassC, AssemblyB*] *failure message*, but found [AssemblyB.ClassC, FluentAssertions.Specs*]."); - } - - [Fact] - public void When_type_is_equal_to_the_same_type_using_generics_it_succeeds() - { - // Arrange - Type type = typeof(ClassWithAttribute); - - // Act - Action act = () => - type.Should().Be(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_type_is_equal_to_another_type_using_generics_it_fails() - { - // Arrange - Type type = typeof(ClassWithAttribute); - - // Act - Action act = () => - type.Should().Be("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type to be *.ClassWithoutAttribute *failure message*, but found *.ClassWithAttribute."); - } - - #endregion - - #region NotBe - - [Fact] - public void When_type_is_not_equal_to_the_another_type_it_succeeds() - { - // Arrange - Type type = typeof(ClassWithAttribute); - Type otherType = typeof(ClassWithoutAttribute); - - // Act - Action act = () => - type.Should().NotBe(otherType); - - // Assert - act.Should().NotThrow(); + // Act + Action act = () => + typeFromThisAssembly.Should().Be(typeFromOtherAssembly, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected type to be [AssemblyB.ClassC, AssemblyB*] *failure message*, but found [AssemblyB.ClassC, FluentAssertions.Specs*]."); + } + + [Fact] + public void When_type_is_equal_to_the_same_type_using_generics_it_succeeds() + { + // Arrange + Type type = typeof(ClassWithAttribute); + + // Act + Action act = () => + type.Should().Be(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_type_is_equal_to_another_type_using_generics_it_fails() + { + // Arrange + Type type = typeof(ClassWithAttribute); + + // Act + Action act = () => + type.Should().Be("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected type to be *.ClassWithoutAttribute *failure message*, but found *.ClassWithAttribute."); + } } - [Fact] - public void When_type_is_not_equal_to_the_same_type_it_fails() + public class NotBe { - // Arrange - Type type = typeof(ClassWithAttribute); - Type sameType = typeof(ClassWithAttribute); - - // Act - Action act = () => - type.Should().NotBe(sameType, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type not to be [*.ClassWithAttribute*] *failure message*, but it is."); + [Fact] + public void When_type_is_not_equal_to_the_another_type_it_succeeds() + { + // Arrange + Type type = typeof(ClassWithAttribute); + Type otherType = typeof(ClassWithoutAttribute); + + // Act + Action act = () => + type.Should().NotBe(otherType); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_type_is_not_equal_to_the_same_type_it_fails() + { + // Arrange + Type type = typeof(ClassWithAttribute); + Type sameType = typeof(ClassWithAttribute); + + // Act + Action act = () => + type.Should().NotBe(sameType, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected type not to be [*.ClassWithAttribute*] *failure message*, but it is."); + } + + [Fact] + public void When_type_is_not_equal_to_the_same_null_type_it_fails() + { + // Arrange + Type type = null; + Type sameType = null; + + // Act + Action act = () => + type.Should().NotBe(sameType); + + // Assert + act.Should().Throw(); + } + + [Fact] + public void When_type_is_not_equal_to_another_type_using_generics_it_succeeds() + { + // Arrange + Type type = typeof(ClassWithAttribute); + + // Act + Action act = () => + type.Should().NotBe(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_type_is_not_equal_to_the_same_type_using_generics_it_fails() + { + // Arrange + Type type = typeof(ClassWithAttribute); + + // Act + Action act = () => + type.Should().NotBe("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected type not to be [*.ClassWithAttribute*] *failure message*, but it is."); + } } - - [Fact] - public void When_type_is_not_equal_to_the_same_null_type_it_fails() - { - // Arrange - Type type = null; - Type sameType = null; - - // Act - Action act = () => - type.Should().NotBe(sameType); - - // Assert - act.Should().Throw(); - } - - [Fact] - public void When_type_is_not_equal_to_another_type_using_generics_it_succeeds() - { - // Arrange - Type type = typeof(ClassWithAttribute); - - // Act - Action act = () => - type.Should().NotBe(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_type_is_not_equal_to_the_same_type_using_generics_it_fails() - { - // Arrange - Type type = typeof(ClassWithAttribute); - - // Act - Action act = () => - type.Should().NotBe("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type not to be [*.ClassWithAttribute*] *failure message*, but it is."); - } - - #endregion } } diff --git a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.BeAbstract.cs b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.BeAbstract.cs index d6f7721738..a8abc4e428 100644 --- a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.BeAbstract.cs +++ b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.BeAbstract.cs @@ -9,143 +9,141 @@ namespace FluentAssertions.Specs.Types /// public partial class TypeAssertionSpecs { - #region BeAbstract - - [Fact] - public void When_type_is_abstract_it_succeeds() - { - // Arrange / Act / Assert - typeof(Abstract).Should().BeAbstract(); - } - - [Theory] - [InlineData(typeof(ClassWithoutMembers), "Expected type *.ClassWithoutMembers to be abstract.")] - [InlineData(typeof(Sealed), "Expected type *.Sealed to be abstract.")] - [InlineData(typeof(Static), "Expected type *.Static to be abstract.")] - public void When_type_is_not_abstract_it_fails(Type type, string exceptionMessage) + public class BeAbstract { - // Act - Action act = () => type.Should().BeAbstract(); - - // Assert - act.Should().Throw() - .WithMessage(exceptionMessage); + [Fact] + public void When_type_is_abstract_it_succeeds() + { + // Arrange / Act / Assert + typeof(Abstract).Should().BeAbstract(); + } + + [Theory] + [InlineData(typeof(ClassWithoutMembers), "Expected type *.ClassWithoutMembers to be abstract.")] + [InlineData(typeof(Sealed), "Expected type *.Sealed to be abstract.")] + [InlineData(typeof(Static), "Expected type *.Static to be abstract.")] + public void When_type_is_not_abstract_it_fails(Type type, string exceptionMessage) + { + // Act + Action act = () => type.Should().BeAbstract(); + + // Assert + act.Should().Throw() + .WithMessage(exceptionMessage); + } + + [Fact] + public void When_type_is_not_abstract_it_fails_with_a_meaningful_message() + { + // Arrange + var type = typeof(ClassWithoutMembers); + + // Act + Action act = () => type.Should().BeAbstract("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected type *.ClassWithoutMembers to be abstract *failure message*."); + } + + [Theory] + [InlineData(typeof(IDummyInterface), "*.IDummyInterface must be a class.")] + [InlineData(typeof(Struct), "*.Struct must be a class.")] + [InlineData(typeof(ExampleDelegate), "*.ExampleDelegate must be a class.")] + public void When_type_is_not_valid_for_BeAbstract_it_throws_exception(Type type, string exceptionMessage) + { + // Act + Action act = () => type.Should().BeAbstract(); + + // Assert + act.Should().Throw() + .WithMessage(exceptionMessage); + } + + [Fact] + public void When_subject_is_null_be_abstract_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().BeAbstract("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected type to be abstract *failure message*, but type is ."); + } } - [Fact] - public void When_type_is_not_abstract_it_fails_with_a_meaningful_message() - { - // Arrange - var type = typeof(ClassWithoutMembers); - - // Act - Action act = () => type.Should().BeAbstract("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type *.ClassWithoutMembers to be abstract *failure message*."); - } - - [Theory] - [InlineData(typeof(IDummyInterface), "*.IDummyInterface must be a class.")] - [InlineData(typeof(Struct), "*.Struct must be a class.")] - [InlineData(typeof(ExampleDelegate), "*.ExampleDelegate must be a class.")] - public void When_type_is_not_valid_for_BeAbstract_it_throws_exception(Type type, string exceptionMessage) - { - // Act - Action act = () => type.Should().BeAbstract(); - - // Assert - act.Should().Throw() - .WithMessage(exceptionMessage); - } - - [Fact] - public void When_subject_is_null_be_abstract_should_fail() - { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().BeAbstract("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type to be abstract *failure message*, but type is ."); - } - - #endregion - - #region NotBeAbstract - - [Theory] - [InlineData(typeof(ClassWithoutMembers))] - [InlineData(typeof(Sealed))] - [InlineData(typeof(Static))] - public void When_type_is_not_abstract_it_succeeds(Type type) + public class NotBeAbstract { - // Arrange / Act / Assert - type.Should().NotBeAbstract(); + [Theory] + [InlineData(typeof(ClassWithoutMembers))] + [InlineData(typeof(Sealed))] + [InlineData(typeof(Static))] + public void When_type_is_not_abstract_it_succeeds(Type type) + { + // Arrange / Act / Assert + type.Should().NotBeAbstract(); + } + + [Fact] + public void When_type_is_abstract_it_fails() + { + // Arrange + var type = typeof(Abstract); + + // Act + Action act = () => type.Should().NotBeAbstract(); + + // Assert + act.Should().Throw() + .WithMessage("Expected type *.Abstract not to be abstract."); + } + + [Fact] + public void When_type_is_abstract_it_fails_with_a_meaningful_message() + { + // Arrange + var type = typeof(Abstract); + + // Act + Action act = () => type.Should().NotBeAbstract("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected type *.Abstract not to be abstract *failure message*."); + } + + [Theory] + [InlineData(typeof(IDummyInterface), "*.IDummyInterface must be a class.")] + [InlineData(typeof(Struct), "*.Struct must be a class.")] + [InlineData(typeof(ExampleDelegate), "*.ExampleDelegate must be a class.")] + public void When_type_is_not_valid_for_NotBeAbstract_it_throws_exception(Type type, string exceptionMessage) + { + // Act + Action act = () => type.Should().NotBeAbstract(); + + // Assert + act.Should().Throw() + .WithMessage(exceptionMessage); + } + + [Fact] + public void When_subject_is_null_not_be_abstract_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().NotBeAbstract("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected type not to be abstract *failure message*, but type is ."); + } } - - [Fact] - public void When_type_is_abstract_it_fails() - { - // Arrange - var type = typeof(Abstract); - - // Act - Action act = () => type.Should().NotBeAbstract(); - - // Assert - act.Should().Throw() - .WithMessage("Expected type *.Abstract not to be abstract."); - } - - [Fact] - public void When_type_is_abstract_it_fails_with_a_meaningful_message() - { - // Arrange - var type = typeof(Abstract); - - // Act - Action act = () => type.Should().NotBeAbstract("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type *.Abstract not to be abstract *failure message*."); - } - - [Theory] - [InlineData(typeof(IDummyInterface), "*.IDummyInterface must be a class.")] - [InlineData(typeof(Struct), "*.Struct must be a class.")] - [InlineData(typeof(ExampleDelegate), "*.ExampleDelegate must be a class.")] - public void When_type_is_not_valid_for_NotBeAbstract_it_throws_exception(Type type, string exceptionMessage) - { - // Act - Action act = () => type.Should().NotBeAbstract(); - - // Assert - act.Should().Throw() - .WithMessage(exceptionMessage); - } - - [Fact] - public void When_subject_is_null_not_be_abstract_should_fail() - { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().NotBeAbstract("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type not to be abstract *failure message*, but type is ."); - } - - #endregion } } diff --git a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.BeAssignableTo.cs b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.BeAssignableTo.cs index 4910686e22..8226eda165 100644 --- a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.BeAssignableTo.cs +++ b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.BeAssignableTo.cs @@ -10,332 +10,330 @@ namespace FluentAssertions.Specs.Types /// public partial class TypeAssertionSpecs { - #region BeAssignableTo - - [Fact] - public void When_its_own_type_it_succeeds() - { - // Arrange / Act / Assert - typeof(DummyImplementingClass).Should().BeAssignableTo(); - } - - [Fact] - public void When_its_base_type_it_succeeds() - { - // Arrange / Act / Assert - typeof(DummyImplementingClass).Should().BeAssignableTo(); - } - - [Fact] - public void When_implemented_interface_type_it_succeeds() - { - // Arrange / Act / Assert - typeof(DummyImplementingClass).Should().BeAssignableTo(); - } - - [Fact] - public void When_an_unrelated_type_it_fails() - { - // Arrange - Type someType = typeof(DummyImplementingClass); - Action act = () => someType.Should().BeAssignableTo("we want to test the failure {0}", "message"); - - // Act / Assert - act.Should().Throw() - .WithMessage( - "Expected someType *.DummyImplementingClass to be assignable to *.DateTime *failure message*" + - ", but it is not."); - } - - [Fact] - public void When_its_own_type_instance_it_succeeds() - { - // Arrange / Act / Assert - typeof(DummyImplementingClass).Should().BeAssignableTo(typeof(DummyImplementingClass)); - } - - [Fact] - public void When_its_base_type_instance_it_succeeds() - { - // Arrange / Act / Assert - typeof(DummyImplementingClass).Should().BeAssignableTo(typeof(DummyBaseClass)); - } - - [Fact] - public void When_an_implemented_interface_type_instance_it_succeeds() - { - // Arrange / Act / Assert - typeof(DummyImplementingClass).Should().BeAssignableTo(typeof(IDisposable)); - } - - [Fact] - public void When_an_unrelated_type_instance_it_fails() - { - // Arrange - Type someType = typeof(DummyImplementingClass); - - // Act - Action act = () => - someType.Should().BeAssignableTo(typeof(DateTime), "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("*.DummyImplementingClass to be assignable to *.DateTime *failure message*"); - } - - [Fact] - public void When_constructed_of_open_generic_it_succeeds() - { - // Arrange / Act / Assert - typeof(IDummyInterface).Should().BeAssignableTo(typeof(IDummyInterface<>)); - } - - [Fact] - public void When_implementation_of_open_generic_interface_it_succeeds() - { - // Arrange / Act / Assert - typeof(ClassWithGenericBaseType).Should().BeAssignableTo(typeof(IDummyInterface<>)); - } - - [Fact] - public void When_derived_of_open_generic_class_it_succeeds() - { - // Arrange / Act / Assert - typeof(ClassWithGenericBaseType).Should().BeAssignableTo(typeof(DummyBaseType<>)); - } - - [Fact] - public void When_unrelated_to_open_generic_interface_it_fails() - { - // Arrange - Type someType = typeof(IDummyInterface); - - // Act - Action act = () => - someType.Should().BeAssignableTo(typeof(IDummyInterface<>), "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected someType *.IDummyInterface to be assignable to *.IDummyInterface`1[T] *failure message*" + - ", but it is not."); - } - - [Fact] - public void When_unrelated_to_open_generic_type_it_fails() - { - // Arrange - Type someType = typeof(ClassWithAttribute); - Action act = () => - someType.Should().BeAssignableTo(typeof(DummyBaseType<>), "we want to test the failure {0}", "message"); - - // Act / Assert - act.Should().Throw() - .WithMessage("*.ClassWithAttribute to be assignable to *.DummyBaseType* *failure message*"); - } - - [Fact] - public void When_asserting_an_open_generic_class_is_assignable_to_itself_it_succeeds() - { - // Arrange / Act / Assert - typeof(DummyBaseType<>).Should().BeAssignableTo(typeof(DummyBaseType<>)); - } - - [Fact] - public void When_asserting_a_type_to_be_assignable_to_null_it_should_throw() - { - // Arrange - var type = typeof(DummyBaseType<>); - - // Act - Action act = () => - type.Should().BeAssignableTo(null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("type"); - } - - #endregion - - #region NotBeAssignableTo - - [Fact] - public void When_its_own_type_and_asserting_not_assignable_it_fails() - { - // Arrange - var type = typeof(DummyImplementingClass); - - // Act - Action act = () => - type.Should().NotBeAssignableTo("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type *.DummyImplementingClass to not be assignable to *.DummyImplementingClass *failure message*"); - } - - [Fact] - public void When_its_base_type_and_asserting_not_assignable_it_fails() - { - // Arrange - var type = typeof(DummyImplementingClass); - - // Act - Action act = () => - type.Should().NotBeAssignableTo("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type *.DummyImplementingClass to not be assignable to *.DummyBaseClass *failure message*" + - ", but it is."); - } - - [Fact] - public void When_implemented_interface_type_and_asserting_not_assignable_it_fails() - { - // Arrange - var type = typeof(DummyImplementingClass); - - // Act - Action act = () => - type.Should().NotBeAssignableTo("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type *.DummyImplementingClass to not be assignable to *.IDisposable *failure message*, but it is."); - } - - [Fact] - public void When_an_unrelated_type_and_asserting_not_assignable_it_succeeds() + public class BeAssignableTo { - // Arrange / Act / Assert - typeof(DummyImplementingClass).Should().NotBeAssignableTo(); + [Fact] + public void When_its_own_type_it_succeeds() + { + // Arrange / Act / Assert + typeof(DummyImplementingClass).Should().BeAssignableTo(); + } + + [Fact] + public void When_its_base_type_it_succeeds() + { + // Arrange / Act / Assert + typeof(DummyImplementingClass).Should().BeAssignableTo(); + } + + [Fact] + public void When_implemented_interface_type_it_succeeds() + { + // Arrange / Act / Assert + typeof(DummyImplementingClass).Should().BeAssignableTo(); + } + + [Fact] + public void When_an_unrelated_type_it_fails() + { + // Arrange + Type someType = typeof(DummyImplementingClass); + Action act = () => someType.Should().BeAssignableTo("we want to test the failure {0}", "message"); + + // Act / Assert + act.Should().Throw() + .WithMessage( + "Expected someType *.DummyImplementingClass to be assignable to *.DateTime *failure message*" + + ", but it is not."); + } + + [Fact] + public void When_its_own_type_instance_it_succeeds() + { + // Arrange / Act / Assert + typeof(DummyImplementingClass).Should().BeAssignableTo(typeof(DummyImplementingClass)); + } + + [Fact] + public void When_its_base_type_instance_it_succeeds() + { + // Arrange / Act / Assert + typeof(DummyImplementingClass).Should().BeAssignableTo(typeof(DummyBaseClass)); + } + + [Fact] + public void When_an_implemented_interface_type_instance_it_succeeds() + { + // Arrange / Act / Assert + typeof(DummyImplementingClass).Should().BeAssignableTo(typeof(IDisposable)); + } + + [Fact] + public void When_an_unrelated_type_instance_it_fails() + { + // Arrange + Type someType = typeof(DummyImplementingClass); + + // Act + Action act = () => + someType.Should().BeAssignableTo(typeof(DateTime), "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("*.DummyImplementingClass to be assignable to *.DateTime *failure message*"); + } + + [Fact] + public void When_constructed_of_open_generic_it_succeeds() + { + // Arrange / Act / Assert + typeof(IDummyInterface).Should().BeAssignableTo(typeof(IDummyInterface<>)); + } + + [Fact] + public void When_implementation_of_open_generic_interface_it_succeeds() + { + // Arrange / Act / Assert + typeof(ClassWithGenericBaseType).Should().BeAssignableTo(typeof(IDummyInterface<>)); + } + + [Fact] + public void When_derived_of_open_generic_class_it_succeeds() + { + // Arrange / Act / Assert + typeof(ClassWithGenericBaseType).Should().BeAssignableTo(typeof(DummyBaseType<>)); + } + + [Fact] + public void When_unrelated_to_open_generic_interface_it_fails() + { + // Arrange + Type someType = typeof(IDummyInterface); + + // Act + Action act = () => + someType.Should().BeAssignableTo(typeof(IDummyInterface<>), "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected someType *.IDummyInterface to be assignable to *.IDummyInterface`1[T] *failure message*" + + ", but it is not."); + } + + [Fact] + public void When_unrelated_to_open_generic_type_it_fails() + { + // Arrange + Type someType = typeof(ClassWithAttribute); + Action act = () => + someType.Should().BeAssignableTo(typeof(DummyBaseType<>), "we want to test the failure {0}", "message"); + + // Act / Assert + act.Should().Throw() + .WithMessage("*.ClassWithAttribute to be assignable to *.DummyBaseType* *failure message*"); + } + + [Fact] + public void When_asserting_an_open_generic_class_is_assignable_to_itself_it_succeeds() + { + // Arrange / Act / Assert + typeof(DummyBaseType<>).Should().BeAssignableTo(typeof(DummyBaseType<>)); + } + + [Fact] + public void When_asserting_a_type_to_be_assignable_to_null_it_should_throw() + { + // Arrange + var type = typeof(DummyBaseType<>); + + // Act + Action act = () => + type.Should().BeAssignableTo(null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("type"); + } } - [Fact] - public void When_its_own_type_instance_and_asserting_not_assignable_it_fails() + public class NotBeAssignableTo { - // Arrange - var type = typeof(DummyImplementingClass); - - // Act - Action act = () => - type.Should().NotBeAssignableTo(typeof(DummyImplementingClass), "we want to test the failure {0}", "message"); - - // Act / Assert - act.Should().Throw() - .WithMessage( - "Expected type *.DummyImplementingClass to not be assignable to *.DummyImplementingClass *failure message*" + - ", but it is."); + [Fact] + public void When_its_own_type_and_asserting_not_assignable_it_fails() + { + // Arrange + var type = typeof(DummyImplementingClass); + + // Act + Action act = () => + type.Should().NotBeAssignableTo("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type *.DummyImplementingClass to not be assignable to *.DummyImplementingClass *failure message*"); + } + + [Fact] + public void When_its_base_type_and_asserting_not_assignable_it_fails() + { + // Arrange + var type = typeof(DummyImplementingClass); + + // Act + Action act = () => + type.Should().NotBeAssignableTo("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type *.DummyImplementingClass to not be assignable to *.DummyBaseClass *failure message*" + + ", but it is."); + } + + [Fact] + public void When_implemented_interface_type_and_asserting_not_assignable_it_fails() + { + // Arrange + var type = typeof(DummyImplementingClass); + + // Act + Action act = () => + type.Should().NotBeAssignableTo("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type *.DummyImplementingClass to not be assignable to *.IDisposable *failure message*, but it is."); + } + + [Fact] + public void When_an_unrelated_type_and_asserting_not_assignable_it_succeeds() + { + // Arrange / Act / Assert + typeof(DummyImplementingClass).Should().NotBeAssignableTo(); + } + + [Fact] + public void When_its_own_type_instance_and_asserting_not_assignable_it_fails() + { + // Arrange + var type = typeof(DummyImplementingClass); + + // Act + Action act = () => + type.Should().NotBeAssignableTo(typeof(DummyImplementingClass), "we want to test the failure {0}", "message"); + + // Act / Assert + act.Should().Throw() + .WithMessage( + "Expected type *.DummyImplementingClass to not be assignable to *.DummyImplementingClass *failure message*" + + ", but it is."); + } + + [Fact] + public void When_its_base_type_instance_and_asserting_not_assignable_it_fails() + { + // Arrange + var type = typeof(DummyImplementingClass); + + // Act + Action act = () => + type.Should().NotBeAssignableTo(typeof(DummyBaseClass), "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type *.DummyImplementingClass to not be assignable to *.DummyBaseClass *failure message*" + + ", but it is."); + } + + [Fact] + public void When_an_implemented_interface_type_instance_and_asserting_not_assignable_it_fails() + { + // Arrange + var type = typeof(DummyImplementingClass); + + // Act + Action act = () => + type.Should().NotBeAssignableTo(typeof(IDisposable), "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type *.DummyImplementingClass to not be assignable to *.IDisposable *failure message*, but it is."); + } + + [Fact] + public void When_an_unrelated_type_instance_and_asserting_not_assignable_it_succeeds() + { + // Arrange / Act / Assert + typeof(DummyImplementingClass).Should().NotBeAssignableTo(typeof(DateTime)); + } + + [Fact] + public void When_unrelated_to_open_generic_interface_and_asserting_not_assignable_it_succeeds() + { + // Arrange / Act / Assert + typeof(ClassWithAttribute).Should().NotBeAssignableTo(typeof(IDummyInterface<>)); + } + + [Fact] + public void When_unrelated_to_open_generic_class_and_asserting_not_assignable_it_succeeds() + { + // Arrange / Act / Assert + typeof(ClassWithAttribute).Should().NotBeAssignableTo(typeof(DummyBaseType<>)); + } + + [Fact] + public void When_implementation_of_open_generic_interface_and_asserting_not_assignable_it_fails() + { + // Arrange + Type type = typeof(ClassWithGenericBaseType); + + // Act + Action act = () => + type.Should().NotBeAssignableTo(typeof(IDummyInterface<>), "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type *.ClassWithGenericBaseType to not be assignable to *.IDummyInterface`1[T] *failure message*" + + ", but it is."); + } + + [Fact] + public void When_derived_from_open_generic_class_and_asserting_not_assignable_it_fails() + { + // Arrange + Type type = typeof(ClassWithGenericBaseType); + + // Act + Action act = () => + type.Should().NotBeAssignableTo(typeof(IDummyInterface<>), "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type *.ClassWithGenericBaseType to not be assignable to *.IDummyInterface`1[T] *failure message*" + + ", but it is."); + } + + [Fact] + public void When_asserting_a_type_not_to_be_assignable_to_null_it_should_throw() + { + // Arrange + var type = typeof(DummyBaseType<>); + + // Act + Action act = + () => type.Should().NotBeAssignableTo(null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("type"); + } } - - [Fact] - public void When_its_base_type_instance_and_asserting_not_assignable_it_fails() - { - // Arrange - var type = typeof(DummyImplementingClass); - - // Act - Action act = () => - type.Should().NotBeAssignableTo(typeof(DummyBaseClass), "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type *.DummyImplementingClass to not be assignable to *.DummyBaseClass *failure message*" + - ", but it is."); - } - - [Fact] - public void When_an_implemented_interface_type_instance_and_asserting_not_assignable_it_fails() - { - // Arrange - var type = typeof(DummyImplementingClass); - - // Act - Action act = () => - type.Should().NotBeAssignableTo(typeof(IDisposable), "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type *.DummyImplementingClass to not be assignable to *.IDisposable *failure message*, but it is."); - } - - [Fact] - public void When_an_unrelated_type_instance_and_asserting_not_assignable_it_succeeds() - { - // Arrange / Act / Assert - typeof(DummyImplementingClass).Should().NotBeAssignableTo(typeof(DateTime)); - } - - [Fact] - public void When_unrelated_to_open_generic_interface_and_asserting_not_assignable_it_succeeds() - { - // Arrange / Act / Assert - typeof(ClassWithAttribute).Should().NotBeAssignableTo(typeof(IDummyInterface<>)); - } - - [Fact] - public void When_unrelated_to_open_generic_class_and_asserting_not_assignable_it_succeeds() - { - // Arrange / Act / Assert - typeof(ClassWithAttribute).Should().NotBeAssignableTo(typeof(DummyBaseType<>)); - } - - [Fact] - public void When_implementation_of_open_generic_interface_and_asserting_not_assignable_it_fails() - { - // Arrange - Type type = typeof(ClassWithGenericBaseType); - - // Act - Action act = () => - type.Should().NotBeAssignableTo(typeof(IDummyInterface<>), "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type *.ClassWithGenericBaseType to not be assignable to *.IDummyInterface`1[T] *failure message*" + - ", but it is."); - } - - [Fact] - public void When_derived_from_open_generic_class_and_asserting_not_assignable_it_fails() - { - // Arrange - Type type = typeof(ClassWithGenericBaseType); - - // Act - Action act = () => - type.Should().NotBeAssignableTo(typeof(IDummyInterface<>), "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type *.ClassWithGenericBaseType to not be assignable to *.IDummyInterface`1[T] *failure message*" + - ", but it is."); - } - - [Fact] - public void When_asserting_a_type_not_to_be_assignable_to_null_it_should_throw() - { - // Arrange - var type = typeof(DummyBaseType<>); - - // Act - Action act = - () => type.Should().NotBeAssignableTo(null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("type"); - } - - #endregion } } diff --git a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.BeDecoratedWith.cs b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.BeDecoratedWith.cs index 72a8bba418..43198f50f9 100644 --- a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.BeDecoratedWith.cs +++ b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.BeDecoratedWith.cs @@ -9,201 +9,199 @@ namespace FluentAssertions.Specs.Types /// public partial class TypeAssertionSpecs { - #region BeDecoratedWith - - [Fact] - public void When_type_is_decorated_with_expected_attribute_it_succeeds() + public class BeDecoratedWith { - // Arrange - Type typeWithAttribute = typeof(ClassWithAttribute); - - // Act - Action act = () => - typeWithAttribute.Should().BeDecoratedWith(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_type_is_decorated_with_expected_attribute_it_should_allow_chaining() - { - // Arrange - Type typeWithAttribute = typeof(ClassWithAttribute); - - // Act - Action act = () => - typeWithAttribute.Should().BeDecoratedWith() - .Which.IsEnabled.Should().BeTrue(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_type_is_not_decorated_with_expected_attribute_it_fails() - { - // Arrange - Type typeWithoutAttribute = typeof(ClassWithoutAttribute); - - // Act - Action act = () => - typeWithoutAttribute.Should().BeDecoratedWith("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type *.ClassWithoutAttribute to be decorated with *.DummyClassAttribute *failure message*" + - ", but the attribute was not found."); - } - - [Fact] - public void When_injecting_a_null_predicate_into_BeDecoratedWith_it_should_throw() - { - // Arrange - Type typeWithAttribute = typeof(ClassWithAttribute); - - // Act - Action act = () => - typeWithAttribute.Should().BeDecoratedWith(isMatchingAttributePredicate: null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("isMatchingAttributePredicate"); - } - - [Fact] - public void When_type_is_decorated_with_expected_attribute_with_the_expected_properties_it_succeeds() - { - // Arrange - Type typeWithAttribute = typeof(ClassWithAttribute); - - // Act - Action act = () => - typeWithAttribute.Should() - .BeDecoratedWith(a => (a.Name == "Expected") && a.IsEnabled); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_type_is_decorated_with_expected_attribute_with_the_expected_properties_it_should_allow_chaining() - { - // Arrange - Type typeWithAttribute = typeof(ClassWithAttribute); - - // Act - Action act = () => - typeWithAttribute.Should() - .BeDecoratedWith(a => a.Name == "Expected") + [Fact] + public void When_type_is_decorated_with_expected_attribute_it_succeeds() + { + // Arrange + Type typeWithAttribute = typeof(ClassWithAttribute); + + // Act + Action act = () => + typeWithAttribute.Should().BeDecoratedWith(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_type_is_decorated_with_expected_attribute_it_should_allow_chaining() + { + // Arrange + Type typeWithAttribute = typeof(ClassWithAttribute); + + // Act + Action act = () => + typeWithAttribute.Should().BeDecoratedWith() .Which.IsEnabled.Should().BeTrue(); - // Assert - act.Should().NotThrow(); + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_type_is_not_decorated_with_expected_attribute_it_fails() + { + // Arrange + Type typeWithoutAttribute = typeof(ClassWithoutAttribute); + + // Act + Action act = () => + typeWithoutAttribute.Should().BeDecoratedWith("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type *.ClassWithoutAttribute to be decorated with *.DummyClassAttribute *failure message*" + + ", but the attribute was not found."); + } + + [Fact] + public void When_injecting_a_null_predicate_into_BeDecoratedWith_it_should_throw() + { + // Arrange + Type typeWithAttribute = typeof(ClassWithAttribute); + + // Act + Action act = () => + typeWithAttribute.Should().BeDecoratedWith(isMatchingAttributePredicate: null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("isMatchingAttributePredicate"); + } + + [Fact] + public void When_type_is_decorated_with_expected_attribute_with_the_expected_properties_it_succeeds() + { + // Arrange + Type typeWithAttribute = typeof(ClassWithAttribute); + + // Act + Action act = () => + typeWithAttribute.Should() + .BeDecoratedWith(a => (a.Name == "Expected") && a.IsEnabled); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_type_is_decorated_with_expected_attribute_with_the_expected_properties_it_should_allow_chaining() + { + // Arrange + Type typeWithAttribute = typeof(ClassWithAttribute); + + // Act + Action act = () => + typeWithAttribute.Should() + .BeDecoratedWith(a => a.Name == "Expected") + .Which.IsEnabled.Should().BeTrue(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_type_is_decorated_with_expected_attribute_that_has_an_unexpected_property_it_fails() + { + // Arrange + Type typeWithAttribute = typeof(ClassWithAttribute); + + // Act + Action act = () => + typeWithAttribute.Should() + .BeDecoratedWith(a => (a.Name == "Unexpected") && a.IsEnabled); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type *.ClassWithAttribute to be decorated with *.DummyClassAttribute that matches " + + "(a.Name == \"Unexpected\")*a.IsEnabled, but no matching attribute was found."); + } } - [Fact] - public void When_type_is_decorated_with_expected_attribute_that_has_an_unexpected_property_it_fails() + public class NotBeDecoratedWith { - // Arrange - Type typeWithAttribute = typeof(ClassWithAttribute); - - // Act - Action act = () => - typeWithAttribute.Should() - .BeDecoratedWith(a => (a.Name == "Unexpected") && a.IsEnabled); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type *.ClassWithAttribute to be decorated with *.DummyClassAttribute that matches " + - "(a.Name == \"Unexpected\")*a.IsEnabled, but no matching attribute was found."); + [Fact] + public void When_type_is_not_decorated_with_unexpected_attribute_it_succeeds() + { + // Arrange + Type typeWithoutAttribute = typeof(ClassWithoutAttribute); + + // Act + Action act = () => + typeWithoutAttribute.Should().NotBeDecoratedWith(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_type_is_decorated_with_unexpected_attribute_it_fails() + { + // Arrange + Type typeWithAttribute = typeof(ClassWithAttribute); + + // Act + Action act = () => + typeWithAttribute.Should().NotBeDecoratedWith("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type *.ClassWithAttribute to not be decorated with *.DummyClassAttribute* *failure message*" + + ", but the attribute was found."); + } + + [Fact] + public void When_injecting_a_null_predicate_into_NotBeDecoratedWith_it_should_throw() + { + // Arrange + Type typeWithoutAttribute = typeof(ClassWithAttribute); + + // Act + Action act = () => typeWithoutAttribute.Should() + .NotBeDecoratedWith(isMatchingAttributePredicate: null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("isMatchingAttributePredicate"); + } + + [Fact] + public void When_type_is_not_decorated_with_unexpected_attribute_with_the_expected_properties_it_succeeds() + { + // Arrange + Type typeWithoutAttribute = typeof(ClassWithAttribute); + + // Act + Action act = () => + typeWithoutAttribute.Should() + .NotBeDecoratedWith(a => (a.Name == "Unexpected") && a.IsEnabled); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_type_is_not_decorated_with_expected_attribute_that_has_an_unexpected_property_it_fails() + { + // Arrange + Type typeWithoutAttribute = typeof(ClassWithAttribute); + + // Act + Action act = () => + typeWithoutAttribute.Should() + .NotBeDecoratedWith(a => (a.Name == "Expected") && a.IsEnabled); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type *.ClassWithAttribute to not be decorated with *.DummyClassAttribute that matches " + + "(a.Name == \"Expected\") * a.IsEnabled, but a matching attribute was found."); + } } - - #endregion - - #region NotBeDecoratedWith - - [Fact] - public void When_type_is_not_decorated_with_unexpected_attribute_it_succeeds() - { - // Arrange - Type typeWithoutAttribute = typeof(ClassWithoutAttribute); - - // Act - Action act = () => - typeWithoutAttribute.Should().NotBeDecoratedWith(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_type_is_decorated_with_unexpected_attribute_it_fails() - { - // Arrange - Type typeWithAttribute = typeof(ClassWithAttribute); - - // Act - Action act = () => - typeWithAttribute.Should().NotBeDecoratedWith("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type *.ClassWithAttribute to not be decorated with *.DummyClassAttribute* *failure message*" + - ", but the attribute was found."); - } - - [Fact] - public void When_injecting_a_null_predicate_into_NotBeDecoratedWith_it_should_throw() - { - // Arrange - Type typeWithoutAttribute = typeof(ClassWithAttribute); - - // Act - Action act = () => typeWithoutAttribute.Should() - .NotBeDecoratedWith(isMatchingAttributePredicate: null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("isMatchingAttributePredicate"); - } - - [Fact] - public void When_type_is_not_decorated_with_unexpected_attribute_with_the_expected_properties_it_succeeds() - { - // Arrange - Type typeWithoutAttribute = typeof(ClassWithAttribute); - - // Act - Action act = () => - typeWithoutAttribute.Should() - .NotBeDecoratedWith(a => (a.Name == "Unexpected") && a.IsEnabled); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_type_is_not_decorated_with_expected_attribute_that_has_an_unexpected_property_it_fails() - { - // Arrange - Type typeWithoutAttribute = typeof(ClassWithAttribute); - - // Act - Action act = () => - typeWithoutAttribute.Should() - .NotBeDecoratedWith(a => (a.Name == "Expected") && a.IsEnabled); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type *.ClassWithAttribute to not be decorated with *.DummyClassAttribute that matches " + - "(a.Name == \"Expected\") * a.IsEnabled, but a matching attribute was found."); - } - - #endregion } } diff --git a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.BeDecoratedWithOrInherit.cs b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.BeDecoratedWithOrInherit.cs index 7aabde5638..8567ae0ce2 100644 --- a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.BeDecoratedWithOrInherit.cs +++ b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.BeDecoratedWithOrInherit.cs @@ -9,202 +9,200 @@ namespace FluentAssertions.Specs.Types /// public partial class TypeAssertionSpecs { - #region BeDecoratedWithOrInherit - - [Fact] - public void When_type_inherits_expected_attribute_it_succeeds() + public class BeDecoratedWithOrInherit { - // Arrange - Type typeWithAttribute = typeof(ClassWithInheritedAttribute); - - // Act - Action act = () => - typeWithAttribute.Should().BeDecoratedWithOrInherit(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_type_inherits_expected_attribute_it_should_allow_chaining() - { - // Arrange - Type typeWithAttribute = typeof(ClassWithInheritedAttribute); - - // Act - Action act = () => - typeWithAttribute.Should().BeDecoratedWithOrInherit() - .Which.IsEnabled.Should().BeTrue(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_type_does_not_inherit_expected_attribute_it_fails() - { - // Arrange - Type type = typeof(ClassWithoutAttribute); - - // Act - Action act = () => - type.Should().BeDecoratedWithOrInherit("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type *.ClassWithoutAttribute to be decorated with or inherit *.DummyClassAttribute " + - "*failure message*, but the attribute was not found."); - } - - [Fact] - public void When_injecting_a_null_predicate_into_BeDecoratedWithOrInherit_it_should_throw() - { - // Arrange - Type typeWithAttribute = typeof(ClassWithInheritedAttribute); - - // Act - Action act = () => typeWithAttribute.Should() - .BeDecoratedWithOrInherit(isMatchingAttributePredicate: null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("isMatchingAttributePredicate"); - } - - [Fact] - public void When_type_inherits_expected_attribute_with_the_expected_properties_it_succeeds() - { - // Arrange - Type typeWithAttribute = typeof(ClassWithInheritedAttribute); - - // Act - Action act = () => - typeWithAttribute.Should() - .BeDecoratedWithOrInherit(a => (a.Name == "Expected") && a.IsEnabled); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_type_inherits_expected_attribute_with_the_expected_properties_it_should_allow_chaining() - { - // Arrange - Type typeWithAttribute = typeof(ClassWithInheritedAttribute); - - // Act - Action act = () => - typeWithAttribute.Should() - .BeDecoratedWithOrInherit(a => a.Name == "Expected") + [Fact] + public void When_type_inherits_expected_attribute_it_succeeds() + { + // Arrange + Type typeWithAttribute = typeof(ClassWithInheritedAttribute); + + // Act + Action act = () => + typeWithAttribute.Should().BeDecoratedWithOrInherit(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_type_inherits_expected_attribute_it_should_allow_chaining() + { + // Arrange + Type typeWithAttribute = typeof(ClassWithInheritedAttribute); + + // Act + Action act = () => + typeWithAttribute.Should().BeDecoratedWithOrInherit() .Which.IsEnabled.Should().BeTrue(); - // Assert - act.Should().NotThrow(); + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_type_does_not_inherit_expected_attribute_it_fails() + { + // Arrange + Type type = typeof(ClassWithoutAttribute); + + // Act + Action act = () => + type.Should().BeDecoratedWithOrInherit("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type *.ClassWithoutAttribute to be decorated with or inherit *.DummyClassAttribute " + + "*failure message*, but the attribute was not found."); + } + + [Fact] + public void When_injecting_a_null_predicate_into_BeDecoratedWithOrInherit_it_should_throw() + { + // Arrange + Type typeWithAttribute = typeof(ClassWithInheritedAttribute); + + // Act + Action act = () => typeWithAttribute.Should() + .BeDecoratedWithOrInherit(isMatchingAttributePredicate: null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("isMatchingAttributePredicate"); + } + + [Fact] + public void When_type_inherits_expected_attribute_with_the_expected_properties_it_succeeds() + { + // Arrange + Type typeWithAttribute = typeof(ClassWithInheritedAttribute); + + // Act + Action act = () => + typeWithAttribute.Should() + .BeDecoratedWithOrInherit(a => (a.Name == "Expected") && a.IsEnabled); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_type_inherits_expected_attribute_with_the_expected_properties_it_should_allow_chaining() + { + // Arrange + Type typeWithAttribute = typeof(ClassWithInheritedAttribute); + + // Act + Action act = () => + typeWithAttribute.Should() + .BeDecoratedWithOrInherit(a => a.Name == "Expected") + .Which.IsEnabled.Should().BeTrue(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_type_inherits_expected_attribute_that_has_an_unexpected_property_it_fails() + { + // Arrange + Type typeWithAttribute = typeof(ClassWithInheritedAttribute); + + // Act + Action act = () => + typeWithAttribute.Should() + .BeDecoratedWithOrInherit(a => (a.Name == "Unexpected") && a.IsEnabled); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type *.ClassWithInheritedAttribute to be decorated with or inherit *.DummyClassAttribute " + + "that matches (a.Name == \"Unexpected\")*a.IsEnabled, but no matching attribute was found."); + } } - [Fact] - public void When_type_inherits_expected_attribute_that_has_an_unexpected_property_it_fails() + public class NotBeDecoratedWithOrInherit { - // Arrange - Type typeWithAttribute = typeof(ClassWithInheritedAttribute); - - // Act - Action act = () => - typeWithAttribute.Should() - .BeDecoratedWithOrInherit(a => (a.Name == "Unexpected") && a.IsEnabled); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type *.ClassWithInheritedAttribute to be decorated with or inherit *.DummyClassAttribute " + - "that matches (a.Name == \"Unexpected\")*a.IsEnabled, but no matching attribute was found."); + [Fact] + public void When_type_does_not_inherit_unexpected_attribute_it_succeeds() + { + // Arrange + Type typeWithoutAttribute = typeof(ClassWithoutAttribute); + + // Act + Action act = () => + typeWithoutAttribute.Should().NotBeDecoratedWithOrInherit(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_type_inherits_unexpected_attribute_it_fails() + { + // Arrange + Type typeWithAttribute = typeof(ClassWithInheritedAttribute); + + // Act + Action act = () => + typeWithAttribute.Should() + .NotBeDecoratedWithOrInherit("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type *.ClassWithInheritedAttribute to not be decorated with or inherit *.DummyClassAttribute " + + "*failure message* attribute was found."); + } + + [Fact] + public void When_injecting_a_null_predicate_into_NotBeDecoratedWithOrInherit_it_should_throw() + { + // Arrange + Type typeWithoutAttribute = typeof(ClassWithInheritedAttribute); + + // Act + Action act = () => typeWithoutAttribute.Should() + .NotBeDecoratedWithOrInherit(isMatchingAttributePredicate: null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("isMatchingAttributePredicate"); + } + + [Fact] + public void When_type_does_not_inherit_unexpected_attribute_with_the_expected_properties_it_succeeds() + { + // Arrange + Type typeWithoutAttribute = typeof(ClassWithInheritedAttribute); + + // Act + Action act = () => + typeWithoutAttribute.Should() + .NotBeDecoratedWithOrInherit(a => (a.Name == "Unexpected") && a.IsEnabled); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_type_does_not_inherit_expected_attribute_that_has_an_unexpected_property_it_fails() + { + // Arrange + Type typeWithoutAttribute = typeof(ClassWithInheritedAttribute); + + // Act + Action act = () => + typeWithoutAttribute.Should() + .NotBeDecoratedWithOrInherit(a => (a.Name == "Expected") && a.IsEnabled); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type *.ClassWithInheritedAttribute to not be decorated with or inherit *.DummyClassAttribute " + + "that matches (a.Name == \"Expected\") * a.IsEnabled, but a matching attribute was found."); + } } - - #endregion - - #region NotBeDecoratedWithOrInherit - - [Fact] - public void When_type_does_not_inherit_unexpected_attribute_it_succeeds() - { - // Arrange - Type typeWithoutAttribute = typeof(ClassWithoutAttribute); - - // Act - Action act = () => - typeWithoutAttribute.Should().NotBeDecoratedWithOrInherit(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_type_inherits_unexpected_attribute_it_fails() - { - // Arrange - Type typeWithAttribute = typeof(ClassWithInheritedAttribute); - - // Act - Action act = () => - typeWithAttribute.Should() - .NotBeDecoratedWithOrInherit("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type *.ClassWithInheritedAttribute to not be decorated with or inherit *.DummyClassAttribute " + - "*failure message* attribute was found."); - } - - [Fact] - public void When_injecting_a_null_predicate_into_NotBeDecoratedWithOrInherit_it_should_throw() - { - // Arrange - Type typeWithoutAttribute = typeof(ClassWithInheritedAttribute); - - // Act - Action act = () => typeWithoutAttribute.Should() - .NotBeDecoratedWithOrInherit(isMatchingAttributePredicate: null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("isMatchingAttributePredicate"); - } - - [Fact] - public void When_type_does_not_inherit_unexpected_attribute_with_the_expected_properties_it_succeeds() - { - // Arrange - Type typeWithoutAttribute = typeof(ClassWithInheritedAttribute); - - // Act - Action act = () => - typeWithoutAttribute.Should() - .NotBeDecoratedWithOrInherit(a => (a.Name == "Unexpected") && a.IsEnabled); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_type_does_not_inherit_expected_attribute_that_has_an_unexpected_property_it_fails() - { - // Arrange - Type typeWithoutAttribute = typeof(ClassWithInheritedAttribute); - - // Act - Action act = () => - typeWithoutAttribute.Should() - .NotBeDecoratedWithOrInherit(a => (a.Name == "Expected") && a.IsEnabled); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type *.ClassWithInheritedAttribute to not be decorated with or inherit *.DummyClassAttribute " + - "that matches (a.Name == \"Expected\") * a.IsEnabled, but a matching attribute was found."); - } - - #endregion } } diff --git a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.BeDerivedFrom.cs b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.BeDerivedFrom.cs index bbd3154e86..ab5866c623 100644 --- a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.BeDerivedFrom.cs +++ b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.BeDerivedFrom.cs @@ -10,262 +10,258 @@ namespace FluentAssertions.Specs.Types /// public partial class TypeAssertionSpecs { - #region BeDerivedFrom - - [Fact] - public void When_asserting_a_type_is_derived_from_its_base_class_it_succeeds() - { - // Arrange - var type = typeof(DummyImplementingClass); - - // Act - Action act = () => - type.Should().BeDerivedFrom(typeof(DummyBaseClass)); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_type_is_derived_from_an_unrelated_class_it_fails() - { - // Arrange - var type = typeof(DummyBaseClass); - - // Act - Action act = () => - type.Should().BeDerivedFrom(typeof(ClassWithMembers), "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type *.DummyBaseClass to be derived from *.ClassWithMembers *failure message*, but it is not."); - } - - [Fact] - public void When_asserting_a_type_is_derived_from_an_interface_it_fails() - { - // Arrange - var type = typeof(ClassThatImplementsInterface); - - // Act - Action act = () => - type.Should().BeDerivedFrom(typeof(IDummyInterface), "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type *.ClassThatImplementsInterface to be derived from *.IDummyInterface *failure message*" + - ", but *.IDummyInterface is an interface."); - } - - [Fact] - public void When_asserting_a_type_is_derived_from_an_open_generic_it_succeeds() - { - // Arrange - var type = typeof(DummyBaseType); - - // Act - Action act = () => - type.Should().BeDerivedFrom(typeof(DummyBaseType<>)); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_type_is_derived_from_an_open_generic_base_class_it_succeeds() - { - // Arrange - var type = typeof(ClassWithGenericBaseType); - - // Act - Action act = () => - type.Should().BeDerivedFrom(typeof(DummyBaseType<>)); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_type_is_derived_from_an_unrelated_open_generic_class_it_fails() - { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should().BeDerivedFrom(typeof(DummyBaseType<>), "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type *.ClassWithMembers to be derived from *.DummyBaseType`* *failure message*, but it is not."); - } - - [Fact] - public void When_asserting_a_type_to_be_derived_from_null_it_should_throw() - { - // Arrange - var type = typeof(DummyBaseType<>); - - // Act - Action act = - () => type.Should().BeDerivedFrom(null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("baseType"); - } - - #endregion - - #region BeDerivedFromOfT - - [Fact] - public void When_asserting_a_type_is_DerivedFromOfT_its_base_class_it_succeeds() - { - // Arrange - var type = typeof(DummyImplementingClass); - - // Act - Action act = () => - type.Should().BeDerivedFrom(); - - // Assert - act.Should().NotThrow(); - } - - #endregion - - #region NotBeDerivedFrom - - [Fact] - public void When_asserting_a_type_is_not_derived_from_an_unrelated_class_it_succeeds() - { - // Arrange - var type = typeof(DummyBaseClass); - - // Act - Action act = () => - type.Should().NotBeDerivedFrom(typeof(ClassWithMembers)); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_type_is_not_derived_from_its_base_class_it_fails() - { - // Arrange - var type = typeof(DummyImplementingClass); - - // Act - Action act = () => - type.Should().NotBeDerivedFrom(typeof(DummyBaseClass), "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type *.DummyImplementingClass not to be derived from *.DummyBaseClass *failure message*" + - ", but it is."); - } - - [Fact] - public void When_asserting_a_type_is_not_derived_from_an_interface_it_fails() + public class BeDerivedFrom { - // Arrange - var type = typeof(ClassThatImplementsInterface); - - // Act - Action act = () => - type.Should().NotBeDerivedFrom(typeof(IDummyInterface), "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type *.ClassThatImplementsInterface not to be derived from *.IDummyInterface *failure message*" + - ", but *.IDummyInterface is an interface."); + [Fact] + public void When_asserting_a_type_is_derived_from_its_base_class_it_succeeds() + { + // Arrange + var type = typeof(DummyImplementingClass); + + // Act + Action act = () => + type.Should().BeDerivedFrom(typeof(DummyBaseClass)); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_is_derived_from_an_unrelated_class_it_fails() + { + // Arrange + var type = typeof(DummyBaseClass); + + // Act + Action act = () => + type.Should().BeDerivedFrom(typeof(ClassWithMembers), "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type *.DummyBaseClass to be derived from *.ClassWithMembers *failure message*, but it is not."); + } + + [Fact] + public void When_asserting_a_type_is_derived_from_an_interface_it_fails() + { + // Arrange + var type = typeof(ClassThatImplementsInterface); + + // Act + Action act = () => + type.Should().BeDerivedFrom(typeof(IDummyInterface), "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type *.ClassThatImplementsInterface to be derived from *.IDummyInterface *failure message*" + + ", but *.IDummyInterface is an interface."); + } + + [Fact] + public void When_asserting_a_type_is_derived_from_an_open_generic_it_succeeds() + { + // Arrange + var type = typeof(DummyBaseType); + + // Act + Action act = () => + type.Should().BeDerivedFrom(typeof(DummyBaseType<>)); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_is_derived_from_an_open_generic_base_class_it_succeeds() + { + // Arrange + var type = typeof(ClassWithGenericBaseType); + + // Act + Action act = () => + type.Should().BeDerivedFrom(typeof(DummyBaseType<>)); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_is_derived_from_an_unrelated_open_generic_class_it_fails() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should().BeDerivedFrom(typeof(DummyBaseType<>), "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type *.ClassWithMembers to be derived from *.DummyBaseType`* *failure message*, but it is not."); + } + + [Fact] + public void When_asserting_a_type_to_be_derived_from_null_it_should_throw() + { + // Arrange + var type = typeof(DummyBaseType<>); + + // Act + Action act = + () => type.Should().BeDerivedFrom(null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("baseType"); + } } - [Fact] - public void When_asserting_a_type_is_not_derived_from_an_unrelated_open_generic_it_succeeds() + public class BeDerivedFromOfT { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should().NotBeDerivedFrom(typeof(DummyBaseType<>)); - - // Assert - act.Should().NotThrow(); + [Fact] + public void When_asserting_a_type_is_DerivedFromOfT_its_base_class_it_succeeds() + { + // Arrange + var type = typeof(DummyImplementingClass); + + // Act + Action act = () => + type.Should().BeDerivedFrom(); + + // Assert + act.Should().NotThrow(); + } } - [Fact] - public void When_asserting_an_open_generic_type_is_not_derived_from_itself_it_succeeds() + public class NotBeDerivedFrom { - // Arrange - var type = typeof(DummyBaseType<>); - - // Act - Action act = () => - type.Should().NotBeDerivedFrom(typeof(DummyBaseType<>)); - - // Assert - act.Should().NotThrow(); + [Fact] + public void When_asserting_a_type_is_not_derived_from_an_unrelated_class_it_succeeds() + { + // Arrange + var type = typeof(DummyBaseClass); + + // Act + Action act = () => + type.Should().NotBeDerivedFrom(typeof(ClassWithMembers)); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_is_not_derived_from_its_base_class_it_fails() + { + // Arrange + var type = typeof(DummyImplementingClass); + + // Act + Action act = () => + type.Should().NotBeDerivedFrom(typeof(DummyBaseClass), "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type *.DummyImplementingClass not to be derived from *.DummyBaseClass *failure message*" + + ", but it is."); + } + + [Fact] + public void When_asserting_a_type_is_not_derived_from_an_interface_it_fails() + { + // Arrange + var type = typeof(ClassThatImplementsInterface); + + // Act + Action act = () => + type.Should().NotBeDerivedFrom(typeof(IDummyInterface), "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type *.ClassThatImplementsInterface not to be derived from *.IDummyInterface *failure message*" + + ", but *.IDummyInterface is an interface."); + } + + [Fact] + public void When_asserting_a_type_is_not_derived_from_an_unrelated_open_generic_it_succeeds() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should().NotBeDerivedFrom(typeof(DummyBaseType<>)); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_an_open_generic_type_is_not_derived_from_itself_it_succeeds() + { + // Arrange + var type = typeof(DummyBaseType<>); + + // Act + Action act = () => + type.Should().NotBeDerivedFrom(typeof(DummyBaseType<>)); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_is_not_derived_from_its_open_generic_it_fails() + { + // Arrange + var type = typeof(DummyBaseType); + + // Act + Action act = () => + type.Should().NotBeDerivedFrom(typeof(DummyBaseType<>), "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type *.DummyBaseType`1[*.ClassWithGenericBaseType] not to be derived from *.DummyBaseType`1[T] " + + "*failure message*, but it is."); + } + + [Fact] + public void When_asserting_a_type_not_to_be_derived_from_null_it_should_throw() + { + // Arrange + var type = typeof(DummyBaseType<>); + + // Act + Action act = + () => type.Should().NotBeDerivedFrom(null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("baseType"); + } } - [Fact] - public void When_asserting_a_type_is_not_derived_from_its_open_generic_it_fails() + public class NotBeDerivedFromOfT { - // Arrange - var type = typeof(DummyBaseType); - - // Act - Action act = () => - type.Should().NotBeDerivedFrom(typeof(DummyBaseType<>), "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type *.DummyBaseType`1[*.ClassWithGenericBaseType] not to be derived from *.DummyBaseType`1[T] " + - "*failure message*, but it is."); - } - - [Fact] - public void When_asserting_a_type_not_to_be_derived_from_null_it_should_throw() - { - // Arrange - var type = typeof(DummyBaseType<>); - - // Act - Action act = - () => type.Should().NotBeDerivedFrom(null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("baseType"); + [Fact] + public void When_asserting_a_type_is_not_DerivedFromOfT_an_unrelated_class_it_succeeds() + { + // Arrange + var type = typeof(DummyBaseClass); + + // Act + Action act = () => + type.Should().NotBeDerivedFrom(); + + // Assert + act.Should().NotThrow(); + } } - - #endregion - - #region NotBeDerivedFromOfT - - [Fact] - public void When_asserting_a_type_is_not_DerivedFromOfT_an_unrelated_class_it_succeeds() - { - // Arrange - var type = typeof(DummyBaseClass); - - // Act - Action act = () => - type.Should().NotBeDerivedFrom(); - - // Assert - act.Should().NotThrow(); - } - - #endregion } } diff --git a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.BeSealed.cs b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.BeSealed.cs index d98592295f..6886e3bdd4 100644 --- a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.BeSealed.cs +++ b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.BeSealed.cs @@ -9,143 +9,141 @@ namespace FluentAssertions.Specs.Types /// public partial class TypeAssertionSpecs { - #region BeSealed - - [Fact] - public void When_type_is_sealed_it_succeeds() - { - // Arrange / Act / Assert - typeof(Sealed).Should().BeSealed(); - } - - [Theory] - [InlineData(typeof(ClassWithoutMembers), "Expected type *.ClassWithoutMembers to be sealed.")] - [InlineData(typeof(Abstract), "Expected type *.Abstract to be sealed.")] - [InlineData(typeof(Static), "Expected type *.Static to be sealed.")] - public void When_type_is_not_sealed_it_fails(Type type, string exceptionMessage) + public class BeSealed { - // Act - Action act = () => type.Should().BeSealed(); - - // Assert - act.Should().Throw() - .WithMessage(exceptionMessage); + [Fact] + public void When_type_is_sealed_it_succeeds() + { + // Arrange / Act / Assert + typeof(Sealed).Should().BeSealed(); + } + + [Theory] + [InlineData(typeof(ClassWithoutMembers), "Expected type *.ClassWithoutMembers to be sealed.")] + [InlineData(typeof(Abstract), "Expected type *.Abstract to be sealed.")] + [InlineData(typeof(Static), "Expected type *.Static to be sealed.")] + public void When_type_is_not_sealed_it_fails(Type type, string exceptionMessage) + { + // Act + Action act = () => type.Should().BeSealed(); + + // Assert + act.Should().Throw() + .WithMessage(exceptionMessage); + } + + [Fact] + public void When_type_is_not_sealed_it_fails_with_a_meaningful_message() + { + // Arrange + var type = typeof(ClassWithoutMembers); + + // Act + Action act = () => type.Should().BeSealed("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected type *.ClassWithoutMembers to be sealed *failure message*."); + } + + [Theory] + [InlineData(typeof(IDummyInterface), "*.IDummyInterface must be a class.")] + [InlineData(typeof(Struct), "*.Struct must be a class.")] + [InlineData(typeof(ExampleDelegate), "*.ExampleDelegate must be a class.")] + public void When_type_is_not_valid_for_BeSealed_it_throws_exception(Type type, string exceptionMessage) + { + // Act + Action act = () => type.Should().BeSealed(); + + // Assert + act.Should().Throw() + .WithMessage(exceptionMessage); + } + + [Fact] + public void When_subject_is_null_be_sealed_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().BeSealed("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected type to be sealed *failure message*, but type is ."); + } } - [Fact] - public void When_type_is_not_sealed_it_fails_with_a_meaningful_message() - { - // Arrange - var type = typeof(ClassWithoutMembers); - - // Act - Action act = () => type.Should().BeSealed("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type *.ClassWithoutMembers to be sealed *failure message*."); - } - - [Theory] - [InlineData(typeof(IDummyInterface), "*.IDummyInterface must be a class.")] - [InlineData(typeof(Struct), "*.Struct must be a class.")] - [InlineData(typeof(ExampleDelegate), "*.ExampleDelegate must be a class.")] - public void When_type_is_not_valid_for_BeSealed_it_throws_exception(Type type, string exceptionMessage) - { - // Act - Action act = () => type.Should().BeSealed(); - - // Assert - act.Should().Throw() - .WithMessage(exceptionMessage); - } - - [Fact] - public void When_subject_is_null_be_sealed_should_fail() - { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().BeSealed("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type to be sealed *failure message*, but type is ."); - } - - #endregion - - #region NotBeSealed - - [Theory] - [InlineData(typeof(ClassWithoutMembers))] - [InlineData(typeof(Abstract))] - [InlineData(typeof(Static))] - public void When_type_is_not_sealed_it_succeeds(Type type) + public class NotBeSealed { - // Arrange / Act / Assert - type.Should().NotBeSealed(); + [Theory] + [InlineData(typeof(ClassWithoutMembers))] + [InlineData(typeof(Abstract))] + [InlineData(typeof(Static))] + public void When_type_is_not_sealed_it_succeeds(Type type) + { + // Arrange / Act / Assert + type.Should().NotBeSealed(); + } + + [Fact] + public void When_type_is_sealed_it_fails() + { + // Arrange + var type = typeof(Sealed); + + // Act + Action act = () => type.Should().NotBeSealed(); + + // Assert + act.Should().Throw() + .WithMessage("Expected type *.Sealed not to be sealed."); + } + + [Fact] + public void When_type_is_sealed_it_fails_with_a_meaningful_message() + { + // Arrange + var type = typeof(Sealed); + + // Act + Action act = () => type.Should().NotBeSealed("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected type *.Sealed not to be sealed *failure message*."); + } + + [Theory] + [InlineData(typeof(IDummyInterface), "*.IDummyInterface must be a class.")] + [InlineData(typeof(Struct), "*.Struct must be a class.")] + [InlineData(typeof(ExampleDelegate), "*.ExampleDelegate must be a class.")] + public void When_type_is_not_valid_for_NotBeSealed_it_throws_exception(Type type, string exceptionMessage) + { + // Act + Action act = () => type.Should().NotBeSealed(); + + // Assert + act.Should().Throw() + .WithMessage(exceptionMessage); + } + + [Fact] + public void When_subject_is_null_not_be_sealed_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().NotBeSealed("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected type not to be sealed *failure message*, but type is ."); + } } - - [Fact] - public void When_type_is_sealed_it_fails() - { - // Arrange - var type = typeof(Sealed); - - // Act - Action act = () => type.Should().NotBeSealed(); - - // Assert - act.Should().Throw() - .WithMessage("Expected type *.Sealed not to be sealed."); - } - - [Fact] - public void When_type_is_sealed_it_fails_with_a_meaningful_message() - { - // Arrange - var type = typeof(Sealed); - - // Act - Action act = () => type.Should().NotBeSealed("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type *.Sealed not to be sealed *failure message*."); - } - - [Theory] - [InlineData(typeof(IDummyInterface), "*.IDummyInterface must be a class.")] - [InlineData(typeof(Struct), "*.Struct must be a class.")] - [InlineData(typeof(ExampleDelegate), "*.ExampleDelegate must be a class.")] - public void When_type_is_not_valid_for_NotBeSealed_it_throws_exception(Type type, string exceptionMessage) - { - // Act - Action act = () => type.Should().NotBeSealed(); - - // Assert - act.Should().Throw() - .WithMessage(exceptionMessage); - } - - [Fact] - public void When_subject_is_null_not_be_sealed_should_fail() - { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().NotBeSealed("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type not to be sealed *failure message*, but type is ."); - } - - #endregion } } diff --git a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.BeStatic.cs b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.BeStatic.cs index f3f0a6ca7a..273618f1a5 100644 --- a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.BeStatic.cs +++ b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.BeStatic.cs @@ -9,143 +9,141 @@ namespace FluentAssertions.Specs.Types /// public partial class TypeAssertionSpecs { - #region BeStatic - - [Fact] - public void When_type_is_static_it_succeeds() - { - // Arrange / Act / Assert - typeof(Static).Should().BeStatic(); - } - - [Theory] - [InlineData(typeof(ClassWithoutMembers), "Expected type *.ClassWithoutMembers to be static.")] - [InlineData(typeof(Sealed), "Expected type *.Sealed to be static.")] - [InlineData(typeof(Abstract), "Expected type *.Abstract to be static.")] - public void When_type_is_not_static_it_fails(Type type, string exceptionMessage) + public class BeStatic { - // Act - Action act = () => type.Should().BeStatic(); - - // Assert - act.Should().Throw() - .WithMessage(exceptionMessage); + [Fact] + public void When_type_is_static_it_succeeds() + { + // Arrange / Act / Assert + typeof(Static).Should().BeStatic(); + } + + [Theory] + [InlineData(typeof(ClassWithoutMembers), "Expected type *.ClassWithoutMembers to be static.")] + [InlineData(typeof(Sealed), "Expected type *.Sealed to be static.")] + [InlineData(typeof(Abstract), "Expected type *.Abstract to be static.")] + public void When_type_is_not_static_it_fails(Type type, string exceptionMessage) + { + // Act + Action act = () => type.Should().BeStatic(); + + // Assert + act.Should().Throw() + .WithMessage(exceptionMessage); + } + + [Fact] + public void When_type_is_not_static_it_fails_with_a_meaningful_message() + { + // Arrange + var type = typeof(ClassWithoutMembers); + + // Act + Action act = () => type.Should().BeStatic("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected type *.ClassWithoutMembers to be static *failure message*."); + } + + [Theory] + [InlineData(typeof(IDummyInterface), "*.IDummyInterface must be a class.")] + [InlineData(typeof(Struct), "*.Struct must be a class.")] + [InlineData(typeof(ExampleDelegate), "*.ExampleDelegate must be a class.")] + public void When_type_is_not_valid_for_BeStatic_it_throws_exception(Type type, string exceptionMessage) + { + // Act + Action act = () => type.Should().BeStatic(); + + // Assert + act.Should().Throw() + .WithMessage(exceptionMessage); + } + + [Fact] + public void When_subject_is_null_be_static_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().BeStatic("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected type to be static *failure message*, but type is ."); + } } - [Fact] - public void When_type_is_not_static_it_fails_with_a_meaningful_message() - { - // Arrange - var type = typeof(ClassWithoutMembers); - - // Act - Action act = () => type.Should().BeStatic("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type *.ClassWithoutMembers to be static *failure message*."); - } - - [Theory] - [InlineData(typeof(IDummyInterface), "*.IDummyInterface must be a class.")] - [InlineData(typeof(Struct), "*.Struct must be a class.")] - [InlineData(typeof(ExampleDelegate), "*.ExampleDelegate must be a class.")] - public void When_type_is_not_valid_for_BeStatic_it_throws_exception(Type type, string exceptionMessage) - { - // Act - Action act = () => type.Should().BeStatic(); - - // Assert - act.Should().Throw() - .WithMessage(exceptionMessage); - } - - [Fact] - public void When_subject_is_null_be_static_should_fail() - { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().BeStatic("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type to be static *failure message*, but type is ."); - } - - #endregion - - #region NotBeStatic - - [Theory] - [InlineData(typeof(ClassWithoutMembers))] - [InlineData(typeof(Sealed))] - [InlineData(typeof(Abstract))] - public void When_type_is_not_static_it_succeeds(Type type) + public class NotBeStatic { - // Arrange / Act / Assert - type.Should().NotBeStatic(); + [Theory] + [InlineData(typeof(ClassWithoutMembers))] + [InlineData(typeof(Sealed))] + [InlineData(typeof(Abstract))] + public void When_type_is_not_static_it_succeeds(Type type) + { + // Arrange / Act / Assert + type.Should().NotBeStatic(); + } + + [Fact] + public void When_type_is_static_it_fails() + { + // Arrange + var type = typeof(Static); + + // Act + Action act = () => type.Should().NotBeStatic(); + + // Assert + act.Should().Throw() + .WithMessage("Expected type *.Static not to be static."); + } + + [Fact] + public void When_type_is_static_it_fails_with_a_meaningful_message() + { + // Arrange + var type = typeof(Static); + + // Act + Action act = () => type.Should().NotBeStatic("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected type *.Static not to be static *failure message*."); + } + + [Theory] + [InlineData(typeof(IDummyInterface), "*.IDummyInterface must be a class.")] + [InlineData(typeof(Struct), "*.Struct must be a class.")] + [InlineData(typeof(ExampleDelegate), "*.ExampleDelegate must be a class.")] + public void When_type_is_not_valid_for_NotBeStatic_it_throws_exception(Type type, string exceptionMessage) + { + // Act + Action act = () => type.Should().NotBeStatic(); + + // Assert + act.Should().Throw() + .WithMessage(exceptionMessage); + } + + [Fact] + public void When_subject_is_null_not_be_static_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().NotBeStatic("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected type not to be static *failure message*, but type is ."); + } } - - [Fact] - public void When_type_is_static_it_fails() - { - // Arrange - var type = typeof(Static); - - // Act - Action act = () => type.Should().NotBeStatic(); - - // Assert - act.Should().Throw() - .WithMessage("Expected type *.Static not to be static."); - } - - [Fact] - public void When_type_is_static_it_fails_with_a_meaningful_message() - { - // Arrange - var type = typeof(Static); - - // Act - Action act = () => type.Should().NotBeStatic("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type *.Static not to be static *failure message*."); - } - - [Theory] - [InlineData(typeof(IDummyInterface), "*.IDummyInterface must be a class.")] - [InlineData(typeof(Struct), "*.Struct must be a class.")] - [InlineData(typeof(ExampleDelegate), "*.ExampleDelegate must be a class.")] - public void When_type_is_not_valid_for_NotBeStatic_it_throws_exception(Type type, string exceptionMessage) - { - // Act - Action act = () => type.Should().NotBeStatic(); - - // Assert - act.Should().Throw() - .WithMessage(exceptionMessage); - } - - [Fact] - public void When_subject_is_null_not_be_static_should_fail() - { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().NotBeStatic("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type not to be static *failure message*, but type is ."); - } - - #endregion } } diff --git a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveAccessModifier.cs b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveAccessModifier.cs index 8c351a6e76..126f8edf91 100644 --- a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveAccessModifier.cs +++ b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveAccessModifier.cs @@ -11,327 +11,327 @@ namespace FluentAssertions.Specs.Types /// public partial class TypeAssertionSpecs { - public class NotHaveAccessModifier + public class HaveAccessModifier { - public class HaveAccessModifier - { - [Fact] - public void When_asserting_a_public_type_is_public_it_succeeds() - { - // Arrange - Type type = typeof(IPublicInterface); - - // Act - Action act = () => - type.Should().HaveAccessModifier(CSharpAccessModifier.Public); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_public_member_is_internal_it_throws() - { - // Arrange - Type type = typeof(IPublicInterface); - - // Act - Action act = () => - type - .Should() - .HaveAccessModifier(CSharpAccessModifier.Internal, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type IPublicInterface to be Internal *failure message*, but it is Public."); - } - - [Fact] - public void An_internal_class_has_an_internal_access_modifier() - { - // Arrange - Type type = typeof(InternalClass); - - // Act / Assert - type.Should().HaveAccessModifier(CSharpAccessModifier.Internal); - } + [Fact] + public void When_asserting_a_public_type_is_public_it_succeeds() + { + // Arrange + Type type = typeof(IPublicInterface); + + // Act + Action act = () => + type.Should().HaveAccessModifier(CSharpAccessModifier.Public); - [Fact] - public void An_internal_interface_has_an_internal_access_modifier() - { - // Arrange - Type type = typeof(IInternalInterface); + // Assert + act.Should().NotThrow(); + } - // Act / Assert - type.Should().HaveAccessModifier(CSharpAccessModifier.Internal); - } + [Fact] + public void When_asserting_a_public_member_is_internal_it_throws() + { + // Arrange + Type type = typeof(IPublicInterface); - [Fact] - public void An_internal_struct_has_an_internal_access_modifier() - { - // Arrange - Type type = typeof(InternalStruct); + // Act + Action act = () => + type + .Should() + .HaveAccessModifier(CSharpAccessModifier.Internal, "we want to test the failure {0}", "message"); - // Act / Assert - type.Should().HaveAccessModifier(CSharpAccessModifier.Internal); - } + // Assert + act.Should().Throw() + .WithMessage("Expected type IPublicInterface to be Internal *failure message*, but it is Public."); + } + + [Fact] + public void An_internal_class_has_an_internal_access_modifier() + { + // Arrange + Type type = typeof(InternalClass); + + // Act / Assert + type.Should().HaveAccessModifier(CSharpAccessModifier.Internal); + } + + [Fact] + public void An_internal_interface_has_an_internal_access_modifier() + { + // Arrange + Type type = typeof(IInternalInterface); + + // Act / Assert + type.Should().HaveAccessModifier(CSharpAccessModifier.Internal); + } + + [Fact] + public void An_internal_struct_has_an_internal_access_modifier() + { + // Arrange + Type type = typeof(InternalStruct); + + // Act / Assert + type.Should().HaveAccessModifier(CSharpAccessModifier.Internal); + } + + [Fact] + public void An_internal_enum_has_an_internal_access_modifier() + { + // Arrange + Type type = typeof(InternalEnum); + + // Act / Assert + type.Should().HaveAccessModifier(CSharpAccessModifier.Internal); + } + + [Fact] + public void An_internal_class_does_not_have_a_protected_internal_modifier() + { + // Arrange + Type type = typeof(InternalClass); + + // Act + Action act = () => + type.Should().HaveAccessModifier( + CSharpAccessModifier.ProtectedInternal, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected type InternalClass to be ProtectedInternal *failure message*, but it is Internal."); + } + + [Fact] + public void An_internal_interface_does_not_have_a_protected_internal_modifier() + { + // Arrange + Type type = typeof(IInternalInterface); + + // Act + Action act = () => + type.Should().HaveAccessModifier( + CSharpAccessModifier.ProtectedInternal, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type IInternalInterface to be ProtectedInternal *failure message*, but it is Internal."); + } + + [Fact] + public void An_internal_struct_does_not_have_a_protected_internal_modifier() + { + // Arrange + Type type = typeof(InternalStruct); + + // Act + Action act = () => + type.Should().HaveAccessModifier( + CSharpAccessModifier.ProtectedInternal, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected type InternalStruct to be ProtectedInternal *failure message*, but it is Internal."); + } + + [Fact] + public void An_internal_enum_does_not_have_a_protected_internal_modifier() + { + // Arrange + Type type = typeof(InternalEnum); + + // Act + Action act = () => + type.Should().HaveAccessModifier( + CSharpAccessModifier.ProtectedInternal, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected type InternalEnum to be ProtectedInternal *failure message*, but it is Internal."); + } + + [Fact] + public void When_asserting_a_nested_private_type_is_private_it_succeeds() + { + // Arrange + Type type = typeof(Nested).GetNestedType("PrivateClass", BindingFlags.NonPublic | BindingFlags.Instance); + + // Act + Action act = () => + type.Should().HaveAccessModifier(CSharpAccessModifier.Private); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_nested_private_type_is_protected_it_throws() + { + // Arrange + Type type = typeof(Nested).GetNestedType("PrivateClass", BindingFlags.NonPublic | BindingFlags.Instance); + + // Act + Action act = () => + type.Should().HaveAccessModifier(CSharpAccessModifier.Protected, "we want to test the failure {0}", + "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected type PrivateClass to be Protected *failure message*, but it is Private."); + } - [Fact] - public void An_internal_enum_has_an_internal_access_modifier() - { - // Arrange - Type type = typeof(InternalEnum); + [Fact] + public void When_asserting_a_nested_protected_type_is_protected_it_succeeds() + { + // Arrange + Type type = typeof(Nested).GetNestedType("ProtectedEnum", BindingFlags.NonPublic | BindingFlags.Instance); + + // Act + Action act = () => + type.Should().HaveAccessModifier(CSharpAccessModifier.Protected); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_nested_protected_type_is_public_it_throws() + { + // Arrange + Type type = typeof(Nested).GetNestedType("ProtectedEnum", BindingFlags.NonPublic | BindingFlags.Instance); - // Act / Assert + // Act + Action act = () => + type.Should().HaveAccessModifier(CSharpAccessModifier.Public); + + // Assert + act.Should().Throw() + .WithMessage("Expected type ProtectedEnum to be Public, but it is Protected."); + } + + [Fact] + public void When_asserting_a_nested_public_type_is_public_it_succeeds() + { + // Arrange + Type type = typeof(Nested.IPublicInterface); + + // Act + Action act = () => + type.Should().HaveAccessModifier(CSharpAccessModifier.Public); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_nested_public_member_is_internal_it_throws() + { + // Arrange + Type type = typeof(Nested.IPublicInterface); + + // Act + Action act = () => + type + .Should() + .HaveAccessModifier(CSharpAccessModifier.Internal, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected type IPublicInterface to be Internal *failure message*, but it is Public."); + } + + [Fact] + public void When_asserting_a_nested_internal_type_is_internal_it_succeeds() + { + // Arrange + Type type = typeof(Nested.InternalClass); + + // Act + Action act = () => type.Should().HaveAccessModifier(CSharpAccessModifier.Internal); - } - - [Fact] - public void An_internal_class_does_not_have_a_protected_internal_modifier() - { - // Arrange - Type type = typeof(InternalClass); - - // Act - Action act = () => - type.Should().HaveAccessModifier( - CSharpAccessModifier.ProtectedInternal, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type InternalClass to be ProtectedInternal *failure message*, but it is Internal."); - } - - [Fact] - public void An_internal_interface_does_not_have_a_protected_internal_modifier() - { - // Arrange - Type type = typeof(IInternalInterface); - - // Act - Action act = () => - type.Should().HaveAccessModifier( - CSharpAccessModifier.ProtectedInternal, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type IInternalInterface to be ProtectedInternal *failure message*, but it is Internal."); - } - - [Fact] - public void An_internal_struct_does_not_have_a_protected_internal_modifier() - { - // Arrange - Type type = typeof(InternalStruct); - - // Act - Action act = () => - type.Should().HaveAccessModifier( - CSharpAccessModifier.ProtectedInternal, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type InternalStruct to be ProtectedInternal *failure message*, but it is Internal."); - } - - [Fact] - public void An_internal_enum_does_not_have_a_protected_internal_modifier() - { - // Arrange - Type type = typeof(InternalEnum); - - // Act - Action act = () => - type.Should().HaveAccessModifier( - CSharpAccessModifier.ProtectedInternal, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type InternalEnum to be ProtectedInternal *failure message*, but it is Internal."); - } - - [Fact] - public void When_asserting_a_nested_private_type_is_private_it_succeeds() - { - // Arrange - Type type = typeof(Nested).GetNestedType("PrivateClass", BindingFlags.NonPublic | BindingFlags.Instance); - - // Act - Action act = () => - type.Should().HaveAccessModifier(CSharpAccessModifier.Private); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_nested_private_type_is_protected_it_throws() - { - // Arrange - Type type = typeof(Nested).GetNestedType("PrivateClass", BindingFlags.NonPublic | BindingFlags.Instance); - - // Act - Action act = () => - type.Should().HaveAccessModifier(CSharpAccessModifier.Protected, "we want to test the failure {0}", - "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type PrivateClass to be Protected *failure message*, but it is Private."); - } - - [Fact] - public void When_asserting_a_nested_protected_type_is_protected_it_succeeds() - { - // Arrange - Type type = typeof(Nested).GetNestedType("ProtectedEnum", BindingFlags.NonPublic | BindingFlags.Instance); - - // Act - Action act = () => - type.Should().HaveAccessModifier(CSharpAccessModifier.Protected); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_nested_protected_type_is_public_it_throws() - { - // Arrange - Type type = typeof(Nested).GetNestedType("ProtectedEnum", BindingFlags.NonPublic | BindingFlags.Instance); - - // Act - Action act = () => - type.Should().HaveAccessModifier(CSharpAccessModifier.Public); - - // Assert - act.Should().Throw() - .WithMessage("Expected type ProtectedEnum to be Public, but it is Protected."); - } - - [Fact] - public void When_asserting_a_nested_public_type_is_public_it_succeeds() - { - // Arrange - Type type = typeof(Nested.IPublicInterface); - - // Act - Action act = () => - type.Should().HaveAccessModifier(CSharpAccessModifier.Public); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_nested_public_member_is_internal_it_throws() - { - // Arrange - Type type = typeof(Nested.IPublicInterface); - - // Act - Action act = () => - type - .Should() - .HaveAccessModifier(CSharpAccessModifier.Internal, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type IPublicInterface to be Internal *failure message*, but it is Public."); - } - - [Fact] - public void When_asserting_a_nested_internal_type_is_internal_it_succeeds() - { - // Arrange - Type type = typeof(Nested.InternalClass); - - // Act - Action act = () => - type.Should().HaveAccessModifier(CSharpAccessModifier.Internal); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_nested_internal_type_is_protected_internal_it_throws() - { - // Arrange - Type type = typeof(Nested.InternalClass); - - // Act - Action act = () => - type.Should().HaveAccessModifier( - CSharpAccessModifier.ProtectedInternal, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type InternalClass to be ProtectedInternal *failure message*, but it is Internal."); - } - - [Fact] - public void When_asserting_a_nested_protected_internal_member_is_protected_internal_it_succeeds() - { - // Arrange - Type type = typeof(Nested.IProtectedInternalInterface); - - // Act - Action act = () => - type.Should().HaveAccessModifier(CSharpAccessModifier.ProtectedInternal); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_nested_protected_internal_member_is_private_it_throws() - { - // Arrange - Type type = typeof(Nested.IProtectedInternalInterface); - - // Act - Action act = () => - type.Should().HaveAccessModifier(CSharpAccessModifier.Private, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type IProtectedInternalInterface to be Private *failure message*, but it is ProtectedInternal."); - } - - [Fact] - public void When_subject_is_null_have_access_modifier_should_fail() - { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().HaveAccessModifier(CSharpAccessModifier.Public, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type to be Public *failure message*, but type is ."); - } - - [Fact] - public void When_asserting_a_type_has_an_access_modifier_with_an_invalid_enum_value_it_should_throw() - { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().HaveAccessModifier((CSharpAccessModifier)int.MaxValue); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("accessModifier"); - } + + // Assert + act.Should().NotThrow(); } + [Fact] + public void When_asserting_a_nested_internal_type_is_protected_internal_it_throws() + { + // Arrange + Type type = typeof(Nested.InternalClass); + + // Act + Action act = () => + type.Should().HaveAccessModifier( + CSharpAccessModifier.ProtectedInternal, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected type InternalClass to be ProtectedInternal *failure message*, but it is Internal."); + } + + [Fact] + public void When_asserting_a_nested_protected_internal_member_is_protected_internal_it_succeeds() + { + // Arrange + Type type = typeof(Nested.IProtectedInternalInterface); + + // Act + Action act = () => + type.Should().HaveAccessModifier(CSharpAccessModifier.ProtectedInternal); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_nested_protected_internal_member_is_private_it_throws() + { + // Arrange + Type type = typeof(Nested.IProtectedInternalInterface); + + // Act + Action act = () => + type.Should().HaveAccessModifier(CSharpAccessModifier.Private, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type IProtectedInternalInterface to be Private *failure message*, but it is ProtectedInternal."); + } + + [Fact] + public void When_subject_is_null_have_access_modifier_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().HaveAccessModifier(CSharpAccessModifier.Public, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected type to be Public *failure message*, but type is ."); + } + + [Fact] + public void When_asserting_a_type_has_an_access_modifier_with_an_invalid_enum_value_it_should_throw() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().HaveAccessModifier((CSharpAccessModifier)int.MaxValue); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("accessModifier"); + } + } + + public class NotHaveAccessModifier + { [Fact] public void When_asserting_a_public_type_is_not_private_it_succeeds() { diff --git a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveConstructor.cs b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveConstructor.cs index 2c422ebf31..a9f865e790 100644 --- a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveConstructor.cs +++ b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveConstructor.cs @@ -10,137 +10,135 @@ namespace FluentAssertions.Specs.Types /// public partial class TypeAssertionSpecs { - #region HaveConstructor - - [Fact] - public void When_asserting_a_type_has_a_constructor_which_it_does_it_succeeds() - { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should() - .HaveConstructor(new Type[] { typeof(string) }) - .Which.Should() - .HaveAccessModifier(CSharpAccessModifier.Private); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_type_has_a_constructor_which_it_does_not_it_fails() - { - // Arrange - var type = typeof(ClassWithNoMembers); - - // Act - Action act = () => - type.Should().HaveConstructor(new[] { typeof(int), typeof(Type) }, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected constructor *ClassWithNoMembers(System.Int32, System.Type) to exist *failure message*" + - ", but it does not."); - } - - [Fact] - public void When_subject_is_null_have_constructor_should_fail() - { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().HaveConstructor(new[] { typeof(string) }, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected constructor type(System.String) to exist *failure message*, but type is ."); - } - - [Fact] - public void When_asserting_a_type_has_a_constructor_of_null_it_should_throw() - { - // Arrange - Type type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should().HaveConstructor(null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("parameterTypes"); - } - - #endregion - - #region NotHaveConstructor - - [Fact] - public void When_asserting_a_type_does_not_have_a_constructor_which_it_does_not_it_succeeds() + public class HaveConstructor { - // Arrange - var type = typeof(ClassWithNoMembers); - - // Act - Action act = () => - type.Should() - .NotHaveConstructor(new Type[] { typeof(string) }); - - // Assert - act.Should().NotThrow(); + [Fact] + public void When_asserting_a_type_has_a_constructor_which_it_does_it_succeeds() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should() + .HaveConstructor(new Type[] { typeof(string) }) + .Which.Should() + .HaveAccessModifier(CSharpAccessModifier.Private); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_has_a_constructor_which_it_does_not_it_fails() + { + // Arrange + var type = typeof(ClassWithNoMembers); + + // Act + Action act = () => + type.Should().HaveConstructor(new[] { typeof(int), typeof(Type) }, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected constructor *ClassWithNoMembers(System.Int32, System.Type) to exist *failure message*" + + ", but it does not."); + } + + [Fact] + public void When_subject_is_null_have_constructor_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().HaveConstructor(new[] { typeof(string) }, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected constructor type(System.String) to exist *failure message*, but type is ."); + } + + [Fact] + public void When_asserting_a_type_has_a_constructor_of_null_it_should_throw() + { + // Arrange + Type type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should().HaveConstructor(null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("parameterTypes"); + } } - [Fact] - public void When_asserting_a_type_does_not_have_a_constructor_which_it_does_it_fails() + public class NotHaveConstructor { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should().NotHaveConstructor(new[] { typeof(string) }, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected constructor *.ClassWithMembers(System.String) not to exist *failure message*, but it does."); + [Fact] + public void When_asserting_a_type_does_not_have_a_constructor_which_it_does_not_it_succeeds() + { + // Arrange + var type = typeof(ClassWithNoMembers); + + // Act + Action act = () => + type.Should() + .NotHaveConstructor(new Type[] { typeof(string) }); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_does_not_have_a_constructor_which_it_does_it_fails() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should().NotHaveConstructor(new[] { typeof(string) }, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected constructor *.ClassWithMembers(System.String) not to exist *failure message*, but it does."); + } + + [Fact] + public void When_subject_is_null_not_have_constructor_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().NotHaveConstructor(new[] { typeof(string) }, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected constructor type(System.String) not to exist *failure message*, but type is ."); + } + + [Fact] + public void When_asserting_a_type_does_not_have_a_constructor_of_null_it_should_throw() + { + // Arrange + Type type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should().NotHaveConstructor(null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("parameterTypes"); + } } - - [Fact] - public void When_subject_is_null_not_have_constructor_should_fail() - { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().NotHaveConstructor(new[] { typeof(string) }, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected constructor type(System.String) not to exist *failure message*, but type is ."); - } - - [Fact] - public void When_asserting_a_type_does_not_have_a_constructor_of_null_it_should_throw() - { - // Arrange - Type type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should().NotHaveConstructor(null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("parameterTypes"); - } - - #endregion } } diff --git a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveDefaultConstructor.cs b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveDefaultConstructor.cs index 7f4bd5b371..4b59e38483 100644 --- a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveDefaultConstructor.cs +++ b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveDefaultConstructor.cs @@ -10,176 +10,174 @@ namespace FluentAssertions.Specs.Types /// public partial class TypeAssertionSpecs { - #region HaveDefaultConstructor - - [Fact] - public void When_asserting_a_type_has_a_default_constructor_which_it_does_it_succeeds() + public class HaveDefaultConstructor { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => + [Fact] + public void When_asserting_a_type_has_a_default_constructor_which_it_does_it_succeeds() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should() + .HaveDefaultConstructor() + .Which.Should() + .HaveAccessModifier(CSharpAccessModifier.ProtectedInternal); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_has_a_default_constructor_which_it_does_not_it_succeeds() + { + // Arrange + var type = typeof(ClassWithNoMembers); + + // Act + Action act = () => + type.Should() + .HaveDefaultConstructor() + .Which.Should() + .HaveAccessModifier(CSharpAccessModifier.Public); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_has_a_default_constructor_which_it_does_not_and_a_cctor_it_succeeds() + { + // Arrange + var type = typeof(ClassWithCctor); + + // Act type.Should() - .HaveDefaultConstructor() - .Which.Should() - .HaveAccessModifier(CSharpAccessModifier.ProtectedInternal); - - // Assert - act.Should().NotThrow(); + .HaveDefaultConstructor() + .Which.Should() + .HaveAccessModifier(CSharpAccessModifier.Public); + Action act = () => + type.Should() + .HaveDefaultConstructor() + .Which.Should() + .HaveAccessModifier(CSharpAccessModifier.Public); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_has_a_default_constructor_which_it_does_not_and_a_cctor_it_fails() + { + // Arrange + var type = typeof(ClassWithCctorAndNonDefaultConstructor); + + // Act + Action act = () => + type.Should().HaveDefaultConstructor("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected constructor *ClassWithCctorAndNonDefaultConstructor() to exist *failure message*" + + ", but it does not."); + } + + [Fact] + public void When_subject_is_null_have_default_constructor_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().HaveDefaultConstructor("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected constructor type() to exist *failure message*, but type is ."); + } } - [Fact] - public void When_asserting_a_type_has_a_default_constructor_which_it_does_not_it_succeeds() + public class NotHaveDefaultConstructor { - // Arrange - var type = typeof(ClassWithNoMembers); - - // Act - Action act = () => - type.Should() - .HaveDefaultConstructor() - .Which.Should() - .HaveAccessModifier(CSharpAccessModifier.Public); - - // Assert - act.Should().NotThrow(); + [Fact] + public void When_asserting_a_type_does_not_have_a_default_constructor_which_it_does_not_it_succeeds() + { + // Arrange + var type = typeof(ClassWithCctorAndNonDefaultConstructor); + + // Act + Action act = () => + type.Should() + .NotHaveDefaultConstructor(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_does_not_have_a_default_constructor_which_it_does_it_fails() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should() + .NotHaveDefaultConstructor("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected constructor *.ClassWithMembers() not to exist *failure message*, but it does."); + } + + [Fact] + public void When_asserting_a_type_does_not_have_a_default_constructor_which_it_does_and_a_cctor_it_fails() + { + // Arrange + var type = typeof(ClassWithCctor); + + // Act + Action act = () => + type.Should().NotHaveDefaultConstructor("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected constructor *ClassWithCctor*() not to exist *failure message*, but it does."); + } + + [Fact] + public void When_asserting_a_type_does_not_have_a_default_constructor_which_it_does_not_and_a_cctor_it_succeeds() + { + // Arrange + var type = typeof(ClassWithCctorAndNonDefaultConstructor); + + // Act + Action act = () => + type.Should().NotHaveDefaultConstructor(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_subject_is_null_not_have_default_constructor_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().NotHaveDefaultConstructor("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected constructor type() not to exist *failure message*, but type is ."); + } } - [Fact] - public void When_asserting_a_type_has_a_default_constructor_which_it_does_not_and_a_cctor_it_succeeds() - { - // Arrange - var type = typeof(ClassWithCctor); - - // Act - type.Should() - .HaveDefaultConstructor() - .Which.Should() - .HaveAccessModifier(CSharpAccessModifier.Public); - Action act = () => - type.Should() - .HaveDefaultConstructor() - .Which.Should() - .HaveAccessModifier(CSharpAccessModifier.Public); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_type_has_a_default_constructor_which_it_does_not_and_a_cctor_it_fails() - { - // Arrange - var type = typeof(ClassWithCctorAndNonDefaultConstructor); - - // Act - Action act = () => - type.Should().HaveDefaultConstructor("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected constructor *ClassWithCctorAndNonDefaultConstructor() to exist *failure message*" + - ", but it does not."); - } - - [Fact] - public void When_subject_is_null_have_default_constructor_should_fail() - { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().HaveDefaultConstructor("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected constructor type() to exist *failure message*, but type is ."); - } - - #endregion - - #region NotHaveDefaultConstructor - - [Fact] - public void When_asserting_a_type_does_not_have_a_default_constructor_which_it_does_not_it_succeeds() - { - // Arrange - var type = typeof(ClassWithCctorAndNonDefaultConstructor); - - // Act - Action act = () => - type.Should() - .NotHaveDefaultConstructor(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_type_does_not_have_a_default_constructor_which_it_does_it_fails() - { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should() - .NotHaveDefaultConstructor("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected constructor *.ClassWithMembers() not to exist *failure message*, but it does."); - } - - [Fact] - public void When_asserting_a_type_does_not_have_a_default_constructor_which_it_does_and_a_cctor_it_fails() - { - // Arrange - var type = typeof(ClassWithCctor); - - // Act - Action act = () => - type.Should().NotHaveDefaultConstructor("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected constructor *ClassWithCctor*() not to exist *failure message*, but it does."); - } - - [Fact] - public void When_asserting_a_type_does_not_have_a_default_constructor_which_it_does_not_and_a_cctor_it_succeeds() - { - // Arrange - var type = typeof(ClassWithCctorAndNonDefaultConstructor); - - // Act - Action act = () => - type.Should().NotHaveDefaultConstructor(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_subject_is_null_not_have_default_constructor_should_fail() - { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().NotHaveDefaultConstructor("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected constructor type() not to exist *failure message*, but type is ."); - } - - #endregion - internal class ClassWithNoMembers { } diff --git a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveExplicitConversionOperator.cs b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveExplicitConversionOperator.cs index 827a5c1801..84ed2ef5d1 100644 --- a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveExplicitConversionOperator.cs +++ b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveExplicitConversionOperator.cs @@ -9,278 +9,274 @@ namespace FluentAssertions.Specs.Types /// public partial class TypeAssertionSpecs { - #region HaveExplicitConversionOperator - - [Fact] - public void When_asserting_a_type_has_an_explicit_conversion_operator_which_it_does_it_succeeds() - { - // Arrange - var type = typeof(TypeWithConversionOperators); - var sourceType = typeof(TypeWithConversionOperators); - var targetType = typeof(byte); - - // Act - Action act = () => - type.Should() - .HaveExplicitConversionOperator(sourceType, targetType) - .Which.Should() - .NotBeNull(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_type_has_an_explicit_conversion_operator_which_it_does_not_it_fails_with_a_useful_message() - { - // Arrange - var type = typeof(TypeWithConversionOperators); - var sourceType = typeof(TypeWithConversionOperators); - var targetType = typeof(string); - - // Act - Action act = () => - type.Should().HaveExplicitConversionOperator( - sourceType, targetType, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected public static explicit System.String(*.TypeWithConversionOperators) to exist *failure message*" + - ", but it does not."); - } - - [Fact] - public void When_subject_is_null_have_explicit_conversion_operator_should_fail() - { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().HaveExplicitConversionOperator( - typeof(TypeWithConversionOperators), typeof(string), "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected public static explicit System.String(*.TypeWithConversionOperators) to exist *failure message*" + - ", but type is ."); - } - - [Fact] - public void When_asserting_a_type_has_an_explicit_conversion_operator_from_null_it_should_throw() - { - // Arrange - var type = typeof(TypeWithConversionOperators); - - // Act - Action act = () => - type.Should().HaveExplicitConversionOperator(null, typeof(string)); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("sourceType"); - } - - [Fact] - public void When_asserting_a_type_has_an_explicit_conversion_operator_to_null_it_should_throw() + public class HaveExplicitConversionOperator { - // Arrange - var type = typeof(TypeWithConversionOperators); - - // Act - Action act = () => - type.Should().HaveExplicitConversionOperator(typeof(TypeWithConversionOperators), null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("targetType"); + [Fact] + public void When_asserting_a_type_has_an_explicit_conversion_operator_which_it_does_it_succeeds() + { + // Arrange + var type = typeof(TypeWithConversionOperators); + var sourceType = typeof(TypeWithConversionOperators); + var targetType = typeof(byte); + + // Act + Action act = () => + type.Should() + .HaveExplicitConversionOperator(sourceType, targetType) + .Which.Should() + .NotBeNull(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_has_an_explicit_conversion_operator_which_it_does_not_it_fails_with_a_useful_message() + { + // Arrange + var type = typeof(TypeWithConversionOperators); + var sourceType = typeof(TypeWithConversionOperators); + var targetType = typeof(string); + + // Act + Action act = () => + type.Should().HaveExplicitConversionOperator( + sourceType, targetType, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected public static explicit System.String(*.TypeWithConversionOperators) to exist *failure message*" + + ", but it does not."); + } + + [Fact] + public void When_subject_is_null_have_explicit_conversion_operator_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().HaveExplicitConversionOperator( + typeof(TypeWithConversionOperators), typeof(string), "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected public static explicit System.String(*.TypeWithConversionOperators) to exist *failure message*" + + ", but type is ."); + } + + [Fact] + public void When_asserting_a_type_has_an_explicit_conversion_operator_from_null_it_should_throw() + { + // Arrange + var type = typeof(TypeWithConversionOperators); + + // Act + Action act = () => + type.Should().HaveExplicitConversionOperator(null, typeof(string)); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("sourceType"); + } + + [Fact] + public void When_asserting_a_type_has_an_explicit_conversion_operator_to_null_it_should_throw() + { + // Arrange + var type = typeof(TypeWithConversionOperators); + + // Act + Action act = () => + type.Should().HaveExplicitConversionOperator(typeof(TypeWithConversionOperators), null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("targetType"); + } } - #endregion - - #region HaveExplicitConversionOperatorOfT - - [Fact] - public void When_asserting_a_type_has_an_explicit_conversion_operatorOfT_which_it_does_it_succeeds() + public class HaveExplicitConversionOperatorOfT { - // Arrange - var type = typeof(TypeWithConversionOperators); - - // Act - Action act = () => - type.Should() - .HaveExplicitConversionOperator() - .Which.Should() - .NotBeNull(); - - // Assert - act.Should().NotThrow(); + [Fact] + public void When_asserting_a_type_has_an_explicit_conversion_operatorOfT_which_it_does_it_succeeds() + { + // Arrange + var type = typeof(TypeWithConversionOperators); + + // Act + Action act = () => + type.Should() + .HaveExplicitConversionOperator() + .Which.Should() + .NotBeNull(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_has_an_explicit_conversion_operatorOfT_which_it_does_not_it_fails() + { + // Arrange + var type = typeof(TypeWithConversionOperators); + + // Act + Action act = () => + type.Should().HaveExplicitConversionOperator( + "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected public static explicit System.String(*.TypeWithConversionOperators) to exist *failure message*" + + ", but it does not."); + } + + [Fact] + public void When_subject_is_null_have_explicit_conversion_operatorOfT_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().HaveExplicitConversionOperator( + "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected public static explicit System.String(*.TypeWithConversionOperators) to exist *failure message*" + + ", but type is ."); + } } - [Fact] - public void When_asserting_a_type_has_an_explicit_conversion_operatorOfT_which_it_does_not_it_fails() + public class NotHaveExplicitConversionOperator { - // Arrange - var type = typeof(TypeWithConversionOperators); - - // Act - Action act = () => - type.Should().HaveExplicitConversionOperator( - "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected public static explicit System.String(*.TypeWithConversionOperators) to exist *failure message*" + - ", but it does not."); + [Fact] + public void When_asserting_a_type_does_not_have_an_explicit_conversion_operator_which_it_does_not_it_succeeds() + { + // Arrange + var type = typeof(TypeWithConversionOperators); + var sourceType = typeof(TypeWithConversionOperators); + var targetType = typeof(string); + + // Act + Action act = () => + type.Should() + .NotHaveExplicitConversionOperator(sourceType, targetType); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_does_not_have_an_explicit_conversion_operator_which_it_does_it_fails() + { + // Arrange + var type = typeof(TypeWithConversionOperators); + var sourceType = typeof(TypeWithConversionOperators); + var targetType = typeof(byte); + + // Act + Action act = () => + type.Should().NotHaveExplicitConversionOperator( + sourceType, targetType, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected public static explicit *.Byte(*.TypeWithConversionOperators) to not exist *failure message*" + + ", but it does."); + } + + [Fact] + public void When_subject_is_null_not_have_explicit_conversion_operator_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().NotHaveExplicitConversionOperator( + typeof(TypeWithConversionOperators), typeof(string), "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected public static explicit *.String(*.TypeWithConversionOperators) to not exist *failure message*" + + ", but type is ."); + } + + [Fact] + public void When_asserting_a_type_does_not_have_an_explicit_conversion_operator_from_null_it_should_throw() + { + // Arrange + var type = typeof(TypeWithConversionOperators); + + // Act + Action act = () => + type.Should().NotHaveExplicitConversionOperator(null, typeof(string)); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("sourceType"); + } + + [Fact] + public void When_asserting_a_type_does_not_have_an_explicit_conversion_operator_to_null_it_should_throw() + { + // Arrange + var type = typeof(TypeWithConversionOperators); + + // Act + Action act = () => + type.Should().NotHaveExplicitConversionOperator(typeof(TypeWithConversionOperators), null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("targetType"); + } } - [Fact] - public void When_subject_is_null_have_explicit_conversion_operatorOfT_should_fail() + public class NotHaveExplicitConversionOperatorOfT { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().HaveExplicitConversionOperator( - "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected public static explicit System.String(*.TypeWithConversionOperators) to exist *failure message*" + - ", but type is ."); + [Fact] + public void When_asserting_a_type_does_not_have_an_explicit_conversion_operatorOfT_which_it_does_not_it_succeeds() + { + // Arrange + var type = typeof(TypeWithConversionOperators); + + // Act + Action act = () => + type.Should() + .NotHaveExplicitConversionOperator(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_does_not_have_an_explicit_conversion_operatorOfT_which_it_does_it_fails() + { + // Arrange + var type = typeof(TypeWithConversionOperators); + + // Act + Action act = () => + type.Should().NotHaveExplicitConversionOperator( + "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected public static explicit *.Byte(*.TypeWithConversionOperators) to not exist *failure message*" + + ", but it does."); + } } - - #endregion - - #region NotHaveExplicitConversionOperator - - [Fact] - public void When_asserting_a_type_does_not_have_an_explicit_conversion_operator_which_it_does_not_it_succeeds() - { - // Arrange - var type = typeof(TypeWithConversionOperators); - var sourceType = typeof(TypeWithConversionOperators); - var targetType = typeof(string); - - // Act - Action act = () => - type.Should() - .NotHaveExplicitConversionOperator(sourceType, targetType); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_type_does_not_have_an_explicit_conversion_operator_which_it_does_it_fails() - { - // Arrange - var type = typeof(TypeWithConversionOperators); - var sourceType = typeof(TypeWithConversionOperators); - var targetType = typeof(byte); - - // Act - Action act = () => - type.Should().NotHaveExplicitConversionOperator( - sourceType, targetType, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected public static explicit *.Byte(*.TypeWithConversionOperators) to not exist *failure message*" + - ", but it does."); - } - - [Fact] - public void When_subject_is_null_not_have_explicit_conversion_operator_should_fail() - { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().NotHaveExplicitConversionOperator( - typeof(TypeWithConversionOperators), typeof(string), "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected public static explicit *.String(*.TypeWithConversionOperators) to not exist *failure message*" + - ", but type is ."); - } - - [Fact] - public void When_asserting_a_type_does_not_have_an_explicit_conversion_operator_from_null_it_should_throw() - { - // Arrange - var type = typeof(TypeWithConversionOperators); - - // Act - Action act = () => - type.Should().NotHaveExplicitConversionOperator(null, typeof(string)); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("sourceType"); - } - - [Fact] - public void When_asserting_a_type_does_not_have_an_explicit_conversion_operator_to_null_it_should_throw() - { - // Arrange - var type = typeof(TypeWithConversionOperators); - - // Act - Action act = () => - type.Should().NotHaveExplicitConversionOperator(typeof(TypeWithConversionOperators), null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("targetType"); - } - - #endregion - - #region NotHaveExplicitConversionOperatorOfT - - [Fact] - public void When_asserting_a_type_does_not_have_an_explicit_conversion_operatorOfT_which_it_does_not_it_succeeds() - { - // Arrange - var type = typeof(TypeWithConversionOperators); - - // Act - Action act = () => - type.Should() - .NotHaveExplicitConversionOperator(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_type_does_not_have_an_explicit_conversion_operatorOfT_which_it_does_it_fails() - { - // Arrange - var type = typeof(TypeWithConversionOperators); - - // Act - Action act = () => - type.Should().NotHaveExplicitConversionOperator( - "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected public static explicit *.Byte(*.TypeWithConversionOperators) to not exist *failure message*" + - ", but it does."); - } - - #endregion } } diff --git a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveExplicitMethod.cs b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveExplicitMethod.cs index 0c54efb6dc..7f3c955ee2 100644 --- a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveExplicitMethod.cs +++ b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveExplicitMethod.cs @@ -9,523 +9,519 @@ namespace FluentAssertions.Specs.Types /// public partial class TypeAssertionSpecs { - #region HaveExplicitMethod - - [Fact] - public void When_asserting_a_type_explicitly_implements_a_method_which_it_does_it_succeeds() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - var interfaceType = typeof(IExplicitInterface); - - // Act - Action act = () => - type.Should() - .HaveExplicitMethod(interfaceType, "ExplicitMethod", new Type[0]); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_type_explicitly_implements_a_method_which_it_implements_implicitly_and_explicitly_it_succeeds() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - var interfaceType = typeof(IExplicitInterface); - - // Act - Action act = () => - type.Should() - .HaveExplicitMethod(interfaceType, "ExplicitImplicitMethod", new Type[0]); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_type_explicitly_implements_a_method_which_it_implements_implicitly_it_fails() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - var interfaceType = typeof(IExplicitInterface); - - // Act - Action act = () => - type.Should() - .HaveExplicitMethod(interfaceType, "ImplicitMethod", new Type[0]); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected *.ClassExplicitlyImplementingInterface to explicitly implement " + - "*.IExplicitInterface.ImplicitMethod(), but it does not."); - } - - [Fact] - public void When_asserting_a_type_explicitly_implements_a_method_which_it_does_not_implement_it_fails() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - var interfaceType = typeof(IExplicitInterface); - - // Act - Action act = () => - type.Should() - .HaveExplicitMethod(interfaceType, "NonExistentMethod", new Type[0]); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected *.ClassExplicitlyImplementingInterface to explicitly implement " + - "*.IExplicitInterface.NonExistentMethod(), but it does not."); - } - - [Fact] - public void When_asserting_a_type_explicitly_implements_a_method_from_an_unimplemented_interface_it_fails() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - var interfaceType = typeof(IDummyInterface); - - // Act - Action act = () => - type.Should() - .HaveExplicitMethod(interfaceType, "NonExistentProperty", new Type[0]); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type *.ClassExplicitlyImplementingInterface to implement interface " + - "*.IDummyInterface, but it does not."); - } - - [Fact] - public void When_subject_is_null_have_explicit_method_should_fail() - { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().HaveExplicitMethod( - typeof(IExplicitInterface), "ExplicitMethod", new Type[0], "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type to explicitly implement *.IExplicitInterface.ExplicitMethod() *failure message*" + - ", but type is ."); - } - - [Fact] - public void When_asserting_a_type_has_an_explicit_method_with_a_null_interface_it_should_throw() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - // Act - Action act = () => - type.Should().HaveExplicitMethod(null, "ExplicitMethod", new Type[0]); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("interfaceType"); - } - - [Fact] - public void When_asserting_a_type_has_an_explicit_method_with_a_null_parameter_type_list_it_should_throw() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - // Act - Action act = () => - type.Should().HaveExplicitMethod(typeof(IExplicitInterface), "ExplicitMethod", null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("parameterTypes"); - } - - [Fact] - public void When_asserting_a_type_has_an_explicit_method_with_a_null_name_it_should_throw() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - // Act - Action act = () => - type.Should().HaveExplicitMethod(typeof(IExplicitInterface), null, new Type[0]); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("name"); - } - - [Fact] - public void When_asserting_a_type_has_an_explicit_method_with_an_empty_name_it_should_throw() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - // Act - Action act = () => - type.Should().HaveExplicitMethod(typeof(IExplicitInterface), string.Empty, new Type[0]); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("name"); - } - - #endregion - - #region HaveExplicitMethodOfT - - [Fact] - public void When_asserting_a_type_explicitly_implementsOfT_a_method_which_it_does_it_succeeds() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - // Act - Action act = () => - type.Should() - .HaveExplicitMethod("ExplicitMethod", new Type[0]); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_subject_is_null_have_explicit_methodOfT_should_fail() - { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().HaveExplicitMethod( - "ExplicitMethod", new Type[0], "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type to explicitly implement *.IExplicitInterface.ExplicitMethod() *failure message*" + - ", but type is ."); - } - - [Fact] - public void When_asserting_a_type_has_an_explicit_methodOfT_with_a_null_parameter_type_list_it_should_throw() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - // Act - Action act = () => - type.Should().HaveExplicitMethod("ExplicitMethod", null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("parameterTypes"); - } - - [Fact] - public void When_asserting_a_type_has_an_explicit_methodOfT_with_a_null_name_it_should_throw() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - // Act - Action act = () => - type.Should().HaveExplicitMethod(null, new Type[0]); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("name"); - } - - [Fact] - public void When_asserting_a_type_has_an_explicit_methodOfT_with_an_empty_name_it_should_throw() + public class HaveExplicitMethod { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - // Act - Action act = () => - type.Should().HaveExplicitMethod(string.Empty, new Type[0]); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("name"); + [Fact] + public void When_asserting_a_type_explicitly_implements_a_method_which_it_does_it_succeeds() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + var interfaceType = typeof(IExplicitInterface); + + // Act + Action act = () => + type.Should() + .HaveExplicitMethod(interfaceType, "ExplicitMethod", new Type[0]); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_explicitly_implements_a_method_which_it_implements_implicitly_and_explicitly_it_succeeds() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + var interfaceType = typeof(IExplicitInterface); + + // Act + Action act = () => + type.Should() + .HaveExplicitMethod(interfaceType, "ExplicitImplicitMethod", new Type[0]); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_explicitly_implements_a_method_which_it_implements_implicitly_it_fails() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + var interfaceType = typeof(IExplicitInterface); + + // Act + Action act = () => + type.Should() + .HaveExplicitMethod(interfaceType, "ImplicitMethod", new Type[0]); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected *.ClassExplicitlyImplementingInterface to explicitly implement " + + "*.IExplicitInterface.ImplicitMethod(), but it does not."); + } + + [Fact] + public void When_asserting_a_type_explicitly_implements_a_method_which_it_does_not_implement_it_fails() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + var interfaceType = typeof(IExplicitInterface); + + // Act + Action act = () => + type.Should() + .HaveExplicitMethod(interfaceType, "NonExistentMethod", new Type[0]); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected *.ClassExplicitlyImplementingInterface to explicitly implement " + + "*.IExplicitInterface.NonExistentMethod(), but it does not."); + } + + [Fact] + public void When_asserting_a_type_explicitly_implements_a_method_from_an_unimplemented_interface_it_fails() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + var interfaceType = typeof(IDummyInterface); + + // Act + Action act = () => + type.Should() + .HaveExplicitMethod(interfaceType, "NonExistentProperty", new Type[0]); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type *.ClassExplicitlyImplementingInterface to implement interface " + + "*.IDummyInterface, but it does not."); + } + + [Fact] + public void When_subject_is_null_have_explicit_method_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().HaveExplicitMethod( + typeof(IExplicitInterface), "ExplicitMethod", new Type[0], "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type to explicitly implement *.IExplicitInterface.ExplicitMethod() *failure message*" + + ", but type is ."); + } + + [Fact] + public void When_asserting_a_type_has_an_explicit_method_with_a_null_interface_it_should_throw() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + // Act + Action act = () => + type.Should().HaveExplicitMethod(null, "ExplicitMethod", new Type[0]); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("interfaceType"); + } + + [Fact] + public void When_asserting_a_type_has_an_explicit_method_with_a_null_parameter_type_list_it_should_throw() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + // Act + Action act = () => + type.Should().HaveExplicitMethod(typeof(IExplicitInterface), "ExplicitMethod", null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("parameterTypes"); + } + + [Fact] + public void When_asserting_a_type_has_an_explicit_method_with_a_null_name_it_should_throw() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + // Act + Action act = () => + type.Should().HaveExplicitMethod(typeof(IExplicitInterface), null, new Type[0]); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("name"); + } + + [Fact] + public void When_asserting_a_type_has_an_explicit_method_with_an_empty_name_it_should_throw() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + // Act + Action act = () => + type.Should().HaveExplicitMethod(typeof(IExplicitInterface), string.Empty, new Type[0]); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("name"); + } } - #endregion - - #region NotHaveExplicitMethod - - [Fact] - public void When_asserting_a_type_does_not_explicitly_implement_a_method_which_it_does_it_fails() + public class HaveExplicitMethodOfT { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - var interfaceType = typeof(IExplicitInterface); - - // Act - Action act = () => - type.Should() - .NotHaveExplicitMethod(interfaceType, "ExplicitMethod", new Type[0]); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected *.ClassExplicitlyImplementingInterface to not explicitly implement " + - "*.IExplicitInterface.ExplicitMethod(), but it does."); + [Fact] + public void When_asserting_a_type_explicitly_implementsOfT_a_method_which_it_does_it_succeeds() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + // Act + Action act = () => + type.Should() + .HaveExplicitMethod("ExplicitMethod", new Type[0]); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_subject_is_null_have_explicit_methodOfT_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().HaveExplicitMethod( + "ExplicitMethod", new Type[0], "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type to explicitly implement *.IExplicitInterface.ExplicitMethod() *failure message*" + + ", but type is ."); + } + + [Fact] + public void When_asserting_a_type_has_an_explicit_methodOfT_with_a_null_parameter_type_list_it_should_throw() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + // Act + Action act = () => + type.Should().HaveExplicitMethod("ExplicitMethod", null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("parameterTypes"); + } + + [Fact] + public void When_asserting_a_type_has_an_explicit_methodOfT_with_a_null_name_it_should_throw() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + // Act + Action act = () => + type.Should().HaveExplicitMethod(null, new Type[0]); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("name"); + } + + [Fact] + public void When_asserting_a_type_has_an_explicit_methodOfT_with_an_empty_name_it_should_throw() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + // Act + Action act = () => + type.Should().HaveExplicitMethod(string.Empty, new Type[0]); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("name"); + } } - [Fact] - public void When_asserting_a_type_does_not_explicitly_implement_a_method_which_it_implements_implicitly_and_explicitly_it_fails() + public class NotHaveExplicitMethod { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - var interfaceType = typeof(IExplicitInterface); - - // Act - Action act = () => - type.Should() - .NotHaveExplicitMethod(interfaceType, "ExplicitImplicitMethod", new Type[0]); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected *.ClassExplicitlyImplementingInterface to not explicitly implement " + - "*.IExplicitInterface.ExplicitImplicitMethod(), but it does."); + [Fact] + public void When_asserting_a_type_does_not_explicitly_implement_a_method_which_it_does_it_fails() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + var interfaceType = typeof(IExplicitInterface); + + // Act + Action act = () => + type.Should() + .NotHaveExplicitMethod(interfaceType, "ExplicitMethod", new Type[0]); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected *.ClassExplicitlyImplementingInterface to not explicitly implement " + + "*.IExplicitInterface.ExplicitMethod(), but it does."); + } + + [Fact] + public void When_asserting_a_type_does_not_explicitly_implement_a_method_which_it_implements_implicitly_and_explicitly_it_fails() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + var interfaceType = typeof(IExplicitInterface); + + // Act + Action act = () => + type.Should() + .NotHaveExplicitMethod(interfaceType, "ExplicitImplicitMethod", new Type[0]); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected *.ClassExplicitlyImplementingInterface to not explicitly implement " + + "*.IExplicitInterface.ExplicitImplicitMethod(), but it does."); + } + + [Fact] + public void When_asserting_a_type_does_not_explicitly_implement_a_method_which_it_implements_implicitly_it_succeeds() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + var interfaceType = typeof(IExplicitInterface); + + // Act + Action act = () => + type.Should() + .NotHaveExplicitMethod(interfaceType, "ImplicitMethod", new Type[0]); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_does_not_explicitly_implement_a_method_which_it_does_not_implement_it_succeeds() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + var interfaceType = typeof(IExplicitInterface); + + // Act + Action act = () => + type.Should() + .NotHaveExplicitMethod(interfaceType, "NonExistentMethod", new Type[0]); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_does_not_explicitly_implement_a_method_from_an_unimplemented_interface_it_succeeds() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + var interfaceType = typeof(IDummyInterface); + + // Act + Action act = () => + type.Should() + .NotHaveExplicitMethod(interfaceType, "NonExistentMethod", new Type[0]); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type *.ClassExplicitlyImplementingInterface to implement interface *.IDummyInterface" + + ", but it does not."); + } + + [Fact] + public void When_subject_is_null_not_have_explicit_method_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().NotHaveExplicitMethod( + typeof(IExplicitInterface), "ExplicitMethod", new Type[0], "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type to not explicitly implement *.IExplicitInterface.ExplicitMethod() *failure message*" + + ", but type is ."); + } + + [Fact] + public void When_asserting_a_type_does_not_have_an_explicit_method_inherited_from_null_it_should_throw() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + // Act + Action act = () => + type.Should().NotHaveExplicitMethod(null, "ExplicitMethod", new Type[0]); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("interfaceType"); + } + + [Fact] + public void When_asserting_a_type_does_not_have_an_explicit_method_with_a_null_parameter_type_list_it_should_throw() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + // Act + Action act = () => + type.Should().NotHaveExplicitMethod(typeof(IExplicitInterface), "ExplicitMethod", null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("parameterTypes"); + } + + [Fact] + public void When_asserting_a_type_does_not_have_an_explicit_method_with_a_null_name_it_should_throw() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + // Act + Action act = () => + type.Should().NotHaveExplicitMethod(typeof(IExplicitInterface), null, new Type[0]); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("name"); + } + + [Fact] + public void When_asserting_a_type_does_not_have_an_explicit_method_with_an_empty_name_it_should_throw() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + // Act + Action act = () => + type.Should().NotHaveExplicitMethod(typeof(IExplicitInterface), string.Empty, new Type[0]); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("name"); + } } - [Fact] - public void When_asserting_a_type_does_not_explicitly_implement_a_method_which_it_implements_implicitly_it_succeeds() + public class NotHaveExplicitMethodOfT { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - var interfaceType = typeof(IExplicitInterface); - - // Act - Action act = () => - type.Should() - .NotHaveExplicitMethod(interfaceType, "ImplicitMethod", new Type[0]); - - // Assert - act.Should().NotThrow(); + [Fact] + public void When_asserting_a_type_does_not_explicitly_implementOfT_a_method_which_it_does_it_fails() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + // Act + Action act = () => + type.Should() + .NotHaveExplicitMethod("ExplicitMethod", new Type[0]); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected *.ClassExplicitlyImplementingInterface to not explicitly implement " + + "*.IExplicitInterface.ExplicitMethod(), but it does."); + } + + [Fact] + public void When_subject_is_null_not_have_explicit_methodOfT_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().NotHaveExplicitMethod( + "ExplicitMethod", new Type[0], "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type to not explicitly implement *.IExplicitInterface.ExplicitMethod() *failure message*" + + ", but type is ."); + } + + [Fact] + public void When_asserting_a_type_does_not_have_an_explicit_methodOfT_with_a_null_parameter_type_list_it_should_throw() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + // Act + Action act = () => + type.Should().NotHaveExplicitMethod("ExplicitMethod", null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("parameterTypes"); + } + + [Fact] + public void When_asserting_a_type_does_not_have_an_explicit_methodOfT_with_a_null_name_it_should_throw() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + // Act + Action act = () => + type.Should().NotHaveExplicitMethod(null, new Type[0]); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("name"); + } + + [Fact] + public void When_asserting_a_type_does_not_have_an_explicit_methodOfT_with_an_empty_name_it_should_throw() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + // Act + Action act = () => + type.Should().NotHaveExplicitMethod(string.Empty, new Type[0]); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("name"); + } } - - [Fact] - public void When_asserting_a_type_does_not_explicitly_implement_a_method_which_it_does_not_implement_it_succeeds() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - var interfaceType = typeof(IExplicitInterface); - - // Act - Action act = () => - type.Should() - .NotHaveExplicitMethod(interfaceType, "NonExistentMethod", new Type[0]); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_type_does_not_explicitly_implement_a_method_from_an_unimplemented_interface_it_succeeds() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - var interfaceType = typeof(IDummyInterface); - - // Act - Action act = () => - type.Should() - .NotHaveExplicitMethod(interfaceType, "NonExistentMethod", new Type[0]); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type *.ClassExplicitlyImplementingInterface to implement interface *.IDummyInterface" + - ", but it does not."); - } - - [Fact] - public void When_subject_is_null_not_have_explicit_method_should_fail() - { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().NotHaveExplicitMethod( - typeof(IExplicitInterface), "ExplicitMethod", new Type[0], "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type to not explicitly implement *.IExplicitInterface.ExplicitMethod() *failure message*" + - ", but type is ."); - } - - [Fact] - public void When_asserting_a_type_does_not_have_an_explicit_method_inherited_from_null_it_should_throw() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - // Act - Action act = () => - type.Should().NotHaveExplicitMethod(null, "ExplicitMethod", new Type[0]); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("interfaceType"); - } - - [Fact] - public void When_asserting_a_type_does_not_have_an_explicit_method_with_a_null_parameter_type_list_it_should_throw() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - // Act - Action act = () => - type.Should().NotHaveExplicitMethod(typeof(IExplicitInterface), "ExplicitMethod", null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("parameterTypes"); - } - - [Fact] - public void When_asserting_a_type_does_not_have_an_explicit_method_with_a_null_name_it_should_throw() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - // Act - Action act = () => - type.Should().NotHaveExplicitMethod(typeof(IExplicitInterface), null, new Type[0]); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("name"); - } - - [Fact] - public void When_asserting_a_type_does_not_have_an_explicit_method_with_an_empty_name_it_should_throw() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - // Act - Action act = () => - type.Should().NotHaveExplicitMethod(typeof(IExplicitInterface), string.Empty, new Type[0]); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("name"); - } - - #endregion - - #region NotHaveExplicitMethodOfT - - [Fact] - public void When_asserting_a_type_does_not_explicitly_implementOfT_a_method_which_it_does_it_fails() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - // Act - Action act = () => - type.Should() - .NotHaveExplicitMethod("ExplicitMethod", new Type[0]); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected *.ClassExplicitlyImplementingInterface to not explicitly implement " + - "*.IExplicitInterface.ExplicitMethod(), but it does."); - } - - [Fact] - public void When_subject_is_null_not_have_explicit_methodOfT_should_fail() - { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().NotHaveExplicitMethod( - "ExplicitMethod", new Type[0], "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type to not explicitly implement *.IExplicitInterface.ExplicitMethod() *failure message*" + - ", but type is ."); - } - - [Fact] - public void When_asserting_a_type_does_not_have_an_explicit_methodOfT_with_a_null_parameter_type_list_it_should_throw() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - // Act - Action act = () => - type.Should().NotHaveExplicitMethod("ExplicitMethod", null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("parameterTypes"); - } - - [Fact] - public void When_asserting_a_type_does_not_have_an_explicit_methodOfT_with_a_null_name_it_should_throw() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - // Act - Action act = () => - type.Should().NotHaveExplicitMethod(null, new Type[0]); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("name"); - } - - [Fact] - public void When_asserting_a_type_does_not_have_an_explicit_methodOfT_with_an_empty_name_it_should_throw() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - // Act - Action act = () => - type.Should().NotHaveExplicitMethod(string.Empty, new Type[0]); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("name"); - } - - #endregion } } diff --git a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveExplicitProperty.cs b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveExplicitProperty.cs index 8c128e066c..3b4103af98 100644 --- a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveExplicitProperty.cs +++ b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveExplicitProperty.cs @@ -9,463 +9,459 @@ namespace FluentAssertions.Specs.Types /// public partial class TypeAssertionSpecs { - #region HaveExplicitProperty - - [Fact] - public void When_asserting_a_type_explicitly_implements_a_property_which_it_does_it_succeeds() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - var interfaceType = typeof(IExplicitInterface); - - // Act - Action act = () => - type.Should() - .HaveExplicitProperty(interfaceType, "ExplicitStringProperty"); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_type_explicitly_implements_a_property_which_it_implements_implicitly_and_explicitly_it_succeeds() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - var interfaceType = typeof(IExplicitInterface); - - // Act - Action act = () => - type.Should() - .HaveExplicitProperty(interfaceType, "ExplicitImplicitStringProperty"); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_type_explicitly_implements_a_property_which_it_implements_implicitly_it_fails() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - var interfaceType = typeof(IExplicitInterface); - - // Act - Action act = () => - type.Should() - .HaveExplicitProperty(interfaceType, "ImplicitStringProperty"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected *.ClassExplicitlyImplementingInterface to explicitly implement " + - "*.IExplicitInterface.ImplicitStringProperty, but it does not."); - } - - [Fact] - public void When_asserting_a_type_explicitly_implements_a_property_which_it_does_not_implement_it_fails() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - var interfaceType = typeof(IExplicitInterface); - - // Act - Action act = () => - type.Should() - .HaveExplicitProperty(interfaceType, "NonExistentProperty"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected *.ClassExplicitlyImplementingInterface to explicitly implement " + - "*.IExplicitInterface.NonExistentProperty, but it does not."); - } - - [Fact] - public void When_asserting_a_type_explicitly_implements_a_property_from_an_unimplemented_interface_it_fails() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - var interfaceType = typeof(IDummyInterface); - - // Act - Action act = () => - type.Should() - .HaveExplicitProperty(interfaceType, "NonExistentProperty"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type *.ClassExplicitlyImplementingInterface to implement interface *.IDummyInterface" + - ", but it does not."); - } - - [Fact] - public void When_subject_is_null_have_explicit_property_should_fail() - { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().HaveExplicitProperty( - typeof(IExplicitInterface), "ExplicitStringProperty", "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type to explicitly implement *.IExplicitInterface.ExplicitStringProperty *failure message*" + - ", but type is ."); - } - - [Fact] - public void When_asserting_a_type_has_an_explicit_property_inherited_by_null_it_should_throw() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - // Act - Action act = () => - type.Should().HaveExplicitProperty(null, "ExplicitStringProperty"); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("interfaceType"); - } - - [Fact] - public void When_asserting_a_type_has_an_explicit_property_with_a_null_name_it_should_throw() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - // Act - Action act = () => - type.Should().HaveExplicitProperty(typeof(IExplicitInterface), null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("name"); - } - - [Fact] - public void When_asserting_a_type_has_an_explicit_property_with_an_empty_name_it_should_throw() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - // Act - Action act = () => - type.Should().HaveExplicitProperty(typeof(IExplicitInterface), string.Empty); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("name"); - } - - #endregion - - #region HaveExplicitPropertyOfT - - [Fact] - public void When_asserting_a_type_explicitlyOfT_implements_a_property_which_it_does_it_succeeds() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - // Act - Action act = () => - type.Should() - .HaveExplicitProperty("ExplicitStringProperty"); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_type_has_an_explicitlyOfT_property_with_a_null_name_it_should_throw() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - // Act - Action act = () => - type.Should().HaveExplicitProperty(null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("name"); - } - - [Fact] - public void When_asserting_a_type_has_an_explicitlyOfT_property_with_an_empty_name_it_should_throw() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - // Act - Action act = () => - type.Should().HaveExplicitProperty(string.Empty); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("name"); - } - - [Fact] - public void When_subject_is_null_have_explicit_propertyOfT_should_fail() - { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().HaveExplicitProperty( - "ExplicitStringProperty", "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type to explicitly implement *.IExplicitInterface.ExplicitStringProperty *failure message*" + - ", but type is ."); - } - - #endregion - - #region NotHaveExplicitProperty - - [Fact] - public void When_asserting_a_type_does_not_explicitly_implement_a_property_which_it_does_it_fails() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - var interfaceType = typeof(IExplicitInterface); - - // Act - Action act = () => - type.Should() - .NotHaveExplicitProperty(interfaceType, "ExplicitStringProperty"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected *.ClassExplicitlyImplementingInterface to not explicitly implement " + - "*.IExplicitInterface.ExplicitStringProperty, but it does."); - } - - [Fact] - public void When_asserting_a_type_does_not_explicitly_implement_a_property_which_it_implements_implicitly_and_explicitly_it_fails() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - var interfaceType = typeof(IExplicitInterface); - - // Act - Action act = () => - type.Should() - .NotHaveExplicitProperty(interfaceType, "ExplicitImplicitStringProperty"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected *.ClassExplicitlyImplementingInterface to not explicitly implement " + - "*.IExplicitInterface.ExplicitImplicitStringProperty, but it does."); - } - - [Fact] - public void When_asserting_a_type_does_not_explicitly_implement_a_property_which_it_implements_implicitly_it_succeeds() + public class HaveExplicitProperty { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - var interfaceType = typeof(IExplicitInterface); - - // Act - Action act = () => - type.Should() - .NotHaveExplicitProperty(interfaceType, "ImplicitStringProperty"); - - // Assert - act.Should().NotThrow(); + [Fact] + public void When_asserting_a_type_explicitly_implements_a_property_which_it_does_it_succeeds() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + var interfaceType = typeof(IExplicitInterface); + + // Act + Action act = () => + type.Should() + .HaveExplicitProperty(interfaceType, "ExplicitStringProperty"); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_explicitly_implements_a_property_which_it_implements_implicitly_and_explicitly_it_succeeds() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + var interfaceType = typeof(IExplicitInterface); + + // Act + Action act = () => + type.Should() + .HaveExplicitProperty(interfaceType, "ExplicitImplicitStringProperty"); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_explicitly_implements_a_property_which_it_implements_implicitly_it_fails() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + var interfaceType = typeof(IExplicitInterface); + + // Act + Action act = () => + type.Should() + .HaveExplicitProperty(interfaceType, "ImplicitStringProperty"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected *.ClassExplicitlyImplementingInterface to explicitly implement " + + "*.IExplicitInterface.ImplicitStringProperty, but it does not."); + } + + [Fact] + public void When_asserting_a_type_explicitly_implements_a_property_which_it_does_not_implement_it_fails() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + var interfaceType = typeof(IExplicitInterface); + + // Act + Action act = () => + type.Should() + .HaveExplicitProperty(interfaceType, "NonExistentProperty"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected *.ClassExplicitlyImplementingInterface to explicitly implement " + + "*.IExplicitInterface.NonExistentProperty, but it does not."); + } + + [Fact] + public void When_asserting_a_type_explicitly_implements_a_property_from_an_unimplemented_interface_it_fails() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + var interfaceType = typeof(IDummyInterface); + + // Act + Action act = () => + type.Should() + .HaveExplicitProperty(interfaceType, "NonExistentProperty"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type *.ClassExplicitlyImplementingInterface to implement interface *.IDummyInterface" + + ", but it does not."); + } + + [Fact] + public void When_subject_is_null_have_explicit_property_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().HaveExplicitProperty( + typeof(IExplicitInterface), "ExplicitStringProperty", "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type to explicitly implement *.IExplicitInterface.ExplicitStringProperty *failure message*" + + ", but type is ."); + } + + [Fact] + public void When_asserting_a_type_has_an_explicit_property_inherited_by_null_it_should_throw() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + // Act + Action act = () => + type.Should().HaveExplicitProperty(null, "ExplicitStringProperty"); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("interfaceType"); + } + + [Fact] + public void When_asserting_a_type_has_an_explicit_property_with_a_null_name_it_should_throw() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + // Act + Action act = () => + type.Should().HaveExplicitProperty(typeof(IExplicitInterface), null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("name"); + } + + [Fact] + public void When_asserting_a_type_has_an_explicit_property_with_an_empty_name_it_should_throw() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + // Act + Action act = () => + type.Should().HaveExplicitProperty(typeof(IExplicitInterface), string.Empty); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("name"); + } } - [Fact] - public void When_asserting_a_type_does_not_explicitly_implement_a_property_which_it_does_not_implement_it_succeeds() + public class HaveExplicitPropertyOfT { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - var interfaceType = typeof(IExplicitInterface); - - // Act - Action act = () => - type.Should() - .NotHaveExplicitProperty(interfaceType, "NonExistentProperty"); - - // Assert - act.Should().NotThrow(); + [Fact] + public void When_asserting_a_type_explicitlyOfT_implements_a_property_which_it_does_it_succeeds() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + // Act + Action act = () => + type.Should() + .HaveExplicitProperty("ExplicitStringProperty"); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_has_an_explicitlyOfT_property_with_a_null_name_it_should_throw() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + // Act + Action act = () => + type.Should().HaveExplicitProperty(null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("name"); + } + + [Fact] + public void When_asserting_a_type_has_an_explicitlyOfT_property_with_an_empty_name_it_should_throw() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + // Act + Action act = () => + type.Should().HaveExplicitProperty(string.Empty); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("name"); + } + + [Fact] + public void When_subject_is_null_have_explicit_propertyOfT_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().HaveExplicitProperty( + "ExplicitStringProperty", "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type to explicitly implement *.IExplicitInterface.ExplicitStringProperty *failure message*" + + ", but type is ."); + } } - [Fact] - public void When_asserting_a_type_does_not_explicitly_implement_a_property_from_an_unimplemented_interface_it_succeeds() + public class NotHaveExplicitProperty { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - var interfaceType = typeof(IDummyInterface); - - // Act - Action act = () => - type.Should() - .NotHaveExplicitProperty(interfaceType, "NonExistentProperty"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type *.ClassExplicitlyImplementingInterface to implement interface *.IDummyInterface" + - ", but it does not."); + [Fact] + public void When_asserting_a_type_does_not_explicitly_implement_a_property_which_it_does_it_fails() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + var interfaceType = typeof(IExplicitInterface); + + // Act + Action act = () => + type.Should() + .NotHaveExplicitProperty(interfaceType, "ExplicitStringProperty"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected *.ClassExplicitlyImplementingInterface to not explicitly implement " + + "*.IExplicitInterface.ExplicitStringProperty, but it does."); + } + + [Fact] + public void When_asserting_a_type_does_not_explicitly_implement_a_property_which_it_implements_implicitly_and_explicitly_it_fails() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + var interfaceType = typeof(IExplicitInterface); + + // Act + Action act = () => + type.Should() + .NotHaveExplicitProperty(interfaceType, "ExplicitImplicitStringProperty"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected *.ClassExplicitlyImplementingInterface to not explicitly implement " + + "*.IExplicitInterface.ExplicitImplicitStringProperty, but it does."); + } + + [Fact] + public void When_asserting_a_type_does_not_explicitly_implement_a_property_which_it_implements_implicitly_it_succeeds() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + var interfaceType = typeof(IExplicitInterface); + + // Act + Action act = () => + type.Should() + .NotHaveExplicitProperty(interfaceType, "ImplicitStringProperty"); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_does_not_explicitly_implement_a_property_which_it_does_not_implement_it_succeeds() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + var interfaceType = typeof(IExplicitInterface); + + // Act + Action act = () => + type.Should() + .NotHaveExplicitProperty(interfaceType, "NonExistentProperty"); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_does_not_explicitly_implement_a_property_from_an_unimplemented_interface_it_succeeds() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + var interfaceType = typeof(IDummyInterface); + + // Act + Action act = () => + type.Should() + .NotHaveExplicitProperty(interfaceType, "NonExistentProperty"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type *.ClassExplicitlyImplementingInterface to implement interface *.IDummyInterface" + + ", but it does not."); + } + + [Fact] + public void When_subject_is_null_not_have_explicit_property_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().NotHaveExplicitProperty( + typeof(IExplicitInterface), "ExplicitStringProperty", "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type to not explicitly implement *IExplicitInterface.ExplicitStringProperty *failure message*" + + ", but type is ."); + } + + [Fact] + public void When_asserting_a_type_does_not_have_an_explicit_property_inherited_from_null_it_should_throw() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + // Act + Action act = () => + type.Should().NotHaveExplicitProperty(null, "ExplicitStringProperty"); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("interfaceType"); + } + + [Fact] + public void When_asserting_a_type_does_not_have_an_explicit_property_with_a_null_name_it_should_throw() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + // Act + Action act = () => + type.Should().NotHaveExplicitProperty(typeof(IExplicitInterface), null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("name"); + } + + [Fact] + public void When_asserting_a_type_does_not_have_an_explicit_property_with_an_empty_name_it_should_throw() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + // Act + Action act = () => + type.Should().NotHaveExplicitProperty(typeof(IExplicitInterface), string.Empty); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("name"); + } } - [Fact] - public void When_subject_is_null_not_have_explicit_property_should_fail() + public class NotHaveExplicitPropertyOfT { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().NotHaveExplicitProperty( - typeof(IExplicitInterface), "ExplicitStringProperty", "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type to not explicitly implement *IExplicitInterface.ExplicitStringProperty *failure message*" + - ", but type is ."); + [Fact] + public void When_asserting_a_type_does_not_explicitlyOfT_implement_a_property_which_it_does_it_fails() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + // Act + Action act = () => + type.Should() + .NotHaveExplicitProperty("ExplicitStringProperty"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected *.ClassExplicitlyImplementingInterface to not explicitly implement " + + "*.IExplicitInterface.ExplicitStringProperty, but it does."); + } + + [Fact] + public void When_subject_is_null_not_have_explicit_propertyOfT_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().NotHaveExplicitProperty( + "ExplicitStringProperty", "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type to not explicitly implement *.IExplicitInterface.ExplicitStringProperty *failure message*" + + ", but type is ."); + } + + [Fact] + public void When_asserting_a_type_does_not_have_an_explicitlyOfT_property_with_a_null_name_it_should_throw() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + // Act + Action act = () => + type.Should().NotHaveExplicitProperty(null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("name"); + } + + [Fact] + public void When_asserting_a_type_does_not_have_an_explicitlyOfT_property_with_an_empty_name_it_should_throw() + { + // Arrange + var type = typeof(ClassExplicitlyImplementingInterface); + + // Act + Action act = () => + type.Should().NotHaveExplicitProperty(string.Empty); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("name"); + } } - - [Fact] - public void When_asserting_a_type_does_not_have_an_explicit_property_inherited_from_null_it_should_throw() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - // Act - Action act = () => - type.Should().NotHaveExplicitProperty(null, "ExplicitStringProperty"); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("interfaceType"); - } - - [Fact] - public void When_asserting_a_type_does_not_have_an_explicit_property_with_a_null_name_it_should_throw() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - // Act - Action act = () => - type.Should().NotHaveExplicitProperty(typeof(IExplicitInterface), null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("name"); - } - - [Fact] - public void When_asserting_a_type_does_not_have_an_explicit_property_with_an_empty_name_it_should_throw() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - // Act - Action act = () => - type.Should().NotHaveExplicitProperty(typeof(IExplicitInterface), string.Empty); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("name"); - } - - #endregion - - #region NotHaveExplicitPropertyOfT - - [Fact] - public void When_asserting_a_type_does_not_explicitlyOfT_implement_a_property_which_it_does_it_fails() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - // Act - Action act = () => - type.Should() - .NotHaveExplicitProperty("ExplicitStringProperty"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected *.ClassExplicitlyImplementingInterface to not explicitly implement " + - "*.IExplicitInterface.ExplicitStringProperty, but it does."); - } - - [Fact] - public void When_subject_is_null_not_have_explicit_propertyOfT_should_fail() - { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().NotHaveExplicitProperty( - "ExplicitStringProperty", "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type to not explicitly implement *.IExplicitInterface.ExplicitStringProperty *failure message*" + - ", but type is ."); - } - - [Fact] - public void When_asserting_a_type_does_not_have_an_explicitlyOfT_property_with_a_null_name_it_should_throw() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - // Act - Action act = () => - type.Should().NotHaveExplicitProperty(null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("name"); - } - - [Fact] - public void When_asserting_a_type_does_not_have_an_explicitlyOfT_property_with_an_empty_name_it_should_throw() - { - // Arrange - var type = typeof(ClassExplicitlyImplementingInterface); - - // Act - Action act = () => - type.Should().NotHaveExplicitProperty(string.Empty); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("name"); - } - - #endregion } } diff --git a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveImplicitConversionOperator.cs b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveImplicitConversionOperator.cs index 6695dfadb2..391629aa03 100644 --- a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveImplicitConversionOperator.cs +++ b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveImplicitConversionOperator.cs @@ -9,296 +9,292 @@ namespace FluentAssertions.Specs.Types /// public partial class TypeAssertionSpecs { - #region HaveImplicitConversionOperator - - [Fact] - public void When_asserting_a_type_has_an_implicit_conversion_operator_which_it_does_it_succeeds() - { - // Arrange - var type = typeof(TypeWithConversionOperators); - var sourceType = typeof(TypeWithConversionOperators); - var targetType = typeof(int); - - // Act - Action act = () => - type.Should() - .HaveImplicitConversionOperator(sourceType, targetType) - .Which.Should() - .NotBeNull(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_type_has_an_implicit_conversion_operator_which_it_does_not_it_fails() - { - // Arrange - var type = typeof(TypeWithConversionOperators); - var sourceType = typeof(TypeWithConversionOperators); - var targetType = typeof(string); - - // Act - Action act = () => - type.Should().HaveImplicitConversionOperator( - sourceType, targetType, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected public static implicit *.String(*.TypeWithConversionOperators) to exist *failure message*" + - ", but it does not."); - } - - [Fact] - public void When_subject_is_null_have_implicit_conversion_operator_should_fail() - { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().HaveImplicitConversionOperator( - typeof(TypeWithConversionOperators), typeof(string), "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected public static implicit *.String(*.TypeWithConversionOperators) to exist *failure message*" + - ", but type is ."); - } - - [Fact] - public void When_asserting_a_type_has_an_implicit_conversion_operator_from_null_it_should_throw() - { - // Arrange - var type = typeof(TypeWithConversionOperators); - - // Act - Action act = () => - type.Should().HaveImplicitConversionOperator(null, typeof(string)); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("sourceType"); - } - - [Fact] - public void When_asserting_a_type_has_an_implicit_conversion_operator_to_null_it_should_throw() - { - // Arrange - var type = typeof(TypeWithConversionOperators); - - // Act - Action act = () => - type.Should().HaveImplicitConversionOperator(typeof(TypeWithConversionOperators), null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("targetType"); - } - - #endregion - - #region HaveImplicitConversionOperatorOfT - - [Fact] - public void When_asserting_a_type_has_an_implicit_conversion_operatorOfT_which_it_does_it_succeeds() - { - // Arrange - var type = typeof(TypeWithConversionOperators); - - // Act - Action act = () => - type.Should() - .HaveImplicitConversionOperator() - .Which.Should() - .NotBeNull(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_type_has_an_implicit_conversion_operatorOfT_which_it_does_not_it_fails() - { - // Arrange - var type = typeof(TypeWithConversionOperators); - - // Act - Action act = () => - type.Should().HaveImplicitConversionOperator( - "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected public static implicit *.String(*.TypeWithConversionOperators) to exist *failure message*" + - ", but it does not."); - } - - [Fact] - public void When_subject_is_null_have_implicit_conversion_operatorOfT_should_fail() - { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().HaveImplicitConversionOperator( - "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected public static implicit *.String(*.TypeWithConversionOperators) to exist *failure message*" + - ", but type is ."); - } - - #endregion - - #region NotHaveImplicitConversionOperator - - [Fact] - public void When_asserting_a_type_does_not_have_an_implicit_conversion_operator_which_it_does_not_it_succeeds() - { - // Arrange - var type = typeof(TypeWithConversionOperators); - var sourceType = typeof(TypeWithConversionOperators); - var targetType = typeof(string); - - // Act - Action act = () => - type.Should() - .NotHaveImplicitConversionOperator(sourceType, targetType); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_type_does_not_have_an_implicit_conversion_operator_which_it_does_it_fails() + public class HaveImplicitConversionOperator { - // Arrange - var type = typeof(TypeWithConversionOperators); - var sourceType = typeof(TypeWithConversionOperators); - var targetType = typeof(int); - - // Act - Action act = () => - type.Should().NotHaveImplicitConversionOperator( - sourceType, targetType, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected public static implicit *.Int32(*.TypeWithConversionOperators) to not exist *failure message*" + - ", but it does."); + [Fact] + public void When_asserting_a_type_has_an_implicit_conversion_operator_which_it_does_it_succeeds() + { + // Arrange + var type = typeof(TypeWithConversionOperators); + var sourceType = typeof(TypeWithConversionOperators); + var targetType = typeof(int); + + // Act + Action act = () => + type.Should() + .HaveImplicitConversionOperator(sourceType, targetType) + .Which.Should() + .NotBeNull(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_has_an_implicit_conversion_operator_which_it_does_not_it_fails() + { + // Arrange + var type = typeof(TypeWithConversionOperators); + var sourceType = typeof(TypeWithConversionOperators); + var targetType = typeof(string); + + // Act + Action act = () => + type.Should().HaveImplicitConversionOperator( + sourceType, targetType, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected public static implicit *.String(*.TypeWithConversionOperators) to exist *failure message*" + + ", but it does not."); + } + + [Fact] + public void When_subject_is_null_have_implicit_conversion_operator_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().HaveImplicitConversionOperator( + typeof(TypeWithConversionOperators), typeof(string), "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected public static implicit *.String(*.TypeWithConversionOperators) to exist *failure message*" + + ", but type is ."); + } + + [Fact] + public void When_asserting_a_type_has_an_implicit_conversion_operator_from_null_it_should_throw() + { + // Arrange + var type = typeof(TypeWithConversionOperators); + + // Act + Action act = () => + type.Should().HaveImplicitConversionOperator(null, typeof(string)); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("sourceType"); + } + + [Fact] + public void When_asserting_a_type_has_an_implicit_conversion_operator_to_null_it_should_throw() + { + // Arrange + var type = typeof(TypeWithConversionOperators); + + // Act + Action act = () => + type.Should().HaveImplicitConversionOperator(typeof(TypeWithConversionOperators), null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("targetType"); + } } - [Fact] - public void When_subject_is_null_not_have_implicit_conversion_operator_should_fail() + public class HaveImplicitConversionOperatorOfT { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().NotHaveImplicitConversionOperator( - typeof(TypeWithConversionOperators), typeof(string), "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected public static implicit *.String(*.TypeWithConversionOperators) to not exist *failure message*" + - ", but type is ."); + [Fact] + public void When_asserting_a_type_has_an_implicit_conversion_operatorOfT_which_it_does_it_succeeds() + { + // Arrange + var type = typeof(TypeWithConversionOperators); + + // Act + Action act = () => + type.Should() + .HaveImplicitConversionOperator() + .Which.Should() + .NotBeNull(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_has_an_implicit_conversion_operatorOfT_which_it_does_not_it_fails() + { + // Arrange + var type = typeof(TypeWithConversionOperators); + + // Act + Action act = () => + type.Should().HaveImplicitConversionOperator( + "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected public static implicit *.String(*.TypeWithConversionOperators) to exist *failure message*" + + ", but it does not."); + } + + [Fact] + public void When_subject_is_null_have_implicit_conversion_operatorOfT_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().HaveImplicitConversionOperator( + "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected public static implicit *.String(*.TypeWithConversionOperators) to exist *failure message*" + + ", but type is ."); + } } - [Fact] - public void When_asserting_a_type_does_not_have_an_implicit_conversion_operator_from_null_it_should_throw() + public class NotHaveImplicitConversionOperator { - // Arrange - var type = typeof(TypeWithConversionOperators); - - // Act - Action act = () => - type.Should().NotHaveImplicitConversionOperator(null, typeof(string)); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("sourceType"); + [Fact] + public void When_asserting_a_type_does_not_have_an_implicit_conversion_operator_which_it_does_not_it_succeeds() + { + // Arrange + var type = typeof(TypeWithConversionOperators); + var sourceType = typeof(TypeWithConversionOperators); + var targetType = typeof(string); + + // Act + Action act = () => + type.Should() + .NotHaveImplicitConversionOperator(sourceType, targetType); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_does_not_have_an_implicit_conversion_operator_which_it_does_it_fails() + { + // Arrange + var type = typeof(TypeWithConversionOperators); + var sourceType = typeof(TypeWithConversionOperators); + var targetType = typeof(int); + + // Act + Action act = () => + type.Should().NotHaveImplicitConversionOperator( + sourceType, targetType, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected public static implicit *.Int32(*.TypeWithConversionOperators) to not exist *failure message*" + + ", but it does."); + } + + [Fact] + public void When_subject_is_null_not_have_implicit_conversion_operator_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().NotHaveImplicitConversionOperator( + typeof(TypeWithConversionOperators), typeof(string), "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected public static implicit *.String(*.TypeWithConversionOperators) to not exist *failure message*" + + ", but type is ."); + } + + [Fact] + public void When_asserting_a_type_does_not_have_an_implicit_conversion_operator_from_null_it_should_throw() + { + // Arrange + var type = typeof(TypeWithConversionOperators); + + // Act + Action act = () => + type.Should().NotHaveImplicitConversionOperator(null, typeof(string)); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("sourceType"); + } + + [Fact] + public void When_asserting_a_type_does_not_have_an_implicit_conversion_operator_to_null_it_should_throw() + { + // Arrange + var type = typeof(TypeWithConversionOperators); + + // Act + Action act = () => + type.Should().NotHaveImplicitConversionOperator(typeof(TypeWithConversionOperators), null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("targetType"); + } } - [Fact] - public void When_asserting_a_type_does_not_have_an_implicit_conversion_operator_to_null_it_should_throw() + public class NotHaveImplicitConversionOperatorOfT { - // Arrange - var type = typeof(TypeWithConversionOperators); - - // Act - Action act = () => - type.Should().NotHaveImplicitConversionOperator(typeof(TypeWithConversionOperators), null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("targetType"); + [Fact] + public void When_asserting_a_type_does_not_have_an_implicit_conversion_operatorOfT_which_it_does_not_it_succeeds() + { + // Arrange + var type = typeof(TypeWithConversionOperators); + + // Act + Action act = () => + type.Should() + .NotHaveImplicitConversionOperator(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_does_not_have_an_implicit_conversion_operatorOfT_which_it_does_it_fails() + { + // Arrange + var type = typeof(TypeWithConversionOperators); + + // Act + Action act = () => + type.Should().NotHaveImplicitConversionOperator( + "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected public static implicit *.Int32(*.TypeWithConversionOperators) to not exist *failure message*" + + ", but it does."); + } + + [Fact] + public void When_subject_is_null_not_have_implicit_conversion_operatorOfT_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().NotHaveImplicitConversionOperator( + "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected public static implicit *.String(*.TypeWithConversionOperators) to not exist *failure message*" + + ", but type is ."); + } } - - #endregion - - #region NotHaveImplicitConversionOperatorOfT - - [Fact] - public void When_asserting_a_type_does_not_have_an_implicit_conversion_operatorOfT_which_it_does_not_it_succeeds() - { - // Arrange - var type = typeof(TypeWithConversionOperators); - - // Act - Action act = () => - type.Should() - .NotHaveImplicitConversionOperator(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_type_does_not_have_an_implicit_conversion_operatorOfT_which_it_does_it_fails() - { - // Arrange - var type = typeof(TypeWithConversionOperators); - - // Act - Action act = () => - type.Should().NotHaveImplicitConversionOperator( - "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected public static implicit *.Int32(*.TypeWithConversionOperators) to not exist *failure message*" + - ", but it does."); - } - - [Fact] - public void When_subject_is_null_not_have_implicit_conversion_operatorOfT_should_fail() - { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().NotHaveImplicitConversionOperator( - "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected public static implicit *.String(*.TypeWithConversionOperators) to not exist *failure message*" + - ", but type is ."); - } - - #endregion } } diff --git a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveIndexer.cs b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveIndexer.cs index 3b1fc79a81..398bc5eeae 100644 --- a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveIndexer.cs +++ b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveIndexer.cs @@ -10,169 +10,167 @@ namespace FluentAssertions.Specs.Types /// public partial class TypeAssertionSpecs { - #region HaveIndexer - - [Fact] - public void When_asserting_a_type_has_an_indexer_which_it_does_then_it_succeeds() - { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should() - .HaveIndexer(typeof(string), new[] { typeof(string) }) - .Which.Should() - .BeWritable(CSharpAccessModifier.Internal) - .And.BeReadable(CSharpAccessModifier.Private); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_type_has_an_indexer_which_it_does_not_it_fails() - { - // Arrange - var type = typeof(ClassWithNoMembers); - - // Act - Action act = () => - type.Should().HaveIndexer( - typeof(string), new[] { typeof(int), typeof(Type) }, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected String *ClassWithNoMembers[System.Int32, System.Type] to exist *failure message*" + - ", but it does not."); - } - - [Fact] - public void When_asserting_a_type_has_an_indexer_with_different_parameters_it_fails() - { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should().HaveIndexer( - typeof(string), new[] { typeof(int), typeof(Type) }, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected String *.ClassWithMembers[System.Int32, System.Type] to exist *failure message*, but it does not."); - } - - [Fact] - public void When_subject_is_null_have_indexer_should_fail() - { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().HaveIndexer(typeof(string), new[] { typeof(string) }, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected String type[System.String] to exist *failure message*, but type is ."); - } - - [Fact] - public void When_asserting_a_type_has_an_indexer_for_null_it_should_throw() + public class HaveIndexer { - // Arrange - Type type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should().HaveIndexer(null, new[] { typeof(string) }); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("indexerType"); + [Fact] + public void When_asserting_a_type_has_an_indexer_which_it_does_then_it_succeeds() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should() + .HaveIndexer(typeof(string), new[] { typeof(string) }) + .Which.Should() + .BeWritable(CSharpAccessModifier.Internal) + .And.BeReadable(CSharpAccessModifier.Private); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_has_an_indexer_which_it_does_not_it_fails() + { + // Arrange + var type = typeof(ClassWithNoMembers); + + // Act + Action act = () => + type.Should().HaveIndexer( + typeof(string), new[] { typeof(int), typeof(Type) }, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected String *ClassWithNoMembers[System.Int32, System.Type] to exist *failure message*" + + ", but it does not."); + } + + [Fact] + public void When_asserting_a_type_has_an_indexer_with_different_parameters_it_fails() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should().HaveIndexer( + typeof(string), new[] { typeof(int), typeof(Type) }, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected String *.ClassWithMembers[System.Int32, System.Type] to exist *failure message*, but it does not."); + } + + [Fact] + public void When_subject_is_null_have_indexer_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().HaveIndexer(typeof(string), new[] { typeof(string) }, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected String type[System.String] to exist *failure message*, but type is ."); + } + + [Fact] + public void When_asserting_a_type_has_an_indexer_for_null_it_should_throw() + { + // Arrange + Type type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should().HaveIndexer(null, new[] { typeof(string) }); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("indexerType"); + } + + [Fact] + public void When_asserting_a_type_has_an_indexer_with_a_null_parameter_type_list_it_should_throw() + { + // Arrange + Type type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should().HaveIndexer(typeof(string), null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("parameterTypes"); + } } - - [Fact] - public void When_asserting_a_type_has_an_indexer_with_a_null_parameter_type_list_it_should_throw() + + public class NotHaveIndexer { - // Arrange - Type type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should().HaveIndexer(typeof(string), null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("parameterTypes"); + [Fact] + public void When_asserting_a_type_does_not_have_an_indexer_which_it_does_not_it_succeeds() + { + // Arrange + var type = typeof(ClassWithoutMembers); + + // Act + Action act = () => + type.Should().NotHaveIndexer(new[] { typeof(string) }); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_does_not_have_an_indexer_which_it_does_it_fails() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should().NotHaveIndexer(new[] { typeof(string) }, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected indexer *.ClassWithMembers[System.String] to not exist *failure message*, but it does."); + } + + [Fact] + public void When_subject_is_null_not_have_indexer_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().NotHaveIndexer(new[] { typeof(string) }, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected indexer type[System.String] to not exist *failure message*, but type is ."); + } + + [Fact] + public void When_asserting_a_type_does_not_have_an_indexer_for_null_it_should_throw() + { + // Arrange + Type type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should().NotHaveIndexer(null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("parameterTypes"); + } } - - #endregion - - #region NotHaveIndexer - - [Fact] - public void When_asserting_a_type_does_not_have_an_indexer_which_it_does_not_it_succeeds() - { - // Arrange - var type = typeof(ClassWithoutMembers); - - // Act - Action act = () => - type.Should().NotHaveIndexer(new[] { typeof(string) }); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_type_does_not_have_an_indexer_which_it_does_it_fails() - { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should().NotHaveIndexer(new[] { typeof(string) }, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected indexer *.ClassWithMembers[System.String] to not exist *failure message*, but it does."); - } - - [Fact] - public void When_subject_is_null_not_have_indexer_should_fail() - { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().NotHaveIndexer(new[] { typeof(string) }, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected indexer type[System.String] to not exist *failure message*, but type is ."); - } - - [Fact] - public void When_asserting_a_type_does_not_have_an_indexer_for_null_it_should_throw() - { - // Arrange - Type type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should().NotHaveIndexer(null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("parameterTypes"); - } - - #endregion } } diff --git a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveMethod.cs b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveMethod.cs index b92c96c8c6..f1ae220a47 100644 --- a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveMethod.cs +++ b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveMethod.cs @@ -10,229 +10,227 @@ namespace FluentAssertions.Specs.Types /// public partial class TypeAssertionSpecs { - #region HaveMethod - - [Fact] - public void When_asserting_a_type_has_a_method_which_it_does_it_succeeds() - { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should() - .HaveMethod("VoidMethod", new Type[] { }) - .Which.Should() - .HaveAccessModifier(CSharpAccessModifier.Private) - .And.ReturnVoid(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_type_has_a_method_which_it_does_not_it_fails() - { - // Arrange - var type = typeof(ClassWithNoMembers); - - // Act - Action act = () => - type.Should().HaveMethod( - "NonExistentMethod", new[] { typeof(int), typeof(Type) }, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected method *ClassWithNoMembers.NonExistentMethod(*.Int32, *.Type) to exist *failure message*" + - ", but it does not."); - } - - [Fact] - public void When_asserting_a_type_has_a_method_with_different_parameter_types_it_fails() - { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should().HaveMethod( - "VoidMethod", new[] { typeof(int), typeof(Type) }, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected method *.ClassWithMembers.VoidMethod(*.Int32, *.Type) to exist *failure message*" + - ", but it does not."); - } - - [Fact] - public void When_subject_is_null_have_method_should_fail() - { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().HaveMethod("Name", new[] { typeof(string) }, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected method type.Name(System.String) to exist *failure message*, but type is ."); - } - - [Fact] - public void When_asserting_a_type_has_a_method_with_a_null_name_it_should_throw() - { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should().HaveMethod(null, new[] { typeof(string) }); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("name"); - } - - [Fact] - public void When_asserting_a_type_has_a_method_with_an_empty_name_it_should_throw() - { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should().HaveMethod(string.Empty, new[] { typeof(string) }); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("name"); - } - - [Fact] - public void When_asserting_a_type_has_a_method_with_a_null_parameter_type_list_it_should_throw() - { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should().HaveMethod("Name", null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("parameterTypes"); - } - - #endregion - - #region NotHaveMethod - - [Fact] - public void When_asserting_a_type_does_not_have_a_method_which_it_does_not_it_succeeds() - { - // Arrange - var type = typeof(ClassWithoutMembers); - - // Act - Action act = () => - type.Should().NotHaveMethod("NonExistentMethod", new Type[] { }); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_type_does_not_have_a_method_which_it_has_with_different_parameter_types_it_succeeds() - { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should().NotHaveMethod("VoidMethod", new[] { typeof(int) }); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_type_does_not_have_that_method_which_it_does_it_fails() + public class HaveMethod { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should().NotHaveMethod("VoidMethod", new Type[] { }, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected method Void *.ClassWithMembers.VoidMethod() to not exist *failure message*, but it does."); + [Fact] + public void When_asserting_a_type_has_a_method_which_it_does_it_succeeds() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should() + .HaveMethod("VoidMethod", new Type[] { }) + .Which.Should() + .HaveAccessModifier(CSharpAccessModifier.Private) + .And.ReturnVoid(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_has_a_method_which_it_does_not_it_fails() + { + // Arrange + var type = typeof(ClassWithNoMembers); + + // Act + Action act = () => + type.Should().HaveMethod( + "NonExistentMethod", new[] { typeof(int), typeof(Type) }, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected method *ClassWithNoMembers.NonExistentMethod(*.Int32, *.Type) to exist *failure message*" + + ", but it does not."); + } + + [Fact] + public void When_asserting_a_type_has_a_method_with_different_parameter_types_it_fails() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should().HaveMethod( + "VoidMethod", new[] { typeof(int), typeof(Type) }, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected method *.ClassWithMembers.VoidMethod(*.Int32, *.Type) to exist *failure message*" + + ", but it does not."); + } + + [Fact] + public void When_subject_is_null_have_method_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().HaveMethod("Name", new[] { typeof(string) }, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected method type.Name(System.String) to exist *failure message*, but type is ."); + } + + [Fact] + public void When_asserting_a_type_has_a_method_with_a_null_name_it_should_throw() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should().HaveMethod(null, new[] { typeof(string) }); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("name"); + } + + [Fact] + public void When_asserting_a_type_has_a_method_with_an_empty_name_it_should_throw() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should().HaveMethod(string.Empty, new[] { typeof(string) }); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("name"); + } + + [Fact] + public void When_asserting_a_type_has_a_method_with_a_null_parameter_type_list_it_should_throw() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should().HaveMethod("Name", null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("parameterTypes"); + } } - [Fact] - public void When_subject_is_null_not_have_method_should_fail() + public class NotHaveMethod { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().NotHaveMethod("Name", new[] { typeof(string) }, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected method type.Name(System.String) to not exist *failure message*, but type is ."); + [Fact] + public void When_asserting_a_type_does_not_have_a_method_which_it_does_not_it_succeeds() + { + // Arrange + var type = typeof(ClassWithoutMembers); + + // Act + Action act = () => + type.Should().NotHaveMethod("NonExistentMethod", new Type[] { }); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_does_not_have_a_method_which_it_has_with_different_parameter_types_it_succeeds() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should().NotHaveMethod("VoidMethod", new[] { typeof(int) }); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_does_not_have_that_method_which_it_does_it_fails() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should().NotHaveMethod("VoidMethod", new Type[] { }, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected method Void *.ClassWithMembers.VoidMethod() to not exist *failure message*, but it does."); + } + + [Fact] + public void When_subject_is_null_not_have_method_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().NotHaveMethod("Name", new[] { typeof(string) }, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected method type.Name(System.String) to not exist *failure message*, but type is ."); + } + + [Fact] + public void When_asserting_a_type_does_not_have_a_method_with_a_null_name_it_should_throw() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should().NotHaveMethod(null, new[] { typeof(string) }); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("name"); + } + + [Fact] + public void When_asserting_a_type_does_not_have_a_method_with_an_empty_name_it_should_throw() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should().NotHaveMethod(string.Empty, new[] { typeof(string) }); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("name"); + } + + [Fact] + public void When_asserting_a_type_does_not_have_a_method_with_a_null_parameter_type_list_it_should_throw() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should().NotHaveMethod("Name", null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("parameterTypes"); + } } - - [Fact] - public void When_asserting_a_type_does_not_have_a_method_with_a_null_name_it_should_throw() - { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should().NotHaveMethod(null, new[] { typeof(string) }); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("name"); - } - - [Fact] - public void When_asserting_a_type_does_not_have_a_method_with_an_empty_name_it_should_throw() - { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should().NotHaveMethod(string.Empty, new[] { typeof(string) }); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("name"); - } - - [Fact] - public void When_asserting_a_type_does_not_have_a_method_with_a_null_parameter_type_list_it_should_throw() - { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should().NotHaveMethod("Name", null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("parameterTypes"); - } - - #endregion } } diff --git a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveProperty.cs b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveProperty.cs index 44888e9cbd..f13b02c56b 100644 --- a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveProperty.cs +++ b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveProperty.cs @@ -10,251 +10,248 @@ namespace FluentAssertions.Specs.Types /// public partial class TypeAssertionSpecs { - #region HaveProperty - - [Fact] - public void When_asserting_a_type_has_a_property_which_it_does_then_it_succeeds() - { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should() - .HaveProperty(typeof(string), "PrivateWriteProtectedReadProperty") - .Which.Should() - .BeWritable(CSharpAccessModifier.Private) - .And.BeReadable(CSharpAccessModifier.Protected); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_type_has_a_property_which_it_does_not_it_fails() - { - // Arrange - var type = typeof(ClassWithNoMembers); - - // Act - Action act = () => - type.Should().HaveProperty(typeof(string), "PublicProperty", "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected String *ClassWithNoMembers.PublicProperty to exist *failure message*, but it does not."); - } - - [Fact] - public void When_asserting_a_type_has_a_property_which_it_has_with_a_different_type_it_fails() - { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should() - .HaveProperty(typeof(int), "PrivateWriteProtectedReadProperty", "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected String *.ClassWithMembers.PrivateWriteProtectedReadProperty to be of type System.Int32 " + - "*failure message*, but it is not."); - } - - [Fact] - public void When_subject_is_null_have_property_should_fail() - { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().HaveProperty(typeof(string), "PublicProperty", "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected String type.PublicProperty to exist *failure message*, but type is ."); - } - - [Fact] - public void When_asserting_a_type_has_a_property_of_null_it_should_throw() - { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should().HaveProperty(null, "PublicProperty"); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("propertyType"); - } - - [Fact] - public void When_asserting_a_type_has_a_property_with_a_null_name_it_should_throw() - { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should().HaveProperty(typeof(string), null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("name"); - } - - [Fact] - public void When_asserting_a_type_has_a_property_with_an_empty_name_it_should_throw() - { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should().HaveProperty(typeof(string), string.Empty); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("name"); - } - - #endregion - - #region HavePropertyOfT - - [Fact] - public void When_asserting_a_type_has_a_propertyOfT_which_it_does_then_it_succeeds() - { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should() - .HaveProperty("PrivateWriteProtectedReadProperty") - .Which.Should() - .BeWritable(CSharpAccessModifier.Private) - .And.BeReadable(CSharpAccessModifier.Protected); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_type_has_a_propertyOfT_with_a_null_name_it_should_throw() - { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should().HaveProperty(null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("name"); - } - - [Fact] - public void When_asserting_a_type_has_a_propertyOfT_with_an_empty_name_it_should_throw() - { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should().HaveProperty(string.Empty); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("name"); - } - - #endregion - - #region NotHaveProperty - - [Fact] - public void When_asserting_a_type_does_not_have_a_property_which_it_does_not_it_succeeds() - { - // Arrange - var type = typeof(ClassWithoutMembers); - - // Act - Action act = () => - type.Should().NotHaveProperty("Property"); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_type_does_not_have_a_property_which_it_does_it_fails() - { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should().NotHaveProperty("PrivateWriteProtectedReadProperty", "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected String *.ClassWithMembers.PrivateWriteProtectedReadProperty to not exist *failure message*" + - ", but it does."); - } - - [Fact] - public void When_subject_is_null_not_have_property_should_fail() + public class HaveProperty { - // Arrange - Type type = null; - - // Act - Action act = () => - type.Should().NotHaveProperty("PublicProperty", "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage("Expected type.PublicProperty to not exist *failure message*, but type is ."); + [Fact] + public void When_asserting_a_type_has_a_property_which_it_does_then_it_succeeds() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should() + .HaveProperty(typeof(string), "PrivateWriteProtectedReadProperty") + .Which.Should() + .BeWritable(CSharpAccessModifier.Private) + .And.BeReadable(CSharpAccessModifier.Protected); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_has_a_property_which_it_does_not_it_fails() + { + // Arrange + var type = typeof(ClassWithNoMembers); + + // Act + Action act = () => + type.Should().HaveProperty(typeof(string), "PublicProperty", "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected String *ClassWithNoMembers.PublicProperty to exist *failure message*, but it does not."); + } + + [Fact] + public void When_asserting_a_type_has_a_property_which_it_has_with_a_different_type_it_fails() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should() + .HaveProperty(typeof(int), "PrivateWriteProtectedReadProperty", "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected String *.ClassWithMembers.PrivateWriteProtectedReadProperty to be of type System.Int32 " + + "*failure message*, but it is not."); + } + + [Fact] + public void When_subject_is_null_have_property_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().HaveProperty(typeof(string), "PublicProperty", "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected String type.PublicProperty to exist *failure message*, but type is ."); + } + + [Fact] + public void When_asserting_a_type_has_a_property_of_null_it_should_throw() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should().HaveProperty(null, "PublicProperty"); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("propertyType"); + } + + [Fact] + public void When_asserting_a_type_has_a_property_with_a_null_name_it_should_throw() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should().HaveProperty(typeof(string), null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("name"); + } + + [Fact] + public void When_asserting_a_type_has_a_property_with_an_empty_name_it_should_throw() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should().HaveProperty(typeof(string), string.Empty); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("name"); + } } - [Fact] - public void When_asserting_a_type_does_not_have_a_property_with_a_null_name_it_should_throw() + public class HavePropertyOfT { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should().NotHaveProperty(null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("name"); + [Fact] + public void When_asserting_a_type_has_a_propertyOfT_which_it_does_then_it_succeeds() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should() + .HaveProperty("PrivateWriteProtectedReadProperty") + .Which.Should() + .BeWritable(CSharpAccessModifier.Private) + .And.BeReadable(CSharpAccessModifier.Protected); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_has_a_propertyOfT_with_a_null_name_it_should_throw() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should().HaveProperty(null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("name"); + } + + [Fact] + public void When_asserting_a_type_has_a_propertyOfT_with_an_empty_name_it_should_throw() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should().HaveProperty(string.Empty); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("name"); + } } - [Fact] - public void When_asserting_a_type_does_not_have_a_property_with_an_empty_name_it_should_throw() + public class NotHaveProperty { - // Arrange - var type = typeof(ClassWithMembers); - - // Act - Action act = () => - type.Should().NotHaveProperty(string.Empty); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("name"); + [Fact] + public void When_asserting_a_type_does_not_have_a_property_which_it_does_not_it_succeeds() + { + // Arrange + var type = typeof(ClassWithoutMembers); + + // Act + Action act = () => + type.Should().NotHaveProperty("Property"); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_does_not_have_a_property_which_it_does_it_fails() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should().NotHaveProperty("PrivateWriteProtectedReadProperty", "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected String *.ClassWithMembers.PrivateWriteProtectedReadProperty to not exist *failure message*" + + ", but it does."); + } + + [Fact] + public void When_subject_is_null_not_have_property_should_fail() + { + // Arrange + Type type = null; + + // Act + Action act = () => + type.Should().NotHaveProperty("PublicProperty", "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage("Expected type.PublicProperty to not exist *failure message*, but type is ."); + } + + [Fact] + public void When_asserting_a_type_does_not_have_a_property_with_a_null_name_it_should_throw() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should().NotHaveProperty(null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("name"); + } + + [Fact] + public void When_asserting_a_type_does_not_have_a_property_with_an_empty_name_it_should_throw() + { + // Arrange + var type = typeof(ClassWithMembers); + + // Act + Action act = () => + type.Should().NotHaveProperty(string.Empty); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("name"); + } } - - #endregion } } diff --git a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.Implement.cs b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.Implement.cs index 6614396893..c1b06bf1bc 100644 --- a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.Implement.cs +++ b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.Implement.cs @@ -9,174 +9,170 @@ namespace FluentAssertions.Specs.Types /// public partial class TypeAssertionSpecs { - #region Implement - - [Fact] - public void When_asserting_a_type_implements_an_interface_which_it_does_then_it_succeeds() - { - // Arrange - var type = typeof(ClassThatImplementsInterface); - - // Act - Action act = () => - type.Should().Implement(typeof(IDummyInterface)); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_type_does_not_implement_an_interface_which_it_does_then_it_fails() + public class Implement { - // Arrange - var type = typeof(ClassThatDoesNotImplementInterface); - - // Act - Action act = () => - type.Should().Implement(typeof(IDummyInterface), "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type *.ClassThatDoesNotImplementInterface to implement interface *.IDummyInterface " + - "*failure message*, but it does not."); + [Fact] + public void When_asserting_a_type_implements_an_interface_which_it_does_then_it_succeeds() + { + // Arrange + var type = typeof(ClassThatImplementsInterface); + + // Act + Action act = () => + type.Should().Implement(typeof(IDummyInterface)); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_does_not_implement_an_interface_which_it_does_then_it_fails() + { + // Arrange + var type = typeof(ClassThatDoesNotImplementInterface); + + // Act + Action act = () => + type.Should().Implement(typeof(IDummyInterface), "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type *.ClassThatDoesNotImplementInterface to implement interface *.IDummyInterface " + + "*failure message*, but it does not."); + } + + [Fact] + public void When_asserting_a_type_implements_a_NonInterface_type_it_fails() + { + // Arrange + var type = typeof(ClassThatDoesNotImplementInterface); + + // Act + Action act = () => + type.Should().Implement(typeof(DateTime), "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type *.ClassThatDoesNotImplementInterface to implement interface *.DateTime *failure message*" + + ", but *.DateTime is not an interface."); + } + + [Fact] + public void When_asserting_a_type_to_implement_null_it_should_throw() + { + // Arrange + var type = typeof(DummyBaseType<>); + + // Act + Action act = () => + type.Should().Implement(null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("interfaceType"); + } } - - [Fact] - public void When_asserting_a_type_implements_a_NonInterface_type_it_fails() - { - // Arrange - var type = typeof(ClassThatDoesNotImplementInterface); - - // Act - Action act = () => - type.Should().Implement(typeof(DateTime), "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type *.ClassThatDoesNotImplementInterface to implement interface *.DateTime *failure message*" + - ", but *.DateTime is not an interface."); - } - - [Fact] - public void When_asserting_a_type_to_implement_null_it_should_throw() + + public class ImplementOfT { - // Arrange - var type = typeof(DummyBaseType<>); - - // Act - Action act = () => - type.Should().Implement(null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("interfaceType"); + [Fact] + public void When_asserting_a_type_implementsOfT_an_interface_which_it_does_then_it_succeeds() + { + // Arrange + var type = typeof(ClassThatImplementsInterface); + + // Act + Action act = () => + type.Should().Implement(); + + // Assert + act.Should().NotThrow(); + } } - #endregion - - #region ImplementOfT - - [Fact] - public void When_asserting_a_type_implementsOfT_an_interface_which_it_does_then_it_succeeds() + public class NotImplement { - // Arrange - var type = typeof(ClassThatImplementsInterface); - - // Act - Action act = () => - type.Should().Implement(); - - // Assert - act.Should().NotThrow(); + [Fact] + public void When_asserting_a_type_does_not_implement_an_interface_which_it_does_not_then_it_succeeds() + { + // Arrange + var type = typeof(ClassThatDoesNotImplementInterface); + + // Act + Action act = () => + type.Should().NotImplement(typeof(IDummyInterface)); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_type_implements_an_interface_which_it_does_not_then_it_fails() + { + // Arrange + var type = typeof(ClassThatImplementsInterface); + + // Act + Action act = () => + type.Should().NotImplement(typeof(IDummyInterface), "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type *.ClassThatImplementsInterface to not implement interface *.IDummyInterface " + + "*failure message*, but it does."); + } + + [Fact] + public void When_asserting_a_type_does_not_implement_a_NonInterface_type_it_fails() + { + // Arrange + var type = typeof(ClassThatDoesNotImplementInterface); + + // Act + Action act = () => + type.Should().NotImplement(typeof(DateTime), "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected type *.ClassThatDoesNotImplementInterface to not implement interface *.DateTime *failure message*" + + ", but *.DateTime is not an interface."); + } + + [Fact] + public void When_asserting_a_type_not_to_implement_null_it_should_throw() + { + // Arrange + var type = typeof(ClassThatDoesNotImplementInterface); + + // Act + Action act = () => + type.Should().NotImplement(null); + + // Assert + act.Should().ThrowExactly() + .WithParameterName("interfaceType"); + } } - #endregion - - #region NotImplement - - [Fact] - public void When_asserting_a_type_does_not_implement_an_interface_which_it_does_not_then_it_succeeds() + public class NotImplementOfT { - // Arrange - var type = typeof(ClassThatDoesNotImplementInterface); - - // Act - Action act = () => - type.Should().NotImplement(typeof(IDummyInterface)); - - // Assert - act.Should().NotThrow(); + [Fact] + public void When_asserting_a_type_does_not_implementOfT_an_interface_which_it_does_not_then_it_succeeds() + { + // Arrange + var type = typeof(ClassThatDoesNotImplementInterface); + + // Act + Action act = () => + type.Should().NotImplement(); + + // Assert + act.Should().NotThrow(); + } } - - [Fact] - public void When_asserting_a_type_implements_an_interface_which_it_does_not_then_it_fails() - { - // Arrange - var type = typeof(ClassThatImplementsInterface); - - // Act - Action act = () => - type.Should().NotImplement(typeof(IDummyInterface), "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type *.ClassThatImplementsInterface to not implement interface *.IDummyInterface " + - "*failure message*, but it does."); - } - - [Fact] - public void When_asserting_a_type_does_not_implement_a_NonInterface_type_it_fails() - { - // Arrange - var type = typeof(ClassThatDoesNotImplementInterface); - - // Act - Action act = () => - type.Should().NotImplement(typeof(DateTime), "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected type *.ClassThatDoesNotImplementInterface to not implement interface *.DateTime *failure message*" + - ", but *.DateTime is not an interface."); - } - - [Fact] - public void When_asserting_a_type_not_to_implement_null_it_should_throw() - { - // Arrange - var type = typeof(ClassThatDoesNotImplementInterface); - - // Act - Action act = () => - type.Should().NotImplement(null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("interfaceType"); - } - - #endregion - - #region NotImplementOfT - - [Fact] - public void When_asserting_a_type_does_not_implementOfT_an_interface_which_it_does_not_then_it_succeeds() - { - // Arrange - var type = typeof(ClassThatDoesNotImplementInterface); - - // Act - Action act = () => - type.Should().NotImplement(); - - // Assert - act.Should().NotThrow(); - } - - #endregion } } diff --git a/Tests/FluentAssertions.Specs/Types/TypeSelectorAssertionSpecs.cs b/Tests/FluentAssertions.Specs/Types/TypeSelectorAssertionSpecs.cs index 7e48528bed..467ebe4f59 100644 --- a/Tests/FluentAssertions.Specs/Types/TypeSelectorAssertionSpecs.cs +++ b/Tests/FluentAssertions.Specs/Types/TypeSelectorAssertionSpecs.cs @@ -10,794 +10,784 @@ namespace FluentAssertions.Specs.Types { public class TypeSelectorAssertionSpecs { - #region BeSealed - - [Fact] - public void When_all_types_are_sealed_it_succeeds() + public class BeSealed { - // Arrange - var types = new TypeSelector(new[] + [Fact] + public void When_all_types_are_sealed_it_succeeds() { - typeof(Sealed) - }); - - // Act / Assert - types.Should().BeSealed(); - } - - [Fact] - public void When_any_type_is_not_sealed_it_fails_with_a_meaningful_message() - { - // Arrange - var types = new TypeSelector(new[] + // Arrange + var types = new TypeSelector(new[] + { + typeof(Sealed) + }); + + // Act / Assert + types.Should().BeSealed(); + } + + [Fact] + public void When_any_type_is_not_sealed_it_fails_with_a_meaningful_message() { - typeof(Sealed), - typeof(Abstract) - }); + // Arrange + var types = new TypeSelector(new[] + { + typeof(Sealed), + typeof(Abstract) + }); - // Act - Action act = () => types.Should().BeSealed("we want to test the failure {0}", "message"); + // Act + Action act = () => types.Should().BeSealed("we want to test the failure {0}", "message"); - // Assert - act.Should().Throw() - .WithMessage( - "Expected all types to be sealed *failure message*, but the following types are not:*\"*.Abstract\"."); + // Assert + act.Should().Throw() + .WithMessage( + "Expected all types to be sealed *failure message*, but the following types are not:*\"*.Abstract\"."); + } } - #endregion - - #region NotBeSealed - - [Fact] - public void When_all_types_are_not_sealed_it_succeeds() + public class NotBeSealed { - // Arrange - var types = new TypeSelector(new[] + [Fact] + public void When_all_types_are_not_sealed_it_succeeds() { - typeof(Abstract) - }); - - // Act / Assert - types.Should().NotBeSealed(); - } - - [Fact] - public void When_any_type_is_sealed_it_fails_with_a_meaningful_message() - { - // Arrange - var types = new TypeSelector(new[] + // Arrange + var types = new TypeSelector(new[] + { + typeof(Abstract) + }); + + // Act / Assert + types.Should().NotBeSealed(); + } + + [Fact] + public void When_any_type_is_sealed_it_fails_with_a_meaningful_message() { - typeof(Abstract), - typeof(Sealed) - }); + // Arrange + var types = new TypeSelector(new[] + { + typeof(Abstract), + typeof(Sealed) + }); - // Act - Action act = () => types.Should().NotBeSealed("we want to test the failure {0}", "message"); + // Act + Action act = () => types.Should().NotBeSealed("we want to test the failure {0}", "message"); - // Assert - act.Should().Throw() - .WithMessage("Expected all types not to be sealed *failure message*, but the following types are:*\"*.Sealed\"."); + // Assert + act.Should().Throw() + .WithMessage("Expected all types not to be sealed *failure message*, but the following types are:*\"*.Sealed\"."); + } } - #endregion - - #region BeDecoratedWith - - [Fact] - public void When_asserting_a_selection_of_decorated_types_is_decorated_with_an_attribute_it_succeeds() + public class BeDecoratedWith { - // Arrange - var types = new TypeSelector(new[] + [Fact] + public void When_asserting_a_selection_of_decorated_types_is_decorated_with_an_attribute_it_succeeds() { - typeof(ClassWithAttribute) - }); - - // Act - Action act = () => - types.Should().BeDecoratedWith(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_selection_of_non_decorated_types_is_decorated_with_an_attribute_it_fails() - { - // Arrange - var types = new TypeSelector(new[] - { - typeof(ClassWithAttribute), - typeof(ClassWithoutAttribute), - typeof(OtherClassWithoutAttribute) - }); - - // Act - Action act = () => - types.Should().BeDecoratedWith("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected all types to be decorated with *.DummyClassAttribute *failure message*" + - ", but the attribute was not found on the following types:" + - "*\"*.ClassWithoutAttribute*.OtherClassWithoutAttribute\"."); - } - - [Fact] - public void When_injecting_a_null_predicate_into_TypeSelector_BeDecoratedWith_it_should_throw() - { - // Arrange - var types = new TypeSelector(typeof(ClassWithAttribute)); - - // Act - Action act = () => types.Should() - .BeDecoratedWith(isMatchingAttributePredicate: null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("isMatchingAttributePredicate"); - } - - [Fact] - public void When_asserting_a_selection_of_types_with_unexpected_attribute_property_it_fails() - { - // Arrange - var types = new TypeSelector(new[] - { - typeof(ClassWithAttribute), - typeof(ClassWithoutAttribute), - typeof(OtherClassWithoutAttribute) - }); - - // Act - Action act = () => - types.Should() - .BeDecoratedWith( - a => (a.Name == "Expected") && a.IsEnabled, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected all types to be decorated with *.DummyClassAttribute that matches " + - "(a.Name == \"Expected\") * a.IsEnabled *failure message*, but no matching attribute was found on " + - "the following types:*\"*.ClassWithoutAttribute*.OtherClassWithoutAttribute\"."); - } - - #endregion - - #region BeDecoratedWithOrInherit - - [Fact] - public void When_asserting_a_selection_of_decorated_types_inheriting_an_attribute_it_succeeds() - { - // Arrange - var types = new TypeSelector(new[] + // Arrange + var types = new TypeSelector(new[] + { + typeof(ClassWithAttribute) + }); + + // Act + Action act = () => + types.Should().BeDecoratedWith(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_selection_of_non_decorated_types_is_decorated_with_an_attribute_it_fails() { - typeof(ClassWithInheritedAttribute) - }); - - // Act - Action act = () => - types.Should().BeDecoratedWithOrInherit(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_selection_of_non_decorated_types_inheriting_an_attribute_it_fails() - { - // Arrange - var types = new TypeSelector(new[] - { - typeof(ClassWithAttribute), - typeof(ClassWithoutAttribute), - typeof(OtherClassWithoutAttribute) - }); - - // Act - Action act = () => - types.Should().BeDecoratedWithOrInherit("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected all types to be decorated with or inherit *.DummyClassAttribute *failure message*" + - ", but the attribute was not found on the following types:" + - "*\"*.ClassWithoutAttribute*.OtherClassWithoutAttribute\"."); - } - - [Fact] - public void When_injecting_a_null_predicate_into_TypeSelector_BeDecoratedWithOrInherit_it_should_throw() - { - // Arrange - var types = new TypeSelector(typeof(ClassWithAttribute)); - - // Act - Action act = () => types.Should() - .BeDecoratedWithOrInherit(isMatchingAttributePredicate: null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("isMatchingAttributePredicate"); - } - - [Fact] - public void When_asserting_a_selection_of_types_with_some_inheriting_attributes_with_unexpected_attribute_property_it_fails() - { - // Arrange - var types = new TypeSelector(new[] - { - typeof(ClassWithAttribute), - typeof(ClassWithInheritedAttribute), - typeof(ClassWithoutAttribute), - typeof(OtherClassWithoutAttribute) - }); - - // Act - Action act = () => - types.Should() - .BeDecoratedWithOrInherit( - a => (a.Name == "Expected") && a.IsEnabled, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected all types to be decorated with or inherit *.DummyClassAttribute that matches " + - "(a.Name == \"Expected\")*a.IsEnabled *failure message*, but no matching attribute was found " + - "on the following types:*\"*.ClassWithoutAttribute*.OtherClassWithoutAttribute\"."); - } + // Arrange + var types = new TypeSelector(new[] + { + typeof(ClassWithAttribute), + typeof(ClassWithoutAttribute), + typeof(OtherClassWithoutAttribute) + }); + + // Act + Action act = () => + types.Should().BeDecoratedWith("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected all types to be decorated with *.DummyClassAttribute *failure message*" + + ", but the attribute was not found on the following types:" + + "*\"*.ClassWithoutAttribute*.OtherClassWithoutAttribute\"."); + } + + [Fact] + public void When_injecting_a_null_predicate_into_TypeSelector_BeDecoratedWith_it_should_throw() + { + // Arrange + var types = new TypeSelector(typeof(ClassWithAttribute)); - #endregion + // Act + Action act = () => types.Should() + .BeDecoratedWith(isMatchingAttributePredicate: null); - #region NotBeDecoratedWith + // Assert + act.Should().ThrowExactly() + .WithParameterName("isMatchingAttributePredicate"); + } - [Fact] - public void When_asserting_a_selection_of_non_decorated_types_is_not_decorated_with_an_attribute_it_succeeds() - { - // Arrange - var types = new TypeSelector(new[] + [Fact] + public void When_asserting_a_selection_of_types_with_unexpected_attribute_property_it_fails() { - typeof(ClassWithoutAttribute), - typeof(OtherClassWithoutAttribute) - }); - - // Act - Action act = () => - types.Should().NotBeDecoratedWith(); + // Arrange + var types = new TypeSelector(new[] + { + typeof(ClassWithAttribute), + typeof(ClassWithoutAttribute), + typeof(OtherClassWithoutAttribute) + }); + + // Act + Action act = () => + types.Should() + .BeDecoratedWith( + a => (a.Name == "Expected") && a.IsEnabled, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected all types to be decorated with *.DummyClassAttribute that matches " + + "(a.Name == \"Expected\") * a.IsEnabled *failure message*, but no matching attribute was found on " + + "the following types:*\"*.ClassWithoutAttribute*.OtherClassWithoutAttribute\"."); + } + } + + public class BeDecoratedWithOrInherit + { + [Fact] + public void When_asserting_a_selection_of_decorated_types_inheriting_an_attribute_it_succeeds() + { + // Arrange + var types = new TypeSelector(new[] + { + typeof(ClassWithInheritedAttribute) + }); + + // Act + Action act = () => + types.Should().BeDecoratedWithOrInherit(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_selection_of_non_decorated_types_inheriting_an_attribute_it_fails() + { + // Arrange + var types = new TypeSelector(new[] + { + typeof(ClassWithAttribute), + typeof(ClassWithoutAttribute), + typeof(OtherClassWithoutAttribute) + }); + + // Act + Action act = () => + types.Should().BeDecoratedWithOrInherit("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected all types to be decorated with or inherit *.DummyClassAttribute *failure message*" + + ", but the attribute was not found on the following types:" + + "*\"*.ClassWithoutAttribute*.OtherClassWithoutAttribute\"."); + } + + [Fact] + public void When_injecting_a_null_predicate_into_TypeSelector_BeDecoratedWithOrInherit_it_should_throw() + { + // Arrange + var types = new TypeSelector(typeof(ClassWithAttribute)); - // Assert - act.Should().NotThrow(); - } + // Act + Action act = () => types.Should() + .BeDecoratedWithOrInherit(isMatchingAttributePredicate: null); - [Fact] - public void When_asserting_a_selection_of_decorated_types_is_not_decorated_with_an_attribute_it_fails() - { - // Arrange - var types = new TypeSelector(new[] - { - typeof(ClassWithoutAttribute), - typeof(ClassWithAttribute) - }); - - // Act - Action act = () => - types.Should().NotBeDecoratedWith("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected all types to not be decorated *.DummyClassAttribute *failure message*" + - ", but the attribute was found on the following types:*\"*.ClassWithAttribute\"."); - } + // Assert + act.Should().ThrowExactly() + .WithParameterName("isMatchingAttributePredicate"); + } - [Fact] - public void When_injecting_a_null_predicate_into_TypeSelector_NotBeDecoratedWith_it_should_throw() - { - // Arrange - var types = new TypeSelector(typeof(ClassWithAttribute)); + [Fact] + public void When_asserting_a_selection_of_types_with_some_inheriting_attributes_with_unexpected_attribute_property_it_fails() + { + // Arrange + var types = new TypeSelector(new[] + { + typeof(ClassWithAttribute), + typeof(ClassWithInheritedAttribute), + typeof(ClassWithoutAttribute), + typeof(OtherClassWithoutAttribute) + }); + + // Act + Action act = () => + types.Should() + .BeDecoratedWithOrInherit( + a => (a.Name == "Expected") && a.IsEnabled, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected all types to be decorated with or inherit *.DummyClassAttribute that matches " + + "(a.Name == \"Expected\")*a.IsEnabled *failure message*, but no matching attribute was found " + + "on the following types:*\"*.ClassWithoutAttribute*.OtherClassWithoutAttribute\"."); + } + } + + public class NotBeDecoratedWith + { + [Fact] + public void When_asserting_a_selection_of_non_decorated_types_is_not_decorated_with_an_attribute_it_succeeds() + { + // Arrange + var types = new TypeSelector(new[] + { + typeof(ClassWithoutAttribute), + typeof(OtherClassWithoutAttribute) + }); + + // Act + Action act = () => + types.Should().NotBeDecoratedWith(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_selection_of_decorated_types_is_not_decorated_with_an_attribute_it_fails() + { + // Arrange + var types = new TypeSelector(new[] + { + typeof(ClassWithoutAttribute), + typeof(ClassWithAttribute) + }); + + // Act + Action act = () => + types.Should().NotBeDecoratedWith("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected all types to not be decorated *.DummyClassAttribute *failure message*" + + ", but the attribute was found on the following types:*\"*.ClassWithAttribute\"."); + } + + [Fact] + public void When_injecting_a_null_predicate_into_TypeSelector_NotBeDecoratedWith_it_should_throw() + { + // Arrange + var types = new TypeSelector(typeof(ClassWithAttribute)); - // Act - Action act = () => types.Should() - .NotBeDecoratedWith(isMatchingAttributePredicate: null); + // Act + Action act = () => types.Should() + .NotBeDecoratedWith(isMatchingAttributePredicate: null); - // Assert - act.Should().ThrowExactly() - .WithParameterName("isMatchingAttributePredicate"); - } + // Assert + act.Should().ThrowExactly() + .WithParameterName("isMatchingAttributePredicate"); + } - [Fact] - public void When_asserting_a_selection_of_types_with_unexpected_attribute_and_unexpected_attribute_property_it_fails() - { - // Arrange - var types = new TypeSelector(new[] + [Fact] + public void When_asserting_a_selection_of_types_with_unexpected_attribute_and_unexpected_attribute_property_it_fails() { - typeof(ClassWithoutAttribute), - typeof(ClassWithAttribute) - }); + // Arrange + var types = new TypeSelector(new[] + { + typeof(ClassWithoutAttribute), + typeof(ClassWithAttribute) + }); + + // Act + Action act = () => + types.Should() + .NotBeDecoratedWith( + a => (a.Name == "Expected") && a.IsEnabled, "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected all types to not be decorated with *.DummyClassAttribute that matches " + + "(a.Name == \"Expected\") * a.IsEnabled *failure message*, but a matching attribute was found " + + "on the following types:*\"*.ClassWithAttribute\"."); + } + } + + public class NotBeDecoratedWithOrInherit + { + [Fact] + public void When_asserting_a_selection_of_non_decorated_types_does_not_inherit_an_attribute_it_succeeds() + { + // Arrange + var types = new TypeSelector(new[] + { + typeof(ClassWithoutAttribute), + typeof(OtherClassWithoutAttribute) + }); + + // Act + Action act = () => + types.Should().NotBeDecoratedWithOrInherit(); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_a_selection_of_decorated_types_does_not_inherit_an_attribute_it_fails() + { + // Arrange + var types = new TypeSelector(new[] + { + typeof(ClassWithoutAttribute), + typeof(ClassWithInheritedAttribute) + }); + + // Act + Action act = () => + types.Should().NotBeDecoratedWithOrInherit("we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected all types to not be decorated with or inherit *.DummyClassAttribute *failure message*" + + ", but the attribute was found on the following types:*\"*.ClassWithInheritedAttribute\"."); + } + + [Fact] + public void When_a_selection_of_types_do_inherit_unexpected_attribute_with_the_expected_properties_it_succeeds() + { + // Arrange + var types = new TypeSelector(typeof(ClassWithInheritedAttribute)); - // Act - Action act = () => - types.Should() - .NotBeDecoratedWith( + // Act + Action act = () => types.Should() + .NotBeDecoratedWithOrInherit( a => (a.Name == "Expected") && a.IsEnabled, "we want to test the failure {0}", "message"); - // Assert - act.Should().Throw() - .WithMessage( - "Expected all types to not be decorated with *.DummyClassAttribute that matches " + - "(a.Name == \"Expected\") * a.IsEnabled *failure message*, but a matching attribute was found " + - "on the following types:*\"*.ClassWithAttribute\"."); - } - - #endregion + // Assert + act.Should().Throw() + .WithMessage( + "Expected all types to not be decorated with or inherit *.DummyClassAttribute that matches " + + "(a.Name == \"Expected\") * a.IsEnabled *failure message*, but a matching attribute was found " + + "on the following types:*\"*.ClassWithInheritedAttribute\"."); + } - #region NotBeDecoratedWithOrInherit - - [Fact] - public void When_asserting_a_selection_of_non_decorated_types_does_not_inherit_an_attribute_it_succeeds() - { - // Arrange - var types = new TypeSelector(new[] + [Fact] + public void When_a_selection_of_types_do_not_inherit_unexpected_attribute_with_the_expected_properties_it_succeeds() { - typeof(ClassWithoutAttribute), - typeof(OtherClassWithoutAttribute) - }); - - // Act - Action act = () => - types.Should().NotBeDecoratedWithOrInherit(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_asserting_a_selection_of_decorated_types_does_not_inherit_an_attribute_it_fails() - { - // Arrange - var types = new TypeSelector(new[] - { - typeof(ClassWithoutAttribute), - typeof(ClassWithInheritedAttribute) - }); - - // Act - Action act = () => - types.Should().NotBeDecoratedWithOrInherit("we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected all types to not be decorated with or inherit *.DummyClassAttribute *failure message*" + - ", but the attribute was found on the following types:*\"*.ClassWithInheritedAttribute\"."); - } - - [Fact] - public void When_a_selection_of_types_do_inherit_unexpected_attribute_with_the_expected_properties_it_succeeds() - { - // Arrange - var types = new TypeSelector(typeof(ClassWithInheritedAttribute)); - - // Act - Action act = () => types.Should() - .NotBeDecoratedWithOrInherit( - a => (a.Name == "Expected") && a.IsEnabled, "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected all types to not be decorated with or inherit *.DummyClassAttribute that matches " + - "(a.Name == \"Expected\") * a.IsEnabled *failure message*, but a matching attribute was found " + - "on the following types:*\"*.ClassWithInheritedAttribute\"."); - } - - [Fact] - public void When_a_selection_of_types_do_not_inherit_unexpected_attribute_with_the_expected_properties_it_succeeds() - { - // Arrange - var types = new TypeSelector(typeof(ClassWithoutAttribute)); - - // Act - Action act = () => types.Should() - .NotBeDecoratedWithOrInherit(a => (a.Name == "Expected") && a.IsEnabled); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_injecting_a_null_predicate_into_TypeSelector_NotBeDecoratedWithOrInherit_it_should_throw() - { - // Arrange - var types = new TypeSelector(typeof(ClassWithAttribute)); - - // Act - Action act = () => types.Should() - .NotBeDecoratedWithOrInherit(isMatchingAttributePredicate: null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("isMatchingAttributePredicate"); - } - - #endregion - - #region BeInNamespace - - [Fact] - public void When_a_type_is_in_the_expected_namespace_it_should_not_throw() - { - // Arrange - var types = new TypeSelector(typeof(ClassInDummyNamespace)); + // Arrange + var types = new TypeSelector(typeof(ClassWithoutAttribute)); - // Act - Action act = () => types.Should().BeInNamespace(nameof(DummyNamespace)); + // Act + Action act = () => types.Should() + .NotBeDecoratedWithOrInherit(a => (a.Name == "Expected") && a.IsEnabled); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_a_type_is_not_in_the_expected_namespace_it_should_throw() - { - // Arrange - var types = new TypeSelector(new[] - { - typeof(ClassInDummyNamespace), - typeof(ClassNotInDummyNamespace), - typeof(OtherClassNotInDummyNamespace) - }); - - // Act - Action act = () => - types.Should().BeInNamespace(nameof(DummyNamespace), "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected all types to be in namespace \"DummyNamespace\" *failure message*, but the following types " + - "are in a different namespace:*\"*.ClassNotInDummyNamespace*.OtherClassNotInDummyNamespace\"."); - } - - [Fact] - public void When_a_type_is_in_the_expected_global_namespace_it_should_not_throw() - { - // Arrange - var types = new TypeSelector(typeof(ClassInGlobalNamespace)); + [Fact] + public void When_injecting_a_null_predicate_into_TypeSelector_NotBeDecoratedWithOrInherit_it_should_throw() + { + // Arrange + var types = new TypeSelector(typeof(ClassWithAttribute)); - // Act - Action act = () => types.Should().BeInNamespace(null); + // Act + Action act = () => types.Should() + .NotBeDecoratedWithOrInherit(isMatchingAttributePredicate: null); - // Assert - act.Should().NotThrow(); + // Assert + act.Should().ThrowExactly() + .WithParameterName("isMatchingAttributePredicate"); + } } - [Fact] - public void When_a_type_in_the_global_namespace_is_not_in_the_expected_namespace_it_should_throw() + public class BeInNamespace { - // Arrange - var types = new TypeSelector(typeof(ClassInGlobalNamespace)); + [Fact] + public void When_a_type_is_in_the_expected_namespace_it_should_not_throw() + { + // Arrange + var types = new TypeSelector(typeof(ClassInDummyNamespace)); - // Act - Action act = () => types.Should().BeInNamespace(nameof(DummyNamespace)); + // Act + Action act = () => types.Should().BeInNamespace(nameof(DummyNamespace)); - // Assert - act.Should().Throw() - .WithMessage( - "Expected all types to be in namespace \"DummyNamespace\", but the following types " + - "are in a different namespace:*\"ClassInGlobalNamespace\"."); - } - - [Fact] - public void When_a_type_is_not_in_the_expected_global_namespace_it_should_throw() - { - // Arrange - var types = new TypeSelector(new[] - { - typeof(ClassInDummyNamespace), - typeof(ClassNotInDummyNamespace), - typeof(OtherClassNotInDummyNamespace) - }); - - // Act - Action act = () => types.Should().BeInNamespace(null); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected all types to be in namespace , but the following types are in a different namespace:" + - "*\"*.ClassInDummyNamespace*.ClassNotInDummyNamespace*.OtherClassNotInDummyNamespace\"."); - } + // Assert + act.Should().NotThrow(); + } - #endregion + [Fact] + public void When_a_type_is_not_in_the_expected_namespace_it_should_throw() + { + // Arrange + var types = new TypeSelector(new[] + { + typeof(ClassInDummyNamespace), + typeof(ClassNotInDummyNamespace), + typeof(OtherClassNotInDummyNamespace) + }); + + // Act + Action act = () => + types.Should().BeInNamespace(nameof(DummyNamespace), "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected all types to be in namespace \"DummyNamespace\" *failure message*, but the following types " + + "are in a different namespace:*\"*.ClassNotInDummyNamespace*.OtherClassNotInDummyNamespace\"."); + } + + [Fact] + public void When_a_type_is_in_the_expected_global_namespace_it_should_not_throw() + { + // Arrange + var types = new TypeSelector(typeof(ClassInGlobalNamespace)); - #region NotBeInNamespace + // Act + Action act = () => types.Should().BeInNamespace(null); - [Fact] - public void When_a_type_is_not_in_the_unexpected_namespace_it_should_not_throw() - { - // Arrange - var types = new TypeSelector(typeof(ClassInDummyNamespace)); + // Assert + act.Should().NotThrow(); + } - // Act - Action act = () => - types.Should().NotBeInNamespace($"{nameof(DummyNamespace)}.{nameof(DummyNamespace.InnerDummyNamespace)}"); + [Fact] + public void When_a_type_in_the_global_namespace_is_not_in_the_expected_namespace_it_should_throw() + { + // Arrange + var types = new TypeSelector(typeof(ClassInGlobalNamespace)); - // Assert - act.Should().NotThrow(); - } + // Act + Action act = () => types.Should().BeInNamespace(nameof(DummyNamespace)); - [Fact] - public void When_a_type_is_not_in_the_unexpected_parent_namespace_it_should_not_throw() - { - // Arrange - var types = new TypeSelector(typeof(ClassInInnerDummyNamespace)); + // Assert + act.Should().Throw() + .WithMessage( + "Expected all types to be in namespace \"DummyNamespace\", but the following types " + + "are in a different namespace:*\"ClassInGlobalNamespace\"."); + } - // Act - Action act = () => types.Should().NotBeInNamespace(nameof(DummyNamespace)); + [Fact] + public void When_a_type_is_not_in_the_expected_global_namespace_it_should_throw() + { + // Arrange + var types = new TypeSelector(new[] + { + typeof(ClassInDummyNamespace), + typeof(ClassNotInDummyNamespace), + typeof(OtherClassNotInDummyNamespace) + }); - // Assert - act.Should().NotThrow(); - } + // Act + Action act = () => types.Should().BeInNamespace(null); - [Fact] - public void When_a_type_is_in_the_unexpected_namespace_it_should_throw() - { - // Arrange - var types = new TypeSelector(new[] - { - typeof(ClassInDummyNamespace), - typeof(ClassNotInDummyNamespace), - typeof(OtherClassNotInDummyNamespace) - }); - - // Act - Action act = () => - types.Should().NotBeInNamespace(nameof(DummyNamespace), "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected no types to be in namespace \"DummyNamespace\" *failure message*" + - ", but the following types are in the namespace:*\"DummyNamespace.ClassInDummyNamespace\"."); + // Assert + act.Should().Throw() + .WithMessage( + "Expected all types to be in namespace , but the following types are in a different namespace:" + + "*\"*.ClassInDummyNamespace*.ClassNotInDummyNamespace*.OtherClassNotInDummyNamespace\"."); + } } - #endregion - - #region BeUnderNamespace - - [Fact] - public void When_a_type_is_under_the_expected_namespace_it_should_not_throw() + public class NotBeInNamespace { - // Arrange - var types = new TypeSelector(typeof(ClassInDummyNamespace)); + [Fact] + public void When_a_type_is_not_in_the_unexpected_namespace_it_should_not_throw() + { + // Arrange + var types = new TypeSelector(typeof(ClassInDummyNamespace)); - // Act - Action act = () => types.Should().BeUnderNamespace(nameof(DummyNamespace)); + // Act + Action act = () => + types.Should().NotBeInNamespace($"{nameof(DummyNamespace)}.{nameof(DummyNamespace.InnerDummyNamespace)}"); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_a_type_is_under_the_expected_nested_namespace_it_should_not_throw() - { - // Arrange - var types = new TypeSelector(typeof(ClassInInnerDummyNamespace)); + [Fact] + public void When_a_type_is_not_in_the_unexpected_parent_namespace_it_should_not_throw() + { + // Arrange + var types = new TypeSelector(typeof(ClassInInnerDummyNamespace)); - // Act - Action act = () => - types.Should().BeUnderNamespace($"{nameof(DummyNamespace)}.{nameof(DummyNamespace.InnerDummyNamespace)}"); + // Act + Action act = () => types.Should().NotBeInNamespace(nameof(DummyNamespace)); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_a_type_is_under_the_expected_parent_namespace_it_should_not_throw() - { - // Arrange - var types = new TypeSelector(typeof(ClassInInnerDummyNamespace)); + [Fact] + public void When_a_type_is_in_the_unexpected_namespace_it_should_throw() + { + // Arrange + var types = new TypeSelector(new[] + { + typeof(ClassInDummyNamespace), + typeof(ClassNotInDummyNamespace), + typeof(OtherClassNotInDummyNamespace) + }); + + // Act + Action act = () => + types.Should().NotBeInNamespace(nameof(DummyNamespace), "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected no types to be in namespace \"DummyNamespace\" *failure message*" + + ", but the following types are in the namespace:*\"DummyNamespace.ClassInDummyNamespace\"."); + } + } + + public class BeUnderNamespace + { + [Fact] + public void When_a_type_is_under_the_expected_namespace_it_should_not_throw() + { + // Arrange + var types = new TypeSelector(typeof(ClassInDummyNamespace)); - // Act - Action act = () => types.Should().BeUnderNamespace(nameof(DummyNamespace)); + // Act + Action act = () => types.Should().BeUnderNamespace(nameof(DummyNamespace)); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_a_type_is_exactly_under_the_expected_global_namespace_it_should_not_throw() - { - // Arrange - var types = new TypeSelector(typeof(ClassInGlobalNamespace)); + [Fact] + public void When_a_type_is_under_the_expected_nested_namespace_it_should_not_throw() + { + // Arrange + var types = new TypeSelector(typeof(ClassInInnerDummyNamespace)); - // Act - Action act = () => types.Should().BeUnderNamespace(null); + // Act + Action act = () => + types.Should().BeUnderNamespace($"{nameof(DummyNamespace)}.{nameof(DummyNamespace.InnerDummyNamespace)}"); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_a_type_is_under_the_expected_global_namespace_it_should_not_throw() - { - // Arrange - var types = new TypeSelector(new[] + [Fact] + public void When_a_type_is_under_the_expected_parent_namespace_it_should_not_throw() { - typeof(ClassInDummyNamespace), - typeof(ClassNotInDummyNamespace), - typeof(OtherClassNotInDummyNamespace) - }); + // Arrange + var types = new TypeSelector(typeof(ClassInInnerDummyNamespace)); - // Act - Action act = () => types.Should().BeUnderNamespace(null); + // Act + Action act = () => types.Should().BeUnderNamespace(nameof(DummyNamespace)); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_a_type_only_shares_a_prefix_with_the_expected_namespace_it_should_throw() - { - // Arrange - var types = new TypeSelector(typeof(ClassInDummyNamespaceTwo)); + [Fact] + public void When_a_type_is_exactly_under_the_expected_global_namespace_it_should_not_throw() + { + // Arrange + var types = new TypeSelector(typeof(ClassInGlobalNamespace)); - // Act - Action act = () => - types.Should().BeUnderNamespace(nameof(DummyNamespace), "we want to test the failure {0}", "message"); + // Act + Action act = () => types.Should().BeUnderNamespace(null); - // Assert - act.Should().Throw() - .WithMessage("Expected the namespaces of all types to start with \"DummyNamespace\" *failure message*" + - ", but the namespaces of the following types do not start with it:*\"*.ClassInDummyNamespaceTwo\"."); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_asserting_a_selection_of_types_not_under_a_namespace_is_under_that_namespace_it_fails() - { - // Arrange - var types = new TypeSelector(new[] - { - typeof(ClassInDummyNamespace), - typeof(ClassInInnerDummyNamespace) - }); - - // Act - Action act = () => - types.Should().BeUnderNamespace($"{nameof(DummyNamespace)}.{nameof(DummyNamespace.InnerDummyNamespace)}"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected the namespaces of all types to start with \"DummyNamespace.InnerDummyNamespace\"" + - ", but the namespaces of the following types do not start with it:*\"*.ClassInDummyNamespace\"."); - } + [Fact] + public void When_a_type_is_under_the_expected_global_namespace_it_should_not_throw() + { + // Arrange + var types = new TypeSelector(new[] + { + typeof(ClassInDummyNamespace), + typeof(ClassNotInDummyNamespace), + typeof(OtherClassNotInDummyNamespace) + }); + + // Act + Action act = () => types.Should().BeUnderNamespace(null); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_a_type_only_shares_a_prefix_with_the_expected_namespace_it_should_throw() + { + // Arrange + var types = new TypeSelector(typeof(ClassInDummyNamespaceTwo)); - #endregion + // Act + Action act = () => + types.Should().BeUnderNamespace(nameof(DummyNamespace), "we want to test the failure {0}", "message"); - #region NotBeUnderNamespace + // Assert + act.Should().Throw() + .WithMessage("Expected the namespaces of all types to start with \"DummyNamespace\" *failure message*" + + ", but the namespaces of the following types do not start with it:*\"*.ClassInDummyNamespaceTwo\"."); + } - [Fact] - public void When_a_types_is_not_under_the_unexpected_namespace_it_should_not_throw() - { - // Arrange - var types = new TypeSelector(new[] + [Fact] + public void When_asserting_a_selection_of_types_not_under_a_namespace_is_under_that_namespace_it_fails() { - typeof(ClassInDummyNamespace), - typeof(ClassNotInDummyNamespace), - typeof(OtherClassNotInDummyNamespace) - }); - - // Act - Action act = () => - types.Should().NotBeUnderNamespace($"{nameof(DummyNamespace)}.{nameof(DummyNamespace.InnerDummyNamespace)}"); + // Arrange + var types = new TypeSelector(new[] + { + typeof(ClassInDummyNamespace), + typeof(ClassInInnerDummyNamespace) + }); - // Assert - act.Should().NotThrow(); - } + // Act + Action act = () => + types.Should().BeUnderNamespace($"{nameof(DummyNamespace)}.{nameof(DummyNamespace.InnerDummyNamespace)}"); - [Fact] - public void When_a_type_is_under_the_unexpected_namespace_it_shold_throw() - { - // Arrange - var types = new TypeSelector(typeof(ClassInDummyNamespace)); - - // Act - Action act = () => - types.Should().NotBeUnderNamespace(nameof(DummyNamespace), "we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected the namespaces of all types to not start with \"DummyNamespace\" *failure message*" + - ", but the namespaces of the following types start with it:*\"*.ClassInDummyNamespace\"."); - } - - [Fact] - public void When_a_type_is_under_the_unexpected_nested_namespace_it_should_throw() - { - // Arrange - var types = new TypeSelector(typeof(ClassInInnerDummyNamespace)); - - // Act - Action act = () => - types.Should().NotBeUnderNamespace($"{nameof(DummyNamespace)}.{nameof(DummyNamespace.InnerDummyNamespace)}"); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected the namespaces of all types to not start with \"DummyNamespace.InnerDummyNamespace\"" + - ", but the namespaces of the following types start with it:*\"*.ClassInInnerDummyNamespace\"."); + // Assert + act.Should().Throw() + .WithMessage( + "Expected the namespaces of all types to start with \"DummyNamespace.InnerDummyNamespace\"" + + ", but the namespaces of the following types do not start with it:*\"*.ClassInDummyNamespace\"."); + } } - [Fact] - public void When_a_type_is_under_the_unexpected_parent_namespace_it_should_throw() + public class NotBeUnderNamespace { - // Arrange - var types = new TypeSelector(typeof(ClassInInnerDummyNamespace)); - - // Act - Action act = () => types.Should().NotBeUnderNamespace(nameof(DummyNamespace)); + [Fact] + public void When_a_types_is_not_under_the_unexpected_namespace_it_should_not_throw() + { + // Arrange + var types = new TypeSelector(new[] + { + typeof(ClassInDummyNamespace), + typeof(ClassNotInDummyNamespace), + typeof(OtherClassNotInDummyNamespace) + }); + + // Act + Action act = () => + types.Should().NotBeUnderNamespace($"{nameof(DummyNamespace)}.{nameof(DummyNamespace.InnerDummyNamespace)}"); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_a_type_is_under_the_unexpected_namespace_it_shold_throw() + { + // Arrange + var types = new TypeSelector(typeof(ClassInDummyNamespace)); + + // Act + Action act = () => + types.Should().NotBeUnderNamespace(nameof(DummyNamespace), "we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected the namespaces of all types to not start with \"DummyNamespace\" *failure message*" + + ", but the namespaces of the following types start with it:*\"*.ClassInDummyNamespace\"."); + } + + [Fact] + public void When_a_type_is_under_the_unexpected_nested_namespace_it_should_throw() + { + // Arrange + var types = new TypeSelector(typeof(ClassInInnerDummyNamespace)); + + // Act + Action act = () => + types.Should().NotBeUnderNamespace($"{nameof(DummyNamespace)}.{nameof(DummyNamespace.InnerDummyNamespace)}"); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected the namespaces of all types to not start with \"DummyNamespace.InnerDummyNamespace\"" + + ", but the namespaces of the following types start with it:*\"*.ClassInInnerDummyNamespace\"."); + } + + [Fact] + public void When_a_type_is_under_the_unexpected_parent_namespace_it_should_throw() + { + // Arrange + var types = new TypeSelector(typeof(ClassInInnerDummyNamespace)); - // Assert - act.Should().Throw() - .WithMessage( - "Expected the namespaces of all types to not start with \"DummyNamespace\"" + - ", but the namespaces of the following types start with it:*\"*.ClassInInnerDummyNamespace\"."); - } + // Act + Action act = () => types.Should().NotBeUnderNamespace(nameof(DummyNamespace)); - [Fact] - public void When_a_type_is_under_the_unexpected_global_namespace_it_should_throw() - { - // Arrange - var types = new TypeSelector(typeof(ClassInGlobalNamespace)); + // Assert + act.Should().Throw() + .WithMessage( + "Expected the namespaces of all types to not start with \"DummyNamespace\"" + + ", but the namespaces of the following types start with it:*\"*.ClassInInnerDummyNamespace\"."); + } - // Act - Action act = () => types.Should().NotBeUnderNamespace(null); + [Fact] + public void When_a_type_is_under_the_unexpected_global_namespace_it_should_throw() + { + // Arrange + var types = new TypeSelector(typeof(ClassInGlobalNamespace)); - // Assert - act.Should().Throw() - .WithMessage( - "Expected the namespaces of all types to not start with " + - ", but the namespaces of the following types start with it:*\"ClassInGlobalNamespace\"."); - } + // Act + Action act = () => types.Should().NotBeUnderNamespace(null); - [Fact] - public void When_a_type_is_under_the_unexpected_parent_global_namespace_it_should_throw() - { - // Arrange - var types = new TypeSelector(new[] - { - typeof(ClassInDummyNamespace), - typeof(ClassNotInDummyNamespace), - typeof(OtherClassNotInDummyNamespace) - }); - - // Act - Action act = () => types.Should().NotBeUnderNamespace(null); - - // Assert - act.Should().Throw() - .WithMessage( - "Expected the namespaces of all types to not start with , but the namespaces of the following types " + - "start with it:*\"*.ClassInDummyNamespace*.ClassNotInDummyNamespace*.OtherClassNotInDummyNamespace\"."); - } + // Assert + act.Should().Throw() + .WithMessage( + "Expected the namespaces of all types to not start with " + + ", but the namespaces of the following types start with it:*\"ClassInGlobalNamespace\"."); + } - [Fact] - public void When_a_type_only_shares_a_prefix_with_the_unexpected_namespace_it_should_not_throw() - { - // Arrange - var types = new TypeSelector(typeof(ClassInDummyNamespaceTwo)); + [Fact] + public void When_a_type_is_under_the_unexpected_parent_global_namespace_it_should_throw() + { + // Arrange + var types = new TypeSelector(new[] + { + typeof(ClassInDummyNamespace), + typeof(ClassNotInDummyNamespace), + typeof(OtherClassNotInDummyNamespace) + }); + + // Act + Action act = () => types.Should().NotBeUnderNamespace(null); + + // Assert + act.Should().Throw() + .WithMessage( + "Expected the namespaces of all types to not start with , but the namespaces of the following types " + + "start with it:*\"*.ClassInDummyNamespace*.ClassNotInDummyNamespace*.OtherClassNotInDummyNamespace\"."); + } + + [Fact] + public void When_a_type_only_shares_a_prefix_with_the_unexpected_namespace_it_should_not_throw() + { + // Arrange + var types = new TypeSelector(typeof(ClassInDummyNamespaceTwo)); - // Act - Action act = () => types.Should().NotBeUnderNamespace(nameof(DummyNamespace)); + // Act + Action act = () => types.Should().NotBeUnderNamespace(nameof(DummyNamespace)); - // Assert - act.Should().NotThrow(); + // Assert + act.Should().NotThrow(); + } } - - #endregion } }