Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / MonoRail / Castle.MonoRail.Framework.Tests / Helpers / FormHelperSetOperation.cs
blob13ed1259cc5d3b0b0e598cba21ebe5b1dfc14944
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 namespace Castle.MonoRail.Framework.Tests.Helpers
17 using System.Globalization;
18 using System.IO;
19 using System.Threading;
20 using Castle.MonoRail.Framework.Helpers;
21 using Core;
22 using NUnit.Framework;
24 [TestFixture]
25 public class FormHelperSetOperation
27 [SetUp]
28 public void Init()
30 CultureInfo en = CultureInfo.CreateSpecificCulture("en");
32 Thread.CurrentThread.CurrentCulture = en;
33 Thread.CurrentThread.CurrentUICulture = en;
36 [Test]
37 public void EmptyInitialSet()
39 OperationState state = SetOperation.IterateOnDataSource(
40 null, new int[] {1, 2, 3, 4}, null);
42 Assert.IsNotNull(state);
43 Assert.IsTrue(state is ListDataSourceState);
44 Assert.IsNull(state.TargetSuffix);
46 bool iterated = false;
47 int index = 1;
49 foreach (SetItem item in state)
51 iterated = true;
53 Assert.IsFalse(item.IsSelected);
54 Assert.IsNotNull(item.Text);
55 Assert.IsNotNull(item.Value);
57 Assert.AreEqual(index.ToString(), item.Text);
58 Assert.AreEqual(index.ToString(), item.Value);
60 index++;
63 Assert.IsTrue(iterated);
66 [Test]
67 public void ApplyingFormat()
69 OperationState state = SetOperation.IterateOnDataSource(
70 null, new int[] {1, 2, 3, 4}, DictHelper.Create("textformat=C"));
72 Assert.IsNotNull(state);
73 Assert.IsTrue(state is ListDataSourceState);
74 Assert.IsNull(state.TargetSuffix);
76 bool iterated = false;
77 int index = 1;
79 foreach (SetItem item in state)
81 iterated = true;
83 Assert.IsFalse(item.IsSelected);
84 Assert.IsNotNull(item.Text);
85 Assert.IsNotNull(item.Value);
87 Assert.AreEqual(index.ToString("C"), item.Text);
88 Assert.AreEqual(index.ToString(), item.Value);
90 index++;
93 Assert.IsTrue(iterated);
97 [Test]
98 public void NullDataSource()
100 OperationState state = SetOperation.IterateOnDataSource(
101 new int[] {1, 2, 3, 4}, null, null);
103 Assert.IsNotNull(state);
104 Assert.IsTrue(state is NoIterationState);
105 Assert.IsNull(state.TargetSuffix);
107 Assert.IsFalse(state.MoveNext());
110 [Test]
111 public void EmptyDataSource()
113 OperationState state = SetOperation.IterateOnDataSource(
114 new int[] {1, 2, 3, 4}, new int[0], null);
116 Assert.IsNotNull(state);
117 Assert.IsTrue(state is NoIterationState);
118 Assert.IsNull(state.TargetSuffix);
120 Assert.IsFalse(state.MoveNext());
123 [Test]
124 public void SingleValueInitialSet_EnumPairs()
126 OperationState state = SetOperation.IterateOnDataSource(
127 FileMode.Append, new Pair<FileMode, string>[]
129 new Pair<FileMode, string>(FileMode.Append, "append"),
130 new Pair<FileMode, string>(FileMode.Create, "create"),
131 new Pair<FileMode, string>(FileMode.CreateNew, "new"),
132 new Pair<FileMode, string>(FileMode.Open, "open")
133 }, DictHelper.Create("value=First", "text=Second"));
135 Assert.IsNotNull(state);
136 Assert.IsTrue(state is DifferentTypeOperationState);
137 Assert.AreEqual("", state.TargetSuffix);
139 bool iterated = false;
140 int index = 1;
142 foreach(SetItem item in state)
144 iterated = true;
146 if (index == 1)
148 Assert.IsTrue(item.IsSelected);
150 else
152 Assert.IsFalse(item.IsSelected);
154 Assert.IsNotNull(item.Text);
155 Assert.IsNotNull(item.Value);
157 index++;
160 Assert.IsTrue(iterated);
163 [Test]
164 public void SingleValueInitialSet_SameTypes()
166 OperationState state = SetOperation.IterateOnDataSource(
167 1, new int[] {1, 2, 3, 4}, null);
169 Assert.IsNotNull(state);
170 Assert.IsTrue(state is SameTypeOperationState);
171 Assert.AreEqual("", state.TargetSuffix);
173 bool iterated = false;
174 int index = 1;
176 foreach (SetItem item in state)
178 iterated = true;
180 if (index == 1)
182 Assert.IsTrue(item.IsSelected);
184 else
186 Assert.IsFalse(item.IsSelected);
188 Assert.IsNotNull(item.Text);
189 Assert.IsNotNull(item.Value);
191 Assert.AreEqual(index.ToString(), item.Text);
192 Assert.AreEqual(index.ToString(), item.Value);
194 index++;
197 Assert.IsTrue(iterated);
200 [Test]
201 public void MultipleValuesInitialSet_SameTypes()
203 OperationState state = SetOperation.IterateOnDataSource(
204 new int[] {1, 2}, new int[] {1, 2, 3, 4}, null);
206 Assert.IsNotNull(state);
207 Assert.IsTrue(state is SameTypeOperationState);
208 Assert.AreEqual("", state.TargetSuffix);
210 bool iterated = false;
211 int index = 1;
213 foreach (SetItem item in state)
215 iterated = true;
217 if (index == 1 || index == 2)
219 Assert.IsTrue(item.IsSelected);
221 else
223 Assert.IsFalse(item.IsSelected);
225 Assert.IsNotNull(item.Text);
226 Assert.IsNotNull(item.Value);
228 Assert.AreEqual(index.ToString(), item.Text);
229 Assert.AreEqual(index.ToString(), item.Value);
231 index++;
234 Assert.IsTrue(iterated);
237 [Test]
238 public void SingleValueInitialSet_SameTypes_NonPrimitive()
240 OperationState state = SetOperation.IterateOnDataSource(
241 new Role(1, "Admin"), new Role[] {new Role(0, "User"), new Role(1, "Admin")},
242 DictHelper.Create("text=Name", "value=Id"));
244 Assert.IsNotNull(state);
245 Assert.IsTrue(state is SameTypeOperationState);
246 Assert.AreEqual("Id", state.TargetSuffix);
248 bool iterated = false;
249 int index = 1;
251 foreach (SetItem item in state)
253 iterated = true;
255 Assert.IsNotNull(item.Text);
256 Assert.IsNotNull(item.Value);
258 if (index == 1)
260 Assert.IsFalse(item.IsSelected);
261 Assert.AreEqual("User", item.Text);
262 Assert.AreEqual("0", item.Value);
264 else
266 Assert.IsTrue(item.IsSelected);
267 Assert.AreEqual("Admin", item.Text);
268 Assert.AreEqual("1", item.Value);
271 index++;
274 Assert.IsTrue(iterated);
277 [Test]
278 public void MultipleValuesInitialSet_SameTypes_NonPrimitive()
280 OperationState state = SetOperation.IterateOnDataSource(
281 new Role[] {new Role(0, "User"), new Role(1, "Admin")},
282 new Role[] {new Role(0, "User"), new Role(1, "Admin")},
283 DictHelper.Create("text=Name", "value=Id"));
285 Assert.IsNotNull(state);
286 Assert.IsTrue(state is SameTypeOperationState);
287 Assert.AreEqual("Id", state.TargetSuffix);
289 bool iterated = false;
290 int index = 1;
292 foreach (SetItem item in state)
294 iterated = true;
296 Assert.IsNotNull(item.Text);
297 Assert.IsNotNull(item.Value);
298 Assert.IsTrue(item.IsSelected);
300 if (index == 1)
302 Assert.AreEqual("User", item.Text);
303 Assert.AreEqual("0", item.Value);
305 else
307 Assert.AreEqual("Admin", item.Text);
308 Assert.AreEqual("1", item.Value);
311 index++;
314 Assert.IsTrue(iterated);
317 [Test]
318 public void MultipleValuesInitialSet_DifferentTypes_NonPrimitive()
320 OperationState state = SetOperation.IterateOnDataSource(
321 new ProductCategory[] {new ProductCategory(0, "User"), new ProductCategory(1, "Admin")},
322 new Role[] {new Role(0, "User"), new Role(1, "Admin")},
323 DictHelper.Create("text=Name", "value=Id"));
325 Assert.IsNotNull(state);
326 Assert.IsTrue(state is DifferentTypeOperationState);
327 Assert.AreEqual("Id", state.TargetSuffix);
329 bool iterated = false;
330 int index = 1;
332 foreach (SetItem item in state)
334 iterated = true;
336 Assert.IsNotNull(item.Text);
337 Assert.IsNotNull(item.Value);
338 Assert.IsTrue(item.IsSelected);
340 if (index == 1)
342 Assert.AreEqual("User", item.Text);
343 Assert.AreEqual("0", item.Value);
345 else
347 Assert.AreEqual("Admin", item.Text);
348 Assert.AreEqual("1", item.Value);
351 index++;
354 Assert.IsTrue(iterated);
357 [Test]
358 public void SingleValueInitialSet_DifferentTypes_NonPrimitive_NoNameMatching()
360 OperationState state = SetOperation.IterateOnDataSource(
361 new Role2(1, "Admin"),
362 new Role[] {new Role(0, "User"), new Role(1, "Admin")},
363 DictHelper.Create("text=Name", "value=Id", "sourceproperty=identification"));
365 Assert.IsNotNull(state);
366 Assert.IsTrue(state is DifferentTypeOperationState);
367 Assert.AreEqual("Identification", state.TargetSuffix);
369 bool iterated = false;
370 int index = 1;
372 foreach (SetItem item in state)
374 iterated = true;
376 Assert.IsNotNull(item.Text);
377 Assert.IsNotNull(item.Value);
379 if (index == 1)
381 Assert.IsFalse(item.IsSelected);
382 Assert.AreEqual("User", item.Text);
383 Assert.AreEqual("0", item.Value);
385 else
387 Assert.IsTrue(item.IsSelected);
388 Assert.AreEqual("Admin", item.Text);
389 Assert.AreEqual("1", item.Value);
392 index++;
395 Assert.IsTrue(iterated);
398 [Test]
399 public void MultipleValuesInitialSet_DifferentTypes_NonPrimitive_NoNameMatching()
401 OperationState state = SetOperation.IterateOnDataSource(
402 new Role2[] {new Role2(0, "User"), new Role2(1, "Admin")},
403 new Role[] {new Role(0, "User"), new Role(1, "Admin")},
404 DictHelper.Create("text=Name", "value=Id", "sourceproperty=identification"));
406 Assert.IsNotNull(state);
407 Assert.IsTrue(state is DifferentTypeOperationState);
408 Assert.AreEqual("Identification", state.TargetSuffix);
410 bool iterated = false;
411 int index = 1;
413 foreach (SetItem item in state)
415 iterated = true;
417 Assert.IsNotNull(item.Text);
418 Assert.IsNotNull(item.Value);
419 Assert.IsTrue(item.IsSelected);
421 if (index == 1)
423 Assert.AreEqual("User", item.Text);
424 Assert.AreEqual("0", item.Value);
426 else
428 Assert.AreEqual("Admin", item.Text);
429 Assert.AreEqual("1", item.Value);
432 index++;
435 Assert.IsTrue(iterated);
438 /// <summary>
439 /// The target is a decimal while the data source is an array of strings
440 /// </summary>
441 [Test]
442 public void SingleSelectionDifferentTypes()
444 CultureInfo en = CultureInfo.CreateSpecificCulture("pt-br");
446 Thread.CurrentThread.CurrentCulture = en;
447 Thread.CurrentThread.CurrentUICulture = en;
449 OperationState state = SetOperation.IterateOnDataSource(
450 (decimal?) 1.5,
451 new string[] {"1", "1,5", "2"}, null);
452 Assert.IsNotNull(state);
453 Assert.IsTrue(state is DifferentTypeOperationState);
455 bool iterated = false;
456 int index = 0;
458 foreach (SetItem item in state)
460 iterated = true;
462 Assert.IsNotNull(item.Text);
463 Assert.IsNotNull(item.Value);
465 if (index == 1)
467 Assert.IsTrue(item.IsSelected);
468 Assert.AreEqual("1,5", item.Text);
469 Assert.AreEqual("1,5", item.Value);
471 else
473 Assert.IsFalse(item.IsSelected);
476 index++;
479 Assert.IsTrue(iterated);
482 [Test]
483 public void MultipleValuesInitialSet_DifferentTypes_Primitive_NoNameMatching()
485 OperationState state = SetOperation.IterateOnDataSource(
486 new int[] {0, 1},
487 new Role[] {new Role(0, "User"), new Role(1, "Admin")},
488 DictHelper.Create("text=Name", "value=Id"));
490 Assert.IsNotNull(state);
491 Assert.IsTrue(state is DifferentTypeOperationState);
492 Assert.AreEqual("", state.TargetSuffix);
494 bool iterated = false;
495 int index = 1;
497 foreach (SetItem item in state)
499 iterated = true;
501 Assert.IsNotNull(item.Text);
502 Assert.IsNotNull(item.Value);
503 Assert.IsTrue(item.IsSelected);
505 if (index == 1)
507 Assert.AreEqual("User", item.Text);
508 Assert.AreEqual("0", item.Value);
510 else
512 Assert.AreEqual("Admin", item.Text);
513 Assert.AreEqual("1", item.Value);
516 index++;
519 Assert.IsTrue(iterated);