Refactored the Kernel registration fluent interface to be more readable, better suppo...
[castle.git] / MonoRail / Castle.MonoRail.Framework.Tests / ViewEngines / ViewEngineBaseTestCase.cs
blobf9369d7913e670a8f051ebae311a831f1dcdf1ff
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
16 #pragma warning disable 67
18 namespace Castle.MonoRail.Framework.Tests
20 using System;
21 using System.Collections;
22 using System.Collections.Generic;
23 using System.IO;
24 using NUnit.Framework;
26 [TestFixture]
27 public class ViewEngineBaseTestCase
29 [Test]
30 public void IsTemplateForJSGeneration_ShouldReturnFalseForTemplateWithAnotherExtension()
32 const string templateName = "view.test";
33 TestableViewEngineBase engine = new TestableViewEngineBase();
34 engine.Service(new TestServiceProvider(new TestViewSourceLoader(templateName)));
35 Assert.IsFalse(engine.IsTemplateForJSGeneration(templateName),
36 "This is a template typed by extension as not for JSGen and should not have been accepted");
39 [Test]
40 public void IsTemplateForJSGeneration_ShouldReturnFalseForNonExistentTemplateWithNoExtension()
42 TestableViewEngineBase engine = new TestableViewEngineBase();
43 engine.Service(new TestServiceProvider(new TestViewSourceLoader()));
44 Assert.IsFalse(engine.IsTemplateForJSGeneration("view.testjs"),
45 "This template does not 'exist' so it should have failed");
48 [Test]
49 public void IsTemplateForJSGeneration_ShouldReturnTrueForExistingTemplateWithCorrectExtension()
51 const string templateName = "fakeview.testjs";
52 TestableViewEngineBase engine = new TestableViewEngineBase();
53 engine.Service(new TestServiceProvider(new TestViewSourceLoader(templateName)));
54 Assert.IsTrue(engine.IsTemplateForJSGeneration(templateName),
55 "Should have been accepted and found with correct extension");
58 [Test]
59 public void IsTemplateForJSGeneration_ShouldReturnTrueForExistingTemplateWithoutExtension()
61 TestableViewEngineBase engine = new TestableViewEngineBase();
62 engine.Service(new TestServiceProvider(new TestViewSourceLoader("fakeview.testjs")));
63 Assert.IsTrue(engine.IsTemplateForJSGeneration("fakeview"),
64 "Should have been accepted and found without extension");
68 public class TestViewSourceLoader : IViewSourceLoader
70 private readonly string[] views;
72 public TestViewSourceLoader(params string[] views)
74 this.views = views;
77 #region IViewSourceLoader Members
79 /// <summary>
80 /// Evaluates whether the specified template exists.
81 /// </summary>
82 /// <param name="templateName">The template name</param>
83 /// <returns><c>true</c> if it exists</returns>
84 public bool HasSource(string templateName)
86 foreach(string view in views)
88 if (view.Equals(templateName, StringComparison.InvariantCultureIgnoreCase))
90 return true;
93 return false;
96 /// <summary>
97 /// Builds and returns a representation of a view template
98 /// </summary>
99 /// <param name="templateName">The template name</param>
100 /// <returns></returns>
101 public IViewSource GetViewSource(string templateName)
103 throw new NotImplementedException();
106 /// <summary>
107 /// Gets a list of views on the specified directory
108 /// </summary>
109 /// <param name="dirName">Directory name</param>
110 /// <param name="fileExtensionsToInclude">Optional fileExtensions to include in listing.</param>
111 /// <returns></returns>
112 public string[] ListViews(string dirName, params string[] fileExtensionsToInclude)
114 throw new NotImplementedException();
117 /// <summary>
118 /// Gets/sets the root directory of views, obtained from the configuration.
119 /// </summary>
120 /// <value></value>
121 public string VirtualViewDir
123 get { throw new NotImplementedException(); }
124 set { throw new NotImplementedException(); }
127 /// <summary>
128 /// Gets/sets the root directory of views, obtained from the configuration.
129 /// </summary>
130 public string ViewRootDir
132 get { throw new NotImplementedException(); }
133 set { throw new NotImplementedException(); }
136 /// <summary>
137 /// Gets or sets whether the instance should use cache
138 /// </summary>
139 public bool EnableCache
141 get { throw new NotImplementedException(); }
142 set { throw new NotImplementedException(); }
145 /// <summary>
146 /// Gets a list of assembly sources
147 /// </summary>
148 public IList AssemblySources
150 get { throw new NotImplementedException(); }
153 /// <summary>
154 /// Adds the assembly source.
155 /// </summary>
156 /// <param name="assemblySourceInfo">The assembly source info.</param>
157 public void AddAssemblySource(AssemblySourceInfo assemblySourceInfo)
159 throw new NotImplementedException();
162 /// <summary>
163 /// Raised when the view is changed.
164 /// </summary>
165 public event FileSystemEventHandler ViewChanged;
167 #endregion
170 public class TestServiceProvider : IServiceProvider
172 private readonly IViewSourceLoader viewsourceloader;
174 public TestServiceProvider(IViewSourceLoader viewsourceloader)
176 this.viewsourceloader = viewsourceloader;
179 #region IServiceProvider Members
181 ///<summary>
182 ///Gets the service object of the specified type.
183 ///</summary>
185 ///<returns>
186 ///A service object of type serviceType.-or- null if there is no service object of type serviceType.
187 ///</returns>
189 ///<param name="serviceType">An object that specifies the type of service object to get. </param><filterpriority>2</filterpriority>
190 public object GetService(Type serviceType)
192 if (serviceType == typeof(IViewSourceLoader))
193 return viewsourceloader;
194 return null;
197 #endregion
200 public class TestableViewEngineBase : ViewEngineBase
202 private bool _supportsJSGeneration;
203 private string _viewFileExtension;
204 private string _jsGeneratorFileExtension;
206 public TestableViewEngineBase()
208 _supportsJSGeneration = false;
209 _viewFileExtension = ".test";
210 _jsGeneratorFileExtension = ".testjs";
213 public override bool SupportsJSGeneration
215 get { return _supportsJSGeneration; }
218 public override string ViewFileExtension
220 get { return _viewFileExtension; }
223 public override string JSGeneratorFileExtension
225 get { return _jsGeneratorFileExtension; }
228 /// <summary>
229 /// Processes the view - using the templateName
230 /// to obtain the correct template,
231 /// and using the context to output the result.
232 /// </summary>
233 public override void Process(String templateName, TextWriter output, IEngineContext context, IController controller,
234 IControllerContext controllerContext)
236 throw new NotImplementedException();
239 public override void Process(string templateName, string layoutName, TextWriter output,
240 IDictionary<string, object> parameters)
242 throw new NotImplementedException();
245 /// <summary>
246 /// Should process the specified partial. The partial name must contains
247 /// the path relative to the views folder.
248 /// </summary>
249 public override void ProcessPartial(String partialName, TextWriter output, IEngineContext context,
250 IController controller, IControllerContext controllerContext)
252 throw new NotImplementedException();
255 /// <summary>
256 /// Implementors should return a generator instance if
257 /// the view engine supports JS generation.
258 /// </summary>
259 /// <returns>A JS generator instance</returns>
260 public override object CreateJSGenerator(JSCodeGeneratorInfo generatorInfo,
261 IEngineContext context, IController controller,
262 IControllerContext controllerContext)
264 throw new NotImplementedException();
267 public override void GenerateJS(string templateName, TextWriter output, JSCodeGeneratorInfo generatorInfo,
268 IEngineContext context, IController controller, IControllerContext controllerContext)
270 throw new NotImplementedException();
273 /// <summary>
274 /// Wraps the specified content in the layout using the
275 /// context to output the result.
276 /// </summary>
277 public override void RenderStaticWithinLayout(String contents, IEngineContext context, IController controller,
278 IControllerContext controllerContext)
280 throw new NotImplementedException();