public void DefaultActionValue_Add_MapCollection() { int location = 10; G groupState = Mock.Of <G>(); DefaultValueProvider <G> valueProvider = new DefaultValueProvider <G>(); Dictionary <IValuable <G>, ValuePointer <G> > collectionValues = new Dictionary <IValuable <G>, ValuePointer <G> >() { { new DefaultIntValue <G>(3), new ValuePointer <G> { Value = Mock.Of <IValuable <G> >() } }, { new DefaultIntValue <G>(4), new ValuePointer <G> { Value = Mock.Of <IValuable <G> >() } }, { new DefaultIntValue <G>(5), new ValuePointer <G> { Value = Mock.Of <IValuable <G> >() } }, }; DefaultArrayMapValue <G> collectionValue = new DefaultArrayMapValue <G>(collectionValues, valueProvider); DefaultActionValue <G> sut = new DefaultActionValue <G>(groupState, location); Assert.Throws <EngineRuntimeException>(() => { sut.Add(collectionValue, valueProvider); }); }
public void DefaultGroupValue_Get_NotExisting() { string pointerName = "grouppointername"; DefaultValueProvider <G> valueProvider = new DefaultValueProvider <G>(); DeclaredValuePointer <G> groupPointer = new DeclaredValuePointer <G>("group", valueProvider.Null) { Identifier = pointerName }; List <DeclaredValuePointer <G> > groupPointers = new List <DeclaredValuePointer <G> >() { new DeclaredValuePointer <G>("group"), groupPointer, new DeclaredValuePointer <G>("group") }; Dictionary <string, int> pointerMap = new Dictionary <string, int> { { pointerName, 1 } }; G groupState = Mock.Of <G>(m => m.GroupPointers == groupPointers && m.PointerMap == pointerMap); DefaultStringValue <G> key = new DefaultStringValue <G>("otherpointer"); DefaultGroupValue <G> sut = new DefaultGroupValue <G>(groupState); EngineRuntimeException ex = Assert.Throws <EngineRuntimeException>(() => { sut.Get(key, true, valueProvider); }); Assert.Equal("Cannot create group pointer using accessor", ex.Message); }
public void DefaultActionValue_Add_NonMapCollection() { int location = 10; G groupState = Mock.Of <G>(); DefaultValueProvider <G> valueProvider = new DefaultValueProvider <G>(); Dictionary <IValuable <G>, ValuePointer <G> > collectionValues = new Dictionary <IValuable <G>, ValuePointer <G> >() { { new DefaultIntValue <G>(0), new ValuePointer <G> { Value = Mock.Of <IValuable <G> >() } }, { new DefaultIntValue <G>(1), new ValuePointer <G> { Value = Mock.Of <IValuable <G> >() } }, { new DefaultIntValue <G>(2), new ValuePointer <G> { Value = Mock.Of <IValuable <G> >() } } }; ICollectionValue <G> collectionValue = valueProvider.GetCollection(collectionValues, null); DefaultActionValue <G> sut = new DefaultActionValue <G>(groupState, location); DefaultCollectionValue <G> result = (DefaultCollectionValue <G>)sut.Add(collectionValue, valueProvider); List <KeyValuePair <IValuable <G>, ValuePointer <G> > > entries = result.GetEntries(); Assert.Equal(Enumerable.Range(0, 4), entries.Select(kv => kv.Key).Cast <IValue>().Select(v => Convert.ToInt32(v.GetData()))); Assert.Equal(new IValuable <G>[] { sut }.Concat(collectionValues.Values.Select(v => v.Value)), entries.Select(kv => kv.Value).Select(vp => vp.Value)); Assert.False(result.IsMap); Assert.Equal(Enumerable.Range(0, 4), entries.Select(e => e.Value).Cast <EntryValuePointer <G> >().Select(v => v.Index)); }
public void InstructionExecutor_Execute_AllExecutable() { InstructionExecutionBody <G> executionBody = Mock.Of <InstructionExecutionBody <G> >(); Instruction <G> instruction = TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, executionBody); Group <G> group = new Group <G> { Instructions = new[] { instruction, instruction, instruction, instruction, instruction, instruction, instruction } }; G groupState = Mock.Of <G>(m => m.Group == group); E executionState = new ExecutionState <G>(groupState); InstructionExecutor <G> sut = new InstructionExecutor <G>(); sut.Execute(executionState); Assert.Equal(7, executionState.InstructionIndex); Mock.Get(executionBody).Verify(m => m(sut, executionState, It.IsAny <object[]>(), It.IsAny <StackList <ValuePointer <G> > >(), It.IsAny <StackList <StackValuePointer <G> > >()), Times.Exactly(7)); }
public void InstructionExecutor_Execute_NotExecutable_StopProcessing() { InstructionExecutionBody <G> executionBody = Mock.Of <InstructionExecutionBody <G> >(); InstructionExecutionBody <G> specialExecutionBody = Mock.Of <InstructionExecutionBody <G> >(); Instruction <G> specialInstruction = TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, specialExecutionBody); Group <G> group = new Group <G> { Instructions = new[] { TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, executionBody), TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, executionBody), TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, executionBody), specialInstruction, TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, executionBody), TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, executionBody), TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, executionBody), } }; G groupState = Mock.Of <G>(m => m.Group == group); Mock.Get(specialExecutionBody).Setup(m => m(It.IsAny <IInstructionExecutor <G> >(), It.IsAny <E>(), It.IsAny <object[]>(), It.IsAny <StackList <ValuePointer <G> > >(), It.IsAny <StackList <StackValuePointer <G> > >())).Callback((IInstructionExecutor <G> ie, ExecutionState <G> e, object[] payload, StackList <ValuePointer <G> > stackRegister, StackList <StackValuePointer <G> > stackPointers) => { e.Executable = false; }); E executionState = new ExecutionState <G>(groupState); InstructionExecutor <G> sut = new InstructionExecutor <G>(); sut.Execute(executionState); Assert.Equal(4, executionState.InstructionIndex); }
public void DefaultActionValue_Add(object otherVal, bool throwsException) { G groupState = Mock.Of <G>(); DefaultValueProvider <G> valueProvider = new DefaultValueProvider <G>(); DefaultActionValue <G> sut = new DefaultActionValue <G>(groupState, 10); IValuable <G> newValue = null; Action action = () => { newValue = sut.Add(valueProvider.GetAsValue(otherVal), valueProvider); }; if (throwsException) { Assert.Throws <EngineRuntimeException>(action); } else { action(); } if (!throwsException) { switch (otherVal) { case null: Assert.Equal(sut, newValue); break; } } }
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); }
public void DefaultValueProvider_GetGroup() { G groupState = Mock.Of <G>(); DefaultValueProvider <G> sut = new DefaultValueProvider <G>(); IGroupValue <G> result = sut.GetGroup(groupState); Assert.Equal(groupState, result.State); }
public void DefaultActionValue_IsNotEqualTo_SameAction() { G groupState = Mock.Of <G>(); DefaultValueProvider <G> valueProvider = new DefaultValueProvider <G>(); DefaultActionValue <G> sut = new DefaultActionValue <G>(groupState, 10); IValue <G, bool> result = (IValue <G, bool>)sut.IsNotEqualTo(sut, valueProvider); Assert.False(result.Data); }
public void DefaultActionValue_IsNotEqualTo(object otherVal) { G groupState = Mock.Of <G>(); DefaultValueProvider <G> valueProvider = new DefaultValueProvider <G>(); DefaultActionValue <G> sut = new DefaultActionValue <G>(groupState, 10); IValue <G, bool> result = (IValue <G, bool>)sut.IsNotEqualTo(valueProvider.GetAsValue(otherVal), valueProvider); Assert.True(result.Data); }
public void DefaultActionValue_Clone_NoOverrides() { int location = 10; G groupState = Mock.Of <G>(); DefaultActionValue <G> sut = new DefaultActionValue <G>(groupState, location); IActionValue <G> result = sut.Clone(); Assert.Equal(location, result.Location); Assert.Equal(groupState, result.GroupState); }
public void DefaultGroupValue_IsNotEqualTo_SameGroup() { G groupState = Mock.Of <G>(); DefaultValueProvider <G> valueProvider = new DefaultValueProvider <G>(); DefaultGroupValue <G> otherGroupValue = new DefaultGroupValue <G>(groupState); DefaultGroupValue <G> sut = new DefaultGroupValue <G>(groupState); IValue <G, bool> result = (IValue <G, bool>)sut.IsNotEqualTo(otherGroupValue, valueProvider); Assert.False(result.Data); }
public void DefaultValueProvider_GetAction() { int location = 20; G groupState = Mock.Of <G>(); DefaultValueProvider <G> sut = new DefaultValueProvider <G>(); IActionValue <G> result = sut.GetAction(groupState, location); Assert.Equal(location, result.Location); Assert.Equal(groupState, result.GroupState); }
public void StepInterrupt_Intersects() { Group <G> group = new Group <G> { GroupName = "groupname" }; G groupState = Mock.Of <G>(m => m.Group == group); ExecutionState <G> executionState = new ExecutionState <G>(groupState); StepInterrupt <G> sut = new StepInterrupt <G>(); bool result = sut.Intersects(executionState); Assert.True(result); }
public void DefaultActionValue_Execute_WithActionOverrides() { int location = 10; IValuable <G> overrideActionValue = Mock.Of <IValuable <G> >(); Dictionary <int, IValuable <G> > actionOverrides = new Dictionary <int, IValuable <G> > { { location, overrideActionValue } }; G groupState = Mock.Of <G>(m => m.ActionOverrides == actionOverrides); ExecutionState <G> executionState = new ExecutionState <G>(groupState); IValueProvider <G> valueProvider = Mock.Of <IValueProvider <G> >(); DefaultActionValue <G> sut = new DefaultActionValue <G>(groupState, location); sut.Execute(executionState, valueProvider); Mock.Get(overrideActionValue).Verify(m => m.Execute(executionState, valueProvider), Times.Once); }
public void DefaultActionValue_Execute_NoActionOverrides() { int location = 10; StackValuePointer <G>[] initStackPointers = new[] { new StackValuePointer <G>(), new StackValuePointer <G>() }; G groupState = Mock.Of <G>(); ExecutionState <G> executionState = new ExecutionState <G>(groupState); IValueProvider <G> valueProvider = Mock.Of <IValueProvider <G> >(); DefaultActionValue <G> sut = new DefaultActionValue <G>(groupState, location); sut.InitStackPointers.AddRange(initStackPointers); sut.Execute(executionState, valueProvider); Assert.Equal(initStackPointers, executionState.StackPointers); Assert.Equal(location - 1, executionState.InstructionIndex); Assert.Equal(groupState, executionState.GroupState); }
public void InstructionExecutor_Execute_EngineRuntimeException_RethrownAsNewEngineRuntimeException() { InstructionExecutionBody <G> executionBody = Mock.Of <InstructionExecutionBody <G> >(); InstructionExecutionBody <G> specialExecutionBody = Mock.Of <InstructionExecutionBody <G> >(); int sourcePosition = 20; string exceptionMessage = "Engine exception"; EngineRuntimeException thrownException = new EngineRuntimeException(exceptionMessage); Instruction <G> specialInstruction = TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, sourcePosition, false, specialExecutionBody); Group <G> group = new Group <G> { Instructions = new[] { TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, executionBody), TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, executionBody), TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, executionBody), specialInstruction, TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, executionBody), TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, executionBody), TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, executionBody) } }; G groupState = Mock.Of <G>(m => m.Group == group); Mock.Get(specialExecutionBody).Setup(m => m(It.IsAny <IInstructionExecutor <G> >(), It.IsAny <E>(), It.IsAny <object[]>(), It.IsAny <StackList <ValuePointer <G> > >(), It.IsAny <StackList <StackValuePointer <G> > >())).Callback((IInstructionExecutor <G> ie, ExecutionState <G> e, object[] payload, StackList <ValuePointer <G> > stackRegister, StackList <StackValuePointer <G> > stackPointers) => { throw thrownException; }); E executionState = new ExecutionState <G>(groupState); InstructionExecutor <G> sut = new InstructionExecutor <G>(); EngineRuntimeException ex = Assert.Throws <EngineRuntimeException>(() => sut.Execute(executionState)); Assert.Equal(exceptionMessage, ex.Message); Assert.Equal(thrownException, ex.InnerException); Assert.Equal(sourcePosition, ex.Position); }
public void DefaultGroupValue_Get_Existing() { string pointerName = "grouppointername"; DefaultValueProvider <G> valueProvider = new DefaultValueProvider <G>(); DeclaredValuePointer <G> groupPointer = new DeclaredValuePointer <G>("group", valueProvider.Null) { Identifier = pointerName }; List <DeclaredValuePointer <G> > groupPointers = new List <DeclaredValuePointer <G> >() { new DeclaredValuePointer <G>("group"), groupPointer, new DeclaredValuePointer <G>("group") }; Dictionary <string, int> pointerMap = new Dictionary <string, int> { { pointerName, 1 } }; G groupState = Mock.Of <G>(m => m.GroupPointers == groupPointers && m.PointerMap == pointerMap); DefaultStringValue <G> key = new DefaultStringValue <G>(pointerName); DefaultGroupValue <G> sut = new DefaultGroupValue <G>(groupState); ValuePointer <G> result = sut.Get(key, false, valueProvider) as ValuePointer <G>; Assert.Equal(groupPointer, result); }