1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
21 using System
.Collections
;
22 using System
.Collections
.Generic
;
24 using NUnit
.Framework
;
27 public class ViewEngineBaseTestCase
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");
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");
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");
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
)
77 #region IViewSourceLoader Members
80 /// Evaluates whether the specified template exists.
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
))
97 /// Builds and returns a representation of a view template
99 /// <param name="templateName">The template name</param>
100 /// <returns></returns>
101 public IViewSource
GetViewSource(string templateName
)
103 throw new NotImplementedException();
107 /// Gets a list of views on the specified directory
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();
118 /// Gets/sets the root directory of views, obtained from the configuration.
121 public string VirtualViewDir
123 get { throw new NotImplementedException(); }
124 set { throw new NotImplementedException(); }
128 /// Gets/sets the root directory of views, obtained from the configuration.
130 public string ViewRootDir
132 get { throw new NotImplementedException(); }
133 set { throw new NotImplementedException(); }
137 /// Gets or sets whether the instance should use cache
139 public bool EnableCache
141 get { throw new NotImplementedException(); }
142 set { throw new NotImplementedException(); }
146 /// Gets a list of assembly sources
148 public IList AssemblySources
150 get { throw new NotImplementedException(); }
154 /// Adds the assembly source.
156 /// <param name="assemblySourceInfo">The assembly source info.</param>
157 public void AddAssemblySource(AssemblySourceInfo assemblySourceInfo
)
159 throw new NotImplementedException();
163 /// Raised when the view is changed.
165 public event FileSystemEventHandler ViewChanged
;
170 public class TestServiceProvider
: IServiceProvider
172 private readonly IViewSourceLoader viewsourceloader
;
174 public TestServiceProvider(IViewSourceLoader viewsourceloader
)
176 this.viewsourceloader
= viewsourceloader
;
179 #region IServiceProvider Members
182 ///Gets the service object of the specified type.
186 ///A service object of type serviceType.-or- null if there is no service object of type serviceType.
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
;
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; }
229 /// Processes the view - using the templateName
230 /// to obtain the correct template,
231 /// and using the context to output the result.
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();
246 /// Should process the specified partial. The partial name must contains
247 /// the path relative to the views folder.
249 public override void ProcessPartial(String partialName
, TextWriter output
, IEngineContext context
,
250 IController controller
, IControllerContext controllerContext
)
252 throw new NotImplementedException();
256 /// Implementors should return a generator instance if
257 /// the view engine supports JS generation.
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();
274 /// Wraps the specified content in the layout using the
275 /// context to output the result.
277 public override void RenderStaticWithinLayout(String contents
, IEngineContext context
, IController controller
,
278 IControllerContext controllerContext
)
280 throw new NotImplementedException();