PageRenderTime 34ms CodeModel.GetById 24ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 25
 26
 27using System;
 28using NUnit.Framework;
 29
 30using Manos.Templates.Testing;
 31
 32namespace Manos.Templates.Tests
 33{
 34	[TestFixture]
 35	public class TemplateFactoryTest
 36	{
 37		[SetUp]
 38		public void Setup ()
 39		{
 40			//
 41			// Otherwise template names will be registered across tests
 42			//
 43			TemplateFactory.Clear ();
 44		}
 45		
 46		[Test]
 47		public void RegisterTemplate_NullName_Throws ()
 48		{
 49			IManosTemplate template = new ManosTemplateStub ();
 50			
 51			Assert.Throws<ArgumentNullException> (() => TemplateFactory.Register (null, template));
 52		}
 53		
 54		[Test]
 55		public void RegisterTemplate_NullTemplate_Throws ()
 56		{
 57			string name = "foobar";
 58			
 59			Assert.Throws<ArgumentNullException> (() => TemplateFactory.Register (name, null));
 60		}
 61		
 62		[Test]
 63		public void RegisterTemplate_AlreadyRegistered_Throws ()
 64		{
 65			var name = "foobar";
 66			var template = new ManosTemplateStub ();
 67			
 68			TemplateFactory.Register (name, template);
 69			
 70			Assert.Throws<InvalidOperationException> (() => TemplateFactory.Register (name, template));
 71		}
 72		
 73		[Test]
 74		public void RegisterTemplate_RegisterAndRetrieve_ItemIsRegistered ()
 75		{
 76			var name = "foobar";
 77			var expected = new ManosTemplateStub ();
 78			
 79			TemplateFactory.Register (name, expected);
 80			
 81			var retrieved = TemplateFactory.Get (name);
 82			
 83			Assert.AreEqual (expected, retrieved); 
 84		}
 85		
 86		[Test]
 87		public void TryGet_NullName_Throws ()
 88		{
 89			IManosTemplate template;
 90			
 91			Assert.Throws<ArgumentNullException> (() => TemplateFactory.TryGet (null, out template));
 92		}
 93		
 94		[Test]
 95		public void Get_NullName_Throws ()
 96		{
 97			Assert.Throws<ArgumentNullException> (() => TemplateFactory.Get (null));
 98		}
 99		
100		[Test]
101		public void Get_NonExistent_ReturnsNull ()
102		{
103			var name = "foo";
104			var template = TemplateFactory.Get (name);
105			
106			Assert.IsNull (template);
107		}
108		
109		[Test]
110		public void Clear_RegisteredItems_UnregistersItems ()
111		{
112			var name = "blah";
113			IManosTemplate template = new ManosTemplateStub ();
114			
115			TemplateFactory.Register (name, template);
116			
117			TemplateFactory.Clear ();
118			
119			template = TemplateFactory.Get (name);
120			Assert.IsNull (template);
121		}
122		
123		[Test]
124		public void TryGet_NonExistant_ReturnsFalse ()
125		{
126			var name = "wolfbear";
127			IManosTemplate template = null;
128			
129			Assert.IsFalse (TemplateFactory.TryGet (name, out template));
130		}
131		
132		[Test]
133		public void TryGet_NonExistant_SetsTemplateNull ()
134		{
135			var name = "wolfbear";
136			IManosTemplate template = new ManosTemplateStub ();
137			
138			TemplateFactory.TryGet (name, out template);
139			
140			Assert.IsNull (template);
141		}
142		
143		[Test]
144		public void TryGet_RegisteredTemplate_ReturnsTrue ()
145		{
146			var name = "barkingpossum";
147			IManosTemplate template = new ManosTemplateStub ();
148			
149			TemplateFactory.Register (name, template);
150			
151			bool found = TemplateFactory.TryGet (name, out template);
152			
153			Assert.IsTrue (found);
154		}
155		
156		[Test]
157		public void TryGet_RegisteredTemplate_SetsTemplate ()
158		{
159			var name = "manbearpig";
160			IManosTemplate expected = new ManosTemplateStub ();
161			
162			TemplateFactory.Register (name, expected);
163			
164			IManosTemplate actual = null;
165			TemplateFactory.TryGet (name, out actual);
166			
167			Assert.AreSame (expected, actual);
168		}
169	}
170}
171