MR-223: AmbiguousMatch exception when querying properties from NHibernate-enabled...
[castle.git] / MonoRail / Castle.MonoRail.Framework.Tests / Helpers / FormHelperCheckboxFieldListTestCase.cs
blob8747fb8478d033f59763a6ef819eca360c2632fa
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 Subscription subscription;
45 private Month[] months;
46 private MockClass mock;
48 [SetUp]
49 public void Init()
51 CultureInfo en = CultureInfo.CreateSpecificCulture("en");
53 Thread.CurrentThread.CurrentCulture = en;
54 Thread.CurrentThread.CurrentUICulture = en;
56 helper = new FormHelper();
58 subscription = new Subscription();
59 mock = new MockClass();
60 months = new Month[] {new Month(1, "January"), new Month(1, "February")};
61 product = new Product("memory card", 10, (decimal) 12.30);
62 user = new SimpleUser();
63 mock.Values = new int[] { 2, 3 };
65 HomeController controller = new HomeController();
67 controller.PropertyBag.Add("product", product);
68 controller.PropertyBag.Add("user", user);
69 controller.PropertyBag.Add("roles", new Role[] { new Role(1, "a"), new Role(2, "b"), new Role(3, "c") });
70 controller.PropertyBag.Add("sendemail", true);
71 controller.PropertyBag.Add("confirmation", "abc");
72 controller.PropertyBag.Add("fileaccess", FileAccess.Read);
73 controller.PropertyBag.Add("subscription", subscription);
74 controller.PropertyBag.Add("months", months);
75 controller.PropertyBag.Add("mock", mock);
77 helper.SetController(controller);
80 /// <summary>
81 /// Code posted on the mailing list
82 /// </summary>
83 [Test]
84 public void BugReport1()
86 FormHelper.CheckboxList list =
87 helper.CreateCheckboxList("mock.Values", new int[] {1,2,3,4});
89 Assert.IsNotNull(list);
91 int index = 0;
93 foreach(Object item in list)
95 String content = list.Item();
96 if (index == 1 || index == 2)
98 Assert.AreEqual("<input type=\"checkbox\" id=\"mock_Values_" + index +
99 "_\" name=\"mock.Values[" + index + "]\" value=\"" + item + "\" checked=\"checked\" />", content);
101 else
103 Assert.AreEqual("<input type=\"checkbox\" id=\"mock_Values_" + index +
104 "_\" name=\"mock.Values[" + index + "]\" value=\"" + item + "\" />", content);
106 index++;
111 /// <summary>
112 /// Tests the subscription.Months as null and an int array as source
113 /// </summary>
114 [Test]
115 public void CheckboxFieldList1()
117 subscription.Months = null;
119 FormHelper.CheckboxList list =
120 helper.CreateCheckboxList("subscription.Months", new int[] {1,2,3,4,5,6});
122 Assert.IsNotNull(list);
124 int index = 0;
126 foreach(Object item in list)
128 String content = list.Item();
129 Assert.AreEqual("<input type=\"checkbox\" id=\"subscription_Months_" + index +
130 "_\" name=\"subscription.Months[" + index + "]\" value=\"" + item + "\" />", content);
131 index++;
135 /// <summary>
136 /// Tests the subscription.Months as non null and an int array as source
137 /// </summary>
138 [Test]
139 public void CheckboxFieldList2()
141 subscription.Months = new int[] { 1, 2, 3, 4, 5 };
143 FormHelper.CheckboxList list =
144 helper.CreateCheckboxList("subscription.Months", new int[] {1,2,3,4,5});
146 Assert.IsNotNull(list);
148 int index = 0;
150 foreach(Object item in list)
152 String content = list.Item();
153 Assert.AreEqual("<input type=\"checkbox\" id=\"subscription_Months_" + index +
154 "_\" name=\"subscription.Months[" + index + "]\" value=\"" + item + "\" checked=\"checked\" />", content);
155 index++;
159 /// <summary>
160 /// Tests the subscription.Months2 as null and using a <c>Month</c> array as data source
161 /// </summary>
162 [Test]
163 public void CheckboxFieldList3()
165 FormHelper.CheckboxList list =
166 helper.CreateCheckboxList("subscription.Months2", months, DictHelper.Create("value=id"));
168 Assert.IsNotNull(list);
170 int index = 0;
172 foreach(Month item in list)
174 String content = list.Item();
175 Assert.AreEqual("<input type=\"checkbox\" id=\"subscription_Months2_" + index +
176 "_\" name=\"subscription.Months2[" + index + "].id\" value=\"" + item.Id + "\" />", content);
177 index++;
181 /// <summary>
182 /// Tests the subscription.Months2 as null and using a <c>Month</c> array as data source
183 /// </summary>
184 [Test]
185 public void CheckboxFieldList3a()
187 subscription.Months3 = null;
189 FormHelper.CheckboxList list =
190 helper.CreateCheckboxList("subscription.Months3", months, DictHelper.Create("value=id"));
192 Assert.IsNotNull(list);
194 int index = 0;
196 foreach(Month item in list)
198 String content = list.Item();
199 Assert.AreEqual("<input type=\"checkbox\" id=\"subscription_Months3_" + index +
200 "_\" name=\"subscription.Months3[" + index + "].id\" value=\"" + item.Id + "\" />", content);
201 index++;
205 /// <summary>
206 /// Tests the subscription.Months2 as non null and using a <c>Month</c> array as data source
207 /// </summary>
208 [Test]
209 public void CheckboxFieldList4()
211 subscription.Months2 = new Month[] {new Month(3, "March")};
213 FormHelper.CheckboxList list =
214 helper.CreateCheckboxList("subscription.Months2", months, DictHelper.Create("value=id"));
216 Assert.IsNotNull(list);
218 int index = 0;
220 foreach(Month item in list)
222 String content = list.Item();
223 Assert.AreEqual("<input type=\"checkbox\" id=\"subscription_Months2_" + index +
224 "_\" name=\"subscription.Months2[" + index + "].Id\" value=\"" + item.Id + "\" />", content);
225 index++;
229 /// <summary>
230 /// Tests the subscription.Months2 as non null and using a <c>Month</c> array as data source
231 /// but with selection
232 /// </summary>
233 [Test]
234 public void CheckboxFieldList5()
236 subscription.Months2 = new Month[] {new Month(1, "January"), new Month(2, "Feb") };
238 FormHelper.CheckboxList list =
239 helper.CreateCheckboxList("subscription.Months2", months, DictHelper.Create("value=id"));
241 Assert.IsNotNull(list);
243 int index = 0;
245 foreach(Month item in list)
247 String content = list.Item();
248 Assert.AreEqual("<input type=\"checkbox\" id=\"subscription_Months2_" + index +
249 "_\" name=\"subscription.Months2[" + index + "].Id\" value=\"" + item.Id + "\" checked=\"checked\" />", content);
250 index++;
254 #if DOTNET2
256 [Test]
257 public void CheckboxFieldListInDotNet2()
259 FormHelper.CheckboxList list =
260 helper.CreateCheckboxList("subscription.Months4", months, DictHelper.Create("value=id"));
262 Assert.IsNotNull(list);
264 int index = 0;
266 foreach(Month item in list)
268 String content = list.Item();
269 Assert.AreEqual("<input type=\"checkbox\" id=\"subscription_Months4_" + index +
270 "_\" name=\"subscription.Months4[" + index + "].id\" value=\"" + item.Id + "\" />", content);
271 index++;
275 [Test]
276 public void CheckboxFieldListInDotNet2_WithSelection()
278 subscription.Months4.Add(new Month(1, "January"));
279 subscription.Months4.Add(new Month(2, "Feb"));
281 FormHelper.CheckboxList list =
282 helper.CreateCheckboxList("subscription.Months4", months, DictHelper.Create("value=id"));
284 Assert.IsNotNull(list);
286 int index = 0;
288 foreach(Month item in list)
290 String content = list.Item();
291 Assert.AreEqual("<input type=\"checkbox\" id=\"subscription_Months4_" + index +
292 "_\" name=\"subscription.Months4[" + index + "].Id\" value=\"" + item.Id + "\" checked=\"checked\" />", content);
293 index++;
297 #endif