Created
April 11, 2018 08:00
-
-
Save BernardNotarianni/f89aeb09321c24ac64e351994f18a55b to your computer and use it in GitHub Desktop.
premier increment de refactoring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | |
import Test.Hspec | |
main :: IO () | |
main = hspec spec | |
newtype Montant = Montant Int | |
deriving (Eq, Ord, Enum, Num) | |
instance Show Montant where | |
show (Montant m) = show m | |
historiqueVide :: [a] | |
historiqueVide = [] | |
data Compte = Compte { historique :: [ Operation ] } | |
nouveauCompte :: Compte | |
nouveauCompte = Compte historiqueVide | |
data Operation = Depot Montant | |
deriving (Eq, Show) | |
operation :: Operation -> Compte -> Compte | |
operation op compte = compte { historique = op : historique compte } | |
solde :: Compte -> Montant | |
solde (Compte historique) = sum $ fmap montant historique | |
where | |
montant (Depot m) = m | |
releveCompteExemple = | |
[ "date || credit || debit || balance" | |
, "14/01/2012 || || 500.00 || 2500.00" | |
, "13/01/2012 || 2000.00 || || 3000.00" | |
, "10/01/2012 || 1000.00 || || 1000.00" | |
] | |
releve :: Compte -> [ ( String, String) ] | |
releve (Compte historique) = | |
("Montant", "Solde") : formatte (montantsAvecSolde historique) | |
where | |
formatte = fmap ligne | |
ligne (m, s) = (show m , show (m+s)) | |
montantsAvecSolde operations = fmap prendMontantAvecSolde | |
prendMontantAvecSolde (Depot m) = (m, 0) | |
spec :: Spec | |
spec = describe "Functional Calisthenics" $ do | |
let depose10 = operation $ Depot 10 | |
depose20 = operation $ Depot 20 | |
describe "Depot sur un compte" $ do | |
it "le solde initial d'un compte est 0" $ | |
solde nouveauCompte `shouldBe` 0 | |
it "quand on operation 10 sur un nouveau compte, le solde de 10" $ | |
solde (depose10 nouveauCompte) `shouldBe` 10 | |
it "quand on operation 10 sur un compte avec 20, le solde de 30" $ | |
solde (depose10 . depose20 $ nouveauCompte) `shouldBe` 30 | |
describe "Releve" $ do | |
it "quand on depose 5 le releve de compte indique montant: 5 solde: 5" $ | |
releve ( depose10 nouveauCompte) `shouldBe` [ ("Montant","Solde") | |
, ("10","10") | |
] | |
it "quand on depose 5 puis 10 le releve de compte donne 2 lignes" $ | |
releve (operation (Depot 10) (operation (Depot 5) nouveauCompte)) | |
`shouldBe` [ ("Montant","Solde") | |
, ("5","5") | |
, ("10","15") | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment