diff options
Diffstat (limited to 'OpenSim/Data/Tests/PropertyCompareConstraint.cs')
-rw-r--r-- | OpenSim/Data/Tests/PropertyCompareConstraint.cs | 180 |
1 files changed, 135 insertions, 45 deletions
diff --git a/OpenSim/Data/Tests/PropertyCompareConstraint.cs b/OpenSim/Data/Tests/PropertyCompareConstraint.cs index 063267b..5f53725 100644 --- a/OpenSim/Data/Tests/PropertyCompareConstraint.cs +++ b/OpenSim/Data/Tests/PropertyCompareConstraint.cs | |||
@@ -69,6 +69,15 @@ 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 | //prevent loops... | ||
73 | if(propertyNames.Count > 50) | ||
74 | { | ||
75 | failingPropertyName = string.Join(".", propertyNames.Reverse().ToArray()); | ||
76 | failingActual = actual; | ||
77 | failingExpected = expected; | ||
78 | return false; | ||
79 | } | ||
80 | |||
72 | if (actual.GetType() != expected.GetType()) | 81 | if (actual.GetType() != expected.GetType()) |
73 | { | 82 | { |
74 | propertyNames.Push("GetType()"); | 83 | propertyNames.Push("GetType()"); |
@@ -122,6 +131,60 @@ namespace OpenSim.Data.Tests | |||
122 | return true; | 131 | return true; |
123 | } | 132 | } |
124 | 133 | ||
134 | IComparable comp = actual as IComparable; | ||
135 | if (comp != null) | ||
136 | { | ||
137 | if (comp.CompareTo(expected) != 0) | ||
138 | { | ||
139 | failingPropertyName = string.Join(".", propertyNames.Reverse().ToArray()); | ||
140 | failingActual = actual; | ||
141 | failingExpected = expected; | ||
142 | return false; | ||
143 | } | ||
144 | return true; | ||
145 | } | ||
146 | |||
147 | //Now try the much more annoying IComparable<T> | ||
148 | Type icomparableInterface = actual.GetType().GetInterface("IComparable`1"); | ||
149 | if (icomparableInterface != null) | ||
150 | { | ||
151 | int result = (int)icomparableInterface.GetMethod("CompareTo").Invoke(actual, new[] { expected }); | ||
152 | if (result != 0) | ||
153 | { | ||
154 | failingPropertyName = string.Join(".", propertyNames.Reverse().ToArray()); | ||
155 | failingActual = actual; | ||
156 | failingExpected = expected; | ||
157 | return false; | ||
158 | } | ||
159 | return true; | ||
160 | } | ||
161 | |||
162 | IEnumerable arr = actual as IEnumerable; | ||
163 | if (arr != null) | ||
164 | { | ||
165 | List<object> actualList = arr.Cast<object>().ToList(); | ||
166 | List<object> expectedList = ((IEnumerable)expected).Cast<object>().ToList(); | ||
167 | if (actualList.Count != expectedList.Count) | ||
168 | { | ||
169 | propertyNames.Push("Count"); | ||
170 | failingPropertyName = string.Join(".", propertyNames.Reverse().ToArray()); | ||
171 | failingActual = actualList.Count; | ||
172 | failingExpected = expectedList.Count; | ||
173 | propertyNames.Pop(); | ||
174 | return false; | ||
175 | } | ||
176 | //actualList and expectedList should be the same size. | ||
177 | for (int i = 0; i < actualList.Count; i++) | ||
178 | { | ||
179 | propertyNames.Push("[" + i + "]"); | ||
180 | if (!ObjectCompare(expectedList[i], actualList[i], propertyNames)) | ||
181 | return false; | ||
182 | propertyNames.Pop(); | ||
183 | } | ||
184 | //Everything seems okay... | ||
185 | return true; | ||
186 | } | ||
187 | |||
125 | //Skip static properties. I had a nasty problem comparing colors because of all of the public static colors. | 188 | //Skip static properties. I had a nasty problem comparing colors because of all of the public static colors. |
126 | PropertyInfo[] properties = expected.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); | 189 | PropertyInfo[] properties = expected.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); |
127 | foreach (var property in properties) | 190 | foreach (var property in properties) |
@@ -147,41 +210,6 @@ namespace OpenSim.Data.Tests | |||
147 | return false; | 210 | return false; |
148 | } | 211 | } |
149 | 212 | ||
150 | IComparable comp = actualValue as IComparable; | ||
151 | if (comp != null) | ||
152 | { | ||
153 | if (comp.CompareTo(expectedValue) != 0) | ||
154 | { | ||
155 | propertyNames.Push(property.Name); | ||
156 | failingPropertyName = string.Join(".", propertyNames.Reverse().ToArray()); | ||
157 | propertyNames.Pop(); | ||
158 | failingActual = actualValue; | ||
159 | failingExpected = expectedValue; | ||
160 | return false; | ||
161 | } | ||
162 | continue; | ||
163 | } | ||
164 | |||
165 | IEnumerable arr = actualValue as IEnumerable; | ||
166 | if (arr != null) | ||
167 | { | ||
168 | List<object> actualList = arr.Cast<object>().ToList(); | ||
169 | List<object> expectedList = ((IEnumerable)expectedValue).Cast<object>().ToList(); | ||
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; | ||
183 | } | ||
184 | |||
185 | propertyNames.Push(property.Name); | 213 | propertyNames.Push(property.Name); |
186 | if (!ObjectCompare(expectedValue, actualValue, propertyNames)) | 214 | if (!ObjectCompare(expectedValue, actualValue, propertyNames)) |
187 | return false; | 215 | return false; |
@@ -223,15 +251,7 @@ namespace OpenSim.Data.Tests | |||
223 | { | 251 | { |
224 | //If the inside of the lambda is the access to x, we've hit the end of the chain. | 252 | //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. | 253 | // 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) | 254 | 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 | } | 255 | } |
236 | } | 256 | } |
237 | } | 257 | } |
@@ -270,7 +290,7 @@ namespace OpenSim.Data.Tests | |||
270 | { | 290 | { |
271 | HasInt actual = new HasInt { TheValue = 5 }; | 291 | HasInt actual = new HasInt { TheValue = 5 }; |
272 | HasInt expected = new HasInt { TheValue = 4 }; | 292 | HasInt expected = new HasInt { TheValue = 4 }; |
273 | var constraint = Constraints.PropertyCompareConstraint(expected).IgnoreProperty(x=>x.TheValue); | 293 | var constraint = Constraints.PropertyCompareConstraint(expected).IgnoreProperty(x => x.TheValue); |
274 | 294 | ||
275 | Assert.That(constraint.Matches(actual), Is.True); | 295 | Assert.That(constraint.Matches(actual), Is.True); |
276 | } | 296 | } |
@@ -312,6 +332,28 @@ namespace OpenSim.Data.Tests | |||
312 | } | 332 | } |
313 | 333 | ||
314 | [Test] | 334 | [Test] |
335 | public void UUIDShouldMatch() | ||
336 | { | ||
337 | UUID uuid1 = UUID.Random(); | ||
338 | UUID uuid2 = UUID.Parse(uuid1.ToString()); | ||
339 | |||
340 | var constraint = Constraints.PropertyCompareConstraint(uuid1); | ||
341 | |||
342 | Assert.That(constraint.Matches(uuid2), Is.True); | ||
343 | } | ||
344 | |||
345 | [Test] | ||
346 | public void UUIDShouldNotMatch() | ||
347 | { | ||
348 | UUID uuid1 = UUID.Random(); | ||
349 | UUID uuid2 = UUID.Random(); | ||
350 | |||
351 | var constraint = Constraints.PropertyCompareConstraint(uuid1); | ||
352 | |||
353 | Assert.That(constraint.Matches(uuid2), Is.False); | ||
354 | } | ||
355 | |||
356 | [Test] | ||
315 | public void TestColors() | 357 | public void TestColors() |
316 | { | 358 | { |
317 | Color actual = Color.Red; | 359 | Color actual = Color.Red; |
@@ -321,5 +363,53 @@ namespace OpenSim.Data.Tests | |||
321 | 363 | ||
322 | Assert.That(constraint.Matches(actual), Is.True); | 364 | Assert.That(constraint.Matches(actual), Is.True); |
323 | } | 365 | } |
366 | |||
367 | [Test] | ||
368 | public void ShouldCompareLists() | ||
369 | { | ||
370 | List<int> expected = new List<int> { 1, 2, 3 }; | ||
371 | List<int> actual = new List<int> { 1, 2, 3 }; | ||
372 | |||
373 | var constraint = Constraints.PropertyCompareConstraint(expected); | ||
374 | Assert.That(constraint.Matches(actual), Is.True); | ||
375 | } | ||
376 | |||
377 | |||
378 | [Test] | ||
379 | public void ShouldFailToCompareListsThatAreDifferent() | ||
380 | { | ||
381 | List<int> expected = new List<int> { 1, 2, 3 }; | ||
382 | List<int> actual = new List<int> { 1, 2, 4 }; | ||
383 | |||
384 | var constraint = Constraints.PropertyCompareConstraint(expected); | ||
385 | Assert.That(constraint.Matches(actual), Is.False); | ||
386 | } | ||
387 | |||
388 | [Test] | ||
389 | public void ShouldFailToCompareListsThatAreDifferentLengths() | ||
390 | { | ||
391 | List<int> expected = new List<int> { 1, 2, 3 }; | ||
392 | List<int> actual = new List<int> { 1, 2 }; | ||
393 | |||
394 | var constraint = Constraints.PropertyCompareConstraint(expected); | ||
395 | Assert.That(constraint.Matches(actual), Is.False); | ||
396 | } | ||
397 | |||
398 | public class Recursive | ||
399 | { | ||
400 | public Recursive Other { get; set; } | ||
401 | } | ||
402 | |||
403 | [Test] | ||
404 | public void ErrorsOutOnRecursive() | ||
405 | { | ||
406 | Recursive parent = new Recursive(); | ||
407 | Recursive child = new Recursive(); | ||
408 | parent.Other = child; | ||
409 | child.Other = parent; | ||
410 | |||
411 | var constraint = Constraints.PropertyCompareConstraint(child); | ||
412 | Assert.That(constraint.Matches(child), Is.False); | ||
413 | } | ||
324 | } | 414 | } |
325 | } \ No newline at end of file | 415 | } \ No newline at end of file |