Venciity

UnitTestsPhonebookRepository

Jan 17th, 2015
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.12 KB | None | 0 0
  1. using System;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using PhonebookSystem;
  4. using System.Linq;
  5.  
  6. namespace Phonebook.Tests
  7. {
  8.     [TestClass]
  9.     public class UnitTestsPhonebookRepository
  10.     {
  11.         [TestMethod]
  12.         public void TestAddSinglePhonebookEntry()
  13.         {
  14.             PhonebookRepository phonebook = new PhonebookRepository();
  15.             bool isNew = phonebook.AddPhone("Nakov", new string[] { "+359887333444" });
  16.             Assert.AreEqual(true, isNew);
  17.             Assert.AreEqual(1, phonebook.EntriesCount);
  18.             Assert.AreEqual(1, phonebook.PhonesCount);
  19.  
  20.             // Notes: we have intentionally added the properties EntriesCount
  21.             // and PhonesCount in the PhonebookRepository class to simplify testing
  22.         }
  23.  
  24.         [TestMethod]
  25.         public void TestAddDuplicatedEntry()
  26.         {
  27.             PhonebookRepository phonebook = new PhonebookRepository();
  28.             bool isNew = phonebook.AddPhone("Nakov", new string[] { "+359887333444" });
  29.             Assert.AreEqual(true, isNew);
  30.             isNew = phonebook.AddPhone("Nakov", new string[] { "+359887333444" });
  31.             Assert.AreEqual(false, isNew);
  32.             isNew = phonebook.AddPhone("Nakov", new string[] { "+359887333444" });
  33.             Assert.AreEqual(false, isNew);
  34.             Assert.AreEqual(1, phonebook.EntriesCount);
  35.             Assert.AreEqual(1, phonebook.PhonesCount);
  36.         }
  37.  
  38.         [TestMethod]
  39.         public void TestAddEntryDifferentCasing()
  40.         {
  41.             PhonebookRepository phonebook = new PhonebookRepository();
  42.             bool isNew = phonebook.AddPhone("Nakov", new string[] { "+359887333444" });
  43.             Assert.AreEqual(true, isNew);
  44.             isNew = phonebook.AddPhone("NAKOV", new string[] { "+359887333555" });
  45.             Assert.AreEqual(false, isNew);
  46.             isNew = phonebook.AddPhone("nakov", new string[] { "+359887333777" });
  47.             Assert.AreEqual(false, isNew);
  48.             Assert.AreEqual(1, phonebook.EntriesCount);
  49.             Assert.AreEqual(3, phonebook.PhonesCount);
  50.         }
  51.  
  52.         [TestMethod]
  53.         public void TestAddWithMerge()
  54.         {
  55.             PhonebookRepository phonebook = new PhonebookRepository();
  56.             bool isNew = phonebook.AddPhone("Nakov", new string[] { "+359887333444", "+359887333555" });
  57.             Assert.AreEqual(true, isNew);
  58.             isNew = phonebook.AddPhone("Nakov", new string[] { "+359887333555", "+359887333777" });
  59.             Assert.AreEqual(false, isNew);
  60.             isNew = phonebook.AddPhone("Nakov", new string[] { "+359887333555" });
  61.             Assert.AreEqual(false, isNew);
  62.             Assert.AreEqual(1, phonebook.EntriesCount);
  63.             Assert.AreEqual(3, phonebook.PhonesCount);
  64.         }
  65.  
  66.         [TestMethod]
  67.         public void TestMethodAddPhoneManyForms()
  68.         {
  69.             // Conversion to cannonical form is not responsibility of the PhonebookRepository
  70.             // class and thus the below phones are considered different
  71.  
  72.             PhonebookRepository phonebook = new PhonebookRepository();
  73.             phonebook.AddPhone("Nakov", new string[] { "+359887333555" });
  74.             phonebook.AddPhone("Nakov", new string[] { "0887 333 555" });
  75.             phonebook.AddPhone("Nakov", new string[] { "0887 33 35 55" });
  76.             phonebook.AddPhone("Nakov", new string[] { "+359 887 33 35 55" });
  77.             phonebook.AddPhone("Nakov", new string[] { "(+359) 887 33 35 55" });
  78.             phonebook.AddPhone("Nakov", new string[] { "(+359) 887 333-555" });
  79.             phonebook.AddPhone("Nakov", new string[] { "0887 / 33 35 55" });
  80.             Assert.AreEqual(1, phonebook.EntriesCount);
  81.             Assert.AreEqual(7, phonebook.PhonesCount);
  82.         }
  83.  
  84.         [TestMethod]
  85.         public void TestListSingleEntrySinglePhone()
  86.         {
  87.             PhonebookRepository phonebook = new PhonebookRepository();
  88.             phonebook.AddPhone("Nakov", new string[] { "+359887333555" });
  89.             string expectedResult = "[Nakov: +359887333555]";
  90.             PhonebookEntry[] listedEntries = phonebook.ListEntries(0, 1);
  91.             string actualResult = string.Join("; ", (object[])listedEntries);
  92.             Assert.AreEqual(expectedResult, actualResult);
  93.         }
  94.  
  95.         [TestMethod]
  96.         public void TestListSingleEntryMultiplePhones()
  97.         {
  98.             PhonebookRepository phonebook = new PhonebookRepository();
  99.             phonebook.AddPhone("Nakov", new string[] { "+359887333555" });
  100.             phonebook.AddPhone("Nakov", new string[] { "+3592555444" });
  101.             string expectedResult = "[Nakov: +3592555444, +359887333555]";
  102.             PhonebookEntry[] listedEntries = phonebook.ListEntries(0, 1);
  103.             string actualResult = string.Join("; ", (object[])listedEntries);
  104.             Assert.AreEqual(expectedResult, actualResult);
  105.         }
  106.  
  107.         [TestMethod]
  108.         public void TestListMultipleEntriesWithMergeAndSorting()
  109.         {
  110.             PhonebookRepository phonebook = new PhonebookRepository();
  111.             phonebook.AddPhone("Svetlin Nakov", new string[] { "+359887333555", "+35962445566" });
  112.             phonebook.AddPhone("SVETLIN NAKOV", new string[] { "+3592555444", "+359887333555" });
  113.             phonebook.AddPhone("Niki Kostov", new string[] { "+35989911222", "+35929887744" });
  114.             phonebook.AddPhone("niki kostov", new string[] { "+35929887744", "+35989911222" });
  115.             string expectedResult =
  116.                 "[Niki Kostov: +35929887744, +35989911222]; [Svetlin Nakov: +3592555444, +35962445566, +359887333555]";
  117.             PhonebookEntry[] listedEntries = phonebook.ListEntries(0, 2);
  118.             string actualResult = string.Join("; ", (object[])listedEntries);
  119.             Assert.AreEqual(expectedResult, actualResult);
  120.         }
  121.  
  122.         [TestMethod]
  123.         public void TestListMultipleEntriesSubPageWithSorting()
  124.         {
  125.             PhonebookRepository phonebook = new PhonebookRepository();
  126.             phonebook.AddPhone("Nakov", new string[] { "+359887333555" });
  127.             phonebook.AddPhone("Niki", new string[] { "+35989911222" });
  128.             phonebook.AddPhone("Ani", new string[] { "+359886344544" });
  129.             phonebook.AddPhone("Yana", new string[] { "+3599874456" });
  130.             phonebook.AddPhone("Tanya", new string[] { "+359884222333" });
  131.             string expectedResult =
  132.                 "[Nakov: +359887333555]; [Niki: +35989911222]; [Tanya: +359884222333]";
  133.             PhonebookEntry[] listedEntries = phonebook.ListEntries(1, 3);
  134.             string actualResult = string.Join("; ", (object[])listedEntries);
  135.             Assert.AreEqual(expectedResult, actualResult);
  136.         }
  137.  
  138.         [TestMethod]
  139.         [ExpectedException(typeof(ArgumentOutOfRangeException))]
  140.         public void TestListMultipleEntriesNegativeStart()
  141.         {
  142.             PhonebookRepository phonebook = new PhonebookRepository();
  143.             phonebook.AddPhone("Nakov", new string[] { "+359887333555" });
  144.             phonebook.ListEntries(-1, 1);
  145.         }
  146.  
  147.         [TestMethod]
  148.         [ExpectedException(typeof(ArgumentOutOfRangeException))]
  149.         public void TestListMultipleEntriesInvalidStart()
  150.         {
  151.             PhonebookRepository phonebook = new PhonebookRepository();
  152.             phonebook.AddPhone("Nakov", new string[] { "+359887333555" });
  153.             phonebook.ListEntries(1, 1);
  154.         }
  155.  
  156.         [TestMethod]
  157.         [ExpectedException(typeof(ArgumentOutOfRangeException))]
  158.         public void TestListMultipleEntriesInvalidCount()
  159.         {
  160.             PhonebookRepository phonebook = new PhonebookRepository();
  161.             phonebook.AddPhone("Nakov", new string[] { "+359887333555" });
  162.             phonebook.AddPhone("Jorro", new string[] { "+359888444777" });
  163.             phonebook.ListEntries(1, 2);
  164.         }
  165.  
  166.         [TestMethod]
  167.         public void TestChangeSingleExistingPhone()
  168.         {
  169.             PhonebookRepository phonebook = new PhonebookRepository();
  170.             phonebook.AddPhone("Nakov", new string[] { "+359887333555" });
  171.             int changedPhonesCount = phonebook.ChangePhone("+359887333555", "+359888888888");
  172.             Assert.AreEqual(1, changedPhonesCount);
  173.             string expectedResult = "[Nakov: +359888888888]";
  174.             PhonebookEntry[] listedEntries = phonebook.ListEntries(0, 1);
  175.             string actualResult = string.Join("; ", (object[])listedEntries);
  176.             Assert.AreEqual(expectedResult, actualResult);
  177.         }
  178.  
  179.         [TestMethod]
  180.         public void TestChangeSingleNonExistingPhone()
  181.         {
  182.             PhonebookRepository phonebook = new PhonebookRepository();
  183.             phonebook.AddPhone("Nakov", new string[] { "+359887333555" });
  184.             phonebook.AddPhone("Niki", new string[] { "+359887333666" });
  185.             int changedPhonesCount = phonebook.ChangePhone("+359887333777", "+359888888888");
  186.             Assert.AreEqual(0, changedPhonesCount);
  187.             string expectedResult = "[Nakov: +359887333555]; [Niki: +359887333666]";
  188.             PhonebookEntry[] listedEntries = phonebook.ListEntries(0, 2);
  189.             string actualResult = string.Join("; ", (object[])listedEntries);
  190.             Assert.AreEqual(expectedResult, actualResult);
  191.         }
  192.  
  193.         [TestMethod]
  194.         public void TestChangeSharedPhone()
  195.         {
  196.             PhonebookRepository phonebook = new PhonebookRepository();
  197.             phonebook.AddPhone("Nakov", new string[] { "+359887333555", "+3592981981" });
  198.             phonebook.AddPhone("Ina", new string[] { "+3592981981" });
  199.             phonebook.AddPhone("Aneliya", new string[] { "+3592981981" });
  200.             phonebook.AddPhone("Niki", new string[] { "+3592981981", "+359999888777" });
  201.             int changedPhonesCount = phonebook.ChangePhone("+3592981981", "+3592982982");
  202.             Assert.AreEqual(4, changedPhonesCount);
  203.             string expectedResult =
  204.                 "[Aneliya: +3592982982]; [Ina: +3592982982]; [Nakov: +3592982982, +359887333555]; " +
  205.                 "[Niki: +3592982982, +359999888777]";
  206.             PhonebookEntry[] listedEntries = phonebook.ListEntries(0, 4);
  207.             string actualResult = string.Join("; ", (object[])listedEntries);
  208.             Assert.AreEqual(expectedResult, actualResult);
  209.         }
  210.  
  211.         [TestMethod]
  212.         public void TestChangePhoneWithMerge()
  213.         {
  214.             PhonebookRepository phonebook = new PhonebookRepository();
  215.             phonebook.AddPhone("Nakov", new string[] { "+359887333555", "+359887333999" });
  216.             phonebook.AddPhone("Ina", new string[] { "+359887333999" });
  217.             phonebook.AddPhone("Ani", new string[] { "+359887333555", "359887333444" });
  218.             int changedPhonesCount = phonebook.ChangePhone("+359887333999", "+359887333555");
  219.             Assert.AreEqual(2, changedPhonesCount);
  220.             string expectedResult =
  221.                 "[Ani: +359887333555, 359887333444]; [Ina: +359887333555]; [Nakov: +359887333555]";
  222.             PhonebookEntry[] listedEntries = phonebook.ListEntries(0, 3);
  223.             string actualResult = string.Join("; ", (object[])listedEntries);
  224.             Assert.AreEqual(expectedResult, actualResult);
  225.         }
  226.     }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment