Repository Public MIT Research preview
1"""Tests de la démonstration S13 : chaque chiffre affiché dans les diapositives et les figures est vérifié ici."""2from __future__ import annotations34import subprocess5import sys6from pathlib import Path78SESSION = Path(__file__).resolve().parents[2]9sys.path.insert(0, str(SESSION / "demos" / "src"))1011from atlas_s13.evaluation import (compter_lignes, decider, diff_unifie, tout_evaluer, # noqa: E40212 valeur_de)13from atlas_s13.projet import CORRECTIFS, DEPART, PERIMETRE_TACHE_1, VERSIONS, contenu, fichiers # noqa: E4021415BILANS = {b.nom: b for b in tout_evaluer()}16SCRIPTS = SESSION / "demos" / "src" / "atlas_s13"171819def test_cinq_versions_dans_l_ordre():20 assert [v.nom for v in VERSIONS] == ["départ", "correctif 1", "correctif 2", "correctif 3", "corrigé"]21 assert list(BILANS) == [v.nom for v in VERSIONS]22 assert PERIMETRE_TACHE_1 == {"calculatrice/operations.py"}232425def test_point_de_depart():26 b = BILANS["départ"]27 assert b.fichiers_modifies == () and (b.lignes_ajoutees, b.lignes_retirees) == (0, 0)28 assert b.livres == (3, 5) # diapositive S13-D22 : 5 tests fournis, 3 réussis au départ29 assert b.reference == (0, 3)30 assert b.verdict == "INCOMPLET"313233def test_correctif_1_reussit_les_tests_fournis_mais_contredit_la_specification():34 b = BILANS["correctif 1"]35 assert b.fichiers_modifies == ("calculatrice/operations.py",) and b.hors_perimetre == ()36 assert (b.lignes_ajoutees, b.lignes_retirees) == (3, 1)37 assert b.livres == (5, 5) # les tests fournis ne voient rien : S13-D2438 assert b.reference == (2, 3) and b.echecs_reference == ("test_division_non_entiere",)39 assert (b.verdict, b.motif) == ("REFUSER", "échoue à un test de référence")404142def test_correctif_2_est_juste_mais_sort_du_perimetre():43 b = BILANS["correctif 2"]44 assert b.fichiers_modifies == ("calculatrice/operations.py", "tests/test_operations.py")45 assert b.hors_perimetre == ("tests/test_operations.py",)46 assert (b.lignes_ajoutees, b.lignes_retirees) == (5, 3)47 assert b.livres == (5, 5) and b.reference == (3, 3) # aucun test ne le trahit : seul le diff le montre48 assert (b.verdict, b.motif) == ("REFUSER", "modifie un fichier hors périmètre")495051def test_correctif_3_est_acceptable():52 b = BILANS["correctif 3"]53 assert b.fichiers_modifies == ("calculatrice/operations.py",) and b.hors_perimetre == ()54 assert (b.lignes_ajoutees, b.lignes_retirees) == (3, 1)55 assert b.livres == (5, 5) and b.reference == (3, 3)56 assert (b.verdict, b.motif) == ("ACCEPTER", "périmètre respecté, tests réussis")575859def test_corrige():60 b = BILANS["corrigé"]61 assert len(b.fichiers_modifies) == 4 and b.hors_perimetre is None62 assert b.livres == (17, 17) and b.reference == (3, 3)63 assert b.verdict == "RÉFÉRENCE"646566def test_un_seul_correctif_est_accepte():67 assert [b.verdict for b in BILANS.values() if b.hors_perimetre is not None] == ["REFUSER", "REFUSER", "ACCEPTER"]686970def test_le_piege_de_la_division_entiere():71 """diviser(7, 2) : 3 avec le correctif 1, 3.5 avec les deux autres (diapositives S13-D02, S13-D24)."""72 assert [valeur_de(v, "diviser(7, 2)") for v in CORRECTIFS] == ["3", "3.5", "3.5"]73 assert [valeur_de(v, "diviser(8, 2)") for v in CORRECTIFS] == ["4", "4.0", "4.0"]74 assert valeur_de(VERSIONS[0], "diviser(7, 2)") == "NotImplementedError"757677def test_regle_de_verdict():78 assert decider((), (5, 5), (3, 3))[0] == "ACCEPTER"79 assert decider(("x.py",), (5, 5), (3, 3)) == ("REFUSER", "modifie un fichier hors périmètre")80 assert decider((), (4, 5), (3, 3)) == ("REFUSER", "les tests livrés échouent")81 assert decider((), (5, 5), (2, 3)) == ("REFUSER", "échoue à un test de référence")82 # Le périmètre est examiné en premier : c'est la question la moins coûteuse (figure S13-F04).83 assert decider(("x.py",), (0, 5), (0, 3))[1] == "modifie un fichier hors périmètre"848586def test_diff_deterministe_et_lisible():87 diff = diff_unifie(CORRECTIFS[0])88 assert diff == diff_unifie(CORRECTIFS[0])89 assert diff.startswith("--- a/calculatrice/operations.py\n+++ b/calculatrice/operations.py\n")90 assert "+ return a // b" in diff and "- raise NotImplementedError" in diff # diapositive S13-D2391 assert compter_lignes(diff) == (3, 1)92 assert diff_unifie(VERSIONS[0]) == ""939495def test_les_correctifs_ne_different_du_depart_que_par_diviser():96 depart = fichiers(DEPART)97 for version in CORRECTIFS:98 apres = contenu(version)99 assert set(apres) == set(depart)100 avant_lignes = depart["calculatrice/operations.py"].splitlines()101 apres_lignes = apres["calculatrice/operations.py"].splitlines()102 debut = avant_lignes.index("def diviser(a: float, b: float) -> float:")103 assert avant_lignes[:debut + 2] == apres_lignes[:debut + 2]104 assert avant_lignes[-5:] == apres_lignes[-5:] # moyenne reste à écrire105106107def test_demo_s_execute_et_affiche_les_lignes_des_diapositives():108 sortie = subprocess.run([sys.executable, str(SCRIPTS / "demo.py")], capture_output=True, text=True, check=True).stdout109 for ligne in (" départ 0 0 0 -",110 " correctif 1 1 3 1 aucun",111 " correctif 2 2 5 3 tests/test_operations.py",112 " correctif 1 5/5 2/3 échec : test_division_non_entiere",113 " correctif 2 5/5 3/3",114 " corrigé 17/17 3/3",115 " correctif 1 REFUSER échoue à un test de référence",116 " correctif 2 REFUSER modifie un fichier hors périmètre",117 " correctif 3 ACCEPTER périmètre respecté, tests réussis",118 " correctif 1 -> 3",119 " Les 5 tests fournis réussissent pourtant avec les trois correctifs."):120 assert ligne in sortie, ligne121 assert max(len(ligne) for ligne in sortie.splitlines()) <= 80122 readme = (SESSION / "demos" / "README.md").read_text(encoding="utf-8")123 assert sortie.strip() in readme124125126def test_diffs_et_donnees_de_figure_a_jour():127 for script in ("generer_diffs.py", "exporter_donnees_figure.py"):128 assert subprocess.run([sys.executable, str(SCRIPTS / script), "--check"]).returncode == 0, script129130131def test_donnees_de_figure_contiennent_les_valeurs_evaluees():132 donnees = (SESSION / "figures" / "src" / "S13-F05-donnees.tex").read_text(encoding="utf-8")133 for attendu in (r"\def\AtlasFigUnLivres{5/5}%", r"\def\AtlasFigUnReference{2/3}%", r"\def\AtlasFigUnAccepte{0}%",134 r"\def\AtlasFigDeuxHors{1}%", r"\def\AtlasFigDeuxReference{3/3}%", r"\def\AtlasFigDeuxAccepte{0}%",135 r"\def\AtlasFigTroisPerimetreOk{1}%", r"\def\AtlasFigTroisAccepte{1}%"):136 assert attendu in donnees, attendu137 assert all(ligne.endswith("%") for ligne in donnees.splitlines())138