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
.Framework
.Tests
.Helpers
18 using System
.Collections
;
19 using System
.Collections
.Generic
;
21 using System
.Globalization
;
23 using System
.Threading
;
25 using Castle
.MonoRail
.Framework
.Helpers
;
26 using Castle
.MonoRail
.Framework
.Tests
.Controllers
;
28 using NUnit
.Framework
;
31 public class FormHelperSelectTestCase
33 private FormHelper helper
;
34 private Product product
;
35 private SimpleUser user
;
36 private Subscription subscription
;
37 private Month
[] months
;
38 private Contact contact
;
39 private DataTable workTable
;
44 CultureInfo en
= CultureInfo
.CreateSpecificCulture("en");
46 Thread
.CurrentThread
.CurrentCulture
= en
;
47 Thread
.CurrentThread
.CurrentUICulture
= en
;
49 helper
= new FormHelper();
51 subscription
= new Subscription();
52 months
= new Month
[] {new Month(1, "January"), new Month(1, "February")}
;
53 product
= new Product("memory card", 10, (decimal) 12.30);
54 user
= new SimpleUser();
55 contact
= new Contact();
57 HomeController controller
= new HomeController();
58 ControllerContext context
= new ControllerContext();
60 context
.PropertyBag
.Add("product", product
);
61 context
.PropertyBag
.Add("user", user
);
62 context
.PropertyBag
.Add("roles", new Role
[] { new Role(1, "a"), new Role(2, "b"), new Role(3, "c") }
);
63 context
.PropertyBag
.Add("sendemail", true);
64 context
.PropertyBag
.Add("confirmation", "abc");
65 context
.PropertyBag
.Add("fileaccess", FileAccess
.Read
);
66 context
.PropertyBag
.Add("subscription", subscription
);
67 context
.PropertyBag
.Add("months", months
);
68 context
.PropertyBag
.Add("contact", contact
);
70 workTable
= new DataTable("Customers");
71 DataColumn workCol
= workTable
.Columns
.Add("CustID", typeof(Int32
));
72 workCol
.AllowDBNull
= false;
73 workCol
.Unique
= true;
74 workTable
.Columns
.Add("Name", typeof(String
));
76 DataRow row
= workTable
.NewRow();
78 row
[1] = "chris rocks";
79 workTable
.Rows
.Add(row
);
80 row
= workTable
.NewRow();
82 row
[1] = "will ferrell";
83 workTable
.Rows
.Add(row
);
85 helper
.SetController(controller
, context
);
89 public void SimplisticSelect()
91 ArrayList list
= new ArrayList();
96 Assert
.AreEqual("<select id=\"product_category_id\" name=\"product.category.id\" >" + Environment
.NewLine
+
97 "<option value=\"cat1\">cat1</option>" + Environment
.NewLine
+ "<option value=\"cat2\">cat2</option>" + Environment
.NewLine
+ "</select>",
98 helper
.Select("product.category.id", list
));
102 public void SelectWithFirstOption()
104 ArrayList list
= new ArrayList();
108 Assert
.AreEqual("<select id=\"product_category_id\" name=\"product.category.id\" >" + Environment
.NewLine
+
109 "<option value=\"0\">Please select</option>" + Environment
.NewLine
+
110 "<option value=\"cat1\">cat1</option>" + Environment
.NewLine
+ "<option value=\"cat2\">cat2</option>" + Environment
.NewLine
+ "</select>",
111 helper
.Select("product.category.id", list
, DictHelper
.Create("firstoption=Please select") ));
115 public void SelectWithValueAndText()
117 ArrayList list
= new ArrayList();
118 list
.Add(new ProductCategory(1, "cat1"));
119 list
.Add(new ProductCategory(2, "cat2"));
121 Assert
.AreEqual("<select id=\"product_category_id\" name=\"product.category.id\" >" + Environment
.NewLine
+
122 "<option value=\"1\">cat1</option>" + Environment
.NewLine
+ "<option value=\"2\">cat2</option>" + Environment
.NewLine
+ "</select>",
123 helper
.Select("product.category.id", list
, DictHelper
.Create("value=id", "text=name") ));
127 public void SelectOnPrimitiveArrayWithoutValueAndText()
129 Assert
.AreEqual("<select id=\"product_category_id\" name=\"product.category.id\" >" + Environment
.NewLine
+
130 "<option value=\"1\">1</option>" + Environment
.NewLine
+ "<option value=\"2\">2</option>" + Environment
.NewLine
+ "<option value=\"3\">3</option>" + Environment
.NewLine
+ "<option value=\"4\">4</option>" + Environment
.NewLine
+ "<option selected=\"selected\" value=\"5\">5</option>" + Environment
.NewLine
+ "</select>",
131 helper
.Select("product.category.id", 5, new int[] { 1, 2, 3, 4, 5}
, DictHelper
.Create() ));
135 public void SelectWithValueAndTextAndSelect()
137 ArrayList list
= new ArrayList();
138 list
.Add(new ProductCategory(1, "cat1"));
139 list
.Add(new ProductCategory(2, "cat2"));
141 product
.Category
.Id
= 2;
143 Assert
.AreEqual("<select id=\"product_category_id\" name=\"product.category.id\" >" + Environment
.NewLine
+
144 "<option value=\"1\">cat1</option>" + Environment
.NewLine
+ "<option selected=\"selected\" value=\"2\">cat2</option>" + Environment
.NewLine
+ "</select>",
145 helper
.Select("product.category.id", list
, DictHelper
.Create("value=id", "text=name") ));
149 public void SelectWithNoSelection()
151 ArrayList list
= new ArrayList();
152 list
.Add(new Role(1, "role1"));
153 list
.Add(new Role(2, "role2"));
155 Assert
.AreEqual("<select id=\"user_roles\" name=\"user.roles\" >" + Environment
.NewLine
+
156 "<option value=\"1\">role1</option>" + Environment
.NewLine
+ "<option value=\"2\">role2</option>" + Environment
.NewLine
+ "</select>",
157 helper
.Select("user.roles", list
, DictHelper
.Create("value=id", "text=name") ));
161 public void SelectWithSelection()
163 ArrayList list
= new ArrayList();
164 list
.Add(new Role(1, "role1"));
165 list
.Add(new Role(2, "role2"));
167 user
.Roles
.Add(new Role(1, "role1"));
168 user
.Roles
.Add(new Role(2, "role2"));
170 Assert
.AreEqual("<select id=\"user_roles\" name=\"user.roles\" >" + Environment
.NewLine
+
171 "<option selected=\"selected\" value=\"1\">role1</option>" + Environment
.NewLine
+ "<option selected=\"selected\" value=\"2\">role2</option>" + Environment
.NewLine
+ "</select>",
172 helper
.Select("user.roles", list
, DictHelper
.Create("value=id", "text=name") ));
176 public void SelectWithSelection2()
178 ArrayList list
= new ArrayList();
179 list
.Add(new Role(1, "role1"));
180 list
.Add(new Role(2, "role2"));
182 user
.Roles
.Add(new Role(1, "role1"));
184 Assert
.AreEqual("<select id=\"user_roles\" name=\"user.roles\" >" + Environment
.NewLine
+
185 "<option selected=\"selected\" value=\"1\">role1</option>" + Environment
.NewLine
+ "<option value=\"2\">role2</option>" + Environment
.NewLine
+ "</select>",
186 helper
.Select("user.roles", list
, DictHelper
.Create("value=id", "text=name") ));
190 public void SelectWithArraySelection()
192 ArrayList list
= new ArrayList();
193 list
.Add(new Role(1, "role1"));
194 list
.Add(new Role(2, "role2"));
196 user
.Roles
.Add(new Role(1, "role1"));
197 user
.Roles
.Add(new Role(2, "role2"));
199 Assert
.AreEqual("<select id=\"user_RolesAsArray\" name=\"user.RolesAsArray\" >" + Environment
.NewLine
+
200 "<option selected=\"selected\" value=\"1\">role1</option>" + Environment
.NewLine
+ "<option selected=\"selected\" value=\"2\">role2</option>" + Environment
.NewLine
+ "</select>",
201 helper
.Select("user.RolesAsArray", list
, DictHelper
.Create("value=id", "text=name") ));
205 public void SelectWithArraySelection2()
207 ArrayList list
= new ArrayList();
208 list
.Add(new Role(1, "role1"));
209 list
.Add(new Role(2, "role2"));
211 user
.Roles
.Add(new Role(1, "role1"));
213 Assert
.AreEqual("<select id=\"user_RolesAsArray\" name=\"user.RolesAsArray\" >" + Environment
.NewLine
+
214 "<option selected=\"selected\" value=\"1\">role1</option>" + Environment
.NewLine
+ "<option value=\"2\">role2</option>" + Environment
.NewLine
+ "</select>",
215 helper
.Select("user.RolesAsArray", list
, DictHelper
.Create("value=id", "text=name") ));
219 public void SelectWithArraySelectionNoId()
221 ArrayList list
= new ArrayList();
222 list
.Add(new Role(1, "role1"));
223 list
.Add(new Role(2, "role2"));
225 user
.Roles
.Add(new Role(1, "role1"));
226 user
.Roles
.Add(new Role(2, "role2"));
228 Assert
.AreEqual("<select id=\"user_RolesAsArray\" name=\"user.RolesAsArray\" >" + Environment
.NewLine
+
229 "<option selected=\"selected\" value=\"role1\">role1</option>" + Environment
.NewLine
+ "<option selected=\"selected\" value=\"role2\">role2</option>" + Environment
.NewLine
+ "</select>",
230 helper
.Select("user.RolesAsArray", list
));
234 public void UsingDataTable()
237 "<select id=\"user_id\" name=\"user.id\" >" + Environment
.NewLine
+
238 "<option value=\"1\">chris rocks</option>" + Environment
.NewLine
+ "<option value=\"2\">will ferrell</option>" + Environment
.NewLine
+ "</select>",
239 helper
.Select("user.id", workTable
.Rows
, DictHelper
.Create("value=custid", "text=name", "sourceProperty=id")));
243 public void UsingEnums()
246 "<select id=\"user_registration\" name=\"user.registration\" >" + Environment
.NewLine
+
247 "<option value=\"1\">unregistered</option>" + Environment
.NewLine
+
248 "<option value=\"2\">pending</option>" + Environment
.NewLine
+
249 "<option selected=\"selected\" value=\"6\">registered</option>" + Environment
.NewLine
+
251 helper
.Select("user.registration", Enum
.GetValues(typeof(SimpleUser
.RegistrationEnum
))));
255 public void UsingEnumsAsText()
258 "<select id=\"user_registration\" name=\"user.registration\" >" + Environment
.NewLine
+
259 "<option value=\"unregistered\">unregistered</option>" + Environment
.NewLine
+
260 "<option value=\"pending\">pending</option>" + Environment
.NewLine
+
261 "<option selected=\"selected\" value=\"registered\">registered</option>" + Environment
.NewLine
+
263 helper
.Select("user.registration", Enum
.GetNames(typeof(SimpleUser
.RegistrationEnum
))));
268 public void UsingInterface()
270 List
<IInterfacedList
> list
= new List
<IInterfacedList
>();
272 list
.Add(new InterfacedClassA(1, "ernst"));
273 list
.Add(new InterfacedClassB(2, "enix"));
275 Assert
.AreEqual("<select id=\"user_name\" name=\"user.name\" >" + Environment
.NewLine
+
276 "<option value=\"1\">ernst</option>" + Environment
.NewLine
+ "<option value=\"2\">enix</option>" + Environment
.NewLine
+ "</select>",
277 helper
.Select("user.name", list
, DictHelper
.Create("value=id", "text=name")));
281 public void BasicFunctionalityInDotNet2()
283 List
<Month
> list
= new List
<Month
>();
284 list
.Add(new Month(1, "Jan"));
285 list
.Add(new Month(2, "Feb"));
287 Assert
.AreEqual("<select id=\"contact_dobmonth_id\" name=\"contact.dobmonth.id\" >" + Environment
.NewLine
+
288 "<option value=\"1\">Jan</option>" + Environment
.NewLine
+ "<option value=\"2\">Feb</option>" + Environment
.NewLine
+ "</select>",
289 helper
.Select("contact.dobmonth.id", list
, DictHelper
.Create("value=id", "text=name")));
293 public void SelectWithCompositKey()
295 ArrayList list
= new ArrayList();
296 list
.Add(new ClassWithCompositKey(1, "cat1"));
297 list
.Add(new ClassWithCompositKey(2, "cat2"));
299 Assert
.AreEqual("<select id=\"product_category_id\" name=\"product.category.id\" >" + Environment
.NewLine
+
300 "<option value=\"1\">cat1</option>" + Environment
.NewLine
+ "<option value=\"2\">cat2</option>" + Environment
.NewLine
+ "</select>",
301 helper
.Select("product.category.id", list
, DictHelper
.Create("value=Key.Id", "text=name")));