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 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(); Assert.NotNull(joueurCourantAvant); Assert.Equal(0, joueurCourantAvant); regles.PasserLaMain(); int? joueurCourantApres = (int?)fieldInfo.GetValue(regles); Joueur courantApres = regles.JoueurCourant(); 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(); 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().Plateau; 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().Plateau.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().Plateau; 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().Plateau.AjouterCode(codeSecret); IEnumerable gagnants = regles.Gagnants(); Assert.Single(gagnants); Assert.Contains(regles.JoueurCourant(), gagnants); } } }