From 6c58c3218f130812c3b5210509b3f341321c0565 Mon Sep 17 00:00:00 2001 From: Yoan Date: Wed, 14 Jun 2023 18:10:03 +0200 Subject: [PATCH] Tests unitaires, Int2Color converter et test coverage --- .drone.yml | 2 +- Sources/Model/Managers/Manager.cs | 18 +++--- .../Ohara/Converters/Int2ColorConverter.cs | 31 ++++++++++ Sources/Ohara/ModalBateau.xaml | 10 +++- Sources/Ohara/ModalBateau.xaml.cs | 12 ++++ Sources/Ohara/ModalEquipage.xaml | 8 ++- Sources/Ohara/ModalEquipage.xaml.cs | 10 ++++ Sources/Ohara/ModalIle.xaml | 8 ++- Sources/Ohara/ModalIle.xaml.cs | 10 ++++ Sources/Ohara/PageFavoris.xaml.cs | 2 +- Sources/TestConsole/Program.cs | 4 +- Sources/TestProject1/TestBestiaire.cs | 20 +++++++ Sources/TestProject1/TestManager.cs | 2 +- Sources/TestProject1/XMLSerializerTests.cs | 59 +++++++++++++++++-- 14 files changed, 172 insertions(+), 24 deletions(-) create mode 100644 Sources/Ohara/Converters/Int2ColorConverter.cs diff --git a/.drone.yml b/.drone.yml index 1cbf34b..4f6cb47 100644 --- a/.drone.yml +++ b/.drone.yml @@ -30,7 +30,7 @@ steps: sonar_token: from_secret: SECRET_SONAR_LOGIN project_key: Ohara - coverage_exclusions: "Tests/**" + coverage_exclusions: "Tests/**","Ohara/**" commands: - cd Sources/ - dotnet restore Ohara.sln diff --git a/Sources/Model/Managers/Manager.cs b/Sources/Model/Managers/Manager.cs index 6f5e08a..d28ebfb 100644 --- a/Sources/Model/Managers/Manager.cs +++ b/Sources/Model/Managers/Manager.cs @@ -181,15 +181,15 @@ namespace Model.Managers } /// \brief Récupère la liste des objets Ohara ayant le parametre EstFavori égal a true. /// \return La liste des objets Ohara ayant le parametre EstFavori égal a true. - public List GetFavoris() - { - List listeFavoris = new List(); - listeFavoris.AddRange(Bateaux); - listeFavoris.AddRange(Equipages); - listeFavoris.AddRange(Bestiaire); - listeFavoris.AddRange(Fruits); - listeFavoris.AddRange(Iles); - listeFavoris.AddRange(Personnages); + public HashSet GetFavoris() + { + HashSet listeFavoris = new HashSet(); + listeFavoris.UnionWith(Bateaux); + listeFavoris.UnionWith(Equipages); + listeFavoris.UnionWith(Bestiaire); + listeFavoris.UnionWith(Fruits); + listeFavoris.UnionWith(Iles); + listeFavoris.UnionWith(Personnages); foreach(ObjetOhara obj in listeFavoris.ToList()) { if (obj.EstFavori == false) diff --git a/Sources/Ohara/Converters/Int2ColorConverter.cs b/Sources/Ohara/Converters/Int2ColorConverter.cs new file mode 100644 index 0000000..bb3f5b4 --- /dev/null +++ b/Sources/Ohara/Converters/Int2ColorConverter.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Ohara.Converters +{ + public class Int2ColorConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + if (value is not int) return Colors.Red; + switch (value) + { + case < 0: + return Colors.Orange; + case >0: + return Colors.Green; + default: + return Colors.White; + } + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } +} diff --git a/Sources/Ohara/ModalBateau.xaml b/Sources/Ohara/ModalBateau.xaml index b60df87..237f75c 100644 --- a/Sources/Ohara/ModalBateau.xaml +++ b/Sources/Ohara/ModalBateau.xaml @@ -2,9 +2,13 @@ + + + @@ -24,13 +28,13 @@ - - diff --git a/Sources/Ohara/ModalBateau.xaml.cs b/Sources/Ohara/ModalBateau.xaml.cs index 6b09d8e..d48bdcd 100644 --- a/Sources/Ohara/ModalBateau.xaml.cs +++ b/Sources/Ohara/ModalBateau.xaml.cs @@ -1,3 +1,4 @@ + using Model.Classes; using Model.Managers; @@ -27,12 +28,23 @@ public partial class ModalBateau : ContentPage { if (manager.SelectedItem != null) { + if (nouveauBateau.PremierChap < 0 || nouveauBateau.PremierEp < 0) + { + await DisplayAlert("Erreur", "Les paramètres PremierChap et PremierEp doivent être superieur à 0.", "OK"); + return; + } manager.ModifierBateau(nouveauBateau, ancienNom); nouveauBateau = manager.SelectedItem as Bateau; } else { + if (nouveauBateau.PremierChap<0 || nouveauBateau.PremierEp < 0) + { + await DisplayAlert("Erreur", "Les paramètres PremierChap et PremierEp doivent être superieur à 0.", "OK"); + return; + } manager.AjouterBateau(nouveauBateau); + } await Navigation.PopModalAsync(); diff --git a/Sources/Ohara/ModalEquipage.xaml b/Sources/Ohara/ModalEquipage.xaml index 4866043..ef5b200 100644 --- a/Sources/Ohara/ModalEquipage.xaml +++ b/Sources/Ohara/ModalEquipage.xaml @@ -2,9 +2,13 @@ + + + @@ -36,13 +40,13 @@ - - diff --git a/Sources/Ohara/ModalEquipage.xaml.cs b/Sources/Ohara/ModalEquipage.xaml.cs index a50c0d1..2e7256e 100644 --- a/Sources/Ohara/ModalEquipage.xaml.cs +++ b/Sources/Ohara/ModalEquipage.xaml.cs @@ -30,11 +30,21 @@ public partial class ModalEquipage : ContentPage { if (manager.SelectedItem != null) { + if (nouvelEquipage.PremierChap < 0 || nouvelEquipage.PremierEp < 0) + { + await DisplayAlert("Erreur", "Les paramètres PremierChap et PremierEp doivent être superieur à 0.", "OK"); + return; + } manager.ModifierEquipage(nouvelEquipage, ancienNom); nouvelEquipage = manager.SelectedItem as Equipage; } else { + if (nouvelEquipage.PremierChap < 0 || nouvelEquipage.PremierEp < 0) + { + await DisplayAlert("Erreur", "Les paramètres PremierChap et PremierEp doivent être superieur à 0.", "OK"); + return; + } manager.AjouterEquip(nouvelEquipage); } diff --git a/Sources/Ohara/ModalIle.xaml b/Sources/Ohara/ModalIle.xaml index 91c8a4d..d94a965 100644 --- a/Sources/Ohara/ModalIle.xaml +++ b/Sources/Ohara/ModalIle.xaml @@ -2,9 +2,13 @@ + + + @@ -36,13 +40,13 @@ - - diff --git a/Sources/Ohara/ModalIle.xaml.cs b/Sources/Ohara/ModalIle.xaml.cs index 16292d3..2959dd4 100644 --- a/Sources/Ohara/ModalIle.xaml.cs +++ b/Sources/Ohara/ModalIle.xaml.cs @@ -29,11 +29,21 @@ public partial class ModalIle : ContentPage { if (manager.SelectedItem != null) { + if (nouvelIle.PremierChap < 0 || nouvelIle.PremierEp < 0) + { + await DisplayAlert("Erreur", "Les paramètres PremierChap et PremierEp doivent être superieur à 0.", "OK"); + return; + } manager.ModifierIle(nouvelIle,ancienNom); manager.SelectedItem=nouvelIle; } else { + if (nouvelIle.PremierChap < 0 || nouvelIle.PremierEp < 0) + { + await DisplayAlert("Erreur", "Les paramètres PremierChap et PremierEp doivent être superieur à 0.", "OK"); + return; + } manager.AjouterIle(nouvelIle); } diff --git a/Sources/Ohara/PageFavoris.xaml.cs b/Sources/Ohara/PageFavoris.xaml.cs index cd7dec6..e1723ca 100644 --- a/Sources/Ohara/PageFavoris.xaml.cs +++ b/Sources/Ohara/PageFavoris.xaml.cs @@ -15,7 +15,7 @@ public partial class PageFavoris : ContentPage void OnTextChanged(object sender, EventArgs e) { SearchBar searchBar = (SearchBar)sender; - BindableLayout.SetItemsSource(listeFav, manager.RechercheObjetOhara(searchBar.Text, manager.GetFavoris())); + BindableLayout.SetItemsSource(listeFav, manager.RechercheObjetOhara(searchBar.Text, manager.GetFavoris().ToList())); } searchBar.TextChanged += OnTextChanged; diff --git a/Sources/TestConsole/Program.cs b/Sources/TestConsole/Program.cs index c99a4b6..329d999 100644 --- a/Sources/TestConsole/Program.cs +++ b/Sources/TestConsole/Program.cs @@ -4,6 +4,8 @@ using Model.Serializer; using Model.Managers; using System.Collections.ObjectModel; + + // Tests fonctionnels : Manager manager = new Manager(new XML_Serializer()); @@ -105,7 +107,7 @@ foreach (Bateau b in manager.Bateaux) // Recherche dansune liste de objet ohara pour afficher les objets ohara correspondant au mot clé "Sunny" -foreach (ObjetOhara o in manager.RechercheObjetOhara("Sunny", manager.GetFavoris())) +foreach (ObjetOhara o in manager.RechercheObjetOhara("Sunny", manager.GetFavoris().ToList())) { Console.WriteLine(o.Nom); } diff --git a/Sources/TestProject1/TestBestiaire.cs b/Sources/TestProject1/TestBestiaire.cs index 12ce704..7f0ad5f 100644 --- a/Sources/TestProject1/TestBestiaire.cs +++ b/Sources/TestProject1/TestBestiaire.cs @@ -37,5 +37,25 @@ namespace TestProject1 Assert.False(resultat2); Assert.False(resultat3); } + [Fact] + public void GetHashCode_BestiaireWithSameProperties_ReturnsSameHashCode() + { + Bestiaire obj1 = new Bestiaire("Humains", "??", "Les humains sont ...", "Ils possèdent les caractéristiques suivantes ...", ""); + Bestiaire obj2 = new Bestiaire("Humains", "??", "Les humains sont ...", "Ils possèdent les caractéristiques suivantes ...", ""); + + var hashCode1 = obj1.GetHashCode(); + var hashCode2 = obj2.GetHashCode(); + + Assert.True(hashCode1 == hashCode2); + } + [Fact] + public void ToString_ReturnsCorrectStringRepresentation() + { + Bestiaire obj1 = new Bestiaire("Humains", "??", "Les humains sont ...", "Ils possèdent les caractéristiques suivantes ...", ""); + + var result = obj1.ToString(); + + Assert.True("Bestiaire : Humains False ?? Les humains sont ... Ils possèdent les caractéristiques suivantes ... baseimage.png" == result); + } } } diff --git a/Sources/TestProject1/TestManager.cs b/Sources/TestProject1/TestManager.cs index d2bf4e1..3eac1ac 100644 --- a/Sources/TestProject1/TestManager.cs +++ b/Sources/TestProject1/TestManager.cs @@ -110,7 +110,7 @@ namespace TestProject1 var obj1 =manager.Fruits.First(); var obj2 = manager.Equipages.First(); manager.ModifierFavFDD(obj1, true); - var result = manager.GetFavoris(); + var result = manager.GetFavoris().ToList(); Assert.True(1==result.Count); Assert.Contains(obj1, result); diff --git a/Sources/TestProject1/XMLSerializerTests.cs b/Sources/TestProject1/XMLSerializerTests.cs index 99137a2..ee5442b 100644 --- a/Sources/TestProject1/XMLSerializerTests.cs +++ b/Sources/TestProject1/XMLSerializerTests.cs @@ -14,12 +14,63 @@ namespace TestProject1 public class XMLSerializerTests { public XML_Serializer serializer = new XML_Serializer(); - + private string TestFolderPath = Directory.GetCurrentDirectory(); [Fact] - public void InitialiserFichiers_FileDoesNotExist_SetPersonnageCalled() + public void InitialiserFichiers_FileDoesNotExist_SetPersonnageIfStubManagerReturnsNonNull() { - - Assert.True(File.Exists(Path.Combine(serializer.Chemin, "./personnage.xml"))); + // Arrange + var serializer = new XML_Serializer(TestFolderPath); + var stubManager = new StubManager(); + var expectedPersonnages = stubManager.GetPersonnages(); + var expectedBateaux = stubManager.GetBateaux(); + var expectedFruits = stubManager.GetFruits(); + var expectedBestiaires= stubManager.GetBestiaires(); + var expectedEquipages=stubManager.GetEquipages(); + var expectedIles = stubManager.GetIles(); + + // Act + serializer.InitialiserFichiers(stubManager); + + // Assert + if (!File.Exists(Path.Combine(TestFolderPath, "./personnage.xml"))) + { + if (expectedPersonnages != null) + { + Assert.True(expectedPersonnages.ToList()== serializer.GetPersonnages()); + } + } + + if (!File.Exists(Path.Combine(TestFolderPath, "./bateau.xml"))) + { + if (expectedBateaux != null) + { + Assert.True(expectedBateaux.ToList() == serializer.GetBateaux()); + } + } + + if (!File.Exists(Path.Combine(TestFolderPath, "./fruitdudemon.xml"))) + { + if (expectedFruits != null) + { + Assert.True(expectedFruits.ToList() == serializer.GetFruits()); + } + } + + if (!File.Exists(Path.Combine(TestFolderPath, "./equipage.xml"))) + { + if (expectedEquipages != null) + { + Assert.True(expectedEquipages.ToList() == serializer.GetEquipages()); + } + } + + if (!File.Exists(Path.Combine(TestFolderPath, "./ile.xml"))) + { + if (expectedIles != null) + { + Assert.True(expectedIles.ToList() == serializer.GetIles()); + } + } } [Fact] public void SetPersonnage_SerializesAndWritesToFile()