comparison UnitTests/CS/EditableObjects/EditableArrayListTest.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.ComponentModel;
3 using System.IO;
4 using System.Runtime.Serialization.Formatters.Binary;
5 using System.Windows.Forms;
6
7 using NUnit.Framework;
8
9 using BLToolkit.EditableObjects;
10 using BLToolkit.Reflection;
11
12 namespace EditableObjects
13 {
14 [Serializable]
15 public abstract class SerializableObject : EditableObject
16 {
17 public abstract int ID { get; set; }
18 public abstract Guid UUID { get; set; }
19 public abstract string Name { get; set; }
20
21 public abstract EditableList<string> Array { get; set; }
22 }
23
24 [TestFixture]
25 public class EditableArrayListTest
26 {
27 public abstract class EditableTestObject : EditableObject
28 {
29 public abstract int ID { get; set; }
30 public abstract string Name { get; set; }
31 public abstract int Seconds { get; set; }
32
33 public static EditableTestObject CreateInstance()
34 {
35 return (EditableTestObject)TypeAccessor.CreateInstance(typeof(EditableTestObject));
36 }
37
38 public static EditableTestObject CreateInstance(int id, string name, int seconds)
39 {
40 var instance = CreateInstance();
41
42 instance.ID = id;
43 instance.Name = name;
44 instance.Seconds = seconds;
45
46 return instance;
47 }
48
49 public override string ToString()
50 {
51 return string.Format("EditableTestObject - ID:({0}) Name: ({1}) Seconds({2})", ID, Name, Seconds);
52 }
53 }
54
55 private static readonly EditableTestObject[] _testObjects = new EditableTestObject[]
56 {
57 EditableTestObject.CreateInstance(0, "Smith", 24),
58 EditableTestObject.CreateInstance(1, "John", 22),
59 EditableTestObject.CreateInstance(2, "Anna", 48),
60 EditableTestObject.CreateInstance(3, "Tim", 56),
61 EditableTestObject.CreateInstance(4, "Xiniu", 39),
62 EditableTestObject.CreateInstance(5, "Kirill", 30)
63 };
64
65 public EditableArrayListTest()
66 {
67 _testList = new EditableArrayList(typeof(EditableTestObject));
68 _testList.ListChanged += TestList_ListChanged;
69 }
70
71 private readonly EditableArrayList _testList;
72
73 private void TestList_ListChanged(object sender, ListChangedEventArgs e)
74 {
75 var array = sender as EditableArrayList;
76
77 Assert.IsNotNull(array);
78 if (e.ListChangedType != ListChangedType.Reset)
79 Assert.That(array.IsDirty);
80
81 if(e.ListChangedType == ListChangedType.ItemAdded)
82 {
83 var o = array[e.NewIndex];
84
85 Assert.IsNotNull(o);
86 Assert.That(array.NewItems.Contains(o));
87 }
88
89 if (e.ListChangedType != ListChangedType.ItemDeleted && e.ListChangedType != ListChangedType.Reset)
90 Console.WriteLine("ListChanged (ID:{3}). Type: {0}, OldIndex: {1}, NewIndex: {2}", e.ListChangedType, e.OldIndex, e.NewIndex, (e.NewIndex >= 0 && e.NewIndex < _testList.Count) ? ((EditableTestObject)_testList[e.NewIndex]).ID : -1);
91 else
92 Console.WriteLine("ListChanged (ID:???). Type: {0}, OldIndex: {1}, NewIndex: {2}", e.ListChangedType, e.OldIndex, e.NewIndex);
93 }
94
95 [Test]
96 public void TestAdd()
97 {
98 Console.WriteLine("--- TestAdd ---");
99 _testList.Clear();
100 _testList.RemoveSort();
101
102 for (var i = 0; i < 3; i++)
103 _testList.Add(_testObjects[i]);
104
105 Assert.AreEqual(_testList.Count, 3);
106 Assert.AreEqual(_testList[0], _testObjects[0]);
107 Assert.AreEqual(_testList[1], _testObjects[1]);
108 Assert.AreEqual(_testList[2], _testObjects[2]);
109
110 var _subArray = new EditableTestObject[3];
111
112 for (var i = 3; i < _testObjects.Length; i++)
113 _subArray[i-3] = _testObjects[i];
114
115 _testList.AddRange(_subArray);
116 Assert.AreEqual(_testList.Count, 6);
117
118 for (var i = 3; i < _testObjects.Length; i++)
119 Assert.AreEqual(_subArray[i - 3], _testObjects[i]);
120
121 PrintList();
122
123 _testList.Clear();
124 }
125
126 [Test]
127 public void TestAddNew()
128 {
129 var list = new EditableList<EditableTestObject>();
130 var listChangedFired = false;
131
132 list.ListChanged += (sender, args) => listChangedFired = true;
133
134 list.AddNew();
135 Assert.That(listChangedFired);
136 Assert.AreEqual(1, list.Count);
137 Assert.AreEqual(1, list.NewItems.Count);
138
139 listChangedFired = false;
140 list.CancelNew(0);
141 Assert.That(listChangedFired);
142 Assert.IsEmpty(list);
143 Assert.IsEmpty(list.NewItems);
144 }
145
146 [Test]
147 public void TestInsert()
148 {
149 Console.WriteLine("--- TestInsert ---");
150 _testList.Clear();
151 _testList.RemoveSort();
152
153 for (var i = 0; i < 3; i++)
154 _testList.Insert(0, _testObjects[i]);
155
156 Assert.AreEqual(_testList.Count, 3);
157 Assert.AreEqual(_testList[0], _testObjects[2]);
158 Assert.AreEqual(_testList[1], _testObjects[1]);
159 Assert.AreEqual(_testList[2], _testObjects[0]);
160
161 var _subArray = new EditableTestObject[3];
162
163 for (var i = 3; i < _testObjects.Length; i++)
164 _subArray[i-3] = _testObjects[i];
165
166 _testList.InsertRange(0, _subArray);
167 Assert.AreEqual(_testList.Count, 6);
168
169 Assert.AreEqual(_testList[0], _testObjects[3]);
170 Assert.AreEqual(_testList[1], _testObjects[4]);
171 Assert.AreEqual(_testList[2], _testObjects[5]);
172
173 PrintList();
174
175 _testList.Clear();
176 }
177
178 [Test]
179 public void TestRemove()
180 {
181 Console.WriteLine("--- TestRemove ---");
182
183 _testList.Clear();
184 _testList.RemoveSort();
185 _testList.AddRange(_testObjects);
186
187 _testList.RemoveAt(2);
188 Assert.AreEqual(_testList.Count, 5);
189 Assert.IsEmpty(_testList.DelItems);
190
191 _testList.AcceptChanges();
192 Assert.IsEmpty(_testList.NewItems);
193
194 _testList.Remove(_testList[0]);
195 Assert.AreEqual(_testList.Count, 4);
196 Assert.IsNotEmpty(_testList.DelItems);
197
198 _testList.Clear();
199 Assert.AreEqual(_testList.Count, 0);
200 }
201
202 [Test]
203 public void TestRemoveRange()
204 {
205 Console.WriteLine("--- TestRemoveRange ---");
206
207 _testList.Clear();
208 _testList.RemoveSort();
209 _testList.AcceptChanges();
210 _testList.AddRange(_testObjects);
211
212 Assert.AreEqual(6, _testList.Count);
213 Assert.AreEqual(6, _testList.NewItems.Count);
214 Assert.AreEqual(0, _testList.DelItems.Count);
215
216 _testList.RemoveRange(1, 3);
217
218 Assert.AreEqual(3, _testList.Count);
219 Assert.AreEqual(3, _testList.NewItems.Count);
220 Assert.AreEqual(0, _testList.DelItems.Count);
221 Assert.AreEqual(0, ((EditableTestObject)_testList[0]).ID);
222 Assert.AreEqual(4, ((EditableTestObject)_testList[1]).ID);
223
224 _testList.AcceptChanges();
225
226 Assert.AreEqual(3, _testList.Count);
227 Assert.AreEqual(0, _testList.NewItems.Count);
228 Assert.AreEqual(0, _testList.DelItems.Count);
229
230 _testList.RemoveRange(0, 1);
231
232 Assert.AreEqual(2, _testList.Count);
233 Assert.AreEqual(0, _testList.NewItems.Count);
234 Assert.AreEqual(1, _testList.DelItems.Count);
235
236 _testList.Clear();
237
238 Assert.AreEqual(0, _testList.Count);
239 Assert.AreEqual(0, _testList.NewItems.Count);
240 Assert.AreEqual(3, _testList.DelItems.Count);
241
242 _testList.AcceptChanges();
243
244 Assert.AreEqual(0, _testList.Count);
245 Assert.AreEqual(0, _testList.NewItems.Count);
246 Assert.AreEqual(0, _testList.DelItems.Count);
247 }
248
249 [Test]
250 public void TestSortedAdd()
251 {
252 Console.WriteLine("--- TestSortedAdd ---");
253 _testList.Clear();
254 _testList.RemoveSort();
255 _testList.ApplySort(_testList.GetItemProperties(null)["Seconds"], ListSortDirection.Descending);
256
257 for (var i = 0; i < 3; i++)
258 _testList.Add(_testObjects[i]);
259
260 Assert.AreEqual(_testList.Count, 3);
261 Assert.AreEqual(_testList[1], _testObjects[0]);
262 Assert.AreEqual(_testList[2], _testObjects[1]);
263 Assert.AreEqual(_testList[0], _testObjects[2]);
264
265 var _subArray = new EditableTestObject[3];
266
267 for (var i = 3; i < _testObjects.Length; i++)
268 _subArray[i-3] = _testObjects[i];
269
270 _testList.AddRange(_subArray);
271 Assert.AreEqual(_testList.Count, 6);
272
273 Assert.AreEqual(_testList[0], _testObjects[3]);
274 Assert.AreEqual(_testList[2], _testObjects[4]);
275 Assert.AreEqual(_testList[3], _testObjects[5]);
276
277 PrintList();
278
279 _testList.Clear();
280 Assert.AreEqual(_testList.Count, 0);
281
282 _testList.ApplySort(_testList.GetItemProperties(null)["Seconds"], ListSortDirection.Ascending);
283
284 for (var i = 0; i < 3; i++)
285 _testList.Add(_testObjects[i]);
286
287 Assert.AreEqual(_testList.Count, 3);
288 Assert.AreEqual(_testList[1], _testObjects[0]);
289 Assert.AreEqual(_testList[0], _testObjects[1]);
290 Assert.AreEqual(_testList[2], _testObjects[2]);
291
292 _testList.AddRange(_subArray);
293 Assert.AreEqual(_testList.Count, 6);
294
295 Assert.AreEqual(_testList[5], _testObjects[3]);
296 Assert.AreEqual(_testList[3], _testObjects[4]);
297 Assert.AreEqual(_testList[2], _testObjects[5]);
298
299 PrintList();
300
301 _testList.Clear();
302 }
303
304 [Test]
305 public void TestSortedInsert()
306 {
307 Console.WriteLine("--- TestSortedInsert ---");
308 _testList.Clear();
309 _testList.RemoveSort();
310 _testList.ApplySort(_testList.GetItemProperties(null)["Seconds"], ListSortDirection.Descending);
311
312 for (var i = 0; i < 3; i++)
313 _testList.Insert(0, _testObjects[i]);
314
315 Assert.AreEqual(_testList.Count, 3);
316 Assert.AreEqual(_testList[0], _testObjects[2]);
317 Assert.AreEqual(_testList[1], _testObjects[0]);
318 Assert.AreEqual(_testList[2], _testObjects[1]);
319
320 var _subArray = new EditableTestObject[3];
321
322 for (var i = 3; i < _testObjects.Length; i++)
323 _subArray[i-3] = _testObjects[i];
324
325 _testList.InsertRange(0, _subArray);
326 Assert.AreEqual(_testList.Count, 6);
327
328 Assert.AreEqual(_testList[0], _testObjects[3]);
329 Assert.AreEqual(_testList[2], _testObjects[4]);
330 Assert.AreEqual(_testList[3], _testObjects[5]);
331
332 PrintList();
333
334 _testList.Clear();
335 Assert.AreEqual(_testList.Count, 0);
336
337 _testList.ApplySort(_testList.GetItemProperties(null)["Seconds"], ListSortDirection.Ascending);
338
339 for (var i = 0; i < 3; i++)
340 _testList.Insert(0, _testObjects[i]);
341
342 Assert.AreEqual(_testList.Count, 3);
343 Assert.AreEqual(_testList[0], _testObjects[1]);
344 Assert.AreEqual(_testList[1], _testObjects[0]);
345 Assert.AreEqual(_testList[2], _testObjects[2]);
346
347 _testList.InsertRange(0, _subArray);
348 Assert.AreEqual(_testList.Count, 6);
349
350 Assert.AreEqual(_testList[2], _testObjects[5]);
351 Assert.AreEqual(_testList[3], _testObjects[4]);
352 Assert.AreEqual(_testList[5], _testObjects[3]);
353
354 PrintList();
355
356 _testList.Clear();
357 }
358
359 [Test]
360 public void TestSortedPropertyChange()
361 {
362 Console.WriteLine("--- TestSortedPropertyChange ---");
363 _testList.Clear();
364 _testList.RemoveSort();
365 _testList.ApplySort(_testList.GetItemProperties(null)["Seconds"], ListSortDirection.Descending);
366 _testList.AddRange(_testObjects);
367
368 var eto = EditableTestObject.CreateInstance(6, "Dummy", 10);
369
370 _testList.Add(eto);
371 Assert.AreEqual(_testList.Count, 7);
372 Assert.AreEqual(_testList[6], eto);
373
374 eto.Seconds = 20;
375 Assert.AreEqual(_testList[6], eto);
376
377 eto.Seconds = 23;
378 Assert.AreEqual(_testList[5], eto);
379
380 eto.Seconds = 30;
381 Assert.AreEqual(_testList[4], eto);
382
383 eto.Seconds = 40;
384 Assert.AreEqual(_testList[2], eto);
385
386 eto.Seconds = 50;
387 Assert.AreEqual(_testList[1], eto);
388
389 eto.Seconds = 60;
390 Assert.AreEqual(_testList[0], eto);
391 }
392
393 private void PrintList()
394 {
395 Console.WriteLine("--- Print List ---");
396 foreach (EditableTestObject o in _testList)
397 Console.WriteLine(o);
398 }
399
400 [Test]
401 public void SerializationTest()
402 {
403 //Configuration.NotifyOnEqualSet = true;
404
405 var test = TypeAccessor<SerializableObject>.CreateInstance();
406 var stream = new MemoryStream();
407 var bf = new BinaryFormatter();
408
409 bf.Serialize(stream, test);
410
411 //Configuration.NotifyOnChangesOnly = false;
412 }
413
414 //////[Test] Resharpe 8 issue
415 public void SerializationTest2()
416 {
417 //Configuration.NotifyOnChangesOnly = true;
418
419 var list = new EditableList<SerializableObject> { TypeAccessor<SerializableObject>.CreateInstance() };
420
421 var formatter = new BinaryFormatter();
422
423 using (var stream = new MemoryStream())
424 {
425 formatter.Serialize(stream, list);
426 stream.Position = 0;
427
428 var result = formatter.Deserialize(stream);
429
430 Assert.IsNotNull(result);
431
432 var eal = (EditableList<SerializableObject>)result;
433
434 Console.WriteLine(eal.Count);
435
436 eal.ListChanged += eal_ListChanged;
437
438 eal[0].ID = 0;
439 _notificationCount = 0;
440 eal[0].ID = -100;
441 eal[0].ID = -100;
442 eal[0].ID = -100;
443 eal[0].ID = -100;
444 eal[0].ID = -100;
445 eal[0].ID = -100;
446
447 Console.WriteLine(eal.Count);
448
449 //Assert.AreEqual(_notificationCount, 1);
450 }
451
452 //Configuration.NotifyOnChangesOnly = false;
453 }
454
455 private static int _notificationCount;
456
457 static void eal_ListChanged(object sender, ListChangedEventArgs e)
458 {
459 Console.WriteLine(e.ListChangedType);
460 _notificationCount++;
461 }
462
463 [Test]
464 public void SortTest()
465 {
466 var dataList = new EditableList<EditableTestObject>
467 {
468 EditableTestObject.CreateInstance(1, "John", 60),
469 EditableTestObject.CreateInstance(1, "John", 60),
470 EditableTestObject.CreateInstance(1, "John", 60),
471 EditableTestObject.CreateInstance(2, "Tester", 70),
472 EditableTestObject.CreateInstance(2, "Tester", 70),
473 EditableTestObject.CreateInstance(2, "Tester", 70),
474 EditableTestObject.CreateInstance(3, "Tester", 70),
475 EditableTestObject.CreateInstance(3, "Tester", 70),
476 EditableTestObject.CreateInstance(3, "Tester", 70)
477 };
478
479 var bindingSource = new BindingSource(dataList, null) { Sort = "ID" };
480 var prev = 0;
481
482 foreach (var o in dataList)
483 {
484 Assert.IsTrue(o.ID >= prev);
485 prev = o.ID;
486 }
487
488 bindingSource[0] = EditableTestObject.CreateInstance(2, "John", 60);
489
490 prev = 0;
491
492 foreach (var o in dataList)
493 {
494 Assert.IsTrue(o.ID >= prev);
495 prev = o.ID;
496 }
497 }
498
499 public class DerivedEditableList<T> : EditableList<T>
500 {
501 public event EventHandler OnListChangedCalled;
502
503 protected void OnOnListChangedCalled()
504 {
505 if (OnListChangedCalled != null)
506 OnListChangedCalled(this, EventArgs.Empty);
507 }
508
509 protected override void OnListChanged(ListChangedEventArgs e)
510 {
511 OnOnListChangedCalled();
512 base.OnListChanged(e);
513 }
514 }
515
516
517 [Test]
518 public void DerivedOnListChanged()
519 {
520 var called = false;
521 var list = new DerivedEditableList<int>();
522
523 list.OnListChangedCalled += (sender, args) => called = true;
524
525 list.Add(1);
526
527 Assert.IsTrue(called);
528 Assert.AreEqual(1, list.NewItems.Count);
529 Assert.AreEqual(1, list.Count);
530 Assert.AreEqual(0, list.DelItems.Count);
531
532 called = false;
533 list.RemoveAt(0);
534
535 Assert.IsTrue(called);
536 Assert.AreEqual(0, list.NewItems.Count);
537 Assert.AreEqual(0, list.Count);
538 Assert.AreEqual(0, list.DelItems.Count);
539 }
540
541 [Test]
542 public void CloneTest()
543 {
544 var src = new EditableList<int> { 1, 2, 3 };
545 var clone = (EditableList<int>)src.Clone();
546
547 Assert.AreEqual(src.Count, clone.Count);
548 Assert.AreEqual(src.NewItems.Count, clone.NewItems.Count);
549 Assert.AreEqual(src.DelItems.Count, clone.DelItems.Count);
550
551 src.AcceptChanges();
552
553 clone = (EditableList<int>)src.Clone();
554
555 Assert.AreEqual(src.Count, clone.Count);
556 Assert.AreEqual(src.NewItems.Count, clone.NewItems.Count);
557 Assert.AreEqual(src.DelItems.Count, clone.DelItems.Count);
558
559 src.RemoveAt(1);
560
561 clone = (EditableList<int>)src.Clone();
562
563 Assert.AreEqual(src.Count, clone.Count);
564 Assert.AreEqual(src.NewItems.Count, clone.NewItems.Count);
565 Assert.AreEqual(src.DelItems.Count, clone.DelItems.Count);
566 }
567
568 [Test]
569 public void CreateCleanTest()
570 {
571 var list = new EditableList<int>(new[] { 1, 2, 3 });
572
573 Assert.IsFalse(list.IsDirty);
574
575 Assert.AreEqual(3, list.Count);
576 Assert.AreEqual(0, list.NewItems.Count);
577 Assert.AreEqual(0, list.DelItems.Count);
578
579 list = new EditableList<int>(new int[] { });
580
581 Assert.IsFalse(list.IsDirty);
582
583 Assert.AreEqual(0, list.Count);
584 Assert.AreEqual(0, list.NewItems.Count);
585 Assert.AreEqual(0, list.DelItems.Count);
586 }
587
588 [Test]
589 public void IsDirtyTest()
590 {
591 var list = new EditableList<EditableTestObject>();
592
593 list.AddNew();
594 list.AddNew();
595
596 Assert.IsTrue(list.IsDirty);
597
598 list.AcceptChanges();
599
600 Assert.IsFalse(list.IsDirty);
601
602 list[1].ID = 101;
603
604 Assert.IsTrue(list[1].IsDirty);
605 Assert.IsTrue(list.IsDirty);
606 }
607
608 [Test]
609 public void CreateWithDirtyTest()
610 {
611 var to = EditableTestObject.CreateInstance();
612
613 to.ID = 10;
614
615 Assert.IsTrue(to.IsDirty);
616
617 var list = new EditableList<EditableTestObject>(new[] { to });
618
619 Assert.IsTrue(to.IsDirty);
620 Assert.IsTrue(list.IsDirty);
621 }
622 }
623 }