using System; using System.Collections.Generic; using System.IO; using Application; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace UnitTestProject { [TestClass] public class UserTest { public int setArrangePesel(string pesel) { User user = new User(); user.Pesel = pesel; return user.CheckingPesel(); } public void setArrangeListOfUsers(ref List list_of_users) { list_of_users.Add(new User() { FirstName = "Adam", LastName = "Kowalski", City = "Poznań", Pesel = "98042155976" }); list_of_users.Add(new User() { FirstName = "Jan", LastName = "Nowak", City = "Bydgoszcz", Pesel = "85070789433" }); list_of_users.Add(new User() { FirstName = "Adam", LastName = "Kowalski", City = "Warszawa", Pesel = "55122952416" }); } public bool CheckConditionOfUserExist(ref List list_of_users,User u1) { bool condition = false; foreach (var user in list_of_users) { if (u1.Equals(user)) { condition = true; break; } } return condition; } //////////////////////////////////////////////////////////////////////////// /// check Pesel [TestMethod] public void CheckingValidPesel() { //arrange int actualResult = setArrangePesel("47042886355"); int expectedResult = 0; //Assert Assert.AreEqual(expectedResult, actualResult); } ///checking invalid pesel [TestMethod] public void InvalidDigitControl() { //arrange int actualResult = setArrangePesel("47042886356"); int expectedResult = 1; //Assert Assert.AreEqual(expectedResult, actualResult); } [TestMethod] public void InvalidDateOfPesel() { //arrange int actualResult = setArrangePesel("47007286356"); int expectedResult = 2; //Assert Assert.AreEqual(expectedResult, actualResult); } [TestMethod] public void InvalidCharactersOfPesel() { //arrange int actualResult = setArrangePesel("47042886a56"); int expectedResult = 3; //Assert Assert.AreEqual(expectedResult, actualResult); } [TestMethod] public void InvalidLengthOfPesel() { //arrange int actualResult = setArrangePesel("474"); int expectedResult = 4; //Assert Assert.AreEqual(expectedResult, actualResult); } //////////////////////////////////////////////////////////////////////////// //user exists [TestMethod] public void CheckNotExistingUser() { //arrange var list_of_users = new List(); setArrangeListOfUsers(ref list_of_users); User u1 =new User(){ FirstName = "Andrzej", LastName = "Frankowski", City = "Lublin", Pesel = "71052429624" }; bool conditionBeforeAddUser = CheckConditionOfUserExist(ref list_of_users, u1); bool condition = User.UserExists(ref list_of_users, u1); bool conditionAfterAddUser = CheckConditionOfUserExist(ref list_of_users, u1); //Assert Assert.IsFalse(condition); Assert.IsFalse(conditionBeforeAddUser); Assert.IsFalse(conditionAfterAddUser); } //user with this same pesel but diffrent name and surname [TestMethod] public void CheckExistingUser1() { //arrange var list_of_users = new List(); setArrangeListOfUsers(ref list_of_users); User u1 = new User() { FirstName = "Andrzej", LastName = "Frankowski", City = "Lublin", Pesel = "55122952416" }; bool conditionBeforeAddUser = CheckConditionOfUserExist(ref list_of_users, u1); bool condition = User.UserExists(ref list_of_users, u1); bool conditionAfterAddUser = CheckConditionOfUserExist(ref list_of_users, u1); //Assert Assert.IsTrue(condition); Assert.IsFalse(conditionBeforeAddUser); Assert.IsTrue(conditionAfterAddUser); } //user with this same pesel and other data [TestMethod] public void CheckExistingUser2() { //arrange var list_of_users = new List(); setArrangeListOfUsers(ref list_of_users); User u1 = new User() { FirstName = "Adam", LastName = "Kowalski", City = "Warszawa", Pesel = "55122952416" }; bool conditionBeforeAddUser = CheckConditionOfUserExist(ref list_of_users, u1); bool condition = User.UserExists(ref list_of_users, u1); bool conditionAfterAddUser = CheckConditionOfUserExist(ref list_of_users, u1); //Assert Assert.IsTrue(condition); Assert.IsTrue(conditionBeforeAddUser); Assert.IsTrue(conditionAfterAddUser); } //////////////////////////////////////////////////////////////////////////// /// check method equals [TestMethod] public void CheckMethodEquals() { //arrange User user =new User() { FirstName = "Adam", LastName = "Kowalski", City = "Warszawa", Pesel = "55122952416" }; User user1= new User() { FirstName = "Adam", LastName = "Kowalski", City = "Warszawa", Pesel = "55122952416" }; User user2= new User() { FirstName = "Jan", LastName = "Nowak", City = "Warszawa", Pesel = "71052429624" }; Object obj = new Object(); /// equals bool conditionEquals = user.Equals(user1); /// not equals bool conditionNotEquals = user.Equals(user2); //Assert Assert.IsTrue(conditionEquals); Assert.IsFalse(conditionNotEquals); /// throw exception - incorrect comparing object Assert.ThrowsException(() => user.Equals(obj)); } } }