public void Callback_WhenHasNeutralStronghold_ShouldNotActivateAStronghold( [Frozen] ISystemVariableManager systemVariableManager, [Frozen] IStrongholdActivationCondition activationCondition, [Frozen] IStrongholdManager strongholdManager, IStronghold sh1, IStronghold sh2, IFixture fixture) { sh1.StrongholdState.Returns(StrongholdState.Inactive); sh2.StrongholdState.Returns(StrongholdState.Neutral); var lastCheckTime = Substitute.For <SystemVariable>(); lastCheckTime.Value.Returns(SystemClock.Now.AddHours(-8)); systemVariableManager["Stronghold.neutral_check"].Returns(lastCheckTime); var locker = new LockerStub(); fixture.Register <ILocker>(() => locker); strongholdManager.GetEnumerator().Returns(_ => new List <IStronghold> { sh1, sh2 }.GetEnumerator()); var checker = fixture.Create <StrongholdActivationChecker>(); checker.Callback(null); strongholdManager.DidNotReceive().Activate(sh2); strongholdManager.DidNotReceive().Activate(sh1); }
public void Callback_WhenNoStrongholdsAreInactive_ShouldNotThrowExceptionAndShouldUpdateNeutralCheckTime( [Frozen] ISystemVariableManager systemVariableManager, [Frozen] IStrongholdManager strongholdManager, [Frozen] IDbManager dbManager, IStronghold sh1, IStronghold sh2, IFixture fixture) { sh1.StrongholdState.Returns(StrongholdState.Occupied); sh2.StrongholdState.Returns(StrongholdState.Occupied); var lastCheckTime = Substitute.For <SystemVariable>(); lastCheckTime.Value.Returns(SystemClock.Now.AddHours(-8)); systemVariableManager["Stronghold.neutral_check"].Returns(lastCheckTime); var locker = new LockerStub(); fixture.Register <ILocker>(() => locker); strongholdManager.GetEnumerator().Returns(_ => new List <IStronghold> { sh1, sh2 }.GetEnumerator()); var checker = fixture.Create <StrongholdActivationChecker>(); checker.Invoking(c => c.Callback(null)).ShouldNotThrow(); lastCheckTime.Received().Value = SystemClock.Now; dbManager.Received(1).Save(lastCheckTime); }
public void WhenSomeoneJoins(IEnumerable <int> moveTimes, DateTime expectedTime) { var tribe = Substitute.For <ITribe>(); var targetCity = Substitute.For <ICity>(); ICity stubCity; var stub = CreateStub(out stubCity); var formula = Substitute.For <Formula>(); var dbManager = Substitute.For <IDbManager>(); var gameObjectLocator = Substitute.For <IGameObjectLocator>(); var scheduler = Substitute.For <IScheduler>(); var procedure = Substitute.For <Procedure>(); var radiusLocator = Substitute.For <ITileLocator>(); var actionFactory = Substitute.For <IActionFactory>(); var locker = new LockerStub(gameObjectLocator); var initializer = Substitute.For <ITroopObjectInitializerFactory>(); ICity newCity; var newStub = CreateStub(out newCity); ICity newCity2; var newStub2 = CreateStub(out newCity2); SystemClock.SetClock(startTime); Queue <int> moveTimeReturns = new Queue <int>(); foreach (var moveTime in moveTimes) { moveTimeReturns.Enqueue(moveTime); } formula.MoveTimeTotal(Arg.Any <ITroopStub>(), 0, true).Returns(x => moveTimeReturns.Dequeue()); Assignment assignment = new Assignment(tribe, TARGET_X, TARGET_Y, targetCity, AttackMode.Normal, targetTime, "Description", true, formula, dbManager, gameObjectLocator, scheduler, procedure, radiusLocator, actionFactory, locker, initializer); assignment.Add(stub); assignment.Add(newStub); assignment.Add(newStub2); assignment.TroopCount.Should().Be(3); dbManager.Received(3).Save(assignment); scheduler.Received(3).Put(assignment); assignment.Callback(null); assignment.Time.Should().Be(expectedTime); }
public void TestAlignmentPoints(decimal initialValue, int cycles, decimal expectedValue) { // Config vars Config.actions_skip_city_actions = false; Config.troop_starve = false; // TechManager var technologies = Substitute.For <ITechnologyManager>(); technologies.GetEffects(Arg.Any <EffectCode>()).ReturnsForAnyArgs(x => new List <Effect>()); // City var city = Substitute.For <ICity>(); city.AlignmentPoint = initialValue; // Not necessarily related to this test city.Id.Returns((uint)1); city.Resource.Returns(new LazyResource(1, 1, 1, 1, 1)); city.GetEnumerator().Returns(x => new List <IStructure>().GetEnumerator()); city.Technologies.Returns(technologies); // Locker var locker = new LockerStub(new GameObjectLocatorStub(city)); CityPassiveAction action = new CityPassiveAction(1, Substitute.For <IObjectTypeFactory>(), locker, Substitute.For <Formula>(), Substitute.For <IActionFactory>(), Substitute.For <Procedure>(), Substitute.For <IGameObjectLocator>(), Substitute.For <IBattleFormulas>(), Substitute.For <IStructureCsvFactory>(), Substitute.For <TechnologyFactory>(), Substitute.For <UnitFactory>()) { WorkerObject = city }; for (int i = 0; i < cycles; i++) { action.Callback(null); } city.AlignmentPoint.Should().Be(expectedValue); }
public void Callback_WhenInactiveStrongholdsHaveSameScore_ShouldActivateClosestToCenter( [Frozen] ISystemVariableManager systemVariableManager, [Frozen] IStrongholdActivationCondition activationCondition, [Frozen] IStrongholdManager strongholdManager, [Frozen] ITileLocator tileLocator, IStronghold sh1, IStronghold sh2, IFixture fixture) { sh1.StrongholdState.Returns(StrongholdState.Inactive); sh1.PrimaryPosition.Returns(new Position(10, 20)); sh2.StrongholdState.Returns(StrongholdState.Inactive); sh2.PrimaryPosition.Returns(new Position(30, 40)); activationCondition.Score(null).ReturnsForAnyArgs(0); tileLocator.TileDistance(sh1.PrimaryPosition, 1, Arg.Any <Position>(), 1) .Returns(10); tileLocator.TileDistance(sh2.PrimaryPosition, 1, Arg.Any <Position>(), 1) .Returns(5); var lastCheckTime = Substitute.For <SystemVariable>(); lastCheckTime.Value.Returns(SystemClock.Now.AddHours(-8)); systemVariableManager["Stronghold.neutral_check"].Returns(lastCheckTime); var locker = new LockerStub(); fixture.Register <ILocker>(() => locker); strongholdManager.GetEnumerator().Returns(_ => new List <IStronghold> { sh1, sh2 }.GetEnumerator()); var checker = fixture.Create <StrongholdActivationChecker>(); checker.Callback(null); strongholdManager.Received().Activate(sh2); strongholdManager.DidNotReceive().Activate(sh1); }
public void UserCancelled_WhenUserHasDifferentTechnologies_ShouldRefundCorrectAmount( UserCancelledTestData testData, [FrozenMock] Formula formula, [FrozenMock] UnitFactory unitFactory, [Frozen] IWorld world, ICity city, IStructure structure, ITechnologyManager technologyManager) { ushort trainedCount = 0; formula.GetInstantTrainCount(structure).ReturnsForAnyArgs(testData.InstantTrainCount); formula.GetXForYTotal(technologyManager, testData.TrainCount).Returns(testData.TotalCount); formula.GetXForYPaidFor(technologyManager, testData.InstantTrainCount + testData.CompletedTraining).Returns(testData.TotalPaidFor); formula.UnitTrainCost(city, 100, 0).Returns(testData.CostPerUnit); formula.GetActionCancelResource(DateTime.MinValue, null) .ReturnsForAnyArgs(c => c.Arg <Resource>()); city.DefaultTroop.When(m => m.AddUnit(FormationType.Normal, 100, Arg.Any <ushort>())) .Do(args => { trainedCount += (ushort)args[2]; }); city.Resource.HasEnough(testData.TotalCost).ReturnsForAnyArgs(true); city.Id.Returns <uint>(1); structure.City.Returns(city); structure.Technologies.Returns(technologyManager); IStructure outStructure; ICity outCity; city.TryGetStructure(10, out outStructure).Returns(args => { args[1] = structure; return(true); }); world.TryGetObjects(1, 10, out outCity, out outStructure).Returns(args => { args[2] = city; args[3] = structure; return(true); }); var gameObjectLocator = new GameObjectLocatorStub(city); var locker = new LockerStub(gameObjectLocator); var action = new UnitTrainActiveAction(1, 10, 100, testData.TrainCount, unitFactory, locker, world, formula); action.WorkerObject = Substitute.For <ICanDo>(); action.Execute(); action.ActionCount.Should().Be(testData.ExpectedQueuedCount); city.Resource.Received(1).Subtract(testData.TotalCost); for (var i = 0; i < testData.CompletedTraining; i++) { action.Callback(null); } action.UserCancelled(); city.Resource.Received(1).Add(testData.ExpectedRefundAmount); trainedCount.Should().Be(testData.ExpectedCityReceivedCount); }