CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
SortedBindingListTests.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="SortedBindingListTests.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>no summary</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Collections.Generic;
10using System.Text;
11using System.ComponentModel;
12using Csla.TestHelpers;
13
14#if !NUNIT
15using Microsoft.VisualStudio.TestTools.UnitTesting;
16#else
17using NUnit.Framework;
18using TestClass = NUnit.Framework.TestFixtureAttribute;
19using TestInitialize = NUnit.Framework.SetUpAttribute;
20using TestCleanup = NUnit.Framework.TearDownAttribute;
21using TestMethod = NUnit.Framework.TestAttribute;
22#endif
23
25{
26 [TestClass()]
28 {
29 private static TestDIContext _testDIContext;
30
32 public static void ClassInitialize(TestContext context)
33 {
34 _testDIContext = TestDIContextFactory.CreateDefaultContext();
35 }
36
37 [TestMethod()]
38 public void AscendingSort()
39 {
40 int[] intArr = { 45, 23, 57, 56, 11, 87, 94, 44 };
41 SortedBindingList<int> sortedList = new SortedBindingList<int>(intArr);
42 sortedList.ListChanged += new ListChangedEventHandler(sortedList_ListChanged);
43
44 Assert.AreEqual(false, sortedList.IsSorted);
45 Assert.AreEqual(56, intArr[3]);
46 sortedList.ApplySort("", ListSortDirection.Ascending);
47 Assert.AreEqual(44, sortedList[2]);
48 Assert.AreEqual(8, sortedList.Count);
49 Assert.AreEqual(true, sortedList.Contains(56));
50 Assert.AreEqual(4, sortedList.IndexOf(56));
51 Assert.AreEqual(true, sortedList.IsReadOnly);
52 Assert.AreEqual(true, sortedList.IsSorted);
53
54 foreach (int item in sortedList)
55 {
56 Console.WriteLine(item.ToString());
57 }
58
59 sortedList.RemoveSort();
60 Assert.AreEqual(false, sortedList.IsSorted);
61 Assert.AreEqual(56, sortedList[3]);
62
63 // This list dowes not support searching
64 Assert.IsFalse(sortedList.SupportsSearching);
65 // and Find should return -1 because underlying list dows not implement IBindingList
66 Assert.AreEqual(-1, sortedList.Find("", 56));
67 }
68
69 public void sortedList_ListChanged(object sender, ListChangedEventArgs e)
70 {
71 //called only on applysort() and removesort()
72 Console.WriteLine("list changed");
73 }
74
75 [TestMethod()]
76 public void DescendingSort()
77 {
78 string[] strArr = { "zandy", "alex", "Chris", "bert", "alfred", "Bert", "Jimmy", "chris", "chris", "mobbit", "myper", "Corey", "Monkey" };
80
81 Assert.AreEqual("Bert", sortedList[5]);
82
83 sortedList.ApplySort("", ListSortDirection.Descending);
84
85 foreach (string item in sortedList)
86 {
87 Console.WriteLine(item);
88 }
89
90 for (int i = 0; i < sortedList.Count; i++)
91 {
92 Console.WriteLine("regular loop: " + sortedList[i]);
93 }
94
95 Assert.AreEqual("Corey", sortedList[5]);
96
97 Console.WriteLine();
98 Console.WriteLine(sortedList.Count);
99 }
100
101 //[TestMethod()]
102 //public void TestFirstItemIssue()
103 //{
104 // int[] intArray = { 10, 4, 6 };
105 // SortedBindingList<int> sortedList = new SortedBindingList<int>(intArray);
106
107 // foreach (int item in sortedList)
108 // {
109 // Console.WriteLine(item.ToString());
110 // }
111
112 // sortedList.ApplySort("", ListSortDirection.Ascending);
113
114 // Console.WriteLine();
115
116 // foreach (int item in sortedList)
117 // {
118 // Console.WriteLine(item.ToString());
119 // }
120
121 // Console.WriteLine();
122
123 // sortedList.ApplySort("", ListSortDirection.Descending);
124
125 // foreach (int item in sortedList)
126 // {
127 // Console.WriteLine(item.ToString());
128 // }
129 //}
130
131 [TestMethod()]
132 public void CopyTo()
133 {
134 int[] intArray = { 5, 7, 1, 3, 5, 44, 32 };
135 SortedBindingList<int> sortedList = new SortedBindingList<int>(intArray);
136
137 int[] intArray2 = { 3, 75, 1222, 3333, 511, 443, 332 };
138
139 Assert.AreEqual(1222, intArray2[2]);
140
141 sortedList.ApplySort("", ListSortDirection.Descending);
142 Assert.AreEqual(44, sortedList[0], "Sorted values incorrect");
143
144 sortedList.CopyTo(intArray2, 0);
145
146 Assert.AreEqual(44, intArray2[0], "Copied values incorrect");
147 Assert.AreEqual(7, intArray2[2], "Copied values incorrect");
148
149 foreach (int item in intArray2)
150 {
151 Console.WriteLine(item.ToString());
152 }
153 }
154
155 [TestMethod]
156 public void IndexOf()
157 {
158 List<string> list = new List<string>();
159
160 string barney = "Barney";
161 string charlie = "Charlie";
162 string zeke = "Zeke";
163
164 list.AddRange(new string[] { charlie, barney, zeke });
165
167
168 Assert.AreEqual(1, sortedList.IndexOf(barney), "Unsorted index should be 1");
169
170 sortedList.ApplySort(string.Empty, System.ComponentModel.ListSortDirection.Ascending);
171
172 Assert.AreEqual(1, sortedList.IndexOf(charlie), "Sorted index should be 1");
173 }
174
175 [TestMethod]
176 public void SourceList()
177 {
178 List<string> list = new List<string>();
179
180 string barney = "Barney";
181 string charlie = "Charlie";
182 string zeke = "Zeke";
183
184 list.AddRange(new string[] { charlie, barney, zeke });
185
187
188 Assert.IsTrue(ReferenceEquals(list, sortedList.SourceList), "List references should match");
189 }
190
191 [TestMethod]
192 [ExpectedException(typeof(ArgumentException))]
194 {
195 int[] intArray = { 5, 7, 1, 3, 5, 44, 32 };
196 SortedBindingList<int> sortedList = new SortedBindingList<int>(intArray);
197 sortedList.ApplySort("NotAProperty", ListSortDirection.Ascending);
198 }
199
200 [TestMethod]
201 [ExpectedException(typeof(ArgumentException))]
203 {
204 int[] intArray = { 5, 7, 1, 3, 5, 44, 32 };
205 SortedBindingList<int> sortedList = new SortedBindingList<int>(intArray);
206 sortedList.Find("NotAProperty", 7);
207 }
208
209 [TestMethod]
211 {
212 IDataPortal<MyChildren> dataPortal = _testDIContext.CreateDataPortal<MyChildren>();
213
214 var source = dataPortal.Create();
215 source.BeginEdit();
216 var sorted = new SortedBindingList<MyChild>(source);
217 sorted.ApplySort(MyChild.NameProperty.Name, ListSortDirection.Ascending);
218 var current = source.AddNew();
219 current.BeginEdit();
220 current.CancelEdit();
221 source.CancelEdit();
222 }
223 }
224
226 public class MyChildren
227 : BusinessBindingListBase<MyChildren, MyChild>
228 {
229 [Create]
230 private void Create()
231 { }
232 }
233
235 public class MyChild
236 : BusinessBase<MyChild>
237 {
238 public static readonly PropertyInfo<string> NameProperty = RegisterProperty<string>(p => p.Name);
239 public string Name
240 {
241 get { return GetProperty(NameProperty); }
242 set { SetProperty(NameProperty, value); }
243 }
244
245 [CreateChild]
246 private void CreateChild()
247 {
248 using (BypassPropertyChecks)
249 {
250 Name = Guid.NewGuid().ToString();
251 }
252 }
253 }
254}
This is the base class from which most business objects will be derived.
Definition: BusinessBase.cs:38
This is the base class from which most business collections or lists will be derived.
Maintains metadata about a property.
Provides a sorted view into an existing IList(Of T).
void ApplySort(string propertyName, ListSortDirection direction)
Applies a sort to the view.
int Find(string propertyName, object key)
Finds an item in the view
bool IsReadOnly
Implemented by IList source object.
bool IsSorted
Gets a value indicating whether the view is currently sorted.
ListChangedEventHandler ListChanged
Raised to indicate that the list's data has changed.
IList< T > SourceList
Gets the source list over which this SortedBindingList is a view.
int Count
Implemented by IList source object.
bool Contains(T item)
Implemented by IList source object.
int IndexOf(T item)
Implemented by IList source object.
void CopyTo(T[] array, int arrayIndex)
Implemented by IList source object.
static readonly PropertyInfo< string > NameProperty
void sortedList_ListChanged(object sender, ListChangedEventArgs e)
Type to carry context information for DI in unit tests
Interface defining the members of the data portal type.
Definition: IDataPortalT.cs:17
object Create(params object[] criteria)
Called by a factory method in a business class to create a new object, which is loaded with default v...
@ Serializable
Prevents updating or inserting until the transaction is complete.