Browse Source

Improved XML printer in debugger unit tests - "Get" methods are invoked, there is an explicit ignore list.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@5109 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
David Srbecký 16 years ago
parent
commit
24ba514697
  1. 116
      src/AddIns/Misc/Debugger/Debugger.Tests/Project/Src/DebuggerTestsBase.cs

116
src/AddIns/Misc/Debugger/Debugger.Tests/Project/Src/DebuggerTestsBase.cs

@ -59,6 +59,40 @@ namespace Debugger.Tests
testName = null; testName = null;
expandProperties = new List<string>(); expandProperties = new List<string>();
ignoreProperties = new List<string>();
ignoreProperties.AddRange(new string [] {
"*.GetType",
"*.GetHashCode",
"*.GetEnumerator",
"MemberInfo.MemberType",
"Type.DeclaringType",
"Type.IsAbstract",
"Type.IsAnsiClass",
"Type.IsAutoLayout",
"Type.IsLayoutSequential",
"Type.IsPublic",
"Type.IsNestedPublic",
"Type.IsSealed",
"Type.IsSerializable",
"Type.Name",
"Type.Namespace",
"Type.UnderlyingSystemType",
"MethodBase.CallingConvention",
"MethodBase.GetMethodBody",
"MethodBase.GetMethodImplementationFlags",
"MethodBase.GetParameters",
"MethodBase.IsHideBySig",
"MethodBase.IsPublic",
"MethodBase.IsVirtual",
"MethodBase.IsSpecialName",
"MethodBase.Name",
"MethodBase.ReturnParameter",
"MethodBase.ParameterCount",
"PropertyInfo.CanRead",
"PropertyInfo.CanWrite",
"PropertyInfo.GetGetMethod",
"PropertyInfo.GetSetMethod",
});
testDoc = new XmlDocument(); testDoc = new XmlDocument();
testDoc.AppendChild(testDoc.CreateXmlDeclaration("1.0","utf-8",null)); testDoc.AppendChild(testDoc.CreateXmlDeclaration("1.0","utf-8",null));
@ -211,20 +245,38 @@ namespace Debugger.Tests
} }
List<string> expandProperties; List<string> expandProperties;
List<string> ignoreProperties;
protected void ExpandProperties(params string[] props) protected void ExpandProperties(params string[] props)
{ {
expandProperties = new List<string>(props); expandProperties = new List<string>(props);
} }
bool ShouldExpandProperty(System.Reflection.PropertyInfo propertyInfo) bool ListContains(List<string> list, MemberInfo memberInfo)
{ {
return Type declaringType = memberInfo.DeclaringType;
(propertyInfo.GetCustomAttributes(typeof(Debugger.Tests.ExpandAttribute), true).Length > 0) || while(declaringType != null) {
expandProperties.Contains(propertyInfo.Name) || if (list.Contains(declaringType.Name + "." + memberInfo.Name) ||
expandProperties.Contains("*") || list.Contains(declaringType.Name + ".*") ||
expandProperties.Contains(propertyInfo.DeclaringType.Name + "." + propertyInfo.Name) || list.Contains("*." + memberInfo.Name) ||
expandProperties.Contains(propertyInfo.DeclaringType.Name + ".*"); list.Contains("*.*") ||
list.Contains("*"))
return true;
declaringType = declaringType.BaseType;
}
return false;
}
bool ExpandProperty(MemberInfo memberInfo)
{
return (memberInfo.IsDefined(typeof(Debugger.Tests.ExpandAttribute), true)) ||
ListContains(expandProperties, memberInfo);
}
bool IgnoreProperty(MemberInfo memberInfo)
{
return (memberInfo.IsDefined(typeof(Debugger.Tests.IgnoreAttribute), true)) ||
ListContains(ignoreProperties, memberInfo);
} }
public void Serialize(XmlElement container, object obj, int maxDepth, List<object> parents) public void Serialize(XmlElement container, object obj, int maxDepth, List<object> parents)
@ -262,21 +314,39 @@ namespace Debugger.Tests
container = newContainer; container = newContainer;
} }
List<SRPropertyInfo> properties = new List<SRPropertyInfo>(); List<MemberInfo> members = new List<MemberInfo>();
properties.AddRange(type.GetProperties()); members.AddRange(type.GetMembers());
properties.Sort(delegate(SRPropertyInfo a, SRPropertyInfo b) { return a.Name.CompareTo(b.Name);}); members.Sort(delegate(MemberInfo a, MemberInfo b) { return a.Name.CompareTo(b.Name);});
foreach(SRPropertyInfo property in properties) { foreach(MemberInfo member in members) {
if (type.BaseType == typeof(Array)) continue; if (type.BaseType == typeof(Array)) continue;
if (property.GetGetMethod() == null) continue;
if (property.GetGetMethod().GetParameters().Length > 0) continue;
if (property.GetCustomAttributes(typeof(Debugger.Tests.IgnoreAttribute), true).Length > 0) continue;
MethodInfo method;
object val; object val;
PropertyInfo propertyInfo = member as PropertyInfo;
MethodInfo methodInfo = member as MethodInfo;
if (propertyInfo != null) {
if (propertyInfo.GetGetMethod() == null) continue;
method = propertyInfo.GetGetMethod();
} else if (methodInfo != null) {
if (!methodInfo.Name.StartsWith("Get")) continue;
method = methodInfo;
} else {
continue;
}
if (method.GetParameters().Length > 0) continue;
if (IgnoreProperty(member)) continue;
try { try {
val = property.GetValue(obj, new object[] {}); val = method.Invoke(obj, new object[] {});
} catch (System.Exception e) { } catch (System.Exception e) {
while(e.InnerException != null) e = e.InnerException; while(e.InnerException != null) e = e.InnerException;
if (e is NotImplementedException || e is NotSupportedException)
continue;
if (type.IsDefined(typeof(Debugger.Tests.IgnoreOnExceptionAttribute), true) ||
member.IsDefined(typeof(Debugger.Tests.IgnoreOnExceptionAttribute), true))
continue;
val = "{Exception: " + e.Message + "}"; val = "{Exception: " + e.Message + "}";
} }
@ -286,18 +356,18 @@ namespace Debugger.Tests
vals.Add(o.ToString()); vals.Add(o.ToString());
} }
if (vals.Count != 0) { if (vals.Count != 0) {
container.SetAttribute(property.Name, "{" + string.Join(", ", vals.ToArray()) + "}"); container.SetAttribute(member.Name, "{" + string.Join(", ", vals.ToArray()) + "}");
} }
} else { } else {
bool isDefault = false; bool isDefault = false;
if (property.PropertyType == typeof(bool)) { if (method.ReturnType == typeof(bool)) {
isDefault = false.Equals(val); isDefault = false.Equals(val);
} else if (property.PropertyType == typeof(string)) { } else if (method.ReturnType == typeof(string)) {
isDefault = val == null; isDefault = val == null;
} else if (property.PropertyType == typeof(int)) { } else if (method.ReturnType == typeof(int)) {
isDefault = 0.Equals(val); isDefault = 0.Equals(val);
} else if (property.PropertyType == typeof(uint)) { } else if (method.ReturnType == typeof(uint)) {
isDefault = ((uint)0).Equals(val); isDefault = ((uint)0).Equals(val);
} else { } else {
isDefault = val == null; isDefault = val == null;
@ -305,13 +375,13 @@ namespace Debugger.Tests
if (val == null) val = "null"; if (val == null) val = "null";
if (!isDefault) { if (!isDefault) {
container.SetAttribute(property.Name, val.ToString()); container.SetAttribute(member.Name, val.ToString());
} }
} }
if (ShouldExpandProperty(property)) { if (ExpandProperty(member)) {
XmlElement propertyNode = doc.CreateElement(property.Name); XmlElement propertyNode = doc.CreateElement(member.Name);
container.AppendChild(propertyNode); container.AppendChild(propertyNode);
Serialize(propertyNode, val, maxDepth - 1, parents); Serialize(propertyNode, val, maxDepth - 1, parents);
} }

Loading…
Cancel
Save