public void RandomizeCommandChain() { bool chainFull = false; do { //Gets current selection string currentSelection = GetRandomCommand(); //Selection cost variable int selectionCost = 0; //Checks to see what selection it was switch (currentSelection) { case "Rest": selectionCost = 5; break; case "Set Up": selectionCost = 3; break; default: if (knownAttacks.ContainsKey(currentSelection)) { selectionCost = knownAttacks[currentSelection].Cost; } else if (knownTaunts.ContainsKey(currentSelection)) { selectionCost = knownTaunts[currentSelection].Cost; } break; } if (currentTurnCost + selectionCost < MaxTurnCost) { currentTurnCost += selectionCost; CommandChain.Add(currentSelection); } else { chainFull = true; } } while (!chainFull); }
public void ExecuteAndRollback(int numberOfCommands, int falseExecutionIndex) { Fixture fixture = new Fixture(); CommandChain commandChain = new CommandChain(); Assert.IsFalse(commandChain.InputDataSet); Assert.IsFalse(commandChain.WasExecuted); Assert.IsFalse(commandChain.WasRolledBack); Assert.IsNotNull(commandChain.Name); List <string> names = new List <string>(numberOfCommands); List <TestCommand> commands = new List <TestCommand>(numberOfCommands); List <string> executionNames = new List <string>(numberOfCommands); List <string> rollbackNames = new List <string>(numberOfCommands); for (int i = 0; i < numberOfCommands; i++) { string name = fixture.Create <string>(); names.Add(name); bool executionOk = i != falseExecutionIndex; TestCommand command = new TestCommand ( name, () => { executionNames.Add(name); return(executionOk); }, () => { rollbackNames.Add(name); } ); commandChain.Add(command); commands.Add(command); } Assert.IsTrue(commandChain.InputDataSet); Assert.IsFalse(commandChain.WasExecuted); Assert.IsFalse(commandChain.WasRolledBack); Assert.AreEqual(falseExecutionIndex == -1, commandChain.Execute()); Assert.IsTrue(commandChain.WasExecuted); Assert.IsFalse(commandChain.WasRolledBack); Assert.AreEqual(falseExecutionIndex == -1 ? numberOfCommands : falseExecutionIndex + 1, executionNames.Count); Assert.IsTrue(executionNames.SequenceEqual(names.Take(executionNames.Count))); Assert.AreEqual(0, rollbackNames.Count); Assert.AreEqual(executionNames.Count, commands.Take(executionNames.Count).Count(p => p.WasExecuted)); Assert.AreEqual(numberOfCommands - executionNames.Count, commands.Skip(executionNames.Count).Count(p => !p.WasExecuted)); commandChain.Rollback(); Assert.IsTrue(commandChain.WasExecuted); Assert.IsTrue(commandChain.WasRolledBack); Assert.AreEqual(executionNames.Count, rollbackNames.Count); Assert.IsTrue(executionNames.SequenceEqual(((IEnumerable <string>)rollbackNames).Reverse())); Assert.AreEqual(rollbackNames.Count, commands.Take(rollbackNames.Count).Count(p => p.WasRolledBack)); Assert.AreEqual(numberOfCommands - rollbackNames.Count, commands.Skip(rollbackNames.Count).Count(p => !p.WasRolledBack)); }