diff options
author | Adam Frisby | 2009-08-16 03:50:12 +1000 |
---|---|---|
committer | Adam Frisby | 2009-08-16 03:50:12 +1000 |
commit | 8b6d79aa3c1ed6630933bafc1122aa78b8268908 (patch) | |
tree | d956a4da2248b42fdb93d90c7b1c57ba8414732d /OpenSim/Data/Tests/PropertyCompareConstraint.cs | |
parent | * Beginnings of a Security Credential system in MRM. This will eventually lea... (diff) | |
parent | * whoops, missing a / (diff) | |
download | opensim-SC_OLD-8b6d79aa3c1ed6630933bafc1122aa78b8268908.zip opensim-SC_OLD-8b6d79aa3c1ed6630933bafc1122aa78b8268908.tar.gz opensim-SC_OLD-8b6d79aa3c1ed6630933bafc1122aa78b8268908.tar.bz2 opensim-SC_OLD-8b6d79aa3c1ed6630933bafc1122aa78b8268908.tar.xz |
Merge branch 'master' of ssh://opensimulator.org/var/git/opensim
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Data/Tests/PropertyCompareConstraint.cs | 298 |
1 files changed, 298 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 @@ | |||
1 | using System; | ||
2 | using System.Collections; | ||
3 | using System.Collections.Generic; | ||
4 | using System.Drawing; | ||
5 | using System.Linq; | ||
6 | using System.Linq.Expressions; | ||
7 | using System.Reflection; | ||
8 | using NUnit.Framework; | ||
9 | using NUnit.Framework.Constraints; | ||
10 | using NUnit.Framework.SyntaxHelpers; | ||
11 | using OpenMetaverse; | ||
12 | using OpenSim.Framework; | ||
13 | |||
14 | namespace 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 | ||