#!/usr/bin/env python # Copyright 2023 Louis Paternault # # This is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero Public License for more details. # # You should have received a copy of the GNU Affero Public License # along with this. If not, see . import textwrap from IPython.display import HTML TABLEAU_TETE = """ title """ TABLEAU_LIGNE_OK = textwrap.dedent( """\ """ ) TABLEAU_LIGNE_ERREUR = textwrap.dedent( """\ """ ) TABLEAU_LIGNE_EXCEPTION = textwrap.dedent( """\ """ ) TABLEAU_PIED = """
AppelRésultats
attendus
Résultats
obtenus
{fonction}({arguments}) {attendu} {obtenu}
{fonction}({arguments}) {attendu} {obtenu}
{fonction}({arguments}) {attendu} Erreur : {obtenu}
""" def check(fonction, fixtures, *, traitement=None): if traitement is None: traitement = lambda x: x text = TABLEAU_TETE for args, attendu in fixtures: try: obtenu = fonction(*args) if traitement(obtenu) == traitement(attendu): text += TABLEAU_LIGNE_OK.format( fonction=fonction.__name__, arguments=", ".join(map(str, args)), attendu=attendu, obtenu=obtenu, ) else: text += TABLEAU_LIGNE_ERREUR.format( fonction=fonction.__name__, arguments=", ".join(map(str, args)), attendu=attendu, obtenu=obtenu, ) except Exception as erreur: text += TABLEAU_LIGNE_EXCEPTION.format( fonction=fonction.__name__, arguments=", ".join(map(str, args)), attendu=attendu, obtenu=str(erreur), ) return HTML(text + TABLEAU_PIED) if __name__ == "__main__": def tteesstt(a, b): return abs(a / b) print( check( tteesstt, ( ((1, 2), 0.5), ((3, 4), 0.75), ((3, 0), 0), ((-1, 1), -1), ), ) )