Minor changes to improve testability of helpers
[castle.git] / MonoRail / Castle.MonoRail.Framework.Tests / Helpers / FormHelperSetOperation.cs
blobb374b7622a1d101eb624c679393d1bb05c5ce5c7
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.Globalization;
18 using System.Threading;
20 using Castle.MonoRail.Framework.Helpers;
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_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;
134 int index = 1;
136 foreach(SetItem item in state)
138 iterated = true;
140 if (index == 1)
142 Assert.IsTrue(item.IsSelected);
144 else
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);
154 index++;
157 Assert.IsTrue(iterated);
160 [Test]
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;
171 int index = 1;
173 foreach(SetItem item in state)
175 iterated = true;
177 if (index == 1 || index == 2)
179 Assert.IsTrue(item.IsSelected);
181 else
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);
191 index++;
194 Assert.IsTrue(iterated);
197 [Test]
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;
209 int index = 1;
211 foreach(SetItem item in state)
213 iterated = true;
215 Assert.IsNotNull(item.Text);
216 Assert.IsNotNull(item.Value);
218 if (index == 1)
220 Assert.IsFalse(item.IsSelected);
221 Assert.AreEqual("User", item.Text);
222 Assert.AreEqual("0", item.Value);
224 else
226 Assert.IsTrue(item.IsSelected);
227 Assert.AreEqual("Admin", item.Text);
228 Assert.AreEqual("1", item.Value);
231 index++;
234 Assert.IsTrue(iterated);
237 [Test]
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;
250 int index = 1;
252 foreach(SetItem item in state)
254 iterated = true;
256 Assert.IsNotNull(item.Text);
257 Assert.IsNotNull(item.Value);
258 Assert.IsTrue(item.IsSelected);
260 if (index == 1)
262 Assert.AreEqual("User", item.Text);
263 Assert.AreEqual("0", item.Value);
265 else
267 Assert.AreEqual("Admin", item.Text);
268 Assert.AreEqual("1", item.Value);
271 index++;
274 Assert.IsTrue(iterated);
277 [Test]
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;
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 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;
330 int index = 1;
332 foreach(SetItem item in state)
334 iterated = true;
336 Assert.IsNotNull(item.Text);
337 Assert.IsNotNull(item.Value);
339 if (index == 1)
341 Assert.IsFalse(item.IsSelected);
342 Assert.AreEqual("User", item.Text);
343 Assert.AreEqual("0", item.Value);
345 else
347 Assert.IsTrue(item.IsSelected);
348 Assert.AreEqual("Admin", item.Text);
349 Assert.AreEqual("1", item.Value);
352 index++;
355 Assert.IsTrue(iterated);
358 [Test]
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;
371 int index = 1;
373 foreach(SetItem item in state)
375 iterated = true;
377 Assert.IsNotNull(item.Text);
378 Assert.IsNotNull(item.Value);
379 Assert.IsTrue(item.IsSelected);
381 if (index == 1)
383 Assert.AreEqual("User", item.Text);
384 Assert.AreEqual("0", item.Value);
386 else
388 Assert.AreEqual("Admin", item.Text);
389 Assert.AreEqual("1", item.Value);
392 index++;
395 Assert.IsTrue(iterated);
398 /// <summary>
399 /// The target is a decimal while the data source is an array of strings
400 /// </summary>
401 [Test]
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(
410 (decimal?) 1.5,
411 new string[] { "1", "1,5", "2" }, null);
412 Assert.IsNotNull(state);
413 Assert.IsTrue(state is DifferentTypeOperationState);
415 bool iterated = false;
416 int index = 0;
418 foreach(SetItem item in state)
420 iterated = true;
422 Assert.IsNotNull(item.Text);
423 Assert.IsNotNull(item.Value);
425 if (index == 1)
427 Assert.IsTrue(item.IsSelected);
428 Assert.AreEqual("1,5", item.Text);
429 Assert.AreEqual("1,5", item.Value);
431 else
433 Assert.IsFalse(item.IsSelected);
436 index++;
439 Assert.IsTrue(iterated);
442 [Test]
443 public void MultipleValuesInitialSet_DifferentTypes_Primitive_NoNameMatching()
445 OperationState state = SetOperation.IterateOnDataSource(
446 new int[] {0,1},
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;
455 int index = 1;
457 foreach(SetItem item in state)
459 iterated = true;
461 Assert.IsNotNull(item.Text);
462 Assert.IsNotNull(item.Value);
463 Assert.IsTrue(item.IsSelected);
465 if (index == 1)
467 Assert.AreEqual("User", item.Text);
468 Assert.AreEqual("0", item.Value);
470 else
472 Assert.AreEqual("Admin", item.Text);
473 Assert.AreEqual("1", item.Value);
476 index++;
479 Assert.IsTrue(iterated);