Fixing an issue with output parameters that are of type IntPtr
[castle.git] / MonoRail / Castle.MonoRail.TestSupport / BaseViewComponentTest.cs
blob8428d67c3f7d8af92151910b33123108748f0d02
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.
15 namespace Castle.MonoRail.TestSupport
17 using System;
18 using System.Collections.Generic;
19 using System.IO;
20 using Castle.MonoRail.Framework;
21 using Castle.MonoRail.Framework.Test;
23 /// <summary>
24 /// Base class to test view components.
25 /// </summary>
26 ///
27 /// <example>
28 /// The following test makes sure the component rendered the inner sections correctly.
29 /// <code lang="cs">
30 /// [TestFixture]
31 /// public class DiggStylePaginationTestCase : BaseViewComponentTest
32 /// {
33 /// private DiggStylePagination diggComponent;
34 /// private IPaginatedPage singlePage, secondPageOfThree;
35 ///
36 /// [SetUp]
37 /// public void Init()
38 /// {
39 /// diggComponent = new DiggStylePagination();
40 /// singlePage = new Page(new string[] { "a", "b", "c" }, 1, 4, 1);
41 /// secondPageOfThree = new Page(new string[] { "a", "b", "c", "d" }, 2, 4, 10);
42 /// }
43 ///
44 /// [TearDown]
45 /// public void Terminate()
46 /// {
47 /// CleanUp();
48 /// }
49 ///
50 /// [Test]
51 /// public void PageWithNoLinksInvokesStartAndEndSections()
52 /// {
53 /// List&lt;string&gt; actions = new List&lt;string&gt;();
54 /// // pass mock inner sections to component
55 /// SectionRender["startblock"] = delegate(IDictionary context, TextWriter writer) { actions.Add("started"); };
56 /// SectionRender["endblock"] = delegate(IDictionary context, TextWriter writer) { actions.Add("ended"); };
57 /// SectionRender["link"] = delegate(IDictionary context, TextWriter writer) { actions.Add("link"); };
58 ///
59 /// diggComponent.Page = singlePage;
60 ///
61 /// PrepareViewComponent(diggComponent);
62 /// diggComponent.Render();
63 ///
64 /// // make sure component "rendered" inner sections
65 /// Assert.AreEqual(2, actions.Count);
66 /// Assert.AreEqual("started", actions[0]);
67 /// Assert.AreEqual("ended", actions[1]);
68 /// }
69 /// }
70 /// </code>
71 /// </example>
72 ///
73 /// <remarks>
74 /// You must call <see cref="PrepareViewComponent"/> before testing a view component instance
75 /// and you should call <see cref="CleanUp"/> after each test case (use the TearDown).
76 /// </remarks>
77 public abstract class BaseViewComponentTest : BaseControllerTest
79 private IMockViewComponentContext componentContext;
80 private IViewEngine viewEngine;
81 private StringWriter writer;
83 /// <summary>
84 /// Use this dictionary to add inner sections as available inner sections to
85 /// the view component.
86 /// </summary>
87 public IDictionary<string, TestSectionRender> SectionRender;
89 /// <summary>
90 /// This delegate is called when the viewcomponent renders its body.
91 /// </summary>
92 public TestSectionRender OnBodyRender;
94 /// <summary>
95 /// This delegate is called when the viewcomponent renders a view
96 /// </summary>
97 public TestViewRender OnViewRender;
99 /// <summary>
100 /// Initializes a new instance of the <see cref="BaseViewComponentTest"/> class.
101 /// </summary>
102 protected BaseViewComponentTest()
104 CleanUp();
107 /// <summary>
108 /// Initialize the view component with mock services it needs to
109 /// be functional.
110 /// </summary>
111 /// <param name="component">The component instance.</param>
112 protected void PrepareViewComponent(ViewComponent component)
114 if (Context == null)
116 BuildEngineContext("", "Controller", "Action");
119 viewEngine = BuildViewEngine();
121 componentContext = BuildViewComponentContext(component.GetType().Name);
123 component.Init(Context, componentContext);
126 /// <summary>
127 /// Gets the output -- ie what the viewcomponent wrote to the output stream.
128 /// </summary>
129 /// <value>The output.</value>
130 protected string Output
132 get { return writer.ToString(); }
135 /// <summary>
136 /// Cleans the up all state created to test a view component.
137 /// </summary>
138 protected void CleanUp()
140 writer = new StringWriter();
141 SectionRender = new Dictionary<string, TestSectionRender>(StringComparer.InvariantCultureIgnoreCase);
142 OnBodyRender = null;
143 OnViewRender = null;
146 /// <summary>
147 /// Builds the view engine.
148 /// </summary>
149 /// <returns></returns>
150 protected virtual IViewEngine BuildViewEngine()
152 return new ViewEngineStub(".view", ".jsview", true);
155 /// <summary>
156 /// Builds the view component context.
157 /// </summary>
158 /// <param name="viewComponentName">Name of the view component.</param>
159 /// <returns></returns>
160 protected virtual IMockViewComponentContext BuildViewComponentContext(string viewComponentName)
162 MockViewComponentContext compContext = new MockViewComponentContext(viewComponentName, writer, viewEngine);
164 compContext.SectionRender = SectionRender;
165 compContext.OnBodyRender = OnBodyRender;
166 compContext.OnViewRender = OnViewRender;
168 return compContext;