From 9f1cf7c25eebb6e2cd635afc6d2b7d7c7552bfc7 Mon Sep 17 00:00:00 2001 From: hulivet1 Date: Fri, 21 Oct 2022 15:32:54 +0200 Subject: [PATCH] =?UTF-8?q?Ajout=20de=20tests=20de=20classe=20d'exception?= =?UTF-8?q?=20et=20am=C3=A9loration=20de=20inscrit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Code/Model/Inscrit.cs | 59 +++++++++++++--- Code/Model/InvalidMailException.cs | 32 +++++++++ Code/Model/InvalidPasswordException.cs | 32 +++++++++ Code/TestsUnitaires/TU_Compte.cs | 18 ----- Code/TestsUnitaires/TestUnitCompte.cs | 65 ++++++++--------- Code/TestsUnitaires/TestUnitInscrit.cs | 96 ++++++++++++++++++++++++++ 6 files changed, 244 insertions(+), 58 deletions(-) create mode 100644 Code/Model/InvalidMailException.cs create mode 100644 Code/Model/InvalidPasswordException.cs delete mode 100644 Code/TestsUnitaires/TU_Compte.cs create mode 100644 Code/TestsUnitaires/TestUnitInscrit.cs diff --git a/Code/Model/Inscrit.cs b/Code/Model/Inscrit.cs index cba18c5..3b66995 100644 --- a/Code/Model/Inscrit.cs +++ b/Code/Model/Inscrit.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; //using Banque; @@ -9,6 +10,56 @@ namespace Model { public class Inscrit { + + public string Id { get; private set; } + public string Nom { get; private set; } + + public string Mail + { + get => mail; + set + { + if (value.Length == 0) + { + throw new InvalidMailException(value, "Longueur d'un mail doit être superieur a 0", new ArgumentNullException()); + } + if (!Regex.IsMatch(value, "(@)(.+)")) + { + throw new InvalidMailException(value, "Un mail doit contenir le symbole '@'"); + } + mail = value; + } + } + private string mail; + + public string Prenom { get; private set; } + + public string Mdp + { + get => mdp; + set + { + if (value.Length <= 8) + { + throw new InvalidPasswordException(value, "La longeur d'un mot de passe doit être obligatoirement superieure a 8"); + } + if (!Regex.IsMatch(value, "[A-Z]+")) + { + throw new InvalidPasswordException(value, "Le mot de passe doit contenir au moins une lettre majuscule"); + } + if (!Regex.IsMatch(value, "[0-9]+")) + { + throw new InvalidPasswordException(value, "Le mot de passe doit contenir au moins un chiffre"); + } + mdp = value; + } + } + private string mdp; + + public double SoldeTotal { get; private set; } + public Devises Dev { get; private set; } + public List LesBanques { get; private set; } = new List(); + public Inscrit(string id, string nom, string mail, string prenom, string mdp, double soldeTotal) { Id = id; @@ -28,14 +79,6 @@ namespace Model SoldeTotal = soldeTotal; LesBanques = lesbanques; } - public string Id { get; private set; } - public string Nom { get; private set; } - public string Mail { get; private set; } - public string Prenom { get; private set; } - public string Mdp { get; private set; } - public double SoldeTotal { get; private set; } - public Devises Dev { get; private set; } - public List LesBanques { get; private set; } = new List(); public void ajouterBanque(Banque banque) { diff --git a/Code/Model/InvalidMailException.cs b/Code/Model/InvalidMailException.cs new file mode 100644 index 0000000..d94da07 --- /dev/null +++ b/Code/Model/InvalidMailException.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Model +{ + public class InvalidMailException : ArgumentException + { + private string Mail { get; set; } + public InvalidMailException():base() + { } + + public InvalidMailException(string mail): + base(String.Format("{0} n'est pas un mail valide.", mail)) + { + Mail = mail; + } + public InvalidMailException(string mail, string message): + base(message) + { + Mail = mail; + } + + public InvalidMailException(string mail, string message, Exception innerException) : + base(message, innerException) + { + Mail = mail; + } + } +} diff --git a/Code/Model/InvalidPasswordException.cs b/Code/Model/InvalidPasswordException.cs new file mode 100644 index 0000000..8deef1a --- /dev/null +++ b/Code/Model/InvalidPasswordException.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Model +{ + public class InvalidPasswordException : ArgumentException + { + private string Mdp { get; set; } + public InvalidPasswordException() : base() + { } + + public InvalidPasswordException(string mdp) : + base(String.Format("{0} n'est pas un mot de passe valide.", mdp)) + { + Mdp = mdp; + } + public InvalidPasswordException(string mdp, string message) : + base(message) + { + Mdp = mdp; + } + + public InvalidPasswordException(string mdp, string message, Exception innerException) : + base(message, innerException) + { + Mdp = mdp; + } + } +} diff --git a/Code/TestsUnitaires/TU_Compte.cs b/Code/TestsUnitaires/TU_Compte.cs deleted file mode 100644 index 9817352..0000000 --- a/Code/TestsUnitaires/TU_Compte.cs +++ /dev/null @@ -1,18 +0,0 @@ -using Model; - -namespace TestsUnitaires -{ - public class TU_Compte - { - - [Fact] - public void Ctor_Compte() - { - Compte c = new Compte("Crédit Agricole", 20000); - Assert.NotNull(c); - Assert.Equal("Crédit Agricole", c.Nom); - Assert.Equal(20000, c.Solde); - } - - } -} \ No newline at end of file diff --git a/Code/TestsUnitaires/TestUnitCompte.cs b/Code/TestsUnitaires/TestUnitCompte.cs index 739b9f7..202e5fd 100644 --- a/Code/TestsUnitaires/TestUnitCompte.cs +++ b/Code/TestsUnitaires/TestUnitCompte.cs @@ -1,35 +1,36 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Model; - -namespace TestsUnitaires -{ - public class TestUnitCompte - { - [Fact] - public void TestConstructeurCompte() - { - Compte c1 = new("Livret A", 234); - Compte c2 = new("&e23R_te7", 1245.34); - Assert.Equal("Livret A", c1.Nom); - Assert.Equal("&e23R_te7", c2.Nom); - Assert.Equal(234, c1.Solde); - Assert.Equal(1245.34, c2.Solde); +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Model; + +namespace TestsUnitaires +{ + public class TestUnitCompte + { + + [Fact] + public void TestConstructeurCompte() + { + Compte c1 = new("Livret A", 234); + Compte c2 = new("&e23R_te7", 1245.34); + Assert.Equal("Livret A", c1.Nom); + Assert.Equal("&e23R_te7", c2.Nom); + Assert.Equal(234, c1.Solde); + Assert.Equal(1245.34, c2.Solde); } - [Fact] - public void testSupprimerBanque() - { - Banque bq = new Banque("Crédit Agricole", "https://creditagricole.fr", "https://yt3.ggpht.com/a/AGF-l7_mEfX2eQaGm8GefLOg5ZMRciNw-pESE3gUWg=s900-c-k-c0xffffffff-no-rj-mo"); - Inscrit i1 = new Inscrit("A1001", "Smith", "smith@gmail.com", "luke", "test", 500); - Assert.NotNull(i1.LesBanques); - i1.ajouterBanque(bq); - Assert.Contains(bq, i1.LesBanques); - i1.SupprimerBanque(bq); + [Fact] + public void testSupprimerBanque() + { + Banque bq = new Banque("Crédit Agricole", "https://creditagricole.fr", "https://yt3.ggpht.com/a/AGF-l7_mEfX2eQaGm8GefLOg5ZMRciNw-pESE3gUWg=s900-c-k-c0xffffffff-no-rj-mo"); + Inscrit i1 = new Inscrit("A1001", "Smith", "smith@gmail.com", "luke", "test20000aA", 500); + Assert.NotNull(i1.LesBanques); + i1.ajouterBanque(bq); + Assert.Contains(bq, i1.LesBanques); + i1.SupprimerBanque(bq); Assert.DoesNotContain(bq, i1.LesBanques); - } - } -} + } + } +} diff --git a/Code/TestsUnitaires/TestUnitInscrit.cs b/Code/TestsUnitaires/TestUnitInscrit.cs new file mode 100644 index 0000000..da00272 --- /dev/null +++ b/Code/TestsUnitaires/TestUnitInscrit.cs @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Model; + +namespace TestsUnitaires +{ + public class TestUnitInscrit + { + [Fact] + public void testCtorInscrit() + { + Inscrit i = new Inscrit("I001", "LIVET", "Hugo.LIVET@etu.uca.fr", "Hugo", "Tu Sauras Passss:)1215", 2000); + Assert.NotNull(i); + Assert.Equal("I001", i.Id); + Assert.Equal("LIVET", i.Nom); + Assert.Equal("Hugo.LIVET@etu.uca.fr", i.Mail); + Assert.Equal("Hugo", i.Prenom); + Assert.Equal("Tu Sauras Passss:)1215", i.Mdp); + Assert.Equal(2000, i.SoldeTotal); + } + + [Fact] + public void testCtorInscrit2() + { + List lesBanques = new List(); + Banque b = new Banque("CA", "enavantouioui.fr", "NaN.fr"); + lesBanques.Add(b); + Inscrit i = new Inscrit("I001", "LIVET", "Hugo.LIVET@etu.uca.fr", "Hugo", "Tu Sauras Passss:)1215", 2000, lesBanques); + Assert.NotNull(i); + Assert.Equal("I001", i.Id); + Assert.Equal("LIVET", i.Nom); + Assert.Equal("Hugo.LIVET@etu.uca.fr", i.Mail); + Assert.Equal("Hugo", i.Prenom); + Assert.Equal("Tu Sauras Passss:)1215", i.Mdp); + Assert.Equal(2000, i.SoldeTotal); + Assert.Contains(b, i.LesBanques); + lesBanques.Remove(b); + Assert.DoesNotContain(b, i.LesBanques); + } + + [Fact] + public void testAjoutBanqueInscrit() + { + Banque b = new Banque("CA", "enavantouioui.fr", "NaN.fr"); + Inscrit i = new Inscrit("I001", "LIVET", "Hugo.LIVET@etu.uca.fr", "Hugo", "Tu Sauras Passss:)1215", 2000); + i.ajouterBanque(b); + Assert.Contains(b, i.LesBanques); + } + + [Fact] + public void testSupprimerBanqueInscrit() + { + Banque b = new Banque("CA", "enavantouioui.fr", "NaN.fr"); + Inscrit i = new Inscrit("I001", "LIVET", "Hugo.LIVET@etu.uca.fr", "Hugo", "Tu Sauras Passss:)1215", 2000); + i.ajouterBanque(b); + i.SupprimerBanque(b); + Assert.DoesNotContain(b, i.LesBanques); + i.ajouterBanque(new Banque("CA", "enavantouioui.fr", "NaN.fr")); + i.SupprimerBanque(new Banque("CA", "enavantouioui.fr", "NaN.fr")); + Assert.DoesNotContain(new Banque("CA", "enavantouioui.fr", "NaN.fr"), i.LesBanques); + } + + [Fact] + public void testChoixDeviseInscrit() + { + Inscrit i = new Inscrit("I001", "LIVET", "Hugo.LIVET@etu.uca.fr", "Hugo", "Tu Sauras Passss:)1215", 2000); + i.ChoisirDevise(Devises.Euro); + Assert.Equal(Devises.Euro, i.Dev); + } + + [Theory] + [InlineData("I000001", "LIVET", "a@a.fr", "Hugo", "123Soleil@azerty", 20000, true)]//OK + [InlineData("I000002", "LIVET", "aa.fr", "Hugo", "123Soleil@azerty", 20000, false)]//Mail invalide psk pas de @ + [InlineData("I000003", "LIVET", "a@a.fr", "Hugo", "123soleil@azerty", 20000, false)]//mdp Invalide psk mdp sans Maj + [InlineData("I000004", "LIVET", "a@a.fr", "Hugo", "Soleil@azerty", 20000, false)]//mdp Invalide psk pas de chiffres + public void CtorInscrit2TU(string id, string nom, string mail, string prenom, string mdp, double solde, bool notShouldThrowException) + { + if (!notShouldThrowException) + { + Assert.ThrowsAny(() => new Inscrit(id, nom, mail, prenom, mdp, solde)); + return; + } + Inscrit i = new Inscrit(id, nom, mail, prenom, mdp, solde); + Assert.NotNull(i); + Assert.Equal(id, i.Id); + Assert.Equal(nom, i.Nom); + Assert.Equal(mail, i.Mail); + Assert.Equal(prenom, i.Prenom); + Assert.Equal(mdp, i.Mdp); + Assert.Equal(solde, i.SoldeTotal); + } + } +}