Ejemplo n.º 1
0
        public void IsOver_AFighterHasNoHealth_ShouldReturnTrue()
        {
            //Arrange
            IHero   fighter1 = new HeroMockBuilder().WithHealth(-5).BuildObject();
            IHero   fighter2 = new HeroMockBuilder().WithHealth(10).BuildObject();
            IBattle battle   = new BattleBuilder(fighter1, fighter2).Build();

            //Act + Assert
            Assert.That(battle.IsOver, Is.True);
        }
Ejemplo n.º 2
0
        public void IsOver_BothFightersHaveHealth_ShouldReturnFalse()
        {
            //Arrange
            IHero   fighter1 = new HeroMockBuilder().WithHealth(20).BuildObject();
            IHero   fighter2 = new HeroMockBuilder().WithHealth(10).BuildObject();
            IBattle battle   = new BattleBuilder(fighter1, fighter2).Build();

            //Act + Assert
            Assert.That(battle.IsOver, Is.False);
        }
        public void Attack_SuperMode_ShouldAttackOpponentWithDoubleStrength()
        {
            //Arrange
            IHero        hero         = new HeroBuilder().WithSuperModeLikeliness(1.0f).Build();
            Mock <IHero> opponentMock = new HeroMockBuilder().Build();

            //Act
            hero.Attack(opponentMock.Object);

            //Assert
            opponentMock.Verify(opponent => opponent.DefendAgainstAttack(hero.Strength * 2), Times.Once,
                                $"Should call 'DefendAgainstAttack' of opponent with strength {hero.Strength * 2}.");
        }
Ejemplo n.º 4
0
        public void FightRound_Fighter2HasNoHealth_ShouldOnlyLetFighter1Attack()
        {
            //Arrange
            Mock <IHero> fighter1Mock = new HeroMockBuilder().WithHealth(100).Build();
            Mock <IHero> fighter2Mock = new HeroMockBuilder().WithHealth(0).Build();
            IBattle      battle       = new BattleBuilder(fighter1Mock.Object, fighter2Mock.Object).Build();

            //Act
            battle.FightRound();

            //Assert
            fighter1Mock.Verify(f => f.Attack(fighter2Mock.Object), Times.Once, "Fighter 1 did not attack fighter 2.");
            fighter2Mock.Verify(f => f.Attack(It.IsAny <IHero>()), Times.Never, "Fighter 2 should not attack. He has no health.");
        }
Ejemplo n.º 5
0
        public void FightRound_ShouldLetThe2FightersAttack()
        {
            //Arrange
            Mock <IHero> fighter1Mock = new HeroMockBuilder().WithHealth(100).Build();
            Mock <IHero> fighter2Mock = new HeroMockBuilder().WithHealth(100).Build();
            IBattle      battle       = new BattleBuilder(fighter1Mock.Object, fighter2Mock.Object).Build();

            //Act
            battle.FightRound();

            //Assert
            fighter1Mock.Verify(f => f.Attack(fighter2Mock.Object), Times.Once, "Fighter 1 did not attack fighter 2.");
            fighter2Mock.Verify(f => f.Attack(fighter1Mock.Object), Times.Once, "Fighter 2 did not attack fighter 1.");
        }
Ejemplo n.º 6
0
        public void Factory_CreateNewBattle_ShouldThrowArgumentNullExceptionForInvalidInput()
        {
            //Arrange
            IBattleFactory factory = CreateFactoryInstance();
            IHero          fighter = new HeroMockBuilder().BuildObject();

            //Act + Assert
            Assert.That(() => factory.CreateNewBattle(null, null), Throws.InstanceOf <ArgumentNullException>(),
                        "Null fighters are not allowed");
            Assert.That(() => factory.CreateNewBattle(fighter, null), Throws.InstanceOf <ArgumentNullException>(),
                        "Null fighters are not allowed");
            Assert.That(() => factory.CreateNewBattle(null, fighter), Throws.InstanceOf <ArgumentNullException>(),
                        "Null fighters are not allowed");
        }
Ejemplo n.º 7
0
        public void Factory_CreateNewBattle_ShouldCreateAValidBattle()
        {
            //Arrange
            IBattleFactory factory  = CreateFactoryInstance();
            IHero          fighter1 = new HeroMockBuilder().BuildObject();
            IHero          fighter2 = new HeroMockBuilder().BuildObject();

            //Act
            IBattle battle = factory.CreateNewBattle(fighter1, fighter2);

            //Assert
            Assert.That(battle, Is.Not.Null, "CreateNewBattle should not return null.");
            Assert.That(battle.Fighter1, Is.SameAs(fighter1), "CreateNewBattle does not set fighter1 correctly.");
            Assert.That(battle.Fighter2, Is.SameAs(fighter2), "CreateNewBattle does not set fighter2 correctly.");
        }
        private void AssertSuperModeLikelinessForAttack(float superModeLikeliness)
        {
            IHero hero = new HeroBuilder().WithSuperModeLikeliness(superModeLikeliness).Build();

            int numberOfAttacks       = 200;
            int numberOfNormalAttacks = 0;
            int numberOfSuperAttacks  = 0;

            for (int i = 0; i < numberOfAttacks; i++)
            {
                Mock <IHero> opponentMock = new HeroMockBuilder().Build();
                opponentMock.Setup(opponent => opponent.DefendAgainstAttack(It.IsAny <int>()))
                .Callback((int attackStrength) =>
                {
                    if (attackStrength == hero.Strength)
                    {
                        numberOfNormalAttacks++;
                    }
                    else if (attackStrength == hero.Strength * 2)
                    {
                        numberOfSuperAttacks++;
                    }
                });
                hero.Attack(opponentMock.Object);
            }

            Assert.That(numberOfNormalAttacks, Is.GreaterThan(0),
                        $"Out of {numberOfAttacks} attacks, no normal attack happened. That is not random enough.");
            Assert.That(numberOfSuperAttacks, Is.GreaterThan(0),
                        $"Out of {numberOfAttacks} attacks, no supermode attack happened. That is not random enough.");
            Assert.That(numberOfSuperAttacks + numberOfNormalAttacks, Is.EqualTo(numberOfAttacks),
                        $"The amount of normal attacks ({numberOfNormalAttacks}) " +
                        $"added with the number of supermode attacks ({numberOfSuperAttacks}) " +
                        $"should be equal to the total number of attacks ({numberOfAttacks}).");
            double actualSuperMode = numberOfSuperAttacks / (double)numberOfAttacks;

            Assert.That(actualSuperMode, Is.EqualTo(hero.SuperModeLikeliness).Within(0.15),
                        $"After {numberOfAttacks} attacks the supermode likeliness seems to be around {Convert.ToInt32(actualSuperMode * 100)}%. " +
                        $"It should be around {Convert.ToInt32(hero.SuperModeLikeliness * 100)}%.");
        }