using CoreLibrary; using CoreLibrary.Core; using CoreLibrary.Exceptions; using CoreLibrary.Joueurs; using CoreLibrary.Regles; using System.Reflection; using Xunit; namespace UnitTesting { public class ReglesClassiquesUT { [Fact] public void TestNom() { Assert.Equal("Règles classiques", new ReglesClassiques().Nom); } [Fact] public void TestNbJoueur() { ReglesClassiques regles = new ReglesClassiques(); Assert.Equal(0, regles.NbJoueurs); regles.AjouterJoueur("Bonjour"); Assert.Equal(1, regles.NbJoueurs); regles.AjouterJoueur("Bonsoir"); Assert.Equal(2, regles.NbJoueurs); } [Fact] public void GenererCode() { ReglesClassiques regles = new ReglesClassiques(); Assert.Equal(new Code(4).NbJetons, regles.GenererCode().NbJetons); Assert.Equal(4, regles.GenererCode().TailleMaximale()); } [Fact] public void TestJoueurCourantPartieNonCommencee() { ReglesClassiques regles = new ReglesClassiques(); Assert.Throws(() => regles.JoueurCourant()); } [Fact] public void TestPasserLaMainPartieNonCommencee() { ReglesClassiques regles = new ReglesClassiques(); Assert.Throws(() => regles.PasserLaMain()); } [Fact] public void TestPasserLaMain() { ReglesClassiques regles = new ReglesClassiques(); Type type = typeof(ReglesClassiques); FieldInfo? fieldInfo = type.GetField("joueurCourant", BindingFlags.NonPublic | BindingFlags.Instance); Assert.NotNull(fieldInfo); regles.AjouterJoueur("céleste"); regles.AjouterJoueur("pauline"); Assert.Throws(() => regles.PasserLaMain()); int? joueurCourantPasDemarree = (int?)fieldInfo.GetValue(regles); Assert.Null(joueurCourantPasDemarree); regles.CommencerLaPartie(); int? joueurCourantAvant = (int?) fieldInfo.GetValue(regles); Joueur courantAvant = regles.JoueurCourant().Item1; Assert.NotNull(joueurCourantAvant); Assert.Equal(0, joueurCourantAvant); regles.PasserLaMain(); int? joueurCourantApres = (int?)fieldInfo.GetValue(regles); Joueur courantApres = regles.JoueurCourant().Item1; Assert.NotNull(joueurCourantApres); Assert.Equal(1, joueurCourantApres); Assert.NotEqual(joueurCourantAvant, joueurCourantApres); Assert.NotEqual(courantAvant, courantApres); regles.PasserLaMain(); int? joueurCourantBoucle = (int?)fieldInfo.GetValue(regles); Joueur courantBoucle = regles.JoueurCourant().Item1; Assert.NotNull(joueurCourantBoucle); Assert.Equal(0, joueurCourantBoucle); Assert.NotEqual(joueurCourantApres, joueurCourantBoucle); Assert.Equal(joueurCourantAvant, joueurCourantBoucle); Assert.NotEqual(courantApres, courantBoucle); } [Fact] public void TestEstTermineeVictoire() { ReglesClassiques regles = new ReglesClassiques(); regles.AjouterJoueur("joueur1"); regles.AjouterJoueur("joueur2"); Assert.False(regles.EstTerminee()); Type type = typeof(ReglesClassiques); FieldInfo? fieldInfo = type.GetField("joueurCourant", BindingFlags.NonPublic | BindingFlags.Instance); Assert.NotNull(fieldInfo); fieldInfo.SetValue(regles, 1); Assert.False(regles.EstTerminee()); fieldInfo.SetValue(regles, 0); regles.CommencerLaPartie(); regles.PasserLaMain(); Assert.False(regles.EstTerminee()); regles.PasserLaMain(); Plateau plateauj1 = regles.JoueurCourant().Item2; type = typeof(Plateau); fieldInfo = type.GetField("codeSecret", BindingFlags.NonPublic | BindingFlags.Instance); Assert.NotNull(fieldInfo); Code? codeSecret = (Code?)fieldInfo.GetValue(plateauj1); Assert.NotNull(codeSecret); regles.JoueurCourant().Item2.AjouterCode(codeSecret); bool estTerminee = regles.EstTerminee(); Assert.True(estTerminee); } [Fact] public void TestGagants() { ReglesClassiques regles = new ReglesClassiques(); Partie partie = new Partie(regles); regles.AjouterJoueur("joueur1"); regles.AjouterJoueur("joueur2"); regles.CommencerLaPartie(); Plateau plateauj1 = regles.JoueurCourant().Item2; Type type = typeof(Plateau); FieldInfo? fieldInfo = type.GetField("codeSecret", BindingFlags.NonPublic | BindingFlags.Instance); Assert.NotNull(fieldInfo); Code? codeSecret = (Code?)fieldInfo.GetValue(plateauj1); Assert.NotNull(codeSecret); regles.JoueurCourant().Item2.AjouterCode(codeSecret); IEnumerable gagnants = regles.Gagnants(); Assert.Single(gagnants); Assert.Contains(regles.JoueurCourant().Item1, gagnants); } [Fact] public void TestPerdants() { ReglesClassiques regles = new ReglesClassiques(); Partie partie = new Partie(regles); regles.AjouterJoueur("joueur1"); regles.AjouterJoueur("joueur2"); regles.CommencerLaPartie(); Plateau plateauj1 = regles.JoueurCourant().Item2; Type type = typeof(Plateau); FieldInfo? fieldInfo = type.GetField("codeSecret", BindingFlags.NonPublic | BindingFlags.Instance); Assert.NotNull(fieldInfo); Code? codeSecret = (Code?)fieldInfo.GetValue(plateauj1); Assert.NotNull(codeSecret); regles.JoueurCourant().Item2.AjouterCode(codeSecret); regles.PasserLaMain(); IEnumerable perdants = regles.Perdants(); Assert.Single(perdants); Assert.Contains(regles.JoueurCourant().Item1, perdants); } [Fact] public void EstTermineeMaxTour() { ReglesClassiques regles = new ReglesClassiques(); regles.AjouterJoueur("1"); regles.AjouterJoueur("2"); regles.CommencerLaPartie(); for (int i = 0; i < 24; ++i) { Plateau plateau = regles.JoueurCourant().Item2; Code code = new Code(4); code.AjouterJeton(new Jeton(Couleur.ROUGE)); code.AjouterJeton(new Jeton(Couleur.ROUGE)); code.AjouterJeton(new Jeton(Couleur.ROUGE)); code.AjouterJeton(new Jeton(Couleur.ROUGE)); plateau.AjouterCode(code); regles.PasserLaMain(); } Assert.True(regles.EstTerminee()); } [Fact] public void EstTermineeNon() { ReglesClassiques regles = new ReglesClassiques(); regles.AjouterJoueur("1"); regles.AjouterJoueur("2"); regles.CommencerLaPartie(); for (int i = 0; i < 12; ++i) regles.PasserLaMain(); Assert.False(regles.EstTerminee()); } [Fact] public void EstTermineeCodeTrouve() { ReglesClassiques regles = new ReglesClassiques(); regles.AjouterJoueur("1"); regles.AjouterJoueur("2"); regles.CommencerLaPartie(); Plateau plateauj1 = regles.JoueurCourant().Item2; Type type = typeof(Plateau); FieldInfo? fieldInfo = type.GetField("codeSecret", BindingFlags.NonPublic | BindingFlags.Instance); Assert.NotNull(fieldInfo); Code? codeSecret = (Code?)fieldInfo.GetValue(plateauj1); Assert.NotNull(codeSecret); regles.JoueurCourant().Item2.AjouterCode(codeSecret); Assert.True(regles.EstTerminee()); } } }