Threed90

NUnit_GymsTests_Exam11.12.2021

Dec 11th, 2021
840
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.86 KB | None | 0 0
  1. using NUnit.Framework;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. namespace Gyms.Tests
  7. {
  8.     public class GymsTests
  9.     {
  10.         // Implement unit tests here
  11.  
  12.         [Test]
  13.         [TestCase(10)]
  14.         [TestCase(0)]
  15.         public void ConstructorShouldSetNewCollectionNoMatterOfCapacity(int value)
  16.         {
  17.             Gym gym = new Gym("TestGym", value);
  18.  
  19.             Assert.IsTrue(gym.Count == 0);
  20.         }
  21.  
  22.         [Test]
  23.         [TestCase("TestGym1", 10)]
  24.         [TestCase("TestGym2", 0)]
  25.         public void ConstructorShouldSetGymNameValues(string name, int capacity)
  26.         {
  27.             Gym gym = new Gym(name, capacity);
  28.  
  29.             string expectedName = name;
  30.             int expectedCapacity = capacity;
  31.  
  32.             Assert.IsTrue(gym.Name == expectedName && gym.Capacity == expectedCapacity);
  33.         }
  34.  
  35.         [Test]
  36.         [TestCase(null)]
  37.         [TestCase("")]
  38.         public void ConstructorShouldThrowsExceptionWhenNameIsNullOrEmpty(string value)
  39.         {
  40.  
  41.             Assert.Throws<ArgumentNullException>(() => new Gym(value, 10));
  42.         }
  43.  
  44.         [Test]
  45.         [TestCase(-1)]
  46.         [TestCase(int.MinValue)]
  47.         public void ConstructorShouldThrowsExceptionWhenCapacityValueIsNegative(int value)
  48.         {
  49.             Assert.Throws<ArgumentException>(() => new Gym("TestGym", value), "Invalid gym capacity.");
  50.         }
  51.  
  52.         [Test]
  53.         public void AddAthleteShouldThrowsExceptionWhenTryToAddMoreThanCapacityAthletes()
  54.         {
  55.             Gym gym = new Gym("TestGym", 0);
  56.  
  57.             Assert.Throws<InvalidOperationException>(() => gym.AddAthlete(new Athlete("testAthlete")), "The gym is full.");
  58.         }
  59.  
  60.         [Test]
  61.         public void AddAthleteShouldIncreaseTheCountWithAddingOfNewAthlete()
  62.         {
  63.             Gym gym = new Gym("TestGym", 10);
  64.             gym.AddAthlete(new Athlete("TestAthlete"));
  65.  
  66.             int expectedCount = 1;
  67.             int actual = gym.Count;
  68.  
  69.             Assert.AreEqual(expectedCount, actual);
  70.         }
  71.  
  72.         [Test]
  73.         public void RemoveAthleteMethodShouldThrowsExceptinWhenNonExistingAthlete()
  74.         {
  75.             Gym gym = new Gym("TestGym", 10);
  76.  
  77.             string name = "NonExistingAthlete";
  78.             Assert.Throws<InvalidOperationException>(() => gym.RemoveAthlete(name), $"The athlete {name} doesn't exist.");
  79.         }
  80.  
  81.         [Test]
  82.         public void RemoveAthleteMethodShouldDecreaseTheCountByRemovingAthlete()
  83.         {
  84.             Gym gym = new Gym("TestGym", 10);
  85.             gym.AddAthlete(new Athlete("TestAthlete1"));
  86.             gym.AddAthlete(new Athlete("TestAthlete2"));
  87.  
  88.             gym.RemoveAthlete("TestAthlete1");
  89.  
  90.             int expected = 1;
  91.             int actual = gym.Count;
  92.  
  93.             Assert.AreEqual(expected, actual);
  94.         }
  95.  
  96.         [Test]
  97.         public void InjureAthleteShouldThrowsExceptionWhenNonExistingAthlete()
  98.         {
  99.             Gym gym = new Gym("TestGym", 10);
  100.  
  101.             string fullName = "NonExistingAthlete";
  102.             Assert.Throws<InvalidOperationException>(() => gym.InjureAthlete(fullName), $"The athlete {fullName} doesn't exist.");
  103.         }
  104.  
  105.         [Test]
  106.         public void InjureAthleteShoulSetTrueToIsInjuredPropertyForGivenAthleteName()
  107.         {
  108.             Gym gym = new Gym("TestGym", 10);
  109.             gym.AddAthlete(new Athlete("TestAthlete1"));
  110.  
  111.             Athlete athlete = gym.InjureAthlete("TestAthlete1");
  112.  
  113.             bool expected = true;
  114.             bool actual = athlete.IsInjured;
  115.  
  116.             Assert.AreEqual(expected, actual);
  117.         }
  118.  
  119.         [Test]
  120.         public void InjureAthleteShouldReturnExactlyTheSameAthleteByGivenName()
  121.         {
  122.             Gym gym = new Gym("TestGym", 10);
  123.             Athlete expected = new Athlete("TestAthlete1");
  124.             gym.AddAthlete(expected);
  125.  
  126.             Athlete actual = gym.InjureAthlete("TestAthlete1");
  127.  
  128.             Assert.IsTrue(actual.FullName == expected.FullName);
  129.             Assert.IsTrue(actual.IsInjured == expected.IsInjured);
  130.             Assert.IsTrue(actual.GetHashCode() == expected.GetHashCode());
  131.         }
  132.  
  133.         [Test]
  134.         public void ReportMethodShouldReturnCorrectStringFormatWhenThereAreInjuredOrRemovedAthletes()
  135.         {
  136.             List<Athlete> list = new List<Athlete>();
  137.             Gym gym = new Gym("TestGym", 10);
  138.             gym.AddAthlete(new Athlete("TestAthlete1"));
  139.             gym.AddAthlete(new Athlete("TestAthlete2"));
  140.             gym.AddAthlete(new Athlete("TestAthlete3"));
  141.  
  142.             list.Add(new Athlete("TestAthlete1") { IsInjured = true});
  143.             list.Add(new Athlete("TestAthlete2"));
  144.  
  145.  
  146.             gym.InjureAthlete("TestAthlete1");
  147.             gym.RemoveAthlete("TestAthlete3");
  148.  
  149.             string expected = $"Active athletes at {gym.Name}: {string.Join(", ", list.Where(a => !a.IsInjured).Select(a => a.FullName))}";
  150.  
  151.             string actual = gym.Report();
  152.  
  153.             Assert.AreEqual(expected, actual);
  154.         }
  155.  
  156.         [Test]
  157.         public void ReportShouldWorkWithNoAthletes()
  158.         {
  159.             Gym gym = new Gym("TestGym", 10);
  160.  
  161.             string expected = $"Active athletes at {gym.Name}: {string.Empty}";
  162.             string actual = gym.Report();
  163.  
  164.             Assert.AreEqual(expected, actual);
  165.         }
  166.  
  167.         [Test]
  168.         public void ReportShouldWorkWithOnlyInjuredAthletes()
  169.         {
  170.             Gym gym = new Gym("TestGym", 10);
  171.             gym.AddAthlete(new Athlete("TestAthlete1") { IsInjured = true });
  172.             gym.AddAthlete(new Athlete("TestAthlete2") { IsInjured = true });
  173.             gym.AddAthlete(new Athlete("TestAthlete3") { IsInjured = true });
  174.  
  175.             string expected = $"Active athletes at {gym.Name}: {string.Empty}";
  176.             string actual = gym.Report();
  177.  
  178.             Assert.AreEqual(expected,actual);
  179.         }
  180.     }
  181. }
  182.  
Advertisement
Add Comment
Please, Sign In to add comment