PageRenderTime 51ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/BurnSystems/tests/BurnSystems.UnitTests/Collections/TypedContainerTests.cs

https://bitbucket.org/mbrenn/entityconnector
C# | 104 lines | 81 code | 20 blank | 3 comment | 0 complexity | f7cf7a0215e419b20683de53023ebbfe MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using NUnit.Framework;
  6. using BurnSystems.Collections;
  7. namespace BurnSystems.UnitTests.Collections
  8. {
  9. /// <summary>
  10. /// Implements some tests for the typed container
  11. /// </summary>
  12. [TestFixture]
  13. public class TypedContainerTests
  14. {
  15. [Test]
  16. public void AddAndRetrieveAvailableObject()
  17. {
  18. var a = new A();
  19. var b = new B();
  20. var typedContainer = new TypedContainer();
  21. typedContainer.Add(a);
  22. typedContainer.Add(b);
  23. var aFound = typedContainer.Get<A>();
  24. Assert.That(aFound, Is.SameAs(a));
  25. var bFound = typedContainer.Get<B>();
  26. Assert.That(bFound, Is.SameAs(b));
  27. var cFound = typedContainer.Get<C_B>();
  28. Assert.That(cFound, Is.Null);
  29. }
  30. [Test]
  31. public void AddAndRetrieveNonAvailableObject()
  32. {
  33. var a = new A();
  34. var b = new B();
  35. var typedContainer = new TypedContainer();
  36. typedContainer.Add(a);
  37. typedContainer.Add(b);
  38. var cFound = typedContainer.Get<C_B>();
  39. Assert.That(cFound, Is.Null);
  40. }
  41. [Test]
  42. public void AddAndRetrieveWithInheritence()
  43. {
  44. var a = new A();
  45. var b = new B();
  46. var c = new C_B();
  47. var typedContainer = new TypedContainer();
  48. typedContainer.Add(a);
  49. typedContainer.Add(b);
  50. typedContainer.Add(c);
  51. var cFound = typedContainer.Get<C_B>();
  52. Assert.That(cFound, Is.SameAs(c));
  53. }
  54. [Test]
  55. public void AddAndRetrieveInheritedClass()
  56. {
  57. var c = new C_B();
  58. var typedContainer = new TypedContainer();
  59. typedContainer.Add(c);
  60. var bFound = typedContainer.Get<B>();
  61. Assert.That(bFound, Is.SameAs(c));
  62. }
  63. [Test]
  64. public void AddAndRetrieveInheritedClassWithException()
  65. {
  66. var b = new B();
  67. var c = new C_B();
  68. var typedContainer = new TypedContainer();
  69. typedContainer.Add(b);
  70. typedContainer.Add(c);
  71. Assert.Throws<InvalidOperationException>(
  72. () => typedContainer.Get<B>());
  73. }
  74. public class A
  75. {
  76. }
  77. public class B
  78. {
  79. }
  80. public class C_B : B
  81. {
  82. }
  83. }
  84. }