softy_02

WarriorTests

Jul 5th, 2020
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.41 KB | None | 0 0
  1. using System;
  2. using NUnit.Framework;
  3.  
  4. namespace Tests
  5. {
  6.     using FightingArena;
  7.     public class WarriorTests
  8.     {
  9.         private Warrior warrior;
  10.         [SetUp]
  11.         public void Setup()
  12.         {
  13.             this.warrior = new Warrior("Hector", 88, 100);
  14.         }
  15.  
  16.         [Test]
  17.         public void CheckIfConstructorWorksProperly()
  18.         {
  19.             var expectedName = "Achilles";
  20.             var expectedDamage = 92;
  21.             var expectedHp = 108;
  22.  
  23.             this.warrior = new Warrior(expectedName, expectedDamage, expectedHp);
  24.  
  25.             var actualName = this.warrior.Name;
  26.             var actualDamage = this.warrior.Damage;
  27.             var actualHp = this.warrior.HP;
  28.  
  29.             Assert.AreEqual(expectedName, actualName);
  30.             Assert.AreEqual(expectedDamage, actualDamage);
  31.             Assert.AreEqual(expectedHp, actualHp);
  32.         }
  33.  
  34.         //Create different test for each unit
  35.         [TestCase(null, 55, 99)]
  36.         [TestCase("     ", 98, 67)]
  37.         [TestCase("", 78, 53)]
  38.         [TestCase("Michael", 0, 34)]
  39.         [TestCase("Pesho", -23, 39)]
  40.         [TestCase("Nial", 55, -12)]
  41.         public void CheckIfPropertiesThrowExceptionsWithInvalidData
  42.             (string name, int damage, int hp)
  43.         {
  44.             Assert.Throws<ArgumentException>(() =>
  45.             {
  46.                 this.warrior = new Warrior(name, damage, hp);
  47.             });
  48.         }
  49.  
  50.         [TestCase("Brown", 32, 31)]
  51.         [TestCase("Brown", 32, 89)]
  52.         public void CheckIfAttackWorksProperlyWithValidValues
  53.             (string name, int damage, int hp)
  54.         {
  55.             var defender = new Warrior(name, damage, hp);
  56.  
  57.             var expectedHp = this.warrior.HP - defender.Damage;
  58.  
  59.             int expectedDefenderHp;
  60.  
  61.             if (defender.HP > this.warrior.Damage)
  62.             {
  63.                 expectedDefenderHp = defender.HP - this.warrior.Damage;
  64.             }
  65.             else
  66.             {
  67.                 expectedDefenderHp = 0;
  68.             }
  69.  
  70.             this.warrior.Attack(defender);
  71.  
  72.             Assert.AreEqual(expectedHp, warrior.HP);
  73.             Assert.AreEqual(expectedDefenderHp, defender.HP);
  74.         }
  75.        
  76.         [TestCase(30)]
  77.         [TestCase(16)]
  78.         [TestCase(0)]
  79.         public void AttackShouldThrowExceptionWhenAttackerHpAreLessThanMinimumHp(int hp)
  80.         {
  81.             var attacker = new Warrior("Silver", 90, hp);
  82.  
  83.             Assert.Throws<InvalidOperationException>(() =>
  84.             {
  85.                 attacker.Attack(this.warrior);
  86.             });
  87.         }
  88.  
  89.         [TestCase(30)]
  90.         [TestCase(13)]
  91.         [TestCase(0)]
  92.         public void AttackShouldThrowExceptionWhenDefenderHpAreLessThanMinimumHp(int hp)
  93.         {
  94.             var defender = new Warrior("Shogun", 45, hp);
  95.  
  96.             Assert.Throws<InvalidOperationException>(() =>
  97.             {
  98.                 this.warrior.Attack(defender);
  99.             });
  100.         }
  101.  
  102.         [TestCase("Achilles", 110, 120)]
  103.         [TestCase("Thor", 400, 540)]
  104.         [TestCase("Zeus", 250, 230)]
  105.         public void AttackShouldThrowExceptionWhenTryingToAttackStrongerEnemy(
  106.             string name, int damage, int hp)
  107.         {
  108.             var enemy = new Warrior(name, damage, hp);
  109.  
  110.             Assert.Throws<InvalidOperationException>(() =>
  111.             {
  112.                 this.warrior.Attack(enemy);
  113.             });
  114.         }
  115.     }
  116. }
Add Comment
Please, Sign In to add comment