PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Framework/src/Ncqrs.Tests/InternalExtensionsTests.cs

https://github.com/elfrostie/ncqrs
C# | 175 lines | 142 code | 33 blank | 0 comment | 0 complexity | 7e01f1f97704e0f97cc17d3e9805fefa MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using FluentAssertions;
  4. using NUnit.Framework;
  5. using System.IO;
  6. namespace Ncqrs.Tests
  7. {
  8. [TestFixture]
  9. public class InternalExtensionsTests
  10. {
  11. [Test]
  12. public void IsEmpty_should_return_true_on_empty_array()
  13. {
  14. var target = new object[0];
  15. var isEmpty = InternalExtensions.IsEmpty(target);
  16. Assert.IsTrue(isEmpty);
  17. }
  18. [Test]
  19. public void IsEmpty_should_return_true_on_empty_list()
  20. {
  21. var target = new List<object>(0);
  22. var isEmpty = InternalExtensions.IsEmpty(target);
  23. Assert.IsTrue(isEmpty);
  24. }
  25. [Test]
  26. public void IsEmpty_should_return_false_on_non_empty_array()
  27. {
  28. var target = new object[1];
  29. var isEmpty = InternalExtensions.IsEmpty(target);
  30. Assert.IsFalse(isEmpty);
  31. }
  32. [Test]
  33. public void IsEmpty_should_return_false_on_non_empty_list()
  34. {
  35. var target = new List<object>();
  36. target.Add(new object());
  37. var isEmpty = InternalExtensions.IsEmpty(target);
  38. Assert.IsFalse(isEmpty);
  39. }
  40. [Test]
  41. public void IsEmpty_should_throw_ArgumentNullException_on_null_reference()
  42. {
  43. List<object> target = null;
  44. Action act = ()=>InternalExtensions.IsEmpty(target);
  45. act.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("source");
  46. }
  47. [Test]
  48. public void IsNullOrEmpty_should_return_true_on_null()
  49. {
  50. var result = InternalExtensions.IsNullOrEmpty(null);
  51. result.Should().BeTrue();
  52. }
  53. [Test]
  54. public void IsNullOrEmpty_should_return_true_on_empty()
  55. {
  56. var result = InternalExtensions.IsNullOrEmpty(string.Empty);
  57. result.Should().BeTrue();
  58. }
  59. [Test]
  60. public void IsNullOrEmpty_should_return_false_on_string_with_text()
  61. {
  62. var result = InternalExtensions.IsNullOrEmpty("Hello world");
  63. result.Should().BeFalse();
  64. }
  65. [Test]
  66. public void Clone_should_return_other_instance()
  67. {
  68. var source = new List<object>();
  69. var clone = InternalExtensions.Clone(source);
  70. Assert.AreNotSame(source, clone);
  71. }
  72. [Test]
  73. public void Clone_should_return_list_with_same_values()
  74. {
  75. var source = new List<int>{1,2,3,4,5};
  76. var clone = InternalExtensions.Clone(source);
  77. clone.Should().ContainInOrder(source);
  78. }
  79. [Test]
  80. public void Clone_should_throw_ArgumentNullException_when_source_is_null()
  81. {
  82. List<string> nullTarget = null;
  83. Action act = ()=> InternalExtensions.Clone(nullTarget);
  84. act.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("source");
  85. }
  86. [Test]
  87. public void Implements_should_throw_when_source_is_null()
  88. {
  89. var interfaceType = typeof (IDisposable);
  90. Action act = () => InternalExtensions.Implements(null, interfaceType);
  91. act.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("source");
  92. }
  93. [Test]
  94. public void Implements_should_throw_when_interfaceType_is_null()
  95. {
  96. var source = typeof(Stream);
  97. Action act = () => InternalExtensions.Implements(source, null);
  98. act.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("interfaceType");
  99. }
  100. [Test]
  101. public void Implements_should_throw_when_interfaceType_is_not_an_interface_type()
  102. {
  103. var source = typeof(Stream);
  104. var wrongInterfaceType = typeof (String);
  105. Action act = () => InternalExtensions.Implements(source, wrongInterfaceType);
  106. act.ShouldThrow<ArgumentException>().And.ParamName.Should().Be("interfaceType");
  107. }
  108. [Test]
  109. public void Implements_should_return_false_when_source_does_not_implement_the_interface()
  110. {
  111. var source = typeof (Stream);
  112. var interfaceType = typeof (IDisposable);
  113. InternalExtensions.Implements(source, interfaceType).Should().BeTrue();
  114. }
  115. [Test]
  116. public void Implements_should_return_true_when_source_does_implement_the_interface()
  117. {
  118. var source = typeof(String);
  119. var interfaceType = typeof(IDisposable);
  120. InternalExtensions.Implements(source, interfaceType).Should().BeFalse();
  121. }
  122. [Test]
  123. public void Implements_generic_should_throw_when_interfaceType_is_not_an_interface_type()
  124. {
  125. var source = typeof(Stream);
  126. Action act = () => InternalExtensions.Implements<String>(source);
  127. act.ShouldThrow<ArgumentException>().And.ParamName.Should().Be("interfaceType");
  128. }
  129. [Test]
  130. public void Implements_generic_should_return_false_when_source_does_not_implement_the_interface()
  131. {
  132. var source = typeof(Stream);
  133. InternalExtensions.Implements<IDisposable>(source).Should().BeTrue();
  134. }
  135. [Test]
  136. public void Implements_generic_should_return_true_when_source_does_implement_the_interface()
  137. {
  138. var source = typeof(String);
  139. InternalExtensions.Implements<IDisposable>(source).Should().BeFalse();
  140. }
  141. }
  142. }