29 using System.Collections;
30 using System.Collections.Generic;
33 using System.Linq.Expressions;
34 using System.Reflection;
35 using NUnit.Framework;
36 using NUnit.Framework.Constraints;
38 using OpenSim.Framework;
39 using OpenSim.Tests.Common;
41 namespace OpenSim.Data.Tests
43 public static class Constraints
46 public static PropertyCompareConstraint<T> PropertyCompareConstraint<T>(T expected)
48 return new PropertyCompareConstraint<T>(expected);
52 public class PropertyCompareConstraint<T> : NUnit.Framework.Constraints.Constraint
54 private readonly
object _expected;
56 private string failingPropertyName = string.Empty;
57 private object failingExpected;
58 private object failingActual;
65 public override bool Matches(
object actual)
67 return ObjectCompare(_expected, actual,
new Stack<string>());
70 private bool ObjectCompare(
object expected,
object actual, Stack<string> propertyNames)
73 if (actual == null && expected == null)
77 if (actual == null || expected == null)
79 failingPropertyName = string.Join(
".", propertyNames.Reverse().ToArray());
80 failingActual = actual;
81 failingExpected = expected;
86 if (propertyNames.Count > 50)
88 failingPropertyName = string.Join(
".", propertyNames.Reverse().ToArray());
89 failingActual = actual;
90 failingExpected = expected;
94 if (actual.GetType() != expected.GetType())
96 propertyNames.Push(
"GetType()");
97 failingPropertyName = string.Join(
".", propertyNames.Reverse().ToArray());
99 failingActual = actual.GetType();
100 failingExpected = expected.GetType();
104 if (actual.GetType() == typeof(Color))
106 Color actualColor = (Color) actual;
107 Color expectedColor = (Color) expected;
108 if (actualColor.R != expectedColor.R)
110 propertyNames.Push(
"R");
111 failingPropertyName = string.Join(
".", propertyNames.Reverse().ToArray());
113 failingActual = actualColor.R;
114 failingExpected = expectedColor.R;
117 if (actualColor.G != expectedColor.G)
119 propertyNames.Push(
"G");
120 failingPropertyName = string.Join(
".", propertyNames.Reverse().ToArray());
122 failingActual = actualColor.G;
123 failingExpected = expectedColor.G;
126 if (actualColor.B != expectedColor.B)
128 propertyNames.Push(
"B");
129 failingPropertyName = string.Join(
".", propertyNames.Reverse().ToArray());
131 failingActual = actualColor.B;
132 failingExpected = expectedColor.B;
135 if (actualColor.A != expectedColor.A)
137 propertyNames.Push(
"A");
138 failingPropertyName = string.Join(
".", propertyNames.Reverse().ToArray());
140 failingActual = actualColor.A;
141 failingExpected = expectedColor.A;
147 IComparable comp = actual as IComparable;
150 if (comp.CompareTo(expected) != 0)
152 failingPropertyName = string.Join(
".", propertyNames.Reverse().ToArray());
153 failingActual = actual;
154 failingExpected = expected;
161 Type icomparableInterface = actual.GetType().GetInterface(
"IComparable`1");
162 if (icomparableInterface != null)
164 int result = (int)icomparableInterface.GetMethod(
"CompareTo").Invoke(actual,
new[] { expected });
167 failingPropertyName = string.Join(
".", propertyNames.Reverse().ToArray());
168 failingActual = actual;
169 failingExpected = expected;
178 List<object> actualList = arr.Cast<
object>().ToList();
179 List<object> expectedList = ((
IEnumerable)expected).Cast<
object>().ToList();
180 if (actualList.Count != expectedList.Count)
182 propertyNames.Push(
"Count");
183 failingPropertyName = string.Join(
".", propertyNames.Reverse().ToArray());
184 failingActual = actualList.Count;
185 failingExpected = expectedList.Count;
190 for (
int i = 0; i < actualList.Count; i++)
192 propertyNames.Push(
"[" + i +
"]");
193 if (!ObjectCompare(expectedList[i], actualList[i], propertyNames))
202 PropertyInfo[] properties = expected.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
203 foreach (var property
in properties)
205 if (ignores.Contains(property.Name))
208 object actualValue = property.GetValue(actual, null);
209 object expectedValue = property.GetValue(expected, null);
211 propertyNames.Push(property.Name);
212 if (!ObjectCompare(expectedValue, actualValue, propertyNames))
222 writer.WriteExpectedValue(failingExpected);
227 writer.WriteActualValue(failingActual);
229 writer.Write(
" On Property: " + failingPropertyName);
234 readonly List<string> ignores =
new List<string>();
237 Expression express = func.Body;
238 PullApartExpression(express);
243 private void PullApartExpression(Expression express)
246 if (express is UnaryExpression)
247 PullApartExpression(((UnaryExpression)express).Operand);
248 if (express is MemberExpression)
252 ignores.Add(((MemberExpression)express).Member.Name);
262 public int TheValue {
get; set; }
270 var constraint = Constraints.PropertyCompareConstraint(expected);
272 Assert.That(constraint.Matches(actual), Is.True);
280 var constraint = Constraints.PropertyCompareConstraint(expected);
282 Assert.That(constraint.Matches(actual), Is.False);
291 var constraint = Constraints.PropertyCompareConstraint(expected).IgnoreProperty(x => x.TheValue);
293 Assert.That(constraint.Matches(actual), Is.True);
299 UUID uuid1 = UUID.Random();
300 AssetBase actual =
new AssetBase(uuid1,
"asset one", (sbyte)AssetType.Texture, UUID.Zero.ToString());
301 AssetBase expected =
new AssetBase(uuid1,
"asset one", (sbyte)AssetType.Texture, UUID.Zero.ToString());
303 var constraint = Constraints.PropertyCompareConstraint(expected);
305 Assert.That(constraint.Matches(actual), Is.True);
311 UUID uuid1 = UUID.Random();
312 AssetBase actual =
new AssetBase(uuid1,
"asset one", (sbyte)AssetType.Texture, UUID.Zero.ToString());
313 AssetBase expected =
new AssetBase(UUID.Random(),
"asset one", (sbyte)AssetType.Texture, UUID.Zero.ToString());
315 var constraint = Constraints.PropertyCompareConstraint(expected);
317 Assert.That(constraint.Matches(actual), Is.False);
323 UUID uuid1 = UUID.Random();
324 AssetBase actual =
new AssetBase(uuid1,
"asset one", (sbyte)AssetType.Texture, UUID.Zero.ToString());
325 AssetBase expected =
new AssetBase(uuid1,
"asset two", (sbyte)AssetType.Texture, UUID.Zero.ToString());
327 var constraint = Constraints.PropertyCompareConstraint(expected);
329 Assert.That(constraint.Matches(actual), Is.False);
335 UUID uuid1 = UUID.Random();
336 UUID uuid2 = UUID.Parse(uuid1.ToString());
338 var constraint = Constraints.PropertyCompareConstraint(uuid1);
340 Assert.That(constraint.Matches(uuid2), Is.True);
346 UUID uuid1 = UUID.Random();
347 UUID uuid2 = UUID.Random();
349 var constraint = Constraints.PropertyCompareConstraint(uuid1);
351 Assert.That(constraint.Matches(uuid2), Is.False);
357 Color actual = Color.Red;
358 Color expected = Color.FromArgb(actual.A, actual.R, actual.G, actual.B);
360 var constraint = Constraints.PropertyCompareConstraint(expected);
362 Assert.That(constraint.Matches(actual), Is.True);
368 List<int> expected =
new List<int> { 1, 2, 3 };
369 List<int> actual =
new List<int> { 1, 2, 3 };
371 var constraint = Constraints.PropertyCompareConstraint(expected);
372 Assert.That(constraint.Matches(actual), Is.True);
379 List<int> expected =
new List<int> { 1, 2, 3 };
380 List<int> actual =
new List<int> { 1, 2, 4 };
382 var constraint = Constraints.PropertyCompareConstraint(expected);
383 Assert.That(constraint.Matches(actual), Is.False);
389 List<int> expected =
new List<int> { 1, 2, 3 };
390 List<int> actual =
new List<int> { 1, 2 };
392 var constraint = Constraints.PropertyCompareConstraint(expected);
393 Assert.That(constraint.Matches(actual), Is.False);
406 parent.Other = child;
407 child.Other = parent;
409 var constraint = Constraints.PropertyCompareConstraint(child);
410 Assert.That(constraint.Matches(child), Is.False);
void AssetShouldNotMatch()
void ShouldFailToCompareListsThatAreDifferent()
void AssetShouldNotMatch2()
PropertyCompareConstraint< T > IgnoreProperty(Expression< Func< T, object >> func)
void ShouldFailToCompareListsThatAreDifferentLengths()
void ShouldCompareLists()
Asset class. All Assets are reference by this class or a class derived from this class ...
void UUIDShouldNotMatch()
override void WriteDescriptionTo(MessageWriter writer)
System.Collections.IEnumerable IEnumerable
override bool Matches(object actual)
override void WriteActualValueTo(MessageWriter writer)
void ErrorsOutOnRecursive()
PropertyCompareConstraint(T expected)