public void InvokeInstanceMethodThrowsWhenInstanceIsNull() { MethodInvocationSpec spec = new MethodInvocationSpec(typeof(NonGenericClass), NonGenericClassEmptyMethodInfo, EmptyArray <KeyValuePair <ISlotInfo, object> > .Instance, NullConverter.Instance); Assert.Throws <ArgumentNullException>(delegate { spec.Invoke(null); }); }
public void FormatThrowsIfFormatterIsNull() { MethodInvocationSpec spec = new MethodInvocationSpec(typeof(NonGenericClass), NonGenericClassEmptyMethodInfo, EmptyArray <KeyValuePair <ISlotInfo, object> > .Instance, Mocks.Stub <IConverter>()); Assert.Throws <ArgumentNullException>(delegate { spec.Format("Foo", null); }); }
public void FormatStringIsJustTheEntityIfThereAreNoSlots() { Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>(); MethodInvocationSpec spec = new MethodInvocationSpec(typeof(NonGenericClass), NonGenericClassEmptyMethodInfo, slotValues, NullConverter.Instance); Assert.AreEqual("Foo", spec.Format("Foo", RuntimeAccessor.ServiceLocator.Resolve <IFormatter>())); }
public void InvokeInstanceMethodThrowsWhenInstanceIsOfWrongType() { GenericClass <int> instance = new GenericClass <int>(); Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>(); MethodInvocationSpec spec = new MethodInvocationSpec(typeof(NonGenericClass), NonGenericClassEmptyMethodInfo, slotValues, NullConverter.Instance); Assert.Throws <TargetException>(delegate { spec.Invoke(instance); }); }
public void InvokeThrowsUnwrappedException() { ITypeInfo type = Reflector.Wrap(typeof(NonGenericClass)); IMethodInfo method = type.GetMethod("StaticMethodThatThrows", PublicStatic); Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>(); MethodInvocationSpec spec = new MethodInvocationSpec(typeof(NonGenericClass), method, slotValues, NullConverter.Instance); Assert.Throws <InvalidOperationException>(delegate { spec.Invoke(null); }); }
public void FormatDescribesTheMethod() { ITypeInfo type = Reflector.Wrap(typeof(GenericClass <>)); IMethodInfo method = type.GetMethod("StaticMethod", PublicStatic); Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>(); slotValues.Add((IGenericParameterInfo)method.GenericArguments[0], typeof(int)); slotValues.Add(method.Parameters[0], 1); MethodInvocationSpec spec = new MethodInvocationSpec(typeof(GenericClass <int>), method, slotValues, NullConverter.Instance); Assert.AreEqual("Foo<int>(1)", spec.Format("Foo", RuntimeAccessor.ServiceLocator.Resolve <IFormatter>())); }
public void InvokeStaticMethodWithGenericMethodInstantiation() { ITypeInfo type = Reflector.Wrap(typeof(GenericClass <int>)); IMethodInfo method = Reflector.Wrap(type.GetMethod("StaticMethod", PublicStatic).Resolve(true).MakeGenericMethod(typeof(int))); Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>(); slotValues.Add(method.Parameters[0], 1); MethodInvocationSpec spec = new MethodInvocationSpec(typeof(GenericClass <int>), method, slotValues, NullConverter.Instance); spec.Invoke(null); Assert.AreEqual(typeof(int), GenericClass <int> .staticMethodTypeValue); Assert.AreEqual(1, GenericClass <int> .staticMethodParamValue); }
public void InvokeInstanceMethodWithGenericMethodOnGenericTypeDefn() { ITypeInfo type = Reflector.Wrap(typeof(GenericClass <>)); IMethodInfo method = type.GetMethod("InstanceMethod", PublicInstance); Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>(); slotValues.Add((IGenericParameterInfo)method.GenericArguments[0], typeof(int)); slotValues.Add(method.Parameters[0], 1); GenericClass <int> instance = new GenericClass <int>(); MethodInvocationSpec spec = new MethodInvocationSpec(typeof(GenericClass <int>), method, slotValues, NullConverter.Instance); spec.Invoke(instance); Assert.AreEqual(typeof(int), instance.instanceMethodTypeValue); Assert.AreEqual(1, instance.instanceMethodParamValue); }
/// <summary> /// Applies semantic actions to a test to estalish its runtime behavior. /// </summary> /// <remarks> /// <para> /// This method is called after <see cref="InitializeTest" />. /// </para> /// <para> /// The default behavior for a <see cref="TestMethodPatternAttribute" /> /// is to configure the test actions as follows: /// <list type="bullet"> /// <item><see cref="PatternTestInstanceActions.BeforeTestInstanceChain" />: Set the /// test step name, <see cref="PatternTestInstanceState.TestMethod" /> and /// <see cref="PatternTestInstanceState.TestArguments" /> based on any values bound /// to the test method's generic parameter and method parameter slots.</item> /// <item><see cref="PatternTestInstanceActions.ExecuteTestInstanceChain" />: Invoke the method.</item> /// </list> /// </para> /// <para> /// You can override this method to change the semantics as required. /// </para> /// </remarks> /// <param name="testBuilder">The test builder.</param> /// <param name="method">The test method.</param> protected virtual void SetTestSemantics(ITestBuilder testBuilder, IMethodInfo method) { testBuilder.TestInstanceActions.BeforeTestInstanceChain.After( delegate(PatternTestInstanceState testInstanceState) { MethodInvocationSpec spec = testInstanceState.GetTestMethodInvocationSpec(method); testInstanceState.TestMethod = spec.ResolvedMethod; testInstanceState.TestArguments = spec.ResolvedArguments; if (!testInstanceState.IsReusingPrimaryTestStep) { testInstanceState.NameBase = spec.Format(testInstanceState.NameBase, testInstanceState.Formatter); } }); testBuilder.TestInstanceActions.ExecuteTestInstanceChain.After(Execute); }
public void SpecPropertiesDescribeTheMethod() { ITypeInfo type = Reflector.Wrap(typeof(GenericClass <>)); IMethodInfo method = type.GetMethod("StaticMethod", PublicStatic); Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>(); slotValues.Add((IGenericParameterInfo)method.GenericArguments[0], typeof(int)); slotValues.Add(method.Parameters[0], 1); MethodInvocationSpec spec = new MethodInvocationSpec(typeof(GenericClass <int>), method, slotValues, NullConverter.Instance); Assert.AreEqual(typeof(GenericClass <int>), spec.ResolvedType); Assert.AreSame(method, spec.Method); Assert.AreSame(slotValues, spec.SlotValues); Assert.AreSame(NullConverter.Instance, spec.Converter); Assert.AreEqual("Void StaticMethod[Int32](Int32)", spec.ResolvedMethod.ToString()); Assert.AreElementsEqual(new object[] { 1 }, spec.ResolvedArguments); }
public object InvokeFixtureMethod(IMethodInfo method, IEnumerable <KeyValuePair <ISlotInfo, object> > slotValues) { if (method == null) { throw new ArgumentNullException("method"); } if (slotValues == null) { throw new ArgumentNullException("slotValues"); } if (fixtureType == null) { throw new InvalidOperationException("This method cannot be used when FixtureType is null."); } var spec = new MethodInvocationSpec(fixtureType, method, slotValues, Converter); return(spec.Invoke(fixtureInstance)); }