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
17 using System
.Globalization
;
19 using System
.Threading
;
20 using Castle
.MonoRail
.Framework
.Helpers
;
22 using NUnit
.Framework
;
25 public class FormHelperSetOperation
30 CultureInfo en
= CultureInfo
.CreateSpecificCulture("en");
32 Thread
.CurrentThread
.CurrentCulture
= en
;
33 Thread
.CurrentThread
.CurrentUICulture
= en
;
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;
49 foreach (SetItem item
in state
)
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
);
63 Assert
.IsTrue(iterated
);
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;
79 foreach (SetItem item
in state
)
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
);
93 Assert
.IsTrue(iterated
);
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());
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());
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;
142 foreach(SetItem item
in state
)
148 Assert
.IsTrue(item
.IsSelected
);
152 Assert
.IsFalse(item
.IsSelected
);
154 Assert
.IsNotNull(item
.Text
);
155 Assert
.IsNotNull(item
.Value
);
160 Assert
.IsTrue(iterated
);
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;
176 foreach (SetItem item
in state
)
182 Assert
.IsTrue(item
.IsSelected
);
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
);
197 Assert
.IsTrue(iterated
);
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;
213 foreach (SetItem item
in state
)
217 if (index
== 1 || index
== 2)
219 Assert
.IsTrue(item
.IsSelected
);
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
);
234 Assert
.IsTrue(iterated
);
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;
251 foreach (SetItem item
in state
)
255 Assert
.IsNotNull(item
.Text
);
256 Assert
.IsNotNull(item
.Value
);
260 Assert
.IsFalse(item
.IsSelected
);
261 Assert
.AreEqual("User", item
.Text
);
262 Assert
.AreEqual("0", item
.Value
);
266 Assert
.IsTrue(item
.IsSelected
);
267 Assert
.AreEqual("Admin", item
.Text
);
268 Assert
.AreEqual("1", item
.Value
);
274 Assert
.IsTrue(iterated
);
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;
292 foreach (SetItem item
in state
)
296 Assert
.IsNotNull(item
.Text
);
297 Assert
.IsNotNull(item
.Value
);
298 Assert
.IsTrue(item
.IsSelected
);
302 Assert
.AreEqual("User", item
.Text
);
303 Assert
.AreEqual("0", item
.Value
);
307 Assert
.AreEqual("Admin", item
.Text
);
308 Assert
.AreEqual("1", item
.Value
);
314 Assert
.IsTrue(iterated
);
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;
332 foreach (SetItem item
in state
)
336 Assert
.IsNotNull(item
.Text
);
337 Assert
.IsNotNull(item
.Value
);
338 Assert
.IsTrue(item
.IsSelected
);
342 Assert
.AreEqual("User", item
.Text
);
343 Assert
.AreEqual("0", item
.Value
);
347 Assert
.AreEqual("Admin", item
.Text
);
348 Assert
.AreEqual("1", item
.Value
);
354 Assert
.IsTrue(iterated
);
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;
372 foreach (SetItem item
in state
)
376 Assert
.IsNotNull(item
.Text
);
377 Assert
.IsNotNull(item
.Value
);
381 Assert
.IsFalse(item
.IsSelected
);
382 Assert
.AreEqual("User", item
.Text
);
383 Assert
.AreEqual("0", item
.Value
);
387 Assert
.IsTrue(item
.IsSelected
);
388 Assert
.AreEqual("Admin", item
.Text
);
389 Assert
.AreEqual("1", item
.Value
);
395 Assert
.IsTrue(iterated
);
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;
413 foreach (SetItem item
in state
)
417 Assert
.IsNotNull(item
.Text
);
418 Assert
.IsNotNull(item
.Value
);
419 Assert
.IsTrue(item
.IsSelected
);
423 Assert
.AreEqual("User", item
.Text
);
424 Assert
.AreEqual("0", item
.Value
);
428 Assert
.AreEqual("Admin", item
.Text
);
429 Assert
.AreEqual("1", item
.Value
);
435 Assert
.IsTrue(iterated
);
439 /// The target is a decimal while the data source is an array of strings
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(
451 new string[] {"1", "1,5", "2"}
, null);
452 Assert
.IsNotNull(state
);
453 Assert
.IsTrue(state
is DifferentTypeOperationState
);
455 bool iterated
= false;
458 foreach (SetItem item
in state
)
462 Assert
.IsNotNull(item
.Text
);
463 Assert
.IsNotNull(item
.Value
);
467 Assert
.IsTrue(item
.IsSelected
);
468 Assert
.AreEqual("1,5", item
.Text
);
469 Assert
.AreEqual("1,5", item
.Value
);
473 Assert
.IsFalse(item
.IsSelected
);
479 Assert
.IsTrue(iterated
);
483 public void MultipleValuesInitialSet_DifferentTypes_Primitive_NoNameMatching()
485 OperationState state
= SetOperation
.IterateOnDataSource(
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;
497 foreach (SetItem item
in state
)
501 Assert
.IsNotNull(item
.Text
);
502 Assert
.IsNotNull(item
.Value
);
503 Assert
.IsTrue(item
.IsSelected
);
507 Assert
.AreEqual("User", item
.Text
);
508 Assert
.AreEqual("0", item
.Value
);
512 Assert
.AreEqual("Admin", item
.Text
);
513 Assert
.AreEqual("1", item
.Value
);
519 Assert
.IsTrue(iterated
);