/src/Manos.Tests/Manos.Template/TemplateFactoryTest.cs

http://github.com/jacksonh/manos · C# · 171 lines · 106 code · 39 blank · 26 comment · 0 complexity · 33a192af73e839276991239caa5a9084 MD5 · raw file

  1. //
  2. // Copyright (C) 2010 Jackson Harper (jackson@manosdemono.com)
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. //
  24. using System;
  25. using NUnit.Framework;
  26. using Manos.Templates.Testing;
  27. namespace Manos.Templates.Tests
  28. {
  29. [TestFixture]
  30. public class TemplateFactoryTest
  31. {
  32. [SetUp]
  33. public void Setup ()
  34. {
  35. //
  36. // Otherwise template names will be registered across tests
  37. //
  38. TemplateFactory.Clear ();
  39. }
  40. [Test]
  41. public void RegisterTemplate_NullName_Throws ()
  42. {
  43. IManosTemplate template = new ManosTemplateStub ();
  44. Assert.Throws<ArgumentNullException> (() => TemplateFactory.Register (null, template));
  45. }
  46. [Test]
  47. public void RegisterTemplate_NullTemplate_Throws ()
  48. {
  49. string name = "foobar";
  50. Assert.Throws<ArgumentNullException> (() => TemplateFactory.Register (name, null));
  51. }
  52. [Test]
  53. public void RegisterTemplate_AlreadyRegistered_Throws ()
  54. {
  55. var name = "foobar";
  56. var template = new ManosTemplateStub ();
  57. TemplateFactory.Register (name, template);
  58. Assert.Throws<InvalidOperationException> (() => TemplateFactory.Register (name, template));
  59. }
  60. [Test]
  61. public void RegisterTemplate_RegisterAndRetrieve_ItemIsRegistered ()
  62. {
  63. var name = "foobar";
  64. var expected = new ManosTemplateStub ();
  65. TemplateFactory.Register (name, expected);
  66. var retrieved = TemplateFactory.Get (name);
  67. Assert.AreEqual (expected, retrieved);
  68. }
  69. [Test]
  70. public void TryGet_NullName_Throws ()
  71. {
  72. IManosTemplate template;
  73. Assert.Throws<ArgumentNullException> (() => TemplateFactory.TryGet (null, out template));
  74. }
  75. [Test]
  76. public void Get_NullName_Throws ()
  77. {
  78. Assert.Throws<ArgumentNullException> (() => TemplateFactory.Get (null));
  79. }
  80. [Test]
  81. public void Get_NonExistent_ReturnsNull ()
  82. {
  83. var name = "foo";
  84. var template = TemplateFactory.Get (name);
  85. Assert.IsNull (template);
  86. }
  87. [Test]
  88. public void Clear_RegisteredItems_UnregistersItems ()
  89. {
  90. var name = "blah";
  91. IManosTemplate template = new ManosTemplateStub ();
  92. TemplateFactory.Register (name, template);
  93. TemplateFactory.Clear ();
  94. template = TemplateFactory.Get (name);
  95. Assert.IsNull (template);
  96. }
  97. [Test]
  98. public void TryGet_NonExistant_ReturnsFalse ()
  99. {
  100. var name = "wolfbear";
  101. IManosTemplate template = null;
  102. Assert.IsFalse (TemplateFactory.TryGet (name, out template));
  103. }
  104. [Test]
  105. public void TryGet_NonExistant_SetsTemplateNull ()
  106. {
  107. var name = "wolfbear";
  108. IManosTemplate template = new ManosTemplateStub ();
  109. TemplateFactory.TryGet (name, out template);
  110. Assert.IsNull (template);
  111. }
  112. [Test]
  113. public void TryGet_RegisteredTemplate_ReturnsTrue ()
  114. {
  115. var name = "barkingpossum";
  116. IManosTemplate template = new ManosTemplateStub ();
  117. TemplateFactory.Register (name, template);
  118. bool found = TemplateFactory.TryGet (name, out template);
  119. Assert.IsTrue (found);
  120. }
  121. [Test]
  122. public void TryGet_RegisteredTemplate_SetsTemplate ()
  123. {
  124. var name = "manbearpig";
  125. IManosTemplate expected = new ManosTemplateStub ();
  126. TemplateFactory.Register (name, expected);
  127. IManosTemplate actual = null;
  128. TemplateFactory.TryGet (name, out actual);
  129. Assert.AreSame (expected, actual);
  130. }
  131. }
  132. }