Simple impl to MockRailsEngineContext.Url
[castle.git] / MonoRail / Castle.MonoRail.Framework / Test / MockViewComponentContext.cs
blob32a475688e78bcbfea17ccb7f58d2a873c06a60e
1 // Copyright 2004-2007 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.
15 namespace Castle.MonoRail.Framework.Test
17 using System;
18 using System.Collections;
19 using System.Collections.Specialized;
20 using System.Collections.Generic;
21 using System.IO;
23 /// <summary>
24 /// Used to hook a viewcomponent call to render a nested section
25 /// </summary>
26 /// <param name="context">The content available to the section</param>
27 /// <param name="writer">The writer</param>
28 public delegate void TestSectionRender(IDictionary context, TextWriter writer);
30 /// <summary>
31 /// Used to hook a viewcomponent call to render a view template
32 /// </summary>
33 /// <param name="name">view name</param>
34 /// <param name="context">The content available to the view</param>
35 /// <param name="writer">The writer</param>
36 public delegate void TestViewRender(string name, IDictionary context, TextWriter writer);
38 /// <summary>
39 /// Represents a mock implementation of <see cref="IMockViewComponentContext"/> for unit test purposes.
40 /// </summary>
41 public class MockViewComponentContext : IMockViewComponentContext
43 private string viewToRender;
44 private string componentName;
45 private TextWriter writer;
46 private IDictionary contextVars = new HybridDictionary(true);
47 private IDictionary componentParameters = new HybridDictionary(true);
48 private IViewEngine viewEngine;
49 private IDictionary<string, TestSectionRender> section2delegate;
51 /// <summary>
52 /// Event that is raised when a section is rendered by the viewcomponent.
53 /// </summary>
54 public TestSectionRender OnBodyRender;
56 /// <summary>
57 /// Event that is raised when a view is rendered by the viewcomponent.
58 /// </summary>
59 public TestViewRender OnViewRender;
61 /// <summary>
62 /// Initializes a new instance of the <see cref="MockViewComponentContext"/> class.
63 /// </summary>
64 protected MockViewComponentContext()
66 section2delegate = new Dictionary<string, TestSectionRender>(StringComparer.InvariantCultureIgnoreCase);
69 /// <summary>
70 /// Initializes a new instance of the <see cref="MockViewComponentContext"/> class.
71 /// </summary>
72 /// <param name="componentName">Name of the component.</param>
73 /// <param name="writer">The writer.</param>
74 /// <param name="viewEngine">The view engine.</param>
75 public MockViewComponentContext(string componentName, TextWriter writer, IViewEngine viewEngine) : this()
77 this.writer = writer;
78 this.componentName = componentName;
79 this.viewEngine = viewEngine;
82 /// <summary>
83 /// Gets or sets the section render dictionary.
84 /// </summary>
85 /// <value>The section render.</value>
86 public IDictionary<string, TestSectionRender> SectionRender
88 get { return section2delegate; }
89 set { section2delegate = value; }
92 #region IViewComponentContext
94 /// <summary>
95 /// Gets the name of the component.
96 /// </summary>
97 /// <value>The name of the component.</value>
98 public virtual string ComponentName
100 get { return componentName; }
103 /// <summary>
104 /// Determines whether the current component declaration on the view
105 /// has the specified section.
106 /// </summary>
107 /// <param name="sectionName">Name of the section.</param>
108 /// <returns>
109 /// <c>true</c> if the specified section exists; otherwise, <c>false</c>.
110 /// </returns>
111 public bool HasSection(string sectionName)
113 return section2delegate.ContainsKey(sectionName);
116 /// <summary>
117 /// Renders the view specified to the writer.
118 /// </summary>
119 /// <param name="name">The view template name</param>
120 /// <param name="writer">A writer to output</param>
121 public void RenderView(string name, TextWriter writer)
123 if (OnViewRender != null)
125 OnViewRender(name, contextVars, writer);
129 /// <summary>
130 /// Renders the component body.
131 /// </summary>
132 public void RenderBody()
134 RenderBody(writer);
137 /// <summary>
138 /// Renders the body into the specified <see cref="TextWriter"/>
139 /// </summary>
140 /// <param name="writer">The writer.</param>
141 public void RenderBody(TextWriter writer)
143 if (OnBodyRender != null)
145 OnBodyRender(contextVars, writer);
149 /// <summary>
150 /// Renders the the specified section.
151 /// No exception will the throw if the section cannot be found.
152 /// </summary>
153 /// <param name="sectionName">Name of the section.</param>
154 public void RenderSection(string sectionName)
156 RenderSection(sectionName, writer);
159 /// <summary>
160 /// Renders the the specified section.
161 /// No exception will the throw if the section cannot be found.
162 /// </summary>
163 /// <param name="sectionName">Name of the section.</param>
164 /// <param name="writer">The writer to output the section content.</param>
165 public void RenderSection(string sectionName, TextWriter writer)
167 if (section2delegate.ContainsKey(sectionName))
169 section2delegate[sectionName](contextVars, writer);
173 /// <summary>
174 /// Gets the writer used to render the view component
175 /// </summary>
176 /// <value>The writer.</value>
177 public virtual TextWriter Writer
179 get { return writer; }
182 /// <summary>
183 /// Gets the dictionary that holds variables for the
184 /// view and for the view component
185 /// </summary>
186 /// <value>The context vars.</value>
187 public virtual IDictionary ContextVars
189 get { return contextVars; }
192 /// <summary>
193 /// Gets the component parameters that the view has passed
194 /// to the component
195 /// </summary>
196 /// <value>The component parameters.</value>
197 public virtual IDictionary ComponentParameters
199 get { return componentParameters; }
202 /// <summary>
203 /// Gets or sets the view to render.
204 /// </summary>
205 /// <value>The view to render.</value>
206 public string ViewToRender
208 get { return viewToRender; }
209 set { viewToRender = value; }
212 /// <summary>
213 /// Gets the view engine instance.
214 /// </summary>
215 /// <value>The view engine.</value>
216 public virtual IViewEngine ViewEngine
218 get { return viewEngine; }
221 #endregion