/// <summary> /// Runs the operations for a type, that inherit one of the Facade classes. /// </summary> /// <param name="testItem">CspecTestItem that describes the status of the running test</param> /// <param name="fields">class fields</param> /// <param name="description">the @it operation</param> /// <param name="obj">object that represents the spec</param> /// <param name="describedObject">the object that's beeing described</param> private void RunOperation(CSpecTestItem testItem, FieldInfo[] fields, Action<string> description, object obj, object describedObject) { MethodInfo beforeOp = null; MethodInfo afterOp = null; ProxyTrace trace = null; Type objType = obj.GetType(); //Now call the operation methods. foreach (var field in fields.Where(x => x.FieldType.Name == "DescribeAll" || x.FieldType.Name == "Describe" )) { Delegate testRunner = (Delegate)field.GetValue(obj); testItem.Name = field.Name; if (BeforeOperation != null) BeforeOperation(testItem); beforeOp = objType.GetMethod("BeforeOperation", BindingFlags.NonPublic | BindingFlags.Instance); afterOp = objType.GetMethod("AfterOperation", BindingFlags.NonPublic | BindingFlags.Instance); if (beforeOp != null) beforeOp.Invoke(obj, null); try { if (describedObject != null && describedObject.GetType().GetInterfaces().Length != 0) { trace = (ProxyTrace)describedObject.GetType().GetField("trace").GetValue(describedObject); trace = new ProxyTrace(); describedObject.GetType().GetField("trace").SetValue(describedObject, trace); //put to lookup //NOTE: when doing multithreaded runners the entire section of this code //should be locked by monitor //NOTE: Moved to CSpecFacade constructor, it has much more sence there //CurrentDescribedObject = describedObject; } if (field.FieldType.Name == "DescribeAll") { if (describedObject != null) testRunner.DynamicInvoke(description, describedObject); else testRunner.DynamicInvoke(description); } else { ItAttribute it = (ItAttribute)field.GetCustomAttributes(typeof(ItAttribute), false)[0]; Console.WriteLine(it.Description); testRunner.DynamicInvoke(null); } testItem.Results = "Test Passed!"; testItem.TestSucceed = true; if (AfterOperation != null) AfterOperation(testItem); Passed++; } catch (System.Exception ex) { HandleRunnerException(ex, testItem, trace); } if (afterOp != null) afterOp.Invoke(obj, null); } }
private static string TransField(FieldInfo[] fieldInfos, string prop) { //FieldInfo fi = type.GetField("fn_" + prop); FieldInfo fi = fieldInfos.Where(x => x.Name == fn_ + prop).FirstOrDefault(); if (fi != null) { return (string)fi.GetValue(null); } return string.Empty; }