1 // Copyright 2004-2007 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
;
18 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_SameTypes()
126 OperationState state
= SetOperation
.IterateOnDataSource(
127 1, new int[] {1, 2, 3, 4}
, null);
129 Assert
.IsNotNull(state
);
130 Assert
.IsTrue(state
is SameTypeOperationState
);
131 Assert
.AreEqual("", state
.TargetSuffix
);
133 bool iterated
= false;
136 foreach(SetItem item
in state
)
142 Assert
.IsTrue(item
.IsSelected
);
146 Assert
.IsFalse(item
.IsSelected
);
148 Assert
.IsNotNull(item
.Text
);
149 Assert
.IsNotNull(item
.Value
);
151 Assert
.AreEqual(index
.ToString(), item
.Text
);
152 Assert
.AreEqual(index
.ToString(), item
.Value
);
157 Assert
.IsTrue(iterated
);
161 public void MultipleValuesInitialSet_SameTypes()
163 OperationState state
= SetOperation
.IterateOnDataSource(
164 new int[] {1,2}
, new int[] {1, 2, 3, 4}
, null);
166 Assert
.IsNotNull(state
);
167 Assert
.IsTrue(state
is SameTypeOperationState
);
168 Assert
.AreEqual("", state
.TargetSuffix
);
170 bool iterated
= false;
173 foreach(SetItem item
in state
)
177 if (index
== 1 || index
== 2)
179 Assert
.IsTrue(item
.IsSelected
);
183 Assert
.IsFalse(item
.IsSelected
);
185 Assert
.IsNotNull(item
.Text
);
186 Assert
.IsNotNull(item
.Value
);
188 Assert
.AreEqual(index
.ToString(), item
.Text
);
189 Assert
.AreEqual(index
.ToString(), item
.Value
);
194 Assert
.IsTrue(iterated
);
198 public void SingleValueInitialSet_SameTypes_NonPrimitive()
200 OperationState state
= SetOperation
.IterateOnDataSource(
201 new Role(1, "Admin"), new Role
[] {new Role(0,"User"), new Role(1,"Admin")}
,
202 DictHelper
.Create("text=Name", "value=Id"));
204 Assert
.IsNotNull(state
);
205 Assert
.IsTrue(state
is SameTypeOperationState
);
206 Assert
.AreEqual("Id", state
.TargetSuffix
);
208 bool iterated
= false;
211 foreach(SetItem item
in state
)
215 Assert
.IsNotNull(item
.Text
);
216 Assert
.IsNotNull(item
.Value
);
220 Assert
.IsFalse(item
.IsSelected
);
221 Assert
.AreEqual("User", item
.Text
);
222 Assert
.AreEqual("0", item
.Value
);
226 Assert
.IsTrue(item
.IsSelected
);
227 Assert
.AreEqual("Admin", item
.Text
);
228 Assert
.AreEqual("1", item
.Value
);
234 Assert
.IsTrue(iterated
);
238 public void MultipleValuesInitialSet_SameTypes_NonPrimitive()
240 OperationState state
= SetOperation
.IterateOnDataSource(
241 new Role
[] {new Role(0,"User"), new Role(1,"Admin")}
,
242 new Role
[] {new Role(0,"User"), new Role(1,"Admin")}
,
243 DictHelper
.Create("text=Name", "value=Id"));
245 Assert
.IsNotNull(state
);
246 Assert
.IsTrue(state
is SameTypeOperationState
);
247 Assert
.AreEqual("Id", state
.TargetSuffix
);
249 bool iterated
= false;
252 foreach(SetItem item
in state
)
256 Assert
.IsNotNull(item
.Text
);
257 Assert
.IsNotNull(item
.Value
);
258 Assert
.IsTrue(item
.IsSelected
);
262 Assert
.AreEqual("User", item
.Text
);
263 Assert
.AreEqual("0", item
.Value
);
267 Assert
.AreEqual("Admin", item
.Text
);
268 Assert
.AreEqual("1", item
.Value
);
274 Assert
.IsTrue(iterated
);
278 public void MultipleValuesInitialSet_DifferentTypes_NonPrimitive()
280 OperationState state
= SetOperation
.IterateOnDataSource(
281 new ProductCategory
[] {new ProductCategory(0,"User"), new ProductCategory(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 DifferentTypeOperationState
);
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 SingleValueInitialSet_DifferentTypes_NonPrimitive_NoNameMatching()
320 OperationState state
= SetOperation
.IterateOnDataSource(
321 new Role2(1,"Admin"),
322 new Role
[] {new Role(0,"User"), new Role(1,"Admin")}
,
323 DictHelper
.Create("text=Name", "value=Id", "sourceproperty=identification"));
325 Assert
.IsNotNull(state
);
326 Assert
.IsTrue(state
is DifferentTypeOperationState
);
327 Assert
.AreEqual("Identification", state
.TargetSuffix
);
329 bool iterated
= false;
332 foreach(SetItem item
in state
)
336 Assert
.IsNotNull(item
.Text
);
337 Assert
.IsNotNull(item
.Value
);
341 Assert
.IsFalse(item
.IsSelected
);
342 Assert
.AreEqual("User", item
.Text
);
343 Assert
.AreEqual("0", item
.Value
);
347 Assert
.IsTrue(item
.IsSelected
);
348 Assert
.AreEqual("Admin", item
.Text
);
349 Assert
.AreEqual("1", item
.Value
);
355 Assert
.IsTrue(iterated
);
359 public void MultipleValuesInitialSet_DifferentTypes_NonPrimitive_NoNameMatching()
361 OperationState state
= SetOperation
.IterateOnDataSource(
362 new Role2
[] {new Role2(0,"User"), new Role2(1,"Admin")}
,
363 new Role
[] {new Role(0,"User"), new Role(1,"Admin")}
,
364 DictHelper
.Create("text=Name", "value=Id", "sourceproperty=identification"));
366 Assert
.IsNotNull(state
);
367 Assert
.IsTrue(state
is DifferentTypeOperationState
);
368 Assert
.AreEqual("Identification", state
.TargetSuffix
);
370 bool iterated
= false;
373 foreach(SetItem item
in state
)
377 Assert
.IsNotNull(item
.Text
);
378 Assert
.IsNotNull(item
.Value
);
379 Assert
.IsTrue(item
.IsSelected
);
383 Assert
.AreEqual("User", item
.Text
);
384 Assert
.AreEqual("0", item
.Value
);
388 Assert
.AreEqual("Admin", item
.Text
);
389 Assert
.AreEqual("1", item
.Value
);
395 Assert
.IsTrue(iterated
);
399 /// The target is a decimal while the data source is an array of strings
402 public void SingleSelectionDifferentTypes()
404 CultureInfo en
= CultureInfo
.CreateSpecificCulture("pt-br");
406 Thread
.CurrentThread
.CurrentCulture
= en
;
407 Thread
.CurrentThread
.CurrentUICulture
= en
;
409 OperationState state
= SetOperation
.IterateOnDataSource(
411 new string[] { "1", "1,5", "2" }
, null);
412 Assert
.IsNotNull(state
);
413 Assert
.IsTrue(state
is DifferentTypeOperationState
);
415 bool iterated
= false;
418 foreach(SetItem item
in state
)
422 Assert
.IsNotNull(item
.Text
);
423 Assert
.IsNotNull(item
.Value
);
427 Assert
.IsTrue(item
.IsSelected
);
428 Assert
.AreEqual("1,5", item
.Text
);
429 Assert
.AreEqual("1,5", item
.Value
);
433 Assert
.IsFalse(item
.IsSelected
);
439 Assert
.IsTrue(iterated
);
443 public void MultipleValuesInitialSet_DifferentTypes_Primitive_NoNameMatching()
445 OperationState state
= SetOperation
.IterateOnDataSource(
447 new Role
[] {new Role(0,"User"), new Role(1,"Admin")}
,
448 DictHelper
.Create("text=Name", "value=Id"));
450 Assert
.IsNotNull(state
);
451 Assert
.IsTrue(state
is DifferentTypeOperationState
);
452 Assert
.AreEqual("", state
.TargetSuffix
);
454 bool iterated
= false;
457 foreach(SetItem item
in state
)
461 Assert
.IsNotNull(item
.Text
);
462 Assert
.IsNotNull(item
.Value
);
463 Assert
.IsTrue(item
.IsSelected
);
467 Assert
.AreEqual("User", item
.Text
);
468 Assert
.AreEqual("0", item
.Value
);
472 Assert
.AreEqual("Admin", item
.Text
);
473 Assert
.AreEqual("1", item
.Value
);
479 Assert
.IsTrue(iterated
);