Python avancé : Gestion d'erreurs et mécanisme d'exception
Bilan jeux TDD php
1. Bilan du jeux de
coding
C. Brun
<christophe.brun.c
Introduction
Etape1 : Sujet
Classe + Ajouter
Etape 2 :
Exception
Etape 3 :
Ajouter plusieurs
valeurs
Etape 4 :
Soustraction
Etape 5 :
InitResultat
Etape 6 : Test
Résultat Après
Init
Ultimate
Bilan
Bilan du jeux de coding
C. Brun <christophe.brun.cl194@gadz.org>
Perso
Septembre 2013
2. Bilan du jeux de
coding
C. Brun
<christophe.brun.c
Introduction
Etape1 : Sujet
Classe + Ajouter
Etape 2 :
Exception
Etape 3 :
Ajouter plusieurs
valeurs
Etape 4 :
Soustraction
Etape 5 :
InitResultat
Etape 6 : Test
Résultat Après
Init
Ultimate
Bilan
Sommaire
Introduction
Etape1 : Sujet Classe + Ajouter
Etape 2 : Exception
Etape 3 : Ajouter plusieurs valeurs
Etape 4 : Soustraction
Etape 5 : InitResultat
Etape 6 : Test Résultat Après Init
Ultimate
Bilan
3. Bilan du jeux de
coding
C. Brun
<christophe.brun.c
Introduction
Etape1 : Sujet
Classe + Ajouter
Etape 2 :
Exception
Etape 3 :
Ajouter plusieurs
valeurs
Etape 4 :
Soustraction
Etape 5 :
InitResultat
Etape 6 : Test
Résultat Après
Init
Ultimate
Bilan
Introduction I
But du jeux
Introduction au TDD
Voir votre comportement sans spécifications
Voir votre façon de coder
Bilan
Confronter les résultats de chacun (anonyme)
Réagir
Evoluer
4. Bilan du jeux de
coding
C. Brun
<christophe.brun.c
Introduction
Etape1 : Sujet
Classe + Ajouter
Etape 2 :
Exception
Etape 3 :
Ajouter plusieurs
valeurs
Etape 4 :
Soustraction
Etape 5 :
InitResultat
Etape 6 : Test
Résultat Après
Init
Ultimate
Bilan
Etape 1 : Sujet Classe + Ajouter
class CalculatriceTest extends PHPUnit_Framework_TestCase {
public function testInstances()
{
new Calculatrice;
}
public function testResultatDefaultsZero()
{
$calc = new Calculatrice;
$this->assertSame(0,$calc->getResultat());
}
public function testAjouterNombre()
{
$calc = new Calculatrice;
$calc->ajouter(2);
$this->assertEquals(2,$calc->getResultat());
}
}
?>
5. Bilan du jeux de
coding
C. Brun
<christophe.brun.c
Introduction
Etape1 : Sujet
Classe + Ajouter
Etape 2 :
Exception
Etape 3 :
Ajouter plusieurs
valeurs
Etape 4 :
Soustraction
Etape 5 :
InitResultat
Etape 6 : Test
Résultat Après
Init
Ultimate
Bilan
Etape 1 : Reponse 1
class Calculatrice {
private $resultat = 0;
public function getResultat()
{
return $this->resultat;
}
public function ajouter($nombre)
{
$this->resultat=2;
}
}
?>
6. Bilan du jeux de
coding
C. Brun
<christophe.brun.c
Introduction
Etape1 : Sujet
Classe + Ajouter
Etape 2 :
Exception
Etape 3 :
Ajouter plusieurs
valeurs
Etape 4 :
Soustraction
Etape 5 :
InitResultat
Etape 6 : Test
Résultat Après
Init
Ultimate
Bilan
Etape 1 : Reponse 2
class Calculatrice {
private $resultats = 0;
function __construct() {
$this->resultats = 0;
}
function ajouter($nb) {
$this->resultats += $nb;
}
function getResultat() {
return $this->resultats;
}
}
?>
7. Bilan du jeux de
coding
C. Brun
<christophe.brun.c
Introduction
Etape1 : Sujet
Classe + Ajouter
Etape 2 :
Exception
Etape 3 :
Ajouter plusieurs
valeurs
Etape 4 :
Soustraction
Etape 5 :
InitResultat
Etape 6 : Test
Résultat Après
Init
Ultimate
Bilan
Etape 1 : Reponse 3
class Calculatrice {
protected $_result = 0;
public function __construct() {
}
public function ajouter( $nbr ) {
$this->_result += intval( $nbr );
}
public function getResultat() {
return $this->_result;
}
}
?>
8. Bilan du jeux de
coding
C. Brun
<christophe.brun.c
Introduction
Etape1 : Sujet
Classe + Ajouter
Etape 2 :
Exception
Etape 3 :
Ajouter plusieurs
valeurs
Etape 4 :
Soustraction
Etape 5 :
InitResultat
Etape 6 : Test
Résultat Après
Init
Ultimate
Bilan
Etape 1 : Reponse 4
class Calculatrice {
var $resultat;
function Calculatrice() {
$this->resultat = 0;
}
public function ajouter( $num ) {
if (!is_numeric($num)) {
throw new Exception(’"’ . $num . ’" n’est pas de type numérique.’);
}
$this->resultat += $num;
}
public function getResultat() {
return $this->resultat;
}
}
?>
9. Bilan du jeux de
coding
C. Brun
<christophe.brun.c
Introduction
Etape1 : Sujet
Classe + Ajouter
Etape 2 :
Exception
Etape 3 :
Ajouter plusieurs
valeurs
Etape 4 :
Soustraction
Etape 5 :
InitResultat
Etape 6 : Test
Résultat Après
Init
Ultimate
Bilan
Etape 1 : Reponse 5
class Calculatrice {
public function __construct() {
$this->Result = 0;
}
public function getResultat() {
return $this->Result;
}
/*
* PRIVATE
*/
private function calculer( $signe, $nb ) {
$this->Result = $this->Result . $signe . $nb;
}
/*
* OPERATION
*/
public function ajouter( $nb ) {
$nb = isset( $nb ) ? $nb : $this->Result;
self::calculer( (int)’+’, $nb );
}
}
?>
10. Bilan du jeux de
coding
C. Brun
<christophe.brun.c
Introduction
Etape1 : Sujet
Classe + Ajouter
Etape 2 :
Exception
Etape 3 :
Ajouter plusieurs
valeurs
Etape 4 :
Soustraction
Etape 5 :
InitResultat
Etape 6 : Test
Résultat Après
Init
Ultimate
Bilan
Etape 1 : Reponse 6
Class Calculatrice {
private $vResultat;
private $vInitial;
/**
* Constructeur
*/
function __construct() {
$this->setDefaut();
}
/**
* Méthode pour faire l’addition
* @param $valeur
*/
public function ajouter($valeur) {
$this->vResultat = $this->vInitial + $valeur;
}
/**
* Méthode pour faire la soustraction
* @param $valeur
*/
public function suprimer($valeur) {
$this->vResultat = $this->vInitial - $valeur;
}
/**
* Méthode pour faire la multiplication
* @param $valeur
*/
public function multiplier($valeur) {
$this->vResultat = $this->vInitial * $valeur;
}
/**
* Méthode pour faire la division
* @param $valeur
*/
public function diviser($valeur) {
$this->vResultat = $this->vInitial / $valeur;
}
/**
* Méthode pour faire la puissance 2
* @param $valeur
*/
public function exposant2($valeur) {
$this->vResultat = $valeur * $valeur;
}
11. Bilan du jeux de
coding
C. Brun
<christophe.brun.c
Introduction
Etape1 : Sujet
Classe + Ajouter
Etape 2 :
Exception
Etape 3 :
Ajouter plusieurs
valeurs
Etape 4 :
Soustraction
Etape 5 :
InitResultat
Etape 6 : Test
Résultat Après
Init
Ultimate
Bilan
Etape 2 : Exception
public function testAjouterNombre()
{
$calc = new Calculatrice;
$calc->ajouter(2);
$this->assertEquals(2,$calc->getResultat());
}
/**
* @expectedException InvalidArgumentException
*/
public function testValeurNumerique()
{
$calc = new Calculatrice;
$calc->ajouter(’un chiffre’);
}
}
?>
12. Bilan du jeux de
coding
C. Brun
<christophe.brun.c
Introduction
Etape1 : Sujet
Classe + Ajouter
Etape 2 :
Exception
Etape 3 :
Ajouter plusieurs
valeurs
Etape 4 :
Soustraction
Etape 5 :
InitResultat
Etape 6 : Test
Résultat Après
Init
Ultimate
Bilan
Etape 2 : Reponse 1
class Calculatrice {
private $resultat = 0;
public function getResultat()
{
return $this->resultat;
}
public function ajouter($nombre)
{
if ($nombre == ’un chiffre’)
throw new InvalidArgumentException;
$this->resultat=2;
}
}
?>
13. Bilan du jeux de
coding
C. Brun
<christophe.brun.c
Introduction
Etape1 : Sujet
Classe + Ajouter
Etape 2 :
Exception
Etape 3 :
Ajouter plusieurs
valeurs
Etape 4 :
Soustraction
Etape 5 :
InitResultat
Etape 6 : Test
Résultat Après
Init
Ultimate
Bilan
Etape 2 : Response 2
class Calculatrice {
private $resultats;
function __construct() {
$this->resultats = 0;
}
function ajouter($nb) {
if (!is_numeric($nb)) {
throw new InvalidArgumentException(’You must give a numeric.’);
}
$this->resultats += $nb;
}
function getResultat() {
return $this->resultats;
}
}
?>
14. Bilan du jeux de
coding
C. Brun
<christophe.brun.c
Introduction
Etape1 : Sujet
Classe + Ajouter
Etape 2 :
Exception
Etape 3 :
Ajouter plusieurs
valeurs
Etape 4 :
Soustraction
Etape 5 :
InitResultat
Etape 6 : Test
Résultat Après
Init
Ultimate
Bilan
Etape 2 : Response 3
*/
Class Calculatrice {
private $vResultat;
private $vInitial;
/**
* Constructeur
*/
function __construct() {
$this->setDefaut();
}
/**
* Méthode pour faire l’addition
* @param $valeur
*/
public function ajouter($valeur) {
if( !is_numeric($valeur) )
throw new InvalidArgumentException(’Argument non valide : ’
$this->vResultat = $this->vInitial + $valeur;
}
public function getResultat() {
$this->vInitial = $this->vResultat;
return $this->vResultat;
}
/**
* Méthode Mettre tous les valeur par defaut
*/
public function setDefaut() {
$this->vResultat = $this->vInitial = 0;
}
}
?>
15. Bilan du jeux de
coding
C. Brun
<christophe.brun.c
Introduction
Etape1 : Sujet
Classe + Ajouter
Etape 2 :
Exception
Etape 3 :
Ajouter plusieurs
valeurs
Etape 4 :
Soustraction
Etape 5 :
InitResultat
Etape 6 : Test
Résultat Après
Init
Ultimate
Bilan
Etape 3 : Ajouter plusieurs valeurs
/**
* @expectedException InvalidArgumentException
*/
public function testValeurNumerique()
{
$calc = new Calculatrice;
$calc->ajouter(’un chiffre’);
}
public function testAjouterPlusieursValeurs()
{
$calc = new Calculatrice;
$calc->ajouter(1,2,3,4,5);
$this->assertEquals(15,$calc->getResultat());
}
}
?>
16. Bilan du jeux de
coding
C. Brun
<christophe.brun.c
Introduction
Etape1 : Sujet
Classe + Ajouter
Etape 2 :
Exception
Etape 3 :
Ajouter plusieurs
valeurs
Etape 4 :
Soustraction
Etape 5 :
InitResultat
Etape 6 : Test
Résultat Après
Init
Ultimate
Bilan
Etape 3 : Response 1
class Calculatrice {
private $resultats;
function __construct() {
$this->resultats = 0;
}
function ajouter() {
$numberList = func_get_args();
foreach ($numberList as $key => $number) {
if (!is_numeric($number)) {
throw new InvalidArgumentException(’You must provide a number for the argument #’.($key+1));
}
$this->resultats += $number;
}
}
function getResultat() {
return $this->resultats;
}
}
?>
17. Bilan du jeux de
coding
C. Brun
<christophe.brun.c
Introduction
Etape1 : Sujet
Classe + Ajouter
Etape 2 :
Exception
Etape 3 :
Ajouter plusieurs
valeurs
Etape 4 :
Soustraction
Etape 5 :
InitResultat
Etape 6 : Test
Résultat Après
Init
Ultimate
Bilan
Etape 3 : Response 2
class Calculatrice {
protected $_result = 0;
public function __construct() {
}
public function ajouter( ) {
$numargs = func_num_args();
if( $numargs == 0 ) {
throw new InvalidArgumentException();
}
$arg_list = func_get_args();
foreach( $arg_list as $nbr ) {
if( !is_numeric( $nbr ) ) {
throw new InvalidArgumentException();
}
$this->_result += $nbr;
}
}
public function getResultat() {
return $this->_result;
}
}
?>
18. Bilan du jeux de
coding
C. Brun
<christophe.brun.c
Introduction
Etape1 : Sujet
Classe + Ajouter
Etape 2 :
Exception
Etape 3 :
Ajouter plusieurs
valeurs
Etape 4 :
Soustraction
Etape 5 :
InitResultat
Etape 6 : Test
Résultat Après
Init
Ultimate
Bilan
Etape 3 : Response 3
*/
Class Calculatrice {
private $vResultat;
private $vInitial;
/**
* Constructeur
*/
function __construct() {
$this->setDefaut();
}
/**
* Méthode pour faire l’addition
* @param $valeur
*/
public function ajouter() {
$args = func_get_args();
$valeur = 0;
foreach($args as $arg)
{
if( !is_numeric($arg) ) {
throw new InvalidArgumentException(’Argument non valide : ’
}
$valeur += $arg;
}
$this->vResultat = $this->vInitial + $valeur;
}
public function getResultat() {
$this->vInitial = $this->vResultat;
return $this->vResultat;
}
/**
* Méthode Mettre tous les valeur par defaut
*/
public function setDefaut() {
$this->vResultat = $this->vInitial = 0;
}
}
?>
19. Bilan du jeux de
coding
C. Brun
<christophe.brun.c
Introduction
Etape1 : Sujet
Classe + Ajouter
Etape 2 :
Exception
Etape 3 :
Ajouter plusieurs
valeurs
Etape 4 :
Soustraction
Etape 5 :
InitResultat
Etape 6 : Test
Résultat Après
Init
Ultimate
Bilan
Etape 4 : soustraire
public function testSoustraire()
{
$this->calc->soustraire(4);
$this->assertEquals(-4,$this->calc->getResultat());
}
/**
* @expectedException InvalidArgumentException
*/
public function testValeurNumeriqueSoustraction()
{
$this->calc->soustraire(’bonjour’);
}
public function testSoustrairePlusieursValeurs()
{
$this->calc->soustraire(4,2);
$this->assertEquals(-6,$this->calc->getResultat());
}
}
?>
20. Bilan du jeux de
coding
C. Brun
<christophe.brun.c
Introduction
Etape1 : Sujet
Classe + Ajouter
Etape 2 :
Exception
Etape 3 :
Ajouter plusieurs
valeurs
Etape 4 :
Soustraction
Etape 5 :
InitResultat
Etape 6 : Test
Résultat Après
Init
Ultimate
Bilan
Etape 4 : Response 1
class Calculatrice {
const ERROR_VAL_NOT_NUMERIC = "La valeur à ajouter n’est pas numérique";
private $resultat;
function __construct ()
{
$this->resultat = 0;
}
public function getResultat()
{
return $this->resultat;
}
public function ajouter()
{
foreach(func_get_args() as $nombre){
if (!is_numeric($nombre)) {
throw new InvalidArgumentException(self::ERROR_VAL_NOT_NUMERIC);
}
else {
$this->resultat += $nombre;
}
}
}
public function soustraire()
{
foreach(func_get_args() as $nombre){
if (!is_numeric($nombre)) {
throw new InvalidArgumentException(self::ERROR_VAL_
}
else {
$this->resultat -= $nombre;
}
}
}
}
?>
21. Bilan du jeux de
coding
C. Brun
<christophe.brun.c
Introduction
Etape1 : Sujet
Classe + Ajouter
Etape 2 :
Exception
Etape 3 :
Ajouter plusieurs
valeurs
Etape 4 :
Soustraction
Etape 5 :
InitResultat
Etape 6 : Test
Résultat Après
Init
Ultimate
Bilan
Etape 4 : Response 2
class Calculatrice {
private $resultats;
function __construct() {
$this->resultats = 0;
}
function getResultat() {
return $this->resultats;
}
function operate($numberList, $operation) {
foreach ($numberList as $key => $number) {
if (!is_numeric($number)) {
throw new InvalidArgumentException(’You must provide a number for the argument #’.($key+1));
}
switch ($operation) {
case ’add’:
$this->resultats += $number;
break;
case ’rem’:
$this->resultats -= $number;
break;
}
}
}
function ajouter() {
call_user_func_array(array($this, "operate"), array(func_get_a
}
function soustraire() {
call_user_func_array(array($this, "operate"), array(func_get_a
}
}
?>
22. Bilan du jeux de
coding
C. Brun
<christophe.brun.c
Introduction
Etape1 : Sujet
Classe + Ajouter
Etape 2 :
Exception
Etape 3 :
Ajouter plusieurs
valeurs
Etape 4 :
Soustraction
Etape 5 :
InitResultat
Etape 6 : Test
Résultat Après
Init
Ultimate
Bilan
Etape 4 : Response 3
class Calculatrice {
private $resultat;
function __construct() {
$this->resultat = 0;
}
// Effectue l’opération $operation pour chacun des arguments $args
private function effectuerOperation( $operation, $args ) {
foreach( $args as $num ) {
if (!is_numeric($num)) {
throw new InvalidArgumentException(’"’ . $num . ’" n’est pas de type numérique.’);
}
$this->$operation($num);
}
}
/*************************************************
* OPERATIONS
*************************************************/
private function addition( $num ) {
$this->resultat += $num;
}
private function soustraction( $num ) {
$this->resultat -= $num;
}
/*************************************************
* INTERFACE
*************************************************/
public function ajouter() {
$this->effectuerOperation( ’addition’, func_get_args() );
}
public function soustraire() {
$this->effectuerOperation( ’soustraction’, func_get_args() );
}
public function getResultat() {
return $this->resultat;
}
}
?>
23. Bilan du jeux de
coding
C. Brun
<christophe.brun.c
Introduction
Etape1 : Sujet
Classe + Ajouter
Etape 2 :
Exception
Etape 3 :
Ajouter plusieurs
valeurs
Etape 4 :
Soustraction
Etape 5 :
InitResultat
Etape 6 : Test
Résultat Après
Init
Ultimate
Bilan
Etape 5 : InitResultat
{
$this->calc->soustraire(’bonjour’);
}
public function testSoustrairePlusieursValeurs()
{
$this->calc->soustraire(4,2);
$this->assertEquals(-6,$this->calc->getResultat());
}
public function testInitResultat()
{
$this->assertEquals(4,$this->calc->initResultat(4));
}
}
?>
24. Bilan du jeux de
coding
C. Brun
<christophe.brun.c
Introduction
Etape1 : Sujet
Classe + Ajouter
Etape 2 :
Exception
Etape 3 :
Ajouter plusieurs
valeurs
Etape 4 :
Soustraction
Etape 5 :
InitResultat
Etape 6 : Test
Résultat Après
Init
Ultimate
Bilan
Etape 6 : Test Resultat Apres Init
}
public function testResultatApresInit()
{
$this->calc->initResultat(6);
$this->assertEquals(6,$this->calc->getResultat());
}
}
?>
25. Bilan du jeux de
coding
C. Brun
<christophe.brun.c
Introduction
Etape1 : Sujet
Classe + Ajouter
Etape 2 :
Exception
Etape 3 :
Ajouter plusieurs
valeurs
Etape 4 :
Soustraction
Etape 5 :
InitResultat
Etape 6 : Test
Résultat Après
Init
Ultimate
Bilan
Niveau Bonus
Toujours TDD
Refactoring ?
26. Bilan du jeux de
coding
C. Brun
<christophe.brun.c
Introduction
Etape1 : Sujet
Classe + Ajouter
Etape 2 :
Exception
Etape 3 :
Ajouter plusieurs
valeurs
Etape 4 :
Soustraction
Etape 5 :
InitResultat
Etape 6 : Test
Résultat Après
Init
Ultimate
Bilan
Un bilan ? ? ?
TDD c’est bien ?
TDD c’est facile ?
Mauvais / Bon coding ? ? ? ?
Il ne faut pas aller trop et/ou pas assez loin (pas
toujours)
Le refactoring ?
CodeToTrash ?
BabyTask ?
guard ? qui ?
27. Bilan du jeux de
coding
C. Brun
<christophe.brun.c
Introduction
Etape1 : Sujet
Classe + Ajouter
Etape 2 :
Exception
Etape 3 :
Ajouter plusieurs
valeurs
Etape 4 :
Soustraction
Etape 5 :
InitResultat
Etape 6 : Test
Résultat Après
Init
Ultimate
Bilan
Et Maintenant