public void ShouldThrowExceptionOnInitializationIfCalledWithTheSameMethodCallId() { const int MethodCallId = 123; TestInitializationContext context = new TestInitializationContext(null, typeof(string).GetMethod("Intern"), new object[0]); this.actionDiscoverer.GetInitializationActions(context).Returns(new[] { new InitializationAction("Action1"), new InitializationAction("Action2") }); this.manager.Initialize(MethodCallId, context); Assert.ThrowsDelegate action = () => this.manager.Initialize(MethodCallId, context); Assert.Throws <InvalidOperationException>(action); }
public void ShouldSetInitializationContextIfInitializationHandlerImplementsIInitializationContextAware() { InitializationActionExecutor executor = new InitializationActionExecutor(); TestInitializationContext context = new TestInitializationContext(null, typeof(string).GetMethod("Intern"), new object[0]); InitializationAction action = new InitializationAction("Action") { State = new InitializationHandler(typeof(InitializationContextAwareInitializer), new object[0]), Context = context }; InitializationContextAwareInitializer.InitializationContext = null; executor.ExecuteInitializationForAction(action); Assert.Equal(context, InitializationContextAwareInitializer.InitializationContext); }
public void ShouldDiscoverAndExecuteInitializersInOrderOnInitialization() { const int MethodCallId = 123; TestInitializationContext context = new TestInitializationContext(null, typeof(string).GetMethod("Intern"), new object[0]); this.actionDiscoverer.GetInitializationActions(context).Returns(new[] { new InitializationAction("Action1"), new InitializationAction("Action2") }); this.manager.Initialize(MethodCallId, context); Received.InOrder( () => { this.actionExecutor.ExecuteInitializationForAction(Arg.Is <InitializationAction>(action => action.Id == "Action1")); this.actionExecutor.ExecuteInitializationForAction(Arg.Is <InitializationAction>(action => action.Id == "Action2")); }); }
/// <summary> /// Sets initialization context. /// </summary> /// <param name="context">The context.</param> public void SetInitializationContext(object context) { TestInitializationContext testInitializationContextCandidate = context as TestInitializationContext; if (testInitializationContextCandidate == null) { throw new NotSupportedException(string.Format("Only contexts derived from '{0}' are supported.", typeof(TestInitializationContext).AssemblyQualifiedName)); } ParameterInfo[] parameters = testInitializationContextCandidate.Method.GetParameters(); for (int index = 0; index < testInitializationContextCandidate.Arguments.Length; ++index) { if (this.NeedToProvideValue(testInitializationContextCandidate.Arguments[index], parameters[index])) { testInitializationContextCandidate.Arguments[index] = this.ResolveValue(testInitializationContextCandidate.Arguments[index], parameters[index]); } } }
public void ShouldEnumerateAndSubstituteValuesForAppropriateParameters() { Action <IComparable, IEnumerable, IDisposable> testDelegate = this.TestMethod; object[] intactParameter = new object[0]; object[] arguments = { null, intactParameter, null }; TestInitializationContext context = new TestInitializationContext(testDelegate.Target, testDelegate.Method, arguments); this.provider.SetInitializationContext(context); Assert.Equal(5, this.provider.ReceivedCalls().Count()); this.provider.Received().TestNeedToProvideValue(Arg.Is <object>(obj => obj == null), Arg.Is <ParameterInfo>(parameter => parameter.Position == 0)); this.provider.Received().TestNeedToProvideValue(Arg.Is <object[]>(obj => obj.Length == 0), Arg.Is <ParameterInfo>(parameter => parameter.Position == 1)); this.provider.Received().TestNeedToProvideValue(Arg.Is <object>(obj => obj == null), Arg.Is <ParameterInfo>(parameter => parameter.Position == 2)); this.provider.Received().TestResolveValue(Arg.Is <object>(obj => obj == null), Arg.Is <ParameterInfo>(parameter => parameter.Position == 0)); this.provider.DidNotReceive().TestResolveValue(Arg.Is <object[]>(obj => obj.Length == 0), Arg.Is <ParameterInfo>(parameter => parameter.Position == 1)); this.provider.Received().TestResolveValue(Arg.Is <object>(obj => obj == null), Arg.Is <ParameterInfo>(parameter => parameter.Position == 2)); }
public void ShouldDiscoverActionsByInitializationHandlerAttributes() { const string Argument = "argument"; TestInitializationActionDiscoverer actionDiscoverer = new TestInitializationActionDiscoverer(); Test test = new Test(); IEnumerable <InitializationAction> actions = actionDiscoverer.GetInitializationActions(new TestInitializationContext(test, typeof(Test).GetMethod("TestMethod"), new object[] { Argument })).ToArray(); Assert.Equal(1, actions.Count()); TestInitializationContext expectedInitializationContext = new TestInitializationContext(test, typeof(Test).GetMethod("TestMethod"), new object[] { Argument }); Assert.Equal(typeof(InitializationHandler1).AssemblyQualifiedName, actions.Single().Id); Assert.IsType <InitializationHandler>(actions.Single().State); Assert.Equal(typeof(InitializationHandler1), ((InitializationHandler)actions.Single().State).Type); Assert.Equal(new object[] { "parameter" }, ((InitializationHandler)actions.Single().State).Arguments); Assert.Equal(expectedInitializationContext.Instance, ((TestInitializationContext)actions.Single().Context).Instance); Assert.Equal(expectedInitializationContext.Method, ((TestInitializationContext)actions.Single().Context).Method); Assert.Equal(expectedInitializationContext.Arguments, ((TestInitializationContext)actions.Single().Context).Arguments); }