Fixing an issue with output parameters that are of type IntPtr
[castle.git] / MonoRail / Castle.MonoRail.Framework.Tests / Helpers / FormHelperCheckboxFieldListTestCase.cs
blob1d560dfe92b273b1df00a5d0104c2a186cb22b50
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 using System.Text.RegularExpressions;
17 namespace Castle.MonoRail.Framework.Tests.Helpers
19 using System;
20 using System.Globalization;
21 using System.IO;
22 using System.Threading;
24 using Castle.MonoRail.Framework.Helpers;
25 using Castle.MonoRail.Framework.Tests.Controllers;
27 using NUnit.Framework;
29 [TestFixture]
30 public class FormHelperCheckboxFieldListTestCase
32 class MockClass
34 private int[] values;
36 public int[] Values
38 get { return values; }
39 set { values = value; }
43 private FormHelper helper;
44 private Product product;
45 private SimpleUser user;
46 private SimpleUser[] users;
47 private Subscription subscription;
48 private Month[] months;
49 private MockClass mock;
51 [SetUp]
52 public void Init()
54 CultureInfo en = CultureInfo.CreateSpecificCulture("en");
56 Thread.CurrentThread.CurrentCulture = en;
57 Thread.CurrentThread.CurrentUICulture = en;
59 helper = new FormHelper();
61 subscription = new Subscription();
62 mock = new MockClass();
63 months = new Month[] {new Month(1, "January"), new Month(1, "February")};
64 product = new Product("memory card", 10, (decimal) 12.30);
65 user = new SimpleUser();
66 users = new SimpleUser[] { new SimpleUser(1, false), new SimpleUser(2, true), new SimpleUser(3, false), new SimpleUser(4, true) };
67 mock.Values = new int[] { 2, 3 };
69 HomeController controller = new HomeController();
70 ControllerContext context = new ControllerContext();
72 context.PropertyBag.Add("product", product);
73 context.PropertyBag.Add("user", user);
74 context.PropertyBag.Add("users", users);
75 context.PropertyBag.Add("roles", new Role[] { new Role(1, "a"), new Role(2, "b"), new Role(3, "c") });
76 context.PropertyBag.Add("sendemail", true);
77 context.PropertyBag.Add("confirmation", "abc");
78 context.PropertyBag.Add("fileaccess", FileAccess.Read);
79 context.PropertyBag.Add("subscription", subscription);
80 context.PropertyBag.Add("months", months);
81 context.PropertyBag.Add("mock", mock);
83 helper.SetController(controller, context);
86 /// <summary>
87 /// Code posted on the mailing list
88 /// </summary>
89 [Test]
90 public void BugReport1()
92 FormHelper.CheckboxList list =
93 helper.CreateCheckboxList("mock.Values", new int[] {1,2,3,4});
95 Assert.IsNotNull(list);
97 int index = 0;
99 foreach(Object item in list)
101 String content = list.Item();
102 if (index == 1 || index == 2)
104 Assert.AreEqual("<input type=\"checkbox\" id=\"mock_Values_" + index +
105 "_\" name=\"mock.Values[" + index + "]\" value=\"" + item + "\" checked=\"checked\" />", content);
107 else
109 Assert.AreEqual("<input type=\"checkbox\" id=\"mock_Values_" + index +
110 "_\" name=\"mock.Values[" + index + "]\" value=\"" + item + "\" />", content);
112 index++;
117 /// <summary>
118 /// Tests the subscription.Months as null and an int array as source
119 /// </summary>
120 [Test]
121 public void CheckboxFieldList1()
123 subscription.Months = null;
125 FormHelper.CheckboxList list =
126 helper.CreateCheckboxList("subscription.Months", new int[] {1,2,3,4,5,6});
128 Assert.IsNotNull(list);
130 int index = 0;
132 foreach(Object item in list)
134 String content = list.Item();
135 Assert.AreEqual("<input type=\"checkbox\" id=\"subscription_Months_" + index + "_\" name=\"subscription.Months[" + index + "]\" value=\"" + item + "\" />", content);
136 index++;
140 /// <summary>
141 /// Tests the subscription.Months as non null and an int array as source
142 /// </summary>
143 [Test]
144 public void CheckboxFieldList2()
146 subscription.Months = new int[] { 1, 2, 3, 4, 5 };
148 FormHelper.CheckboxList list =
149 helper.CreateCheckboxList("subscription.Months", new int[] {1,2,3,4,5});
151 Assert.IsNotNull(list);
153 int index = 0;
155 foreach(Object item in list)
157 String content = list.Item();
158 Assert.AreEqual("<input type=\"checkbox\" id=\"subscription_Months_" + index + "_\" " +
159 "name=\"subscription.Months[" + index + "]\" value=\"" + item + "\" checked=\"checked\" />", content);
160 index++;
164 /// <summary>
165 /// Tests the subscription.Months2 as null and using a <c>Month</c> array as data source
166 /// </summary>
167 [Test]
168 public void CheckboxFieldList3()
170 FormHelper.CheckboxList list =
171 helper.CreateCheckboxList("subscription.Months2", months, DictHelper.Create("value=id"));
173 Assert.IsNotNull(list);
175 int index = 0;
177 foreach(Month item in list)
179 String content = list.Item();
180 Assert.AreEqual("<input type=\"checkbox\" id=\"subscription_Months2_" + index +
181 "_\" name=\"subscription.Months2[" + index + "].id\" value=\"" + item.Id + "\" />", content);
182 index++;
186 /// <summary>
187 /// Tests the subscription.Months2 as null and using a <c>Month</c> array as data source
188 /// </summary>
189 [Test]
190 public void CheckboxFieldList3a()
192 subscription.Months3 = null;
194 FormHelper.CheckboxList list =
195 helper.CreateCheckboxList("subscription.Months3", months, DictHelper.Create("value=id"));
197 Assert.IsNotNull(list);
199 int index = 0;
201 foreach(Month item in list)
203 String content = list.Item();
204 Assert.AreEqual("<input type=\"checkbox\" id=\"subscription_Months3_" + index +
205 "_\" name=\"subscription.Months3[" + index + "].id\" value=\"" + item.Id + "\" />", content);
206 index++;
210 /// <summary>
211 /// Tests the subscription.Months2 as non null and using a <c>Month</c> array as data source
212 /// </summary>
213 [Test]
214 public void CheckboxFieldList4()
216 subscription.Months2 = new Month[] {new Month(3, "March")};
218 FormHelper.CheckboxList list =
219 helper.CreateCheckboxList("subscription.Months2", months, DictHelper.Create("value=id"));
221 Assert.IsNotNull(list);
223 int index = 0;
225 foreach(Month item in list)
227 String content = list.Item();
228 Assert.AreEqual("<input type=\"checkbox\" id=\"subscription_Months2_" + index +
229 "_\" name=\"subscription.Months2[" + index + "].Id\" value=\"" + item.Id + "\" />", content);
230 index++;
234 /// <summary>
235 /// Tests the subscription.Months2 as non null and using a <c>Month</c> array as data source
236 /// but with selection
237 /// </summary>
238 [Test]
239 public void CheckboxFieldList5()
241 subscription.Months2 = new Month[] {new Month(1, "January"), new Month(2, "Feb") };
243 FormHelper.CheckboxList list =
244 helper.CreateCheckboxList("subscription.Months2", months, DictHelper.Create("value=id"));
246 Assert.IsNotNull(list);
248 int index = 0;
250 foreach(Month item in list)
252 String content = list.Item();
253 Assert.AreEqual("<input type=\"checkbox\" id=\"subscription_Months2_" + index +
254 "_\" name=\"subscription.Months2[" + index + "].Id\" value=\"" + item.Id + "\" checked=\"checked\" />", content);
255 index++;
259 [Test]
260 public void UsingIdPerElement()
262 subscription.Months = new int[] { 1, 2, 3, 4, 5 };
264 FormHelper.CheckboxList list =
265 helper.CreateCheckboxList("subscription.Months", new int[] { 1, 2 });
267 Assert.IsNotNull(list);
269 int index = 0;
271 foreach(Object item in list)
273 string content = list.Item("menu" + index);
275 if (index < 2)
277 Assert.AreEqual("<input type=\"checkbox\" id=\"menu" + index + "\" name=\"subscription.Months[" + index + "]\" value=\"" + item + "\" checked=\"checked\" />", content);
279 else
281 Assert.AreEqual("<input type=\"checkbox\" name=\"subscription.Months[" + index + "]\" value=\"" + item + "\" id=\"menu" + index + "\" />", content);
283 index++;
287 [Test]
288 public void CheckboxFieldListInDotNet2()
290 FormHelper.CheckboxList list =
291 helper.CreateCheckboxList("subscription.Months4", months, DictHelper.Create("value=id"));
293 Assert.IsNotNull(list);
295 int index = 0;
297 foreach(Month item in list)
299 String content = list.Item();
300 Assert.AreEqual("<input type=\"checkbox\" id=\"subscription_Months4_" + index + "_\" name=\"subscription.Months4[" + index + "].id\" value=\"" + item.Id + "\" />", content);
302 index++;
306 [Test]
307 public void CheckboxFieldListInDotNet2_WithSelection()
309 subscription.Months4.Add(new Month(1, "January"));
310 subscription.Months4.Add(new Month(2, "Feb"));
312 FormHelper.CheckboxList list =
313 helper.CreateCheckboxList("subscription.Months4", months, DictHelper.Create("value=id"));
315 Assert.IsNotNull(list);
317 int index = 0;
319 foreach(Month item in list)
321 String content = list.Item();
322 Assert.AreEqual("<input type=\"checkbox\" id=\"subscription_Months4_" + index + "_\" name=\"subscription.Months4[" + index + "].Id\" value=\"" + item.Id + "\" checked=\"checked\" />", content);
323 index++;
327 /// <summary>
328 /// Test a simple checkbox list with LabelFor values
329 /// </summary>
330 [Test]
331 public void CheckBoxFieldListWithLabelFor()
333 subscription.Months = new int[] { 1, 2, 3, 4, 5 };
335 FormHelper.CheckboxList list =
336 helper.CreateCheckboxList("subscription.Months", new int[] { 1, 2, 3, 4, 5 });
338 Assert.IsNotNull(list);
340 foreach (Object item in list)
342 string content = list.Item();
343 string label = list.LabelFor(item.ToString());
345 Match checkboxIdMatch = new Regex("\\s+id=\"(?<value>[^\"]+)\"(?:\\s+|>)", RegexOptions.IgnoreCase).Match(content);
346 Match labelIdMatch = new Regex("\\s+for=\"(?<value>[^\"]+)\"(?:\\s+|>)", RegexOptions.IgnoreCase).Match(label);
347 Match labelContentMatch = new Regex("<label[^>]*>(?<value>[^<]*)</label>", RegexOptions.IgnoreCase).Match(label);
349 Assert.AreEqual(checkboxIdMatch.Groups["value"].Value, labelIdMatch.Groups["value"].Value);
350 Assert.AreEqual(item.ToString(), labelContentMatch.Groups["value"].Value);
354 /// <summary>
355 /// Test a simple checkbox list with LabelFor values
356 /// </summary>
357 [Test]
358 public void CheckBoxFieldListWithLabelForUsingCustomLabel()
360 subscription.Months = new int[] { 1, 2, 3, 4, 5 };
362 FormHelper.CheckboxList list =
363 helper.CreateCheckboxList("subscription.Months", new int[] { 1, 2, 3, 4, 5 });
365 Assert.IsNotNull(list);
367 foreach (Object item in list)
369 string content = list.Item();
370 string label = list.LabelFor("Item "+item);
372 Match checkboxIdMatch = new Regex("\\s+id=\"(?<value>[^\"]+)\"(?:\\s+|>)", RegexOptions.IgnoreCase).Match(content);
373 Match labelIdMatch = new Regex("\\s+for=\"(?<value>[^\"]+)\"(?:\\s+|>)", RegexOptions.IgnoreCase).Match(label);
374 Match labelContentMatch = new Regex("<label[^>]*>(?<value>[^<]*)</label>", RegexOptions.IgnoreCase).Match(label);
376 Assert.AreEqual(checkboxIdMatch.Groups["value"].Value, labelIdMatch.Groups["value"].Value);
377 Assert.AreEqual("Item " + item, labelContentMatch.Groups["value"].Value);