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.
15 namespace Castle
.MonoRail
.Views
.Brail
.Tests
18 using System
.Collections
;
20 using System
.Reflection
;
21 using Castle
.MonoRail
.Framework
.Descriptors
;
22 using Castle
.MonoRail
.Framework
.Helpers
;
23 using Castle
.MonoRail
.Framework
.JSGeneration
;
24 using Castle
.MonoRail
.Framework
.JSGeneration
.Prototype
;
25 using Castle
.MonoRail
.Framework
.Resources
;
26 using Castle
.MonoRail
.Framework
.Services
;
27 using Castle
.MonoRail
.Framework
.Test
;
28 using Castle
.MonoRail
.Views
.Brail
.TestSite
.Controllers
;
30 using NUnit
.Framework
;
32 public abstract class BaseViewOnlyTestFixture
34 private readonly string viewSourcePath
;
35 protected ControllerContext ControllerContext
;
36 protected HelperDictionary Helpers
;
37 private string lastOutput
;
38 protected string Layout
;
39 protected MockEngineContext MockEngineContext
;
40 protected Hashtable PropertyBag
;
41 protected string Area
= null;
42 protected string ControllerName
= "test_controller";
43 protected string Action
= "test_action";
44 protected DefaultViewComponentFactory ViewComponentFactory
;
45 protected BooViewEngine BooViewEngine
;
47 public BaseViewOnlyTestFixture()
48 : this("../../../TestSiteBrail")
52 public BaseViewOnlyTestFixture(string viewSource
)
54 viewSourcePath
= viewSource
;
58 public string ViewSourcePath
60 get { return viewSourcePath; }
67 PropertyBag
= new Hashtable(StringComparer
.InvariantCultureIgnoreCase
);
68 Helpers
= new HelperDictionary();
69 MockServices services
= new MockServices();
70 services
.UrlBuilder
= new DefaultUrlBuilder(new MockServerUtility(), new MockRoutingEngine());
71 services
.UrlTokenizer
= new DefaultUrlTokenizer();
72 UrlInfo urlInfo
= new UrlInfo(
73 "example.org", "test", "/TestBrail", "http", 80,
74 "http://test.example.org/test_area/test_controller/test_action.tdd",
75 Area
, ControllerName
, Action
, "tdd", "no.idea");
76 MockEngineContext
= new MockEngineContext(new MockRequest(), new MockResponse(), services
,
78 MockEngineContext
.AddService
<IUrlBuilder
>(services
.UrlBuilder
);
79 MockEngineContext
.AddService
<IUrlTokenizer
>(services
.UrlTokenizer
);
81 ViewComponentFactory
= new DefaultViewComponentFactory();
82 ViewComponentFactory
.Service(MockEngineContext
);
83 ViewComponentFactory
.Initialize();
85 MockEngineContext
.AddService
<IViewComponentFactory
>(ViewComponentFactory
);
86 ControllerContext
= new ControllerContext();
87 ControllerContext
.Helpers
= Helpers
;
88 ControllerContext
.PropertyBag
= PropertyBag
;
89 MockEngineContext
.CurrentControllerContext
= ControllerContext
;
92 Helpers
["urlhelper"] = Helpers
["url"] = new UrlHelper(MockEngineContext
);
93 Helpers
["htmlhelper"] = Helpers
["html"] = new HtmlHelper(MockEngineContext
);
94 Helpers
["dicthelper"] = Helpers
["dict"] = new DictHelper(MockEngineContext
);
95 Helpers
["DateFormatHelper"] = Helpers
["DateFormat"] = new DateFormatHelper(MockEngineContext
);
97 string viewPath
= Path
.Combine(viewSourcePath
, "Views");
99 FileAssemblyViewSourceLoader loader
= new FileAssemblyViewSourceLoader(viewPath
);
100 loader
.AddAssemblySource(
101 new AssemblySourceInfo(Assembly
.GetExecutingAssembly().FullName
,
102 "Castle.MonoRail.Views.Brail.Tests.ResourcedViews"));
104 BooViewEngine
= new BooViewEngine();
105 BooViewEngine
.Options
= new BooViewEngineOptions();
106 BooViewEngine
.Options
.SaveDirectory
= Environment
.CurrentDirectory
;
107 BooViewEngine
.Options
.SaveToDisk
= true;
108 BooViewEngine
.Options
.Debug
= true;
109 BooViewEngine
.Options
.BatchCompile
= false;
111 BooViewEngine
.SetViewSourceLoader(loader
);
112 BooViewEngine
.Initialize();
117 protected virtual void BeforEachTest()
123 public void ProcessView_StripRailsExtension(string url
)
125 ProcessView(url
.Replace(".rails", ""));
128 protected string ProcessView(string templatePath
)
130 StringWriter sw
= new StringWriter();
131 if (string.IsNullOrEmpty(Layout
) == false)
132 ControllerContext
.LayoutNames
= new string[] { Layout, }
;
133 MockEngineContext
.CurrentControllerContext
= ControllerContext
;
134 BooViewEngine
.Process(templatePath
, sw
, MockEngineContext
, null, ControllerContext
);
135 lastOutput
= sw
.ToString();
139 protected string ProcessViewJS(string templatePath
)
141 StringWriter sw
= new StringWriter();
142 if (string.IsNullOrEmpty(Layout
) == false)
143 ControllerContext
.LayoutNames
= new string[] { Layout, }
;
144 MockEngineContext
.CurrentControllerContext
= ControllerContext
;
145 JSCodeGenerator codeGenerator
=
146 new JSCodeGenerator(MockEngineContext
.Server
, null,
147 MockEngineContext
, null, ControllerContext
, MockEngineContext
.Services
.UrlBuilder
);
149 IJSGenerator jsGen
= new PrototypeGenerator(codeGenerator
);
151 codeGenerator
.JSGenerator
= jsGen
;
153 JSCodeGeneratorInfo info
= new JSCodeGeneratorInfo(codeGenerator
, jsGen
, new object[0], new object[0]);
155 BooViewEngine
.GenerateJS(templatePath
, sw
, info
,MockEngineContext
, null, ControllerContext
);
156 lastOutput
= sw
.ToString();
160 protected void AddResource(string name
, string resourceName
, Assembly asm
)
162 IResourceFactory resourceFactory
= new DefaultResourceFactory();
163 ResourceDescriptor descriptor
= new ResourceDescriptor(
169 IResource resource
= resourceFactory
.Create(
172 ControllerContext
.Resources
.Add(name
, resource
);
175 protected string RenderStaticWithLayout(string staticText
)
177 if (string.IsNullOrEmpty(Layout
) == false)
178 ControllerContext
.LayoutNames
= new string[] { Layout, }
;
179 MockEngineContext
.CurrentControllerContext
= ControllerContext
;
181 BooViewEngine
.RenderStaticWithinLayout(staticText
, MockEngineContext
, null, ControllerContext
);
182 lastOutput
= ((StringWriter
)MockEngineContext
.Response
.Output
)
183 .GetStringBuilder().ToString();
187 public void AssertReplyEqualTo(string expected
)
189 Assert
.AreEqual(expected
, lastOutput
);
192 public void AssertReplyContains(string contained
)
194 StringAssert
.Contains(contained
, lastOutput
);