/// <summary> /// Create a new TestMonkeyException /// </summary> /// <remarks> /// Only use this exception if no suitable alternative exists - eg: use InvalidArgumentException if it is a better fit /// </remarks> public static TestMonkeyException Create(Exception innerException, string format, params object[] args) { string message = string.Format(format, args); TestMonkeyException exception = new TestMonkeyException(message, innerException); return(exception); }
private void WalkCallback(Type targetType, object targetObject, object mirrorObject, MemberInfo fieldInfo, string objectName) { if (fieldInfo == null) { return; } if (fieldInfo.Name == _propertyName) { bool exceptionSwallowedTarget; object targetPropertyValue = InvokeMember( targetType , targetObject , fieldInfo , Options.BindingFlags , false , out exceptionSwallowedTarget); T castValue = (T)targetPropertyValue; try { if (Options.LogToConsole) { string message = string.Format("ObjectWalker callback: {0}.{1}", objectName, fieldInfo.Name); Console.WriteLine(message); } _assertWalkerCallback(castValue); } catch (Exception e) { throw TestMonkeyException.Create("AssertWalker callback failed on " + objectName + "." + fieldInfo.Name, e); } } }
/// <summary> /// Someone wrote a unit test with ReferenceEquals assertions and so wrote something pretty useless. /// This prevents the accident /// </summary> public static new bool ReferenceEquals(object objectA, object objectB) { throw TestMonkeyException.Create("You probably meant to call ResourceEquals not ReferenceEquals. Try again cowboy"); }
private void WalkCollection( ReflectionWalkerCallback callback , Type targetType , object targetObject , object mirrorObject , string objectName) { if (targetObject is string) { callback(targetType, targetObject, mirrorObject, null, objectName); return; } IEnumerable targetAsEnumerable = targetObject as IEnumerable; IList targetAsList = targetObject as IList; FieldInfoCollection fields = GetWalkablePropertiesAndFields(targetType); MemberInfo countField = fields.Find(f => f.Name == "Count"); MemberInfo lengthField = fields.Find(f => f.Name == "Length"); if (countField != null) { callback(targetType, targetObject, mirrorObject, countField, objectName); } if (lengthField != null) { callback(targetType, targetObject, mirrorObject, lengthField, objectName); } if (targetAsList != null) { for (int i = 0; i < targetAsList.Count; i++) { object innerTarget = targetAsList[i]; if (innerTarget == null) { Console.WriteLine("CodeGenWalker skipping list item {0} since the inner target is null", i); continue; } bool isSimpleType = IsSimpleType(innerTarget.GetType()); object innerMirrorTarget = null; if (mirrorObject != null) { innerMirrorTarget = ((IList)mirrorObject)[i]; } string objectNameWithIndexer = string.Format("{0}[{1}]", objectName, i); if (IsOfType(innerTarget, Options.UpcastTypes)) { objectNameWithIndexer = GetUpCastTargetName(innerTarget, objectNameWithIndexer); } if (isSimpleType) { // THIS WILL ALWAYS BREAK MOFO callback(targetType, innerTarget, innerMirrorTarget, null, objectNameWithIndexer); } else { InnerWalk(callback, innerTarget, innerMirrorTarget, objectNameWithIndexer); } } } else if (targetAsEnumerable != null) { throw TestMonkeyException.Create("Object Inspector doesn't work with pure IEnumerables: " + objectName); } }