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
.Helpers
;
22 using Castle
.MonoRail
.Framework
.Services
;
23 using Castle
.MonoRail
.Framework
.Test
;
25 using NUnit
.Framework
;
27 public class BaseViewOnlyTestFixture
29 private readonly string viewSourcePath
;
30 protected ControllerContext ControllerContext
;
31 protected Hashtable Helpers
;
32 private string lastOutput
;
33 protected string Layout
;
34 protected MockEngineContext MockEngineContext
;
35 protected Hashtable PropertyBag
;
36 protected string Area
= null;
37 protected string ControllerName
= "test_controller";
38 protected string Action
= "test_action";
39 protected DefaultViewComponentFactory ViewComponentFactory
;
40 protected BooViewEngine BooViewEngine
;
42 public BaseViewOnlyTestFixture()
43 : this("../../../TestSiteBrail")
47 public BaseViewOnlyTestFixture(string viewSource
)
49 viewSourcePath
= viewSource
;
53 public string ViewSourcePath
55 get { return viewSourcePath; }
62 PropertyBag
= new Hashtable(StringComparer
.InvariantCultureIgnoreCase
);
63 Helpers
= new Hashtable(StringComparer
.InvariantCultureIgnoreCase
);
64 MockServices services
= new MockServices();
65 services
.UrlBuilder
= new DefaultUrlBuilder(new MockServerUtility(), new MockRoutingEngine());
66 services
.UrlTokenizer
= new DefaultUrlTokenizer();
67 UrlInfo urlInfo
= new UrlInfo(
68 "example.org", "test", "/TestBrail", "http", 80,
69 "http://test.example.org/test_area/test_controller/test_action.tdd",
70 Area
, ControllerName
, Action
, "tdd", "no.idea");
71 MockEngineContext
= new MockEngineContext(new MockRequest(), new MockResponse(), services
,
73 MockEngineContext
.AddService
<IUrlBuilder
>(services
.UrlBuilder
);
74 MockEngineContext
.AddService
<IUrlTokenizer
>(services
.UrlTokenizer
);
76 ViewComponentFactory
= new DefaultViewComponentFactory();
77 ViewComponentFactory
.Service(MockEngineContext
);
78 ViewComponentFactory
.Initialize();
80 MockEngineContext
.AddService
<IViewComponentFactory
>(ViewComponentFactory
);
81 ControllerContext
= new ControllerContext();
82 ControllerContext
.Helpers
= Helpers
;
83 ControllerContext
.PropertyBag
= PropertyBag
;
84 MockEngineContext
.CurrentControllerContext
= ControllerContext
;
87 Helpers
["urlhelper"] = Helpers
["url"] = new UrlHelper(MockEngineContext
);
88 Helpers
["htmlhelper"] = Helpers
["html"] = new HtmlHelper(MockEngineContext
);
89 Helpers
["dicthelper"] = Helpers
["dict"] = new DictHelper(MockEngineContext
);
90 Helpers
["DateFormatHelper"] = Helpers
["DateFormat"] = new DateFormatHelper(MockEngineContext
);
92 string viewPath
= Path
.Combine(viewSourcePath
, "Views");
94 FileAssemblyViewSourceLoader loader
= new FileAssemblyViewSourceLoader(viewPath
);
95 loader
.AddAssemblySource(
96 new AssemblySourceInfo(Assembly
.GetExecutingAssembly().FullName
,
97 "Castle.MonoRail.Views.Brail.Tests.ResourcedViews"));
99 BooViewEngine
= new BooViewEngine();
100 BooViewEngine
.Options
= new BooViewEngineOptions();
101 BooViewEngine
.Options
.SaveDirectory
= Environment
.CurrentDirectory
;
102 BooViewEngine
.Options
.SaveToDisk
= true;
103 BooViewEngine
.Options
.Debug
= true;
104 BooViewEngine
.Options
.BatchCompile
= false;
106 BooViewEngine
.SetViewSourceLoader(loader
);
107 BooViewEngine
.Initialize();
111 public void ProcessView_StripRailsExtension(string url
)
113 ProcessView(url
.Replace(".rails", ""));
116 protected string ProcessView(string templatePath
)
118 StringWriter sw
= new StringWriter();
119 if (string.IsNullOrEmpty(Layout
) == false)
120 ControllerContext
.LayoutNames
= new string[] { Layout, }
;
121 MockEngineContext
.CurrentControllerContext
= ControllerContext
;
122 BooViewEngine
.Process(templatePath
, sw
, MockEngineContext
, null, ControllerContext
);
123 lastOutput
= sw
.ToString();
127 public void AssertReplyEqualTo(string actual
)
129 Assert
.AreEqual(actual
, lastOutput
);
132 public void AssertReplyContains(string contained
)
134 StringAssert
.Contains(contained
, lastOutput
);