alexbancheva

Presents.Tests_18April2021

Apr 21st, 2021
677
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.51 KB | None | 0 0
  1. namespace Presents.Tests
  2. {
  3.     using NUnit.Framework;
  4.     using System;
  5.     using System.Collections.Generic;
  6.  
  7.     [TestFixture]
  8.     public class PresentsTests
  9.     {
  10.         private Bag bag;
  11.  
  12.         [SetUp]
  13.         public void SetUp()
  14.         {
  15.             this.bag = new Bag();
  16.         }
  17.  
  18.         [Test]
  19.         public void TestIfPresentCtorWorkCorrectly()
  20.         {
  21.             string expName = "Stick";
  22.             double expMagic = 100;
  23.  
  24.             Present present = new Present(expName, expMagic);
  25.  
  26.             Assert.AreEqual(expName, present.Name);
  27.             Assert.AreEqual(expMagic, present.Magic);
  28.         }
  29.  
  30.         [Test]
  31.         public void TestIfBagCtorWorksCorrectly()
  32.         {
  33.             Bag bag = new Bag();
  34.  
  35.             Assert.IsNotNull(bag.GetPresents());
  36.         }
  37.  
  38.         [Test]
  39.         public void CreateShouldThrowxceptionWithLikeNullPresent()
  40.         {
  41.             Present present = null;
  42.  
  43.             Assert.That(() =>
  44.             {
  45.                 bag.Create(present);
  46.             }, Throws.ArgumentNullException);
  47.  
  48.         }
  49.  
  50.         [Test]
  51.         public void CreateShouldThrowExceptionWithLikeExistingPresent()
  52.         {
  53.             Present present = new Present("Stick", 100);
  54.  
  55.             bag.Create(present);
  56.  
  57.             Assert.That(() =>
  58.             {
  59.                 bag.Create(present);
  60.             }, Throws.InvalidOperationException.With.Message.EqualTo("This present already exists!"));
  61.         }
  62.  
  63.         [Test]
  64.         public void CreateShouldPhysicallyAddThePresents()
  65.         {
  66.             string name = "Stick";
  67.             double magic = 100;
  68.  
  69.             Present p1 = new Present(name, magic);
  70.             Present p2 = new Present(name, magic);
  71.  
  72.             bag.Create(p1);
  73.             bag.Create(p2);
  74.  
  75.             IReadOnlyCollection<Present> exp = new List<Present>()
  76.             {
  77.                 p1, p2
  78.             };
  79.  
  80.             IReadOnlyCollection<Present> act = bag.GetPresents();
  81.  
  82.             CollectionAssert.AreEqual(exp, act);
  83.             Assert.AreEqual(2, bag.GetPresents().Count);
  84.         }
  85.  
  86.         [Test]
  87.         public void CreateShouldReturnCorrectMessage()
  88.         {
  89.             string name = "Stick";
  90.             double magic = 100;
  91.  
  92.             Present p1 = new Present(name, magic);
  93.  
  94.             string exp = $"Successfully added present {p1.Name}.";
  95.             string res = bag.Create(p1);
  96.  
  97.             Assert.AreEqual(exp, res);
  98.         }
  99.  
  100.         [Test]
  101.         public void RemoveShouldPhysicallyRemoveThePresent()
  102.         {
  103.             string name = "Stick";
  104.             double magic = 100;
  105.  
  106.             Present p1 = new Present(name, magic);
  107.             Present p2 = new Present(name, magic);
  108.  
  109.             bag.Create(p1);
  110.             bag.Create(p2);
  111.  
  112.             bool res = bag.Remove(p1);
  113.  
  114.             IReadOnlyCollection<Present> exp = new List<Present>()
  115.             {
  116.                 p2
  117.             };
  118.  
  119.             IReadOnlyCollection<Present> act = bag.GetPresents();
  120.  
  121.             Assert.IsTrue(res);
  122.             CollectionAssert.AreEqual(exp, act);
  123.         }
  124.  
  125.         [Test]
  126.         public void TryRemovingNonExistingPresentShouldReturnFalse()
  127.         {
  128.             string name = "Stick";
  129.             double magic = 100;
  130.  
  131.             Present p1 = new Present(name, magic);
  132.  
  133.             bool res = bag.Remove(p1);
  134.  
  135.             Assert.IsFalse(res);
  136.         }
  137.  
  138.         [Test]
  139.         public void GetPresentWithLeastMagicShouldWorkCorrectly()
  140.         {
  141.             string name = "Stick";
  142.             double magic = 100;
  143.  
  144.             Present p1 = new Present(name, magic);
  145.             Present p2 = new Present(name, 50);
  146.             Present exp = new Present(name, 20);
  147.  
  148.             bag.Create(p1);
  149.             bag.Create(p2);
  150.             bag.Create(exp);
  151.  
  152.             Present act = bag.GetPresentWithLeastMagic();
  153.  
  154.             Assert.AreEqual(exp, act);
  155.  
  156.         }
  157.  
  158.         [Test]
  159.         public void GetPresentWithLeastMagicShouldThrowExceptionWhenBagEmpty()
  160.         {
  161.             Assert.That(() =>
  162.             {
  163.                 bag.GetPresentWithLeastMagic();
  164.             }, Throws.InvalidOperationException);
  165.         }
  166.  
  167.         [Test]
  168.         public void GetPresentShouldReturnCorrectPresentWhenNoDuplicates()
  169.         {
  170.             string expName = "Stick";
  171.  
  172.             Present exp = new Present(expName, 100);
  173.             Present p2 = new Present("Another mag present", 50);
  174.  
  175.             bag.Create(exp);
  176.             bag.Create(p2);
  177.  
  178.             Present act = bag.GetPresent(expName);
  179.  
  180.             Assert.AreEqual(exp, act);
  181.         }
  182.  
  183.         [Test]
  184.         public void GetPresentShouldReturnFirstPresentWhenDuplicates()
  185.         {
  186.             string name = "Stick";
  187.             double magic = 100;
  188.  
  189.             Present p1 = new Present(name, magic);
  190.             Present p2 = new Present(name, magic);
  191.  
  192.             bag.Create(p1);
  193.             bag.Create(p2);
  194.  
  195.             Present act = bag.GetPresent(name);
  196.  
  197.             Assert.AreEqual(p1, act);
  198.         }
  199.  
  200.         [Test]
  201.         public void GetPresentShouldReturnNullWhenNameNotExist()
  202.         {
  203.             string name = "Stick";
  204.             double magic = 100;
  205.  
  206.             Present p1 = new Present(name, magic);
  207.             Present p2 = new Present(name, magic);
  208.  
  209.             bag.Create(p1);
  210.             bag.Create(p2);
  211.  
  212.             string invalidName = "Non existing name";
  213.  
  214.             Present act = bag.GetPresent(invalidName);
  215.  
  216.             Assert.IsNull(act);
  217.         }
  218.     }
  219. }
  220.  
Advertisement
Add Comment
Please, Sign In to add comment