Minor changes to improve testability of helpers
[castle.git] / MonoRail / Castle.MonoRail.Framework.Tests / Helpers / FormHelperCheckboxFieldListTestCase.cs
blobfe01ffebc313e52699861268c4527a4d4b0ba3e6
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.Tests.Helpers
17 using System;
18 using System.Globalization;
19 using System.IO;
20 using System.Threading;
22 using Castle.MonoRail.Framework.Helpers;
23 using Castle.MonoRail.Framework.Tests.Controllers;
25 using NUnit.Framework;
27 [TestFixture]
28 public class FormHelperCheckboxFieldListTestCase
30 class MockClass
32 private int[] values;
34 public int[] Values
36 get { return values; }
37 set { values = value; }
41 private FormHelper helper;
42 private Product product;
43 private SimpleUser user;
44 private SimpleUser[] users;
45 private Subscription subscription;
46 private Month[] months;
47 private MockClass mock;
49 [SetUp]
50 public void Init()
52 CultureInfo en = CultureInfo.CreateSpecificCulture("en");
54 Thread.CurrentThread.CurrentCulture = en;
55 Thread.CurrentThread.CurrentUICulture = en;
57 helper = new FormHelper();
59 subscription = new Subscription();
60 mock = new MockClass();
61 months = new Month[] {new Month(1, "January"), new Month(1, "February")};
62 product = new Product("memory card", 10, (decimal) 12.30);
63 user = new SimpleUser();
64 users = new SimpleUser[] { new SimpleUser(1, false), new SimpleUser(2, true), new SimpleUser(3, false), new SimpleUser(4, true) };
65 mock.Values = new int[] { 2, 3 };
67 HomeController controller = new HomeController();
69 controller.PropertyBag.Add("product", product);
70 controller.PropertyBag.Add("user", user);
71 controller.PropertyBag.Add("users", users);
72 controller.PropertyBag.Add("roles", new Role[] { new Role(1, "a"), new Role(2, "b"), new Role(3, "c") });
73 controller.PropertyBag.Add("sendemail", true);
74 controller.PropertyBag.Add("confirmation", "abc");
75 controller.PropertyBag.Add("fileaccess", FileAccess.Read);
76 controller.PropertyBag.Add("subscription", subscription);
77 controller.PropertyBag.Add("months", months);
78 controller.PropertyBag.Add("mock", mock);
80 helper.SetController(controller);
83 /// <summary>
84 /// Code posted on the mailing list
85 /// </summary>
86 [Test]
87 public void BugReport1()
89 FormHelper.CheckboxList list =
90 helper.CreateCheckboxList("mock.Values", new int[] {1,2,3,4});
92 Assert.IsNotNull(list);
94 int index = 0;
96 foreach(Object item in list)
98 String content = list.Item();
99 if (index == 1 || index == 2)
101 Assert.AreEqual("<input type=\"checkbox\" id=\"mock_Values_" + index +
102 "_\" name=\"mock.Values[" + index + "]\" value=\"" + item + "\" checked=\"checked\" />", content);
104 else
106 Assert.AreEqual("<input type=\"checkbox\" id=\"mock_Values_" + index +
107 "_\" name=\"mock.Values[" + index + "]\" value=\"" + item + "\" />", content);
109 index++;
114 /// <summary>
115 /// Tests the subscription.Months as null and an int array as source
116 /// </summary>
117 [Test]
118 public void CheckboxFieldList1()
120 subscription.Months = null;
122 FormHelper.CheckboxList list =
123 helper.CreateCheckboxList("subscription.Months", new int[] {1,2,3,4,5,6});
125 Assert.IsNotNull(list);
127 int index = 0;
129 foreach(Object item in list)
131 String content = list.Item();
132 Assert.AreEqual("<input type=\"checkbox\" id=\"subscription_Months_" + index + "_\" name=\"subscription.Months[" + index + "]\" value=\"" + item + "\" />", content);
133 index++;
137 /// <summary>
138 /// Tests the subscription.Months as non null and an int array as source
139 /// </summary>
140 [Test]
141 public void CheckboxFieldList2()
143 subscription.Months = new int[] { 1, 2, 3, 4, 5 };
145 FormHelper.CheckboxList list =
146 helper.CreateCheckboxList("subscription.Months", new int[] {1,2,3,4,5});
148 Assert.IsNotNull(list);
150 int index = 0;
152 foreach(Object item in list)
154 String content = list.Item();
155 Assert.AreEqual("<input type=\"checkbox\" id=\"subscription_Months_" + index + "_\" " +
156 "name=\"subscription.Months[" + index + "]\" value=\"" + item + "\" checked=\"checked\" />", content);
157 index++;
161 /// <summary>
162 /// Tests the subscription.Months2 as null and using a <c>Month</c> array as data source
163 /// </summary>
164 [Test]
165 public void CheckboxFieldList3()
167 FormHelper.CheckboxList list =
168 helper.CreateCheckboxList("subscription.Months2", months, DictHelper.Create("value=id"));
170 Assert.IsNotNull(list);
172 int index = 0;
174 foreach(Month item in list)
176 String content = list.Item();
177 Assert.AreEqual("<input type=\"checkbox\" id=\"subscription_Months2_" + index +
178 "_\" name=\"subscription.Months2[" + index + "].id\" value=\"" + item.Id + "\" />", content);
179 index++;
183 /// <summary>
184 /// Tests the subscription.Months2 as null and using a <c>Month</c> array as data source
185 /// </summary>
186 [Test]
187 public void CheckboxFieldList3a()
189 subscription.Months3 = null;
191 FormHelper.CheckboxList list =
192 helper.CreateCheckboxList("subscription.Months3", months, DictHelper.Create("value=id"));
194 Assert.IsNotNull(list);
196 int index = 0;
198 foreach(Month item in list)
200 String content = list.Item();
201 Assert.AreEqual("<input type=\"checkbox\" id=\"subscription_Months3_" + index +
202 "_\" name=\"subscription.Months3[" + index + "].id\" value=\"" + item.Id + "\" />", content);
203 index++;
207 /// <summary>
208 /// Tests the subscription.Months2 as non null and using a <c>Month</c> array as data source
209 /// </summary>
210 [Test]
211 public void CheckboxFieldList4()
213 subscription.Months2 = new Month[] {new Month(3, "March")};
215 FormHelper.CheckboxList list =
216 helper.CreateCheckboxList("subscription.Months2", months, DictHelper.Create("value=id"));
218 Assert.IsNotNull(list);
220 int index = 0;
222 foreach(Month item in list)
224 String content = list.Item();
225 Assert.AreEqual("<input type=\"checkbox\" id=\"subscription_Months2_" + index +
226 "_\" name=\"subscription.Months2[" + index + "].Id\" value=\"" + item.Id + "\" />", content);
227 index++;
231 /// <summary>
232 /// Tests the subscription.Months2 as non null and using a <c>Month</c> array as data source
233 /// but with selection
234 /// </summary>
235 [Test]
236 public void CheckboxFieldList5()
238 subscription.Months2 = new Month[] {new Month(1, "January"), new Month(2, "Feb") };
240 FormHelper.CheckboxList list =
241 helper.CreateCheckboxList("subscription.Months2", months, DictHelper.Create("value=id"));
243 Assert.IsNotNull(list);
245 int index = 0;
247 foreach(Month item in list)
249 String content = list.Item();
250 Assert.AreEqual("<input type=\"checkbox\" id=\"subscription_Months2_" + index +
251 "_\" name=\"subscription.Months2[" + index + "].Id\" value=\"" + item.Id + "\" checked=\"checked\" />", content);
252 index++;
256 [Test]
257 public void UsingIdPerElement()
259 subscription.Months = new int[] { 1, 2, 3, 4, 5 };
261 FormHelper.CheckboxList list =
262 helper.CreateCheckboxList("subscription.Months", new int[] { 1, 2 });
264 Assert.IsNotNull(list);
266 int index = 0;
268 foreach(Object item in list)
270 string content = list.Item("menu" + index);
272 if (index < 2)
274 Assert.AreEqual("<input type=\"checkbox\" id=\"menu" + index + "\" name=\"subscription.Months[" + index + "]\" value=\"" + item + "\" checked=\"checked\" />", content);
276 else
278 Assert.AreEqual("<input type=\"checkbox\" name=\"subscription.Months[" + index + "]\" value=\"" + item + "\" id=\"menu" + index + "\" />", content);
280 index++;
284 [Test]
285 public void CheckboxFieldListInDotNet2()
287 FormHelper.CheckboxList list =
288 helper.CreateCheckboxList("subscription.Months4", months, DictHelper.Create("value=id"));
290 Assert.IsNotNull(list);
292 int index = 0;
294 foreach(Month item in list)
296 String content = list.Item();
297 Assert.AreEqual("<input type=\"checkbox\" id=\"subscription_Months4_" + index + "_\" name=\"subscription.Months4[" + index + "].id\" value=\"" + item.Id + "\" />", content);
299 index++;
303 [Test]
304 public void CheckboxFieldListInDotNet2_WithSelection()
306 subscription.Months4.Add(new Month(1, "January"));
307 subscription.Months4.Add(new Month(2, "Feb"));
309 FormHelper.CheckboxList list =
310 helper.CreateCheckboxList("subscription.Months4", months, DictHelper.Create("value=id"));
312 Assert.IsNotNull(list);
314 int index = 0;
316 foreach(Month item in list)
318 String content = list.Item();
319 Assert.AreEqual("<input type=\"checkbox\" id=\"subscription_Months4_" + index + "_\" name=\"subscription.Months4[" + index + "].Id\" value=\"" + item.Id + "\" checked=\"checked\" />", content);
320 index++;