1 // Copyright 2004-2007 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.
15 #pragma warning disable 67
17 namespace Castle
.MonoRail
.Framework
.Tests
20 using System
.Collections
;
22 using NUnit
.Framework
;
25 public class ViewEngineBaseTestCase
28 public void IsTemplateForJSGeneration_ShouldReturnFalseForTemplateWithAnotherExtension()
30 const string templateName
= "view.test";
31 TestableViewEngineBase engine
= new TestableViewEngineBase();
32 engine
.Service(new TestServiceProvider(new TestViewSourceLoader(templateName
)));
33 Assert
.IsFalse(engine
.IsTemplateForJSGeneration(templateName
),
34 "This is a template typed by extension as not for JSGen and should not have been accepted");
38 public void IsTemplateForJSGeneration_ShouldReturnFalseForNonExistentTemplateWithNoExtension()
40 TestableViewEngineBase engine
= new TestableViewEngineBase();
41 engine
.Service(new TestServiceProvider(new TestViewSourceLoader()));
42 Assert
.IsFalse(engine
.IsTemplateForJSGeneration("view.testjs"),
43 "This template does not 'exist' so it should have failed");
47 public void IsTemplateForJSGeneration_ShouldReturnTrueForExistingTemplateWithCorrectExtension()
49 const string templateName
= "fakeview.testjs";
50 TestableViewEngineBase engine
= new TestableViewEngineBase();
51 engine
.Service(new TestServiceProvider(new TestViewSourceLoader(templateName
)));
52 Assert
.IsTrue(engine
.IsTemplateForJSGeneration(templateName
),
53 "Should have been accepted and found with correct extension");
57 public void IsTemplateForJSGeneration_ShouldReturnTrueForExistingTemplateWithoutExtension()
59 TestableViewEngineBase engine
= new TestableViewEngineBase();
60 engine
.Service(new TestServiceProvider(new TestViewSourceLoader("fakeview.testjs")));
61 Assert
.IsTrue(engine
.IsTemplateForJSGeneration("fakeview"),
62 "Should have been accepted and found without extension");
66 public class TestViewSourceLoader
: IViewSourceLoader
68 private readonly string[] views
;
70 public TestViewSourceLoader(params string[] views
)
75 #region IViewSourceLoader Members
78 /// Evaluates whether the specified template exists.
80 /// <param name="templateName">The template name</param>
81 /// <returns><c>true</c> if it exists</returns>
82 public bool HasTemplate(string templateName
)
84 foreach (string view
in views
)
85 if (view
.Equals(templateName
, StringComparison
.InvariantCultureIgnoreCase
))
91 /// Builds and returns a representation of a view template
93 /// <param name="templateName">The template name</param>
94 /// <returns></returns>
95 public IViewSource
GetViewSource(string templateName
)
97 throw new NotImplementedException();
101 /// Gets a list of views on the specified directory
103 /// <param name="dirName">Directory name</param>
104 /// <returns></returns>
105 public string[] ListViews(string dirName
)
107 throw new NotImplementedException();
111 /// Gets/sets the root directory of views, obtained from the configuration.
113 public string ViewRootDir
115 get { throw new NotImplementedException(); }
116 set { throw new NotImplementedException(); }
120 /// Gets or sets whether the instance should use cache
122 public bool EnableCache
124 get { throw new NotImplementedException(); }
125 set { throw new NotImplementedException(); }
129 /// Gets a list of assembly sources
131 public IList AssemblySources
133 get { throw new NotImplementedException(); }
137 /// Adds the assembly source.
139 /// <param name="assemblySourceInfo">The assembly source info.</param>
140 public void AddAssemblySource(AssemblySourceInfo assemblySourceInfo
)
142 throw new NotImplementedException();
146 /// Raised when the view is changed.
148 public event FileSystemEventHandler ViewChanged
;
153 public class TestServiceProvider
: IServiceProvider
155 private readonly IViewSourceLoader viewsourceloader
;
157 public TestServiceProvider(IViewSourceLoader viewsourceloader
)
159 this.viewsourceloader
= viewsourceloader
;
162 #region IServiceProvider Members
165 ///Gets the service object of the specified type.
169 ///A service object of type serviceType.-or- null if there is no service object of type serviceType.
172 ///<param name="serviceType">An object that specifies the type of service object to get. </param><filterpriority>2</filterpriority>
173 public object GetService(Type serviceType
)
175 if (serviceType
== typeof(IViewSourceLoader
))
176 return viewsourceloader
;
183 public class TestableViewEngineBase
: ViewEngineBase
185 private bool _supportsJSGeneration
;
186 private string _viewFileExtension
;
187 private string _jsGeneratorFileExtension
;
189 public TestableViewEngineBase()
191 _supportsJSGeneration
= false;
192 _viewFileExtension
= ".test";
193 _jsGeneratorFileExtension
= ".testjs";
196 public override bool SupportsJSGeneration
198 get { return _supportsJSGeneration; }
201 public override string ViewFileExtension
203 get { return _viewFileExtension; }
206 public override string JSGeneratorFileExtension
208 get { return _jsGeneratorFileExtension; }
212 /// Processes the view - using the templateName
213 /// to obtain the correct template,
214 /// and using the context to output the result.
216 public override void Process(IRailsEngineContext context
, IController controller
, string templateName
)
218 throw new NotImplementedException();
222 /// Processes the view - using the templateName
223 /// to obtain the correct template
224 /// and writes the results to the System.IO.TextWriter.
226 public override void Process(TextWriter output
, IRailsEngineContext context
, IController controller
,
229 throw new NotImplementedException();
233 /// Should process the specified partial. The partial name must contains
234 /// the path relative to the views folder.
236 /// <param name="output">The output.</param>
237 /// <param name="context">The request context.</param>
238 /// <param name="controller">The controller.</param>
239 /// <param name="partialName">The partial name.</param>
240 public override void ProcessPartial(TextWriter output
, IRailsEngineContext context
, IController controller
,
243 throw new NotImplementedException();
247 /// Implementors should return a generator instance if
248 /// the view engine supports JS generation.
250 /// <param name="context">The request context.</param>
251 /// <returns>A JS generator instance</returns>
252 public override object CreateJSGenerator(IRailsEngineContext context
)
254 throw new NotImplementedException();
258 /// Processes the js generation view template - using the templateName
259 /// to obtain the correct template, and using the specified <see cref="TextWriter"/>
260 /// to output the result.
262 /// <param name="output">The output.</param>
263 /// <param name="context">The request context.</param>
264 /// <param name="controller">The controller.</param>
265 /// <param name="templateName">Name of the template.</param>
266 public override void GenerateJS(TextWriter output
, IRailsEngineContext context
, IController controller
,
269 throw new NotImplementedException();
273 /// Wraps the specified content in the layout using the
274 /// context to output the result.
276 public override void ProcessContents(IRailsEngineContext context
, IController controller
, string contents
)
278 throw new NotImplementedException();