diff options
Diffstat (limited to 'OpenSim/Data/Tests/PropertyCompareConstraint.cs')
-rw-r--r-- | OpenSim/Data/Tests/PropertyCompareConstraint.cs | 200 |
1 files changed, 144 insertions, 56 deletions
diff --git a/OpenSim/Data/Tests/PropertyCompareConstraint.cs b/OpenSim/Data/Tests/PropertyCompareConstraint.cs index 063267b..06ca53e 100644 --- a/OpenSim/Data/Tests/PropertyCompareConstraint.cs +++ b/OpenSim/Data/Tests/PropertyCompareConstraint.cs | |||
@@ -69,6 +69,28 @@ namespace OpenSim.Data.Tests | |||
69 | 69 | ||
70 | private bool ObjectCompare(object expected, object actual, Stack<string> propertyNames) | 70 | private bool ObjectCompare(object expected, object actual, Stack<string> propertyNames) |
71 | { | 71 | { |
72 | //If they are both null, they are equal | ||
73 | if (actual == null && expected == null) | ||
74 | return true; | ||
75 | |||
76 | //If only one is null, then they aren't | ||
77 | if (actual == null || expected == null) | ||
78 | { | ||
79 | failingPropertyName = string.Join(".", propertyNames.Reverse().ToArray()); | ||
80 | failingActual = actual; | ||
81 | failingExpected = expected; | ||
82 | return false; | ||
83 | } | ||
84 | |||
85 | //prevent loops... | ||
86 | if (propertyNames.Count > 50) | ||
87 | { | ||
88 | failingPropertyName = string.Join(".", propertyNames.Reverse().ToArray()); | ||
89 | failingActual = actual; | ||
90 | failingExpected = expected; | ||
91 | return false; | ||
92 | } | ||
93 | |||
72 | if (actual.GetType() != expected.GetType()) | 94 | if (actual.GetType() != expected.GetType()) |
73 | { | 95 | { |
74 | propertyNames.Push("GetType()"); | 96 | propertyNames.Push("GetType()"); |
@@ -122,65 +144,69 @@ namespace OpenSim.Data.Tests | |||
122 | return true; | 144 | return true; |
123 | } | 145 | } |
124 | 146 | ||
125 | //Skip static properties. I had a nasty problem comparing colors because of all of the public static colors. | 147 | IComparable comp = actual as IComparable; |
126 | PropertyInfo[] properties = expected.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); | 148 | if (comp != null) |
127 | foreach (var property in properties) | ||
128 | { | 149 | { |
129 | if (ignores.Contains(property.Name)) | 150 | if (comp.CompareTo(expected) != 0) |
130 | continue; | 151 | { |
131 | 152 | failingPropertyName = string.Join(".", propertyNames.Reverse().ToArray()); | |
132 | object actualValue = property.GetValue(actual, null); | 153 | failingActual = actual; |
133 | object expectedValue = property.GetValue(expected, null); | 154 | failingExpected = expected; |
155 | return false; | ||
156 | } | ||
157 | return true; | ||
158 | } | ||
134 | 159 | ||
135 | //If they are both null, they are equal | 160 | //Now try the much more annoying IComparable<T> |
136 | if (actualValue == null && expectedValue == null) | 161 | Type icomparableInterface = actual.GetType().GetInterface("IComparable`1"); |
137 | continue; | 162 | if (icomparableInterface != null) |
163 | { | ||
164 | int result = (int)icomparableInterface.GetMethod("CompareTo").Invoke(actual, new[] { expected }); | ||
165 | if (result != 0) | ||
166 | { | ||
167 | failingPropertyName = string.Join(".", propertyNames.Reverse().ToArray()); | ||
168 | failingActual = actual; | ||
169 | failingExpected = expected; | ||
170 | return false; | ||
171 | } | ||
172 | return true; | ||
173 | } | ||
138 | 174 | ||
139 | //If only one is null, then they aren't | 175 | IEnumerable arr = actual as IEnumerable; |
140 | if (actualValue == null || expectedValue == null) | 176 | if (arr != null) |
177 | { | ||
178 | List<object> actualList = arr.Cast<object>().ToList(); | ||
179 | List<object> expectedList = ((IEnumerable)expected).Cast<object>().ToList(); | ||
180 | if (actualList.Count != expectedList.Count) | ||
141 | { | 181 | { |
142 | propertyNames.Push(property.Name); | 182 | propertyNames.Push("Count"); |
143 | failingPropertyName = string.Join(".", propertyNames.Reverse().ToArray()); | 183 | failingPropertyName = string.Join(".", propertyNames.Reverse().ToArray()); |
184 | failingActual = actualList.Count; | ||
185 | failingExpected = expectedList.Count; | ||
144 | propertyNames.Pop(); | 186 | propertyNames.Pop(); |
145 | failingActual = actualValue; | ||
146 | failingExpected = expectedValue; | ||
147 | return false; | 187 | return false; |
148 | } | 188 | } |
149 | 189 | //actualList and expectedList should be the same size. | |
150 | IComparable comp = actualValue as IComparable; | 190 | for (int i = 0; i < actualList.Count; i++) |
151 | if (comp != null) | ||
152 | { | 191 | { |
153 | if (comp.CompareTo(expectedValue) != 0) | 192 | propertyNames.Push("[" + i + "]"); |
154 | { | 193 | if (!ObjectCompare(expectedList[i], actualList[i], propertyNames)) |
155 | propertyNames.Push(property.Name); | ||
156 | failingPropertyName = string.Join(".", propertyNames.Reverse().ToArray()); | ||
157 | propertyNames.Pop(); | ||
158 | failingActual = actualValue; | ||
159 | failingExpected = expectedValue; | ||
160 | return false; | 194 | return false; |
161 | } | 195 | propertyNames.Pop(); |
162 | continue; | ||
163 | } | 196 | } |
197 | //Everything seems okay... | ||
198 | return true; | ||
199 | } | ||
164 | 200 | ||
165 | IEnumerable arr = actualValue as IEnumerable; | 201 | //Skip static properties. I had a nasty problem comparing colors because of all of the public static colors. |
166 | if (arr != null) | 202 | PropertyInfo[] properties = expected.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); |
167 | { | 203 | foreach (var property in properties) |
168 | List<object> actualList = arr.Cast<object>().ToList(); | 204 | { |
169 | List<object> expectedList = ((IEnumerable)expectedValue).Cast<object>().ToList(); | 205 | if (ignores.Contains(property.Name)) |
170 | if (actualList.Count != expectedList.Count) | ||
171 | { | ||
172 | propertyNames.Push(property.Name); | ||
173 | propertyNames.Push("Count"); | ||
174 | failingPropertyName = string.Join(".", propertyNames.Reverse().ToArray()); | ||
175 | failingActual = actualList.Count; | ||
176 | failingExpected = expectedList.Count; | ||
177 | propertyNames.Pop(); | ||
178 | propertyNames.Pop(); | ||
179 | } | ||
180 | //Todo: A value-wise comparison of all of the values. | ||
181 | //Everything seems okay... | ||
182 | continue; | 206 | continue; |
183 | } | 207 | |
208 | object actualValue = property.GetValue(actual, null); | ||
209 | object expectedValue = property.GetValue(expected, null); | ||
184 | 210 | ||
185 | propertyNames.Push(property.Name); | 211 | propertyNames.Push(property.Name); |
186 | if (!ObjectCompare(expectedValue, actualValue, propertyNames)) | 212 | if (!ObjectCompare(expectedValue, actualValue, propertyNames)) |
@@ -223,15 +249,7 @@ namespace OpenSim.Data.Tests | |||
223 | { | 249 | { |
224 | //If the inside of the lambda is the access to x, we've hit the end of the chain. | 250 | //If the inside of the lambda is the access to x, we've hit the end of the chain. |
225 | // We should track by the fully scoped parameter name, but this is the first rev of doing this. | 251 | // We should track by the fully scoped parameter name, but this is the first rev of doing this. |
226 | if (((MemberExpression)express).Expression is ParameterExpression) | 252 | ignores.Add(((MemberExpression)express).Member.Name); |
227 | { | ||
228 | ignores.Add(((MemberExpression)express).Member.Name); | ||
229 | } | ||
230 | else | ||
231 | { | ||
232 | //Otherwise there could be more parameters inside... | ||
233 | PullApartExpression(((MemberExpression)express).Expression); | ||
234 | } | ||
235 | } | 253 | } |
236 | } | 254 | } |
237 | } | 255 | } |
@@ -270,7 +288,7 @@ namespace OpenSim.Data.Tests | |||
270 | { | 288 | { |
271 | HasInt actual = new HasInt { TheValue = 5 }; | 289 | HasInt actual = new HasInt { TheValue = 5 }; |
272 | HasInt expected = new HasInt { TheValue = 4 }; | 290 | HasInt expected = new HasInt { TheValue = 4 }; |
273 | var constraint = Constraints.PropertyCompareConstraint(expected).IgnoreProperty(x=>x.TheValue); | 291 | var constraint = Constraints.PropertyCompareConstraint(expected).IgnoreProperty(x => x.TheValue); |
274 | 292 | ||
275 | Assert.That(constraint.Matches(actual), Is.True); | 293 | Assert.That(constraint.Matches(actual), Is.True); |
276 | } | 294 | } |
@@ -312,6 +330,28 @@ namespace OpenSim.Data.Tests | |||
312 | } | 330 | } |
313 | 331 | ||
314 | [Test] | 332 | [Test] |
333 | public void UUIDShouldMatch() | ||
334 | { | ||
335 | UUID uuid1 = UUID.Random(); | ||
336 | UUID uuid2 = UUID.Parse(uuid1.ToString()); | ||
337 | |||
338 | var constraint = Constraints.PropertyCompareConstraint(uuid1); | ||
339 | |||
340 | Assert.That(constraint.Matches(uuid2), Is.True); | ||
341 | } | ||
342 | |||
343 | [Test] | ||
344 | public void UUIDShouldNotMatch() | ||
345 | { | ||
346 | UUID uuid1 = UUID.Random(); | ||
347 | UUID uuid2 = UUID.Random(); | ||
348 | |||
349 | var constraint = Constraints.PropertyCompareConstraint(uuid1); | ||
350 | |||
351 | Assert.That(constraint.Matches(uuid2), Is.False); | ||
352 | } | ||
353 | |||
354 | [Test] | ||
315 | public void TestColors() | 355 | public void TestColors() |
316 | { | 356 | { |
317 | Color actual = Color.Red; | 357 | Color actual = Color.Red; |
@@ -321,5 +361,53 @@ namespace OpenSim.Data.Tests | |||
321 | 361 | ||
322 | Assert.That(constraint.Matches(actual), Is.True); | 362 | Assert.That(constraint.Matches(actual), Is.True); |
323 | } | 363 | } |
364 | |||
365 | [Test] | ||
366 | public void ShouldCompareLists() | ||
367 | { | ||
368 | List<int> expected = new List<int> { 1, 2, 3 }; | ||
369 | List<int> actual = new List<int> { 1, 2, 3 }; | ||
370 | |||
371 | var constraint = Constraints.PropertyCompareConstraint(expected); | ||
372 | Assert.That(constraint.Matches(actual), Is.True); | ||
373 | } | ||
374 | |||
375 | |||
376 | [Test] | ||
377 | public void ShouldFailToCompareListsThatAreDifferent() | ||
378 | { | ||
379 | List<int> expected = new List<int> { 1, 2, 3 }; | ||
380 | List<int> actual = new List<int> { 1, 2, 4 }; | ||
381 | |||
382 | var constraint = Constraints.PropertyCompareConstraint(expected); | ||
383 | Assert.That(constraint.Matches(actual), Is.False); | ||
384 | } | ||
385 | |||
386 | [Test] | ||
387 | public void ShouldFailToCompareListsThatAreDifferentLengths() | ||
388 | { | ||
389 | List<int> expected = new List<int> { 1, 2, 3 }; | ||
390 | List<int> actual = new List<int> { 1, 2 }; | ||
391 | |||
392 | var constraint = Constraints.PropertyCompareConstraint(expected); | ||
393 | Assert.That(constraint.Matches(actual), Is.False); | ||
394 | } | ||
395 | |||
396 | public class Recursive | ||
397 | { | ||
398 | public Recursive Other { get; set; } | ||
399 | } | ||
400 | |||
401 | [Test] | ||
402 | public void ErrorsOutOnRecursive() | ||
403 | { | ||
404 | Recursive parent = new Recursive(); | ||
405 | Recursive child = new Recursive(); | ||
406 | parent.Other = child; | ||
407 | child.Other = parent; | ||
408 | |||
409 | var constraint = Constraints.PropertyCompareConstraint(child); | ||
410 | Assert.That(constraint.Matches(child), Is.False); | ||
411 | } | ||
324 | } | 412 | } |
325 | } \ No newline at end of file | 413 | } \ No newline at end of file |