aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim
diff options
context:
space:
mode:
authorKunnis2009-08-09 02:05:21 -0500
committerJustin Clark-Casey (justincc)2009-08-14 18:15:25 +0100
commitc18f7560d9ab13c0c7a73c679383ceee13d1b1e3 (patch)
treed64cc86e616ce6d6a9de67cae6bae11513cb70bc /OpenSim
parentRemove one more sponsor reference (diff)
downloadopensim-SC_OLD-c18f7560d9ab13c0c7a73c679383ceee13d1b1e3.zip
opensim-SC_OLD-c18f7560d9ab13c0c7a73c679383ceee13d1b1e3.tar.gz
opensim-SC_OLD-c18f7560d9ab13c0c7a73c679383ceee13d1b1e3.tar.bz2
opensim-SC_OLD-c18f7560d9ab13c0c7a73c679383ceee13d1b1e3.tar.xz
Adding in Reflection-based testing, to ensure that all properties are covered.
Diffstat (limited to '')
-rw-r--r--OpenSim/Data/Tests/PropertyCompareConstraint.cs298
-rw-r--r--OpenSim/Data/Tests/ScrambleForTesting.cs102
2 files changed, 400 insertions, 0 deletions
diff --git a/OpenSim/Data/Tests/PropertyCompareConstraint.cs b/OpenSim/Data/Tests/PropertyCompareConstraint.cs
new file mode 100644
index 0000000..678501e
--- /dev/null
+++ b/OpenSim/Data/Tests/PropertyCompareConstraint.cs
@@ -0,0 +1,298 @@
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Drawing;
5using System.Linq;
6using System.Linq.Expressions;
7using System.Reflection;
8using NUnit.Framework;
9using NUnit.Framework.Constraints;
10using NUnit.Framework.SyntaxHelpers;
11using OpenMetaverse;
12using OpenSim.Framework;
13
14namespace OpenSim.Data.Tests
15{
16 public static class Constraints
17 {
18 //This is here because C# has a gap in the language, you can't infer type from a constructor
19 public static PropertyCompareConstraint<T> PropertyCompareConstraint<T>(T expected)
20 {
21 return new PropertyCompareConstraint<T>(expected);
22 }
23 }
24
25 public class PropertyCompareConstraint<T> : NUnit.Framework.Constraints.Constraint
26 {
27 private readonly object _expected;
28 //the reason everywhere uses propertyNames.Reverse().ToArray() is because the stack is backwards of the order we want to display the properties in.
29 private string failingPropertyName = string.Empty;
30 private object failingExpected;
31 private object failingActual;
32
33 public PropertyCompareConstraint(T expected)
34 {
35 _expected = expected;
36 }
37
38 public override bool Matches(object actual)
39 {
40 return ObjectCompare(_expected, actual, new Stack<string>());
41 }
42
43 private bool ObjectCompare(object expected, object actual, Stack<string> propertyNames)
44 {
45 if (actual.GetType() != expected.GetType())
46 {
47 propertyNames.Push("GetType()");
48 failingPropertyName = string.Join(".", propertyNames.Reverse().ToArray());
49 propertyNames.Pop();
50 failingActual = actual.GetType();
51 failingExpected = expected.GetType();
52 return false;
53 }
54
55 if(actual.GetType() == typeof(Color))
56 {
57 Color actualColor = (Color) actual;
58 Color expectedColor = (Color) expected;
59 if (actualColor.R != expectedColor.R)
60 {
61 propertyNames.Push("R");
62 failingPropertyName = string.Join(".", propertyNames.Reverse().ToArray());
63 propertyNames.Pop();
64 failingActual = actualColor.R;
65 failingExpected = expectedColor.R;
66 return false;
67 }
68 if (actualColor.G != expectedColor.G)
69 {
70 propertyNames.Push("G");
71 failingPropertyName = string.Join(".", propertyNames.Reverse().ToArray());
72 propertyNames.Pop();
73 failingActual = actualColor.G;
74 failingExpected = expectedColor.G;
75 return false;
76 }
77 if (actualColor.B != expectedColor.B)
78 {
79 propertyNames.Push("B");
80 failingPropertyName = string.Join(".", propertyNames.Reverse().ToArray());
81 propertyNames.Pop();
82 failingActual = actualColor.B;
83 failingExpected = expectedColor.B;
84 return false;
85 }
86 if (actualColor.A != expectedColor.A)
87 {
88 propertyNames.Push("A");
89 failingPropertyName = string.Join(".", propertyNames.Reverse().ToArray());
90 propertyNames.Pop();
91 failingActual = actualColor.A;
92 failingExpected = expectedColor.A;
93 return false;
94 }
95 return true;
96 }
97
98 //Skip static properties. I had a nasty problem comparing colors because of all of the public static colors.
99 PropertyInfo[] properties = expected.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
100 foreach (var property in properties)
101 {
102 if (ignores.Contains(property.Name))
103 continue;
104
105 object actualValue = property.GetValue(actual, null);
106 object expectedValue = property.GetValue(expected, null);
107
108 //If they are both null, they are equal
109 if (actualValue == null && expectedValue == null)
110 continue;
111
112 //If only one is null, then they aren't
113 if (actualValue == null || expectedValue == null)
114 {
115 propertyNames.Push(property.Name);
116 failingPropertyName = string.Join(".", propertyNames.Reverse().ToArray());
117 propertyNames.Pop();
118 failingActual = actualValue;
119 failingExpected = expectedValue;
120 return false;
121 }
122
123 IComparable comp = actualValue as IComparable;
124 if (comp != null)
125 {
126 if (comp.CompareTo(expectedValue) != 0)
127 {
128 propertyNames.Push(property.Name);
129 failingPropertyName = string.Join(".", propertyNames.Reverse().ToArray());
130 propertyNames.Pop();
131 failingActual = actualValue;
132 failingExpected = expectedValue;
133 return false;
134 }
135 continue;
136 }
137
138 IEnumerable arr = actualValue as IEnumerable;
139 if (arr != null)
140 {
141 List<object> actualList = arr.Cast<object>().ToList();
142 List<object> expectedList = ((IEnumerable)expectedValue).Cast<object>().ToList();
143 if (actualList.Count != expectedList.Count)
144 {
145 propertyNames.Push(property.Name);
146 propertyNames.Push("Count");
147 failingPropertyName = string.Join(".", propertyNames.Reverse().ToArray());
148 failingActual = actualList.Count;
149 failingExpected = expectedList.Count;
150 propertyNames.Pop();
151 propertyNames.Pop();
152 }
153 //Todo: A value-wise comparison of all of the values.
154 //Everything seems okay...
155 continue;
156 }
157
158 propertyNames.Push(property.Name);
159 if (!ObjectCompare(expectedValue, actualValue, propertyNames))
160 return false;
161 propertyNames.Pop();
162 }
163
164 return true;
165 }
166
167 public override void WriteDescriptionTo(MessageWriter writer)
168 {
169 writer.WriteExpectedValue(failingExpected);
170 }
171
172 public override void WriteActualValueTo(MessageWriter writer)
173 {
174 writer.WriteActualValue(failingActual);
175 writer.WriteLine();
176 writer.Write(" On Property: " + failingPropertyName);
177 }
178
179 //These notes assume the lambda: (x=>x.Parent.Value)
180 //ignores should really contain like a fully dotted version of the property name, but I'm starting with small steps
181 readonly List<string> ignores = new List<string>();
182 public PropertyCompareConstraint<T> IgnoreProperty(Expression<Func<T, object>> func)
183 {
184 Expression express = func.Body;
185 PullApartExpression(express);
186
187 return this;
188 }
189
190 private void PullApartExpression(Expression express)
191 {
192 //This deals with any casts... like implicit casts to object. Not all UnaryExpression are casts, but this is a first attempt.
193 if (express is UnaryExpression)
194 PullApartExpression(((UnaryExpression)express).Operand);
195 if (express is MemberExpression)
196 {
197 //If the inside of the lambda is the access to x, we've hit the end of the chain.
198 // We should track by the fully scoped parameter name, but this is the first rev of doing this.
199 if (((MemberExpression)express).Expression is ParameterExpression)
200 {
201 ignores.Add(((MemberExpression)express).Member.Name);
202 }
203 else
204 {
205 //Otherwise there could be more parameters inside...
206 PullApartExpression(((MemberExpression)express).Expression);
207 }
208 }
209 }
210 }
211
212 [TestFixture]
213 public class PropertyCompareConstraintTest
214 {
215 public class HasInt
216 {
217 public int TheValue { get; set; }
218 }
219
220 [Test]
221 public void IntShouldMatch()
222 {
223 HasInt actual = new HasInt { TheValue = 5 };
224 HasInt expected = new HasInt { TheValue = 5 };
225 var constraint = Constraints.PropertyCompareConstraint(expected);
226
227 Assert.That(constraint.Matches(actual), Is.True);
228 }
229
230 [Test]
231 public void IntShouldNotMatch()
232 {
233 HasInt actual = new HasInt { TheValue = 5 };
234 HasInt expected = new HasInt { TheValue = 4 };
235 var constraint = Constraints.PropertyCompareConstraint(expected);
236
237 Assert.That(constraint.Matches(actual), Is.False);
238 }
239
240
241 [Test]
242 public void IntShouldIgnore()
243 {
244 HasInt actual = new HasInt { TheValue = 5 };
245 HasInt expected = new HasInt { TheValue = 4 };
246 var constraint = Constraints.PropertyCompareConstraint(expected).IgnoreProperty(x=>x.TheValue);
247
248 Assert.That(constraint.Matches(actual), Is.True);
249 }
250
251 [Test]
252 public void AssetShouldMatch()
253 {
254 UUID uuid1 = UUID.Random();
255 AssetBase actual = new AssetBase(uuid1, "asset one");
256 AssetBase expected = new AssetBase(uuid1, "asset one");
257
258 var constraint = Constraints.PropertyCompareConstraint(expected);
259
260 Assert.That(constraint.Matches(actual), Is.True);
261 }
262
263 [Test]
264 public void AssetShouldNotMatch()
265 {
266 UUID uuid1 = UUID.Random();
267 AssetBase actual = new AssetBase(uuid1, "asset one");
268 AssetBase expected = new AssetBase(UUID.Random(), "asset one");
269
270 var constraint = Constraints.PropertyCompareConstraint(expected);
271
272 Assert.That(constraint.Matches(actual), Is.False);
273 }
274
275 [Test]
276 public void AssetShouldNotMatch2()
277 {
278 UUID uuid1 = UUID.Random();
279 AssetBase actual = new AssetBase(uuid1, "asset one");
280 AssetBase expected = new AssetBase(uuid1, "asset two");
281
282 var constraint = Constraints.PropertyCompareConstraint(expected);
283
284 Assert.That(constraint.Matches(actual), Is.False);
285 }
286
287 [Test]
288 public void TestColors()
289 {
290 Color actual = Color.Red;
291 Color expected = Color.FromArgb(actual.A, actual.R, actual.G, actual.B);
292
293 var constraint = Constraints.PropertyCompareConstraint(expected);
294
295 Assert.That(constraint.Matches(actual), Is.True);
296 }
297 }
298} \ No newline at end of file
diff --git a/OpenSim/Data/Tests/ScrambleForTesting.cs b/OpenSim/Data/Tests/ScrambleForTesting.cs
new file mode 100644
index 0000000..c6e467f
--- /dev/null
+++ b/OpenSim/Data/Tests/ScrambleForTesting.cs
@@ -0,0 +1,102 @@
1using System;
2using System.Collections;
3using System.Reflection;
4using System.Text;
5using NUnit.Framework;
6using OpenMetaverse;
7using OpenSim.Framework;
8
9namespace OpenSim.Data.Tests
10{
11 public static class ScrambleForTesting
12 {
13 private static readonly Random random = new Random();
14 public static void Scramble(object obj)
15 {
16 PropertyInfo[] properties = obj.GetType().GetProperties();
17 foreach (var property in properties)
18 {
19 //Skip indexers of classes. We will assume that everything that has an indexer
20 // is also IEnumberable. May not always be true, but should be true normally.
21 if(property.GetIndexParameters().Length > 0)
22 continue;
23
24 RandomizeProperty(obj, property, null);
25 }
26 //Now if it implments IEnumberable, it's probably some kind of list, so we should randomize
27 // everything inside of it.
28 IEnumerable enumerable = obj as IEnumerable;
29 if(enumerable != null)
30 {
31 foreach (object value in enumerable)
32 {
33 Scramble(value);
34 }
35 }
36 }
37
38 private static void RandomizeProperty(object obj, PropertyInfo property, object[] index)
39 {
40 Type t = property.PropertyType;
41 if (!property.CanWrite)
42 return;
43 object value = property.GetValue(obj, index);
44 if (value == null)
45 return;
46
47 if (t == typeof (string))
48 property.SetValue(obj, RandomName(), index);
49 else if (t == typeof (UUID))
50 property.SetValue(obj, UUID.Random(), index);
51 else if (t == typeof (sbyte))
52 property.SetValue(obj, (sbyte)random.Next(sbyte.MinValue, sbyte.MaxValue), index);
53 else if (t == typeof (short))
54 property.SetValue(obj, (short)random.Next(short.MinValue, short.MaxValue), index);
55 else if (t == typeof (int))
56 property.SetValue(obj, random.Next(), index);
57 else if (t == typeof (long))
58 property.SetValue(obj, random.Next() * int.MaxValue, index);
59 else if (t == typeof (byte))
60 property.SetValue(obj, (byte)random.Next(byte.MinValue, byte.MaxValue), index);
61 else if (t == typeof (ushort))
62 property.SetValue(obj, (ushort)random.Next(ushort.MinValue, ushort.MaxValue), index);
63 else if (t == typeof (uint))
64 property.SetValue(obj, Convert.ToUInt32(random.Next()), index);
65 else if (t == typeof (ulong))
66 property.SetValue(obj, Convert.ToUInt64(random.Next()) * Convert.ToUInt64(UInt32.MaxValue), index);
67 else if (t == typeof (bool))
68 property.SetValue(obj, true, index);
69 else if (t == typeof (byte[]))
70 {
71 byte[] bytes = new byte[30];
72 random.NextBytes(bytes);
73 property.SetValue(obj, bytes, index);
74 }
75 else
76 Scramble(value);
77 }
78
79 private static string RandomName()
80 {
81 StringBuilder name = new StringBuilder();
82 int size = random.Next(5, 12);
83 for (int i = 0; i < size; i++)
84 {
85 char ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
86 name.Append(ch);
87 }
88 return name.ToString();
89 }
90 }
91
92 [TestFixture]
93 public class ScrableForTestingTest
94 {
95 [Test]
96 public void TestScramble()
97 {
98 AssetBase actual = new AssetBase(UUID.Random(), "asset one");
99 ScrambleForTesting.Scramble(actual);
100 }
101 }
102} \ No newline at end of file