PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Dev/Contributions/FluentAssertions.Specs/GenericCollectionAssertionsSpecs.cs

#
C# | 198 lines | 85 code | 29 blank | 84 comment | 3 complexity | 137eae1d526e114f6d5ba7b15db46982 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. namespace FluentAssertions.specs
  5. {
  6. [TestClass]
  7. public class GenericCollectionAssertionsSpecs
  8. {
  9. [TestMethod]
  10. public void When_collection_does_not_contain_an_expected_item_matching_a_predicate_it_should_throw()
  11. {
  12. //-----------------------------------------------------------------------------------------------------------
  13. // Arrange
  14. //-----------------------------------------------------------------------------------------------------------
  15. IEnumerable<int> collection = new[] { 1, 2, 3 };
  16. //-----------------------------------------------------------------------------------------------------------
  17. // Act
  18. //-----------------------------------------------------------------------------------------------------------
  19. Action act = () => collection.Should().Contain(item => item > 3, "at least {0} item should be larger than 3", 1);
  20. //-----------------------------------------------------------------------------------------------------------
  21. // Assert
  22. //-----------------------------------------------------------------------------------------------------------
  23. act.ShouldThrow<AssertFailedException>().WithMessage(
  24. "Collection {1, 2, 3} should have an item matching (item > 3) because at least 1 item should be larger than 3.");
  25. }
  26. [TestMethod]
  27. public void When_collection_does_contain_an_expected_item_matching_a_predicate_it_should_not_throw()
  28. {
  29. //-----------------------------------------------------------------------------------------------------------
  30. // Arrange
  31. //-----------------------------------------------------------------------------------------------------------
  32. IEnumerable<int> collection = new[] { 1, 2, 3 };
  33. //-----------------------------------------------------------------------------------------------------------
  34. // Act / Assert
  35. //-----------------------------------------------------------------------------------------------------------
  36. collection.Should().Contain(item => item == 2);
  37. }
  38. [TestMethod]
  39. public void When_a_collection_of_strings_contains_the_expected_string_it_should_not_throw()
  40. {
  41. //-----------------------------------------------------------------------------------------------------------
  42. // Arrange
  43. //-----------------------------------------------------------------------------------------------------------
  44. var strings = new[] { "string1", "string2", "string3" };
  45. //-----------------------------------------------------------------------------------------------------------
  46. // Act / Assert
  47. //-----------------------------------------------------------------------------------------------------------
  48. strings.Should().Contain("string2");
  49. }
  50. [TestMethod]
  51. public void When_a_collection_of_strings_does_not_contain_the_expected_string_it_should_throw()
  52. {
  53. //-----------------------------------------------------------------------------------------------------------
  54. // Arrange
  55. //-----------------------------------------------------------------------------------------------------------
  56. var strings = new[] { "string1", "string2", "string3" };
  57. //-----------------------------------------------------------------------------------------------------------
  58. // Act
  59. //-----------------------------------------------------------------------------------------------------------
  60. Action act = () => strings.Should().Contain("string4", "because {0} is required", "4");
  61. //-----------------------------------------------------------------------------------------------------------
  62. // Act
  63. //-----------------------------------------------------------------------------------------------------------
  64. act.ShouldThrow<AssertFailedException>().WithMessage(
  65. "Expected collection {\"string1\", \"string2\", \"string3\"} to contain \"string4\" because 4 is required.");
  66. }
  67. [TestMethod]
  68. public void When_a_collection_does_not_contain_the_combination_of_a_collection_and_a_single_item_it_should_throw()
  69. {
  70. //-----------------------------------------------------------------------------------------------------------
  71. // Arrange
  72. //-----------------------------------------------------------------------------------------------------------
  73. var strings = new List<string> { "string1", "string2" };
  74. //-----------------------------------------------------------------------------------------------------------
  75. // Act
  76. //-----------------------------------------------------------------------------------------------------------
  77. Action act = () => strings.Should().Contain(strings, "string3");
  78. //-----------------------------------------------------------------------------------------------------------
  79. // Act
  80. //-----------------------------------------------------------------------------------------------------------
  81. act.ShouldThrow<AssertFailedException>().WithMessage(
  82. "Expected collection {\"string1\", \"string2\"} to contain {\"string1\", \"string2\", \"string3\"}, but could not find {\"string3\"}.");
  83. }
  84. [TestMethod]
  85. public void When_asserting_collection_contains_some_values_but_collection_is_null_it_should_throw()
  86. {
  87. //-----------------------------------------------------------------------------------------------------------
  88. // Arrange
  89. //-----------------------------------------------------------------------------------------------------------
  90. string[] strings = null;
  91. //-----------------------------------------------------------------------------------------------------------
  92. // Act
  93. //-----------------------------------------------------------------------------------------------------------
  94. Action act = () => strings.Should().Contain("string4", "because we're checking how it reacts to a null subject");
  95. //-----------------------------------------------------------------------------------------------------------
  96. // Act
  97. //-----------------------------------------------------------------------------------------------------------
  98. act.ShouldThrow<AssertFailedException>().WithMessage(
  99. "Expected collection to contain \"string4\" because we're checking how it reacts to a null subject, but found <null>.");
  100. }
  101. [TestMethod]
  102. public void When_asserting_collection_contains_values_according_to_predicate_but_collection_is_null_it_should_throw()
  103. {
  104. //-----------------------------------------------------------------------------------------------------------
  105. // Arrange
  106. //-----------------------------------------------------------------------------------------------------------
  107. List<string> strings = null;
  108. //-----------------------------------------------------------------------------------------------------------
  109. // Act
  110. //-----------------------------------------------------------------------------------------------------------
  111. Action act = () => strings.Should().Contain(x => x == "xxx", "because we're checking how it reacts to a null subject");
  112. //-----------------------------------------------------------------------------------------------------------
  113. // Act
  114. //-----------------------------------------------------------------------------------------------------------
  115. act.ShouldThrow<AssertFailedException>().WithMessage(
  116. "Expected collection to contain (x == \"xxx\") because we're checking how it reacts to a null subject, but found <null>.");
  117. }
  118. [TestMethod]
  119. public void When_asserting_collection_doesnt_contain_values_according_to_predicate_but_collection_is_null_it_should_throw()
  120. {
  121. //-----------------------------------------------------------------------------------------------------------
  122. // Arrange
  123. //-----------------------------------------------------------------------------------------------------------
  124. List<string> strings = null;
  125. //-----------------------------------------------------------------------------------------------------------
  126. // Act
  127. //-----------------------------------------------------------------------------------------------------------
  128. Action act = () => strings.Should().NotContain(x => x == "xxx", "because we're checking how it reacts to a null subject");
  129. //-----------------------------------------------------------------------------------------------------------
  130. // Act
  131. //-----------------------------------------------------------------------------------------------------------
  132. act.ShouldThrow<AssertFailedException>().WithMessage(
  133. "Expected collection not to contain (x == \"xxx\") because we're checking how it reacts to a null subject, but found <null>.");
  134. }
  135. [TestMethod]
  136. public void When_a_collection_contains_items_not_matching_a_predicate_it_should_throw()
  137. {
  138. //-----------------------------------------------------------------------------------------------------------
  139. // Arrange
  140. //-----------------------------------------------------------------------------------------------------------
  141. var strings = new List<int> { 2, 12, 3, 11, 2};
  142. //-----------------------------------------------------------------------------------------------------------
  143. // Act
  144. //-----------------------------------------------------------------------------------------------------------
  145. Action act = () => strings.Should().OnlyContain(i => i <= 10, "10 is the maximum");
  146. //-----------------------------------------------------------------------------------------------------------
  147. // Act
  148. //-----------------------------------------------------------------------------------------------------------
  149. act.ShouldThrow<AssertFailedException>().WithMessage(
  150. "Expected collection to contain only items matching (i <= 10) because 10 is the maximum, but {12, 11} do(es) not match.");
  151. }
  152. [TestMethod]
  153. public void When_a_collection_contains_only_items_matching_a_predicate_it_should_not_throw()
  154. {
  155. //-----------------------------------------------------------------------------------------------------------
  156. // Arrange
  157. //-----------------------------------------------------------------------------------------------------------
  158. var strings = new List<int> { 2, 9, 3, 8, 2};
  159. //-----------------------------------------------------------------------------------------------------------
  160. // Act
  161. //-----------------------------------------------------------------------------------------------------------
  162. Action act = () => strings.Should().OnlyContain(i => i <= 10);
  163. //-----------------------------------------------------------------------------------------------------------
  164. // Act
  165. //-----------------------------------------------------------------------------------------------------------
  166. act.ShouldNotThrow(); ;
  167. }
  168. }
  169. }