private ValidationInfo(string name, Type type, object expectedValue, IAsserter asserter) { _name = name; _type = type; _expectedValue = expectedValue; _asserter = asserter; }
private static void InvokeMethod(IAsserter asserter, MethodInfo method, Type type, object optionalInstance, object[] args) { var thrownExceptions = new List <Exception>(); try { var taskResult = method.Invoke(optionalInstance, args) as Task; if (taskResult != null) { taskResult.Wait(); } } catch (AggregateException aggregateException) { thrownExceptions.AddRange(aggregateException.InnerExceptions); } catch (TargetInvocationException targetInvocationException) { thrownExceptions.Add(targetInvocationException.InnerException); } catch (Exception genericException) { thrownExceptions.Add(genericException); } for (int i = 0; i < thrownExceptions.Count; i++) { if (thrownExceptions[i] == null) { continue; } asserter.AssertMethodException(thrownExceptions[i], type, method); } }
public void SetUp() { _asserter = MockRepository.GenerateStub <IAsserter>(); _asserter.Stub(stub => stub.NotNull(null, null, null)).IgnoreArguments().WhenCalled( invocation => { if (invocation.Arguments[0] == null) { throw new Exception(string.Format((string)invocation.Arguments[1], (object[])invocation.Arguments[2])); } }); _asserter.Stub(stub => stub.IsNull(null, null, null)).IgnoreArguments().WhenCalled( invocation => { if (invocation.Arguments[0] != null) { throw new Exception(string.Format((string)invocation.Arguments[1], (object[])invocation.Arguments[2])); } }); _asserter.Stub(stub => stub.AreEqual(null, null, null, null)).IgnoreArguments().WhenCalled( invocation => { if (invocation.Arguments[0] != null) { if (!invocation.Arguments[0].Equals(invocation.Arguments[1])) { throw new Exception(string.Format((string)invocation.Arguments[2], (object[])invocation.Arguments[3])); } } else if (invocation.Arguments[1] != null) { throw new Exception(string.Format((string)invocation.Arguments[2], (object[])invocation.Arguments[3])); } }); _asserter.Stub(stub => stub.GreaterThan(null, null, null, null)).IgnoreArguments().WhenCalled( invocation => { if (invocation.Arguments[0] != null) { if (((IComparable)invocation.Arguments[0]).CompareTo(invocation.Arguments[1]) <= 0) { throw new Exception(string.Format((string)invocation.Arguments[2], (object[])invocation.Arguments[3])); } } }); _asserter.Stub(stub => stub.IsTrue(true, null, new object[3])).IgnoreArguments().WhenCalled( invocation => { if (!((bool)invocation.Arguments[0])) { throw new Exception(string.Format((string)invocation.Arguments[1], (object[])invocation.Arguments[2])); } }) ; _htmlHelper = new ConcreteHtmlHelper(_asserter); _htmlHelper.Writer.Write(c_document); }
/// <summary> /// Runs the test as an application with GUI /// </summary> /// <param name="data"></param> /// <param name="asserter"></param> /// <returns></returns> public IWorld CreateSelfTestInCommandLineContext(CommandLineData data, IAsserter asserter) { var holder = new NetworkServerData(); holder.Port = DefaultPort; holder.LoadingData = new LoadingData { AssemblyName = data.Unnamed[0], Level = data.Unnamed[1] }; var test = GetTest(holder.LoadingData, data.Unnamed[3]); new Action<ICvarcTest, IAsserter, NetworkServerData>(SelfTestClientThread).BeginInvoke(test, asserter, holder, null, null); var proposal = SettingsProposal.FromCommandLineData(data); CreateSelfTestServer(holder, proposal); return holder.World; }
/// <summary> /// Helper method to get the asserter to be used for a test case. /// Asserters can be specified at a class, method or parameter level. /// The asserter specified at a specific level overrides the one specified at a more generic level. /// </summary> /// <param name="testFixture"></param> /// <param name="mInfo"></param> /// <param name="pInfo"></param> /// <returns>Returns the asserter applicable for the specified parameter</returns> private static IAsserter GetOutputAsserter(object testFixture, MethodInfo mInfo, ParameterInfo pInfo) { IAsserter outputAsserter = null; object[] classAttributes = testFixture.GetType().GetCustomAttributes(false); for (int i = 0; i < classAttributes.Length; ++i) { if (null != classAttributes[i].GetType().GetInterface("IAsserter", true)) { outputAsserter = (IAsserter)classAttributes[i]; break; } } object[] methodAttributes = mInfo.GetCustomAttributes(false); for (int i = 0; i < methodAttributes.Length; ++i) { //Use the class asserter if a method level asserter is not assigned. if (null != methodAttributes[i].GetType().GetInterface("IAsserter", true)) { outputAsserter = (IAsserter)methodAttributes[i]; break; } } object[] paramAttributes = pInfo.GetCustomAttributes(false); for (int i = 0; i < paramAttributes.Length; ++i) { if (null != paramAttributes[i].GetType().GetInterface("IAsserter", false)) { outputAsserter = (IAsserter)paramAttributes[i]; break; } } return(outputAsserter); }
private ValidationInfo(string name, Type type, IAsserter asserter) { _name = name; _type = type; _asserter = asserter; }
static public void DoAssert( IAsserter asserter ) { Assert.IncrementAssertCount(); if ( !asserter.Test() ) throw new AssertionException( asserter.Message ); }
/// <summary> /// Runs the test as a Unit test. /// </summary> /// <param name="assemblyName"></param> /// <param name="level"></param> /// <param name="testName"></param> /// <param name="asserter"></param> public void RunSelfTestInVSContext(string assemblyName, string level, string testName, IAsserter asserter) { var holder = new NetworkServerData(); holder.Port = DefaultPort; holder.LoadingData = new LoadingData { AssemblyName = assemblyName, Level = level }; var test = GetTest(holder.LoadingData, testName); var thread = new Thread(() => { var proposal = new SettingsProposal { SpeedUp = true }; CreateSelfTestServer(holder, proposal); holder.World.RunActively(1); holder.Close(); }) { IsBackground = true }; thread.Start(); try { SelfTestClientThread(test, asserter, holder); } catch (Exception e) { throw e; } finally { holder.Close(); } }
/// <summary> /// The entry point for a client that performs a test /// </summary> /// <param name="test"></param> /// <param name="asserter"></param> /// <param name="holder"></param> void SelfTestClientThread(ICvarcTest test, IAsserter asserter, NetworkServerData holder) { holder.WaitForServer(); test.Run(holder, asserter); }
private MockMethodBase(string name, ICounter counter, IAsserter asserter) { _name = name; _invokedCounter = counter; _asserter = asserter; }
public ConcreteHtmlHelper(IAsserter asserter) : base(asserter.AreEqual, asserter.GreaterThan, asserter.NotNull, asserter.IsNull, asserter.IsTrue) { }
/// <summary> /// This is the method used to invoke a test method for a specific scenario. /// </summary> /// <param name="methodName">The test method to execute. This must be a private instance method /// in the TestFixture class.</param> /// <param name="scenarioName">The test scenario.</param> /// <param name="testFixture">The class containing the test method.</param> /// <remarks> /// this method is called from an NUnit test case and does the following: /// <list type="bullet"> /// <item> /// Calls the method specified by the <c>methodName</c> parameter; /// passing in all input parameters by using the concrete implementation of <see cref="IDataProvider"/> configured for each parameter. /// </item> /// <item> /// Captures the actual output returned from the test method and /// compares it with the expected output using the concrete implementation of <see cref="IAsserter"/> configured for each return value /// and out/ref parameter. /// </item> /// </list> /// </remarks> public static void Invoke(string methodName, string scenarioName, Object testFixture) { MethodInfo mInfo = testFixture.GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase); ParameterInfo[] pInfos = mInfo.GetParameters(); ParameterInfo returnParamInfo = mInfo.ReturnParameter; ArrayList allParameters = new ArrayList(); List <ParameterInfo> outParameters = new List <ParameterInfo>(); foreach (ParameterInfo pInfo in pInfos) { Object parameter = null; //Try to load a parameter //only if it is an input parameter. //Otherwise, just add the parameterInfo for that parameter //into an array for assertion later. if (pInfo.IsOut) { outParameters.Add(pInfo); } else { IDataProvider dataProvider = GetDataProvider(testFixture, mInfo, pInfo); if (null != dataProvider) { parameter = dataProvider.GetData(pInfo, methodName, scenarioName, testFixture); } } //Add the parameter to the array to be used to call the method //irrespective of whether or not it is an out parameter. //For out parameters, a "null" is added in the array. //This is replaced by an actual object after the method is invoked. allParameters.Add(parameter); } //Call the test method. try { Object[] parameters = allParameters.ToArray(); Object returnObj = mInfo.Invoke(testFixture, parameters); //Save the returned value //and call the corresponding assertion only if the return type is not void. if (returnParamInfo.ParameterType.Name != "Void") { IAsserter outputAsserter = GetOutputAsserter(testFixture, mInfo, returnParamInfo); if (null != outputAsserter) { outputAsserter.AssertOutput(returnObj, returnParamInfo, methodName, scenarioName, testFixture); } } //Save the remaining out parameters and //call the corresponding assertions. foreach (ParameterInfo outPInfo in outParameters) { object outParameter = parameters[outPInfo.Position]; IAsserter outputAsserter = GetOutputAsserter(testFixture, mInfo, outPInfo); if (null != outputAsserter) { outputAsserter.AssertOutput(outParameter, outPInfo, methodName, scenarioName, testFixture); } } } catch (TargetInvocationException e) { throw e.InnerException; } }