Esempio n. 1
0
 public void Interrupt(IInterrupt interrupt)
 {
     if (interrupt != null)
     {
         interrupt.Handle(this.Caster);
     }
     End();
 }
Esempio n. 2
0
        public void InstructionExecutor_Execute_WithInterrupts()
        {
            InstructionExecutionBody <G> interruptableExecutionBody   = Mock.Of <InstructionExecutionBody <G> >();
            InstructionExecutionBody <G> uninterruptableExecutionBody = Mock.Of <InstructionExecutionBody <G> >();
            Instruction <G> interruptableInstruction = TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, true, interruptableExecutionBody);

            Instruction <G> uninterruptableInstruction = TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, uninterruptableExecutionBody);

            Group <G> group = new Group <G>
            {
                Instructions = new[]
                {
                    uninterruptableInstruction,
                    uninterruptableInstruction,
                    uninterruptableInstruction,
                    interruptableInstruction,
                    uninterruptableInstruction,
                    uninterruptableInstruction,
                    uninterruptableInstruction
                }
            };
            G groupState     = Mock.Of <G>(m => m.Group == group);
            E executionState = new ExecutionState <G>(groupState);
            IInterrupt <G> inactiveInterrupt = Mock.Of <IInterrupt <G> >(m => m.Intersects(executionState) == false);

            IInterrupt <G>[] activeInterrupts = new IInterrupt <G>[]
            {
                Mock.Of <IInterrupt <G> >(m => m.Intersects(executionState) == true),
                Mock.Of <IInterrupt <G> >(m => m.Intersects(executionState) == true),
                Mock.Of <IInterrupt <G> >(m => m.Intersects(executionState) == true),
                Mock.Of <IInterrupt <G> >(m => m.Intersects(executionState) == true)
            };
            executionState.Interrupts.Add(inactiveInterrupt);
            executionState.Interrupts.Add(activeInterrupts[0]);
            executionState.Interrupts.Add(inactiveInterrupt);
            executionState.Interrupts.Add(activeInterrupts[1]);
            InterruptedHandler <G> interruptedHandler = Mock.Of <InterruptedHandler <G> >();

            InstructionExecutor <G> sut = new InstructionExecutor <G>();

            sut.Interrupts.Add(inactiveInterrupt);
            sut.Interrupts.Add(activeInterrupts[2]);
            sut.Interrupts.Add(inactiveInterrupt);
            sut.Interrupts.Add(activeInterrupts[3]);
            sut.Interrupted += interruptedHandler;
            Mock.Get(interruptedHandler).Setup(m => m(sut, It.IsAny <InterruptedEventArgs <G> >())).Callback((InstructionExecutor <G> sender, InterruptedEventArgs <G> args) =>
            {
                Assert.Equal(executionState, args.ExecutionState);
                Assert.Equal(activeInterrupts.OrderBy(i => i.GetHashCode()), args.Interrupts.OrderBy(i => i.GetHashCode()));
            });

            sut.Execute(executionState);

            Assert.Equal(7, executionState.InstructionIndex);
            Mock.Get(uninterruptableExecutionBody).Verify(m => m(sut, executionState, It.IsAny <object[]>(), It.IsAny <StackList <ValuePointer <G> > >(), It.IsAny <StackList <StackValuePointer <G> > >()), Times.Exactly(6));
            Mock.Get(interruptableExecutionBody).Verify(m => m(sut, executionState, It.IsAny <object[]>(), It.IsAny <StackList <ValuePointer <G> > >(), It.IsAny <StackList <StackValuePointer <G> > >()), Times.Once);
            Mock.Get(interruptedHandler).Verify(m => m(sut, It.IsAny <InterruptedEventArgs <G> >()), Times.Once);
        }