/Dev/Contributions/FluentAssertions.Specs/GenericCollectionAssertionsSpecs.cs
C# | 198 lines | 85 code | 29 blank | 84 comment | 3 complexity | 137eae1d526e114f6d5ba7b15db46982 MD5 | raw file
- using System;
- using System.Collections.Generic;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
-
- namespace FluentAssertions.specs
- {
- [TestClass]
- public class GenericCollectionAssertionsSpecs
- {
- [TestMethod]
- public void When_collection_does_not_contain_an_expected_item_matching_a_predicate_it_should_throw()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- IEnumerable<int> collection = new[] { 1, 2, 3 };
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- Action act = () => collection.Should().Contain(item => item > 3, "at least {0} item should be larger than 3", 1);
-
- //-----------------------------------------------------------------------------------------------------------
- // Assert
- //-----------------------------------------------------------------------------------------------------------
- act.ShouldThrow<AssertFailedException>().WithMessage(
- "Collection {1, 2, 3} should have an item matching (item > 3) because at least 1 item should be larger than 3.");
- }
-
- [TestMethod]
- public void When_collection_does_contain_an_expected_item_matching_a_predicate_it_should_not_throw()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- IEnumerable<int> collection = new[] { 1, 2, 3 };
-
- //-----------------------------------------------------------------------------------------------------------
- // Act / Assert
- //-----------------------------------------------------------------------------------------------------------
- collection.Should().Contain(item => item == 2);
- }
-
- [TestMethod]
- public void When_a_collection_of_strings_contains_the_expected_string_it_should_not_throw()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- var strings = new[] { "string1", "string2", "string3" };
-
- //-----------------------------------------------------------------------------------------------------------
- // Act / Assert
- //-----------------------------------------------------------------------------------------------------------
- strings.Should().Contain("string2");
- }
-
- [TestMethod]
- public void When_a_collection_of_strings_does_not_contain_the_expected_string_it_should_throw()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- var strings = new[] { "string1", "string2", "string3" };
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- Action act = () => strings.Should().Contain("string4", "because {0} is required", "4");
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- act.ShouldThrow<AssertFailedException>().WithMessage(
- "Expected collection {\"string1\", \"string2\", \"string3\"} to contain \"string4\" because 4 is required.");
- }
-
- [TestMethod]
- public void When_a_collection_does_not_contain_the_combination_of_a_collection_and_a_single_item_it_should_throw()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- var strings = new List<string> { "string1", "string2" };
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- Action act = () => strings.Should().Contain(strings, "string3");
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- act.ShouldThrow<AssertFailedException>().WithMessage(
- "Expected collection {\"string1\", \"string2\"} to contain {\"string1\", \"string2\", \"string3\"}, but could not find {\"string3\"}.");
- }
-
- [TestMethod]
- public void When_asserting_collection_contains_some_values_but_collection_is_null_it_should_throw()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- string[] strings = null;
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- Action act = () => strings.Should().Contain("string4", "because we're checking how it reacts to a null subject");
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- act.ShouldThrow<AssertFailedException>().WithMessage(
- "Expected collection to contain \"string4\" because we're checking how it reacts to a null subject, but found <null>.");
- }
-
-
- [TestMethod]
- public void When_asserting_collection_contains_values_according_to_predicate_but_collection_is_null_it_should_throw()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- List<string> strings = null;
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- Action act = () => strings.Should().Contain(x => x == "xxx", "because we're checking how it reacts to a null subject");
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- act.ShouldThrow<AssertFailedException>().WithMessage(
- "Expected collection to contain (x == \"xxx\") because we're checking how it reacts to a null subject, but found <null>.");
- }
-
- [TestMethod]
- public void When_asserting_collection_doesnt_contain_values_according_to_predicate_but_collection_is_null_it_should_throw()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- List<string> strings = null;
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- Action act = () => strings.Should().NotContain(x => x == "xxx", "because we're checking how it reacts to a null subject");
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- act.ShouldThrow<AssertFailedException>().WithMessage(
- "Expected collection not to contain (x == \"xxx\") because we're checking how it reacts to a null subject, but found <null>.");
- }
-
- [TestMethod]
- public void When_a_collection_contains_items_not_matching_a_predicate_it_should_throw()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- var strings = new List<int> { 2, 12, 3, 11, 2};
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- Action act = () => strings.Should().OnlyContain(i => i <= 10, "10 is the maximum");
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- act.ShouldThrow<AssertFailedException>().WithMessage(
- "Expected collection to contain only items matching (i <= 10) because 10 is the maximum, but {12, 11} do(es) not match.");
- }
-
- [TestMethod]
- public void When_a_collection_contains_only_items_matching_a_predicate_it_should_not_throw()
- {
- //-----------------------------------------------------------------------------------------------------------
- // Arrange
- //-----------------------------------------------------------------------------------------------------------
- var strings = new List<int> { 2, 9, 3, 8, 2};
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- Action act = () => strings.Should().OnlyContain(i => i <= 10);
-
- //-----------------------------------------------------------------------------------------------------------
- // Act
- //-----------------------------------------------------------------------------------------------------------
- act.ShouldNotThrow(); ;
- }
- }
- }