From 4d98ec337e5d27b30078ebbe733f2f8033198431 Mon Sep 17 00:00:00 2001 From: Jade_VAN_BRABANDT Date: Tue, 6 Jun 2023 17:56:20 +0200 Subject: [PATCH 1/4] Fix : Code smells --- Sources/Stim.Model/Game.cs | 12 ++++++------ Sources/Stim.Model/Manager.cs | 4 ++-- Sources/Stim.Model/User.cs | 34 +++++++++++++++++----------------- Sources/Stim/Create.xaml.cs | 10 +++++----- Sources/Stub/Stub.cs | 9 ++------- 5 files changed, 32 insertions(+), 37 deletions(-) diff --git a/Sources/Stim.Model/Game.cs b/Sources/Stim.Model/Game.cs index 3dcd37c..9cde10f 100644 --- a/Sources/Stim.Model/Game.cs +++ b/Sources/Stim.Model/Game.cs @@ -23,7 +23,7 @@ namespace Model } } } - private string name; + private string name = default!; [DataMember] public string Description @@ -39,7 +39,7 @@ namespace Model } } } - private string description; + private string description = default!; [DataMember] public int Year @@ -55,7 +55,7 @@ namespace Model } } } - private int year; + private int year = default!; [DataMember] public string Cover @@ -71,7 +71,7 @@ namespace Model } } } - private string cover; + private string cover = default!; [DataMember] public ObservableCollection Tags @@ -87,7 +87,7 @@ namespace Model } } } - private ObservableCollection tags; + private ObservableCollection tags = default!; [DataMember] public List Reviews { get; private init; } @@ -112,7 +112,7 @@ namespace Model } } } - private string lien; + private string lien = default!; public Game(string name, string description, int year, List c_tags, string cover, string c_lien) { diff --git a/Sources/Stim.Model/Manager.cs b/Sources/Stim.Model/Manager.cs index 61e2ad3..99c8552 100644 --- a/Sources/Stim.Model/Manager.cs +++ b/Sources/Stim.Model/Manager.cs @@ -27,10 +27,10 @@ namespace Model .Where(game => game.Name.IndexOf(filterName, StringComparison.OrdinalIgnoreCase) >= 0 ); if (filterTag1 != null) retList = retList - .Where(game => game.Tags != null && game.Tags.Any(tag => tag != null && tag.IndexOf(filterTag1, StringComparison.OrdinalIgnoreCase) >= 0) + .Where(game => game.Tags != null && game.Tags.Any(tag => tag != null && tag.Contains(filterTag1, StringComparison.OrdinalIgnoreCase)) ); if (filterTag2 != null) retList = retList - .Where(game => game.Tags != null && game.Tags.Any(tag => tag != null && tag.IndexOf(filterTag2, StringComparison.OrdinalIgnoreCase) >= 0) + .Where(game => game.Tags != null && game.Tags.Any(tag => tag != null && tag.Contains(filterTag2, StringComparison.OrdinalIgnoreCase)) ); return retList; } diff --git a/Sources/Stim.Model/User.cs b/Sources/Stim.Model/User.cs index 10b5a63..d26d83c 100644 --- a/Sources/Stim.Model/User.cs +++ b/Sources/Stim.Model/User.cs @@ -10,6 +10,17 @@ namespace Model [DataContract] public sealed class User : INotifyPropertyChanged , IEquatable { + [DataMember] + public string UserImage + { + get => userImage; + private set + { + if (!string.IsNullOrWhiteSpace(value)) userImage = value; + else userImage = "no_cover.png"; + } + } + private string userImage = default!; [DataMember] public string? Username { @@ -24,11 +35,11 @@ namespace Model } } } - private string username; + private string username=default!; [DataMember] public string Biographie { - get => biographie ?? "Pas de biographie"; + get => biographie; private set { if (string.IsNullOrWhiteSpace(value)) biographie = "Pas de biographie"; @@ -39,7 +50,7 @@ namespace Model } } } - private string biographie; + private string biographie = default!; [DataMember] public string Email { @@ -55,7 +66,7 @@ namespace Model else email = "Default"; } } - private string email; + private string email = default!; [DataMember] public string Password { @@ -71,7 +82,7 @@ namespace Model } } } - private string password; + private string password = default!; public event PropertyChangedEventHandler? PropertyChanged; @@ -89,17 +100,6 @@ namespace Model get; private init; } - [DataMember] - public string? UserImage - { - get => userImage; - private set - { - if (!string.IsNullOrWhiteSpace(value)) userImage = value; - else userImage = "no_cover.png"; - } - } - private string? userImage; public User(string userImage,string username, string biographie, string email, string password) { @@ -111,7 +111,7 @@ namespace Model else Biographie = biographie; if (email == null) Email = "Default"; else Email = email; - if (password == null) throw new ArgumentNullException("password"); + if (password == null) throw new ArgumentNullException(nameof(password)); else Password = password; Followed_Games = new ObservableCollection(); } diff --git a/Sources/Stim/Create.xaml.cs b/Sources/Stim/Create.xaml.cs index b317763..ebda9ff 100644 --- a/Sources/Stim/Create.xaml.cs +++ b/Sources/Stim/Create.xaml.cs @@ -12,15 +12,15 @@ public partial class Create : ContentPage private async void Creer_un_compte(object sender, EventArgs e) { Error.Clear(); - if (!string.IsNullOrWhiteSpace(Username.Text) || !string.IsNullOrWhiteSpace(Pswd.Text) || !string.IsNullOrWhiteSpace(Email.Text)) + if (!(string.IsNullOrWhiteSpace(Username.Text) || string.IsNullOrWhiteSpace(Pswd.Text) || string.IsNullOrWhiteSpace(Email.Text))) { if (((App)App.Current).Manager.SearchUser(Username.Text) == null) { - Regex rg = new Regex("^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$"); - if (rg.IsMatch(Email.Text)) + GeneratedRegexAttribute rg = new GeneratedRegexAttribute("^([a-zA-Z0-9_.])+@([a-zA-Z0-9_]+[.])+[a-zA-Z0-9_]{2,}$"); + if (rg.Match(Email.Text)) { - rg = new Regex("^(?=.*[A-Za-z])(?=.*[0-9@$!%*#?&])[A-Za-z-0-9@$!%*#?&]{8,}$"); - if (rg.IsMatch(Pswd.Text)) + rg = new GeneratedRegexAttribute("^(?=.*[A-Za-z])(?=.*[0-9@$!%*#?&])[A-Za-z-0-9@$!%*#?&]{8,}$"); + if (rg.Match(Pswd.Text)) { ((App)App.Current).Manager.AddUsertoUserList(new("", Username.Text, "", Email.Text, Pswd.Text)); ((App)App.Current).Manager.CurrentUser = ((App)App.Current).Manager.SearchUser(Username.Text); diff --git a/Sources/Stub/Stub.cs b/Sources/Stub/Stub.cs index 8f7db88..46a5260 100644 --- a/Sources/Stub/Stub.cs +++ b/Sources/Stub/Stub.cs @@ -7,16 +7,11 @@ namespace StimStub [ExcludeFromCodeCoverage] public class Stub : IPersistance { - public List Games - { - get { return games; } - set { games = value; } - } - private List games; + public List Games { get; set; } public Stub() { - Games = LoadGame(); + Games = LoadGame() ?? new(); } public void SaveGame(List games) From b88a31da923bbc8fd5a5cbcc7fb7a82844dfcfd7 Mon Sep 17 00:00:00 2001 From: Jade_VAN_BRABANDT Date: Tue, 6 Jun 2023 17:56:54 +0200 Subject: [PATCH 2/4] =?UTF-8?q?Les=20regexs=20marchent=20pas=20comme=20?= =?UTF-8?q?=C3=A7a=20apparament=20du=20coup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/Stim/Create.xaml.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Sources/Stim/Create.xaml.cs b/Sources/Stim/Create.xaml.cs index ebda9ff..b317763 100644 --- a/Sources/Stim/Create.xaml.cs +++ b/Sources/Stim/Create.xaml.cs @@ -12,15 +12,15 @@ public partial class Create : ContentPage private async void Creer_un_compte(object sender, EventArgs e) { Error.Clear(); - if (!(string.IsNullOrWhiteSpace(Username.Text) || string.IsNullOrWhiteSpace(Pswd.Text) || string.IsNullOrWhiteSpace(Email.Text))) + if (!string.IsNullOrWhiteSpace(Username.Text) || !string.IsNullOrWhiteSpace(Pswd.Text) || !string.IsNullOrWhiteSpace(Email.Text)) { if (((App)App.Current).Manager.SearchUser(Username.Text) == null) { - GeneratedRegexAttribute rg = new GeneratedRegexAttribute("^([a-zA-Z0-9_.])+@([a-zA-Z0-9_]+[.])+[a-zA-Z0-9_]{2,}$"); - if (rg.Match(Email.Text)) + Regex rg = new Regex("^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$"); + if (rg.IsMatch(Email.Text)) { - rg = new GeneratedRegexAttribute("^(?=.*[A-Za-z])(?=.*[0-9@$!%*#?&])[A-Za-z-0-9@$!%*#?&]{8,}$"); - if (rg.Match(Pswd.Text)) + rg = new Regex("^(?=.*[A-Za-z])(?=.*[0-9@$!%*#?&])[A-Za-z-0-9@$!%*#?&]{8,}$"); + if (rg.IsMatch(Pswd.Text)) { ((App)App.Current).Manager.AddUsertoUserList(new("", Username.Text, "", Email.Text, Pswd.Text)); ((App)App.Current).Manager.CurrentUser = ((App)App.Current).Manager.SearchUser(Username.Text); From cf895b870f301091af8aa85d661ca9468efc47ef Mon Sep 17 00:00:00 2001 From: Jade_VAN_BRABANDT Date: Tue, 6 Jun 2023 18:24:04 +0200 Subject: [PATCH 3/4] feat : coverage --- Sources/Stim.Model/Game.cs | 2 +- Sources/Stim.Model/Manager.cs | 6 ++++-- Sources/TestProject1/TestManager.cs | 28 +++++++++++++++++++++++++++- Sources/TestProject1/TestUser.cs | 2 ++ 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/Sources/Stim.Model/Game.cs b/Sources/Stim.Model/Game.cs index 9cde10f..4f44a57 100644 --- a/Sources/Stim.Model/Game.cs +++ b/Sources/Stim.Model/Game.cs @@ -87,7 +87,7 @@ namespace Model } } } - private ObservableCollection tags = default!; + private ObservableCollection tags; [DataMember] public List Reviews { get; private init; } diff --git a/Sources/Stim.Model/Manager.cs b/Sources/Stim.Model/Manager.cs index 99c8552..b2ee887 100644 --- a/Sources/Stim.Model/Manager.cs +++ b/Sources/Stim.Model/Manager.cs @@ -1,4 +1,5 @@ using System.Collections.ObjectModel; +using System.Diagnostics.CodeAnalysis; using System.Linq; namespace Model @@ -24,7 +25,7 @@ namespace Model IEnumerable retList; retList = GameList; if (filterName != null) retList = retList - .Where(game => game.Name.IndexOf(filterName, StringComparison.OrdinalIgnoreCase) >= 0 + .Where(game => game.Name.Contains(filterName, StringComparison.OrdinalIgnoreCase) ); if (filterTag1 != null) retList = retList .Where(game => game.Tags != null && game.Tags.Any(tag => tag != null && tag.Contains(filterTag1, StringComparison.OrdinalIgnoreCase)) @@ -51,7 +52,7 @@ namespace Model gameList.Remove(game); mgrpersistance.SaveGame(gameList); } - + [ExcludeFromCodeCoverage] public void SaveGames() { mgrpersistance.SaveGame(gameList); @@ -64,6 +65,7 @@ namespace Model } return null; } + [ExcludeFromCodeCoverage] public void SaveUser() { mgrpersistance.SaveUser(Users); diff --git a/Sources/TestProject1/TestManager.cs b/Sources/TestProject1/TestManager.cs index c93279f..036907f 100644 --- a/Sources/TestProject1/TestManager.cs +++ b/Sources/TestProject1/TestManager.cs @@ -121,7 +121,8 @@ namespace Test compListAsList = compList.ToList(); compListAsList.Clear(); - compList = compListAsList; list = manager.FilterGames("Elden Ring", "Action", "Solo"); + compList = compListAsList; + list = manager.FilterGames("Elden Ring", "Action", "Solo"); foreach (var game in manager.GameList) { if (game.Name=="Elden Ring" && game.Tags.Any(tag => tag == "Action" && game.Tags.Any(tag => tag == "Solo"))) @@ -133,6 +134,31 @@ namespace Test } } Assert.Equal(compList, list); + list = manager.FilterGames(null, "Action", "Solo"); + compListAsList = compList.ToList(); + compListAsList.Clear(); + compList = compListAsList; + compList = compListAsList; list = manager.FilterGames("Elden Ring", "Action", null); + foreach (var game in manager.GameList) + { + if (game.Name == "Elden Ring" && game.Tags.Any(tag => tag == "Action")) + { + compListAsList = compList.ToList(); + compListAsList.Add(game); + compList = compListAsList; + break; + } + } + Assert.Equal(compList, list); + } + [Fact] + public void Search() + { + IPersistance persistance = new Stub(); + Manager manager = new(persistance); + User user = new(null, "username", "biographie", "adresse.mail@gmail.com", "Azerty123*"); + manager.AddUsertoUserList(user); + Assert.Equal(user, manager.SearchUser("username")); } } } diff --git a/Sources/TestProject1/TestUser.cs b/Sources/TestProject1/TestUser.cs index 0f65799..b554399 100644 --- a/Sources/TestProject1/TestUser.cs +++ b/Sources/TestProject1/TestUser.cs @@ -108,5 +108,7 @@ namespace Test Assert.False(user.Equals(user4 as object)); Assert.False(user.Equals(user2 as object)); } + [Fact] + public void Hashcode } } From 5b6b818c8798d46889ed146b93ea419cbe4dca9e Mon Sep 17 00:00:00 2001 From: Jade_VAN_BRABANDT Date: Tue, 6 Jun 2023 18:24:11 +0200 Subject: [PATCH 4/4] Coverage --- Sources/TestProject1/TestUser.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sources/TestProject1/TestUser.cs b/Sources/TestProject1/TestUser.cs index b554399..4f35126 100644 --- a/Sources/TestProject1/TestUser.cs +++ b/Sources/TestProject1/TestUser.cs @@ -109,6 +109,9 @@ namespace Test Assert.False(user.Equals(user2 as object)); } [Fact] - public void Hashcode + public void Hashcode() + { + User user = new("userimage", "username", "biographie", "adresse.mail@gmail.com", "Azerty123*"); + } } }