From 22a24d37a6afa35c1ac2d2870b24809cd3948af7 Mon Sep 17 00:00:00 2001 From: Arnaud Bailly Date: Fri, 31 Jan 2025 06:50:44 +0100 Subject: Added example in Haskell and some more comments --- pbt/README.md | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- pbt/ts/README.md | 1 + 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 pbt/ts/README.md diff --git a/pbt/README.md b/pbt/README.md index 308ebd9..3fcafa0 100644 --- a/pbt/README.md +++ b/pbt/README.md @@ -62,9 +62,9 @@ function findMinimalCounterExample(x, property, shrink, depth = 0) { Quelques liens intéressants glanés au fil de mes recherches sur le sujet : +* [QuickCheck](https://hackage.haskell.org/package/QuickCheck) : la bibliothèque originelle de PBT pour Haskell * [Shrinking challenge](https://github.com/jlink/shrinking-challenge) : un répertoire de problèmes intéressants pour étudier le comportement des _minimiseurs_ * [arbtest](https://github.com/matklad/arbtest) : une bibliothèque minimale de PBT pour Rust -* [QuickCheck](https://hackage.haskell.org/package/QuickCheck) : la bibliothèque originelle de PBT pour Haskell * [Hypothesis](https://hypothesis.readthedocs.io/en/latest/) : une bibliothèque de PBT pour Python * [The sad state of property-based testing](https://stevana.github.io/the_sad_state_of_property-based_testing_libraries.html) : un historique et une réflexion approfondie sur l'état de l'art du PBT, le constat que les idées et outils "universitaires" sont toujours peu utilisés dans l'industrie, et des pistes pour améliorer la situation, avec une discussion intéressante sur [lobsters](https://lobste.rs/s/uutqvn/sad_state_property_based_testing) * [Testing the hard stuff and staying sane](https://www.youtube.com/watch?v=zi0rHwfiX1Q) : une vidéo de John Hughes, l'inventeur de QuickCheck, qui explique les bases du PBT et du Model-Based Testing @@ -76,3 +76,53 @@ Quelques liens intéressants glanés au fil de mes recherches sur le sujet : * [Choosing properties for PBT](https://fsharpforfunandprofit.com/posts/property-based-testing-2/) * [PBT in practice](https://harrisongoldste.in/papers/icse24-pbt-in-practice.pdf): une étude à base d'entretiens sur l'efficacité _en pratique_ du PBT * [Integrated vs. type-based shrinking](https://hypothesis.works/articles/integrated-shrinking/) : un post du créateur d'[hypothesis](https://hypothesis.works), outil de PBT en Python, justifiant la _minimisation intégrée_ +* Une [présentation](https://slides.com/ktorz/deck-7b00fc) sur le PBT de Matthias Benkort aka. [KtorZ](https://x.com/_KtorZ_) + + +## Un exemple en Haskell + +```haskell +module Table where + +import Data.List (group) +import Test.QuickCheck ( + ASCIIString (getASCIIString), + Arbitrary (..), + PrintableString (getPrintableString), + Property, + listOf, + oneof, + vectorOf, + (===), + (==>), + ) + +data Value = IntValue Int | StringValue String | BoolValue Bool + deriving (Show) + +data Table = Table {rows :: [[Value]]} + deriving (Show) + +instance Arbitrary Value where + arbitrary = + oneof + [ IntValue <$> arbitrary + , StringValue . getPrintableString <$> arbitrary + , BoolValue <$> arbitrary + ] + +instance Arbitrary Table where + arbitrary = do + rowLength <- arbitrary + Table <$> listOf (vectorOf rowLength arbitrary) + +prop_table_are_matrices :: Table -> Property +prop_table_are_matrices (Table rows) = + (length rows > 100) ==> + let rowLengths = map length rows + in length (group rowLengths) === 1 +``` + +## Bricoler son framework de PBT + +Voir [ts](ts/README.md) diff --git a/pbt/ts/README.md b/pbt/ts/README.md new file mode 100644 index 0000000..f9e2831 --- /dev/null +++ b/pbt/ts/README.md @@ -0,0 +1 @@ +Un micro framework de PBT en node/typescript pour illustrer les principaux concepts. -- cgit v1.2.3