packages
The Louise packages, each released on its own
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#33 · main · 4 min 05 s
0Étoiles
0Forks
3Issues
| 1 | // Property checks find a failing case and shrink it to a small one, the |
| 2 | // same for a seed. |
| 3 | |
| 4 | import check |
| 5 | import gen |
| 6 | import std/list |
| 7 | import std/random.{type random} |
| 8 | import std/string |
| 9 | import std/test.{assert, assert_equal} |
| 10 | |
| 11 | fn sums_of_lists :: -> List<Int> / <random> |
| 12 | { |
| 13 | gen.list ~min: 0 ~max: 10 fn { gen.int ~low: 0 ~high: 100 } |
| 14 | } |
| 15 | |
| 16 | fn small_sum :: xs: List<Int> -> Bool |
| 17 | { |
| 18 | list.fold xs 0 fn a b { a + b } <= 10 |
| 19 | } |
| 20 | |
| 21 | fn texts :: -> String / <random> |
| 22 | { |
| 23 | gen.string ~alphabet: "abc" ~min: 0 ~max: 10 |
| 24 | } |
| 25 | |
| 26 | fn has_no_a :: text: String -> Bool |
| 27 | { |
| 28 | !string.contains text "a" |
| 29 | } |
| 30 | |
| 31 | @test |
| 32 | fn a_property_that_holds_has_no_counterexample :: |
| 33 | { |
| 34 | assert_equal |
| 35 | ( check.counterexample |
| 36 | ~seed: 1 |
| 37 | ~runs: 100 |
| 38 | sums_of_lists |
| 39 | fn xs { list.length xs <= 10 } |
| 40 | ) |
| 41 | None |
| 42 | } |
| 43 | |
| 44 | @test |
| 45 | fn a_failing_list_shrinks_to_one_item_just_over_the_limit :: |
| 46 | { |
| 47 | assert_equal |
| 48 | (check.counterexample ~seed: 1 ~runs: 100 sums_of_lists small_sum) |
| 49 | Some([11]) |
| 50 | } |
| 51 | |
| 52 | @test |
| 53 | fn a_failing_string_shrinks_to_the_one_character_that_breaks_it :: |
| 54 | { |
| 55 | assert_equal |
| 56 | (check.counterexample ~seed: 2 ~runs: 100 texts has_no_a) |
| 57 | Some("a") |
| 58 | } |
| 59 | |
| 60 | @test |
| 61 | fn a_seed_finds_the_same_case :: |
| 62 | { |
| 63 | let first = check.counterexample ~seed: 9 ~runs: 50 texts has_no_a |
| 64 | assert_equal (check.counterexample ~seed: 9 ~runs: 50 texts has_no_a) first |
| 65 | } |
| 66 | |
| 67 | @test |
| 68 | @should_panic "property does not hold (seed 1" |
| 69 | fn a_seeded_check_panics_with_the_seed :: |
| 70 | { |
| 71 | check.seeded ~seed: 1 ~runs: 100 sums_of_lists small_sum |
| 72 | } |
| 73 | |
| 74 | @test |
| 75 | @should_panic "[11]" |
| 76 | fn its_message_shows_the_shrunk_case :: |
| 77 | { |
| 78 | check.seeded ~seed: 1 ~runs: 100 sums_of_lists small_sum |
| 79 | } |
| 80 | |
| 81 | @test |
| 82 | fn a_property_takes_its_seed_from_random :: |
| 83 | { |
| 84 | random.with_seed |
| 85 | 4 |
| 86 | fn { check.property ~runs: 50 texts fn t { string.length t <= 10 } } |
| 87 | assert True "no panic" |
| 88 | } |